1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-30 14:06:58 -05:00

Routing: Implement Route interface as the routing result of Router

This commit is contained in:
Vigilans 2020-09-18 17:30:59 +08:00
parent df2d296ffc
commit 4d5a4f4cb6
6 changed files with 122 additions and 65 deletions

View File

@ -266,7 +266,8 @@ func (d *DefaultDispatcher) routedDispatch(ctx context.Context, link *transport.
} }
if d.router != nil && !skipRoutePick { if d.router != nil && !skipRoutePick {
if tag, err := d.router.PickRoute(routing_session.AsRoutingContext(ctx)); err == nil { if route, err := d.router.PickRoute(routing_session.AsRoutingContext(ctx)); err == nil {
tag := route.GetOutboundTag()
if h := d.ohm.GetHandler(tag); h != nil { if h := d.ohm.GetHandler(tag); h != nil {
newError("taking detour [", tag, "] for [", destination, "]").WriteToLog(session.ExportIDToError(ctx)) newError("taking detour [", tag, "] for [", destination, "]").WriteToLog(session.ExportIDToError(ctx))
handler = h handler = h

View File

@ -9,24 +9,12 @@ import (
"v2ray.com/core" "v2ray.com/core"
"v2ray.com/core/common" "v2ray.com/core/common"
"v2ray.com/core/common/net"
"v2ray.com/core/features/dns" "v2ray.com/core/features/dns"
"v2ray.com/core/features/outbound" "v2ray.com/core/features/outbound"
"v2ray.com/core/features/routing" "v2ray.com/core/features/routing"
routing_dns "v2ray.com/core/features/routing/dns"
) )
func init() {
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
r := new(Router)
if err := core.RequireFeatures(ctx, func(d dns.Client, ohm outbound.Manager) error {
return r.Init(config.(*Config), d, ohm)
}); err != nil {
return nil, err
}
return r, nil
}))
}
// Router is an implementation of routing.Router. // Router is an implementation of routing.Router.
type Router struct { type Router struct {
domainStrategy Config_DomainStrategy domainStrategy Config_DomainStrategy
@ -35,6 +23,13 @@ type Router struct {
dns dns.Client dns dns.Client
} }
// Route is an implementation of routing.Route.
type Route struct {
routing.Context
outboundGroupTags []string
outboundTag string
}
// Init initializes the Router. // Init initializes the Router.
func (r *Router) Init(config *Config, d dns.Client, ohm outbound.Manager) error { func (r *Router) Init(config *Config, d dns.Client, ohm outbound.Manager) error {
r.domainStrategy = config.DomainStrategy r.domainStrategy = config.DomainStrategy
@ -74,39 +69,43 @@ func (r *Router) Init(config *Config, d dns.Client, ohm outbound.Manager) error
} }
// PickRoute implements routing.Router. // PickRoute implements routing.Router.
func (r *Router) PickRoute(ctx routing.Context) (string, error) { func (r *Router) PickRoute(ctx routing.Context) (routing.Route, error) {
rule, err := r.pickRouteInternal(ctx) rule, ctx, err := r.pickRouteInternal(ctx)
if err != nil { if err != nil {
return "", err return nil, err
} }
return rule.GetTag() tag, err := rule.GetTag()
if err != nil {
return nil, err
}
return &Route{Context: ctx, outboundTag: tag}, nil
} }
func (r *Router) pickRouteInternal(ctx routing.Context) (*Rule, error) { func (r *Router) pickRouteInternal(ctx routing.Context) (*Rule, routing.Context, error) {
if r.domainStrategy == Config_IpOnDemand { if r.domainStrategy == Config_IpOnDemand {
ctx = ContextWithDNSClient(ctx, r.dns) ctx = routing_dns.ContextWithDNSClient(ctx, r.dns)
} }
for _, rule := range r.rules { for _, rule := range r.rules {
if rule.Apply(ctx) { if rule.Apply(ctx) {
return rule, nil return rule, ctx, nil
} }
} }
if r.domainStrategy != Config_IpIfNonMatch || len(ctx.GetTargetDomain()) == 0 { if r.domainStrategy != Config_IpIfNonMatch || len(ctx.GetTargetDomain()) == 0 {
return nil, common.ErrNoClue return nil, ctx, common.ErrNoClue
} }
ctx = ContextWithDNSClient(ctx, r.dns) ctx = routing_dns.ContextWithDNSClient(ctx, r.dns)
// Try applying rules again if we have IPs. // Try applying rules again if we have IPs.
for _, rule := range r.rules { for _, rule := range r.rules {
if rule.Apply(ctx) { if rule.Apply(ctx) {
return rule, nil return rule, ctx, nil
} }
} }
return nil, common.ErrNoClue return nil, ctx, common.ErrNoClue
} }
// Start implements common.Runnable. // Start implements common.Runnable.
@ -124,34 +123,24 @@ func (*Router) Type() interface{} {
return routing.RouterType() return routing.RouterType()
} }
// ContextWithDNSClient creates a new routing context with domain resolving capability. Resolved domain IPs can be retrieved by GetTargetIPs(). // GetOutboundGroupTags implements routing.Route.
func ContextWithDNSClient(ctx routing.Context, client dns.Client) routing.Context { func (r *Route) GetOutboundGroupTags() []string {
return &resolvableContext{Context: ctx, dnsClient: client} return r.outboundGroupTags
} }
type resolvableContext struct { // GetOutboundTag implements routing.Route.
routing.Context func (r *Route) GetOutboundTag() string {
dnsClient dns.Client return r.outboundTag
resolvedIPs []net.IP
} }
func (ctx *resolvableContext) GetTargetIPs() []net.IP { func init() {
if ips := ctx.Context.GetTargetIPs(); len(ips) != 0 { common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return ips r := new(Router)
if err := core.RequireFeatures(ctx, func(d dns.Client, ohm outbound.Manager) error {
return r.Init(config.(*Config), d, ohm)
}); err != nil {
return nil, err
} }
return r, nil
if len(ctx.resolvedIPs) > 0 { }))
return ctx.resolvedIPs
}
if domain := ctx.GetTargetDomain(); len(domain) != 0 {
ips, err := ctx.dnsClient.LookupIP(domain)
if err == nil {
ctx.resolvedIPs = ips
return ips
}
newError("resolve ip for ", domain).Base(err).WriteToLog()
}
return nil
} }

