Skip to content

Commit

Permalink
IPv6 support
Browse files Browse the repository at this point in the history
  • Loading branch information
ddgth committed Jul 6, 2022
1 parent f7f1edd commit 1880ed0
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 120 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
### 新增优选IPv6功能 ——update 2022.07.06
> 实现方法
​ 更新代码,修改脚本中的 `TYPE` 参数即可

### 新增默认线路记录 ——update 2021.12.15

详情查看addline分支
Expand Down
1 change: 0 additions & 1 deletion cf2dns
Submodule cf2dns deleted from 9e5af1
97 changes: 0 additions & 97 deletions cf2dns.log

Large diffs are not rendered by default.

56 changes: 45 additions & 11 deletions cf2dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
#可以从https://shop.hostmonit.com获取
KEY = "o1zrmHAF"

#CM:移动 CU:联通 CT:电信
#修改需要更改的dnspod域名核子域名
#CM:移动 CU:联通 CT:电信 AB:境外 DEF:默认
#修改需要更改的dnspod域名和子域名
DOMAINS = {
"hostmonit.com": {"@": ["CM","CU","CT"], "shop": ["CM", "CU", "CT"], "stock": ["CM","CU","CT"]},
"4096.me": {"@": ["CM","CU","CT"], "vv": ["CM","CU","CT"]}
"xxxx.me": {"@": ["CM","CU","CT"], "vv": ["CM","CU","CT"]}
}

#解析生效条数 免费的DNSPod相同线路最多支持2条解析
Expand All @@ -34,6 +34,9 @@
#解析生效时间,默认为600秒 如果不是DNS付费版用户 不要修改!!!
TTL = 600

#v4为筛选出IPv4的IP v6为筛选出IPv6的IP
TYPE = 'v4'

#API 密钥
#腾讯云后台获取 https://console.cloud.tencent.com/cam/capi
#阿里云后台获取 https://help.aliyun.com/document_detail/53045.html?spm=a2c4g.11186623.2.11.2c6a2fbdh13O53 注意需要添加DNS控制权限 AliyunDNSFullAccess
Expand All @@ -47,7 +50,7 @@ def get_optimization_ip():
try:
http = urllib3.PoolManager()
headers = headers = {'Content-Type': 'application/json'}
data = {"key": KEY}
data = {"key": KEY, "type": TYPE}
data = json.dumps(data).encode()
response = http.request('POST','https://api.hostmonit.com/get_optimization_ip',body=data, headers=headers)
return json.loads(response.data.decode('utf-8'))
Expand All @@ -56,13 +59,22 @@ def get_optimization_ip():
return None

def changeDNS(line, s_info, c_info, domain, sub_domain, cloud):
global AFFECT_NUM
global AFFECT_NUM, TYPE
if TYPE == 'v6':
recordType = "AAAA"
else:
recordType = "A"

