forked from zbinlin/wireguard-configuration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cn.js
executable file
·87 lines (76 loc) · 2.87 KB
/
cn.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
#!/bin/env node
const https = require('https');
const { promisify } = require('util');
const readline = require('readline');
const DOWNLOAD_URI = 'https://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest';
const PRIORITY = 1024;
async function fetch(uri) {
return new Promise((resolve, reject) => {
https.get(uri, resolve);
});
}
async function parse(res) {
const rl = new readline.createInterface({
input: res,
});
return new Promise((resolve, reject) => {
const result = [];
rl.on('line', line => {
if (/^#/.test(line)) {
return;
}
const [_registry, cc, type, start, value, _date, _status] = line.split('|');
if (cc !== 'CN') {
return;
}
if (type === 'ipv4') {
const address = start;
const len = Math.log2(Number(value));
if (Math.ceil(len) !== len) {
console.error(`!!!!WARNING!!!!: ${address}/${value} can't coverter to CIDR-style range.`);
}
const netmask = 32 - Math.ceil(len);
result.push([address, netmask]);
} else if (type === 'ipv6') {
result.push([start, value]);
}
});
rl.on('error', reject);
rl.on('close', () => {
result.sort((a, b) => Math.sign(b[1] - a[1]));
resolve(result);
});
});
}
function output(ary) {
console.log(`PRIORITY=$\{PRIORITY:-${PRIORITY}}`);
const otherAddresses = [
/** IPv4 **/
'1.1.1.1', '1.0.0.1', /* Cloudflare DNS */
'8.8.8.8', '8.8.4.4', /* Google DNS */
'9.9.9.9', '149.112.112.112', /* Qua9 DNS */
'0.0.0.0/8', /* Current network (From wiki) */
'10.0.0.0/8', /* Private Internet Address */
'100.64.0.0/10', /* Private Internet Address */
'169.254.0.0/16', /* APIPA - Automatic Private IP Addressing */
'172.16.0.0/12', /* Private Internet Address */
'192.168.0.0/17', /* 192.168.0.0-192.168.127.255, 192.168.128.0-192.168.255.255 用于 WireGuard 内网 */
'198.18.0.0/15', /* Private network */
'224.0.0.0/4', /* IP multicast */
'255.255.255.255/32',
/** IPv6 **/
'fc00::/7', /* Unique local addresses */
'fe80::/10', /* Link-local addresses */
'fec0::/10', /* Site-local addresses */
'ff00::/8', /* Multicast addresses */
];
for (const r of otherAddresses) {
const type = r.indexOf(':') >= 0 ? '6' : '4';
console.log(`ip -${type} rule add to ${r} priority $\{PRIORITY}`);
}
for (const [address, netmask] of ary) {
const type = address.indexOf(':') >= 0 ? '6' : '4';
console.log(`ip -${type} rule add to ${address}/${netmask} priority $\{PRIORITY}`);
}
}
fetch(DOWNLOAD_URI).then(parse).then(output);