1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-18 05:25:23 +00:00
v2fly/app/router/config.go

113 lines
2.2 KiB
Go
Raw Normal View History

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 (
"context"
2016-10-12 14:11:13 +00:00
2017-08-29 12:32:54 +00:00
"v2ray.com/core/common/net"
2016-10-12 14:11:13 +00:00
)
type Rule struct {
Tag string
Condition Condition
}
2017-05-08 09:48:41 +00:00
func (r *Rule) Apply(ctx context.Context) bool {
return r.Condition.Apply(ctx)
2016-10-12 14:11:13 +00:00
}
2017-05-17 11:24:53 +00:00
func cidrToCondition(cidr []*CIDR, source bool) (Condition, error) {
2017-08-29 12:32:54 +00:00
ipv4Net := net.NewIPNetTable()
2017-05-17 11:24:53 +00: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:
return nil, newError("invalid IP length").AtError()
}
}
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 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 {
2016-10-12 14:11:13 +00:00
anyCond := NewAnyCondition()
2017-05-08 09:48:41 +00:00
for _, domain := range rr.Domain {
2017-05-08 10:18:13 +00:00
switch domain.Type {
case Domain_Plain:
2016-10-12 14:11:13 +00:00
anyCond.Add(NewPlainDomainMatcher(domain.Value))
2017-05-08 10:18:13 +00:00
case Domain_Regex:
2016-10-12 14:11:13 +00:00
matcher, err := NewRegexpDomainMatcher(domain.Value)
if err != nil {
return nil, err
}
anyCond.Add(matcher)
2017-05-08 10:18:13 +00:00
case Domain_Domain:
anyCond.Add(NewSubDomainMatcher(domain.Value))
default:
panic("Unknown domain type.")
2016-10-12 14:11:13 +00:00
}
}
conds.Add(anyCond)
}
2017-05-08 09:48:41 +00:00
if len(rr.Cidr) > 0 {
2017-05-17 11:24:53 +00:00
cond, err := cidrToCondition(rr.Cidr, false)
if err != nil {
return nil, err
2016-10-12 14:11:13 +00:00
}
2017-05-17 11:24:53 +00:00
conds.Add(cond)
2016-10-12 14:11:13 +00:00
}
2017-05-08 09:48:41 +00:00
if rr.PortRange != nil {
conds.Add(NewPortMatcher(*rr.PortRange))
2016-10-12 14:11:13 +00:00
}
2017-05-08 09:48:41 +00:00
if rr.NetworkList != nil {
conds.Add(NewNetworkMatcher(rr.NetworkList))
2016-10-12 14:11:13 +00:00
}
2017-05-08 09:48:41 +00:00
if len(rr.SourceCidr) > 0 {
2017-05-17 11:24:53 +00:00
cond, err := cidrToCondition(rr.SourceCidr, true)
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
}
2017-05-08 09:48:41 +00:00
if len(rr.UserEmail) > 0 {
conds.Add(NewUserMatcher(rr.UserEmail))
2016-10-18 21:01:39 +00:00
}
2017-05-08 09:48:41 +00:00
if len(rr.InboundTag) > 0 {
conds.Add(NewInboundTagMatcher(rr.InboundTag))
2016-11-13 20:23:34 +00:00
}
2016-10-12 14:11:13 +00:00
if conds.Len() == 0 {
2017-04-09 13:04:04 +00:00
return nil, newError("this rule has no effective fields").AtError()
2016-10-12 14:11:13 +00:00
}
return conds, nil
2015-11-14 13:24:56 +00:00
}