2015-10-28 23:18:07 +01:00
|
|
|
package router
|
|
|
|
|
2021-02-17 04:31:50 +08:00
|
|
|
//go:generate go run github.com/v2fly/v2ray-core/v4/common/errors/errorgen
|
2017-04-09 01:43:25 +02:00
|
|
|
|
2015-10-28 23:18:07 +01:00
|
|
|
import (
|
2017-01-13 13:41:40 +01:00
|
|
|
"context"
|
2021-04-13 16:06:48 +01:00
|
|
|
|
2021-02-17 04:31:50 +08:00
|
|
|
core "github.com/v2fly/v2ray-core/v4"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/features/dns"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/features/outbound"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/features/routing"
|
|
|
|
routing_dns "github.com/v2fly/v2ray-core/v4/features/routing/dns"
|
2015-10-28 23:18:07 +01:00
|
|
|
)
|
|
|
|
|
2018-10-11 20:43:37 +02:00
|
|
|
// Router is an implementation of routing.Router.
|
2016-10-12 16:11:13 +02:00
|
|
|
type Router struct {
|
|
|
|
domainStrategy Config_DomainStrategy
|
2018-11-07 21:08:20 +01:00
|
|
|
rules []*Rule
|
|
|
|
balancers map[string]*Balancer
|
2018-10-11 22:34:31 +02:00
|
|
|
dns dns.Client
|
2015-10-28 23:18:07 +01:00
|
|
|
}
|
|
|
|
|
2020-09-18 17:30:59 +08:00
|
|
|
// Route is an implementation of routing.Route.
|
|
|
|
type Route struct {
|
|
|
|
routing.Context
|
|
|
|
outboundGroupTags []string
|
|
|
|
outboundTag string
|
|
|
|
}
|
|
|
|
|
2018-10-22 15:58:52 +02:00
|
|
|
// Init initializes the Router.
|
2021-01-30 08:31:11 +08:00
|
|
|
func (r *Router) Init(ctx context.Context, config *Config, d dns.Client, ohm outbound.Manager, dispatcher routing.Dispatcher) error {
|
2018-10-22 15:58:52 +02:00
|
|
|
r.domainStrategy = config.DomainStrategy
|
|
|
|
r.dns = d
|
2016-10-12 16:11:13 +02:00
|
|
|
|
2018-11-07 21:08:20 +01:00
|
|
|
r.balancers = make(map[string]*Balancer, len(config.BalancingRule))
|
|
|
|
for _, rule := range config.BalancingRule {
|
2021-01-30 08:31:11 +08:00
|
|
|
balancer, err := rule.Build(ohm, dispatcher)
|
2018-11-07 21:08:20 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-08 21:34:52 +01:00
|
|
|
balancer.InjectContext(ctx)
|
2018-11-07 21:08:20 +01:00
|
|
|
r.balancers[rule.Tag] = balancer
|
|
|
|
}
|
|
|
|
|
|
|
|
r.rules = make([]*Rule, 0, len(config.Rule))
|
|
|
|
for _, rule := range config.Rule {
|
2018-01-10 12:22:37 +01:00
|
|
|
cond, err := rule.BuildCondition()
|
|
|
|
if err != nil {
|
2018-10-22 15:58:52 +02:00
|
|
|
return err
|
2016-10-12 16:11:13 +02:00
|
|
|
}
|
2018-11-07 21:08:20 +01:00
|
|
|
rr := &Rule{
|
|
|
|
Condition: cond,
|
|
|
|
Tag: rule.GetTag(),
|
|
|
|
}
|
|
|
|
btag := rule.GetBalancingTag()
|
|
|
|
if len(btag) > 0 {
|
|
|
|
brule, found := r.balancers[btag]
|
|
|
|
if !found {
|
|
|
|
return newError("balancer ", btag, " not found")
|
|
|
|
}
|
|
|
|
rr.Balancer = brule
|
|
|
|
}
|
|
|
|
r.rules = append(r.rules, rr)
|
2018-01-10 12:22:37 +01:00
|
|
|
}
|
|
|
|
|
2018-10-22 15:58:52 +02:00
|
|
|
return nil
|
2015-10-28 23:18:07 +01:00
|
|
|
}
|
|
|
|
|
2020-09-04 11:32:19 +08:00
|
|
|
// PickRoute implements routing.Router.
|
2020-09-18 17:30:59 +08:00
|
|
|
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()
|
2018-11-07 21:08:20 +01:00
|
|
|
if err != nil {
|
2020-09-18 17:30:59 +08:00
|
|
|
return nil, err
|
2018-11-07 21:08:20 +01:00
|
|
|
}
|
2020-09-18 17:30:59 +08:00
|
|
|
return &Route{Context: ctx, outboundTag: tag}, nil
|
2018-11-07 21:08:20 +01:00
|
|
|
}
|
|
|
|
|
2020-09-18 17:30:59 +08:00
|
|
|
func (r *Router) pickRouteInternal(ctx routing.Context) (*Rule, routing.Context, error) {
|
2020-12-30 18:35:19 +08:00
|
|
|
// SkipDNSResolve is set from DNS module.
|
|
|
|
// the DOH remote server maybe a domain name,
|
|
|
|
// this prevents cycle resolving dead loop
|
|
|
|
skipDNSResolve := ctx.GetSkipDNSResolve()
|
|
|
|
|
|
|
|
if r.domainStrategy == Config_IpOnDemand && !skipDNSResolve {
|
2020-09-18 17:30:59 +08:00
|
|
|
ctx = routing_dns.ContextWithDNSClient(ctx, r.dns)
|
2017-11-15 12:55:47 +01:00
|
|
|
}
|
|
|
|
|
2017-04-23 19:16:56 +02:00
|
|
|
for _, rule := range r.rules {
|
2020-09-04 11:32:19 +08:00
|
|
|
if rule.Apply(ctx) {
|
2020-09-18 17:30:59 +08:00
|
|
|
return rule, ctx, nil
|
2016-10-12 16:11:13 +02:00
|
|
|
}
|
2016-08-18 08:34:21 +02:00
|
|
|
}
|
2017-01-26 20:46:44 +01:00
|
|
|
|
2020-12-30 18:35:19 +08:00
|
|
|
if r.domainStrategy != Config_IpIfNonMatch || len(ctx.GetTargetDomain()) == 0 || skipDNSResolve {
|
2020-09-18 17:30:59 +08:00
|
|
|
return nil, ctx, common.ErrNoClue
|
2017-02-09 22:49:38 +01:00
|
|
|
}
|
|
|
|
|
2020-09-18 17:30:59 +08:00
|
|
|
ctx = routing_dns.ContextWithDNSClient(ctx, r.dns)
|
2018-12-04 20:36:51 +01:00
|
|
|
|
|
|
|
// Try applying rules again if we have IPs.
|
|
|
|
for _, rule := range r.rules {
|
2020-09-04 11:32:19 +08:00
|
|
|
if rule.Apply(ctx) {
|
2020-09-18 17:30:59 +08:00
|
|
|
return rule, ctx, nil
|
2016-10-12 16:11:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-18 17:30:59 +08:00
|
|
|
return nil, ctx, common.ErrNoClue
|
2016-10-16 16:04:30 +02:00
|
|
|
}
|
|
|
|
|
2018-04-04 21:32:40 +02:00
|
|
|
// Start implements common.Runnable.
|
2021-01-30 08:31:11 +08:00
|
|
|
func (r *Router) Start() error {
|
2017-02-01 21:35:40 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-04 21:32:40 +02:00
|
|
|
// Close implements common.Closable.
|
2021-01-30 08:31:11 +08:00
|
|
|
func (r *Router) Close() error {
|
2018-02-08 15:39:46 +01:00
|
|
|
return nil
|
|
|
|
}
|
2017-02-01 21:35:40 +01:00
|
|
|
|
2018-10-12 23:57:56 +02:00
|
|
|
// Type implement common.HasType.
|
|
|
|
func (*Router) Type() interface{} {
|
|
|
|
return routing.RouterType()
|
|
|
|
}
|
2019-02-28 09:28:55 +01:00
|
|
|
|
2020-09-18 17:30:59 +08:00
|
|
|
// GetOutboundGroupTags implements routing.Route.
|
|
|
|
func (r *Route) GetOutboundGroupTags() []string {
|
|
|
|
return r.outboundGroupTags
|
2019-02-28 09:28:55 +01:00
|
|
|
}
|
|
|
|
|
2020-09-18 17:30:59 +08:00
|
|
|
// GetOutboundTag implements routing.Route.
|
|
|
|
func (r *Route) GetOutboundTag() string {
|
|
|
|
return r.outboundTag
|
2020-09-04 11:32:19 +08:00
|
|
|
}
|
2019-02-28 09:28:55 +01:00
|
|
|
|
2020-09-18 17:30:59 +08:00
|
|
|
func init() {
|
|
|
|
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
|
|
r := new(Router)
|
2021-01-30 08:31:11 +08:00
|
|
|
if err := core.RequireFeatures(ctx, func(d dns.Client, ohm outbound.Manager, dispatcher routing.Dispatcher) error {
|
|
|
|
return r.Init(ctx, config.(*Config), d, ohm, dispatcher)
|
2020-09-18 17:30:59 +08:00
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
2019-02-28 09:28:55 +01:00
|
|
|
}
|
2020-09-18 17:30:59 +08:00
|
|
|
return r, nil
|
|
|
|
}))
|
2019-02-28 09:28:55 +01:00
|
|
|
}
|