mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-25 10:01:30 +00:00
Add strategy setting for each dns server
This commit is contained in:
parent
83cea9475d
commit
ac0ead1473
3
.github/update_dependencies.sh
vendored
3
.github/update_dependencies.sh
vendored
|
@ -8,6 +8,3 @@ go get -x github.com/sagernet/sing-tun@$(git -C $PROJECTS/sing-tun rev-parse HEA
|
|||
go get -x github.com/sagernet/sing-shadowsocks@$(git -C $PROJECTS/sing-shadowsocks rev-parse HEAD)
|
||||
go get -x github.com/sagernet/sing-vmess@$(git -C $PROJECTS/sing-vmess rev-parse HEAD)
|
||||
go mod tidy
|
||||
pushd test
|
||||
go mod tidy
|
||||
popd
|
||||
|
|
4
go.mod
4
go.mod
|
@ -19,8 +19,8 @@ require (
|
|||
github.com/sagernet/certmagic v0.0.0-20220819042630-4a57f8b6853a
|
||||
github.com/sagernet/netlink v0.0.0-20220820041223-3cd8365d17ac
|
||||
github.com/sagernet/quic-go v0.0.0-20220818150011-de611ab3e2bb
|
||||
github.com/sagernet/sing v0.0.0-20220821143531-cee85dcd3013
|
||||
github.com/sagernet/sing-dns v0.0.0-20220819010310-839eab1578c9
|
||||
github.com/sagernet/sing v0.0.0-20220822031040-57fe2e623c82
|
||||
github.com/sagernet/sing-dns v0.0.0-20220822023312-3e086b06d666
|
||||
github.com/sagernet/sing-shadowsocks v0.0.0-20220819002358-7461bb09a8f6
|
||||
github.com/sagernet/sing-tun v0.0.0-20220821033717-8b6630a3b006
|
||||
github.com/sagernet/sing-vmess v0.0.0-20220811135656-4f3f07acf9c4
|
||||
|
|
8
go.sum
8
go.sum
|
@ -101,10 +101,10 @@ github.com/sagernet/quic-go v0.0.0-20220818150011-de611ab3e2bb h1:wc0yQ+SBn4TaTY
|
|||
github.com/sagernet/quic-go v0.0.0-20220818150011-de611ab3e2bb/go.mod h1:MIccjRKnPTjWwAOpl+AUGWOkzyTd9tERytudxu+1ra4=
|
||||
github.com/sagernet/sing v0.0.0-20220812082120-05f9836bff8f/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY=
|
||||
github.com/sagernet/sing v0.0.0-20220817130738-ce854cda8522/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY=
|
||||
github.com/sagernet/sing v0.0.0-20220821143531-cee85dcd3013 h1:i5jP3rs+3IFj0OWB22YB3wSJJAoZiBd3YeBk9/vbDHI=
|
||||
github.com/sagernet/sing v0.0.0-20220821143531-cee85dcd3013/go.mod h1:kZvzh1VDa/Dg/Bt5WaYKU0jl5ept8KKDpl3Ay4gRtRQ=
|
||||
github.com/sagernet/sing-dns v0.0.0-20220819010310-839eab1578c9 h1:XgXSOJv8e7+98SJvg1f0luuPR33r4yFcmzxb3R//BTI=
|
||||
github.com/sagernet/sing-dns v0.0.0-20220819010310-839eab1578c9/go.mod h1:MAHy2IKZAA101t3Gr2x0ldwn6XuAs2cjGzSzHy5RhWk=
|
||||
github.com/sagernet/sing v0.0.0-20220822031040-57fe2e623c82 h1:DUHIA4lKxDqYNQyzb7NjZp+//voV25Ue7QoDjUvhgio=
|
||||
github.com/sagernet/sing v0.0.0-20220822031040-57fe2e623c82/go.mod h1:kZvzh1VDa/Dg/Bt5WaYKU0jl5ept8KKDpl3Ay4gRtRQ=
|
||||
github.com/sagernet/sing-dns v0.0.0-20220822023312-3e086b06d666 h1:XUTocA/Ek0dFxUX+xJCWMPPFZCn2GC/uLrBjTSr1vHY=
|
||||
github.com/sagernet/sing-dns v0.0.0-20220822023312-3e086b06d666/go.mod h1:eDyH7AJmqBGjZQdQmpZIzlbTREudZuWDExMuGKgjRVM=
|
||||
github.com/sagernet/sing-shadowsocks v0.0.0-20220819002358-7461bb09a8f6 h1:JJfDeYYhWunvtxsU/mOVNTmFQmnzGx9dY034qG6G3g4=
|
||||
github.com/sagernet/sing-shadowsocks v0.0.0-20220819002358-7461bb09a8f6/go.mod h1:EX3RbZvrwAkPI2nuGa78T2iQXmrkT+/VQtskjou42xM=
|
||||
github.com/sagernet/sing-tun v0.0.0-20220821033717-8b6630a3b006 h1:c1UyJ5H4CNLsg4GsAnNNoDAnHzLB13pm/T4zzvaWcFo=
|
||||
|
|
|
@ -28,6 +28,7 @@ type DNSServerOptions struct {
|
|||
AddressResolver string `json:"address_resolver,omitempty"`
|
||||
AddressStrategy DomainStrategy `json:"address_strategy,omitempty"`
|
||||
AddressFallbackDelay Duration `json:"address_fallback_delay,omitempty"`
|
||||
Strategy DomainStrategy `json:"strategy,omitempty"`
|
||||
Detour string `json:"detour,omitempty"`
|
||||
}
|
||||
|
||||
|
|
|
@ -84,6 +84,7 @@ type Router struct {
|
|||
defaultTransport dns.Transport
|
||||
transports []dns.Transport
|
||||
transportMap map[string]dns.Transport
|
||||
transportDomainStrategy map[dns.Transport]dns.DomainStrategy
|
||||
interfaceBindManager control.BindManager
|
||||
autoDetectInterface bool
|
||||
defaultInterface string
|
||||
|
@ -119,7 +120,7 @@ func NewRouter(ctx context.Context, logger log.ContextLogger, dnsLogger log.Cont
|
|||
geositeOptions: common.PtrValueOrDefault(options.Geosite),
|
||||
geositeCache: make(map[string]adapter.Rule),
|
||||
defaultDetour: options.Final,
|
||||
dnsClient: dns.NewClient(dns.DomainStrategy(dnsOptions.DNSClientOptions.Strategy), dnsOptions.DNSClientOptions.DisableCache, dnsOptions.DNSClientOptions.DisableExpire),
|
||||
dnsClient: dns.NewClient(dnsOptions.DNSClientOptions.DisableCache, dnsOptions.DNSClientOptions.DisableExpire),
|
||||
defaultDomainStrategy: dns.DomainStrategy(dnsOptions.Strategy),
|
||||
interfaceBindManager: control.NewBindManager(),
|
||||
autoDetectInterface: options.AutoDetectInterface,
|
||||
|
@ -145,6 +146,7 @@ func NewRouter(ctx context.Context, logger log.ContextLogger, dnsLogger log.Cont
|
|||
transportMap := make(map[string]dns.Transport)
|
||||
transportTags := make([]string, len(dnsOptions.Servers))
|
||||
transportTagMap := make(map[string]bool)
|
||||
transportDomainStrategy := make(map[dns.Transport]dns.DomainStrategy)
|
||||
for i, server := range dnsOptions.Servers {
|
||||
var tag string
|
||||
if server.Tag != "" {
|
||||
|
@ -202,6 +204,10 @@ func NewRouter(ctx context.Context, logger log.ContextLogger, dnsLogger log.Cont
|
|||
if server.Tag != "" {
|
||||
transportMap[server.Tag] = transport
|
||||
}
|
||||
strategy := dns.DomainStrategy(server.Strategy)
|
||||
if strategy != dns.DomainStrategyAsIS {
|
||||
transportDomainStrategy[transport] = strategy
|
||||
}
|
||||
}
|
||||
if len(transports) == len(dummyTransportMap) {
|
||||
break
|
||||
|
@ -233,6 +239,7 @@ func NewRouter(ctx context.Context, logger log.ContextLogger, dnsLogger log.Cont
|
|||
router.defaultTransport = defaultTransport
|
||||
router.transports = transports
|
||||
router.transportMap = transportMap
|
||||
router.transportDomainStrategy = transportDomainStrategy
|
||||
|
||||
needInterfaceMonitor := options.AutoDetectInterface ||
|
||||
C.IsDarwin && common.Any(inbounds, func(inbound option.Inbound) bool {
|
||||
|
@ -634,27 +641,6 @@ func (r *Router) match(ctx context.Context, metadata *adapter.InboundContext, de
|
|||
return nil, defaultOutbound
|
||||
}
|
||||
|
||||
func (r *Router) matchDNS(ctx context.Context) (context.Context, dns.Transport) {
|
||||
metadata := adapter.ContextFrom(ctx)
|
||||
if metadata == nil {
|
||||
panic("no context")
|
||||
}
|
||||
for i, rule := range r.dnsRules {
|
||||
if rule.Match(metadata) {
|
||||
if rule.DisableCache() {
|
||||
ctx = dns.ContextWithDisableCache(ctx, true)
|
||||
}
|
||||
detour := rule.Outbound()
|
||||
r.dnsLogger.DebugContext(ctx, "match[", i, "] ", rule.String(), " => ", detour)
|
||||
if transport, loaded := r.transportMap[detour]; loaded {
|
||||
return ctx, transport
|
||||
}
|
||||
r.dnsLogger.ErrorContext(ctx, "transport not found: ", detour)
|
||||
}
|
||||
}
|
||||
return ctx, r.defaultTransport
|
||||
}
|
||||
|
||||
func (r *Router) InterfaceBindManager() control.BindManager {
|
||||
return r.interfaceBindManager
|
||||
}
|
||||
|
|
|
@ -15,6 +15,31 @@ import (
|
|||
"golang.org/x/net/dns/dnsmessage"
|
||||
)
|
||||
|
||||
func (r *Router) matchDNS(ctx context.Context) (context.Context, dns.Transport, dns.DomainStrategy) {
|
||||
metadata := adapter.ContextFrom(ctx)
|
||||
if metadata == nil {
|
||||
panic("no context")
|
||||
}
|
||||
for i, rule := range r.dnsRules {
|
||||
if rule.Match(metadata) {
|
||||
if rule.DisableCache() {
|
||||
ctx = dns.ContextWithDisableCache(ctx, true)
|
||||
}
|
||||
detour := rule.Outbound()
|
||||
r.dnsLogger.DebugContext(ctx, "match[", i, "] ", rule.String(), " => ", detour)
|
||||
if transport, loaded := r.transportMap[detour]; loaded {
|
||||
if domainStrategy, dsLoaded := r.transportDomainStrategy[transport]; dsLoaded {
|
||||
return ctx, transport, domainStrategy
|
||||
} else {
|
||||
return ctx, transport, r.defaultDomainStrategy
|
||||
}
|
||||
}
|
||||
r.dnsLogger.ErrorContext(ctx, "transport not found: ", detour)
|
||||
}
|
||||
}
|
||||
return ctx, r.defaultTransport, r.defaultDomainStrategy
|
||||
}
|
||||
|
||||
func (r *Router) Exchange(ctx context.Context, message *dnsmessage.Message) (*dnsmessage.Message, error) {
|
||||
if len(message.Questions) > 0 {
|
||||
r.dnsLogger.DebugContext(ctx, "exchange ", formatDNSQuestion(message.Questions[0]))
|
||||
|
@ -28,10 +53,10 @@ func (r *Router) Exchange(ctx context.Context, message *dnsmessage.Message) (*dn
|
|||
metadata.IPVersion = 6
|
||||
}
|
||||
}
|
||||
ctx, transport := r.matchDNS(ctx)
|
||||
ctx, transport, strategy := r.matchDNS(ctx)
|
||||
ctx, cancel := context.WithTimeout(ctx, C.DNSTimeout)
|
||||
defer cancel()
|
||||
response, err := r.dnsClient.Exchange(ctx, transport, message)
|
||||
response, err := r.dnsClient.Exchange(ctx, transport, message, strategy)
|
||||
if err != nil && len(message.Questions) > 0 {
|
||||
r.dnsLogger.ErrorContext(ctx, E.Cause(err, "exchange failed for ", message.Questions[0].Name.String()))
|
||||
}
|
||||
|
@ -43,7 +68,10 @@ func (r *Router) Exchange(ctx context.Context, message *dnsmessage.Message) (*dn
|
|||
|
||||
func (r *Router) Lookup(ctx context.Context, domain string, strategy dns.DomainStrategy) ([]netip.Addr, error) {
|
||||
r.dnsLogger.DebugContext(ctx, "lookup domain ", domain)
|
||||
ctx, transport := r.matchDNS(ctx)
|
||||
ctx, transport, transportStrategy := r.matchDNS(ctx)
|
||||
if strategy == dns.DomainStrategyAsIS {
|
||||
strategy = transportStrategy
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(ctx, C.DNSTimeout)
|
||||
defer cancel()
|
||||
addrs, err := r.dnsClient.Lookup(ctx, transport, domain, strategy)
|
||||
|
@ -56,7 +84,7 @@ func (r *Router) Lookup(ctx context.Context, domain string, strategy dns.DomainS
|
|||
}
|
||||
|
||||
func (r *Router) LookupDefault(ctx context.Context, domain string) ([]netip.Addr, error) {
|
||||
return r.Lookup(ctx, domain, r.defaultDomainStrategy)
|
||||
return r.Lookup(ctx, domain, dns.DomainStrategyAsIS)
|
||||
}
|
||||
|
||||
func LogDNSAnswers(logger log.ContextLogger, ctx context.Context, domain string, answers []dnsmessage.Resource) {
|
||||
|
|
Loading…
Reference in a new issue