From ce8a54d7dbf2417764cc2d423ba2e6361e951b9d Mon Sep 17 00:00:00 2001 From: iBug Date: Thu, 2 Jul 2020 20:42:14 +0800 Subject: [PATCH] Recreate Python build script --- .gitignore | 3 ++- build.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100755 build.py diff --git a/.gitignore b/.gitignore index b95dbdd..2d0706e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # Output -out.pac +*.pac +dist/ # Python __pycache__/ diff --git a/build.py b/build.py new file mode 100755 index 0000000..f8cef04 --- /dev/null +++ b/build.py @@ -0,0 +1,56 @@ +#!/usr/bin/python3 + +import os +from datetime import datetime +import ipaddress +import requests +from requests.exceptions import RequestException, HTTPError + + +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" + + +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]) + + if not os.path.exists(OUT_DIR): + os.mkdir(OUT_DIR, mode=0o755) + 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-{date}-{key}.txt" + with open(os.path.join(OUT_DIR, filename), "w") as f: + f.write(code) + f.write(data) + + +if __name__ == '__main__': + main()