2020-11-25 14:01:53 +03:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
2024-06-29 21:32:57 +03:00
|
|
|
"context"
|
2023-06-27 11:04:09 +03:00
|
|
|
"regexp"
|
2023-05-21 18:26:22 +03:00
|
|
|
"strings"
|
|
|
|
|
2024-06-29 21:32:57 +03:00
|
|
|
"github.com/xtls/xray-core/common/errors"
|
2020-12-04 04:36:16 +03:00
|
|
|
"github.com/xtls/xray-core/features/outbound"
|
|
|
|
"github.com/xtls/xray-core/features/routing"
|
2020-11-25 14:01:53 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Rule struct {
|
|
|
|
Tag string
|
2024-03-29 18:17:36 +03:00
|
|
|
RuleTag string
|
2020-11-25 14:01:53 +03:00
|
|
|
Balancer *Balancer
|
|
|
|
Condition Condition
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Rule) GetTag() (string, error) {
|
|
|
|
if r.Balancer != nil {
|
|
|
|
return r.Balancer.PickOutbound()
|
|
|
|
}
|
|
|
|
return r.Tag, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply checks rule matching of current routing context.
|
|
|
|
func (r *Rule) Apply(ctx routing.Context) bool {
|
|
|
|
return r.Condition.Apply(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rr *RoutingRule) BuildCondition() (Condition, error) {
|
|
|
|
conds := NewConditionChan()
|
|
|
|
|
|
|
|
if len(rr.Domain) > 0 {
|
2021-04-18 08:21:17 +03:00
|
|
|
switch rr.DomainMatcher {
|
|
|
|
case "linear":
|
|
|
|
matcher, err := NewDomainMatcher(rr.Domain)
|
|
|
|
if err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return nil, errors.New("failed to build domain condition").Base(err)
|
2021-04-18 08:21:17 +03:00
|
|
|
}
|
|
|
|
conds.Add(matcher)
|
|
|
|
case "mph", "hybrid":
|
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
matcher, err := NewMphMatcherGroup(rr.Domain)
|
|
|
|
if err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return nil, errors.New("failed to build domain condition with MphDomainMatcher").Base(err)
|
2021-04-18 08:21:17 +03:00
|
|
|
}
|
2024-06-29 21:32:57 +03:00
|
|
|
errors.LogDebug(context.Background(), "MphDomainMatcher is enabled for ", len(rr.Domain), " domain rule(s)")
|
2021-04-18 08:21:17 +03:00
|
|
|
conds.Add(matcher)
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(rr.UserEmail) > 0 {
|
|
|
|
conds.Add(NewUserMatcher(rr.UserEmail))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(rr.InboundTag) > 0 {
|
|
|
|
conds.Add(NewInboundTagMatcher(rr.InboundTag))
|
|
|
|
}
|
|
|
|
|
|
|
|
if rr.PortList != nil {
|
|
|
|
conds.Add(NewPortMatcher(rr.PortList, false))
|
|
|
|
}
|
|
|
|
|
|
|
|
if rr.SourcePortList != nil {
|
|
|
|
conds.Add(NewPortMatcher(rr.SourcePortList, true))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(rr.Networks) > 0 {
|
|
|
|
conds.Add(NewNetworkMatcher(rr.Networks))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(rr.Geoip) > 0 {
|
|
|
|
cond, err := NewMultiGeoIPMatcher(rr.Geoip, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
conds.Add(cond)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(rr.SourceGeoip) > 0 {
|
|
|
|
cond, err := NewMultiGeoIPMatcher(rr.SourceGeoip, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
conds.Add(cond)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(rr.Protocol) > 0 {
|
|
|
|
conds.Add(NewProtocolMatcher(rr.Protocol))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(rr.Attributes) > 0 {
|
2023-06-27 11:04:09 +03:00
|
|
|
configuredKeys := make(map[string]*regexp.Regexp)
|
2023-05-21 18:26:22 +03:00
|
|
|
for key, value := range rr.Attributes {
|
2023-06-27 11:04:09 +03:00
|
|
|
configuredKeys[strings.ToLower(key)] = regexp.MustCompile(value)
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
2023-05-21 18:26:22 +03:00
|
|
|
conds.Add(&AttributeMatcher{configuredKeys})
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if conds.Len() == 0 {
|
2024-06-29 21:32:57 +03:00
|
|
|
return nil, errors.New("this rule has no effective fields").AtWarning()
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return conds, nil
|
|
|
|
}
|
|
|
|
|
2024-02-18 06:51:37 +03:00
|
|
|
// Build builds the balancing rule
|
|
|
|
func (br *BalancingRule) Build(ohm outbound.Manager, dispatcher routing.Dispatcher) (*Balancer, error) {
|
|
|
|
switch strings.ToLower(br.Strategy) {
|
|
|
|
case "leastping":
|
2021-10-26 08:00:31 +03:00
|
|
|
return &Balancer{
|
2024-03-15 21:49:11 +03:00
|
|
|
selectors: br.OutboundSelector,
|
|
|
|
strategy: &LeastPingStrategy{},
|
|
|
|
fallbackTag: br.FallbackTag,
|
|
|
|
ohm: ohm,
|
2021-10-26 08:00:31 +03:00
|
|
|
}, nil
|
2024-02-18 06:51:37 +03:00
|
|
|
case "roundrobin":
|
2023-12-24 23:29:10 +03:00
|
|
|
return &Balancer{
|
2024-03-15 21:49:11 +03:00
|
|
|
selectors: br.OutboundSelector,
|
2024-05-05 17:07:40 +03:00
|
|
|
strategy: &RoundRobinStrategy{FallbackTag: br.FallbackTag},
|
2024-03-15 21:49:11 +03:00
|
|
|
fallbackTag: br.FallbackTag,
|
|
|
|
ohm: ohm,
|
2023-12-24 23:29:10 +03:00
|
|
|
}, nil
|
2024-02-18 06:51:37 +03:00
|
|
|
case "leastload":
|
|
|
|
i, err := br.StrategySettings.GetInstance()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s, ok := i.(*StrategyLeastLoadConfig)
|
|
|
|
if !ok {
|
2024-06-29 21:32:57 +03:00
|
|
|
return nil, errors.New("not a StrategyLeastLoadConfig").AtError()
|
2024-02-18 06:51:37 +03:00
|
|
|
}
|
|
|
|
leastLoadStrategy := NewLeastLoadStrategy(s)
|
|
|
|
return &Balancer{
|
2024-03-15 21:49:11 +03:00
|
|
|
selectors: br.OutboundSelector,
|
2024-03-29 18:17:36 +03:00
|
|
|
ohm: ohm,
|
2024-03-15 21:49:11 +03:00
|
|
|
fallbackTag: br.FallbackTag,
|
|
|
|
strategy: leastLoadStrategy,
|
2024-02-18 06:51:37 +03:00
|
|
|
}, nil
|
2021-10-26 08:00:31 +03:00
|
|
|
case "random":
|
|
|
|
fallthrough
|
2024-02-18 06:51:37 +03:00
|
|
|
case "":
|
2021-10-26 08:00:31 +03:00
|
|
|
return &Balancer{
|
2024-03-15 21:49:11 +03:00
|
|
|
selectors: br.OutboundSelector,
|
2024-03-29 18:17:36 +03:00
|
|
|
ohm: ohm,
|
2024-03-15 21:49:11 +03:00
|
|
|
fallbackTag: br.FallbackTag,
|
2024-05-05 17:07:40 +03:00
|
|
|
strategy: &RandomStrategy{FallbackTag: br.FallbackTag},
|
2021-10-26 08:00:31 +03:00
|
|
|
}, nil
|
2024-02-18 06:51:37 +03:00
|
|
|
default:
|
2024-06-29 21:32:57 +03:00
|
|
|
return nil, errors.New("unrecognized balancer type")
|
2021-10-26 08:00:31 +03:00
|
|
|
}
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|