-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
修改部分逻辑,记录全部删除再全部添加,仅测试 PNSPod,未测试其他 SDK 兼容性 为本地运行添加 config.json 使用 requests 代替 urlib3 添加前置库 tencentcloud-sdk-python 使用 logging 库记录 Log 未测试 action是否可用
- Loading branch information
Showing
8 changed files
with
380 additions
and
395 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,7 @@ cf2dns.log | |
*.njsproj | ||
*.sln | ||
*.sw? | ||
|
||
config.json | ||
cf2dns.log | ||
cf2dns.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters