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 {
@ -93,9 +94,10 @@ func (r *Router) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS.Msg, er
r.dnsLogger.DebugContext(ctx, "exchange ", formatQuestion(message.Question[0].String())) r.dnsLogger.DebugContext(ctx, "exchange ", formatQuestion(message.Question[0].String()))
} }
var ( var (
response *mDNS.Msg response *mDNS.Msg
cached bool cached bool
err error transport dns.Transport
err error
) )
response, cached = r.dnsClient.ExchangeCache(ctx, message) response, cached = r.dnsClient.ExchangeCache(ctx, message)
if !cached { if !cached {
@ -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,17 +159,22 @@ 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 {
for _, answer := range response.Answer { if _, isFakeIP := transport.(adapter.FakeIPTransport); !isFakeIP {
switch record := answer.(type) { for _, answer := range response.Answer {
case *mDNS.A: switch record := answer.(type) {
r.dnsReverseMapping.Save(M.AddrFromIP(record.A), fqdnToDomain(record.Hdr.Name), int(record.Hdr.Ttl)) case *mDNS.A:
case *mDNS.AAAA: r.dnsReverseMapping.Save(M.AddrFromIP(record.A), fqdnToDomain(record.Hdr.Name), int(record.Hdr.Ttl))
r.dnsReverseMapping.Save(M.AddrFromIP(record.AAAA), fqdnToDomain(record.Hdr.Name), int(record.Hdr.Ttl)) case *mDNS.AAAA:
r.dnsReverseMapping.Save(M.AddrFromIP(record.AAAA), fqdnToDomain(record.Hdr.Name), int(record.Hdr.Ttl))
}
} }
} }
} }
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) {