mirror of
https://github.com/iBug/pac.git
synced 2025-07-13 21:02:16 +08:00
Recreate Python build script
This commit is contained in:
parent
854289a674
commit
ce8a54d7db
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,5 +1,6 @@
|
||||
# Output
|
||||
out.pac
|
||||
*.pac
|
||||
dist/
|
||||
|
||||
# Python
|
||||
__pycache__/
|
||||
|
56
build.py
Executable file
56
build.py
Executable file
@ -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()
|
Loading…
Reference in New Issue
Block a user