diff --git a/README.md b/README.md index 974e3d6..0e6107c 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,8 @@ PATH=/usr/sbin:/usr/bin:/sbin:/bin 修改自 [PBH-BTN/BTN-Collected-Rules](https://github.com/PBH-BTN/BTN-Collected-Rules) 的all.txt, github action 每天自动更新 +新增常见数据中心ip段: [isp/cn](https://github.com/zealic/autorosvpn/tree/master/isp/cn) + 设置transmission的URL阻止清单`https://github.com/jqtmviyu/Transmission-block-xunlei-pcdn/raw/main/transmission_blacklist.gz` ### 定时更新 diff --git a/process_blacklist.py b/process_blacklist.py index d7bfd95..ada026d 100644 --- a/process_blacklist.py +++ b/process_blacklist.py @@ -4,39 +4,40 @@ import re import requests +output_file_name = "transmission_blacklist" -def process_blacklist(url, output_file_name): +upstream_urls = [ + "https://raw.githubusercontent.com/PBH-BTN/BTN-Collected-Rules/main/combine/all.txt", + "https://raw.githubusercontent.com/zealic/autorosvpn/master/isp/cn/route-isp-chinanet.txt", # 中国电信 + "https://raw.githubusercontent.com/zealic/autorosvpn/master/isp/cn/route-isp-cn-cmcc.txt", # 中国移动 + "https://raw.githubusercontent.com/zealic/autorosvpn/master/isp/cn/route-isp-cncgroup.txt", # 中国联通 + "https://raw.githubusercontent.com/zealic/autorosvpn/master/isp/cn/route-isp-cn-crtc.txt", # 中国铁通 +] + +processed_lines = set() + +for url in upstream_urls: response = requests.get(url) lines = response.text.splitlines() - - processed_lines = [] - for line in lines: line = line.strip() if not line or line.startswith("#"): continue - # 检查是否是合法的IP地址或CIDR - if re.match(r"(\d{1,3}\.){3}\d{1,3}(\/\d{1,2})?$", line) or re.match( - r"([a-fA-F0-9:]+)(\/\d{1,3})?$", line - ): + # 过滤掉注释 + if re.match(r"^\d", line): if "/" in line: network = ipaddress.ip_network(line, strict=False) ip_range = f"{network.network_address}-{network.broadcast_address}" + elif "-" in line: + ip_range = line else: ip_range = f"{line}-{line}" - processed_lines.append(f"btn:{ip_range}") + processed_lines.add(f"btn:{ip_range}") - with open(f"{output_file_name}.txt", "w", encoding="utf-8") as file: - for line in processed_lines: - file.write(line + "\n") +with open(f"{output_file_name}.txt", "w", encoding="utf-8") as file: + for line in processed_lines: + file.write(line + "\n") - with gzip.open(f"{output_file_name}.gz", "wt", encoding="utf-8") as file: - for line in processed_lines: - file.write(line + "\n") - - -# 调用函数处理远程文件并压缩为.gz格式 -process_blacklist( - "https://raw.githubusercontent.com/PBH-BTN/BTN-Collected-Rules/main/combine/all.txt", - "transmission_blacklist", -) +with gzip.open(f"{output_file_name}.gz", "wt", encoding="utf-8") as file: + for line in processed_lines: + file.write(line + "\n")