Skip to content

Commit

Permalink
Routing: Implement Route interface as the routing result of Router
Browse files Browse the repository at this point in the history
  • Loading branch information
Vigilans committed Sep 18, 2020
1 parent df2d296 commit 4d5a4f4
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 65 deletions.
3 changes: 2 additions & 1 deletion app/dispatcher/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ func (d *DefaultDispatcher) routedDispatch(ctx context.Context, link *transport.
}

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 {
newError("taking detour [", tag, "] for [", destination, "]").WriteToLog(session.ExportIDToError(ctx))
handler = h
Expand Down
87 changes: 38 additions & 49 deletions app/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,12 @@ import (

"v2ray.com/core"
"v2ray.com/core/common"
"v2ray.com/core/common/net"
"v2ray.com/core/features/dns"
"v2ray.com/core/features/outbound"
"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.
type Router struct {
domainStrategy Config_DomainStrategy
Expand All @@ -35,6 +23,13 @@ type Router struct {
dns dns.Client
}

// Route is an implementation of routing.Route.
type Route struct {
routing.Context
outboundGroupTags []string
outboundTag string
}

// Init initializes the Router.
func (r *Router) Init(config *Config, d dns.Client, ohm outbound.Manager) error {
r.domainStrategy = config.DomainStrategy
Expand Down Expand Up @@ -74,39 +69,43 @@ func (r *Router) Init(config *Config, d dns.Client, ohm outbound.Manager) error
}

// PickRoute implements routing.Router.
func (r *Router) PickRoute(ctx routing.Context) (string, error) {
rule, err := r.pickRouteInternal(ctx)
func (r *Router) PickRoute(ctx routing.Context) (routing.Route, error) {
rule, ctx, err := r.pickRouteInternal(ctx)
if err != nil {
return nil, err
}
tag, err := rule.GetTag()
if err != nil {
return "", err
return nil, err
}
return rule.GetTag()
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 {
ctx = ContextWithDNSClient(ctx, r.dns)
ctx = routing_dns.ContextWithDNSClient(ctx, r.dns)
}

for _, rule := range r.rules {
if rule.Apply(ctx) {
return rule, nil
return rule, ctx, nil
}
}

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.
for _, rule := range r.rules {
if rule.Apply(ctx) {
return rule, nil
return rule, ctx, nil
}
}

return nil, common.ErrNoClue
return nil, ctx, common.ErrNoClue
}

// Start implements common.Runnable.
Expand All @@ -124,34 +123,24 @@ func (*Router) Type() interface{} {
return routing.RouterType()
}

// 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}
// GetOutboundGroupTags implements routing.Route.
func (r *Route) GetOutboundGroupTags() []string {
return r.outboundGroupTags
}

type resolvableContext struct {
routing.Context
dnsClient dns.Client
resolvedIPs []net.IP
// GetOutboundTag implements routing.Route.
func (r *Route) GetOutboundTag() string {
return r.outboundTag
}

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
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
}
newError("resolve ip for ", domain).Base(err).WriteToLog()
}

return nil
return r, nil
}))
}
20 changes: 10 additions & 10 deletions app/router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ func TestSimpleRouter(t *testing.T) {
}))

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)
if tag != "test" {
if tag := route.GetOutboundTag(); tag != "test" {
t.Error("expect tag 'test', bug actually ", tag)
}
}
Expand Down Expand Up @@ -86,9 +86,9 @@ func TestSimpleBalancer(t *testing.T) {
}))

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)
if tag != "test" {
if tag := route.GetOutboundTag(); tag != "test" {
t.Error("expect tag 'test', bug actually ", tag)
}
}
Expand Down Expand Up @@ -121,9 +121,9 @@ func TestIPOnDemand(t *testing.T) {
common.Must(r.Init(config, mockDns, nil))

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)
if tag != "test" {
if tag := route.GetOutboundTag(); tag != "test" {
t.Error("expect tag 'test', bug actually ", tag)
}
}
Expand Down Expand Up @@ -156,9 +156,9 @@ func TestIPIfNonMatchDomain(t *testing.T) {
common.Must(r.Init(config, mockDns, nil))

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)
if tag != "test" {
if tag := route.GetOutboundTag(); tag != "test" {
t.Error("expect tag 'test', bug actually ", tag)
}
}
Expand Down Expand Up @@ -190,9 +190,9 @@ func TestIPIfNonMatchIP(t *testing.T) {
common.Must(r.Init(config, mockDns, nil))

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)
if tag != "test" {
if tag := route.GetOutboundTag(); tag != "test" {
t.Error("expect tag 'test', bug actually ", tag)
}
}
44 changes: 44 additions & 0 deletions features/routing/dns/context.go
Original file line number Diff line number Diff line change
@@ -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}
}
9 changes: 9 additions & 0 deletions features/routing/dns/errors.generated.go
Original file line number Diff line number Diff line change
@@ -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{})
}
24 changes: 19 additions & 5 deletions features/routing/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,26 @@ import (

// Router is a feature to choose an outbound tag for the given request.
//
// v2ray:api:beta
// v2ray:api:stable
type Router interface {
features.Feature

// PickRoute returns a tag of an OutboundHandler based on the given context.
PickRoute(ctx Context) (string, error)
// PickRoute returns a route decision based on the given routing context.
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.
Expand All @@ -31,8 +45,8 @@ func (DefaultRouter) Type() interface{} {
}

// PickRoute implements Router.
func (DefaultRouter) PickRoute(ctx Context) (string, error) {
return "", common.ErrNoClue
func (DefaultRouter) PickRoute(ctx Context) (Route, error) {
return nil, common.ErrNoClue
}

// Start implements common.Runnable.
Expand Down

0 comments on commit 4d5a4f4

Please sign in to comment.