From ec0fa4781f9747adf8e3c167b642062de2530d41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sun, 23 Jul 2023 14:40:59 +0800 Subject: [PATCH] Improve fake-ip dual stack behavior --- route/router.go | 2 ++ route/router_dns.go | 20 ++++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/route/router.go b/route/router.go index c23c8a458e..125993c8e6 100644 --- a/route/router.go +++ b/route/router.go @@ -69,6 +69,7 @@ type Router struct { transportDomainStrategy map[dns.Transport]dns.DomainStrategy dnsReverseMapping *DNSReverseMapping fakeIPStore adapter.FakeIPStore + fakeIPDualStack bool interfaceFinder myInterfaceFinder autoDetectInterface bool defaultInterface string @@ -253,6 +254,7 @@ func NewRouter( inet6Range = fakeIPOptions.Inet6Range.Build() } router.fakeIPStore = fakeip.NewStore(router, router.logger, inet4Range, inet6Range) + router.fakeIPDualStack = inet4Range.IsValid() && inet6Range.IsValid() } usePlatformDefaultInterfaceMonitor := platformInterface != nil && platformInterface.UsePlatformDefaultInterfaceMonitor() diff --git a/route/router_dns.go b/route/router_dns.go index 0fe3735287..5d5682310a 100644 --- a/route/router_dns.go +++ b/route/router_dns.go @@ -63,14 +63,14 @@ func (r *Router) matchDNS(ctx context.Context) (context.Context, dns.Transport, if domainStrategy, dsLoaded := r.transportDomainStrategy[transport]; dsLoaded { return ctx, transport, domainStrategy } else { - return ctx, transport, r.defaultDomainStrategy + return ctx, transport, dns.DomainStrategyAsIS } } } if domainStrategy, dsLoaded := r.transportDomainStrategy[r.defaultTransport]; dsLoaded { return ctx, r.defaultTransport, domainStrategy } else { - return ctx, r.defaultTransport, r.defaultDomainStrategy + return ctx, r.defaultTransport, dns.DomainStrategyAsIS } } @@ -128,6 +128,22 @@ func (r *Router) Lookup(ctx context.Context, domain string, strategy dns.DomainS if strategy == dns.DomainStrategyAsIS { strategy = transportStrategy } + if strategy == dns.DomainStrategyAsIS { + switch r.defaultDomainStrategy { + case dns.DomainStrategyUseIPv4, dns.DomainStrategyUseIPv6: + strategy = r.defaultDomainStrategy + } + } + if strategy == dns.DomainStrategyAsIS && metadata.FakeIP && r.fakeIPDualStack { + if metadata.IPVersion == 4 { + strategy = dns.DomainStrategyUseIPv4 + } else if metadata.IPVersion == 6 { + strategy = dns.DomainStrategyPreferIPv6 + } + } + if strategy == dns.DomainStrategyAsIS { + strategy = r.defaultDomainStrategy + } ctx, cancel := context.WithTimeout(ctx, C.DNSTimeout) defer cancel() addrs, err := r.dnsClient.Lookup(ctx, transport, domain, strategy)