Skip to content

Commit

Permalink
华为云支持配置TTL (#424)
Browse files Browse the repository at this point in the history
华为云支持配置TTL,优先匹配级数最长的主域名
  • Loading branch information
b1n23 authored Feb 26, 2024
1 parent 863c8f0 commit 28471a3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ python run.py -c /path/to/config.json

| key | type | required | default | description | tips |
| :----: | :----------------: | :------: | :---------: | :---------------: | ----------------------------------------------------------------------------------------------------------- |
| id | string ||| api 访问 ID | Cloudflare 为邮箱(使用 Token 时留空)<br>HE.net 可留空 |
| id | string ||| api 访问 ID | Cloudflare 为邮箱(使用 Token 时留空)<br>HE.net 可留空<br>华为云为 Access Key ID (AK) |
| token | string ||| api 授权 token | 部分平台叫 secret key , **反馈粘贴时删除** |
| 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` |
| ipv4 | array | No | `[]` | ipv4 域名列表 |`[]`时,不会获取和更新 IPv4 地址 |
Expand Down
49 changes: 33 additions & 16 deletions dns/huaweidns.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,27 @@ def request(method, path, param=None, body=None, **params):

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


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


def update_record(domain, value, record_type='A', name=None):
def update_record(domain, value, record_type='A'):
"""
更新记录
update
https://support.huaweicloud.com/api-dns/dns_api_64006.html
https://support.huaweicloud.com/api-dns/UpdateRecordSet.html
add
https://support.huaweicloud.com/api-dns/zh-cn_topic_0037134404.html
https://support.huaweicloud.com/api-dns/dns_api_64001.html
"""
info(">>>>>%s(%s)", domain, record_type)
zoneid = get_zone_id(domain)
Expand All @@ -207,20 +214,27 @@ def update_record(domain, value, record_type='A', name=None):
for (rid, record) in records.items():
if record['records'] != value:
"""
PUT https://{endpoint}/v2/zones/{zone_id}/recordsets/{recordset_id}
{
"description": "This is an example record set.",
"ttl": 3600,
"records": [
"192.168.10.1",
"192.168.10.2"
]
"name" : "www.example.com.",
"description" : "This is an example record set.",
"type" : "A",
"ttl" : 3600,
"records" : [ "192.168.10.1", "192.168.10.2" ]
}
"""
body = {
"name": domain,
"description": "Managed by DDNS.",
"type": record_type,
"records": [
value
]
}
# 如果TTL不为空,则添加到字典中
if Config.TTL is not None:
body['ttl'] = Config.TTL
res = request('PUT', '/v2/zones/' + zoneid + '/recordsets/' + record['id'],
body=str(jsonencode(body)))
if res:
Expand All @@ -231,14 +245,17 @@ def update_record(domain, value, record_type='A', name=None):
else:
result[rid] = domain
else: # create
print(domain)
body = {
"name": domain,
"description": "Managed by DDNS.",
"type": record_type,
"records": [
value
]
}
# 如果TTL不为空,则添加到字典中
if Config.TTL is not None:
body['ttl'] = Config.TTL
res = request('POST', '/v2/zones/' + zoneid + '/recordsets',
body=str(jsonencode(body)))
if res:
Expand Down

0 comments on commit 28471a3

Please sign in to comment.