-
Notifications
You must be signed in to change notification settings - Fork 127
/
pattern.go
205 lines (171 loc) · 3.6 KB
/
pattern.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//
// date : 2016-05-13
// author: xjdrew
//
package kone
import (
"net"
"strings"
"github.com/xjdrew/kone/geoip"
"github.com/xjdrew/kone/tcpip"
)
type Pattern interface {
Proxy() string
Match(val interface{}) bool
}
// DOMAIN
type DomainPattern struct {
proxy string
domain string
}
func (p DomainPattern) Proxy() string {
return p.proxy
}
func (p DomainPattern) Match(val interface{}) bool {
v, ok := val.(string)
if !ok {
return false
}
v = strings.ToLower(v)
return v == p.domain
}
func NewDomainPattern(proxy, domain string) Pattern {
return DomainPattern{
proxy: proxy,
domain: strings.ToLower(domain),
}
}
// DOMAIN-SUFFIX
type DomainSuffixPattern struct {
proxy string
suffix string
}
func (p DomainSuffixPattern) Proxy() string {
return p.proxy
}
func (p DomainSuffixPattern) Match(val interface{}) bool {
v, ok := val.(string)
if !ok {
return false
}
v = strings.ToLower(v)
return strings.HasSuffix(v, p.suffix)
}
func NewDomainSuffixPattern(proxy, suffix string) Pattern {
return DomainSuffixPattern{
proxy: proxy,
suffix: strings.ToLower(suffix),
}
}
// DOMAIN-KEYWORD
type DomainKeywordPattern struct {
proxy string
key string
}
func (p DomainKeywordPattern) Proxy() string {
return p.proxy
}
func (p DomainKeywordPattern) Match(val interface{}) bool {
v, ok := val.(string)
if !ok {
return false
}
v = strings.ToLower(v)
return strings.Contains(v, p.key)
}
func NewDomainKeywordPattern(proxy string, key string) Pattern {
return DomainKeywordPattern{
proxy: proxy,
key: strings.ToLower(key),
}
}
// GEOIP
type GEOIPPattern struct {
proxy string
country string
}
func (p GEOIPPattern) Proxy() string {
return p.proxy
}
func (p GEOIPPattern) Match(val interface{}) bool {
var country string
switch ip := val.(type) {
case uint32:
country = geoip.QueryCountry(ip)
case net.IP:
country = geoip.QueryCountryByIP(ip)
}
return p.country == country
}
func NewGEOIPPattern(proxy string, country string) Pattern {
return GEOIPPattern{
proxy: proxy,
country: country,
}
}
// IP-CIDR
type IPCIDRPattern struct {
proxy string
ipNet *net.IPNet
}
func (p IPCIDRPattern) Proxy() string {
return p.proxy
}
func (p IPCIDRPattern) Match(val interface{}) bool {
switch ip := val.(type) {
case net.IP:
return p.ipNet.Contains(ip)
case uint32:
return p.ipNet.Contains(tcpip.ConvertUint32ToIPv4(ip))
}
return false
}
func NewIPCIDRPattern(proxy string, ipNet *net.IPNet) Pattern {
return IPCIDRPattern{
proxy: proxy,
ipNet: ipNet,
}
}
// FINAL
type FinalPattern struct {
proxy string
}
func (p FinalPattern) Proxy() string {
return p.proxy
}
func (p FinalPattern) Match(val interface{}) bool {
return true
}
func NewFinalPattern(proxy string) FinalPattern {
return FinalPattern{proxy: proxy}
}
func CreatePattern(rc RuleConfig) Pattern {
proxy := rc.Proxy
pattern := rc.Pattern
schema := strings.ToUpper(rc.Schema)
switch schema {
case "DOMAIN":
return NewDomainPattern(proxy, pattern)
case "DOMAIN-SUFFIX":
return NewDomainSuffixPattern(proxy, pattern)
case "DOMAIN-KEYWORD":
return NewDomainKeywordPattern(proxy, pattern)
case "IP-CIDR":
fallthrough
case "IP-CIDR6":
if proxy == "DIRECT" { // all IPNet default proxy is DIRECT
logger.Debugf("skip DIRECT rule: %s,%s,%s", rc.Schema, rc.Pattern, rc.Proxy)
return nil
}
_, ipNet, err := net.ParseCIDR(pattern)
if err == nil {
return NewIPCIDRPattern(proxy, ipNet)
}
case "GEOIP":
return NewGEOIPPattern(proxy, pattern)
case "FINAL":
return NewFinalPattern(proxy)
}
logger.Errorf("invalid rule: %s,%s,%s", rc.Schema, rc.Pattern, rc.Proxy)
return nil
}