Skip internal fake-ip queries

This commit is contained in:
世界 2023-12-01 21:48:21 +08:00
parent 38d28e0763
commit a99deb2cb5
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
2 changed files with 7 additions and 4 deletions

View file

@ -261,6 +261,9 @@ func NewRouter(
} }
defaultTransport = transports[0] defaultTransport = transports[0]
} }
if _, isFakeIP := defaultTransport.(adapter.FakeIPTransport); isFakeIP {
return nil, E.New("default DNS server cannot be fakeip")
}
router.defaultTransport = defaultTransport router.defaultTransport = defaultTransport
router.transports = transports router.transports = transports
router.transportMap = transportMap router.transportMap = transportMap

View file

@ -37,7 +37,7 @@ func (m *DNSReverseMapping) Query(address netip.Addr) (string, bool) {
return domain, loaded return domain, loaded
} }
func (r *Router) matchDNS(ctx context.Context) (context.Context, dns.Transport, dns.DomainStrategy) { func (r *Router) matchDNS(ctx context.Context, allowFakeIP bool) (context.Context, dns.Transport, dns.DomainStrategy) {
metadata := adapter.ContextFrom(ctx) metadata := adapter.ContextFrom(ctx)
if metadata == nil { if metadata == nil {
panic("no context") panic("no context")
@ -51,7 +51,7 @@ func (r *Router) matchDNS(ctx context.Context) (context.Context, dns.Transport,
r.dnsLogger.ErrorContext(ctx, "transport not found: ", detour) r.dnsLogger.ErrorContext(ctx, "transport not found: ", detour)
continue continue
} }
if _, isFakeIP := transport.(adapter.FakeIPTransport); isFakeIP && metadata.FakeIP { if _, isFakeIP := transport.(adapter.FakeIPTransport); isFakeIP && !allowFakeIP {
continue continue
} }
r.dnsLogger.DebugContext(ctx, "match[", i, "] ", rule.String(), " => ", detour) r.dnsLogger.DebugContext(ctx, "match[", i, "] ", rule.String(), " => ", detour)
@ -97,7 +97,7 @@ 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)
} }
ctx, transport, strategy := r.matchDNS(ctx) ctx, transport, strategy := r.matchDNS(ctx, true)
ctx, cancel := context.WithTimeout(ctx, C.DNSTimeout) ctx, cancel := context.WithTimeout(ctx, C.DNSTimeout)
defer cancel() defer cancel()
response, err = r.dnsClient.Exchange(ctx, transport, message, strategy) response, err = r.dnsClient.Exchange(ctx, transport, message, strategy)
@ -125,7 +125,7 @@ func (r *Router) Lookup(ctx context.Context, domain string, strategy dns.DomainS
r.dnsLogger.DebugContext(ctx, "lookup domain ", domain) r.dnsLogger.DebugContext(ctx, "lookup domain ", domain)
ctx, metadata := adapter.AppendContext(ctx) ctx, metadata := adapter.AppendContext(ctx)
metadata.Domain = domain metadata.Domain = domain
ctx, transport, transportStrategy := r.matchDNS(ctx) ctx, transport, transportStrategy := r.matchDNS(ctx, false)
if strategy == dns.DomainStrategyAsIS { if strategy == dns.DomainStrategyAsIS {
strategy = transportStrategy strategy = transportStrategy
} }