Skip to content

Commit

Permalink
Return the fastest response of lookupToExchange && Move timeout to …
Browse files Browse the repository at this point in the history
…`sing-dns`
  • Loading branch information
PuerNya committed Jul 1, 2024
1 parent 5362057 commit a3fdaa3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 22 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ require (
howett.net/plist v1.0.1
)

replace github.com/sagernet/sing-dns v0.2.0 => github.com/puernya/sing-dns v0.0.0-20240701164015-ea3006b2aef3
replace github.com/sagernet/sing-dns v0.2.0 => github.com/puernya/sing-dns v0.0.0-20240701174700-43d7ed913da8

replace github.com/sagernet/sing-tun v0.3.2 => github.com/puernya/sing-tun v0.0.0-20240613145259-aafb943eaa33

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/puernya/sing v0.0.0-20240627172524-ee861d432144 h1:talOEvqGoz1zanPj1YTRWYKK68jAlh9eoE6yio7aLJo=
github.com/puernya/sing v0.0.0-20240627172524-ee861d432144/go.mod h1:ieZHA/+Y9YZfXs2I3WtuwgyCZ6GPsIR7HdKb1SdEnls=
github.com/puernya/sing-dns v0.0.0-20240701164015-ea3006b2aef3 h1:hlIFBhCy4SE+9DZ1X4aD1rfqnwp31EysDDXaikJzXvI=
github.com/puernya/sing-dns v0.0.0-20240701164015-ea3006b2aef3/go.mod h1:BJpJv6XLnrUbSyIntOT6DG9FW0f4fETmPAHvNjOprLg=
github.com/puernya/sing-dns v0.0.0-20240701174700-43d7ed913da8 h1:MOyI5aCVvZY38aFeqjJPUAEwJG4RwZgESutFCd/mFXI=
github.com/puernya/sing-dns v0.0.0-20240701174700-43d7ed913da8/go.mod h1:BJpJv6XLnrUbSyIntOT6DG9FW0f4fETmPAHvNjOprLg=
github.com/puernya/sing-tun v0.0.0-20240613145259-aafb943eaa33 h1:Pg43kMURcQxpDEBwH7HUUE87RT6O6G6WkInwLJsTE4c=
github.com/puernya/sing-tun v0.0.0-20240613145259-aafb943eaa33/go.mod h1:DxLIyhjWU/HwGYoX0vNGg2c5QgTQIakphU1MuERR5tQ=
github.com/quic-go/qpack v0.4.0 h1:Cr9BXA1sQS2SmDUWjSofMPNKmvF6IiIfDRmgU0w1ZCo=
Expand Down
27 changes: 8 additions & 19 deletions route/router_dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"time"

