2020-11-25 14:01:53 +03:00
|
|
|
package router
|
|
|
|
|
2020-12-04 04:36:16 +03:00
|
|
|
//go:generate go run github.com/xtls/xray-core/common/errors/errorgen
|
2020-11-25 14:01:53 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-03-29 18:17:36 +03:00
|
|
|
sync "sync"
|
2020-11-25 14:01:53 +03:00
|
|
|
|
2020-12-04 04:36:16 +03:00
|
|
|
"github.com/xtls/xray-core/common"
|
2024-03-29 18:17:36 +03:00
|
|
|
"github.com/xtls/xray-core/common/serial"
|
2020-12-04 04:36:16 +03:00
|
|
|
"github.com/xtls/xray-core/core"
|
|
|
|
"github.com/xtls/xray-core/features/dns"
|
|
|
|
"github.com/xtls/xray-core/features/outbound"
|
|
|
|
"github.com/xtls/xray-core/features/routing"
|
|
|
|
routing_dns "github.com/xtls/xray-core/features/routing/dns"
|
2020-11-25 14:01:53 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Router is an implementation of routing.Router.
|
|
|
|
type Router struct {
|
|
|
|
domainStrategy Config_DomainStrategy
|
|
|
|
rules []*Rule
|
|
|
|
balancers map[string]*Balancer
|
|
|
|
dns dns.Client
|
2024-03-29 18:17:36 +03:00
|
|
|
|
|
|
|
ctx context.Context
|
|
|
|
ohm outbound.Manager
|
|
|
|
dispatcher routing.Dispatcher
|
|
|
|
mu sync.Mutex
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Route is an implementation of routing.Route.
|
|
|
|
type Route struct {
|
|
|
|
routing.Context
|
|
|
|
outboundGroupTags []string
|
|
|
|
outboundTag string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init initializes the Router.
|
2024-02-18 06:51:37 +03:00
|
|
|
func (r *Router) Init(ctx context.Context, config *Config, d dns.Client, ohm outbound.Manager, dispatcher routing.Dispatcher) error {
|
2020-11-25 14:01:53 +03:00
|
|
|
r.domainStrategy = config.DomainStrategy
|
|
|
|
r.dns = d
|
2024-03-29 18:17:36 +03:00
|
|
|
r.ctx = ctx
|
|
|
|
r.ohm = ohm
|
|
|
|
r.dispatcher = dispatcher
|
2020-11-25 14:01:53 +03:00
|
|
|
|
|
|
|
r.balancers = make(map[string]*Balancer, len(config.BalancingRule))
|
|
|
|
for _, rule := range config.BalancingRule {
|
2024-02-18 06:51:37 +03:00
|
|
|
balancer, err := rule.Build(ohm, dispatcher)
|
2020-11-25 14:01:53 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-26 08:00:31 +03:00
|
|
|
balancer.InjectContext(ctx)
|
2020-11-25 14:01:53 +03:00
|
|
|
r.balancers[rule.Tag] = balancer
|
|
|
|
}
|
|
|
|
|
|
|
|
r.rules = make([]*Rule, 0, len(config.Rule))
|
|
|
|
for _, rule := range config.Rule {
|
|
|
|
cond, err := rule.BuildCondition()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rr := &Rule{
|
|
|
|
Condition: cond,
|
|
|
|
Tag: rule.GetTag(),
|
2024-03-29 18:17:36 +03:00
|
|
|
RuleTag: rule.GetRuleTag(),
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PickRoute implements routing.Router.
|
|
|
|
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 nil, err
|
|
|
|
}
|
|
|
|
return &Route{Context: ctx, outboundTag: tag}, nil
|
|
|
|
}
|
|
|
|
|
2024-03-29 18:17:36 +03:00
|
|
|
// AddRule implements routing.Router.
|
|
|
|
func (r *Router) AddRule(config *serial.TypedMessage, shouldAppend bool) error {
|
|
|
|
|
|
|
|
inst, err := config.GetInstance()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if c, ok := inst.(*Config); ok {
|
|
|
|
return r.ReloadRules(c, shouldAppend)
|
|
|
|
}
|
|
|
|
return newError("AddRule: config type error")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Router) ReloadRules(config *Config, shouldAppend bool) error {
|
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
|
|
|
|
|
|
|
if !shouldAppend {
|
|
|
|
r.balancers = make(map[string]*Balancer, len(config.BalancingRule))
|
|
|
|
r.rules = make([]*Rule, 0, len(config.Rule))
|
|
|
|
}
|
|
|
|
for _, rule := range config.BalancingRule {
|
|
|
|
_, found := r.balancers[rule.Tag]
|
|
|
|
if found {
|
|
|
|
return newError("duplicate balancer tag")
|
|
|
|
}
|
|
|
|
balancer, err := rule.Build(r.ohm, r.dispatcher)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
balancer.InjectContext(r.ctx)
|
|
|
|
r.balancers[rule.Tag] = balancer
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rule := range config.Rule {
|
|
|
|
if r.RuleExists(rule.GetRuleTag()) {
|
|
|
|
return newError("duplicate ruleTag ", rule.GetRuleTag())
|
|
|
|
}
|
|
|
|
cond, err := rule.BuildCondition()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rr := &Rule{
|
|
|
|
Condition: cond,
|
|
|
|
Tag: rule.GetTag(),
|
|
|
|
RuleTag: rule.GetRuleTag(),
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Router) RuleExists(tag string) bool {
|
|
|
|
if tag != "" {
|
|
|
|
for _, rule := range r.rules {
|
|
|
|
if rule.RuleTag == tag {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveRule implements routing.Router.
|
|
|
|
func (r *Router) RemoveRule(tag string) error {
|
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
|
|
|
|
|
|
|
newRules := []*Rule{}
|
|
|
|
if tag != "" {
|
|
|
|
for _, rule := range r.rules {
|
|
|
|
if rule.RuleTag != tag {
|
|
|
|
newRules = append(newRules, rule)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r.rules = newRules
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return newError("empty tag name!")
|
|
|
|
|
|
|
|
}
|
2020-11-25 14:01:53 +03:00
|
|
|
func (r *Router) pickRouteInternal(ctx routing.Context) (*Rule, routing.Context, error) {
|
2021-10-16 16:02:51 +03: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-11-25 14:01:53 +03:00
|
|
|
ctx = routing_dns.ContextWithDNSClient(ctx, r.dns)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rule := range r.rules {
|
|
|
|
if rule.Apply(ctx) {
|
|
|
|
return rule, ctx, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-16 16:02:51 +03:00
|
|
|
if r.domainStrategy != Config_IpIfNonMatch || len(ctx.GetTargetDomain()) == 0 || skipDNSResolve {
|
2020-11-25 14:01:53 +03:00
|
|
|
return nil, ctx, common.ErrNoClue
|
|
|
|
}
|
|
|
|
|
|
|
|
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, ctx, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, ctx, common.ErrNoClue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start implements common.Runnable.
|
2024-02-18 06:51:37 +03:00
|
|
|
func (r *Router) Start() error {
|
2020-11-25 14:01:53 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements common.Closable.
|
2024-02-18 06:51:37 +03:00
|
|
|
func (r *Router) Close() error {
|
2020-11-25 14:01:53 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-13 05:51:47 +03:00
|
|
|
// Type implements common.HasType.
|
2020-11-25 14:01:53 +03:00
|
|
|
func (*Router) Type() interface{} {
|
|
|
|
return routing.RouterType()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOutboundGroupTags implements routing.Route.
|
|
|
|
func (r *Route) GetOutboundGroupTags() []string {
|
|
|
|
return r.outboundGroupTags
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOutboundTag implements routing.Route.
|
|
|
|
func (r *Route) GetOutboundTag() string {
|
|
|
|
return r.outboundTag
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
|
|
r := new(Router)
|
2024-02-18 06:51:37 +03: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-11-25 14:01:53 +03:00
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
}))
|
|
|
|
}
|