-
Notifications
You must be signed in to change notification settings - Fork 270
/
pac-template
184 lines (158 loc) · 5.37 KB
/
pac-template
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
var proxy = __PROXY__;
var direct = 'DIRECT';
var directDomains = __DIRECT_DOMAINS__;
var domainsUsingProxy = __DOMAINS__;
var localTlds = __LOCAL_TLDS__;
var cidrs = __CIDRS__;
var hasOwnProperty = Object.hasOwnProperty;
function isIpAddress(ip) {
return /^\d{1,3}(\.\d{1,3}){3}$/.test(ip) || /^([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}$/.test(ip);
}
function RadixTree() {
this.root = {};
}
RadixTree.prototype.insert = function(string) {
var node = this.root;
for (var i = 0; i < string.length; i++) {
var char = string[i];
if (!node[char]) {
node[char] = {};
}
node = node[char];
}
};
RadixTree.prototype.to_list = function() {
return this.root;
};
function ipToBinary(ip) {
// Check if it's IPv4
if (/^\d{1,3}(\.\d{1,3}){3}$/.test(ip)) {
return ip.split('.').map(function(num) {
return ("00000000" + parseInt(num, 10).toString(2)).slice(-8);
}).join('');
} else if (/^([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}$/.test(ip)) {
// Expand the IPv6 address if it contains '::'
var parts = ip.split('::');
var left = parts[0] ? parts[0].split(':') : [];
var right = parts[1] ? parts[1].split(':') : [];
// Calculate the number of zero groups to insert
var zeroGroups = 8 - (left.length + right.length);
// Create the full address by inserting zero groups
var fullAddress = left.concat(Array(zeroGroups + 1).join('0').split('')).concat(right);
// Convert each group to binary and pad to 16 bits
return fullAddress.map(function(group) {
return ("0000000000000000" + parseInt(group || '0', 16).toString(2)).slice(-16);
}).join('');
}
}
function searchRadixTree(bits) {
var currentNode = radixTree;
var isLastNode = false;
for (var i=0; i<bits.length; i++) {
var char = bits[i];
if (currentNode[char]) {
currentNode = currentNode[char];
isLastNode = Object.keys(currentNode).length === 0;
} else {
break;
}
}
return isLastNode;
}
function isInDirectDomain(host) {
for (var i = 0; i < directDomains.length; i++) {
var domain = directDomains[i];
if (host === domain || host.endsWith('.' + domain)) {
return true;
}
}
return false;
}
function isInProxyDomain(host) {
for (var i = 0; i < domainsUsingProxy.length; i++) {
var domain = domainsUsingProxy[i];
if (host === domain || host.endsWith('.' + domain)) {
return true;
}
}
return false;
}
function isLocalTestDomain(domain) {
// Chrome uses .test as testing gTLD.
var tld = domain.substring(domain.lastIndexOf('.'));
if (tld === domain) {
return false;
}
return localTlds.some(function(localTld) {
return tld === localTld;
});
}
/* https://github.com/frenchbread/private-ip */
function isPrivateIp(ip) {
return /^(::f{4}:)?10\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(ip) ||
/^(::f{4}:)?192\.168\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(ip) ||
/^(::f{4}:)?172\.(1[6-9]|2\d|30|31)\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(ip) ||
/^(::f{4}:)?127\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(ip) ||
/^(::f{4}:)?169\.254\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(ip) ||
/^f[cd][0-9a-f]{2}:/i.test(ip) ||
/^fe80:/i.test(ip) ||
/^::1$/.test(ip) ||
/^::$/.test(ip);
}
function FindProxyForURL(url, host) {
if (isInDirectDomain(host)) {
debug('命中直连域名', host, 'N/A');
return direct;
} else if (isInProxyDomain(host)) {
debug('命中代理域名', host, 'N/A');
return proxy;
} else if (isPlainHostName(host) || host === 'localhost' || isLocalTestDomain(host)) {
debug('命中本地主机名或本地tld', host, 'N/A');
return direct;
} else if (isPrivateIp(host)) {
debug('命中私有 IP 地址', host, 'N/A');
return direct;
}
ip = isIpAddress(host) ? host : dnsResolve(host);
if (!ip) {
debug('无法解析 IP 地址', host, 'N/A');
return proxy;
} else if (isPrivateIp(ip)) {
debug('域名解析后命中私有 IP 地址', host, ip);
return direct;
} else if (searchRadixTree(ipToBinary(ip))) {
debug('匹配到直连IP', host, ip);
return direct;
}
debug('未命中任何规则', host, ip);
return proxy;
}
var allowAlert = true
function debug(msg, host, ip) {
if (!allowAlert) {
return
}
try {
alert('[' + host + ' -> ' + ip + '] ' + msg);
} catch (e) {
allowAlert = false
}
}
var radixTree = new RadixTree();
(function () {
var startTime = new Date().getMilliseconds();
debug('开始生成 Radix Tree', 'PAC文件载入开始', startTime.toString());
for (let i=0; i<cidrs.length; i++) {
var cidr = cidrs[i];
var [ip, prefixLen] = cidr.split('/');
if (!cidr.includes(':')) {
var ip = ip.match(/.{1,2}/g).map(function(byte) {
return parseInt(byte, 16);
}).join('.');
}
var bits = ipToBinary(ip).slice(0, prefixLen);
radixTree.insert(bits);
}
radixTree = radixTree.to_list();
debug('Radix Tree 已生成', 'PAC文件载入完毕', cidrs.length.toString()+'个CIDR条目');
})();