Parcourir la source

华为云支持配置TTL (#424)

华为云支持配置TTL,优先匹配级数最长的主域名
b1n23 il y a 1 an
Parent
commit
28471a3351
2 fichiers modifiés avec 34 ajouts et 17 suppressions
  1. 1 1
      README.md
  2. 33 16
      dns/huaweidns.py

+ 1 - 1
README.md

@@ -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 地址                                                                           |

+ 33 - 16
dns/huaweidns.py

@@ -143,12 +143,19 @@ 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
 
 
@@ -156,7 +163,7 @@ 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 + "_" + \
@@ -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)
@@ -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:
@@ -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: