-
Notifications
You must be signed in to change notification settings - Fork 83
/
build.py
executable file
·72 lines (58 loc) · 2.09 KB
/
build.py
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
#!/usr/bin/python3
import os
from datetime import datetime
import ipaddress
import requests
from requests.exceptions import RequestException, HTTPError
import gfwlist
SOURCES = {
'ipdeny.com': 'http://www.ipdeny.com/ipblocks/data/aggregated/cn-aggregated.zone',
'17mon': 'https://raw.githubusercontent.com/17mon/china_ip_list/master/china_ip_list.txt',
}
OUT_DIR = "dist"
# Stub content to disable GFWList check
GFWLIST_STUB = "var DOMAINS = {};\nvar BLACKPAT = [];\nvar WHITEPAT = [];\n"
def fetch_and_convert(src):
response = requests.get(src)
response.raise_for_status()
template = "var CHINA = [\n{}\n];\n"
lines = []
for iprange in response.text.strip().split("\n"):
ipnet = ipaddress.IPv4Network(iprange)
netaddr = int(ipnet.network_address)
netmask = int(ipnet.netmask)
s = f" [0x{netaddr:08X}, 0x{netmask:08X}], // {iprange}"
lines.append(s)
lines.append(" [0xFFFFFFFF, 0xFFFFFFFF] // 255.255.255.255/32") # use broadcast as a placeholder
return template.format("\n".join(lines))
def main():
now = datetime.utcnow()
date = now.strftime("%Y%m%d")
with open("code.js", "r") as f:
code = f.read()
code = code.replace("@@TIME@@", now.isoformat()[:-7])
gfwlist_part = gfwlist.generate_pac_partial()
gfwlist_stub = GFWLIST_STUB
os.makedirs(OUT_DIR, mode=0o755, exist_ok=True)
for key in SOURCES:
print(f"Generating PAC script from source {key}")
try:
data = fetch_and_convert(SOURCES[key])
except RequestException:
continue
except HTTPError:
continue
filename = f"pac-{key}.txt"
filename_gfwlist = f"pac-gfwlist-{key}.txt"
with open(os.path.join(OUT_DIR, filename), "w") as f:
f.write(code)
f.write(data)
f.write("\n")
f.write(gfwlist_stub)
with open(os.path.join(OUT_DIR, filename_gfwlist), "w") as f:
f.write(code)
f.write(data)
f.write("\n")
f.write(gfwlist_part)
if __name__ == '__main__':
main()