-
Notifications
You must be signed in to change notification settings - Fork 1
/
slim.py
492 lines (446 loc) · 17.9 KB
/
slim.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# -*- coding: utf-8 -*-
# Copyright (c) 2021 - 2024 qqAys.
# @Time : 2024/05/28 22:01
# @Author : Jinx
# @Email-Private : me@qqays.xyz
# @Github : https://github.com/qqAys/Aliyun-DDNS
# @File : slim.py
# @Description : 阿里云单记录DDNS脚本, slim版本模拟了阿里SDK请求, 无外部Python库依赖。所需环境: 公网IP网络环境、Python3
import binascii
import configparser
import datetime
import hashlib
import hmac
import json
import os
import platform
import re
import smtplib
import socket
import ssl
import sys
import time
import urllib.request
import uuid
from email.header import Header
from email.mime.text import MIMEText
from typing import Any
from urllib.parse import quote
# dns action
UPDATE = "update"
DESCRIBE = "describe"
# msg level
INFO = "INFO"
ERROR = "ERROR"
class Request:
"""阿里SDK请求模型"""
def __init__(self):
self.query = {}
self.protocol = "HTTPS"
self.port = 80
self.method = "POST"
self.headers = {}
self.pathname = "/"
self.body = None
class Utils:
# 不可更改, 阿里验签用
acs_version = "2015-01-09"
tea_version = "0.3.0"
signature_algorithm = "ACS3-HMAC-SHA256"
def printer(self, level, *msg):
print(
f"{self.get_timestamp()} [{level}] {' '.join(self.to_str(m) for m in msg)}"
)
@staticmethod
def get_unix_time() -> int:
"""获取 unix time"""
return int(time.time())
def get_timestamp(self, utc=False) -> str:
"""获取可读时间"""
if utc: # UTC时间为阿里验签用
try:
return datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
except Exception as GetUtcTimeError:
self.printer(INFO, "获取UTC方法错误, 尝试其他方法", GetUtcTimeError)
return datetime.datetime.now(datetime.UTC).strftime(
"%Y-%m-%dT%H:%M:%SZ"
)
else: # 日志用
return time.strftime(
"%Y-%m-%d %H:%M:%S", time.localtime(self.get_unix_time())
)
def get_agent(self) -> str:
"""构造请求 user-agent , 阿里验签用"""
return (
f"AlibabaCloud ({platform.system()}; {platform.machine()}) "
f"Python/{platform.python_version()} Core/{self.tea_version} TeaDSL/1"
)
@staticmethod
def get_nonce() -> str:
"""构造标识符, 阿里验签用"""
name = socket.gethostname() + str(uuid.uuid1())
namespace = uuid.NAMESPACE_URL
return str(uuid.uuid5(namespace, name))
@staticmethod
def hex_encode(raw) -> str:
"""十六进制编码"""
return binascii.b2a_hex(raw).decode("utf-8")
def hash_bytes(self, raw, sign_type) -> bytes:
"""哈希运算, 默认使用 ACS3-HMAC-SHA256"""
if sign_type == "ACS3-HMAC-SHA256":
return hashlib.sha256(raw).digest()
else:
self.printer(ERROR, "不支持的签名类型:", sign_type)
sys.exit()
@staticmethod
def get_canonical_query_string(query) -> str:
"""规范化查询字符串"""
canon_keys = []
for k, v in query.items():
if v is not None:
canon_keys.append(k)
canon_keys.sort()
query_string = ""
for key in canon_keys:
value = quote(query[key], safe="~", encoding="utf-8") # 进行URL编码
if value is None:
s = f"{key}&"
else:
s = f"{key}={value}&"
query_string += s
return query_string[:-1]
def handle_headers(
self, _headers, canonicalized=True
) -> Any:
"""处理请求头"""
canon_keys = []
tmp_headers = {}
for k, v in _headers.items():
if v is not None:
if k.lower() not in canon_keys:
canon_keys.append(k.lower())
tmp_headers[k.lower()] = [self.to_str(v).strip()]
else:
tmp_headers[k.lower()].append(self.to_str(v).strip())
canon_keys.sort()
if canonicalized is False:
return {key: ",".join(sorted(tmp_headers[key])) for key in canon_keys}
else:
canonical_headers = ""
for key in canon_keys:
header_entry = ",".join(sorted(tmp_headers[key]))
s = f"{key}:{header_entry}\n"
canonical_headers += s
return canonical_headers, ";".join(canon_keys)
@staticmethod
def to_str(val) -> Any:
"""转换字符串"""
if val is None:
return val
if isinstance(val, bytes):
return str(val, encoding="utf-8")
else:
return str(val)
def signature_method(self, secret, source, sign_type) -> bytes:
"""加签, 默认使用 ACS3-HMAC-SHA256"""
source = source.encode("utf-8")
secret = secret.encode("utf-8")
if sign_type == "ACS3-HMAC-SHA256":
return hmac.new(secret, source, hashlib.sha256).digest()
else:
self.printer(ERROR, "不支持的签名类型:", sign_type)
sys.exit()
def get_authorization(self, _request, sign_type, payload, ak, secret) -> str:
"""构建授权"""
canonicalized_query = self.get_canonical_query_string(_request.query)
canonicalized_headers, signed_headers = self.handle_headers(
_request.headers, canonicalized=True
)
_request.headers = self.handle_headers(_request.headers, canonicalized=False)
canonical_request = (
f"{_request.method}\n"
f"{_request.pathname}\n"
f"{canonicalized_query}\n"
f"{canonicalized_headers}\n"
f"{signed_headers}\n"
f"{payload}"
)
str_to_sign = f'{sign_type}\n{self.hex_encode(self.hash_bytes(canonical_request.encode("utf-8"), sign_type))}'
signature = self.hex_encode(
self.signature_method(secret, str_to_sign, sign_type)
)
auth = f"{sign_type} Credential={ak},SignedHeaders={signed_headers},Signature={signature}"
return auth
class AliDDNS:
work_dir = os.path.dirname(__file__)
ini_config = configparser.ConfigParser() # 实例化ConfigParser
record_file = os.path.join(work_dir, "aliyun_domain_record.ini") # 解析记录配置
utils = Utils()
def __init__(self):
"""
配置文件初始化
"""
self.args = sys.argv
if len(self.args) <= 1:
self.config_file = os.path.join(self.work_dir, "config.ini")
else:
self.config_file = self.args[1]
if not os.path.exists(self.config_file):
self.utils.printer(ERROR, f"获取不到配置文件:", self.config_file)
sys.exit()
self.ini_config.read(self.config_file, encoding="utf-8")
self.pattern = r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"
try:
self.pub_ip_url = self.ini_config.get("service", "pub_ip_url")
self.end_point = self.ini_config.get("account", "end_point")
self.access_key_id = self.ini_config.get("account", "access_key_id")
self.access_key_secret = self.ini_config.get("account", "access_key_secret")
self.domain_name = self.ini_config.get("domain", "domain_name")
self.rr_key_word = self.ini_config.get("domain", "rr_key_word")
self.type_key_word = self.ini_config.get("domain", "type_key_word")
self.smtp_host = self.ini_config.get("mail", "smtp_host")
self.smtp_port = int(self.ini_config.get("mail", "smtp_port"))
self.smtp_ssl = self.ini_config.get("mail", "smtp_ssl")
self.mail_sender = self.ini_config.get("mail", "sender")
self.mail_user = self.ini_config.get("mail", "user")
self.mail_passwd = self.ini_config.get("mail", "passwd")
self._pre_match_mail = self.ini_config.get("mail", "send_to").split(",")
except configparser.NoOptionError as NoOptionError:
self.utils.printer(ERROR, "获取不到配置项:", NoOptionError)
sys.exit()
except configparser.NoSectionError as NoSectionError:
self.utils.printer(ERROR, "获取不到配置节:", NoSectionError)
sys.exit()
except Exception as GetConfigError:
self.utils.printer(ERROR, "错误的配置项:", GetConfigError)
sys.exit()
self.send_list = []
if not re.match(self.pattern, self.mail_user):
self.utils.printer(ERROR, "错误的邮箱地址:", self.mail_user)
sys.exit()
for mail in self._pre_match_mail:
if not re.match(self.pattern, mail):
self.utils.printer(ERROR, "错误的邮箱地址:", mail)
else:
self.send_list.append(mail)
def _read_record_config(self) -> dict:
"""
读取解析记录配置
:return: dict
"""
if not os.path.exists(self.record_file):
self.utils.printer(INFO, "获取不到配置文件:", self.record_file)
current_record = self._describe_record()
with open(self.record_file, "w") as record_file:
record_file.write(
"[domain_record]\nid={}\nvalue={}\ntime={}".format(
current_record["record_id"],
current_record["record_value"],
self.utils.get_unix_time(),
)
)
try:
self.ini_config.read(self.record_file)
record_id = self.ini_config.get("domain_record", "id")
record_value = self.ini_config.get("domain_record", "value")
write_time = self.ini_config.get("domain_record", "time")
return {
"record_id": record_id,
"record_value": record_value,
"write_time": write_time,
}
except configparser.NoSectionError as NoSectionError:
self.utils.printer(INFO, "获取不到配置节:", NoSectionError)
current_record = self._describe_record()
with open(self.record_file, "w") as record_file:
record_file.write(
"[domain_record]\nid={}\nvalue={}\ntime={}".format(
current_record["record_id"],
current_record["record_value"],
self.utils.get_unix_time(),
)
)
self.ini_config.read(self.record_file)
record_id = self.ini_config.get("domain_record", "id")
record_value = self.ini_config.get("domain_record", "value")
write_time = self.ini_config.get("domain_record", "time")
return {
"record_id": record_id,
"record_value": record_value,
"write_time": write_time,
}
def _update_record_config(self, record_id: str, record_value: str) -> None:
"""
更新解析记录配置
:param record_id: 记录ID
:param record_value: 记录值
"""
update_config = configparser.ConfigParser()
with open(self.record_file, "w") as configfile:
update_config["domain_record"] = {}
update_config["domain_record"]["id"] = record_id
update_config["domain_record"]["value"] = record_value
update_config["domain_record"]["time"] = str(self.utils.get_unix_time())
update_config.write(configfile)
def _get_pubic_ipaddr(self) -> str:
"""
获取公网IPv4地址
:return: str(ip_address)
"""
try:
with urllib.request.urlopen(self.pub_ip_url) as response:
ip = response.read().decode("utf-8").strip()
except Exception as GetPubicIpERROR:
self.utils.printer(ERROR, "获取公网IP失败:", GetPubicIpERROR)
sys.exit()
return ip
def _handle_request(self, action, data=None) -> Any:
utils = self.utils
headers = {
"accept": "application/json",
"host": self.end_point,
"user-agent": utils.get_agent(),
"x-acs-date": utils.get_timestamp(utc=True),
"x-acs-signature-nonce": utils.get_nonce(),
"x-acs-version": utils.acs_version,
}
hashed_request_payload = utils.hex_encode(
utils.hash_bytes(b"", utils.signature_algorithm)
)
headers["x-acs-content-sha256"] = hashed_request_payload
request = Request()
request.headers = headers
if action == UPDATE:
record_id, value = data
url = f"https://{self.end_point}/?RR={self.rr_key_word}&RecordId={record_id}&Type={self.type_key_word}&Value={value}"
headers["x-acs-action"] = "UpdateDomainRecord"
request.query = {
"RR": self.rr_key_word,
"RecordId": record_id,
"Type": self.type_key_word,
"Value": value,
}
elif action == DESCRIBE:
url = f"https://{self.end_point}/?DomainName={self.domain_name}&RRKeyWord={self.rr_key_word}"
request.headers["x-acs-action"] = "DescribeDomainRecords"
request.query = {
"DomainName": self.domain_name,
"RRKeyWord": self.rr_key_word,
}
else:
self.utils.printer(ERROR, "未知的action:", action)
sys.exit()
authorization = utils.get_authorization(
request,
utils.signature_algorithm,
hashed_request_payload,
self.access_key_id,
self.access_key_secret,
)
request.headers["Authorization"] = authorization
req = urllib.request.Request(
url, data=None, headers=request.headers, method="POST"
)
with urllib.request.urlopen(
req, context=ssl._create_unverified_context()
) as response:
response = response.read().decode("utf-8")
response = json.loads(response)
if action == DESCRIBE:
return response["DomainRecords"]
def _describe_record(self) -> Any:
"""
查询解析记录
:return: dict & bool(false)
"""
try:
response = self._handle_request(DESCRIBE)
except Exception as DescribeError:
self.utils.printer(ERROR, "查询域名主机记录失败:", DescribeError)
self._send_mail(
self.send_list,
"[{}][FAIL]DescribeDomainRecord".format(self.domain_name),
"域名 {} 主机记录 {} 的值于 {} 查询失败, 原因:\n\n{}".format(
self.domain_name,
self.rr_key_word,
self.utils.get_timestamp(),
DescribeError,
),
)
return False
record = response["Record"][0]
record_value = record["Value"]
record_id = record["RecordId"]
return {"record_id": record_id, "record_value": record_value}
def _update_record(self, record_id: str, record_value: str) -> bool:
"""
更新解析记录
:param record_id: 记录ID
:param record_value: 记录值
:return: bool
"""
try:
self._handle_request(UPDATE, data=[record_id, record_value])
self._send_mail(
self.send_list,
"[{}][PASS]UpdateDomainRecord".format(self.domain_name),
"域名 {} 主机记录 {} 的值于 {} 变更成功, 新的值为 {}".format(
self.domain_name,
self.rr_key_word,
self.utils.get_timestamp(),
record_value,
),
)
return True
except Exception as UpdateError:
self.utils.printer(ERROR, "更新域名主机记录失败:", UpdateError)
self._send_mail(
self.send_list,
"[{}][FAIL]UpdateDomainRecord".format(self.domain_name),
"域名 {} 主机记录 {} 的值于 {} 变更失败, 原因:\n\n{}".format(
self.domain_name,
self.rr_key_word,
self.utils.get_timestamp(),
UpdateError,
),
)
return False
def _send_mail(self, to_address: list, header: str, msg: str) -> None:
"""
发送邮件
:param to_address: list(mail_list)
:param header: str
:param msg: str
:return: none
"""
smtp = smtplib.SMTP_SSL(self.smtp_host, self.smtp_port)
try:
smtp.login(self.mail_user, self.mail_passwd)
except Exception as SmtpLoginError:
self.utils.printer(ERROR, "SMTP登陆失败:", SmtpLoginError)
return
message = MIMEText(msg, "plain", "utf-8")
message["From"] = Header(self.mail_sender, "utf-8")
message["Subject"] = Header(header, "utf-8")
for addr in to_address:
message["To"] = Header(addr, "utf-8")
smtp.sendmail(self.mail_user, addr, message.as_string())
smtp.quit()
return
def main(self) -> None:
"""
主入口
:return: none
"""
current_config = self._read_record_config()
current_ip = self._get_pubic_ipaddr()
if current_ip != current_config["record_value"]:
update_result = self._update_record(current_config["record_id"], current_ip)
self._update_record_config(current_config["record_id"], current_ip)
if update_result:
current_config = self._read_record_config()
self.utils.printer(INFO, "更新域名主机记录成功:", current_config)
sys.exit()
if __name__ == "__main__":
service = AliDDNS()
service.main()