v2fly/app/router/config.go

143 lines
3.2 KiB
Go
Raw Normal View History

//go:build !confonly
// +build !confonly
2015-12-07 21:47:47 +00:00
package router
2015-11-14 13:24:56 +00:00
2016-10-12 14:11:13 +00:00
import (
"github.com/v2fly/v2ray-core/v4/app/router/routercommon"
2021-02-16 20:31:50 +00: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 14:11:13 +00:00
)
type Rule struct {
Tag string
2018-11-07 20:08:20 +00:00
Balancer *Balancer
2016-10-12 14:11:13 +00:00
Condition Condition
}
2018-11-07 20:08:20 +00:00
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 {
2017-05-08 09:48:41 +00:00
return r.Condition.Apply(ctx)
2016-10-12 14:11:13 +00:00
}
2017-05-08 09:48:41 +00:00
func (rr *RoutingRule) BuildCondition() (Condition, error) {
2016-10-12 14:11:13 +00:00
conds := NewConditionChan()
2017-05-08 09:48:41 +00:00
if len(rr.Domain) > 0 {
2021-03-03 22:27:23 +00:00
switch rr.DomainMatcher {
2021-03-15 07:27:40 +00:00
case "mph", "hybrid":
matcher, err := NewMphMatcherGroup(rr.Domain)
2021-03-03 22:27:23 +00:00
if err != nil {
return nil, newError("failed to build domain condition with MphDomainMatcher").Base(err)
2021-03-03 22:27:23 +00:00
}
2021-03-16 11:21:14 +00:00
newError("MphDomainMatcher is enabled for ", len(rr.Domain), " domain rule(s)").AtDebug().WriteToLog()
2021-03-03 22:27:23 +00:00
conds.Add(matcher)
case "linear":
fallthrough
2021-03-03 22:27:23 +00: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 14:11:13 +00:00
}
}
if len(rr.UserEmail) > 0 {
conds.Add(NewUserMatcher(rr.UserEmail))
}
if len(rr.InboundTag) > 0 {
conds.Add(NewInboundTagMatcher(rr.InboundTag))
2016-10-12 14:11:13 +00:00
}
2019-02-24 22:43:00 +00:00
if rr.PortList != nil {
conds.Add(NewPortMatcher(rr.PortList, false))
2019-02-24 22:43:00 +00:00
} 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))
2016-10-12 14:11:13 +00:00
}
2018-11-20 15:58:26 +00:00
if len(rr.Networks) > 0 {
conds.Add(NewNetworkMatcher(rr.Networks))
} else if rr.NetworkList != nil {
2018-11-20 15:12:14 +00:00
conds.Add(NewNetworkMatcher(rr.NetworkList.Network))
2016-10-12 14:11:13 +00: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 {
cond, err := NewMultiGeoIPMatcher([]*routercommon.GeoIP{{Cidr: rr.Cidr}}, false)
2017-05-17 11:24:53 +00:00
if err != nil {
return nil, err
2016-10-18 21:01:39 +00:00
}
2017-05-17 11:24:53 +00:00
conds.Add(cond)
2016-10-18 21:01:39 +00: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 {
cond, err := NewMultiGeoIPMatcher([]*routercommon.GeoIP{{Cidr: rr.SourceCidr}}, true)
if err != nil {
return nil, err
}
conds.Add(cond)
2016-11-13 20:23:34 +00:00
}
2018-07-16 11:47:00 +00:00
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)
}
2016-10-12 14:11:13 +00:00
if conds.Len() == 0 {
2017-12-27 21:25:12 +00:00
return nil, newError("this rule has no effective fields").AtWarning()
2016-10-12 14:11:13 +00:00
}
return conds, nil
2015-11-14 13:24:56 +00:00
}
2018-11-07 20:08:20 +00:00
func (br *BalancingRule) Build(ohm outbound.Manager) (*Balancer, error) {
2021-04-08 19:56:04 +00:00
switch br.Strategy {
case "leastPing":
2021-04-08 19:56:04 +00:00
return &Balancer{
selectors: br.OutboundSelector,
strategy: &LeastPingStrategy{},
2021-04-08 19:56:04 +00:00
ohm: ohm,
}, nil
case "random":
fallthrough
default:
2021-04-08 19:56:04 +00:00
return &Balancer{
selectors: br.OutboundSelector,
strategy: &RandomStrategy{},
ohm: ohm,
2021-04-08 19:56:04 +00:00
}, nil
}
}