if line == "CM":
line = "移动"
elif line == "CU":
line = "联通"
elif line == "CT":
line = "电信"
elif line == "AB":
line = "境外"
elif line == "DEF":
line = "默认"
else:
log_cf2dns.logger.error("CHANGE DNS ERROR: ----Time: " + str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) + "----MESSAGE: LINE ERROR")
return
Expand All @@ -75,7 +87,7 @@ def changeDNS(line, s_info, c_info, domain, sub_domain, cloud):
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, "A", line, TTL)
ret = cloud.change_record(domain, info["recordId"], sub_domain, cf_ip, recordType, line, TTL)
if(DNS_SERVER != 1 or ret["code"] == 0):
log_cf2dns.logger.info("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:
Expand All @@ -87,7 +99,7 @@ def changeDNS(line, s_info, c_info, domain, sub_domain, cloud):
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, "A", line, TTL)
ret = cloud.create_record(domain, sub_domain, cf_ip, recordType, line, TTL)
if(DNS_SERVER != 1 or ret["code"] == 0):
log_cf2dns.logger.info("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:
Expand All @@ -100,7 +112,7 @@ def changeDNS(line, s_info, c_info, domain, sub_domain, cloud):
if cf_ip in str(s_info):
create_num += 1
continue
ret = cloud.change_record(domain, info["recordId"], sub_domain, cf_ip, "A", line, TTL)
ret = cloud.change_record(domain, info["recordId"], sub_domain, cf_ip, recordType, line, TTL)
if(DNS_SERVER != 1 or ret["code"] == 0):
log_cf2dns.logger.info("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:
Expand All @@ -110,7 +122,11 @@ def changeDNS(line, s_info, c_info, domain, sub_domain, cloud):
log_cf2dns.logger.error("CHANGE DNS ERROR: ----Time: " + str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) + "----MESSAGE: " + str(e))

def main(cloud):
global AFFECT_NUM
global AFFECT_NUM, TYPE
if TYPE == 'v6':
recordType = "AAAA"
else:
recordType = "A"
if len(DOMAINS) > 0:
try:
cfips = get_optimization_ip()
Expand All @@ -125,23 +141,27 @@ def main(cloud):
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 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"] == "电信":
if record["line"] == "移动" or record["line"] == "联通" or record["line"] == "电信" or record["line"] == "境外" or record["line"] == "默认":
retMsg = cloud.del_record(domain, record["id"])
if(retMsg["code"] == 0):
log_cf2dns.logger.info("DELETE DNS SUCCESS: ----Time: " + str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) + "----DOMAIN: " + domain + "----SUBDOMAIN: " + sub_domain + "----RECORDLINE: "+record["line"] )
else:
log_cf2dns.logger.error("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, "A")
ret = cloud.get_record(domain, 100, sub_domain, recordType)
if DNS_SERVER != 1 or ret["code"] == 0 :
if DNS_SERVER == 1 and "Free" in ret["data"]["domain"]["grade"] and AFFECT_NUM > 2:
AFFECT_NUM = 2
cm_info = []
cu_info = []
ct_info = []
ab_info = []
def_info = []
for record in ret["data"]["records"]:
if record["line"] == "移动":
info = {}
Expand All @@ -158,13 +178,27 @@ def main(cloud):
info["recordId"] = record["id"]
info["value"] = record["value"]
ct_info.append(info)
if record["line"] == "境外":
info = {}
info["recordId"] = record["id"]
info["value"] = record["value"]
ab_info.append(info)
if record["line"] == "默认":
info = {}
info["recordId"] = record["id"]
info["value"] = record["value"]
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()
log_cf2dns.logger.error("CHANGE DNS ERROR: ----Time: " + str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) + "----MESSAGE: " + str(e))
Expand Down
53 changes: 43 additions & 10 deletions cf2dns_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

#可以从https://shop.hostmonit.com获取
KEY = os.environ["KEY"] #"o1zrmHAF"
#CM:移动 CU:联通 CT:电信
#修改需要更改的dnspod域名核子域名
#CM:移动 CU:联通 CT:电信 AB:境外 DEF:默认
#修改需要更改的dnspod域名和子域名
DOMAINS = json.loads(os.environ["DOMAINS"]) #{"hostmonit.com": {"@": ["CM","CU","CT"], "shop": ["CM", "CU", "CT"], "stock": ["CM","CU","CT"]},"4096.me": {"@": ["CM","CU","CT"], "vv": ["CM","CU","CT"]}}
#腾讯云后台获取 https://console.cloud.tencent.com/cam/capi
SECRETID = os.environ["SECRETID"] #'AKIDV**********Hfo8CzfjgN'
Expand All @@ -29,14 +29,16 @@
DNS_SERVER = 1
#解析生效时间,默认为600秒 如果不是DNS付费版用户 不要修改!!!
TTL = 600
#v4为筛选出IPv4的IP v6为筛选出IPv6的IP
TYPE = 'v4'

urllib3.disable_warnings()

def get_optimization_ip():
try:
http = urllib3.PoolManager()
headers = headers = {'Content-Type': 'application/json'}
data = {"key": KEY}
data = {"key": KEY, "type": TYPE}
data = json.dumps(data).encode()
response = http.request('POST','https://api.hostmonit.com/get_optimization_ip',body=data, headers=headers)
return json.loads(response.data.decode('utf-8'))
Expand All @@ -45,13 +47,22 @@ def get_optimization_ip():
return None

def changeDNS(line, s_info, c_info, domain, sub_domain, cloud):
global AFFECT_NUM
global AFFECT_NUM, TYPE
if TYPE == 'v6':
recordType = "AAAA"
else:
recordType = "A"

