-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeed.py
59 lines (54 loc) · 2.11 KB
/
speed.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import re
import requests
from colo_emojis import colo_emojis # 导入 colo_emojis 字典
def get_colo(ip_address):
try:
# 根据 IP 类型构造 URL
if ":" in ip_address: # IPv6
url = f"http://[{ip_address}]/cdn-cgi/trace"
else: # IPv4
url = f"http://{ip_address}/cdn-cgi/trace"
response = requests.get(url, timeout=5)
if response.status_code == 200:
trace_data = response.text
colo_match = re.search(r'colo=([A-Z]+)', trace_data)
if colo_match:
return colo_match.group(1)
except requests.RequestException:
pass
return "CF优选"
def replace_colo(file_path, is_ipv6=False):
with open(file_path, 'r') as file:
lines = file.readlines()
new_lines = []
for line in lines:
# 匹配 IPv4 或 IPv6 地址
if is_ipv6:
match = re.match(r'\[([0-9a-fA-F:]+)\]:(\d+)#(.*?)┃⚡(\d+\.\d+\(MB/s\))', line)
else:
match = re.match(r'([0-9.]+):(\d+)#(.*?)┃⚡(\d+\.\d+\(MB/s\))', line)
if match:
ip_address = match.group(1)
port = match.group(2)
colo = match.group(3)
speed = match.group(4)
new_colo = get_colo(ip_address)
# 获取对应的 Emoji,如果不存在则使用默认的 ☁️
emoji = colo_emojis.get(new_colo, "☁️")
# 打印 IP 和 colo 信息
print(f"File: {file_path}, IP: {ip_address}, Colo: {new_colo}, Emoji: {emoji}")
# 替换原有的 colo 信息
if is_ipv6: # IPv6
new_line = f"[{ip_address}]:{port}#{emoji}{new_colo}┃⚡{speed}\n"
else: # IPv4
new_line = f"{ip_address}:{port}#{emoji}{new_colo}┃⚡{speed}\n"
new_lines.append(new_line)
else:
new_lines.append(line)
with open(file_path, 'w') as file:
file.writelines(new_lines)
if __name__ == "__main__":
# 处理 IPv4 文件
replace_colo('speed/ip.txt', is_ipv6=False)
# 处理 IPv6 文件
replace_colo('speed/ipv6.txt', is_ipv6=True)