2024-07-30 11:02:10 +08:00
|
|
|
import gzip
|
2024-07-30 10:30:19 +08:00
|
|
|
import ipaddress
|
|
|
|
import re
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
|
|
|
|
2024-07-30 11:02:10 +08:00
|
|
|
def process_blacklist(url, output_file_name):
|
2024-07-30 10:30:19 +08:00
|
|
|
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 "/" in line:
|
|
|
|
network = ipaddress.ip_network(line, strict=False)
|
|
|
|
ip_range = f"{network.network_address}-{network.broadcast_address}"
|
|
|
|
else:
|
|
|
|
ip_range = f"{line}-{line}"
|
2024-07-30 13:03:23 +08:00
|
|
|
processed_lines.append(f"btn:{ip_range}")
|
2024-07-30 10:30:19 +08:00
|
|
|
|
2024-07-30 11:02:10 +08:00
|
|
|
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:
|
2024-07-30 10:30:19 +08:00
|
|
|
for line in processed_lines:
|
|
|
|
file.write(line + "\n")
|
|
|
|
|
|
|
|
|
2024-07-30 11:02:10 +08:00
|
|
|
# 调用函数处理远程文件并压缩为.gz格式
|
2024-07-30 10:30:19 +08:00
|
|
|
process_blacklist(
|
|
|
|
"https://raw.githubusercontent.com/PBH-BTN/BTN-Collected-Rules/main/combine/all.txt",
|
2024-07-30 11:02:10 +08:00
|
|
|
"transmission_blacklist",
|
2024-07-30 10:30:19 +08:00
|
|
|
)
|