mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-06-13 00:23:06 +00:00
Routing: fix router select wrong outbound when failed to resolve domain to ip
This commit is contained in:
@@ -49,4 +49,7 @@ type Context interface {
|
||||
|
||||
// GetSkipDNSResolve returns a flag switch for weather skip dns resolve during route pick.
|
||||
GetSkipDNSResolve() bool
|
||||
|
||||
// GetError returns errors during route pick, e.g., built-in-dns fail to resolve domain to IP
|
||||
GetError() error
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package dns
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
"github.com/xtls/xray-core/common/net"
|
||||
"github.com/xtls/xray-core/features/dns"
|
||||
@@ -14,6 +12,7 @@ type ResolvableContext struct {
|
||||
routing.Context
|
||||
dnsClient dns.Client
|
||||
resolvedIPs []net.IP
|
||||
lookupError error
|
||||
}
|
||||
|
||||
// GetTargetIPs overrides original routing.Context's implementation.
|
||||
@@ -22,6 +21,10 @@ func (ctx *ResolvableContext) GetTargetIPs() []net.IP {
|
||||
return ctx.resolvedIPs
|
||||
}
|
||||
|
||||
if ips := ctx.Context.GetTargetIPs(); len(ips) != 0 {
|
||||
return ips
|
||||
}
|
||||
|
||||
if domain := ctx.GetTargetDomain(); len(domain) != 0 {
|
||||
ips, _, err := ctx.dnsClient.LookupIP(domain, dns.IPOption{
|
||||
IPv4Enable: true,
|
||||
@@ -32,16 +35,17 @@ func (ctx *ResolvableContext) GetTargetIPs() []net.IP {
|
||||
ctx.resolvedIPs = ips
|
||||
return ips
|
||||
}
|
||||
errors.LogInfoInner(context.Background(), err, "resolve ip for ", domain)
|
||||
}
|
||||
|
||||
if ips := ctx.Context.GetTargetIPs(); len(ips) != 0 {
|
||||
return ips
|
||||
ctx.lookupError = errors.New("resolve ip for ", domain).Base(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetError override original routing.Context's implementation.
|
||||
func (ctx *ResolvableContext) GetError() error {
|
||||
return ctx.lookupError
|
||||
}
|
||||
|
||||
// ContextWithDNSClient creates a new routing context with domain resolving capability.
|
||||
// Resolved domain IPs can be retrieved by GetTargetIPs().
|
||||
func ContextWithDNSClient(ctx routing.Context, client dns.Client) routing.Context {
|
||||
|
||||
@@ -152,6 +152,11 @@ func (ctx *Context) GetSkipDNSResolve() bool {
|
||||
return ctx.Content.SkipDNSResolve
|
||||
}
|
||||
|
||||
// GetError implements routing.Context.
|
||||
func (ctx *Context) GetError() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// AsRoutingContext creates a context from context.context with session info.
|
||||
func AsRoutingContext(ctx context.Context) routing.Context {
|
||||
outbounds := session.OutboundsFromContext(ctx)
|
||||
|
||||
Reference in New Issue
Block a user