"github.com/sagernet/sing-box/adapter"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-dns"
"github.com/sagernet/sing/common/cache"
Expand Down Expand Up @@ -178,13 +177,11 @@ func (r *Router) exchangeFunc(ctx context.Context, message *mDNS.Msg, isCacheUpd
for {
var (
dnsCtx context.Context
cancel context.CancelFunc
addressLimit bool
)

dnsCtx, transport, strategy, rule, ruleIndex, isFakeIP = r.matchDNS(ctx, true, ruleIndex)
isisAddrReq := isAddressQuery(message)
dnsCtx, cancel = context.WithTimeout(dnsCtx, C.DNSTimeout)
if rule != nil && rule.WithAddressLimit() && isisAddrReq {
addressLimit = true
response, err = r.dnsClient.ExchangeWithResponseCheck(dnsCtx, transport, message, strategy, isCacheUpdate, func(response *mDNS.Msg) bool {
Expand All @@ -195,17 +192,16 @@ func (r *Router) exchangeFunc(ctx context.Context, message *mDNS.Msg, isCacheUpd
addressLimit = false
response, err = r.dnsClient.Exchange(dnsCtx, transport, message, strategy, isCacheUpdate)
}
cancel()
var rejected bool
if err != nil {
if errors.Is(err, dns.ErrResponseRejectedCached) {
rejected = true
r.dnsLogger.DebugContext(ctx, E.Cause(err, "response rejected for ", formatQuestion(message.Question[0].String())), " (cached)")
r.dnsLogger.DebugContext(ctx, E.Cause(err, "response rejected for ", formatQuestion(message.Question[0].String())), " (cached)")
} else if errors.Is(err, dns.ErrResponseRejected) {
rejected = true
r.dnsLogger.DebugContext(ctx, E.Cause(err, "response rejected for ", formatQuestion(message.Question[0].String())))
r.dnsLogger.DebugContext(ctx, E.Cause(err, "response rejected for ", formatQuestion(message.Question[0].String())))
} else if len(message.Question) > 0 {
r.dnsLogger.ErrorContext(ctx, E.Cause(err, "exchange failed for ", formatQuestion(message.Question[0].String())))
r.dnsLogger.ErrorContext(ctx, E.Cause(err, "exchange failed for ", formatQuestion(message.Question[0].String())))
} else {
r.dnsLogger.ErrorContext(ctx, E.Cause(err, "exchange failed for <empty query>"))
}
Expand Down Expand Up @@ -247,9 +243,7 @@ func (r *Router) exchangeFunc(ctx context.Context, message *mDNS.Msg, isCacheUpd
if transport == nil {
continue
}
dnsCtx, cancel = context.WithTimeout(dnsCtx, C.DNSTimeout)
response, err = r.dnsClient.Exchange(dnsCtx, transport, message, strategy, isCacheUpdate)
cancel()
if isFakeIP {
break
}
Expand Down Expand Up @@ -353,13 +347,11 @@ func (r *Router) lookupFunc(ctx context.Context, domain string, strategy dns.Dom
for {
var (
dnsCtx context.Context
cancel context.CancelFunc
addressLimit bool
)
metadata.ResetRuleCache()
metadata.DestinationAddresses = nil
dnsCtx, transport, transportStrategy, rule, ruleIndex, _ = r.matchDNS(ctx, false, ruleIndex)
dnsCtx, cancel = context.WithTimeout(dnsCtx, C.DNSTimeout)
if strategy == dns.DomainStrategyAsIS {
strategy = transportStrategy
}
Expand All @@ -373,20 +365,19 @@ func (r *Router) lookupFunc(ctx context.Context, domain string, strategy dns.Dom
addressLimit = false
responseAddrs, err = r.dnsClient.Lookup(dnsCtx, transport, domain, strategy, isCacheUpdate)
}
cancel()
var rejected bool
if err != nil {
if errors.Is(err, dns.ErrResponseRejectedCached) {
rejected = true
r.dnsLogger.DebugContext(ctx, "response rejected for ", domain, " (cached)")
r.dnsLogger.DebugContext(ctx, "response rejected for ", domain, " (cached)")
} else if errors.Is(err, dns.ErrResponseRejected) {
rejected = true
r.dnsLogger.DebugContext(ctx, "response rejected for ", domain)
r.dnsLogger.DebugContext(ctx, "response rejected for ", domain)
} else {
r.dnsLogger.ErrorContext(ctx, E.Cause(err, "lookup failed for ", domain))
}
} else if len(responseAddrs) == 0 {
r.dnsLogger.ErrorContext(ctx, "lookup failed for ", domain, ": empty result")
r.dnsLogger.ErrorContext(ctx, "lookup failed for ", domain, ": empty result")
err = dns.RCodeNameError
} else {
r.dnsLogger.InfoContext(ctx, "lookup succeed for ", domain, ": ", strings.Join(F.MapToString(responseAddrs), " "))
Expand Down Expand Up @@ -418,13 +409,11 @@ func (r *Router) lookupFunc(ctx context.Context, domain string, strategy dns.Dom
if transport == nil {
continue
}
dnsCtx, cancel = context.WithTimeout(dnsCtx, C.DNSTimeout)
responseAddrs, err = r.dnsClient.Lookup(dnsCtx, transport, domain, strategy, isCacheUpdate)
cancel()
if err != nil {
r.dnsLogger.ErrorContext(ctx, E.Cause(err, "lookup failed for ", domain))
r.dnsLogger.ErrorContext(ctx, E.Cause(err, "lookup failed for ", domain))
} else if len(responseAddrs) == 0 {
r.dnsLogger.ErrorContext(ctx, "lookup failed for ", domain, ": empty result")
r.dnsLogger.ErrorContext(ctx, "lookup failed for ", domain, ": empty result")
err = dns.RCodeNameError
} else {
r.dnsLogger.InfoContext(ctx, "lookup succeed for ", domain, ": ", strings.Join(F.MapToString(responseAddrs), " "))
Expand Down

0 comments on commit a3fdaa3

Please sign in to comment.