Always disable cache for fake-ip DNS transport if independent_cache disabled

This commit is contained in:
PuerNya 2024-02-05 02:42:15 +08:00 committed by 世界
parent 71d1879bd6
commit 2ae192305c
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
2 changed files with 21 additions and 13 deletions

View file

@ -69,6 +69,7 @@ type Router struct {
geositeCache map[string]adapter.Rule geositeCache map[string]adapter.Rule
needFindProcess bool needFindProcess bool
dnsClient *dns.Client dnsClient *dns.Client
dnsIndependentCache bool
defaultDomainStrategy dns.DomainStrategy defaultDomainStrategy dns.DomainStrategy
dnsRules []adapter.DNSRule dnsRules []adapter.DNSRule
ruleSets []adapter.RuleSet ruleSets []adapter.RuleSet
@ -122,6 +123,7 @@ func NewRouter(
geositeOptions: common.PtrValueOrDefault(options.Geosite), geositeOptions: common.PtrValueOrDefault(options.Geosite),
geositeCache: make(map[string]adapter.Rule), geositeCache: make(map[string]adapter.Rule),
needFindProcess: hasRule(options.Rules, isProcessRule) || hasDNSRule(dnsOptions.Rules, isProcessDNSRule) || options.FindProcess, needFindProcess: hasRule(options.Rules, isProcessRule) || hasDNSRule(dnsOptions.Rules, isProcessDNSRule) || options.FindProcess,
dnsIndependentCache: dnsOptions.IndependentCache,
defaultDetour: options.Final, defaultDetour: options.Final,
defaultDomainStrategy: dns.DomainStrategy(dnsOptions.Strategy), defaultDomainStrategy: dns.DomainStrategy(dnsOptions.Strategy),
interfaceFinder: control.NewDefaultInterfaceFinder(), interfaceFinder: control.NewDefaultInterfaceFinder(),

View file

@ -56,7 +56,8 @@ func (r *Router) matchDNS(ctx context.Context, allowFakeIP bool, index int) (con
r.dnsLogger.ErrorContext(ctx, "transport not found: ", detour) r.dnsLogger.ErrorContext(ctx, "transport not found: ", detour)
continue continue
} }
if _, isFakeIP := transport.(adapter.FakeIPTransport); isFakeIP && !allowFakeIP { _, isFakeIP := transport.(adapter.FakeIPTransport)
if isFakeIP && !allowFakeIP {
continue continue
} }
displayRuleIndex := ruleIndex displayRuleIndex := ruleIndex
@ -64,7 +65,7 @@ func (r *Router) matchDNS(ctx context.Context, allowFakeIP bool, index int) (con
displayRuleIndex += index + 1 displayRuleIndex += index + 1
} }
r.dnsLogger.DebugContext(ctx, "match[", displayRuleIndex, "] ", rule.String(), " => ", detour) r.dnsLogger.DebugContext(ctx, "match[", displayRuleIndex, "] ", rule.String(), " => ", detour)
if rule.DisableCache() { if (isFakeIP && !r.dnsIndependentCache) || rule.DisableCache() {
ctx = dns.ContextWithDisableCache(ctx, true) ctx = dns.ContextWithDisableCache(ctx, true)
} }
if rewriteTTL := rule.RewriteTTL(); rewriteTTL != nil { if rewriteTTL := rule.RewriteTTL(); rewriteTTL != nil {
@ -95,6 +96,7 @@ func (r *Router) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS.Msg, er
var ( var (
response *mDNS.Msg response *mDNS.Msg
cached bool cached bool
transport dns.Transport
err error err error
) )
response, cached = r.dnsClient.ExchangeCache(ctx, message) response, cached = r.dnsClient.ExchangeCache(ctx, message)
@ -112,7 +114,6 @@ func (r *Router) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS.Msg, er
metadata.Domain = fqdnToDomain(message.Question[0].Name) metadata.Domain = fqdnToDomain(message.Question[0].Name)
} }
var ( var (
transport dns.Transport
strategy dns.DomainStrategy strategy dns.DomainStrategy
rule adapter.DNSRule rule adapter.DNSRule
ruleIndex int ruleIndex int
@ -158,7 +159,11 @@ func (r *Router) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS.Msg, er
break break
} }
} }
if err != nil {
return nil, err
}
if r.dnsReverseMapping != nil && len(message.Question) > 0 && response != nil && len(response.Answer) > 0 { if r.dnsReverseMapping != nil && len(message.Question) > 0 && response != nil && len(response.Answer) > 0 {
if _, isFakeIP := transport.(adapter.FakeIPTransport); !isFakeIP {
for _, answer := range response.Answer { for _, answer := range response.Answer {
switch record := answer.(type) { switch record := answer.(type) {
case *mDNS.A: case *mDNS.A:
@ -168,7 +173,8 @@ func (r *Router) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS.Msg, er
} }
} }
} }
return response, err }
return response, nil
} }
func (r *Router) Lookup(ctx context.Context, domain string, strategy dns.DomainStrategy) ([]netip.Addr, error) { func (r *Router) Lookup(ctx context.Context, domain string, strategy dns.DomainStrategy) ([]netip.Addr, error) {