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

425 lines
7.7 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 (
"context"
2016-01-17 10:20:49 -05:00
"regexp"
2016-05-24 15:55:46 -04:00
"strings"
2017-11-06 16:30:56 -05:00
"sync"
"time"
2016-01-17 10:20:49 -05:00
2017-08-29 08:32:54 -04:00
"v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
2016-10-18 17:01:39 -04:00
"v2ray.com/core/proxy"
2016-01-17 10:20:49 -05:00
)
type Condition interface {
Apply(ctx context.Context) 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
}
func (v *ConditionChan) Apply(ctx context.Context) bool {
2016-11-27 15:39:09 -05:00
for _, cond := range *v {
if !cond.Apply(ctx) {
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
}
func (v *AnyCondition) Apply(ctx context.Context) bool {
2016-11-27 15:39:09 -05:00
for _, cond := range *v {
if cond.Apply(ctx) {
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
}
2017-11-06 16:30:56 -05:00
type timedResult struct {
timestamp time.Time
result bool
}
2017-11-06 15:12:28 -05:00
type CachableDomainMatcher struct {
2017-11-06 16:30:56 -05:00
sync.Mutex
2017-11-06 15:12:28 -05:00
matchers []domainMatcher
2017-11-06 16:30:56 -05:00
cache map[string]timedResult
lastScan time.Time
2017-11-06 15:12:28 -05:00
}
2016-01-17 10:20:49 -05:00
2017-11-06 15:12:28 -05:00
func NewCachableDomainMatcher() *CachableDomainMatcher {
return &CachableDomainMatcher{
matchers: make([]domainMatcher, 0, 64),
2017-11-06 16:30:56 -05:00
cache: make(map[string]timedResult, 512),
2017-11-06 15:12:28 -05:00
}
}
func (m *CachableDomainMatcher) Add(domain *Domain) error {
switch domain.Type {
case Domain_Plain:
m.matchers = append(m.matchers, NewPlainDomainMatcher(domain.Value))
case Domain_Regex:
rm, err := NewRegexpDomainMatcher(domain.Value)
if err != nil {
return err
}
m.matchers = append(m.matchers, rm)
case Domain_Domain:
m.matchers = append(m.matchers, NewSubDomainMatcher(domain.Value))
default:
2017-12-27 16:25:12 -05:00
return newError("unknown domain type: ", domain.Type).AtWarning()
2017-11-06 15:12:28 -05:00
}
return nil
2016-01-17 10:20:49 -05:00
}
2017-11-06 16:30:56 -05:00
func (m *CachableDomainMatcher) applyInternal(domain string) bool {
for _, matcher := range m.matchers {
if matcher.Apply(domain) {
return true
}
}
return false
}
type cacheResult int
const (
cacheMiss cacheResult = iota
cacheHitTrue
cacheHitFalse
)
func (m *CachableDomainMatcher) findInCache(domain string) cacheResult {
m.Lock()
defer m.Unlock()
r, f := m.cache[domain]
if !f {
return cacheMiss
}
r.timestamp = time.Now()
m.cache[domain] = r
if r.result {
return cacheHitTrue
}
return cacheHitFalse
}
func (m *CachableDomainMatcher) ApplyDomain(domain string) bool {
if len(m.matchers) < 64 {
return m.applyInternal(domain)
}
cr := m.findInCache(domain)
if cr == cacheHitTrue {
return true
}
if cr == cacheHitFalse {
return false
}
r := m.applyInternal(domain)
m.Lock()
defer m.Unlock()
m.cache[domain] = timedResult{
result: r,
timestamp: time.Now(),
}
now := time.Now()
if len(m.cache) > 256 && now.Sub(m.lastScan)/time.Second > 5 {
now := time.Now()
for k, v := range m.cache {
if now.Sub(v.timestamp)/time.Second > 60 {
2018-05-31 06:05:25 -04:00
delete(m.cache, k)
2017-11-06 16:30:56 -05:00
}
}
2018-05-31 06:05:25 -04:00
2017-11-06 16:30:56 -05:00
m.lastScan = now
}
return r
}
2017-11-06 15:12:28 -05:00
func (m *CachableDomainMatcher) Apply(ctx context.Context) bool {
2017-02-09 16:49:38 -05:00
dest, ok := proxy.TargetFromContext(ctx)
if !ok {
return false
}
2016-09-20 05:53:05 -04:00
if !dest.Address.Family().IsDomain() {
2016-01-17 10:20:49 -05:00
return false
}
2017-11-06 16:30:56 -05:00
return m.ApplyDomain(dest.Address.Domain())
2017-11-06 15:12:28 -05:00
}
type domainMatcher interface {
Apply(domain string) bool
}
type PlainDomainMatcher string
func NewPlainDomainMatcher(pattern string) PlainDomainMatcher {
return PlainDomainMatcher(pattern)
}
func (v PlainDomainMatcher) Apply(domain string) bool {
2017-05-08 06:25:36 -04:00
return strings.Contains(domain, string(v))
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
}
2017-11-06 15:12:28 -05:00
func (v *RegexpDomainMatcher) Apply(domain string) bool {
2016-11-27 15:39:09 -05:00
return v.pattern.MatchString(strings.ToLower(domain))
2016-01-17 10:20:49 -05:00
}
2017-05-08 06:25:36 -04:00
type SubDomainMatcher string
2017-05-08 06:18:13 -04:00
2017-11-06 15:12:28 -05:00
func NewSubDomainMatcher(p string) SubDomainMatcher {
2017-05-08 06:25:36 -04:00
return SubDomainMatcher(p)
2017-05-08 06:18:13 -04:00
}
2017-11-06 15:12:28 -05:00
func (m SubDomainMatcher) Apply(domain string) bool {
2017-05-08 06:25:36 -04:00
pattern := string(m)
if !strings.HasSuffix(domain, pattern) {
2017-05-08 06:18:13 -04:00
return false
}
2017-05-08 06:25:36 -04:00
return len(domain) == len(pattern) || domain[len(domain)-len(pattern)-1] == '.'
2017-05-08 06:18:13 -04:00
}
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),
2017-05-17 07:24:53 -04:00
Mask: net.CIDRMask(int(mask), len(ip)*8),
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
}
func (v *CIDRMatcher) Apply(ctx context.Context) bool {
2017-01-28 03:04:29 -05:00
ips := make([]net.IP, 0, 4)
2017-11-15 06:55:47 -05:00
if resolver, ok := proxy.ResolvedIPsFromContext(ctx); ok {
resolvedIPs := resolver.Resolve()
for _, rip := range resolvedIPs {
2017-01-28 03:04:29 -05:00
if !rip.Family().IsIPv6() {
continue
}
2017-01-27 15:19:46 -05:00
ips = append(ips, rip.IP())
}
}
2017-08-29 08:32:54 -04:00
var dest net.Destination
2017-02-09 16:49:38 -05:00
var ok bool
2016-11-27 15:39:09 -05:00
if v.onSource {
2017-02-09 16:49:38 -05:00
dest, ok = proxy.SourceFromContext(ctx)
} else {
2017-02-09 16:49:38 -05:00
dest, ok = proxy.TargetFromContext(ctx)
2016-10-18 17:01:39 -04:00
}
2017-02-09 16:49:38 -05:00
if ok && dest.Address.Family().IsIPv6() {
2017-01-27 15:19:46 -05:00
ips = append(ips, dest.Address.IP())
}
for _, ip := range ips {
if v.cidr.Contains(ip) {
return true
}
}
return false
2016-01-17 10:20:49 -05:00
}
type IPv4Matcher struct {
2017-08-29 08:32:54 -04:00
ipv4net *net.IPNetTable
2016-10-18 17:01:39 -04:00
onSource bool
2016-01-17 10:20:49 -05:00
}
2017-08-29 08:32:54 -04:00
func NewIPv4Matcher(ipnet *net.IPNetTable, 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
}
}
func (v *IPv4Matcher) Apply(ctx context.Context) bool {
2017-01-28 03:04:29 -05:00
ips := make([]net.IP, 0, 4)
2017-11-15 06:55:47 -05:00
if resolver, ok := proxy.ResolvedIPsFromContext(ctx); ok {
resolvedIPs := resolver.Resolve()
for _, rip := range resolvedIPs {
2017-01-28 03:04:29 -05:00
if !rip.Family().IsIPv4() {
continue
}
2017-01-27 15:19:46 -05:00
ips = append(ips, rip.IP())
}
}
2017-08-29 08:32:54 -04:00
var dest net.Destination
2017-02-09 16:49:38 -05:00
var ok bool
2016-11-27 15:39:09 -05:00
if v.onSource {
2017-02-09 16:49:38 -05:00
dest, ok = proxy.SourceFromContext(ctx)
} else {
2017-02-09 16:49:38 -05:00
dest, ok = proxy.TargetFromContext(ctx)
2016-10-18 17:01:39 -04:00
}
2017-01-27 14:38:01 -05:00
2017-02-09 16:49:38 -05:00
if ok && dest.Address.Family().IsIPv4() {
2017-01-27 15:19:46 -05:00
ips = append(ips, dest.Address.IP())
}
for _, ip := range ips {
if v.ipv4net.Contains(ip) {
return true
}
}
return false
2016-01-17 10:20:49 -05:00
}
type PortMatcher struct {
2017-08-29 08:32:54 -04:00
port net.PortRange
2016-01-17 10:20:49 -05:00
}
2017-08-29 08:32:54 -04:00
func NewPortMatcher(portRange net.PortRange) *PortMatcher {
2016-01-17 10:20:49 -05:00
return &PortMatcher{
port: portRange,
}
}
func (v *PortMatcher) Apply(ctx context.Context) bool {
2017-02-09 16:49:38 -05:00
dest, ok := proxy.TargetFromContext(ctx)
if !ok {
return false
}
return v.port.Contains(dest.Port)
2016-01-17 10:20:49 -05:00
}
type NetworkMatcher struct {
2017-08-29 08:32:54 -04:00
network *net.NetworkList
2016-01-17 10:20:49 -05:00
}
2017-08-29 08:32:54 -04:00
func NewNetworkMatcher(network *net.NetworkList) *NetworkMatcher {
2016-01-17 10:20:49 -05:00
return &NetworkMatcher{
network: network,
}
}
func (v *NetworkMatcher) Apply(ctx context.Context) bool {
2017-02-09 16:49:38 -05:00
dest, ok := proxy.TargetFromContext(ctx)
if !ok {
return false
}
return v.network.HasNetwork(dest.Network)
2016-10-18 17:01:39 -04:00
}
type UserMatcher struct {
user []string
}
func NewUserMatcher(users []string) *UserMatcher {
2017-05-08 05:48:41 -04:00
usersCopy := make([]string, 0, len(users))
for _, user := range users {
if len(user) > 0 {
usersCopy = append(usersCopy, user)
}
}
2016-10-18 17:01:39 -04:00
return &UserMatcher{
2017-05-08 05:48:41 -04:00
user: usersCopy,
2016-10-18 17:01:39 -04:00
}
}
func (v *UserMatcher) Apply(ctx context.Context) bool {
user := protocol.UserFromContext(ctx)
if user == nil {
2016-10-18 17:01:39 -04:00
return false
}
2016-11-27 15:39:09 -05:00
for _, u := range v.user {
if u == user.Email {
2016-10-18 17:01:39 -04:00
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 {
2017-05-08 05:48:41 -04:00
tagsCopy := make([]string, 0, len(tags))
for _, tag := range tags {
if len(tag) > 0 {
tagsCopy = append(tagsCopy, tag)
}
}
2016-11-13 15:23:34 -05:00
return &InboundTagMatcher{
2017-05-08 05:48:41 -04:00
tags: tagsCopy,
2016-11-13 15:23:34 -05:00
}
}
func (v *InboundTagMatcher) Apply(ctx context.Context) bool {
2017-02-09 16:49:38 -05:00
tag, ok := proxy.InboundTagFromContext(ctx)
if !ok {
2016-11-13 15:23:34 -05:00
return false
}
2016-11-27 15:39:09 -05:00
for _, t := range v.tags {
if t == tag {
2016-11-13 15:23:34 -05:00
return true
}
}
return false
}