forked from jc-lw/youxuanyuming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbestdomain.py
81 lines (72 loc) · 3.13 KB
/
bestdomain.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import os
import requests
def get_ip_list(url):
response = requests.get(url)
response.raise_for_status()
return response.text.strip().split('\n')
def get_cloudflare_zone(api_token):
headers = {
'Authorization': f'Bearer {api_token}',
'Content-Type': 'application/json',
}
response = requests.get('https://api.cloudflare.com/client/v4/zones', headers=headers)
response.raise_for_status()
zones = response.json().get('result', [])
if not zones:
raise Exception("No zones found")
return zones[0]['id'], zones[0]['name']
def delete_existing_dns_records(api_token, zone_id, subdomain, domain):
headers = {
'Authorization': f'Bearer {api_token}',
'Content-Type': 'application/json',
}
record_name = domain if subdomain == '@' else f'{subdomain}.{domain}'
while True:
response = requests.get(f'https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records?type=A&name={record_name}', headers=headers)
response.raise_for_status()
records = response.json().get('result', [])
if not records:
break
for record in records:
delete_response = requests.delete(f'https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records/{record["id"]}', headers=headers)
delete_response.raise_for_status()
print(f"Del {subdomain}:{record['id']}")
def update_cloudflare_dns(ip_list, api_token, zone_id, subdomain, domain):
headers = {
'Authorization': f'Bearer {api_token}',
'Content-Type': 'application/json',
}
record_name = domain if subdomain == '@' else f'{subdomain}.{domain}'
for ip in ip_list:
data = {
"type": "A",
"name": record_name,
"content": ip,
"ttl": 1,
"proxied": False
}
response = requests.post(f'https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records', json=data, headers=headers)
if response.status_code == 200:
print(f"Add {subdomain}:{ip}")
else:
print(f"Failed to add A record for IP {ip} to subdomain {subdomain}: {response.status_code} {response.text}")
if __name__ == "__main__":
api_token = os.getenv('CF_API_TOKEN')
# 示例URL和子域名对应的IP列表
subdomain_ip_mapping = {
#'bestcf': 'https://ipdb.030101.xyz/api/bestcf.txt', # #域名一,bestcf.域名.com
'api': 'https://raw.githubusercontent.com/xjl0307/youxuanyuming/refs/heads/main/ip.txt', #域名二,api.域名.com
# 添加更多子域名和对应的IP列表URL
}
try:
# 获取Cloudflare域区ID和域名
zone_id, domain = get_cloudflare_zone(api_token)
for subdomain, url in subdomain_ip_mapping.items():
# 获取IP列表
ip_list = get_ip_list(url)
# 删除现有的DNS记录
delete_existing_dns_records(api_token, zone_id, subdomain, domain)
# 更新Cloudflare DNS记录
update_cloudflare_dns(ip_list, api_token, zone_id, subdomain, domain)
except Exception as e:
print(f"Error: {e}")