forked from gacjie/cf2dns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cf2dns_actions.py
175 lines (164 loc) · 9.66 KB
/
cf2dns_actions.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Mail: [email protected]
import sys,os,json,requests,time,base64,shutil,random,traceback
# 生成随机时间,范围在10到150之间
#random_time = random.uniform(10, 100)
#print("本次将等待{}秒执行".format(random_time))
# 延迟执行随机时间
#time.sleep(random_time)
from dns.qCloud import QcloudApiv3 # QcloudApiv3 DNSPod 的 API 更新了 By github@z0z0r4
from dns.aliyun import AliApi
from dns.huawei import HuaWeiApi
config = json.loads(os.environ["CONFIG"])
#CM:移动 CU:联通 CT:电信 AB:境外 DEF:默认
#修改需要更改的dnspod域名和子域名
DOMAINS = json.loads(os.environ["DOMAINS"])
#获取服务商信息
provider_data = json.loads(os.environ["PROVIDER"])
def get_optimization_ip():
try:
headers = {'Content-Type': 'application/json'}
data = {"key": config["key"], "type":iptype }
provider = [item for item in provider_data if item['id'] == config["data_server"]][0]
response = requests.post(provider['get_ip_url'], json=data, headers=headers)
if response.status_code == 200:
return response.json()
else:
print("CHANGE OPTIMIZATION IP ERROR: REQUEST STATUS CODE IS NOT 200")
return None
except Exception as e:
print("CHANGE OPTIMIZATION IP ERROR: " + str(e))
return None
def changeDNS(line, s_info, c_info, domain, sub_domain, cloud):
global config
if iptype == 'v6':
recordType = "AAAA"
else:
recordType = "A"
lines = {"CM": "移动", "CU": "联通", "CT": "电信", "AB": "境外", "DEF": "默认"}
line = lines[line]
try:
create_num = config["affect_num"] - len(s_info)
if create_num == 0:
for info in s_info:
if len(c_info) == 0:
break
cf_ip = c_info.pop(random.randint(0,len(c_info)-1))["ip"]
if cf_ip in str(s_info):
continue
ret = cloud.change_record(domain, info["recordId"], sub_domain, cf_ip, recordType, line, config["ttl"])
if(config["dns_server"] != 1 or ret["code"] == 0):
print("CHANGE DNS SUCCESS: ----Time: " + str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) + "----DOMAIN: " + domain + "----SUBDOMAIN: " + sub_domain + "----RECORDLINE: "+line+"----RECORDID: " + str(info["recordId"]) + "----VALUE: " + cf_ip )
else:
print("CHANGE DNS ERROR: ----Time: " + str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) + "----DOMAIN: " + domain + "----SUBDOMAIN: " + sub_domain + "----RECORDLINE: "+line+"----RECORDID: " + str(info["recordId"]) + "----VALUE: " + cf_ip + "----MESSAGE: " + ret["message"] )
elif create_num > 0:
for i in range(create_num):
if len(c_info) == 0:
break
cf_ip = c_info.pop(random.randint(0,len(c_info)-1))["ip"]
if cf_ip in str(s_info):
continue
ret = cloud.create_record(domain, sub_domain, cf_ip, recordType, line, config["ttl"])
if(config["dns_server"] != 1 or ret["code"] == 0):
print("CREATE DNS SUCCESS: ----Time: " + str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) + "----DOMAIN: " + domain + "----SUBDOMAIN: " + sub_domain + "----RECORDLINE: "+line+"----VALUE: " + cf_ip )
else:
print("CREATE DNS ERROR: ----Time: " + str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) + "----DOMAIN: " + domain + "----SUBDOMAIN: " + sub_domain + "----RECORDLINE: "+line+"----RECORDID: " + str(info["recordId"]) + "----VALUE: " + cf_ip + "----MESSAGE: " + ret["message"] )
else:
for info in s_info:
if create_num == 0 or len(c_info) == 0:
break
cf_ip = c_info.pop(random.randint(0,len(c_info)-1))["ip"]
if cf_ip in str(s_info):
create_num += 1
continue
ret = cloud.change_record(domain, info["recordId"], sub_domain, cf_ip, recordType, line, config["ttl"])
if(config["dns_server"] != 1 or ret["code"] == 0):
print("CHANGE DNS SUCCESS: ----Time: " + str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) + "----DOMAIN: " + domain + "----SUBDOMAIN: " + sub_domain + "----RECORDLINE: "+line+"----RECORDID: " + str(info["recordId"]) + "----VALUE: " + cf_ip )
else:
print("CHANGE DNS ERROR: ----Time: " + str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) + "----DOMAIN: " + domain + "----SUBDOMAIN: " + sub_domain + "----RECORDLINE: "+line+"----RECORDID: " + str(info["recordId"]) + "----VALUE: " + cf_ip + "----MESSAGE: " + ret["message"] )
create_num += 1
except Exception as e:
print("CHANGE DNS ERROR: ----Time: " + str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) + "----MESSAGE: " + str(traceback.print_exc()))
def main(cloud):
global config
if iptype == 'v6':
recordType = "AAAA"
else:
recordType = "A"
if len(DOMAINS) > 0:
try:
cfips = get_optimization_ip()
if cfips == None or cfips["code"] != 200:
print("GET CLOUDFLARE IP ERROR: ----Time: " + str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) )
return
cf_cmips = cfips["info"]["CM"]
cf_cuips = cfips["info"]["CU"]
cf_ctips = cfips["info"]["CT"]
for domain, sub_domains in DOMAINS.items():
for sub_domain, lines in sub_domains.items():
temp_cf_cmips = cf_cmips.copy()
temp_cf_cuips = cf_cuips.copy()
temp_cf_ctips = cf_ctips.copy()
temp_cf_abips = cf_ctips.copy()
temp_cf_defips = cf_ctips.copy()
if config["dns_server"] == 1:
ret = cloud.get_record(domain, 20, sub_domain, "CNAME")
if ret["code"] == 0:
for record in ret["data"]["records"]:
if record["line"] == "移动" or record["line"] == "联通" or record["line"] == "电信":
retMsg = cloud.del_record(domain, record["id"])
if(retMsg["code"] == 0):
print("DELETE DNS SUCCESS: ----Time: " + str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) + "----DOMAIN: " + domain + "----SUBDOMAIN: " + sub_domain + "----RECORDLINE: "+record["line"] )
else:
print("DELETE DNS ERROR: ----Time: " + str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) + "----DOMAIN: " + domain + "----SUBDOMAIN: " + sub_domain + "----RECORDLINE: "+record["line"] + "----MESSAGE: " + retMsg["message"] )
ret = cloud.get_record(domain, 100, sub_domain, recordType)
if config["dns_server"] != 1 or ret["code"] == 0 :
if config["dns_server"] == 1 and "Free" in ret["data"]["domain"]["grade"] and config["affect_num"] > 2:
config["affect_num"] = 2
cm_info = []
cu_info = []
ct_info = []
ab_info = []
def_info = []
for record in ret["data"]["records"]:
info = {}
info["recordId"] = record["id"]
info["value"] = record["value"]
if record["line"] == "移动":
cm_info.append(info)
elif record["line"] == "联通":
cu_info.append(info)
elif record["line"] == "电信":
ct_info.append(info)
elif record["line"] == "境外":
ab_info.append(info)
elif record["line"] == "默认":
def_info.append(info)
for line in lines:
if line == "CM":
changeDNS("CM", cm_info, temp_cf_cmips, domain, sub_domain, cloud)
elif line == "CU":
changeDNS("CU", cu_info, temp_cf_cuips, domain, sub_domain, cloud)
elif line == "CT":
changeDNS("CT", ct_info, temp_cf_ctips, domain, sub_domain, cloud)
elif line == "AB":
changeDNS("AB", ab_info, temp_cf_abips, domain, sub_domain, cloud)
elif line == "DEF":
changeDNS("DEF", def_info, temp_cf_defips, domain, sub_domain, cloud)
except Exception as e:
traceback.print_exc()
print("CHANGE DNS ERROR: ----Time: " + str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) + "----MESSAGE: " + str(traceback.print_exc()))
if __name__ == '__main__':
if config["dns_server"] == 1:
cloud = QcloudApiv3(config["secretid"], config["secretkey"])
elif config["dns_server"] == 2:
cloud = AliApi(config["secretid"], config["secretkey"], config["region_ali"])
elif config["dns_server"] == 3:
cloud = HuaWeiApi(config["secretid"], config["secretkey"], config["region_hw"])
if config["ipv4"] == "on":
iptype = "v4"
main(cloud)
if config["ipv6"] == "on":
iptype = "v6"
main(cloud)