View File

@ -45,9 +45,9 @@ func TestSimpleRouter(t *testing.T) {
})) }))
ctx := session.ContextWithOutbound(context.Background(), &session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2ray.com"), 80)}) ctx := session.ContextWithOutbound(context.Background(), &session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2ray.com"), 80)})
tag, err := r.PickRoute(routing_session.AsRoutingContext(ctx)) route, err := r.PickRoute(routing_session.AsRoutingContext(ctx))
common.Must(err) common.Must(err)
if tag != "test" { if tag := route.GetOutboundTag(); tag != "test" {
t.Error("expect tag 'test', bug actually ", tag) t.Error("expect tag 'test', bug actually ", tag)
} }
} }
@ -86,9 +86,9 @@ func TestSimpleBalancer(t *testing.T) {
})) }))
ctx := session.ContextWithOutbound(context.Background(), &session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2ray.com"), 80)}) ctx := session.ContextWithOutbound(context.Background(), &session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2ray.com"), 80)})
tag, err := r.PickRoute(routing_session.AsRoutingContext(ctx)) route, err := r.PickRoute(routing_session.AsRoutingContext(ctx))
common.Must(err) common.Must(err)
if tag != "test" { if tag := route.GetOutboundTag(); tag != "test" {
t.Error("expect tag 'test', bug actually ", tag) t.Error("expect tag 'test', bug actually ", tag)
} }
} }
@ -121,9 +121,9 @@ func TestIPOnDemand(t *testing.T) {
common.Must(r.Init(config, mockDns, nil)) common.Must(r.Init(config, mockDns, nil))
ctx := session.ContextWithOutbound(context.Background(), &session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2ray.com"), 80)}) ctx := session.ContextWithOutbound(context.Background(), &session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2ray.com"), 80)})
tag, err := r.PickRoute(routing_session.AsRoutingContext(ctx)) route, err := r.PickRoute(routing_session.AsRoutingContext(ctx))
common.Must(err) common.Must(err)
if tag != "test" { if tag := route.GetOutboundTag(); tag != "test" {
t.Error("expect tag 'test', bug actually ", tag) t.Error("expect tag 'test', bug actually ", tag)
} }
} }
@ -156,9 +156,9 @@ func TestIPIfNonMatchDomain(t *testing.T) {
common.Must(r.Init(config, mockDns, nil)) common.Must(r.Init(config, mockDns, nil))
ctx := session.ContextWithOutbound(context.Background(), &session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2ray.com"), 80)}) ctx := session.ContextWithOutbound(context.Background(), &session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2ray.com"), 80)})
tag, err := r.PickRoute(routing_session.AsRoutingContext(ctx)) route, err := r.PickRoute(routing_session.AsRoutingContext(ctx))
common.Must(err) common.Must(err)
if tag != "test" { if tag := route.GetOutboundTag(); tag != "test" {
t.Error("expect tag 'test', bug actually ", tag) t.Error("expect tag 'test', bug actually ", tag)
} }
} }
@ -190,9 +190,9 @@ func TestIPIfNonMatchIP(t *testing.T) {
common.Must(r.Init(config, mockDns, nil)) common.Must(r.Init(config, mockDns, nil))
ctx := session.ContextWithOutbound(context.Background(), &session.Outbound{Target: net.TCPDestination(net.LocalHostIP, 80)}) ctx := session.ContextWithOutbound(context.Background(), &session.Outbound{Target: net.TCPDestination(net.LocalHostIP, 80)})
tag, err := r.PickRoute(routing_session.AsRoutingContext(ctx)) route, err := r.PickRoute(routing_session.AsRoutingContext(ctx))
common.Must(err) common.Must(err)
if tag != "test" { if tag := route.GetOutboundTag(); tag != "test" {
t.Error("expect tag 'test', bug actually ", tag) t.Error("expect tag 'test', bug actually ", tag)
} }
} }

