1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-02-20 23:47:21 -05:00
v2fly/app/router/config.go

151 lines
2.9 KiB
Go
Raw Normal View History

2019-02-01 20:08:21 +01:00
// +build !confonly
2015-12-07 22:47:47 +01:00
package router
2015-11-14 14:24:56 +01:00
2016-10-12 16:11:13 +02:00
import (
"v2ray.com/core/common/net"
2018-11-07 21:08:20 +01:00
"v2ray.com/core/features/outbound"
2016-10-12 16:11:13 +02:00
)
2018-11-01 10:39:03 +01:00
// CIDRList is an alias of []*CIDR to provide sort.Interface.
2018-11-01 08:44:11 +01:00
type CIDRList []*CIDR
2018-11-01 10:39:03 +01:00
// Len implements sort.Interface.
2018-11-01 08:44:11 +01:00
func (l *CIDRList) Len() int {
return len(*l)
}
2018-11-01 10:39:03 +01:00
// Less implements sort.Interface.
2018-11-01 08:44:11 +01: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 10:39:03 +01:00
// Swap implements sort.Interface.
2018-11-01 08:44:11 +01:00
func (l *CIDRList) Swap(i int, j int) {
(*l)[i], (*l)[j] = (*l)[j], (*l)[i]
}
2016-10-12 16:11:13 +02:00
type Rule struct {
Tag string
2018-11-07 21:08:20 +01:00
Balancer *Balancer
2016-10-12 16:11:13 +02:00
Condition Condition
}
2018-11-07 21:08:20 +01:00
func (r *Rule) GetTag() (string, error) {
if r.Balancer != nil {
return r.Balancer.PickOutbound()
}
return r.Tag, nil
}
func (r *Rule) Apply(ctx *Context) bool {
2017-05-08 11:48:41 +02:00
return r.Condition.Apply(ctx)
2016-10-12 16:11:13 +02:00
}
2017-05-08 11:48:41 +02:00
func (rr *RoutingRule) BuildCondition() (Condition, error) {
2016-10-12 16:11:13 +02:00
conds := NewConditionChan()
2017-05-08 11:48:41 +02:00
if len(rr.Domain) > 0 {
2018-08-19 22:28:02 +02:00
matcher, err := NewDomainMatcher(rr.Domain)
2018-08-19 21:04:15 +02:00
if err != nil {
return nil, newError("failed to build domain condition").Base(err)
2016-10-12 16:11:13 +02:00
}
2017-11-06 21:12:28 +01:00
conds.Add(matcher)
2016-10-12 16:11:13 +02:00
}
if len(rr.UserEmail) > 0 {
conds.Add(NewUserMatcher(rr.UserEmail))
}
if len(rr.InboundTag) > 0 {
conds.Add(NewInboundTagMatcher(rr.InboundTag))
2016-10-12 16:11:13 +02:00
}
2019-02-24 23:43:00 +01:00
if rr.PortList != nil {
conds.Add(NewPortMatcher(rr.PortList))
} else if rr.PortRange != nil {
conds.Add(NewPortMatcher(&net.PortList{Range: []*net.PortRange{rr.PortRange}}))
2016-10-12 16:11:13 +02:00
}
2018-11-20 16:58:26 +01:00
if len(rr.Networks) > 0 {
conds.Add(NewNetworkMatcher(rr.Networks))
} else if rr.NetworkList != nil {
2018-11-20 16:12:14 +01:00
conds.Add(NewNetworkMatcher(rr.NetworkList.Network))
2016-10-12 16:11:13 +02: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 21:43:16 +01:00
cond, err := NewMultiGeoIPMatcher([]*GeoIP{{Cidr: rr.Cidr}}, false)
2017-05-17 13:24:53 +02:00
if err != nil {
return nil, err
2016-10-18 23:01:39 +02:00
}
2017-05-17 13:24:53 +02:00
conds.Add(cond)
2016-10-18 23:01:39 +02: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 21:43:16 +01:00
cond, err := NewMultiGeoIPMatcher([]*GeoIP{{Cidr: rr.SourceCidr}}, true)
if err != nil {
return nil, err
}
conds.Add(cond)
2016-11-13 21:23:34 +01:00
}
2018-07-16 13:47:00 +02: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 16:11:13 +02:00
if conds.Len() == 0 {
2017-12-27 22:25:12 +01:00
return nil, newError("this rule has no effective fields").AtWarning()
2016-10-12 16:11:13 +02:00
}
return conds, nil
2015-11-14 14:24:56 +01:00
}
2018-11-07 21:08:20 +01:00
func (br *BalancingRule) Build(ohm outbound.Manager) (*Balancer, error) {
return &Balancer{
selectors: br.OutboundSelector,
strategy: &RandomStrategy{},
2018-11-07 21:25:43 +01:00
ohm: ohm,
2018-11-07 21:08:20 +01:00
}, nil
}