Skip to content

Commit

Permalink
Only reseponse fakeip on dns exchanging
Browse files Browse the repository at this point in the history
  • Loading branch information
PuerNya committed Oct 30, 2023
1 parent ffdb19c commit 50f2808
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions route/router_dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (m *DNSReverseMapping) Query(address netip.Addr) (string, bool) {
return domain, loaded
}

func (r *Router) matchDNS(ctx context.Context) (context.Context, dns.Transport, dns.DomainStrategy) {
func (r *Router) matchDNS(ctx context.Context, useFakeIP bool) (context.Context, dns.Transport, dns.DomainStrategy) {
metadata := adapter.ContextFrom(ctx)
if metadata == nil {
panic("no context")
Expand All @@ -50,7 +50,7 @@ func (r *Router) matchDNS(ctx context.Context) (context.Context, dns.Transport,
r.dnsLogger.ErrorContext(ctx, "transport not found: ", detour)
continue
}
if _, isFakeIP := transport.(adapter.FakeIPTransport); isFakeIP && metadata.FakeIP {
if _, isFakeIP := transport.(adapter.FakeIPTransport); isFakeIP && !useFakeIP {
continue
}
r.dnsLogger.DebugContext(ctx, "match[", i, "] ", rule.String(), " => ", detour)
Expand All @@ -67,10 +67,20 @@ func (r *Router) matchDNS(ctx context.Context) (context.Context, dns.Transport,
}
}
}
if domainStrategy, dsLoaded := r.transportDomainStrategy[r.defaultTransport]; dsLoaded {
return ctx, r.defaultTransport, domainStrategy
transport := r.defaultTransport
if _, isFakeIP := transport.(adapter.FakeIPTransport); isFakeIP && !useFakeIP {
for _, t := range r.transports {
if _, isFakeIP := t.(adapter.FakeIPTransport); isFakeIP {
continue
}
transport = t
break
}
}
if domainStrategy, dsLoaded := r.transportDomainStrategy[transport]; dsLoaded {
return ctx, transport, domainStrategy
} else {
return ctx, r.defaultTransport, r.defaultDomainStrategy
return ctx, transport, r.defaultDomainStrategy
}
}

Expand All @@ -96,7 +106,7 @@ func (r *Router) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS.Msg, er
}
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)
defer cancel()
response, err = r.dnsClient.Exchange(ctx, transport, message, strategy)
Expand Down Expand Up @@ -124,7 +134,7 @@ func (r *Router) Lookup(ctx context.Context, domain string, strategy dns.DomainS
r.dnsLogger.DebugContext(ctx, "lookup domain ", domain)
ctx, metadata := adapter.AppendContext(ctx)
metadata.Domain = domain
ctx, transport, transportStrategy := r.matchDNS(ctx)
ctx, transport, transportStrategy := r.matchDNS(ctx, false)
if strategy == dns.DomainStrategyAsIS {
strategy = transportStrategy
}
Expand Down

0 comments on commit 50f2808

Please sign in to comment.