Skip to content

Commit

Permalink
fix(Rule): invalid dns for process rules
Browse files Browse the repository at this point in the history
  • Loading branch information
igoogolx committed Jun 15, 2024
1 parent b2abd2d commit 842c81b
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion internal/cfg/distribution/system_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 10 additions & 2 deletions internal/cfg/distribution/tun.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions internal/dns/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion internal/matcher/dns.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/tunnel/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down

0 comments on commit 842c81b

Please sign in to comment.