feat: 添加resolve方法解析dns,关闭script规则需要解析IP
This commit is contained in:
parent
9602d42d7d
commit
d578ca788c
69
component/js/function.go
Normal file
69
component/js/function.go
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
//go:build !no_script
|
||||||
|
|
||||||
|
package js
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/Dreamacro/clash/component/resolver"
|
||||||
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
"github.com/Dreamacro/clash/log"
|
||||||
|
"github.com/dop251/goja"
|
||||||
|
"github.com/dop251/goja_nodejs/require"
|
||||||
|
"net/netip"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Context struct {
|
||||||
|
runtime *goja.Runtime
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Context) Resolve(host string, dnsType C.DnsType) []string {
|
||||||
|
var ips []string
|
||||||
|
var ipAddrs []netip.Addr
|
||||||
|
var err error
|
||||||
|
switch dnsType {
|
||||||
|
case C.IPv4:
|
||||||
|
ipAddrs, err = resolver.ResolveAllIPv4(host)
|
||||||
|
case C.IPv6:
|
||||||
|
ipAddrs, err = resolver.ResolveAllIPv6(host)
|
||||||
|
case C.All:
|
||||||
|
ipAddrs, err = resolver.ResolveAllIP(host)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Errorln("Script resolve %s failed, error: %v", host, err)
|
||||||
|
return ips
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, addr := range ipAddrs {
|
||||||
|
ips = append(ips, addr.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
return ips
|
||||||
|
}
|
||||||
|
|
||||||
|
func newContext() require.ModuleLoader {
|
||||||
|
return func(runtime *goja.Runtime, object *goja.Object) {
|
||||||
|
ctx := Context{
|
||||||
|
runtime: runtime,
|
||||||
|
}
|
||||||
|
|
||||||
|
o := object.Get("exports").(*goja.Object)
|
||||||
|
o.Set("resolve", func(call goja.FunctionCall) goja.Value {
|
||||||
|
if len(call.Arguments) < 1 {
|
||||||
|
return runtime.ToValue([]string{})
|
||||||
|
}
|
||||||
|
|
||||||
|
host := call.Argument(0).String()
|
||||||
|
dnsType := C.IPv4
|
||||||
|
if len(call.Arguments) == 2 {
|
||||||
|
dnsType = int(call.Argument(1).ToInteger())
|
||||||
|
}
|
||||||
|
|
||||||
|
ips := ctx.Resolve(host, C.DnsType(dnsType))
|
||||||
|
return runtime.ToValue(ips)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func enable(rt *goja.Runtime) {
|
||||||
|
rt.Set("context", require.Require(rt, "context"))
|
||||||
|
}
|
@ -13,6 +13,8 @@ import (
|
|||||||
func init() {
|
func init() {
|
||||||
logPrinter := console.RequireWithPrinter(&JsLog{})
|
logPrinter := console.RequireWithPrinter(&JsLog{})
|
||||||
require.RegisterNativeModule("console", logPrinter)
|
require.RegisterNativeModule("console", logPrinter)
|
||||||
|
contextFuncLoader := newContext()
|
||||||
|
require.RegisterNativeModule("context", contextFuncLoader)
|
||||||
}
|
}
|
||||||
|
|
||||||
func preSetting(rt *goja.Runtime) {
|
func preSetting(rt *goja.Runtime) {
|
||||||
@ -20,6 +22,7 @@ func preSetting(rt *goja.Runtime) {
|
|||||||
registry.Enable(rt)
|
registry.Enable(rt)
|
||||||
|
|
||||||
console.Enable(rt)
|
console.Enable(rt)
|
||||||
|
enable(rt)
|
||||||
eventloop.EnableConsole(true)
|
eventloop.EnableConsole(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,16 +89,3 @@ type Rule interface {
|
|||||||
RuleExtra() *RuleExtra
|
RuleExtra() *RuleExtra
|
||||||
SetRuleExtra(re *RuleExtra)
|
SetRuleExtra(re *RuleExtra)
|
||||||
}
|
}
|
||||||
|
|
||||||
type JSMetadata struct {
|
|
||||||
Type string `json:"type"`
|
|
||||||
Network string `json:"network"`
|
|
||||||
Host string `json:"host"`
|
|
||||||
SrcIP string `json:"srcIP"`
|
|
||||||
DstIP string `json:"dstIP"`
|
|
||||||
SrcPort string `json:"srcPort"`
|
|
||||||
DstPort string `json:"dstPort"`
|
|
||||||
Uid *int32 `json:"uid"`
|
|
||||||
Process string `json:"process"`
|
|
||||||
ProcessPath string `json:"processPath"`
|
|
||||||
}
|
|
||||||
|
27
constant/script.go
Normal file
27
constant/script.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package constant
|
||||||
|
|
||||||
|
type JSRuleMetadata struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
Network string `json:"network"`
|
||||||
|
Host string `json:"host"`
|
||||||
|
SrcIP string `json:"srcIP"`
|
||||||
|
DstIP string `json:"dstIP"`
|
||||||
|
SrcPort string `json:"srcPort"`
|
||||||
|
DstPort string `json:"dstPort"`
|
||||||
|
Uid *int32 `json:"uid"`
|
||||||
|
Process string `json:"process"`
|
||||||
|
ProcessPath string `json:"processPath"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type DnsType int
|
||||||
|
|
||||||
|
const (
|
||||||
|
IPv4 = 1 << iota
|
||||||
|
IPv6
|
||||||
|
All
|
||||||
|
)
|
||||||
|
|
||||||
|
type JSFunction interface {
|
||||||
|
//Resolve host to ip by Clash DNS
|
||||||
|
Resolve(host string, resolveType DnsType) []string
|
||||||
|
}
|
@ -19,7 +19,7 @@ func (s *Script) RuleType() C.RuleType {
|
|||||||
func (s *Script) Match(metadata *C.Metadata) bool {
|
func (s *Script) Match(metadata *C.Metadata) bool {
|
||||||
res := false
|
res := false
|
||||||
js.Run(s.name, map[string]any{
|
js.Run(s.name, map[string]any{
|
||||||
"metadata": C.JSMetadata{
|
"metadata": C.JSRuleMetadata{
|
||||||
Host: metadata.Host,
|
Host: metadata.Host,
|
||||||
Network: metadata.NetWork.String(),
|
Network: metadata.NetWork.String(),
|
||||||
Type: metadata.Type.String(),
|
Type: metadata.Type.String(),
|
||||||
@ -54,7 +54,7 @@ func (s *Script) Payload() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Script) ShouldResolveIP() bool {
|
func (s *Script) ShouldResolveIP() bool {
|
||||||
return true
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewScript(script string, adapter string) (*Script, error) {
|
func NewScript(script string, adapter string) (*Script, error) {
|
||||||
|
Reference in New Issue
Block a user