From 842c81b1a959e08e1a1e23c1a145dc9b71252bc9 Mon Sep 17 00:00:00 2001 From: igoogolx <27353191+igoogolx@users.noreply.github.com> Date: Sat, 15 Jun 2024 08:59:25 +0800 Subject: [PATCH] fix(Rule): invalid dns for process rules --- internal/cfg/distribution/system_proxy.go | 2 +- internal/cfg/distribution/tun.go | 12 ++++++++++-- internal/dns/client.go | 9 +++++---- internal/matcher/dns.go | 3 ++- internal/tunnel/udp.go | 2 +- 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/internal/cfg/distribution/system_proxy.go b/internal/cfg/distribution/system_proxy.go index 7bdd5f1..38c5719 100644 --- a/internal/cfg/distribution/system_proxy.go +++ b/internal/cfg/distribution/system_proxy.go @@ -41,7 +41,7 @@ func (c SystemProxyConfig) ConnMatcher(metadata *C.Metadata, prevRule constants. return result, err } -func (c SystemProxyConfig) GetDnsType(domain string) (constants.DnsType, error) { +func (c SystemProxyConfig) GetDnsType(domain string, _ *C.Metadata) (constants.DnsType, error) { var rule, err = c.RuleEngine.Match(domain, constants.DomainRuleTypes) if err == nil { if rule.GetPolicy() == constants.PolicyDirect { diff --git a/internal/cfg/distribution/tun.go b/internal/cfg/distribution/tun.go index 23f652d..2b8d12e 100644 --- a/internal/cfg/distribution/tun.go +++ b/internal/cfg/distribution/tun.go @@ -56,8 +56,16 @@ func (c Config) ConnMatcher(metadata *C.Metadata, prevRule constants.Policy) (co return result, err } -func (c Config) GetDnsType(domain string) (constants.DnsType, error) { - var rule, err = c.RuleEngine.Match(domain, constants.DomainRuleTypes) +func (c Config) GetDnsType(domain string, metadata *C.Metadata) (constants.DnsType, error) { + processPath := metadata.ProcessPath + var rule ruleEngine.Rule + var err error + if len(processPath) != 0 { + rule, err = c.RuleEngine.Match(processPath, constants.ProcessRuleTypes) + } + if err == nil { + rule, err = c.RuleEngine.Match(domain, constants.DomainRuleTypes) + } if err == nil { if rule.GetPolicy() == constants.PolicyDirect { return constants.LocalDns, nil diff --git a/internal/dns/client.go b/internal/dns/client.go index 0b17113..e433fcb 100644 --- a/internal/dns/client.go +++ b/internal/dns/client.go @@ -4,6 +4,7 @@ import ( "context" "fmt" cResolver "github.com/Dreamacro/clash/component/resolver" + "github.com/Dreamacro/clash/constant" "github.com/igoogolx/itun2socks/internal/cfg/distribution" "github.com/igoogolx/itun2socks/internal/constants" "github.com/igoogolx/itun2socks/internal/matcher" @@ -32,7 +33,7 @@ type Conn interface { WriteTo([]byte, net.Addr) (int, error) } -func HandleDnsConn(conn Conn) error { +func HandleDnsConn(conn Conn, metadata *constant.Metadata) error { var err error data := pool.NewBytes(pool.BufSize) defer pool.FreeBytes(data) @@ -45,7 +46,7 @@ func HandleDnsConn(conn Conn) error { if err != nil { return fmt.Errorf("fail to unpack dns message: err: %v", err) } - res, err := handle(dnsMessage) + res, err := handle(dnsMessage, metadata) if err != nil { return fmt.Errorf("fail to hanlde dns message: err: %v", err) } @@ -84,7 +85,7 @@ func getResponseIp(msg *D.Msg) []net.IP { return ips } -func handle(dnsMessage *D.Msg) (*D.Msg, error) { +func handle(dnsMessage *D.Msg, metadata *constant.Metadata) (*D.Msg, error) { mux.Lock() defer mux.Unlock() ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) @@ -95,7 +96,7 @@ func handle(dnsMessage *D.Msg) (*D.Msg, error) { if err != nil { return nil, fmt.Errorf("invalid dns question, err: %v", err) } - dnsType, err := matcher.GetDnsMatcher().GetDnsType(question) + dnsType, err := matcher.GetDnsMatcher().GetDnsType(question, metadata) if err != nil { return nil, fmt.Errorf("fail to get dns type, err: %v, question: %v", err, question) } diff --git a/internal/matcher/dns.go b/internal/matcher/dns.go index eaa0809..dee341f 100644 --- a/internal/matcher/dns.go +++ b/internal/matcher/dns.go @@ -1,12 +1,13 @@ package matcher import ( + "github.com/Dreamacro/clash/constant" "github.com/igoogolx/itun2socks/internal/constants" "sync" ) type Dns interface { - GetDnsType(question string) (constants.DnsType, error) + GetDnsType(question string, metadata *constant.Metadata) (constants.DnsType, error) } var defaultDnsMatcher Dns diff --git a/internal/tunnel/udp.go b/internal/tunnel/udp.go index fa758fc..e82679a 100644 --- a/internal/tunnel/udp.go +++ b/internal/tunnel/udp.go @@ -93,7 +93,7 @@ func handleUdpConn(ct conn.UdpConnContext) { //only tun proxy if ct.Metadata().DstPort.String() == constants.DnsPort { - err = dns.HandleDnsConn(ct.Conn()) + err = dns.HandleDnsConn(ct.Conn(), ct.Metadata()) if err != nil { log.Warnln(log.FormatLog(log.UdpPrefix, "fail to handle dns conn, err: %v, remote address: %v"), err, ct.Metadata().RemoteAddress()) }