重构:模块化 RRPP Lite 并更新入口
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""更新 pac_ip_cn.json:从 17mon/china_ip_list 拉取中国 IPv4 段,写入带
|
||||
version/date 戳的本地 PAC 数据文件(客户端按 pac_manifest.json 比对在线更新)。
|
||||
|
||||
用法:
|
||||
python update_pac_ip_cn.py # 拉取并更新(先自动备份)
|
||||
python update_pac_ip_cn.py --dry-run # 只统计,不写文件
|
||||
"""
|
||||
import argparse
|
||||
import ipaddress
|
||||
import json
|
||||
import sys
|
||||
import urllib.request
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
|
||||
SRC_URL = "https://raw.githubusercontent.com/17mon/china_ip_list/master/china_ip_list.txt"
|
||||
|
||||
SCRIPT_DIR = Path(__file__).resolve().parent
|
||||
LITE_DIR = SCRIPT_DIR.parent.parent
|
||||
PAC_FILE = LITE_DIR / "pac_ip_cn.json"
|
||||
BACKUP_DIR = LITE_DIR / "backups"
|
||||
|
||||
|
||||
def fetch_lines(tries=2):
|
||||
for i in range(tries):
|
||||
try:
|
||||
with urllib.request.urlopen(SRC_URL, timeout=20) as r:
|
||||
return r.read().decode("utf-8", "replace").splitlines()
|
||||
except Exception:
|
||||
if i == tries - 1:
|
||||
raise
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="更新 pac_ip_cn.json")
|
||||
parser.add_argument("--dry-run", action="store_true", help="只统计,不写文件")
|
||||
args = parser.parse_args()
|
||||
|
||||
print(f"拉取: {SRC_URL}")
|
||||
cidrs, skipped = [], 0
|
||||
for line in fetch_lines():
|
||||
s = line.strip()
|
||||
if not s or s.startswith("#"):
|
||||
continue
|
||||
try:
|
||||
net = ipaddress.ip_network(s, strict=False)
|
||||
if net.version == 4:
|
||||
cidrs.append(str(net))
|
||||
else:
|
||||
skipped += 1
|
||||
except ValueError:
|
||||
skipped += 1
|
||||
cidrs.sort(key=lambda c: (int(ipaddress.ip_network(c, strict=False).network_address),
|
||||
ipaddress.ip_network(c, strict=False).prefixlen))
|
||||
print(f"共 {len(cidrs)} 条 IPv4 CIDR,跳过 {skipped} 条")
|
||||
|
||||
if args.dry_run:
|
||||
print("--dry-run: 未写文件")
|
||||
return
|
||||
|
||||
now = datetime.now()
|
||||
comment = (
|
||||
f"中国 IPv4 地址段(CIDR)。智能分流依据:解析 IP 命中列表 → 直连,"
|
||||
f"未命中 → 走加密隧道。由 others/update_pac_ip_cn/update_pac_ip_cn.py "
|
||||
f"从 17mon/china_ip_list 生成于 {now.strftime('%Y-%m-%d %H:%M')};"
|
||||
f"客户端按文件内 version/date 从 CDN 在线更新到 %LOCALAPPDATA%\\RRPP。"
|
||||
)
|
||||
out = {"_comment": comment,
|
||||
"version": now.strftime("%Y%m%d%H%M"),
|
||||
"date": now.strftime("%Y-%m-%d"),
|
||||
"cidrs": cidrs}
|
||||
|
||||
BACKUP_DIR.mkdir(exist_ok=True)
|
||||
bak = BACKUP_DIR / f"pac_ip_cn.json.bak_{now.strftime('%Y%m%d_%H%M%S')}"
|
||||
if PAC_FILE.is_file():
|
||||
bak.write_bytes(PAC_FILE.read_bytes())
|
||||
print(f"已备份: {bak}")
|
||||
|
||||
PAC_FILE.write_text(json.dumps(out, ensure_ascii=False, indent=1) + "\n", "utf-8")
|
||||
size = PAC_FILE.stat().st_size / 1024
|
||||
print(f"已写入: {PAC_FILE}({len(cidrs)} 条,{size:.0f} KB)")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user