This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
338 lines (319 loc) · 10.8 KB
/
utils.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
const fs = require('fs');
const dns = require("dns2");
const udp = require('dgram');
const http = require("http");
const https = require("https");
const agent = new https.Agent({ keepAlive: true });
const readline = require('readline');
const base64url = require("base64url");
const { equal } = require('assert');
/**
* 记录类型到 DNS 返回包字段的映射, 用于 DNSRequest
*/
const TYPE_FIELD_MAP = {
0x01: 'address', // A
0x02: 'ns', // NS
0x05: 'domain', // CNAME
0x06: 'domain', // SOA
0x10: 'data', // TXT
0x0C: 'domain', // PTR
0x0F: (r, row) => {
row.exchange = r.exchange;
row.priority = r.priority;
}, // MX
0x1C: 'address', // AAAA
};
/**
* 对 DoH URL 进行预处理
*
* @param {*} config 服务器配置对象
*/
function loadServer(config) {
if (config.Type == "dns") {
config.DNS = {
IP: config.Addr.split(':')[0],
Port: config.Addr.split(':')[1]
}
} else {
config.URL += config.URL.lastIndexOf('?') === -1 ? '?' : '&';
}
}
/**
* 创建 DNS 查询请求
* @param {*} server 服务器配置对象
* @param {int} timeout 查询超时
* @param {*} request DNS 请求包
*/
function createQuery(server, timeout, request) {
if (server.Type == 'dns') {
return DNSRequest(server.DNS, timeout, request);
} else {
return DoHRequest(server.URL, timeout, request);
}
}
/**
* 将 DNS Packet 标记为返回数据包, 以便告知客户端 NO DATA
* @param {*} packet 需要标记的数据包
*/
function emptyPacket(packet) {
packet.header.qr = 1;
return packet;
}
/**
* 读取一个纯文本文件并按行处理首字符不为 # 的行
* @param {string} file 文件路径
* @param {(line: string, lineNumber: int) => void} line 每行执行的回调
* @param {() => void} finish 读取完成的回调
* @param {(e) => void} error 出现错误的回调
*/
function readFile(file, line, finish, error) {
try {
let lineNumber = 0;
readline.createInterface({
input: fs.createReadStream(file)
.on('error', error)
}).on('line', l => {
lineNumber++;
if (l.length > 0 && l[0] != '#') {
line(l, lineNumber);
}
}).on('close', finish);
} catch (e) {
error(e);
}
}
/**
* 读取 IP CIDR 文件, 验证并创建结果数组. 该函数不会调用 reject, 失败时 resolve 空数组
* @param {string} file 文件路径
*/
const LoadIP = (file) => new Promise((resolve, _reject) => {
const result = [];
readFile(file, (line, counter) => {
if (/^([0-9]{1,3}\.){3}[0-9]{1,3}(\/([0-9]|[1-2][0-9]|3[0-2]))?$/.test(line)) {
result.push(line);
} else {
console.log(`File ${file}:${counter} does not contains a valid IPv4 address with proper optional CIDR notation`);
}
}, () => resolve(result), (e) => {
resolve([]);
console.log(`Error occured while reading ${file}: ${e.message}`);
});
});
/**
* 进行 DOH 查询
* @param {string} url 服务器URL, 此函数将直接在 URL 后添加 dns=Base64
* @param {int} timeout HTTP 请求超时
* @param {*} packet 查询包
*/
const DoHRequest = (url, timeout, packet) => new Promise((resolve, reject) => {
const req = https.request(url + 'dns=' + base64url(packet.toBuffer()), {
agent: agent,
method: "GET",
timeout: timeout,
headers: {
"Accept": "application/dns-message",
"User-Agent": "SmartChinaDNS",
"Connection": "Keep-Alive"
}
}).on('timeout', () => {
req.abort();
reject(`HTTP time out requesting ${url}`);
}).on('response', res => {
res.on('data', data => {
switch (res.statusCode) {
case 200:
let answerPacket = new dns.Packet(packet);
answerPacket.header.qr = 1;
answerPacket.additionals = [];
answerPacket.header.arcount = 0;
answerPacket.answers = dns.Packet.parse(data).answers;
resolve(packet);
break;
default:
reject('Server returns error ' + res.statusCode);
break;
}
});
}).on('error', err => {
reject(err.message);
});
req.end();
});
/**
* 进行 DNS 查询
* @param {*} ServerInfo DNS 服务器参数
* @param {int} timeout DNS 等待超时
* @param {*} packet 查询包
*/
const DNSRequest = (ServerInfo, timeout, packet) => new Promise((resolve, reject) => {
const client = new udp.Socket('udp4');
client.once('message', function onMessage(message) {
client.close();
clearTimeout(timeoutWatcher);
const response = dns.Packet.parse(message);
equal(response.header.id, packet.header.id);
resolve(response);
});
client.send(packet.toBuffer(), ServerInfo.Port, ServerInfo.IP, err => {
if (err) {
clearTimeout(timeoutWatcher);
reject(err);
}
});
let timeoutWatcher = setTimeout(() => {
timeoutWatcher = false;
reject(`DNS Timeout`);
}, timeout);
});
/**
* 执行 HTTP(s) RST 检测, 收到 RST 则返回 true
* @param {string} host 检测时使用的 Hostname
* @param {string} ip 要检测的 IP 地址
*/
const RSTCheck = (host, ip) => new Promise((resolve, _reject) => {
const doCheck = (protocol, port) => {
protocol.request({
port: port,
method: "HEAD", // We don't care the response, it's fine when server returns http error.
hostname: ip,
headers: {
"Host": host,
"User-Agent": "SmartChinaDNS"
},
setHost: false,
timeout: 10000
}).on('error', (err) => {
resolve(err.code == "ECONNRESET");
}).on('response', () => {
resolve(false);
}).end();
};
doCheck(http, 80);
doCheck(https, 443);
});
/**
* 通过 value 在 object 中搜索键
* @param {*} object 要搜索的对象
* @param {*} value 要搜索的值
*/
const getKeyByValue = (object, value) => Object.keys(object).find(key => object[key] === value);
class DomainMatcher {
constructor() {
this.DomainList = {};
}
LoadFromFile(file) {
const onLine = (line, _counter) => {
line = line.toLowerCase().split('.');
let root = this.DomainList;
for (let i = line.length - 1; i >= 0; i--) {
if (root[line[i]] === undefined) {
root[line[i]] = i == 0 ? true : {};
}
root = root[line[i]];
if (root === true && i != 0) {
// Already contained in a wider rule
return;
}
}
};
const onError = (e) => {
resolve();
console.log(`Error occured while reading ${file}: ${e.message}`);
};
return new Promise((resolve, _reject) => readFile(file, onLine, resolve, onError));
}
contains(domain) {
let root = this.DomainList;
domain = domain.toLowerCase().split('.');
for (let i = domain.length - 1; root && i >= 0; i--) {
root = root[domain[i]];
if (root === true) {
return true;
}
}
return false;
}
}
/**
* @todo 重构
*/
function GenerateLog(FinalAnswer, rinfo, comment = "") {
for (const q of FinalAnswer.answers) {
let ansData;
switch (q.type) {
case 0x01:
case 0x1C:
ansData = q.address;
break;
case 0x02:
ansData = q.ns;
break;
case 0x05:
ansData = q.domain;
break;
case 0x06:
ansData = `primary ${q.primary} admin ${q.admin}`;
break;
case 0x0C:
ansData = q.domain;
break;
case 0x0F:
ansData = q.exchange;
break;
case 0x10:
ansData = q.data;
break;
default:
ansData = "error";
}
console.log(`Answered[${getKeyByValue(dns.Packet.TYPE, q.type)}] ${q.name} to ${rinfo.address}: ${ansData} ${comment} +${new Date() - rinfo.timeStart}ms`);
}
if (FinalAnswer.answers.length == 0) {
const q = FinalAnswer.questions[0];
console.log(`Answered[${getKeyByValue(dns.Packet.TYPE, q.type)}] ${q.name} to ${rinfo.address}: NODATA ${comment} +${new Date() - rinfo.timeStart}ms`);
}
}
// 还原缩写的IPv6地址
// by Christopher Miller
// http://forrst.com/posts/JS_Expand_Abbreviated_IPv6_Addresses-1OR
function expandIPv6Address(address) {
var fullAddress = "";
var expandedAddress = "";
var validGroupCount = 8;
var validGroupSize = 4;
var ipv4 = "";
var extractIpv4 = /([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/;
var validateIpv4 = /((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})/;
// look for embedded ipv4
if (validateIpv4.test(address)) {
groups = address.match(extractIpv4);
for (var i = 1; i < groups.length; i++) {
ipv4 += ("00" + (parseInt(groups[i], 10).toString(16))).slice(-2) + (i == 2 ? ":" : "");
}
address = address.replace(extractIpv4, ipv4);
}
if (address.indexOf("::") == -1) // All eight groups are present.
fullAddress = address;
else // Consecutive groups of zeroes have been collapsed with "::".
{
var sides = address.split("::");
var groupsPresent = 0;
for (var i = 0; i < sides.length; i++) {
groupsPresent += sides[i].split(":").length;
}
fullAddress += sides[0] + ":";
for (var i = 0; i < validGroupCount - groupsPresent; i++) {
fullAddress += "0000:";
}
fullAddress += sides[1];
}
var groups = fullAddress.split(":");
for (var i = 0; i < validGroupCount; i++) {
while (groups[i].length < validGroupSize) {
groups[i] = "0" + groups[i];
}
expandedAddress += (i != validGroupCount - 1) ? groups[i] + ":" : groups[i];
}
return expandedAddress;
}
module.exports = { DoHRequest, getKeyByValue, DNSRequest, LoadIP, DomainMatcher, GenerateLog, emptyPacket, RSTCheck, loadServer, createQuery };