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

127 lines
2.6 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 (
"errors"
"net"
v2net "v2ray.com/core/common/net"
2016-10-18 21:01:39 +00:00
"v2ray.com/core/proxy"
2016-10-12 14:11:13 +00:00
)
type Rule struct {
Tag string
Condition Condition
}
2016-10-18 21:01:39 +00:00
func (this *Rule) Apply(session *proxy.SessionInfo) bool {
return this.Condition.Apply(session)
2016-10-12 14:11:13 +00:00
}
func (this *RoutingRule) BuildCondition() (Condition, error) {
conds := NewConditionChan()
if len(this.Domain) > 0 {
anyCond := NewAnyCondition()
for _, domain := range this.Domain {
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-10-18 14:42:22 +00:00
if len(this.Cidr) > 0 {
ipv4Net := v2net.NewIPNet()
2016-10-12 14:11:13 +00:00
ipv6Cond := NewAnyCondition()
hasIpv6 := false
2016-10-18 14:42:22 +00:00
for _, ip := range this.Cidr {
2016-10-12 14:11:13 +00:00
switch len(ip.Ip) {
case net.IPv4len:
2016-10-18 14:42:22 +00:00
ipv4Net.AddIP(ip.Ip, byte(ip.Prefix))
2016-10-12 14:11:13 +00:00
case net.IPv6len:
hasIpv6 = true
2016-10-18 21:01:39 +00:00
matcher, err := NewCIDRMatcher(ip.Ip, ip.Prefix, false)
2016-10-12 14:11:13 +00:00
if err != nil {
return nil, err
}
ipv6Cond.Add(matcher)
default:
return nil, errors.New("Router: Invalid IP length.")
}
}
2016-10-18 14:42:22 +00:00
if !ipv4Net.IsEmpty() && hasIpv6 {
2016-10-12 14:11:13 +00:00
cond := NewAnyCondition()
2016-10-18 21:01:39 +00:00
cond.Add(NewIPv4Matcher(ipv4Net, false))
2016-10-12 14:11:13 +00:00
cond.Add(ipv6Cond)
conds.Add(cond)
2016-10-18 14:42:22 +00:00
} else if !ipv4Net.IsEmpty() {
2016-10-18 21:01:39 +00:00
conds.Add(NewIPv4Matcher(ipv4Net, false))
2016-10-12 14:11:13 +00:00
} else if hasIpv6 {
conds.Add(ipv6Cond)
}
}
if this.PortRange != nil {
conds.Add(NewPortMatcher(*this.PortRange))
}
if this.NetworkList != nil {
conds.Add(NewNetworkMatcher(this.NetworkList))
}
2016-10-18 21:01:39 +00:00
if len(this.SourceCidr) > 0 {
ipv4Net := v2net.NewIPNet()
ipv6Cond := NewAnyCondition()
hasIpv6 := false
for _, ip := range this.SourceCidr {
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:
return nil, errors.New("Router: Invalid IP length.")
}
}
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)
}
}
if len(this.UserEmail) > 0 {
conds.Add(NewUserMatcher(this.UserEmail))
}
2016-11-13 20:23:34 +00:00
if len(this.InboundTag) > 0 {
conds.Add(NewInboundTagMatcher(this.InboundTag))
}
2016-10-12 14:11:13 +00:00
if conds.Len() == 0 {
return nil, errors.New("Router: This rule has no effective fields.")
}
return conds, nil
2015-11-14 13:24:56 +00:00
}