Skip to content

Commit 28471a3

Browse files
authored
华为云支持配置TTL (#424)
华为云支持配置TTL,优先匹配级数最长的主域名
1 parent 863c8f0 commit 28471a3

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ python run.py -c /path/to/config.json
121121

122122
| key | type | required | default | description | tips |
123123
| :----: | :----------------: | :------: | :---------: | :---------------: | ----------------------------------------------------------------------------------------------------------- |
124-
| id | string ||| api 访问 ID | Cloudflare 为邮箱(使用 Token 时留空)<br>HE.net 可留空 |
124+
| id | string ||| api 访问 ID | Cloudflare 为邮箱(使用 Token 时留空)<br>HE.net 可留空<br>华为云为 Access Key ID (AK) |
125125
| token | string ||| api 授权 token | 部分平台叫 secret key , **反馈粘贴时删除** |
126126
| dns | string | No | `"dnspod"` | dns 服务商 | 阿里 DNS 为`alidns`,<br>Cloudflare 为 `cloudflare`,<br>dns.com 为 `dnscom`,<br>DNSPOD 国内为 `dnspod`,<br>DNSPOD 国际版为 `dnspod_com`,<br>HE.net 为`he`,<br>华为 DNS 为`huaweidns`,<br>自定义回调为`callback` |
127127
| ipv4 | array | No | `[]` | ipv4 域名列表 |`[]`时,不会获取和更新 IPv4 地址 |

dns/huaweidns.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -143,20 +143,27 @@ def request(method, path, param=None, body=None, **params):
143143

144144
def get_zone_id(domain):
145145
"""
146-
切割域名获取主域名和对应ID https://support.huaweicloud.com/api-dns/zh-cn_topic_0037134402.html
146+
切割域名获取主域名和对应ID https://support.huaweicloud.com/api-dns/dns_api_62003.html
147+
优先匹配级数最长的主域名
147148
"""
148-
zones = request('GET', '/v2/zones', limit=500)['zones']
149-
domain += '.'
150-
zone = next((z for z in zones if domain.endswith(z.get('name'))), None)
151-
zoneid = zone and zone['id']
149+
zoneid = None
150+
domain_slice = domain.split('.')
151+
index = len(domain_slice)
152+
root_domain = '.'.join(domain_slice[-2:])
153+
zones = request('GET', '/v2/zones', limit=500, name=root_domain)['zones']
154+
while (not zoneid) and (index >= 2):
155+
domain = '.'.join(domain_slice[-index:]) + '.'
156+
zone = next((z for z in zones if domain == (z.get('name'))), None)
157+
zoneid = zone and zone['id']
158+
index -= 1
152159
return zoneid
153160

154161

155162
def get_records(zoneid, **conditions):
156163
"""
157164
获取记录ID
158165
返回满足条件的所有记录[]
159-
https://support.huaweicloud.com/api-dns/zh-cn_topic_0037129970.html
166+
https://support.huaweicloud.com/api-dns/dns_api_64004.html
160167
TODO 大于500翻页
161168
"""
162169
cache_key = zoneid + "_" + \
@@ -187,13 +194,13 @@ def get_records(zoneid, **conditions):
187194
return records
188195

189196

190-
def update_record(domain, value, record_type='A', name=None):
197+
def update_record(domain, value, record_type='A'):
191198
"""
192199
更新记录
193200
update
194-
https://support.huaweicloud.com/api-dns/dns_api_64006.html
201+
https://support.huaweicloud.com/api-dns/UpdateRecordSet.html
195202
add
196-
https://support.huaweicloud.com/api-dns/zh-cn_topic_0037134404.html
203+
https://support.huaweicloud.com/api-dns/dns_api_64001.html
197204
"""
198205
info(">>>>>%s(%s)", domain, record_type)
199206
zoneid = get_zone_id(domain)
@@ -207,20 +214,27 @@ def update_record(domain, value, record_type='A', name=None):
207214
for (rid, record) in records.items():
208215
if record['records'] != value:
209216
"""
217+
PUT https://{endpoint}/v2/zones/{zone_id}/recordsets/{recordset_id}
218+
210219
{
211-
"description": "This is an example record set.",
212-
"ttl": 3600,
213-
"records": [
214-
"192.168.10.1",
215-
"192.168.10.2"
216-
]
220+
"name" : "www.example.com.",
221+
"description" : "This is an example record set.",
222+
"type" : "A",
223+
"ttl" : 3600,
224+
"records" : [ "192.168.10.1", "192.168.10.2" ]
217225
}
218226
"""
219227
body = {
228+
"name": domain,
229+
"description": "Managed by DDNS.",
230+
"type": record_type,
220231
"records": [
221232
value
222233
]
223234
}
235+
# 如果TTL不为空,则添加到字典中
236+
if Config.TTL is not None:
237+
body['ttl'] = Config.TTL
224238
res = request('PUT', '/v2/zones/' + zoneid + '/recordsets/' + record['id'],
225239
body=str(jsonencode(body)))
226240
if res:
@@ -231,14 +245,17 @@ def update_record(domain, value, record_type='A', name=None):
231245
else:
232246
result[rid] = domain
233247
else: # create
234-
print(domain)
235248
body = {
236249
"name": domain,
250+
"description": "Managed by DDNS.",
237251
"type": record_type,
238252
"records": [
239253
value
240254
]
241255
}
256+
# 如果TTL不为空,则添加到字典中
257+
if Config.TTL is not None:
258+
body['ttl'] = Config.TTL
242259
res = request('POST', '/v2/zones/' + zoneid + '/recordsets',
243260
body=str(jsonencode(body)))
244261
if res:

0 commit comments

Comments
 (0)