Files
Xray-core/app/router/config.go
T

156 lines
3.6 KiB
Go
Raw Normal View History

2020-11-25 19:01:53 +08:00
package router
import (
2024-06-29 14:32:57 -04:00
"context"
2023-06-27 04:04:09 -04:00
"regexp"
2023-05-21 11:26:22 -04:00
"strings"
2024-06-29 14:32:57 -04:00
"github.com/xtls/xray-core/common/errors"
2020-12-04 09:36:16 +08:00
"github.com/xtls/xray-core/features/outbound"
"github.com/xtls/xray-core/features/routing"
2020-11-25 19:01:53 +08:00
)
type Rule struct {
Tag string
2024-03-29 19:17:36 +04:00
RuleTag string
2020-11-25 19:01:53 +08: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 {
switch rr.DomainMatcher {
case "linear":
matcher, err := NewDomainMatcher(rr.Domain)
if err != nil {
2024-06-29 14:32:57 -04:00
return nil, errors.New("failed to build domain condition").Base(err)
}
conds.Add(matcher)
case "mph", "hybrid":
fallthrough
default:
matcher, err := NewMphMatcherGroup(rr.Domain)
if err != nil {
2024-06-29 14:32:57 -04:00
return nil, errors.New("failed to build domain condition with MphDomainMatcher").Base(err)
}
2024-06-29 14:32:57 -04:00
errors.LogDebug(context.Background(), "MphDomainMatcher is enabled for ", len(rr.Domain), " domain rule(s)")
conds.Add(matcher)
2020-11-25 19:01:53 +08: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 04:04:09 -04:00
configuredKeys := make(map[string]*regexp.Regexp)
2023-05-21 11:26:22 -04:00
for key, value := range rr.Attributes {
2023-06-27 04:04:09 -04:00
configuredKeys[strings.ToLower(key)] = regexp.MustCompile(value)
2020-11-25 19:01:53 +08:00
}
2023-05-21 11:26:22 -04:00
conds.Add(&AttributeMatcher{configuredKeys})
2020-11-25 19:01:53 +08:00
}
if conds.Len() == 0 {
2024-06-29 14:32:57 -04:00
return nil, errors.New("this rule has no effective fields").AtWarning()
2020-11-25 19:01:53 +08:00
}
return conds, nil
}
2024-02-17 22:51:37 -05: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 01:00:31 -04:00
return &Balancer{
selectors: br.OutboundSelector,
strategy: &LeastPingStrategy{},
fallbackTag: br.FallbackTag,
ohm: ohm,
2021-10-26 01:00:31 -04:00
}, nil
2024-02-17 22:51:37 -05:00
case "roundrobin":
return &Balancer{
selectors: br.OutboundSelector,
strategy: &RoundRobinStrategy{FallbackTag: br.FallbackTag},
fallbackTag: br.FallbackTag,
ohm: ohm,
}, nil
2024-02-17 22:51:37 -05:00
case "leastload":
i, err := br.StrategySettings.GetInstance()
if err != nil {
return nil, err
}
s, ok := i.(*StrategyLeastLoadConfig)
if !ok {
2024-06-29 14:32:57 -04:00
return nil, errors.New("not a StrategyLeastLoadConfig").AtError()
2024-02-17 22:51:37 -05:00
}
leastLoadStrategy := NewLeastLoadStrategy(s)
return &Balancer{
selectors: br.OutboundSelector,
2024-03-29 19:17:36 +04:00
ohm: ohm,
fallbackTag: br.FallbackTag,
strategy: leastLoadStrategy,
2024-02-17 22:51:37 -05:00
}, nil
2021-10-26 01:00:31 -04:00
case "random":
fallthrough
2024-02-17 22:51:37 -05:00
case "":
2021-10-26 01:00:31 -04:00
return &Balancer{
selectors: br.OutboundSelector,
2024-03-29 19:17:36 +04:00
ohm: ohm,
fallbackTag: br.FallbackTag,
strategy: &RandomStrategy{FallbackTag: br.FallbackTag},
2021-10-26 01:00:31 -04:00
}, nil
2024-02-17 22:51:37 -05:00
default:
2024-06-29 14:32:57 -04:00
return nil, errors.New("unrecognized balancer type")
2021-10-26 01:00:31 -04:00
}
2020-11-25 19:01:53 +08:00
}