1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2026-01-19 15:45:20 -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

@@ -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.
//
// 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.
@@ -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.