This commit is contained in:
iBug 2018-08-28 00:52:26 +08:00
parent f48823f55c
commit 3fdd6e1c65
6 changed files with 10247 additions and 0 deletions

8
.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Output
out.pac
# Python
__pycache__/
# Other
tmp*

15
build.sh Executable file
View File

@ -0,0 +1,15 @@
#!/bin/sh
SOURCE="http://www.ipdeny.com/ipblocks/data/aggregated/cn-aggregated.zone"
wget -O ip_ranges.txt "$SOURCE"
cat >> ip_ranges.txt <<EOF
10.0.0.0/8
127.0.0.0/16
129.254.0.0/16
172.16.0.0/12
192.168.0.0/16
EOF
python3 ranges_to_js.py > tmp.js
cat code.js tmp.js > out.pac
rm tmp.js

5109
bypass-lan-and-china.pac Normal file

File diff suppressed because it is too large Load Diff

40
code.js Normal file
View File

@ -0,0 +1,40 @@
// Author: iBug
function belongsToSubnet(host, list) {
var ip = host.split(".");
ip = 0x1000000 * Number(ip[0]) + 0x10000 * Number(ip[1]) +
0x100 * Number(ip[2]) + Number(ip[3]);
if (ip < list[0][0])
return false;
// Binary search
var x = 0, y = list.length, middle;
while (y - x > 1) {
middle = Math.floor((x + y) / 2);
if (list[middle][0] < ip)
x = middle;
else
y = middle;
}
// Match
var masked = ip & list[x][1];
return (masked >>> 0) == (list[x][0] >>> 0);
}
var proxy = "__PROXY__";
var direct = "DIRECT";
function FindProxyForURL(url, host) {
var remote = dnsResolve(host);
if (belongsToSubnet(remote, WHITELIST)) {
return direct;
}
return proxy;
}
// Format: [Hex IP, mask]
// e.g. 1.0.1.0/24 = [0x80008000, 0xFFFFFF00]
// Source: http://www.ipdeny.com/ipblocks/data/aggregated/cn-aggregated.zone

5052
ip_ranges.txt Normal file

File diff suppressed because it is too large Load Diff

23
ranges_to_js.py Normal file
View File

@ -0,0 +1,23 @@
#!/usr/bin/python3
def ip_to_num(s):
return sum([int(n) * (256 ** int(e)) for e, n in enumerate(s.split(".")[::-1])])
def n_to_mask(n):
return (2**32 - 1) >> (32 - n) << (32 - n)
def to_upper_hex(n):
return "{:#010X}".format(n).replace("X", "x")
with open("ip_ranges.txt", "r") as f:
text = f.read()
lines = [line for line in text.split("\n") if line]
pairs = [p.split("/") for p in lines]
data = [(ip_to_num(s), n_to_mask(int(n))) for s, n in pairs]
data = sorted(data, key=lambda x: x[0])
out = ",\n".join([" [{}, {}]".format(to_upper_hex(a), to_upper_hex(b)) for a, b in data])
out = "var WHITELIST = [\n" + out + "\n];"
print(out)