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
|
|
|
|
2017-08-29 08:32:54 -04:00
|
|
|
"v2ray.com/core/common/net"
|
2016-10-12 10:11:13 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Rule struct {
|
|
|
|
Tag string
|
|
|
|
Condition Condition
|
|
|
|
}
|
|
|
|
|
2017-05-08 05:48:41 -04:00
|
|
|
func (r *Rule) Apply(ctx context.Context) bool {
|
|
|
|
return r.Condition.Apply(ctx)
|
2016-10-12 10:11:13 -04:00
|
|
|
}
|
|
|
|
|
2017-05-17 07:24:53 -04:00
|
|
|
func cidrToCondition(cidr []*CIDR, source bool) (Condition, error) {
|
2017-08-29 08:32:54 -04:00
|
|
|
ipv4Net := net.NewIPNetTable()
|
2017-05-17 07:24:53 -04:00
|
|
|
ipv6Cond := NewAnyCondition()
|
|
|
|
hasIpv6 := false
|
|
|
|
|
|
|
|
for _, ip := range cidr {
|
|
|
|
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, source)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ipv6Cond.Add(matcher)
|
|
|
|
default:
|
2017-12-27 16:25:12 -05:00
|
|
|
return nil, newError("invalid IP length").AtWarning()
|
2017-05-17 07:24:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ipv4Net.IsEmpty() && hasIpv6 {
|
|
|
|
cond := NewAnyCondition()
|
|
|
|
cond.Add(NewIPv4Matcher(ipv4Net, source))
|
|
|
|
cond.Add(ipv6Cond)
|
|
|
|
return cond, nil
|
|
|
|
} else if !ipv4Net.IsEmpty() {
|
|
|
|
return NewIPv4Matcher(ipv4Net, source), nil
|
|
|
|
} else {
|
|
|
|
return ipv6Cond, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-08-19 16:28:02 -04:00
|
|
|
matcher, err := NewDomainMatcher(rr.Domain)
|
2018-08-19 15:04:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to build domain condition").Base(err)
|
2016-10-12 10:11:13 -04:00
|
|
|
}
|
2017-11-06 15:12:28 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-05-08 05:48:41 -04:00
|
|
|
if rr.PortRange != nil {
|
|
|
|
conds.Add(NewPortMatcher(*rr.PortRange))
|
2016-10-12 10:11:13 -04:00
|
|
|
}
|
|
|
|
|
2017-05-08 05:48:41 -04:00
|
|
|
if rr.NetworkList != nil {
|
|
|
|
conds.Add(NewNetworkMatcher(rr.NetworkList))
|
2016-10-12 10:11:13 -04:00
|
|
|
}
|
|
|
|
|
2018-04-06 09:56:53 -04:00
|
|
|
if len(rr.Cidr) > 0 {
|
|
|
|
cond, err := cidrToCondition(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-04-06 09:56:53 -04:00
|
|
|
if len(rr.SourceCidr) > 0 {
|
|
|
|
cond, err := cidrToCondition(rr.SourceCidr, true)
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|