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 (
|
2017-01-26 14:46:44 -05:00
|
|
|
"context"
|
2016-10-12 10:11:13 -04:00
|
|
|
"net"
|
|
|
|
|
|
|
|
v2net "v2ray.com/core/common/net"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Rule struct {
|
|
|
|
Tag string
|
|
|
|
Condition Condition
|
|
|
|
}
|
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
func (v *Rule) Apply(ctx context.Context) bool {
|
|
|
|
return v.Condition.Apply(ctx)
|
2016-10-12 10:11:13 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *RoutingRule) BuildCondition() (Condition, error) {
|
2016-10-12 10:11:13 -04:00
|
|
|
conds := NewConditionChan()
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
if len(v.Domain) > 0 {
|
2016-10-12 10:11:13 -04:00
|
|
|
anyCond := NewAnyCondition()
|
2016-11-27 15:39:09 -05:00
|
|
|
for _, domain := range v.Domain {
|
2016-10-12 10:11:13 -04:00
|
|
|
if domain.Type == Domain_Plain {
|
|
|
|
anyCond.Add(NewPlainDomainMatcher(domain.Value))
|
|
|
|
} else {
|
|
|
|
matcher, err := NewRegexpDomainMatcher(domain.Value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
anyCond.Add(matcher)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
conds.Add(anyCond)
|
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
if len(v.Cidr) > 0 {
|
2016-10-18 10:42:22 -04:00
|
|
|
ipv4Net := v2net.NewIPNet()
|
2016-10-12 10:11:13 -04:00
|
|
|
ipv6Cond := NewAnyCondition()
|
|
|
|
hasIpv6 := false
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
for _, ip := range v.Cidr {
|
2016-10-12 10:11:13 -04:00
|
|
|
switch len(ip.Ip) {
|
|
|
|
case net.IPv4len:
|
2016-10-18 10:42:22 -04:00
|
|
|
ipv4Net.AddIP(ip.Ip, byte(ip.Prefix))
|
2016-10-12 10:11:13 -04:00
|
|
|
case net.IPv6len:
|
|
|
|
hasIpv6 = true
|
2016-10-18 17:01:39 -04:00
|
|
|
matcher, err := NewCIDRMatcher(ip.Ip, ip.Prefix, false)
|
2016-10-12 10:11:13 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ipv6Cond.Add(matcher)
|
|
|
|
default:
|
2017-04-09 09:04:04 -04:00
|
|
|
return nil, newError("invalid IP length").AtError()
|
2016-10-12 10:11:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-18 10:42:22 -04:00
|
|
|
if !ipv4Net.IsEmpty() && hasIpv6 {
|
2016-10-12 10:11:13 -04:00
|
|
|
cond := NewAnyCondition()
|
2016-10-18 17:01:39 -04:00
|
|
|
cond.Add(NewIPv4Matcher(ipv4Net, false))
|
2016-10-12 10:11:13 -04:00
|
|
|
cond.Add(ipv6Cond)
|
|
|
|
conds.Add(cond)
|
2016-10-18 10:42:22 -04:00
|
|
|
} else if !ipv4Net.IsEmpty() {
|
2016-10-18 17:01:39 -04:00
|
|
|
conds.Add(NewIPv4Matcher(ipv4Net, false))
|
2016-10-12 10:11:13 -04:00
|
|
|
} else if hasIpv6 {
|
|
|
|
conds.Add(ipv6Cond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
if v.PortRange != nil {
|
|
|
|
conds.Add(NewPortMatcher(*v.PortRange))
|
2016-10-12 10:11:13 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
if v.NetworkList != nil {
|
|
|
|
conds.Add(NewNetworkMatcher(v.NetworkList))
|
2016-10-12 10:11:13 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
if len(v.SourceCidr) > 0 {
|
2016-10-18 17:01:39 -04:00
|
|
|
ipv4Net := v2net.NewIPNet()
|
|
|
|
ipv6Cond := NewAnyCondition()
|
|
|
|
hasIpv6 := false
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
for _, ip := range v.SourceCidr {
|
2016-10-18 17:01:39 -04:00
|
|
|
switch len(ip.Ip) {
|
|
|
|
case net.IPv4len:
|
|
|
|
ipv4Net.AddIP(ip.Ip, byte(ip.Prefix))
|
|
|
|
case net.IPv6len:
|
|
|
|
hasIpv6 = true
|
|
|
|
matcher, err := NewCIDRMatcher(ip.Ip, ip.Prefix, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ipv6Cond.Add(matcher)
|
|
|
|
default:
|
2017-04-09 09:04:04 -04:00
|
|
|
return nil, newError("invalid IP length").AtError()
|
2016-10-18 17:01:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ipv4Net.IsEmpty() && hasIpv6 {
|
|
|
|
cond := NewAnyCondition()
|
|
|
|
cond.Add(NewIPv4Matcher(ipv4Net, true))
|
|
|
|
cond.Add(ipv6Cond)
|
|
|
|
conds.Add(cond)
|
|
|
|
} else if !ipv4Net.IsEmpty() {
|
|
|
|
conds.Add(NewIPv4Matcher(ipv4Net, true))
|
|
|
|
} else if hasIpv6 {
|
|
|
|
conds.Add(ipv6Cond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
if len(v.UserEmail) > 0 {
|
|
|
|
conds.Add(NewUserMatcher(v.UserEmail))
|
2016-10-18 17:01:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
if len(v.InboundTag) > 0 {
|
|
|
|
conds.Add(NewInboundTagMatcher(v.InboundTag))
|
2016-11-13 15:23:34 -05:00
|
|
|
}
|
|
|
|
|
2016-10-12 10:11:13 -04:00
|
|
|
if conds.Len() == 0 {
|
2017-04-09 09:04:04 -04:00
|
|
|
return nil, newError("this rule has no effective fields").AtError()
|
2016-10-12 10:11:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return conds, nil
|
2015-11-14 08:24:56 -05:00
|
|
|
}
|