-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmart-pac.js
More file actions
280 lines (260 loc) · 9.05 KB
/
smart-pac.js
File metadata and controls
280 lines (260 loc) · 9.05 KB
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
#!/usr/bin/env node
'use strict'
const commander = require("commander");
const Agent = require("proxy-agent");
const fs = require("fs");
const Multiprogress = require('multi-progress');
const url = require('url');
const https = require('https');
const readline = require('readline');
const base64 = require('base64-stream');
const gfwlisturl = "https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt";
const apnicurl = "https://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest";
let smart = {
"proxy": require('./proxy'),
"regex": {
"white": {
"domain": null,
"url": null
},
"black": {
"pureip": null,
"domain": null,
"url": null
}
},
"chsips": []
};
let gfwlist = {
"white": {
"initial": {
"http": [],
"https": []
},
"domain": []
},
"black": {
"initial": {
"http": [],
"https": []
},
"pureip": [],
"domain": [],
"anywhere": {
"domain": [],
"plain": [],
"regex": []
}
}
};
const reComment = /^(\[.*\]|[ \f\n\r\t\v]*|\!.*)$/;
const reInitial = /^\|https?:\/\/[0-9a-zA-Z-_.*?&=%~/:]+$/;
const rePureip = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
const reDomain = /^\|\|[0-9a-zA-Z-.*]+\/?$/; // 明确的domain中有可能包含*通配符
const reRegex = /^\/.*\/$/;
const reDomain2 = /^[0-9a-zA-Z-.]+$/; // 经常有domain混在url模式中,此时*通配符通常与url相关
const reAnywhere = /^[0-9a-zA-Z-_.*?&=%~/:]+$/;
const reGroup = /\|/g;
commander
.version('2.0.0')
.description(`An automatic program to make Proxy-AutoConfig file.
You need modify proxy.js and user.rule for custumization.
Sample: node smart-pac.js > smart.pac` )
.option('-s, --silent', 'without prompt')
.option('-d, --direct', 'download gfwlist and apnic directly')
.parse(process.argv);
function renderTemplate() {
let { proxy, regex, chsips } = smart;
readline.createInterface({
input: fs.createReadStream('smart.template')
}).on('line', (line) => console.log(eval("`" + line + "`")));
}
const mp = new Multiprogress(process.stderr);
let done = 0;
// 不明原因,globalAgent在https中未生效
// if( !commander.direct )
// https.globalAgent = new Agent( smart.proxy.black
// .split(';')[0].split(' ').join('://').toLowerCase()
// .replace('socks5://','socks5h://'));
let apnicTarget = url.parse(apnicurl);
if (!commander.direct)
apnicTarget.agent = new Agent("socks5h://192.168.119.2:1080");
https.get(apnicTarget)
.on('response', (res) => {
if (!commander.silent) {
const bar = mp.newBar(' Downloading apnic records [:bar] :rate/bps :percent :etas', {
complete: '=',
incomplete: '-',
width: 50,
total: parseInt(res.headers['content-length'], 10)
});
res.on('data', (chunk) => bar.tick(chunk.length));
res.on('end', () => {
done++;
if (done == 3)
renderTemplate();
});
}
const reVer = /^\d.*$/;
const reCN = /^apnic\|CN\|ipv4\|((\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3}))\|(\d+)\|\d{8}\|.*$/
const rl = readline.createInterface({
input: res,
output: null
}).on('line', (line) => {
// 跳过注释、空行与开头的版本说明
if (line.startsWith('#') || line.length == 0)
return;
if (reVer.exec(line))
if (!line.startsWith('2|'))
throw "found a unsupported version.";
// 区分黑名单与白名单
const result = reCN.exec(line)
if (result) {
const sect = [(parseInt(result[2], 10) << 16) * 256 + (parseInt(result[3], 10) << 16) + (parseInt(result[4], 10) << 8) + parseInt(result[5], 10),
parseInt(result[6], 10)];
if (smart.chsips.length == 0) {
smart.chsips.push(sect);
}
else {
const last = smart.chsips[smart.chsips.length - 1];
if (last[0] + last[1] == sect[0])
last[1] += sect[1];
else
smart.chsips.push(sect);
}
}
});
}).on('error', (err) => {
console.error('\nGet apnic records failed!\n');
console.error(err);
process.exit(-1);
});
const readRule = (line) => {
// 跳过注释、空行与开头的版本说明
if (reComment.test(line))
return;
// 区分黑名单与白名单
let list;
if (line.startsWith('@@')) {
line = line.substring(2);
list = gfwlist.white;
}
else
list = gfwlist.black;
if (reInitial.test(line)) {
list = list.initial;
if (line[5] == 's') {
line = line.substring(9);
list = list.https;
}
else {
line = line.substring(8);
list = list.http;
}
list.push(line);
}
else if (rePureip.test(line))
list.pureip.push(line);
else if (reDomain.test(line))
list.domain.push(line.substring(2));
else if (reRegex.test(line)) {
// if (line.match(reGroup) != null && line.match(reGroup).length > 20)
// console.warn('\n"%s" has been skiped for performance reason.\n', line);
if (line.match(reGroup) != null && line.match(reGroup).length < 20)
list.anywhere.regex.push(line.substring(1, line.length - 1));
}
else if (reDomain2.test(line))
list.anywhere.domain.push(line);
else if (reAnywhere.test(line))
list.anywhere.plain.push(line);
else
console.error('\nCan\'t understand %s.\n', line);
}
let gfwTarget = url.parse(gfwlisturl);
if (!commander.direct)
gfwTarget.agent = new Agent("socks5h://192.168.119.2:1080");
https.get(gfwTarget, (res) => {
if (res.statusCode === 200) {
const len = parseInt(res.headers['content-length'], 10);
const bar = mp.newBar(' Downloading GFW List [:bar] :rate/bps :percent :etas', {
complete: '=',
incomplete: '-',
width: 50,
total: len
});
res.on('data', (chunk) => bar.tick(chunk.length));
res.on('end', () => { // after line event
// plain to regex 需要处理: . $ ^ { [ ( | ) * + ? |
// RFC3986 保留字符: ! * ' ( ) ; : @ & = + $ , / ? # [ ]
// RFC3986 除字母与数字之外的非保留字符: - _ . ~
// 需要处理转义的字符: . $ [ ( | ) * + ?
// 实践中遇到的 * 基本都是在当通配符用,所以……
const plain2regex = (url) => {
return url
.replace(/\./g, '\\.')
.replace(/\$/g, '\\$')
.replace(/\[/g, '\\[')
.replace(/\(/g, '\\(')
.replace(/\|/g, '\\|')
.replace(/\)/g, '\\)')
.replace(/\]/g, '\\]')
.replace(/\+/g, '\\+')
.replace(/\?/g, '\\?')
.replace(/\*/g, '\\.*');
};
let regArray = []; // ready for smart.regex.black.url
if (gfwlist.black.initial.http.length > 0 || gfwlist.black.initial.https.length > 0) {
if (gfwlist.black.initial.http.length)
regArray.push(`://(${gfwlist.black.initial.http.map(plain2regex).join('|')})`);
if (gfwlist.black.initial.https.length)
regArray.push(`s://(${gfwlist.black.initial.https.map(plain2regex).join('|')})`);
regArray = [`http(${regArray.join('|')})`];
}
if (gfwlist.black.anywhere.plain.length)
regArray.push(gfwlist.black.anywhere.plain.map(plain2regex).join('|'));
if (gfwlist.black.anywhere.regex.length)
regArray.push(gfwlist.black.anywhere.regex.join('|'));
if (regArray.length)
smart.regex.black.url = regArray.join('|');
regArray = []; // ready for smart.regex.black.domain
if (gfwlist.black.domain.length)
regArray.push(gfwlist.black.domain.map(domain => domain.replace(/\./g, '\\.').replace(/\*/g, '.*')).join('|'));
if (gfwlist.black.anywhere.domain.length)
regArray.push(gfwlist.black.anywhere.domain.map( domain2 =>
( domain2[0] == '.' ? domain2.substring(1) : domain2 ).replace(/\./g, '\\.').replace(/\*/g, '.*')).join('|'));
if (regArray.length)
smart.regex.black.domain = `^(.+\\.)?(${regArray.join('|')})$`;
if (gfwlist.black.pureip.length)
smart.regex.black.pureip = `^(${gfwlist.black.pureip.map(txt => txt.replace(/\./g, '\\.')).join('|')})$`;
regArray = []; // ready for smart.regex.white.url
if (gfwlist.white.initial.http.length)
regArray.push(`://(${gfwlist.white.initial.http.map(plain2regex).join('|')})`);
if (gfwlist.white.initial.https.length)
regArray.push(`s://(${gfwlist.white.initial.https.map(plain2regex).join('|')})`);
if (regArray.length)
smart.regex.white.url = `^http(${regArray.join('|')})`;
if (gfwlist.white.domain.length)
smart.regex.white.domain = `^(.+\\.)?(${gfwlist.white.domain.map(domain => domain.replace(/\./g, '\\.').replace(/\*/g, '.*')).join('|')})$`;
done++;
if (done == 3)
renderTemplate();
});
readline.createInterface({
input: res.pipe(base64.decode()),
output: null
}).on('line', readRule);
}
})
.on('error', (err) => {
console.error('\nGet GFW List failed!\n');
console.error(err);
process.exit(-2);
});
readline.createInterface({
input: fs.createReadStream('user.rule')
.on('end', () => {
done++;
if (done == 3)
renderTemplate();
})
}).on('line', readRule);