View File

@ -0,0 +1,44 @@
package dns
//go:generate errorgen
import (
"v2ray.com/core/common/net"
"v2ray.com/core/features/dns"
"v2ray.com/core/features/routing"
)
// ResolvableContext is an implementation of routing.Context, with domain resolving capability.
type ResolvableContext struct {
routing.Context
dnsClient dns.Client
resolvedIPs []net.IP
}
// GetTargetIPs overrides original routing.Context's implementation.
func (ctx *ResolvableContext) GetTargetIPs() []net.IP {
if ips := ctx.Context.GetTargetIPs(); len(ips) != 0 {
return ips
}
if len(ctx.resolvedIPs) > 0 {
return ctx.resolvedIPs
}
if domain := ctx.GetTargetDomain(); len(domain) != 0 {
ips, err := ctx.dnsClient.LookupIP(domain)
if err == nil {
ctx.resolvedIPs = ips
return ips
}
newError("resolve ip for ", domain).Base(err).WriteToLog()
}
return nil
}
// 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 {
return &ResolvableContext{Context: ctx, dnsClient: client}
}

View File

@ -0,0 +1,9 @@
package dns
import "v2ray.com/core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -7,12 +7,26 @@ import (
// Router is a feature to choose an outbound tag for the given request. // Router is a feature to choose an outbound tag for the given request.
// //
// v2ray:api:beta // v2ray:api:stable
type Router interface { type Router interface {
features.Feature features.Feature
// PickRoute returns a tag of an OutboundHandler based on the given context. // PickRoute returns a route decision based on the given routing context.
PickRoute(ctx Context) (string, error) PickRoute(ctx Context) (Route, error)
}
// Route is the routing result of Router feature.
//
// v2ray:api:stable
type Route interface {
// A Route is also a routing context.
Context
// GetOutboundGroupTags returns the detoured outbound group tags in sequence before a final outbound is chosen.
GetOutboundGroupTags() []string
// GetOutboundTag returns the tag of the outbound the connection was dispatched to.
GetOutboundTag() string
} }
// RouterType return the type of Router interface. Can be used to implement common.HasType. // RouterType return the type of Router interface. Can be used to implement common.HasType.
@ -31,8 +45,8 @@ func (DefaultRouter) Type() interface{} {
} }
// PickRoute implements Router. // PickRoute implements Router.
func (DefaultRouter) PickRoute(ctx Context) (string, error) { func (DefaultRouter) PickRoute(ctx Context) (Route, error) {
return "", common.ErrNoClue return nil, common.ErrNoClue
} }
// Start implements common.Runnable. // Start implements common.Runnable.