|
|
@@ -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
|
|
|
@@ -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
|
|
|
@@ -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.
|
|
|
@@ -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
|
|
|
+ }))
|
|
|
}
|