Skip to content

Commit

Permalink
v1.23.3 (#53)
Browse files Browse the repository at this point in the history
* feat(dns): ignore dns query that is not A type

* fix(dns): invalid local dns client

* chore: upgrade deps
  • Loading branch information
igoogolx authored Jan 4, 2025
1 parent a51d018 commit cbc9afd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.22.0

toolchain go1.22.5

replace github.com/Dreamacro/clash v1.18.0 => github.com/igoogolx/clash v1.19.11
replace github.com/Dreamacro/clash v1.18.0 => github.com/igoogolx/clash v1.19.12

require (
github.com/Dreamacro/clash v1.18.0
Expand Down Expand Up @@ -46,7 +46,7 @@ require (
github.com/go-stack/stack v1.8.1 // indirect
github.com/google/btree v1.1.3 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/insomniacslk/dhcp v0.0.0-20241219180459-a662cc47d412 // indirect
github.com/insomniacslk/dhcp v0.0.0-20241224095048-b56fa0d5f25d // indirect
github.com/josharian/native v1.1.0 // indirect
github.com/mdlayher/netlink v1.7.2 // indirect
github.com/mdlayher/socket v0.5.1 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aN
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/igoogolx/clash v1.19.11 h1:D80keITYMSzkptYNm1Q8U1BrUxyKpyt9k0ZnxvlpKl4=
github.com/igoogolx/clash v1.19.11/go.mod h1:mcr/HVUlxi5S7oHQjxmG+WabMIGx/IUl1YWTkiOirVc=
github.com/igoogolx/clash v1.19.12 h1:u1WaxmBwDplm4bcuaLaVAs9r6HAN/58iz4qsuSOXCxE=
github.com/igoogolx/clash v1.19.12/go.mod h1:NUvOLI+cnhLGoYPrwOd5gQoKiJVO0/8V41nWx0EUBqs=
github.com/igoogolx/sysproxy v1.0.5 h1:bR3ThpCLAo2/hJdUO3V6nsEEYdBS+yaIK4o6ezjXiVM=
github.com/igoogolx/sysproxy v1.0.5/go.mod h1:Ivr0fT7jxcG4mfruV+nNoW0IacTjENzztXcM4lQ91as=
github.com/insomniacslk/dhcp v0.0.0-20241219180459-a662cc47d412 h1:Gpj5alZpJhmJYx8Gljb+SxScp5+smvPA9SmajG4RenY=
github.com/insomniacslk/dhcp v0.0.0-20241219180459-a662cc47d412/go.mod h1:VvGYjkZoJyKqlmT1yzakUs4mfKMNB0XdODP0+rdml6k=
github.com/insomniacslk/dhcp v0.0.0-20241224095048-b56fa0d5f25d h1:VkCNWh6tuQLgDBc6KrUOz/L1mCUQGnR1Ujj8uTgpwwk=
github.com/insomniacslk/dhcp v0.0.0-20241224095048-b56fa0d5f25d/go.mod h1:VvGYjkZoJyKqlmT1yzakUs4mfKMNB0XdODP0+rdml6k=
github.com/josharian/native v1.1.0 h1:uuaP0hAbW7Y4l0ZRQ6C9zfb7Mg1mbFKry/xzDAfmtLA=
github.com/josharian/native v1.1.0/go.mod h1:7X/raswPFr05uY3HiLlYeyQntB6OO7E/d2Cu7qoaN2w=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand Down
21 changes: 13 additions & 8 deletions internal/cfg/distribution/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ func NewDnsDistribution(
defaultInterfaceName string,
disableCache bool,
) (DnsDistribution, error) {

var err error
dd := DnsDistribution{}

//Boost
var bootDnsServers []string
for _, server := range bootDns {
bootDnsServers = append(bootDnsServers, server+"#"+defaultInterfaceName)
Expand All @@ -26,22 +30,28 @@ func NewDnsDistribution(
if err != nil {
return DnsDistribution{}, err
}
dd.Boost = SubDnsDistribution{
Client: boostDnsClient,
Addresses: bootDnsServers,
}

//Local
var localDnsServers []string
for _, server := range bootDns {
localDnsServers = append(localDns, server+"#"+defaultInterfaceName)
for _, server := range localDns {
localDnsServers = append(localDnsServers, server+"#"+defaultInterfaceName)
}
localDnsClient, err := resolver.New(localDnsServers, defaultInterfaceName, func() (C.Proxy, error) {
return conn.GetProxy(constants.PolicyDirect)
}, disableCache)
if err != nil {
return DnsDistribution{}, err
}
dd := DnsDistribution{}
dd.Local = SubDnsDistribution{
Addresses: localDnsServers,
Client: localDnsClient,
}

//Remote
remoteDnsClient, err := resolver.New(remoteDns, defaultInterfaceName, func() (C.Proxy, error) {
return conn.GetProxy(constants.PolicyProxy)
}, disableCache)
Expand All @@ -53,11 +63,6 @@ func NewDnsDistribution(
Addresses: remoteDns,
}

dd.Boost = SubDnsDistribution{
Client: boostDnsClient,
Addresses: bootDnsServers,
}

cResolver.DefaultResolver = boostDnsClient
return dd, nil
}
Expand Down
25 changes: 21 additions & 4 deletions internal/dns/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,27 @@ type Conn interface {
WriteTo([]byte, net.Addr) (int, error)
}

func getDnsQuestion(msg *D.Msg) (string, string, error) {
func handleMsgWithEmptyAnswer(r *D.Msg) *D.Msg {
msg := &D.Msg{}
msg.Answer = []D.RR{}

msg.SetRcode(r, D.RcodeSuccess)
msg.Authoritative = true
msg.RecursionAvailable = true

return msg
}

func getDnsQuestion(msg *D.Msg) (string, uint16, error) {
if len(msg.Question) == 0 {
return "", "", fmt.Errorf("no dns question")
return "", D.TypeNone, fmt.Errorf("no dns question")
}
name := msg.Question[0].Name
if strings.HasSuffix(name, ".") {
name = name[:len(name)-1]
}
qType := msg.Question[0].Qtype
return name, D.TypeToString[qType], nil
return name, qType, nil
}

func getResponseIp(msg *D.Msg) []net.IP {
Expand Down Expand Up @@ -96,6 +107,12 @@ func Handle(dnsMessage *D.Msg, metadata *constant.Metadata) (*D.Msg, error) {
if err != nil {
return nil, fmt.Errorf("invalid dns question, err: %v", err)
}

switch qType {
case D.TypeAAAA, D.TypeSVCB, D.TypeHTTPS:
return handleMsgWithEmptyAnswer(dnsMessage), nil
}

dnsRule, err := getDnsResovler(question, metadata)
if err != nil {
return nil, fmt.Errorf("fail to get dns resolver, err: %v, question: %v", err, question)
Expand All @@ -114,6 +131,6 @@ func Handle(dnsMessage *D.Msg, metadata *constant.Metadata) (*D.Msg, error) {
}
}
elapsed := time.Since(start).Milliseconds()
log.Infoln(log.FormatLog(log.DnsPrefix, "target: %v, type: %v, time: %v ms, result: %v"), question, qType, elapsed, resIps)
log.Infoln(log.FormatLog(log.DnsPrefix, "target: %v, type: %v, time: %v ms, result: %v"), question, D.TypeToString[qType], elapsed, resIps)
return res, err
}

0 comments on commit cbc9afd

Please sign in to comment.