mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-06-27 20:52:40 +03:00
Add strategy setting for each dns server
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user