2020-11-25 13:01:53 +02:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
2021-03-26 10:56:43 +02:00
|
|
|
dm "github.com/xtls/xray-core/common/matcher/domain"
|
2021-03-24 17:01:20 +02:00
|
|
|
"github.com/xtls/xray-core/common/matcher/geoip"
|
2020-12-04 03:36:16 +02:00
|
|
|
"github.com/xtls/xray-core/common/net"
|
|
|
|
"github.com/xtls/xray-core/features/outbound"
|
|
|
|
"github.com/xtls/xray-core/features/routing"
|
2020-11-25 13:01:53 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Rule struct {
|
|
|
|
Tag string
|
|
|
|
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-03-26 10:56:43 +02:00
|
|
|
matcher, err := dm.NewDomainMatcher(rr.Domain)
|
2020-11-25 13:01:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to build domain condition").Base(err)
|
|
|
|
}
|
|
|
|
conds.Add(matcher)
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
} else if rr.PortRange != nil {
|
|
|
|
conds.Add(NewPortMatcher(&net.PortList{Range: []*net.PortRange{rr.PortRange}}, false))
|
|
|
|
}
|
|
|
|
|
|
|
|
if rr.SourcePortList != nil {
|
|
|
|
conds.Add(NewPortMatcher(rr.SourcePortList, true))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(rr.Networks) > 0 {
|
|
|
|
conds.Add(NewNetworkMatcher(rr.Networks))
|
|
|
|
} else if rr.NetworkList != nil {
|
|
|
|
conds.Add(NewNetworkMatcher(rr.NetworkList.Network))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(rr.Geoip) > 0 {
|
2021-03-24 17:01:20 +02:00
|
|
|
cond, err := geoip.NewMultiGeoIPMatcher(rr.Geoip, false)
|
2020-11-25 13:01:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
conds.Add(cond)
|
|
|
|
} else if len(rr.Cidr) > 0 {
|
2021-03-24 17:01:20 +02:00
|
|
|
cond, err := geoip.NewMultiGeoIPMatcher([]*geoip.GeoIP{{Cidr: rr.Cidr}}, false)
|
2020-11-25 13:01:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
conds.Add(cond)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(rr.SourceGeoip) > 0 {
|
2021-03-24 17:01:20 +02:00
|
|
|
cond, err := geoip.NewMultiGeoIPMatcher(rr.SourceGeoip, true)
|
2020-11-25 13:01:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
conds.Add(cond)
|
|
|
|
} else if len(rr.SourceCidr) > 0 {
|
2021-03-24 17:01:20 +02:00
|
|
|
cond, err := geoip.NewMultiGeoIPMatcher([]*geoip.GeoIP{{Cidr: rr.SourceCidr}}, true)
|
2020-11-25 13:01:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
conds.Add(cond)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(rr.Protocol) > 0 {
|
|
|
|
conds.Add(NewProtocolMatcher(rr.Protocol))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(rr.Attributes) > 0 {
|
|
|
|
cond, err := NewAttributeMatcher(rr.Attributes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
conds.Add(cond)
|
|
|
|
}
|
|
|
|
|
|
|
|
if conds.Len() == 0 {
|
|
|
|
return nil, newError("this rule has no effective fields").AtWarning()
|
|
|
|
}
|
|
|
|
|
|
|
|
return conds, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *BalancingRule) Build(ohm outbound.Manager) (*Balancer, error) {
|
|
|
|
return &Balancer{
|
|
|
|
selectors: br.OutboundSelector,
|
|
|
|
strategy: &RandomStrategy{},
|
|
|
|
ohm: ohm,
|
|
|
|
}, nil
|
|
|
|
}
|