2015-12-07 16:47:47 -05:00
|
|
|
package router
|
2015-11-14 08:24:56 -05:00
|
|
|
|
2016-10-12 10:11:13 -04:00
|
|
|
import (
|
2021-02-16 15:31:50 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common/net"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/features/outbound"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/features/routing"
|
2016-10-12 10:11:13 -04:00
|
|
|
)
|
|
|
|
|
2018-11-01 05:39:03 -04:00
|
|
|
// CIDRList is an alias of []*CIDR to provide sort.Interface.
|
2018-11-01 03:44:11 -04:00
|
|
|
type CIDRList []*CIDR
|
|
|
|
|
2018-11-01 05:39:03 -04:00
|
|
|
// Len implements sort.Interface.
|
2018-11-01 03:44:11 -04:00
|
|
|
func (l *CIDRList) Len() int {
|
|
|
|
return len(*l)
|
|
|
|
}
|
|
|
|
|
2018-11-01 05:39:03 -04:00
|
|
|
// Less implements sort.Interface.
|
2018-11-01 03:44:11 -04:00
|
|
|
func (l *CIDRList) Less(i int, j int) bool {
|
|
|
|
ci := (*l)[i]
|
|
|
|
cj := (*l)[j]
|
|
|
|
|
|
|
|
if len(ci.Ip) < len(cj.Ip) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ci.Ip) > len(cj.Ip) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for k := 0; k < len(ci.Ip); k++ {
|
|
|
|
if ci.Ip[k] < cj.Ip[k] {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if ci.Ip[k] > cj.Ip[k] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ci.Prefix < cj.Prefix
|
|
|
|
}
|
|
|
|
|
2018-11-01 05:39:03 -04:00
|
|
|
// Swap implements sort.Interface.
|
2018-11-01 03:44:11 -04:00
|
|
|
func (l *CIDRList) Swap(i int, j int) {
|
|
|
|
(*l)[i], (*l)[j] = (*l)[j], (*l)[i]
|
|
|
|
}
|
|
|
|
|
2016-10-12 10:11:13 -04:00
|
|
|
type Rule struct {
|
|
|
|
Tag string
|
2018-11-07 15:08:20 -05:00
|
|
|
Balancer *Balancer
|
2016-10-12 10:11:13 -04:00
|
|
|
Condition Condition
|
|
|
|
}
|
|
|
|
|
2018-11-07 15:08:20 -05:00
|
|
|
func (r *Rule) GetTag() (string, error) {
|
|
|
|
if r.Balancer != nil {
|
|
|
|
return r.Balancer.PickOutbound()
|
|
|
|
}
|
|
|
|
return r.Tag, nil
|
|
|
|
}
|
|
|
|
|
2020-09-03 23:32:19 -04:00
|
|
|
// Apply checks rule matching of current routing context.
|
|
|
|
func (r *Rule) Apply(ctx routing.Context) bool {
|
2017-05-08 05:48:41 -04:00
|
|
|
return r.Condition.Apply(ctx)
|
2016-10-12 10:11:13 -04:00
|
|
|
}
|
|
|
|
|
2017-05-08 05:48:41 -04:00
|
|
|
func (rr *RoutingRule) BuildCondition() (Condition, error) {
|
2016-10-12 10:11:13 -04:00
|
|
|
conds := NewConditionChan()
|
|
|
|
|
2017-05-08 05:48:41 -04:00
|
|
|
if len(rr.Domain) > 0 {
|
2021-03-03 17:27:23 -05:00
|
|
|
switch rr.DomainMatcher {
|
2021-03-15 03:27:40 -04:00
|
|
|
case "mph", "hybrid":
|
2021-03-15 03:21:38 -04:00
|
|
|
matcher, err := NewMphMatcherGroup(rr.Domain)
|
2021-03-03 17:27:23 -05:00
|
|
|
if err != nil {
|
2021-03-15 03:21:38 -04:00
|
|
|
return nil, newError("failed to build domain condition with MphDomainMatcher").Base(err)
|
2021-03-03 17:27:23 -05:00
|
|
|
}
|
2021-03-16 07:21:14 -04:00
|
|
|
newError("MphDomainMatcher is enabled for ", len(rr.Domain), " domain rule(s)").AtDebug().WriteToLog()
|
2021-03-03 17:27:23 -05:00
|
|
|
conds.Add(matcher)
|
2021-03-07 08:39:36 -05:00
|
|
|
case "linear":
|
|
|
|
fallthrough
|
2021-03-03 17:27:23 -05:00
|
|
|
default:
|
|
|
|
matcher, err := NewDomainMatcher(rr.Domain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to build domain condition").Base(err)
|
|
|
|
}
|
|
|
|
conds.Add(matcher)
|
2016-10-12 10:11:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-06 09:56:53 -04:00
|
|
|
if len(rr.UserEmail) > 0 {
|
|
|
|
conds.Add(NewUserMatcher(rr.UserEmail))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(rr.InboundTag) > 0 {
|
|
|
|
conds.Add(NewInboundTagMatcher(rr.InboundTag))
|
2016-10-12 10:11:13 -04:00
|
|
|
}
|
|
|
|
|
2019-02-24 17:43:00 -05:00
|
|
|
if rr.PortList != nil {
|
2020-08-09 04:53:45 -04:00
|
|
|
conds.Add(NewPortMatcher(rr.PortList, false))
|
2019-02-24 17:43:00 -05:00
|
|
|
} else if rr.PortRange != nil {
|
2020-08-09 04:53:45 -04:00
|
|
|
conds.Add(NewPortMatcher(&net.PortList{Range: []*net.PortRange{rr.PortRange}}, false))
|
|
|
|
}
|
|
|
|
|
|
|
|
if rr.SourcePortList != nil {
|
|
|
|
conds.Add(NewPortMatcher(rr.SourcePortList, true))
|
2016-10-12 10:11:13 -04:00
|
|
|
}
|
|
|
|
|
2018-11-20 10:58:26 -05:00
|
|
|
if len(rr.Networks) > 0 {
|
|
|
|
conds.Add(NewNetworkMatcher(rr.Networks))
|
|
|
|
} else if rr.NetworkList != nil {
|
2018-11-20 10:12:14 -05:00
|
|
|
conds.Add(NewNetworkMatcher(rr.NetworkList.Network))
|
2016-10-12 10:11:13 -04:00
|
|
|
}
|
|
|
|
|
2018-11-01 04:10:41 -04:00
|
|
|
if len(rr.Geoip) > 0 {
|
|
|
|
cond, err := NewMultiGeoIPMatcher(rr.Geoip, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
conds.Add(cond)
|
|
|
|
} else if len(rr.Cidr) > 0 {
|
2018-11-01 16:43:16 -04:00
|
|
|
cond, err := NewMultiGeoIPMatcher([]*GeoIP{{Cidr: rr.Cidr}}, false)
|
2017-05-17 07:24:53 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-10-18 17:01:39 -04:00
|
|
|
}
|
2017-05-17 07:24:53 -04:00
|
|
|
conds.Add(cond)
|
2016-10-18 17:01:39 -04:00
|
|
|
}
|
|
|
|
|
2018-11-01 04:10:41 -04:00
|
|
|
if len(rr.SourceGeoip) > 0 {
|
|
|
|
cond, err := NewMultiGeoIPMatcher(rr.SourceGeoip, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
conds.Add(cond)
|
|
|
|
} else if len(rr.SourceCidr) > 0 {
|
2018-11-01 16:43:16 -04:00
|
|
|
cond, err := NewMultiGeoIPMatcher([]*GeoIP{{Cidr: rr.SourceCidr}}, true)
|
2018-04-06 09:56:53 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
conds.Add(cond)
|
2016-11-13 15:23:34 -05:00
|
|
|
}
|
|
|
|
|
2018-07-16 07:47:00 -04:00
|
|
|
if len(rr.Protocol) > 0 {
|
|
|
|
conds.Add(NewProtocolMatcher(rr.Protocol))
|
|
|
|
}
|
|
|
|
|
2019-02-28 08:04:43 -05:00
|
|
|
if len(rr.Attributes) > 0 {
|
|
|
|
cond, err := NewAttributeMatcher(rr.Attributes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
conds.Add(cond)
|
|
|
|
}
|
|
|
|
|
2016-10-12 10:11:13 -04:00
|
|
|
if conds.Len() == 0 {
|
2017-12-27 16:25:12 -05:00
|
|
|
return nil, newError("this rule has no effective fields").AtWarning()
|
2016-10-12 10:11:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return conds, nil
|
2015-11-14 08:24:56 -05:00
|
|
|
}
|
2018-11-07 15:08:20 -05:00
|
|
|
|
2021-01-29 19:31:11 -05:00
|
|
|
// Build builds the balancing rule
|
|
|
|
func (br *BalancingRule) Build(ohm outbound.Manager, dispatcher routing.Dispatcher) (*Balancer, error) {
|
2021-04-08 15:56:04 -04:00
|
|
|
switch br.Strategy {
|
|
|
|
case "leastPing":
|
|
|
|
return &Balancer{
|
|
|
|
selectors: br.OutboundSelector,
|
|
|
|
strategy: &LeastPingStrategy{},
|
|
|
|
ohm: ohm,
|
|
|
|
}, nil
|
2021-01-29 19:31:11 -05:00
|
|
|
case "leastLoad":
|
|
|
|
i, err := br.StrategySettings.GetInstance()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s, ok := i.(*StrategyLeastLoadConfig)
|
|
|
|
if !ok {
|
|
|
|
return nil, newError("not a StrategyLeastLoadConfig").AtError()
|
|
|
|
}
|
|
|
|
leastLoadStrategy := NewLeastLoadStrategy(s, dispatcher)
|
|
|
|
return &Balancer{
|
|
|
|
selectors: br.OutboundSelector,
|
|
|
|
ohm: ohm, fallbackTag: br.FallbackTag,
|
|
|
|
strategy: leastLoadStrategy,
|
|
|
|
}, nil
|
2021-04-08 15:56:04 -04:00
|
|
|
case "random":
|
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
return &Balancer{
|
|
|
|
selectors: br.OutboundSelector,
|
2021-01-29 19:31:11 -05:00
|
|
|
ohm: ohm, fallbackTag: br.FallbackTag,
|
|
|
|
strategy: &RandomStrategy{},
|
2021-04-08 15:56:04 -04:00
|
|
|
}, nil
|
|
|
|
}
|
2018-11-07 15:08:20 -05:00
|
|
|
}
|