forked from cmliu/AutoCloudflareSpeedTest
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRemoveCFIPs.py
More file actions
31 lines (25 loc) · 956 Bytes
/
RemoveCFIPs.py
File metadata and controls
31 lines (25 loc) · 956 Bytes
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
import os
import sys
import requests
import ipaddress
# 获取命令行参数
filename = sys.argv[1] if len(sys.argv) > 1 else 'ip.txt'
# 检查文件是否存在
if not os.path.exists(filename):
print(f"{filename} 文件不存在")
else:
# 获取Cloudflare的IP段
cloudflare_ips = []
for asn in ['AS13335', 'AS209242']:
response = requests.get(f'http://asn2cidr.ssrc.cf/{asn}')
if response.status_code == 200:
cloudflare_ips.extend(response.text.split('\n'))
# 读取文件中的IP
with open(filename, 'r') as file:
ips = file.read().split('\n')
# 删除符合Cloudflare IP段的IP
ips = [ip for ip in ips if ip and not any(ipaddress.ip_address(ip) in ipaddress.ip_network(cf_ip) for cf_ip in cloudflare_ips)]
# 保存回源文件
with open(filename, 'w') as file:
file.write('\n'.join(ips))
print(f"已完成删除 {filename} 中符合Cloudflare IP段的IP")