Skip to content

Commit

Permalink
更新 DNSPod 部分至 API v3
Browse files Browse the repository at this point in the history
修改部分逻辑,记录全部删除再全部添加,仅测试 PNSPod,未测试其他 SDK 兼容性
为本地运行添加 config.json
使用 requests 代替 urlib3
添加前置库 tencentcloud-sdk-python
使用 logging 库记录 Log
未测试 action是否可用
  • Loading branch information
z0z0r4 committed Jan 3, 2023
1 parent 47520f7 commit 6f26c1e
Show file tree
Hide file tree
Showing 8 changed files with 380 additions and 395 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ cf2dns.log
*.njsproj
*.sln
*.sw?

config.json
cf2dns.log
cf2dns.log
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### 修复腾讯云 DNS 无法调用 --update 2023.1.3
[API 2.0下线通知](https://cloud.tencent.com/document/product/1278/82311)

### 新增支持Actions自选更新V4或V6 ——update 2022.12.19
> 使用方法
Expand Down
Empty file removed cf2dns.log
Empty file.
305 changes: 147 additions & 158 deletions cf2dns.py

Large diffs are not rendered by default.

277 changes: 119 additions & 158 deletions cf2dns_actions.py

Large diffs are not rendered by default.

145 changes: 105 additions & 40 deletions dns/qCloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,126 @@
# -*- coding: utf-8 -*-
# Mail: [email protected]
# Reference: https://cloud.tencent.com/document/product/302/8517
import base64
import hashlib
import hmac
import random
import time
import operator

import json
import urllib.parse
import urllib3
from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.dnspod.v20210323 import dnspod_client, models

class QcloudApi():
class QcloudApiv3():
def __init__(self, SECRETID, SECRETKEY):
self.SecretId = SECRETID
self.secretKey = SECRETKEY
self.cred = credential.Credential(SECRETID, SECRETKEY)

def get(self, module, action, **params):
config = {
'Action': action,
'Nonce': random.randint(10000, 99999),
'SecretId': self.SecretId,
'SignatureMethod': 'HmacSHA256',
'Timestamp': int(time.time()),
def del_record(self, domain: str, record_id: int):
client = dnspod_client.DnspodClient(self.cred, "")
req_model = models.DeleteRecordRequest()
params = {
"Domain": domain,
"RecordId": record_id
}
url_base = '{0}.api.qcloud.com/v2/index.php?'.format(module)
req_model.from_json_string(json.dumps(params))

params_all = dict(config, **params)

params_sorted = sorted(params_all.items(), key=operator.itemgetter(0))
resp = client.DeleteRecord(req_model)
resp = json.loads(resp.to_json_string())
resp["code"] = 0
resp["message"] = "None"
return resp

srcStr = 'GET{0}'.format(url_base) + ''.join("%s=%s&" % (k, v) for k, v in dict(params_sorted).items())[:-1]
signStr = base64.b64encode(hmac.new(bytes(self.secretKey, encoding='utf-8'), bytes(srcStr, encoding='utf-8'), digestmod=hashlib.sha256).digest()).decode('utf-8')
def get_record(self, domain: str, length: int, sub_domain: str, record_type: str):
def format_record(record: dict):
new_record = {}
record["id"] = record['RecordId']
for key in record:
new_record[key.lower()] = record[key]
return new_record
try:
client = dnspod_client.DnspodClient(self.cred, "")

req_model = models.DescribeRecordListRequest()
params = {
"Domain": domain,
"Subdomain": sub_domain,
"RecordType": record_type
}
req_model.from_json_string(json.dumps(params))

config['Signature'] = signStr

params_last = dict(config, **params)
resp = client.DescribeRecordList(req_model)
resp = json.loads(resp.to_json_string())
temp_resp = {}
temp_resp["code"] = 0
temp_resp["data"] = {}
temp_resp["data"]["records"] = []
for record in resp['RecordList']:
temp_resp["data"]["records"].append(format_record(record))
temp_resp["data"]["domain"] = {}
temp_resp["data"]["domain"]["grade"] = self.get_domain(domain)["DomainInfo"]["Grade"] # DP_Free
return temp_resp
except TencentCloudSDKException:
# 构造空响应...
temp_resp = {}
temp_resp["code"] = 0
temp_resp["data"] = {}
temp_resp["data"]["records"] = []
temp_resp["data"]["domain"] = {}
temp_resp["data"]["domain"]["grade"] = self.get_domain(domain)["DomainInfo"]["Grade"] # DP_Free
return temp_resp

params_url = urllib.parse.urlencode(params_last)
def create_record(self, domain: str, sub_domain: str, value: int, record_type: str = "A", line: str = "默认", ttl: int = 600):
client = dnspod_client.DnspodClient(self.cred, "")
req = models.CreateRecordRequest()
params = {
"Domain": domain,
"SubDomain": sub_domain,
"RecordType": record_type,
"RecordLine": line,
"Value": value,
"ttl": ttl
}
req.from_json_string(json.dumps(params))

url = 'https://{0}&'.format(url_base) + params_url
http = urllib3.PoolManager()
r = http.request('GET', url=url, retries=False)
ret = json.loads(r.data.decode('utf-8'))
if ret.get('code', {}) == 0:
return ret
else:
raise Exception(ret)
# 返回的resp是一个CreateRecordResponse的实例,与请求对象对应
resp = client.CreateRecord(req)
resp = json.loads(resp.to_json_string())
resp["code"] = 0
resp["message"] = "None"
return resp

def del_record(self, domain, record):
return self.get(module = 'cns', action = 'RecordDelete', domain = domain, recordId = record)
def change_record(self, domain: str, record_id: int, sub_domain: str, value: str, record_type: str = "A", line: str = "默认", ttl: int = 600):
client = dnspod_client.DnspodClient(self.cred, "")
req = models.ModifyRecordRequest()
params = {
"Domain": domain,
"SubDomain": sub_domain,
"RecordType": record_type,
"RecordLine": line,
"Value": value,
"TTL": ttl,
"RecordId": record_id
}
req.from_json_string(json.dumps(params))

def get_record(self, domain, length, sub_domain, record_type):
return self.get(module = 'cns', action = 'RecordList', domain = domain, length = length, subDomain = sub_domain, recordType = record_type)
# 返回的resp是一个ChangeRecordResponse的实例,与请求对象对应
resp = client.ModifyRecord(req)
resp = json.loads(resp.to_json_string())
resp["code"] = 0
resp["message"] = "None"
return resp

def create_record(self, domain, sub_domain, value, record_type, line, ttl):
return self.get(module = 'cns', action = 'RecordCreate', domain = domain, subDomain = sub_domain, value = value, recordType = record_type, recordLine = line, ttl = ttl)
def get_domain(self, domain: str):
client = dnspod_client.DnspodClient(self.cred, "")

# 实例化一个请求对象,每个接口都会对应一个request对象
req = models.DescribeDomainRequest()
params = {
"Domain": domain
}
req.from_json_string(json.dumps(params))

def change_record(self, domain, record_id, sub_domain, value, record_type, line, ttl):
return self.get(module = 'cns', action = 'RecordModify', domain = domain, recordId =record_id, subDomain = sub_domain, value = value, recordType = record_type, recordLine = line, ttl = ttl)
# 返回的resp是一个DescribeDomainResponse的实例,与请求对象对应
resp = client.DescribeDomain(req)
resp = json.loads(resp.to_json_string())
return resp
38 changes: 0 additions & 38 deletions log.py

This file was deleted.

3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ aliyun-python-sdk-alidns==2.6.19
aliyun-python-sdk-core==2.13.29
huaweicloudsdkcore==3.1.5
huaweicloudsdkdns==3.1.5
urllib3==1.25.10
requests=2.28.1
tencentcloud-sdk-python=3.0.806

0 comments on commit 6f26c1e

Please sign in to comment.