Skip to content

Commit

Permalink
Improve fake-ip dual stack behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Jul 31, 2023
1 parent 8d629ef commit ec0fa47
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
2 changes: 2 additions & 0 deletions route/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
20 changes: 18 additions & 2 deletions route/router_dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ec0fa47

Please sign in to comment.