-
Notifications
You must be signed in to change notification settings - Fork 8
/
template.js
109 lines (84 loc) · 2.38 KB
/
template.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
'use strict';
// Version: 0.3
if (/*@cc_on!@*/!1) { // Is IE?
throw new TypeError('https://rebrand.ly/ac-anticensority');
}
const TOR_PROXIES = 'SOCKS5 localhost:9150; SOCKS5 localhost:9050; DIRECT';
const PROXY_STRING = TOR_PROXIES;
const inputs = {{.INPUTS}};
const maskedAddrMaskAddrPairs = inputs.MASKED_SUBNETS;
const ips = inputs.IPS;
const hostnames = inputs.HOSTNAMES;
function ifFoundByBinaryInString(sortedArrayJoined, target) {
const targetLen = target.length;
let istart = 0;
let iend = (sortedArrayJoined.length / targetLen) - 1;
let imid, offset, newWord;
while (istart < iend) {
imid = (istart + iend) >>> 1;
offset = imid * targetLen;
newWord = sortedArrayJoined.substring( offset, offset + targetLen );
if (target > newWord) {
istart = imid + 1;
} else {
iend = imid;
}
}
offset = iend * targetLen;
return sortedArrayJoined.substring( offset, offset + targetLen ) === target;
}
function areSubsCensored(hostname) {
let x = hostname.lastIndexOf('.');
do {
x = hostname.lastIndexOf('.', x - 1);
const sub = hostname.substring(x + 1);
if(ifFoundByBinaryInString(hostnames[sub.length] || '', sub)) {
return true;
}
} while(x > -1);
return false;
}
function isCensoredByMaskedIp(ip) {
const ipAddr = convert_addr(ip);
for (const pair of maskedAddrMaskAddrPairs) {
const maskedAddr = pair[0];
const maskAddr = pair[1];
if((ipAddr & maskAddr) === maskedAddr) {
return true;
}
}
return false;
}
function FindProxyForURL(url, hostname) {
let ifByHost = false;
let ifByMaskedIp = false;
// Remove last dot.
if (hostname[hostname.length - 1] === '.') {
hostname = hostname.replace(/\.+$/g, '');
}
if (hostname[0] === '.') {
// Yes, it's possible, e.g. `fetch(https://...google.com)`.
// `fetch(https://.)` should fail though.
hostname = hostname.replace(/^\.+/g, '');
}
if (dnsDomainIs(hostname, '.onion')) {
return TOR_PROXIES;
}
return (function isCensored(){
ifByHost = areSubsCensored(hostname);
if (ifByHost) {
return true;
}
const ip = dnsResolve(hostname);
if (ip) {
if (ifFoundByBinaryInString(ips[ip.length] || '', ip)) {
return true;
}
ifByMaskedIp = isCensoredByMaskedIp(ip);
if (ifByMaskedIp) {
return true;
};
}
return false;
})() ? PROXY_STRING : 'DIRECT';
}