1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-14 16:25:57 -05:00
v2fly/app/router/condition.go

170 lines
3.3 KiB
Go
Raw Normal View History

2016-10-12 10:11:13 -04:00
package router
2016-01-17 10:20:49 -05:00
import (
"net"
"regexp"
2016-05-24 15:55:46 -04:00
"strings"
2016-01-17 10:20:49 -05:00
2016-08-20 14:55:45 -04:00
v2net "v2ray.com/core/common/net"
2016-01-17 10:20:49 -05:00
)
type Condition interface {
Apply(dest v2net.Destination) bool
}
type ConditionChan []Condition
func NewConditionChan() *ConditionChan {
var condChan ConditionChan = make([]Condition, 0, 8)
return &condChan
}
func (this *ConditionChan) Add(cond Condition) *ConditionChan {
*this = append(*this, cond)
return this
}
func (this *ConditionChan) Apply(dest v2net.Destination) bool {
for _, cond := range *this {
if !cond.Apply(dest) {
return false
}
}
return true
}
func (this *ConditionChan) Len() int {
return len(*this)
}
2016-01-24 08:40:46 -05:00
type AnyCondition []Condition
func NewAnyCondition() *AnyCondition {
var anyCond AnyCondition = make([]Condition, 0, 8)
return &anyCond
}
func (this *AnyCondition) Add(cond Condition) *AnyCondition {
*this = append(*this, cond)
return this
}
func (this *AnyCondition) Apply(dest v2net.Destination) bool {
for _, cond := range *this {
if cond.Apply(dest) {
return true
}
}
return false
}
func (this *AnyCondition) Len() int {
return len(*this)
}
2016-01-17 10:20:49 -05:00
type PlainDomainMatcher struct {
2016-05-24 15:55:46 -04:00
pattern string
2016-01-17 10:20:49 -05:00
}
func NewPlainDomainMatcher(pattern string) *PlainDomainMatcher {
return &PlainDomainMatcher{
2016-05-24 15:55:46 -04:00
pattern: pattern,
2016-01-17 10:20:49 -05:00
}
}
func (this *PlainDomainMatcher) Apply(dest v2net.Destination) bool {
2016-09-20 05:53:05 -04:00
if !dest.Address.Family().IsDomain() {
2016-01-17 10:20:49 -05:00
return false
}
2016-09-20 05:53:05 -04:00
domain := dest.Address.Domain()
2016-05-24 15:55:46 -04:00
return strings.Contains(domain, this.pattern)
2016-01-17 10:20:49 -05:00
}
type RegexpDomainMatcher struct {
pattern *regexp.Regexp
}
func NewRegexpDomainMatcher(pattern string) (*RegexpDomainMatcher, error) {
r, err := regexp.Compile(pattern)
if err != nil {
return nil, err
}
return &RegexpDomainMatcher{
pattern: r,
}, nil
}
func (this *RegexpDomainMatcher) Apply(dest v2net.Destination) bool {
2016-09-20 05:53:05 -04:00
if !dest.Address.Family().IsDomain() {
2016-01-17 10:20:49 -05:00
return false
}
2016-09-20 05:53:05 -04:00
domain := dest.Address.Domain()
2016-05-24 15:55:46 -04:00
return this.pattern.MatchString(strings.ToLower(domain))
2016-01-17 10:20:49 -05:00
}
type CIDRMatcher struct {
cidr *net.IPNet
}
2016-10-11 17:02:44 -04:00
func NewCIDRMatcher(ip []byte, mask uint32) (*CIDRMatcher, error) {
cidr := &net.IPNet{
IP: net.IP(ip),
Mask: net.CIDRMask(int(mask), len(ip)),
2016-01-17 10:20:49 -05:00
}
return &CIDRMatcher{
cidr: cidr,
}, nil
}
func (this *CIDRMatcher) Apply(dest v2net.Destination) bool {
2016-09-20 05:53:05 -04:00
if !dest.Address.Family().Either(v2net.AddressFamilyIPv4, v2net.AddressFamilyIPv6) {
2016-01-17 10:20:49 -05:00
return false
}
2016-09-20 05:53:05 -04:00
return this.cidr.Contains(dest.Address.IP())
2016-01-17 10:20:49 -05:00
}
type IPv4Matcher struct {
ipv4net *v2net.IPNet
}
func NewIPv4Matcher(ipnet *v2net.IPNet) *IPv4Matcher {
return &IPv4Matcher{
ipv4net: ipnet,
}
}
func (this *IPv4Matcher) Apply(dest v2net.Destination) bool {
2016-09-20 05:53:05 -04:00
if !dest.Address.Family().Either(v2net.AddressFamilyIPv4) {
2016-01-17 10:20:49 -05:00
return false
}
2016-09-20 05:53:05 -04:00
return this.ipv4net.Contains(dest.Address.IP())
2016-01-17 10:20:49 -05:00
}
type PortMatcher struct {
port v2net.PortRange
}
func NewPortMatcher(portRange v2net.PortRange) *PortMatcher {
return &PortMatcher{
port: portRange,
}
}
func (this *PortMatcher) Apply(dest v2net.Destination) bool {
2016-09-20 05:53:05 -04:00
return this.port.Contains(dest.Port)
2016-01-17 10:20:49 -05:00
}
type NetworkMatcher struct {
network *v2net.NetworkList
}
func NewNetworkMatcher(network *v2net.NetworkList) *NetworkMatcher {
return &NetworkMatcher{
network: network,
}
}
func (this *NetworkMatcher) Apply(dest v2net.Destination) bool {
2016-09-20 05:53:05 -04:00
return this.network.HasNetwork(dest.Network)
2016-01-17 10:20:49 -05:00
}