Routing: fix router select wrong outbound when failed to resolve domain to ip

fix-router-fail-ip
patterniha 2025-09-12 22:48:17 +03:30
parent 83c5370eec
commit fc7a3c14d7
6 changed files with 36 additions and 7 deletions

View File

@ -2,6 +2,7 @@ package dispatcher
import ( import (
"context" "context"
go_errors "errors"
"regexp" "regexp"
"strings" "strings"
"sync" "sync"
@ -488,6 +489,12 @@ func (d *DefaultDispatcher) routedDispatch(ctx context.Context, link *transport.
return // DO NOT CHANGE: the traffic shouldn't be processed by default outbound if the specified outbound tag doesn't exist (yet), e.g., VLESS Reverse Proxy return // DO NOT CHANGE: the traffic shouldn't be processed by default outbound if the specified outbound tag doesn't exist (yet), e.g., VLESS Reverse Proxy
} }
} else { } else {
if !go_errors.Is(err, common.ErrNoClue) {
errors.LogWarningInner(ctx, err, "get error during route pick ")
common.Close(link.Writer)
common.Interrupt(link.Reader)
return
}
errors.LogInfo(ctx, "default route for ", destination) errors.LogInfo(ctx, "default route for ", destination)
} }
} }

View File

@ -51,6 +51,10 @@ func (c routingContext) GetSkipDNSResolve() bool {
return false return false
} }
func (c routingContext) GetError() error {
return nil
}
// AsRoutingContext converts a protobuf RoutingContext into an implementation of routing.Context. // AsRoutingContext converts a protobuf RoutingContext into an implementation of routing.Context.
func AsRoutingContext(r *RoutingContext) routing.Context { func AsRoutingContext(r *RoutingContext) routing.Context {
return routingContext{r} return routingContext{r}

View File

@ -195,6 +195,9 @@ func (r *Router) pickRouteInternal(ctx routing.Context) (*Rule, routing.Context,
if rule.Apply(ctx) { if rule.Apply(ctx) {
return rule, ctx, nil return rule, ctx, nil
} }
if err := ctx.GetError(); err != nil {
return nil, ctx, err
}
} }
if r.domainStrategy != Config_IpIfNonMatch || len(ctx.GetTargetDomain()) == 0 || skipDNSResolve { if r.domainStrategy != Config_IpIfNonMatch || len(ctx.GetTargetDomain()) == 0 || skipDNSResolve {
@ -208,6 +211,9 @@ func (r *Router) pickRouteInternal(ctx routing.Context) (*Rule, routing.Context,
if rule.Apply(ctx) { if rule.Apply(ctx) {
return rule, ctx, nil return rule, ctx, nil
} }
if err := ctx.GetError(); err != nil {
return nil, ctx, err
}
} }
return nil, ctx, common.ErrNoClue return nil, ctx, common.ErrNoClue

View File

@ -49,4 +49,7 @@ type Context interface {
// GetSkipDNSResolve returns a flag switch for weather skip dns resolve during route pick. // GetSkipDNSResolve returns a flag switch for weather skip dns resolve during route pick.
GetSkipDNSResolve() bool GetSkipDNSResolve() bool
// GetError returns errors during route pick, e.g., built-in-dns fail to resolve domain to IP
GetError() error
} }

View File

@ -1,8 +1,6 @@
package dns package dns
import ( import (
"context"
"github.com/xtls/xray-core/common/errors" "github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/features/dns" "github.com/xtls/xray-core/features/dns"
@ -14,6 +12,7 @@ type ResolvableContext struct {
routing.Context routing.Context
dnsClient dns.Client dnsClient dns.Client
resolvedIPs []net.IP resolvedIPs []net.IP
lookupError error
} }
// GetTargetIPs overrides original routing.Context's implementation. // GetTargetIPs overrides original routing.Context's implementation.
@ -22,6 +21,10 @@ func (ctx *ResolvableContext) GetTargetIPs() []net.IP {
return ctx.resolvedIPs return ctx.resolvedIPs
} }
if ips := ctx.Context.GetTargetIPs(); len(ips) != 0 {
return ips
}
if domain := ctx.GetTargetDomain(); len(domain) != 0 { if domain := ctx.GetTargetDomain(); len(domain) != 0 {
ips, _, err := ctx.dnsClient.LookupIP(domain, dns.IPOption{ ips, _, err := ctx.dnsClient.LookupIP(domain, dns.IPOption{
IPv4Enable: true, IPv4Enable: true,
@ -32,16 +35,17 @@ func (ctx *ResolvableContext) GetTargetIPs() []net.IP {
ctx.resolvedIPs = ips ctx.resolvedIPs = ips
return ips return ips
} }
errors.LogInfoInner(context.Background(), err, "resolve ip for ", domain) ctx.lookupError = errors.New("resolve ip for ", domain).Base(err)
}
if ips := ctx.Context.GetTargetIPs(); len(ips) != 0 {
return ips
} }
return nil 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. // ContextWithDNSClient creates a new routing context with domain resolving capability.
// Resolved domain IPs can be retrieved by GetTargetIPs(). // Resolved domain IPs can be retrieved by GetTargetIPs().
func ContextWithDNSClient(ctx routing.Context, client dns.Client) routing.Context { func ContextWithDNSClient(ctx routing.Context, client dns.Client) routing.Context {

View File

@ -152,6 +152,11 @@ func (ctx *Context) GetSkipDNSResolve() bool {
return ctx.Content.SkipDNSResolve 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. // AsRoutingContext creates a context from context.context with session info.
func AsRoutingContext(ctx context.Context) routing.Context { func AsRoutingContext(ctx context.Context) routing.Context {
outbounds := session.OutboundsFromContext(ctx) outbounds := session.OutboundsFromContext(ctx)