-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrules.go
346 lines (326 loc) · 8.32 KB
/
rules.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Released under MIT License
// Copyright (c) 2020 domosekai
// Custom routing rules
package main
import (
"bufio"
"log"
"net"
"os"
"sort"
"strconv"
"strings"
"sync"
)
type ipRule struct {
ipnet6 net.IPNet
route int
}
type ipRuleList struct {
rules []ipRule
elseRoute int
rw sync.RWMutex
}
type hostRule struct {
exact string
left string
middle string
right string
domain string
any bool
route int
}
type hostRuleList struct {
rules []hostRule
rw sync.RWMutex
}
var (
ipRules ipRuleList
hostRules hostRuleList
httpRules hostRuleList
)
type byByte []ipRule
func (n byByte) Len() int { return len(n) }
func (n byByte) Less(i, j int) bool {
for b := 0; b < net.IPv6len; b++ {
if n[i].ipnet6.IP[b] < n[j].ipnet6.IP[b] {
return true
} else if n[i].ipnet6.IP[b] > n[j].ipnet6.IP[b] {
return false
}
}
return false
}
func (n byByte) Swap(i, j int) { n[i], n[j] = n[j], n[i] }
func (list *ipRuleList) parseIPList(file string) {
f, err := os.Open(file)
if err != nil {
log.Fatalln(err)
}
defer f.Close()
list.rw.Lock()
defer list.rw.Unlock()
list.rules = nil
list.elseRoute = 0
scanner := bufio.NewScanner(f)
for scanner.Scan() {
r := strings.Split(scanner.Text(), " ")
if len(r) < 2 {
continue
}
route, err := strconv.Atoi(r[0])
if err != nil {
continue
}
var ipstr string
if r[1] == "ELSE" {
list.elseRoute = route
continue
}
if !strings.Contains(r[1], "/") {
// Not CIDR
if strings.Contains(r[1], ":") {
ipstr = r[1] + "/128"
} else {
ipstr = r[1] + "/32"
}
} else {
// CIDR
ipstr = r[1]
}
// ParseCIDR returns 16-byte net.IP and 4 or 16-byte net.IPNet (both IP and Mask)
if _, ipnet, err := net.ParseCIDR(ipstr); err != nil {
log.Fatalf("Invalid IP rule: %s", scanner.Text())
} else {
switch len(ipnet.Mask) {
case net.IPv4len:
list.rules = append(list.rules, ipRule{net.IPNet{IP: ipnet.IP.To16(), Mask: mark4to6(ipnet.Mask)}, route})
case net.IPv6len:
list.rules = append(list.rules, ipRule{*ipnet, route})
}
}
}
}
func (list *hostRuleList) parseHostList(file string) {
f, err := os.Open(file)
if err != nil {
log.Fatalln(err)
}
defer f.Close()
list.rw.Lock()
defer list.rw.Unlock()
list.rules = nil
scanner := bufio.NewScanner(f)
for scanner.Scan() {
r := strings.Split(scanner.Text(), " ")
if len(r) < 2 {
continue
}
route, err := strconv.Atoi(r[0])
if err != nil {
continue
}
if r[1] == "*" {
list.rules = append(list.rules, hostRule{any: true, route: route})
continue
}
if strings.HasPrefix(r[1], "\"") && strings.HasSuffix(r[1], "\"") {
d := strings.TrimSuffix(strings.TrimPrefix(r[1], "\""), "\"")
if d != "" {
list.rules = append(list.rules, hostRule{exact: strings.ToLower(d), route: route})
}
continue
}
if strings.Contains(r[1], "**") {
log.Fatalf("Invalid host rule: %s", scanner.Text())
}
b1 := strings.HasPrefix(r[1], "*")
b2 := strings.HasSuffix(r[1], "*")
parts := strings.Split(strings.TrimSuffix(strings.TrimPrefix(r[1], "*"), "*"), "*")
switch len(parts) {
case 1:
if b1 && b2 {
list.rules = append(list.rules, hostRule{middle: strings.ToLower(parts[0]), route: route})
} else if b1 && !b2 {
list.rules = append(list.rules, hostRule{right: strings.ToLower(parts[0]), route: route})
} else if !b1 && b2 {
list.rules = append(list.rules, hostRule{left: strings.ToLower(parts[0]), route: route})
} else {
// Treat as dnsmasq-style domain if no wildcard
list.rules = append(list.rules, hostRule{domain: strings.ToLower(parts[0]), route: route})
}
case 2:
if b1 && !b2 {
list.rules = append(list.rules, hostRule{middle: strings.ToLower(parts[0]), right: strings.ToLower(parts[1]), route: route})
} else if !b1 && b2 {
list.rules = append(list.rules, hostRule{left: strings.ToLower(parts[0]), middle: strings.ToLower(parts[1]), route: route})
} else if !b1 && !b2 {
list.rules = append(list.rules, hostRule{left: strings.ToLower(parts[0]), right: strings.ToLower(parts[1]), route: route})
}
case 3:
if !b1 && !b2 {
list.rules = append(list.rules, hostRule{left: strings.ToLower(parts[0]), middle: strings.ToLower(parts[1]), right: strings.ToLower(parts[2]), route: route})
}
default:
log.Fatalf("Invalid host rule: %s", scanner.Text())
}
}
}
func (list *hostRuleList) parseHTTPRules(str string) {
list.rw.Lock()
defer list.rw.Unlock()
list.rules = nil
for _, s := range strings.Split(str, ",") {
s = strings.TrimSpace(s)
if s == "" {
continue
}
if s == "*" {
list.rules = append(list.rules, hostRule{any: true, route: 2})
continue
}
if strings.Contains(s, "**") {
continue
}
b1 := strings.HasPrefix(s, "*")
b2 := strings.HasSuffix(s, "*")
parts := strings.Split(strings.TrimSuffix(strings.TrimPrefix(s, "*"), "*"), "*")
switch len(parts) {
case 1:
if b1 && b2 {
list.rules = append(list.rules, hostRule{middle: strings.ToLower(parts[0]), route: 2})
} else if b1 && !b2 {
list.rules = append(list.rules, hostRule{right: strings.ToLower(parts[0]), route: 2})
} else {
// Assume no asterisk as left match
list.rules = append(list.rules, hostRule{left: strings.ToLower(parts[0]), route: 2})
}
case 2:
if b1 && !b2 {
list.rules = append(list.rules, hostRule{middle: strings.ToLower(parts[0]), right: strings.ToLower(parts[1]), route: 2})
} else if !b1 && b2 {
list.rules = append(list.rules, hostRule{left: strings.ToLower(parts[0]), middle: strings.ToLower(parts[1]), route: 2})
} else if !b1 && !b2 {
list.rules = append(list.rules, hostRule{left: strings.ToLower(parts[0]), right: strings.ToLower(parts[1]), route: 2})
}
case 3:
if !b1 && !b2 {
list.rules = append(list.rules, hostRule{left: strings.ToLower(parts[0]), middle: strings.ToLower(parts[1]), right: strings.ToLower(parts[2]), route: 2})
}
default:
log.Fatalf("Invalid HTTP rule: %s", s)
}
}
}
func mark4to6(mark net.IPMask) net.IPMask {
var prefix = []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
p := make(net.IPMask, net.IPv6len)
copy(p, prefix)
p[12] = mark[0]
p[13] = mark[1]
p[14] = mark[2]
p[15] = mark[3]
return p
}
func cmpIPIPNet6(ip net.IP, ipnet net.IPNet) int { // based on net.Contains()
for i := 0; i < net.IPv6len; i++ {
if a, b := ip[i]&ipnet.Mask[i], ipnet.IP[i]&ipnet.Mask[i]; a < b {
return -1
} else if a > b {
return 1
}
}
return 0
}
func (list *ipRuleList) findRouteForIP(ip net.IP) int { // based on sort.Search()
list.rw.RLock()
defer list.rw.RUnlock()
for i, j := 0, len(list.rules); i < j; {
switch k := int(uint(i+j) >> 1); cmpIPIPNet6(ip.To16(), list.rules[k].ipnet6) { // i <= k < j
case -1:
j = k
case 0:
return list.rules[k].route
case 1:
i = k + 1
}
}
if list.elseRoute != 0 {
return list.elseRoute
}
return 0
}
func (list *hostRuleList) findRouteForText(text string, ignoreCase bool) int {
list.rw.RLock()
defer list.rw.RUnlock()
if ignoreCase {
text = strings.ToLower(text)
}
for _, v := range list.rules {
if v.any {
return v.route
}
if v.exact != "" {
if v.exact == text {
return v.route
}
continue
}
if v.domain != "" {
if text == v.domain || strings.HasSuffix(text, "."+v.domain) {
return v.route
}
continue
}
h := text
if v.left != "" {
if strings.HasPrefix(h, v.left) {
h = strings.TrimPrefix(h, v.left)
} else {
continue
}
}
if v.right != "" {
if strings.HasSuffix(h, v.right) {
h = strings.TrimSuffix(h, v.right)
} else {
continue
}
}
if v.middle != "" {
if !strings.Contains(h, v.middle) {
continue
}
}
return v.route
}
return 0
}
func readIPRules(list *ipRuleList, file string) {
list.parseIPList(file)
list.rw.RLock()
if list.rules != nil {
logger.Printf("Loaded %d IP rules", len(list.rules))
sort.Sort(byByte(list.rules))
}
if list.elseRoute != 0 {
logger.Printf("Unmatched IPs will use route %d", list.elseRoute)
}
list.rw.RUnlock()
}
func readHostRules(list *hostRuleList, file string) {
list.parseHostList(file)
list.rw.RLock()
if list.rules != nil {
logger.Printf("Loaded %d host rules", len(list.rules))
}
list.rw.RUnlock()
}
func readHTTPRules(list *hostRuleList, str string) {
list.parseHTTPRules(str)
list.rw.RLock()
if list.rules != nil {
logger.Printf("Loaded %d HTTP rules", len(list.rules))
}
list.rw.RUnlock()
}