update
This commit is contained in:
parent
59bffca8f8
commit
593c08a069
@ -15,35 +15,48 @@ class AliyunDNS:
|
||||
|
||||
def params(self, params): # 请求体公共加密
|
||||
params = {
|
||||
'Format': 'JSON',
|
||||
'Version': '2015-01-09',
|
||||
'AccessKeyId': self.ACCESS_KEY_ID,
|
||||
'SignatureMethod': 'HMAC-SHA1',
|
||||
'SignatureVersion': '1.0',
|
||||
'SignatureNonce': str(time.time()),
|
||||
'Timestamp': datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ'),
|
||||
"Format": "JSON",
|
||||
"Version": "2015-01-09",
|
||||
"AccessKeyId": self.ACCESS_KEY_ID,
|
||||
"SignatureMethod": "HMAC-SHA1",
|
||||
"SignatureVersion": "1.0",
|
||||
"SignatureNonce": str(time.time()),
|
||||
"Timestamp": datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
|
||||
**params
|
||||
}
|
||||
# 加密验证签名部分
|
||||
sign = sorted(params.items(), key=lambda x: x[0])
|
||||
sign = '&'.join([urllib.parse.quote_plus(k) + '=' + urllib.parse.quote_plus(v) for k, v in sign])
|
||||
sign = 'GET&%2F&' + urllib.parse.quote_plus(sign)
|
||||
sign = "&".join([urllib.parse.quote_plus(k) + "=" + urllib.parse.quote_plus(v) for k, v in sign])
|
||||
sign = "GET&%2F&" + urllib.parse.quote_plus(sign)
|
||||
sign = base64.b64encode(
|
||||
hmac.new(
|
||||
self.ACCESS_KEY_SECRET.encode('utf-8') + b'&',
|
||||
sign.encode('utf-8'),
|
||||
self.ACCESS_KEY_SECRET.encode("utf-8") + b"&",
|
||||
sign.encode("utf-8"),
|
||||
hashlib.sha1
|
||||
).digest()
|
||||
).decode('utf-8')
|
||||
params['Signature'] = sign
|
||||
).decode("utf-8")
|
||||
params["Signature"] = sign
|
||||
return params
|
||||
|
||||
def DescribeDomainRecords(self, DomainName, PageSize=100): # 查看所有解析记录
|
||||
def DescribeDomains(self, PageNumber=1, PageSize=100): # 查看所有域名
|
||||
response = requests.get(
|
||||
url=self.BASE_URL,
|
||||
params=self.params(params={
|
||||
'Action': 'DescribeDomainRecords',
|
||||
'DomainName': DomainName,
|
||||
"Action": "DescribeDomains",
|
||||
"PageNumber": str(PageNumber),
|
||||
"PageSize": str(PageSize)
|
||||
})
|
||||
)
|
||||
assert response.status_code == 200
|
||||
return response.json()
|
||||
|
||||
def DescribeDomainRecords(self, DomainName, PageNumber=1, PageSize=100): # 查看所有解析记录
|
||||
response = requests.get(
|
||||
url=self.BASE_URL,
|
||||
params=self.params(params={
|
||||
"Action": "DescribeDomainRecords",
|
||||
"DomainName": DomainName,
|
||||
"PageNumber": str(PageNumber),
|
||||
"PageSize": str(PageSize)
|
||||
})
|
||||
)
|
||||
@ -54,9 +67,9 @@ class AliyunDNS:
|
||||
response = requests.get(
|
||||
url=self.BASE_URL,
|
||||
params=self.params(params={
|
||||
'Action': 'AddDomainRecord',
|
||||
'DomainName': DomainName,
|
||||
'RR': RR, 'Type': Type, 'Value': Value, 'Line': Line, 'TTL': str(TTL)
|
||||
"Action": "AddDomainRecord",
|
||||
"DomainName": DomainName,
|
||||
"RR": RR, "Type": Type, "Value": Value, "Line": Line, "TTL": str(TTL)
|
||||
})
|
||||
)
|
||||
assert response.status_code == 200
|
||||
@ -66,10 +79,9 @@ class AliyunDNS:
|
||||
response = requests.get(
|
||||
url=self.BASE_URL,
|
||||
params=self.params(params={
|
||||
'Action': 'DeleteDomainRecord',
|
||||
'RecordId': RecordId,
|
||||
"Action": "DeleteDomainRecord",
|
||||
"RecordId": RecordId,
|
||||
})
|
||||
)
|
||||
assert response.status_code == 200
|
||||
return response.json()
|
||||
|
||||
|
15
server.py
15
server.py
@ -6,7 +6,6 @@ import uvicorn
|
||||
from dns import AliyunDNS
|
||||
|
||||
# Aliyun DNS 相关配置
|
||||
DOMAIN_NAMES = str(os.getenv("DOMAIN_NAMES", "")).split(",")
|
||||
BASE_URL = str(os.getenv("BASE_URL", "http://alidns.aliyuncs.com"))
|
||||
ACCESS_KEY_ID = str(os.getenv("ACCESS_KEY_ID", ""))
|
||||
ACCESS_KEY_SECRET = str(os.getenv("ACCESS_KEY_SECRET", ""))
|
||||
@ -20,12 +19,14 @@ SERVER_PORT = int(os.getenv("SERVER_PORT", "60000"))
|
||||
|
||||
class Server:
|
||||
def __init__(self):
|
||||
self.aliyun_dns = AliyunDNS(BASE_URL, ACCESS_KEY_ID, ACCESS_KEY_SECRET)
|
||||
self.resolutions = {domain_name: list() for domain_name in DOMAIN_NAMES}
|
||||
self.dns = AliyunDNS(BASE_URL, ACCESS_KEY_ID, ACCESS_KEY_SECRET)
|
||||
domains = self.dns.DescribeDomains()
|
||||
domains = [domain["DomainName"] for domain in domains["Domains"]["Domain"]]
|
||||
self.resolutions = {domain: list() for domain in domains}
|
||||
|
||||
def domain(self, domain):
|
||||
self.resolutions[domain].clear()
|
||||
records = self.aliyun_dns.DescribeDomainRecords(DomainName=domain)
|
||||
records = self.dns.DescribeDomainRecords(DomainName=domain)
|
||||
for record in records["DomainRecords"]["Record"]:
|
||||
self.resolutions[domain].append(
|
||||
[record["RR"], record["Value"], record["RecordId"]])
|
||||
@ -33,7 +34,7 @@ class Server:
|
||||
|
||||
def webui(self, app, path):
|
||||
with gradio.Blocks() as webui:
|
||||
domain = gradio.Dropdown(label="主域名", choices=DOMAIN_NAMES, value=None)
|
||||
domain = gradio.Dropdown(label="主域名", choices=list(self.resolutions.keys()), value=None)
|
||||
editor = gradio.DataFrame(label="编辑解析信息", headers=["子域名", "解析地址", "记录编码"],
|
||||
row_count=100, col_count=(3, "fixed"), datatype=["str", "str", "str"], type="pandas")
|
||||
update = gradio.Button(value="更新解析信息")
|
||||
@ -55,11 +56,11 @@ class Server:
|
||||
for rr, ip, id in (now - last):
|
||||
if rr == "" or ip == "": continue # 子域名或解析地址为空则不做解析
|
||||
print(f"创建解析:{domain} {rr} {ip}")
|
||||
self.aliyun_dns.AddDomainRecord(DomainName=domain, RR=rr, Type="A", Value=ip, Line="default", TTL=600)
|
||||
self.dns.AddDomainRecord(DomainName=domain, RR=rr, Type="A", Value=ip, Line="default", TTL=600)
|
||||
|
||||
for rr, ip, id in (last - now):
|
||||
print(f"删除解析:{domain} {rr} {ip} {id}")
|
||||
self.aliyun_dns.DeleteDomainRecord(RecordId=id)
|
||||
self.dns.DeleteDomainRecord(RecordId=id)
|
||||
|
||||
self.domain(domain=domain) # 更新解析信息
|
||||
return self.resolutions[domain]
|
||||
|
Loading…
x
Reference in New Issue
Block a user