1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-10 09:50:43 +00:00
v2fly/app/router/condition.go

230 lines
4.4 KiB
Go
Raw Normal View History

2016-10-12 14:11:13 +00:00
package router
2016-01-17 15:20:49 +00:00
import (
"net"
"regexp"
2016-05-24 19:55:46 +00:00
"strings"
2016-01-17 15:20:49 +00:00
2016-08-20 18:55:45 +00:00
v2net "v2ray.com/core/common/net"
2016-10-18 21:01:39 +00:00
"v2ray.com/core/proxy"
2016-01-17 15:20:49 +00:00
)
type Condition interface {
2016-10-18 21:01:39 +00:00
Apply(session *proxy.SessionInfo) bool
2016-01-17 15:20:49 +00:00
}
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
}
2016-10-18 21:01:39 +00:00
func (this *ConditionChan) Apply(session *proxy.SessionInfo) bool {
2016-01-17 15:20:49 +00:00
for _, cond := range *this {
2016-10-18 21:01:39 +00:00
if !cond.Apply(session) {
2016-01-17 15:20:49 +00:00
return false
}
}
return true
}
func (this *ConditionChan) Len() int {
return len(*this)
}
2016-01-24 13:40:46 +00: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
}
2016-10-18 21:01:39 +00:00
func (this *AnyCondition) Apply(session *proxy.SessionInfo) bool {
2016-01-24 13:40:46 +00:00
for _, cond := range *this {
2016-10-18 21:01:39 +00:00
if cond.Apply(session) {
2016-01-24 13:40:46 +00:00
return true
}
}
return false
}
func (this *AnyCondition) Len() int {
return len(*this)
}
2016-01-17 15:20:49 +00:00
type PlainDomainMatcher struct {
2016-05-24 19:55:46 +00:00
pattern string
2016-01-17 15:20:49 +00:00
}
func NewPlainDomainMatcher(pattern string) *PlainDomainMatcher {
return &PlainDomainMatcher{
2016-05-24 19:55:46 +00:00
pattern: pattern,
2016-01-17 15:20:49 +00:00
}
}
2016-10-18 21:01:39 +00:00
func (this *PlainDomainMatcher) Apply(session *proxy.SessionInfo) bool {
dest := session.Destination
2016-09-20 09:53:05 +00:00
if !dest.Address.Family().IsDomain() {
2016-01-17 15:20:49 +00:00
return false
}
2016-09-20 09:53:05 +00:00
domain := dest.Address.Domain()
2016-05-24 19:55:46 +00:00
return strings.Contains(domain, this.pattern)
2016-01-17 15:20:49 +00: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
}
2016-10-18 21:01:39 +00:00
func (this *RegexpDomainMatcher) Apply(session *proxy.SessionInfo) bool {
dest := session.Destination
2016-09-20 09:53:05 +00:00
if !dest.Address.Family().IsDomain() {
2016-01-17 15:20:49 +00:00
return false
}
2016-09-20 09:53:05 +00:00
domain := dest.Address.Domain()
2016-05-24 19:55:46 +00:00
return this.pattern.MatchString(strings.ToLower(domain))
2016-01-17 15:20:49 +00:00
}
type CIDRMatcher struct {
2016-10-18 21:01:39 +00:00
cidr *net.IPNet
onSource bool
2016-01-17 15:20:49 +00:00
}
2016-10-18 21:01:39 +00:00
func NewCIDRMatcher(ip []byte, mask uint32, onSource bool) (*CIDRMatcher, error) {
2016-10-11 21:02:44 +00:00
cidr := &net.IPNet{
IP: net.IP(ip),
Mask: net.CIDRMask(int(mask), len(ip)),
2016-01-17 15:20:49 +00:00
}
return &CIDRMatcher{
2016-10-18 21:01:39 +00:00
cidr: cidr,
onSource: onSource,
2016-01-17 15:20:49 +00:00
}, nil
}
2016-10-18 21:01:39 +00:00
func (this *CIDRMatcher) Apply(session *proxy.SessionInfo) bool {
dest := session.Destination
if this.onSource {
dest = session.Source
}
2016-09-20 09:53:05 +00:00
if !dest.Address.Family().Either(v2net.AddressFamilyIPv4, v2net.AddressFamilyIPv6) {
2016-01-17 15:20:49 +00:00
return false
}
2016-09-20 09:53:05 +00:00
return this.cidr.Contains(dest.Address.IP())
2016-01-17 15:20:49 +00:00
}
type IPv4Matcher struct {
2016-10-18 21:01:39 +00:00
ipv4net *v2net.IPNet
onSource bool
2016-01-17 15:20:49 +00:00
}
2016-10-18 21:01:39 +00:00
func NewIPv4Matcher(ipnet *v2net.IPNet, onSource bool) *IPv4Matcher {
2016-01-17 15:20:49 +00:00
return &IPv4Matcher{
2016-10-18 21:01:39 +00:00
ipv4net: ipnet,
onSource: onSource,
2016-01-17 15:20:49 +00:00
}
}
2016-10-18 21:01:39 +00:00
func (this *IPv4Matcher) Apply(session *proxy.SessionInfo) bool {
dest := session.Destination
if this.onSource {
dest = session.Source
}
2016-09-20 09:53:05 +00:00
if !dest.Address.Family().Either(v2net.AddressFamilyIPv4) {
2016-01-17 15:20:49 +00:00
return false
}
2016-09-20 09:53:05 +00:00
return this.ipv4net.Contains(dest.Address.IP())
2016-01-17 15:20:49 +00:00
}
type PortMatcher struct {
port v2net.PortRange
}
func NewPortMatcher(portRange v2net.PortRange) *PortMatcher {
return &PortMatcher{
port: portRange,
}
}
2016-10-18 21:01:39 +00:00
func (this *PortMatcher) Apply(session *proxy.SessionInfo) bool {
return this.port.Contains(session.Destination.Port)
2016-01-17 15:20:49 +00:00
}
type NetworkMatcher struct {
network *v2net.NetworkList
}
func NewNetworkMatcher(network *v2net.NetworkList) *NetworkMatcher {
return &NetworkMatcher{
network: network,
}
}
2016-10-18 21:01:39 +00:00
func (this *NetworkMatcher) Apply(session *proxy.SessionInfo) bool {
return this.network.HasNetwork(session.Destination.Network)
}
type UserMatcher struct {
user []string
}
func NewUserMatcher(users []string) *UserMatcher {
return &UserMatcher{
user: users,
}
}
func (this *UserMatcher) Apply(session *proxy.SessionInfo) bool {
if session.User == nil {
return false
}
for _, u := range this.user {
if u == session.User.Email {
return true
}
}
return false
2016-01-17 15:20:49 +00:00
}
2016-11-13 20:23:34 +00:00
type InboundTagMatcher struct {
tags []string
}
func NewInboundTagMatcher(tags []string) *InboundTagMatcher {
return &InboundTagMatcher{
tags: tags,
}
}
func (this *InboundTagMatcher) Apply(session *proxy.SessionInfo) bool {
if session.Inbound == nil || len(session.Inbound.Tag) == 0 {
return false
}
for _, t := range this.tags {
if t == session.Inbound.Tag {
return true
}
}
return false
}