if line == "CM":
line = "移动"
elif line == "CU":
line = "联通"
elif line == "CT":
line = "电信"
elif line == "AB":
line = "境外"
elif line == "DEF":
line = "默认"
else:
print("CHANGE DNS ERROR: ----Time: " + str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) + "----MESSAGE: LINE ERROR")
return
Expand All @@ -64,7 +75,7 @@ def changeDNS(line, s_info, c_info, domain, sub_domain, cloud):
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, "A", line, TTL)
ret = cloud.change_record(domain, info["recordId"], sub_domain, cf_ip, recordType, line, TTL)
if(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:
Expand All @@ -76,7 +87,7 @@ def changeDNS(line, s_info, c_info, domain, sub_domain, cloud):
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, "A", line, TTL)
ret = cloud.create_record(domain, sub_domain, cf_ip, recordType, line, TTL)
if(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:
Expand All @@ -89,7 +100,7 @@ def changeDNS(line, s_info, c_info, domain, sub_domain, cloud):
if cf_ip in str(s_info):
create_num += 1
continue
ret = cloud.change_record(domain, info["recordId"], sub_domain, cf_ip, "A", line, TTL)
ret = cloud.change_record(domain, info["recordId"], sub_domain, cf_ip, recordType, line, TTL)
if(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:
Expand All @@ -99,7 +110,11 @@ def changeDNS(line, s_info, c_info, domain, sub_domain, cloud):
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 AFFECT_NUM
global AFFECT_NUM, TYPE
if TYPE == 'v6':
recordType = "AAAA"
else:
recordType = "A"
if len(DOMAINS) > 0:
try:
cfips = get_optimization_ip()
Expand All @@ -114,23 +129,27 @@ def main(cloud):
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 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"] == "电信":
if record["line"] == "移动" or record["line"] == "联通" or 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, "A")
ret = cloud.get_record(domain, 100, sub_domain, recordType)
if DNS_SERVER != 1 or ret["code"] == 0 :
if DNS_SERVER == 1 and "Free" in ret["data"]["domain"]["grade"] and AFFECT_NUM > 2:
AFFECT_NUM = 2
cm_info = []
cu_info = []
ct_info = []
ab_info = []
def_info = []
for record in ret["data"]["records"]:
if record["line"] == "移动":
info = {}
Expand All @@ -147,13 +166,27 @@ def main(cloud):
info["recordId"] = record["id"]
info["value"] = record["value"]
ct_info.append(info)
if record["line"] == "境外":
info = {}
info["recordId"] = record["id"]
info["value"] = record["value"]
ab_info.append(info)
if record["line"] == "默认":
info = {}
info["recordId"] = record["id"]
info["value"] = record["value"]
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:
print("CHANGE DNS ERROR: ----Time: " + str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) + "----MESSAGE: " + str(traceback.print_exc()))

Expand Down
Empty file added dns/__init__.py
Empty file.
Binary file removed dns/__pycache__/aliyun.cpython-37.pyc
Binary file not shown.
Binary file removed dns/__pycache__/qCloud.cpython-37.pyc
Binary file not shown.
11 changes: 10 additions & 1 deletion dns/aliyun.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def get_record(self, domain, length, sub_domain, record_type):
request.set_RRKeyWord(sub_domain)
request.set_Type(record_type)
request.set_accept_format(rc_format)
result = clt.do_action(request).decode('utf-8').replace('DomainRecords', 'data', 1).replace('Record', 'records', 1).replace('RecordId', 'id').replace('Value', 'value').replace('Line', 'line').replace('telecom', '电信').replace('unicom', '联通').replace('mobile', '移动')
result = clt.do_action(request).decode('utf-8').replace('DomainRecords', 'data', 1).replace('Record', 'records', 1).replace('RecordId', 'id').replace('Value', 'value').replace('Line', 'line').replace('telecom', '电信').replace('unicom', '联通').replace('mobile', '移动').replace('oversea', '境外').replace('default', '默认')
result = json.JSONDecoder().decode(result)
return result

Expand All @@ -48,6 +48,10 @@ def create_record(self, domain, sub_domain, value, record_type, line, ttl):
line = "unicom"
elif line == "移动":
line = "mobile"
elif line == "境外":
line = "oversea"
elif line == "默认":
line = "default"
request.set_Line(line)
request.set_Type(record_type)
request.set_Value(value)
Expand All @@ -68,6 +72,10 @@ def change_record(self, domain, record_id, sub_domain, value, record_type, line,
line = "unicom"
elif line == "移动":
line = "mobile"
elif line == "境外":
line = "oversea"
elif line == "默认":
line = "default"
request.set_Line(line)
request.set_Type(record_type)
request.set_Value(value)
Expand All @@ -76,3 +84,4 @@ def change_record(self, domain, record_id, sub_domain, value, record_type, line,
result = clt.do_action(request).decode('utf-8')
result = json.JSONDecoder().decode(result)
return result

0 comments on commit 1880ed0

Please sign in to comment.