-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathindex.js
115 lines (89 loc) · 3.13 KB
/
index.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
const fs = require('fs');
const ip = require('ip');
const package = require('./package.json')
const CIDR_PATH = './cn-aggregated.zone.txt';
const TEMPLATE_PAC_PATH = './whitelist_template.pac';
const DIST_PAC_PATH = './whitelist.pac';
const PROXY = 'SOCKS5 localhost:1080';
const cidrsFile = fs.readFileSync(CIDR_PATH);
const cidrs = cidrsFile.toString().split(/\n|\r\n/);
const pacTemplateFile = fs.readFileSync(TEMPLATE_PAC_PATH).toString();
const ipRepo = [];
const addCIDR = cidr => {
if (cidr) {
const cidrData = ip.cidrSubnet(cidr);
const ipStrStart = cidrData.firstAddress;
const ipStrEnd = cidrData.lastAddress;
const ipLongStart = ip.toLong(ipStrStart);
const ipLongEnd = ip.toLong(ipStrEnd);
ipRepo.push([ipLongStart, ipLongEnd]);
}
};
cidrs.forEach(addCIDR)
addCIDR('10.0.0.0/8');
addCIDR('127.0.0.1/32');
addCIDR('100.64.0.0/10');
addCIDR('172.16.0.0/12');
addCIDR('192.168.0.0/16');
ipRepo.sort((a, b) => a[0] - b[0]);
test('192.168.55.32') // Local
test('123.58.180.8') // China Netease
test('46.82.174.68') // Hongkong Google
function test(testIp) {
console.log('\ntesting: ' + testIp);
var testIpLong = ip.toLong(testIp);
var startRange = 0;
var endRange = ipRepo.length;
var leftPot = parseInt((startRange + endRange) / 2);
var rightPot = leftPot + 1;
var leftLong = ipRepo[leftPot][1];
var rightLong = ipRepo[rightPot][0];
while (1) {
if (testIpLong <= leftLong) {
endRange = leftPot;
var leftMin = ipRepo[leftPot][0];
var leftMax = ipRepo[leftPot][1];
console.log('- left of: ', leftPot);
if (testIpLong >= leftMin && testIpLong <= leftMax) {
console.log(`+ match ${testIpLong} between ${leftMin}, ${leftMax} of index-${leftPot}`);
break;
}
leftPot = parseInt((startRange + endRange) / 2);
rightPot = leftPot + 1;
leftLong = ipRepo[leftPot][1];
rightLong = ipRepo[rightPot][0];
} else if (testIpLong >= rightLong) {
startRange = rightPot;
var rightMin = ipRepo[rightPot][0];
var rightMax = ipRepo[rightPot][1];
console.log('- right of: ', rightPot);
if (testIpLong >= rightMin && testIpLong <= rightMax) {
console.log(`+ match ${testIpLong} between ${rightMin}, ${rightMax} of index-${rightPot}`);
break;
}
leftPot = parseInt((startRange + endRange) / 2);
rightPot = leftPot + 1;
leftLong = ipRepo[leftPot][1];
rightLong = ipRepo[rightPot][0];
} else {
// No match -> Pass proxy
console.log('+ no match');
break;
}
}
}
let ipsStr = '';
const addIpInt = (start, end) => ipsStr += ` [${start}, ${end}], \n`;
ipRepo.forEach(ipRange => addIpInt(ipRange[0], ipRange[1]));
Object.assign(String.prototype, {
applyTemplate(key, content) {
return this.replace(new RegExp(`\{\#${key}\}`, 'g'), content);
}
});
var pacContent = pacTemplateFile
.applyTemplate('ipRepo', ipsStr)
.applyTemplate('date', new Date().toUTCString())
.applyTemplate('proxy', PROXY)
.applyTemplate('version', package.version);
fs.writeFileSync(DIST_PAC_PATH, pacContent);
console.log('\nTest Done / Generated');