Router: Use built-in-dns only once for all rules (in "IPOnDemand"/"IPIfNonMatch" mode) (#5210)

pull/5231/head
patterniha 2025-10-15 00:29:04 +03:30 committed by GitHub
parent 7f436f5318
commit 21a9658519
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 12 additions and 5 deletions

View File

@ -12,14 +12,19 @@ import (
// ResolvableContext is an implementation of routing.Context, with domain resolving capability. // ResolvableContext is an implementation of routing.Context, with domain resolving capability.
type ResolvableContext struct { type ResolvableContext struct {
routing.Context routing.Context
dnsClient dns.Client dnsClient dns.Client
resolvedIPs []net.IP cacheIPs []net.IP
hasError bool
} }
// GetTargetIPs overrides original routing.Context's implementation. // GetTargetIPs overrides original routing.Context's implementation.
func (ctx *ResolvableContext) GetTargetIPs() []net.IP { func (ctx *ResolvableContext) GetTargetIPs() []net.IP {
if len(ctx.resolvedIPs) > 0 { if len(ctx.cacheIPs) > 0 {
return ctx.resolvedIPs return ctx.cacheIPs
}
if ctx.hasError {
return nil
} }
if domain := ctx.GetTargetDomain(); len(domain) != 0 { if domain := ctx.GetTargetDomain(); len(domain) != 0 {
@ -29,16 +34,18 @@ func (ctx *ResolvableContext) GetTargetIPs() []net.IP {
FakeEnable: false, FakeEnable: false,
}) })
if err == nil { if err == nil {
ctx.resolvedIPs = ips ctx.cacheIPs = ips
return ips return ips
} }
errors.LogInfoInner(context.Background(), err, "resolve ip for ", domain) errors.LogInfoInner(context.Background(), err, "resolve ip for ", domain)
} }
if ips := ctx.Context.GetTargetIPs(); len(ips) != 0 { if ips := ctx.Context.GetTargetIPs(); len(ips) != 0 {
ctx.cacheIPs = ips
return ips return ips
} }
ctx.hasError = true
return nil return nil
} }