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

230 lines
4.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-10-18 17:01:39 -04:00
"v2ray.com/core/proxy"
2016-01-17 10:20:49 -05:00
)
type Condition interface {
2016-10-18 17:01:39 -04:00
Apply(session *proxy.SessionInfo) bool
2016-01-17 10:20:49 -05:00
}
type ConditionChan []Condition
func NewConditionChan() *ConditionChan {
var condChan ConditionChan = make([]Condition, 0, 8)
return &condChan
}
2016-11-27 15:39:09 -05:00
func (v *ConditionChan) Add(cond Condition) *ConditionChan {
*v = append(*v, cond)
return v
2016-01-17 10:20:49 -05:00
}
2016-11-27 15:39:09 -05:00
func (v *ConditionChan) Apply(session *proxy.SessionInfo) bool {
for _, cond := range *v {
2016-10-18 17:01:39 -04:00
if !cond.Apply(session) {
2016-01-17 10:20:49 -05:00
return false
}
}
return true
}
2016-11-27 15:39:09 -05:00
func (v *ConditionChan) Len() int {
return len(*v)
2016-01-17 10:20:49 -05:00
}
2016-01-24 08:40:46 -05:00
type AnyCondition []Condition
func NewAnyCondition() *AnyCondition {
var anyCond AnyCondition = make([]Condition, 0, 8)
return &anyCond
}
2016-11-27 15:39:09 -05:00
func (v *AnyCondition) Add(cond Condition) *AnyCondition {
*v = append(*v, cond)
return v
2016-01-24 08:40:46 -05:00
}
2016-11-27 15:39:09 -05:00
func (v *AnyCondition) Apply(session *proxy.SessionInfo) bool {
for _, cond := range *v {
2016-10-18 17:01:39 -04:00
if cond.Apply(session) {
2016-01-24 08:40:46 -05:00
return true
}
}
return false
}
2016-11-27 15:39:09 -05:00
func (v *AnyCondition) Len() int {
return len(*v)
2016-01-24 08:40:46 -05:00
}
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
}
}
2016-11-27 15:39:09 -05:00
func (v *PlainDomainMatcher) Apply(session *proxy.SessionInfo) bool {
2016-10-18 17:01:39 -04:00
dest := session.Destination
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-11-27 15:39:09 -05:00
return strings.Contains(domain, v.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
}
2016-11-27 15:39:09 -05:00
func (v *RegexpDomainMatcher) Apply(session *proxy.SessionInfo) bool {
2016-10-18 17:01:39 -04:00
dest := session.Destination
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-11-27 15:39:09 -05:00
return v.pattern.MatchString(strings.ToLower(domain))
2016-01-17 10:20:49 -05:00
}
type CIDRMatcher struct {
2016-10-18 17:01:39 -04:00
cidr *net.IPNet
onSource bool
2016-01-17 10:20:49 -05:00
}
2016-10-18 17:01:39 -04:00
func NewCIDRMatcher(ip []byte, mask uint32, onSource bool) (*CIDRMatcher, error) {
2016-10-11 17:02:44 -04:00
cidr := &net.IPNet{
IP: net.IP(ip),
Mask: net.CIDRMask(int(mask), len(ip)),
2016-01-17 10:20:49 -05:00
}
return &CIDRMatcher{
2016-10-18 17:01:39 -04:00
cidr: cidr,
onSource: onSource,
2016-01-17 10:20:49 -05:00
}, nil
}
2016-11-27 15:39:09 -05:00
func (v *CIDRMatcher) Apply(session *proxy.SessionInfo) bool {
2016-10-18 17:01:39 -04:00
dest := session.Destination
2016-11-27 15:39:09 -05:00
if v.onSource {
2016-10-18 17:01:39 -04:00
dest = session.Source
}
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-11-27 15:39:09 -05:00
return v.cidr.Contains(dest.Address.IP())
2016-01-17 10:20:49 -05:00
}
type IPv4Matcher struct {
2016-10-18 17:01:39 -04:00
ipv4net *v2net.IPNet
onSource bool
2016-01-17 10:20:49 -05:00
}
2016-10-18 17:01:39 -04:00
func NewIPv4Matcher(ipnet *v2net.IPNet, onSource bool) *IPv4Matcher {
2016-01-17 10:20:49 -05:00
return &IPv4Matcher{
2016-10-18 17:01:39 -04:00
ipv4net: ipnet,
onSource: onSource,
2016-01-17 10:20:49 -05:00
}
}
2016-11-27 15:39:09 -05:00
func (v *IPv4Matcher) Apply(session *proxy.SessionInfo) bool {
2016-10-18 17:01:39 -04:00
dest := session.Destination
2016-11-27 15:39:09 -05:00
if v.onSource {
2016-10-18 17:01:39 -04:00
dest = session.Source
}
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-11-27 15:39:09 -05:00
return v.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,
}
}
2016-11-27 15:39:09 -05:00
func (v *PortMatcher) Apply(session *proxy.SessionInfo) bool {
return v.port.Contains(session.Destination.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,
}
}
2016-11-27 15:39:09 -05:00
func (v *NetworkMatcher) Apply(session *proxy.SessionInfo) bool {
return v.network.HasNetwork(session.Destination.Network)
2016-10-18 17:01:39 -04:00
}
type UserMatcher struct {
user []string
}
func NewUserMatcher(users []string) *UserMatcher {
return &UserMatcher{
user: users,
}
}
2016-11-27 15:39:09 -05:00
func (v *UserMatcher) Apply(session *proxy.SessionInfo) bool {
2016-10-18 17:01:39 -04:00
if session.User == nil {
return false
}
2016-11-27 15:39:09 -05:00
for _, u := range v.user {
2016-10-18 17:01:39 -04:00
if u == session.User.Email {
return true
}
}
return false
2016-01-17 10:20:49 -05:00
}
2016-11-13 15:23:34 -05:00
type InboundTagMatcher struct {
tags []string
}
func NewInboundTagMatcher(tags []string) *InboundTagMatcher {
return &InboundTagMatcher{
tags: tags,
}
}
2016-11-27 15:39:09 -05:00
func (v *InboundTagMatcher) Apply(session *proxy.SessionInfo) bool {
2016-11-13 15:23:34 -05:00
if session.Inbound == nil || len(session.Inbound.Tag) == 0 {
return false
}
2016-11-27 15:39:09 -05:00
for _, t := range v.tags {
2016-11-13 15:23:34 -05:00
if t == session.Inbound.Tag {
return true
}
}
return false
}