github.com/eagleql/xray-core@v1.4.4/features/routing/dns/context.go (about) 1 package dns 2 3 //go:generate go run github.com/eagleql/xray-core/common/errors/errorgen 4 5 import ( 6 "github.com/eagleql/xray-core/common/net" 7 "github.com/eagleql/xray-core/features/dns" 8 "github.com/eagleql/xray-core/features/routing" 9 ) 10 11 // ResolvableContext is an implementation of routing.Context, with domain resolving capability. 12 type ResolvableContext struct { 13 routing.Context 14 dnsClient dns.Client 15 resolvedIPs []net.IP 16 } 17 18 // GetTargetIPs overrides original routing.Context's implementation. 19 func (ctx *ResolvableContext) GetTargetIPs() []net.IP { 20 if ips := ctx.Context.GetTargetIPs(); len(ips) != 0 { 21 return ips 22 } 23 24 if len(ctx.resolvedIPs) > 0 { 25 return ctx.resolvedIPs 26 } 27 28 if domain := ctx.GetTargetDomain(); len(domain) != 0 { 29 ips, err := ctx.dnsClient.LookupIP(domain, dns.IPOption{ 30 IPv4Enable: true, 31 IPv6Enable: true, 32 FakeEnable: false, 33 }) 34 if err == nil { 35 ctx.resolvedIPs = ips 36 return ips 37 } 38 newError("resolve ip for ", domain).Base(err).WriteToLog() 39 } 40 41 return nil 42 } 43 44 // ContextWithDNSClient creates a new routing context with domain resolving capability. 45 // Resolved domain IPs can be retrieved by GetTargetIPs(). 46 func ContextWithDNSClient(ctx routing.Context, client dns.Client) routing.Context { 47 return &ResolvableContext{Context: ctx, dnsClient: client} 48 }