-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.js
374 lines (325 loc) · 10.1 KB
/
proxy.js
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// -*- mode: javascript; js-indent-level: 2 -*-
// vim: set filetype=javascript tabstop=2 shiftwidth=2 expandtab:
const direct = "direct";
const blocked = "blocked";
const proxy = "proxy";
const DIRECT = "DIRECT";
var proxyBehaviors = {
proxy: "SOCKS5 127.0.0.1:1080", // the default proxy
direct: DIRECT,
blocked: "PROXY 0.0.0.0:0",
// "companyProxy": "PROXY 192.168.1.1:8080", // domains list in `domain-rules-companyProxy.txt` will use this proxy setting
};
const default_behavior = DIRECT + "; " + proxyBehaviors[proxy];
const ipv4Pattern = /^\d{1,3}(\.\d{1,3}){3}$/;
const ipv6Pattern = /^[a-fA-F0-9:]+$/;
function isIpAddress(host) {
return ipv4Pattern.test(host) || ipv6Pattern.test(host);
}
function ipToNumber(ip) {
const parts = ip.split('.');
return parts.reduce((acc, part) => (acc << 8) + parseInt(part, 10), 0);
}
function numberToIp(number) {
return [
(number >>> 24) & 0xFF,
(number >>> 16) & 0xFF,
(number >>> 8) & 0xFF,
number & 0xFF
].join('.');
}
function ipv6ToTwoNumbers(ip) {
const parts = ip.split(':').map(part => part ? parseInt(part, 16) : 0);
let high = 0n, low = 0n;
for (let i = 0; i < 4; i++) {
high = (high << 16n) + BigInt(parts[i] || 0);
}
for (let i = 4; i < 8; i++) {
low = (low << 16n) + BigInt(parts[i] || 0);
}
return [high, low];
}
function twoNumbersToIpv6(high, low) {
const parts = [];
for (let i = 0; i < 4; i++) {
parts.push(((high >> BigInt(48 - 16 * i)) & 0xFFFFn).toString(16));
}
for (let i = 0; i < 4; i++) {
parts.push(((low >> BigInt(48 - 16 * i)) & 0xFFFFn).toString(16));
}
return parts.join(':').replace(/(:0{1,4}){2,}/, '::');
}
const ipv4NetworkRules = [
[0x7F000000, 8 , direct], // 127.0.0.0/8 (Loopback)
[0xA9FE0000, 16, direct], // 169.254.0.0/16 (Link Local)
[0x64400000, 10, direct], // 100.64.0.0/10 (Carrier-grade NAT)
// begin of ipv4 networks
// end of ipv4 networks
];
const ipv6NetworkRules = [
[0x0000000000000000n, 0x0000000000000000n, 128, direct], // ::/128 (Unspecified Address)
[0x0000000000000000n, 0x0000000000000001n, 128, direct], // ::1/128 (Loopback Address)
[0xfc00000000000000n, 0x0000000000000000n, 7 , direct], // fc00::/7 (Unique Local Address)
[0xff00000000000000n, 0x0000000000000000n, 8 , direct], // ff00::/8 (Multicast Address)
[0x2001000000000000n, 0x0000000000000000n, 16 , direct], // 2001::/16 (Teredo Address)
[0xfe80000000000000n, 0x0000000000000000n, 10 , direct], // fe80::/10 (Link-Local Address)
// begin of ipv6 networks
// end of ipv6 networks
];
const proxyRules = {
"local": direct,
"114.114.114.114": direct,
"whitehouse.com": blocked,
"google": proxy,
// begin of proxy rules
// end of proxy rules
};
const domainRegexpRules = [
[ /^adservice\.google\.([a-z]{2}|com?)(\.[a-z]{2})?$/, blocked], // adservice.google.com.xx
// begin of regexp rules
// end of regexp rules
]
class IPv4TrieNode {
constructor() {
this.children = [null, null]; // 0 and 1
this.isEnd = false;
this.network = null;
this.prefix = null;
this.action = direct;
}
}
class IPv4PrefixTrie {
constructor() {
this.root = new IPv4TrieNode();
}
static buildTrieFromData(data) {
const trie = new IPv4PrefixTrie();
for (const [network, prefix, action] of data) {
let node = trie.root;
node = IPv4PrefixTrie._insertBits(node, network, prefix);
node.isEnd = true;
node.network = network;
node.prefix = prefix;
node.action = action
}
return trie;
}
static _insertBits(node, value, bits) {
let mask = 0x80000000;
for (let i = 0; i < bits; i++) {
const bitIndex = ((value & mask) !== 0 ? 1 : 0);
mask = mask >>> 1;
if (!node.children[bitIndex]) {
node.children[bitIndex] = new IPv4TrieNode();
}
node = node.children[bitIndex];
}
return node;
}
search(ip) {
let node = this.root;
let lastMatch = null;
node = IPv4PrefixTrie._searchBits(node, ip, 32, (matchedNode) => {
if (matchedNode.isEnd) {
lastMatch = matchedNode;
}
});
return lastMatch;
}
static _searchBits(node, value, bits, callback) {
let mask = 0x80000000;
for (let i = 0; i < bits; i++) {
const bitIndex = ((value & mask) !== 0 ? 1 : 0);
mask = mask >>> 1;
if (!node.children[bitIndex]) {
return null;
}
node = node.children[bitIndex];
callback(node);
}
return node;
}
}
class IPv6TrieNode {
constructor() {
this.children = [null, null]; // 0 and 1
this.isEnd = false;
this.networkHigh = null;
this.networkLow = null;
this.prefix = null;
this.action = direct;
}
}
class IPv6PrefixTrie {
constructor() {
this.root = new IPv6TrieNode();
}
static buildTrieFromData(data) {
const trie = new IPv6PrefixTrie();
for (const [networkHigh, networkLow, prefix, action] of data) {
let node = trie.root;
node = IPv6PrefixTrie._insertBits(node, networkHigh, Math.min(prefix, 64));
if (prefix > 64) {
node = IPv6PrefixTrie._insertBits(node, networkLow, prefix - 64);
}
node.isEnd = true;
node.networkHigh = networkHigh;
node.networkLow = networkLow;
node.prefix = prefix;
node.action = action;
}
return trie;
}
static _insertBits(node, value, bits) {
let mask = 0x8000000000000000n;
for (let i = 0; i < bits; i++) {
const bitIndex = ((value & mask) !== 0n ? 1 : 0);
mask = mask >> 1n;
if (!node.children[bitIndex]) {
node.children[bitIndex] = new IPv6TrieNode();
}
node = node.children[bitIndex];
}
return node;
}
search(ipHigh, ipLow) {
let node = this.root;
let lastMatch = null;
node = IPv6PrefixTrie._searchBits(node, ipHigh, 64, (matchedNode) => {
if (matchedNode.isEnd) {
lastMatch = matchedNode;
}
});
if (node) {
node = IPv6PrefixTrie._searchBits(node, ipLow, 64, (matchedNode) => {
if (matchedNode.isEnd) {
lastMatch = matchedNode;
}
});
}
return lastMatch;
}
static _searchBits(node, value, bits, callback) {
let mask = 0x8000000000000000n;
for (let i = 0; i < bits; i++) {
const bitIndex = ((value & mask) !== 0n ? 1 : 0);
mask = mask >> 1n;
if (!node.children[bitIndex]) {
return null;
}
node = node.children[bitIndex];
callback(node);
}
return node;
}
}
const ipv4Trie = IPv4PrefixTrie.buildTrieFromData(ipv4NetworkRules);
const ipv6Trie = IPv6PrefixTrie.buildTrieFromData(ipv6NetworkRules);
function findMatchingNetwork(ip, networks4, networks6) {
if (ip.includes('.')) { // IPv4
const ipNumber = ipToNumber(ip);
return ipv4Trie.search(ipNumber);
} else { // IPv6
const [ipHigh, ipLow] = ipv6ToTwoNumbers(ip);
return ipv6Trie.search(ipHigh, ipLow);
}
return null;
}
function printMatchingNetwork(ip, networks4, networks6) {
const matchedNetwork = findMatchingNetwork(ip, networks4, networks6);
if (matchedNetwork) {
if (ip.includes('.')) { // IPv4
const trie = matchedNetwork;
return `${numberToIp(trie.network)}/${trie.prefix}`;
} else { // IPv6
const trie = matchedNetwork;
return `${twoNumbersToIpv6(trie.networkHigh, trie.networkLow)}/${trie.prefix}`;
}
} else {
return null;
}
}
function FindProxyForURL(_url, _host) {
const host = _host;
if (isIpAddress(host)) {
const match = findMatchingNetwork(host);
if(match) {
return proxyBehaviors[match.action] || default_behavior;
} else {
var action = proxyRules[host];
if (action !== undefined) {
return proxyBehaviors[action] || default_behavior;
}
return default_behavior;
}
}
const match = domainRegexpRules.find(([regexp, value]) => regexp.test(host) );
if(match)
return proxyBehaviors[match[1]] || default_behavior;
var host_segment = host;
while (true) {
var action = proxyRules[host_segment];
if (action !== undefined) {
return proxyBehaviors[action] || default_behavior;
}
var nextDot = host_segment.indexOf(".");
if (nextDot === -1) {
break;
}
host_segment = host_segment.substring(nextDot + 1);
}
var remote_ip = undefined;
if(typeof dnsResolveEx == 'function') {
remote_ip = dnsResolveEx(host);
} else if(typeof dnsResolve == 'function') {
remote_ip = dnsResolve(host);
}
if(remote_ip !== undefined) {
const match = findMatchingNetwork(remote_ip);
if (match) return proxyBehaviors[match.action] || default_behavior;
}
return default_behavior;
}
if (typeof process !== 'undefined' && process.argv.includes('test')) {
function assertNetwork(ip, expected) {
const result = printMatchingNetwork(ip, ipv4NetworkRules, ipv6NetworkRules);
if (result === expected) {
console.log(`OK: Test for ${ip} passed.`);
} else {
console.log(`Failed: Test for ${ip} failed. Expected: ${expected}, but got: ${result}`);
}
}
function assertProxyBehavior(host, expected) {
const result = FindProxyForURL('', host);
if (result === expected) {
console.log(`OK: Test for ${host} => ${expected} passed.`);
} else {
console.log(`Failed: Test for ${host} failed. Expected: ${expected}, but got: ${result}`);
}
}
function assertVisitHostWithProxy(host) {
assertProxyBehavior(host, proxyBehaviors[proxy]);
}
function assertHostWithDefaultAction(host) {
assertProxyBehavior(host, default_behavior);
}
function assertDirectHost(host) {
assertProxyBehavior(host, proxyBehaviors[direct]);
}
function assertBlockedHost(host) {
assertProxyBehavior(host, proxyBehaviors[blocked]);
}
function runTests() {
assertNetwork("127.234.168.10", "127.0.0.0/8");
assertNetwork("1.1.1.1", null);
assertNetwork("fe80::f0:c6b3:c766:9b1e", "fe80::/10");
assertVisitHostWithProxy("com.google");
assertVisitHostWithProxy("domains.google");
assertHostWithDefaultAction("www.not-google");
assertDirectHost("127.3.4.5");
assertDirectHost("114.114.114.114");
assertBlockedHost("www.whitehouse.com");
assertBlockedHost("adservice.google.com.xx")
}
runTests();
}