-
Notifications
You must be signed in to change notification settings - Fork 184
/
CloudflareCIDR-main.py
44 lines (37 loc) · 1.3 KB
/
CloudflareCIDR-main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import os
import shutil
import zipfile
import requests
import re # 导入正则表达式库
# 下载zip文件
url = "https://github.com/ipverse/asn-ip/archive/refs/heads/master.zip"
r = requests.get(url)
with open("master.zip", "wb") as code:
code.write(r.content)
# 解压zip文件
with zipfile.ZipFile("master.zip", 'r') as zip_ref:
zip_ref.extractall(".")
# 将结果存储在这个列表中
ip_addresses = []
included_asns = ['209242', '13335', '149648', '132892', '139242', '202623', '203898', '394536']
# 遍历as文件夹
for root, dirs, files in os.walk("asn-ip-master/as"):
if 'ipv4-aggregated.txt' in files:
asn = root.split('/')[-1]
if asn in included_asns:
with open(os.path.join(root, 'ipv4-aggregated.txt'), 'r') as file:
ips = file.read().splitlines()
ip_addresses.extend(ips)
# 正则表达式用于匹配IPv4地址和子网掩码
ipv4_regex = re.compile(r'^(\d{1,3}\.){3}\d{1,3}(/\d{1,2})$')
# 将结果写入一个新的文件
with open('Clash/CloudflareCIDR.list', 'w') as file:
for ip in ip_addresses:
# 检查IP是否符合IPv4/子网掩码格式
if ipv4_regex.match(ip):
file.write(f"IP-CIDR,{ip},no-resolve\n")
else:
file.write(f"{ip}\n")
# 清理下载的zip文件和解压的文件夹
os.remove("master.zip")
shutil.rmtree("asn-ip-master")