代码拉取完成,页面将自动刷新
import configparser
import sys
import requests
import urllib.parse
import hmac
import base64
import hashlib
import time
import uuid
from datetime import datetime
import tkinter as tk
from tkinter import scrolledtext
# 尝试从配置文件中读取配置
config = configparser.ConfigParser()
config.read('config.ini')
try:
access_key_id_default = config.get('aliyun', 'access_key_id')
access_key_secret_default = config.get('aliyun', 'access_key_secret')
domain_default = config.get('aliyun', 'domain')
subdomain_default = config.get('aliyun', 'subdomain')
except:
access_key_id_default = ""
access_key_secret_default = ""
domain_default = ""
subdomain_default = ""
# 创建主窗口并设置窗口大小和标题
root = tk.Tk()
root.geometry('400x350')
root.title('阿里云DDNS')
# 创建输入框和对应的标签,如果配置文件存在,则预填充输入框
access_key_id_label = tk.Label(root, text="Access Key ID:")
access_key_id_label.pack()
access_key_id_entry = tk.Entry(root, width=50)
access_key_id_entry.insert(0, access_key_id_default)
access_key_id_entry.pack()
access_key_secret_label = tk.Label(root, text="Access Key Secret:")
access_key_secret_label.pack()
access_key_secret_entry = tk.Entry(root, width=50)
access_key_secret_entry.insert(0, access_key_secret_default)
access_key_secret_entry.pack()
domain_label = tk.Label(root, text="Domain:")
domain_label.pack()
domain_entry = tk.Entry(root, width=50)
domain_entry.insert(0, domain_default)
domain_entry.pack()
subdomain_label = tk.Label(root, text="Subdomain:")
subdomain_label.pack()
subdomain_entry = tk.Entry(root, width=50)
subdomain_entry.insert(0, subdomain_default)
subdomain_entry.pack()
# 创建一个滚动文本框来显示输出
output_text = scrolledtext.ScrolledText(root, height=10, width=50)
output_text.pack()
# 获取当前主机的公网IP
def get_current_ip():
try:
response = requests.get('http://ipinfo.io/json')
return response.json()['ip']
except Exception as e:
output_text.insert(tk.END, f'获取当前IP失败: {str(e)}\n')
return None
# 生成签名
def compute_signature(access_key_secret, parameters):
sorted_parameters = sorted(parameters.items(), key=lambda item:item[0])
canonicalized_query_string = urllib.parse.urlencode(sorted_parameters)
string_to_sign = 'GET&%2F&' + urllib.parse.quote(canonicalized_query_string, safe='~')
h = hmac.new((access_key_secret + "&").encode('utf-8'), string_to_sign.encode('utf-8'), hashlib.sha1)
signature = base64.encodebytes(h.digest()).strip()
return signature.decode('utf-8')
# 发送请求
def send_request(parameters):
common_parameters = {
'Format': 'JSON',
'Version': '2015-01-09',
'AccessKeyId': access_key_id_entry.get(),
'SignatureMethod': 'HMAC-SHA1',
'Timestamp': time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
'SignatureVersion': '1.0',
'SignatureNonce': str(uuid.uuid1()),
}
parameters.update(common_parameters)
parameters['Signature'] = compute_signature(access_key_secret_entry.get(), parameters)
url = 'https://alidns.aliyuncs.com/?' + urllib.parse.urlencode(parameters)
try:
response = requests.get(url)
return response.json()
except Exception as e:
output_text.insert(tk.END, f'发送请求失败: {str(e)}\n')
return None
# 获取DNS记录的当前值
def get_record_value():
parameters = {
'Action': 'DescribeSubDomainRecords',
'SubDomain': f"{subdomain_entry.get()}.{domain_entry.get()}",
}
records = send_request(parameters)
if records and records['TotalCount'] != 0:
for record in records['DomainRecords']['Record']:
if record['Type'] == 'A': # 检查记录类型是否为'A'
output_text.insert(tk.END, f"找到A记录,RecordId:{record['RecordId']},值:{record['Value']}\n")
return record['Value'], record['RecordId']
return None, None
# 更新DNS记录
def update_dns(record_id, ip):
parameters = {
'Action': 'UpdateDomainRecord',
'RecordId': record_id,
'RR': subdomain_entry.get(),
'Type': 'A',
'Value': ip,
}
response = send_request(parameters)
if response:
output_text.insert(tk.END, f'DNS记录已更新为 {ip}\n')
return response
# 创建一个按钮,当点击这个按钮时会调用 update_dns_records 函数
def start_process():
current_ip = get_current_ip()
if current_ip:
output_text.insert(tk.END, f'当前IP为 {current_ip}\n')
record_value, record_id = get_record_value()
if record_value is None or record_id is None:
output_text.insert(tk.END, '未找到当前DNS记录\n')
else:
output_text.insert(tk.END, f'当前DNS记录为 {record_value}\n')
if record_value != current_ip:
response = update_dns(record_id, current_ip)
if response and response.get('RecordId') == record_id:
output_text.insert(tk.END, 'DNS记录更新成功\n')
else:
output_text.insert(tk.END, '无需更新DNS记录\n')
update_button = tk.Button(root, text="更新DNS记录", command=start_process)
update_button.pack()
# 启动主循环
root.mainloop()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。