From 331b11c2f26f04433ffa33c7170c7ad806400307 Mon Sep 17 00:00:00 2001 From: NewFututre Date: Sat, 9 Nov 2019 14:37:25 +0800 Subject: [PATCH] feat(ttl): support ttl and update schema #14 --- dns/alidns.py | 7 +- dns/cloudflare.py | 9 +-- dns/dnscom.py | 9 +-- dns/dnspod.py | 6 +- run.py | 7 +- schema.json | 14 ++++ schema/v2.8.json | 166 ++++++++++++++++++++++++++++++++++++++++++++++ schema/v2.json | 4 +- 8 files changed, 204 insertions(+), 18 deletions(-) create mode 100644 schema/v2.8.json diff --git a/dns/alidns.py b/dns/alidns.py index 930810d..21e23f2 100644 --- a/dns/alidns.py +++ b/dns/alidns.py @@ -31,7 +31,7 @@ class Config: ID = "id" TOKEN = "TOKEN" PROXY = None # 代理设置 - TTL = 600 + TTL = None class API: @@ -71,6 +71,7 @@ def request(param=None, **params): """ if param: params.update(param) + params = dict((k, params[k]) for k in params if params[k] is not None) params = signature(params) info("%s: %s", API.SITE, params) @@ -156,7 +157,7 @@ def update_record(domain, value, record_type='A'): if record["Value"] != value: debug(sub, record) res = request(Action="UpdateDomainRecord", RecordId=rid, - Value=value, RR=sub, Type=record_type) + Value=value, RR=sub, Type=record_type, TTL=Config.TTL) if res: # update records get_records.records[main][rid]["Value"] = value @@ -167,7 +168,7 @@ def update_record(domain, value, record_type='A'): result[rid] = domain else: # https://help.aliyun.com/document_detail/29772.html res = request(Action="AddDomainRecord", DomainName=main, - Value=value, RR=sub, Type=record_type) + Value=value, RR=sub, Type=record_type, TTL=Config.TTL) if res: # update records INFO rid = res.get('RecordId') diff --git a/dns/cloudflare.py b/dns/cloudflare.py index 43752b8..f464575 100644 --- a/dns/cloudflare.py +++ b/dns/cloudflare.py @@ -25,7 +25,7 @@ class Config: ID = "AUTH EMAIL" # CloudFlare 验证的是用户Email,等同于其他平台的userID TOKEN = "API KEY" PROXY = None # 代理设置 - TTL = 600 + TTL = None class API: @@ -39,7 +39,8 @@ def request(method, action, param=None, **params): """ if param: params.update(param) - + + params = dict((k, params[k]) for k in params if params[k] is not None) info("%s/%s : %s", API.SITE, action, params) if Config.PROXY: conn = HTTPSConnection(Config.PROXY) @@ -135,7 +136,7 @@ def update_record(domain, value, record_type="A"): for (rid, record) in records.items(): if record['content'] != value: res = request('PUT', '/' + zoneid + '/dns_records/' + record['id'], - type=record_type, content=value, name=domain) + type=record_type, content=value, name=domain, ttl=Config.TTL) if res: get_records.records[cache_key][rid]['content'] = value result[rid] = res.get("name") @@ -146,7 +147,7 @@ def update_record(domain, value, record_type="A"): else: # create # https://api.cloudflare.com/#dns-records-for-a-zone-create-dns-record res = request('POST', '/' + zoneid + '/dns_records', - type=record_type, name=domain, content=value, proxied=False, ttl=600) + type=record_type, name=domain, content=value, proxied=False, ttl=Config.TTL) if res: get_records.records[cache_key][res['id']] = res result = res diff --git a/dns/dnscom.py b/dns/dnscom.py index dced73c..2fdfc63 100644 --- a/dns/dnscom.py +++ b/dns/dnscom.py @@ -31,7 +31,7 @@ class Config: ID = "id" TOKEN = "TOKEN" PROXY = None # 代理设置 - TTL = 600 + TTL = None class API: @@ -65,6 +65,7 @@ def request(action, param=None, **params): """ if param: params.update(param) + params = dict((k, params[k]) for k in params if params[k] is not None) params = signature(params) info("%s/api/%s/ : params:%s", API.SITE, action, params) @@ -81,7 +82,7 @@ def request(action, param=None, **params): conn.close() if response.status < 200 or response.status >= 300: - warning('%s : error[%d]:%s', action, response.status, result) + warning('%s : error[%d]:%s', action, response.status, result) raise Exception(result) else: data = jsondecode(result) @@ -155,7 +156,7 @@ def update_record(domain, value, record_type='A'): if record["value"] != value: debug(sub, record) res = request("record/modify", domainID=domain_id, - recordID=rid, newvalue=value) + recordID=rid, newvalue=value, newTTL=Config.TTL) if res: # update records get_records.records[main][rid]["value"] = value @@ -166,7 +167,7 @@ def update_record(domain, value, record_type='A'): result[rid] = domain else: res = request("record/create", domainID=domain_id, - value=value, host=sub, type=record_type) + value=value, host=sub, type=record_type, TTL=Config.TTL) if res: # update records INFO rid = res.get('recordID') diff --git a/dns/dnspod.py b/dns/dnspod.py index 12e00b3..03d0e58 100644 --- a/dns/dnspod.py +++ b/dns/dnspod.py @@ -25,7 +25,7 @@ class Config: ID = "token id" TOKEN = "token key" PROXY = None # 代理设置 - TTL = 600 + TTL = None class API: @@ -42,7 +42,7 @@ def request(action, param=None, **params): """ if param: params.update(param) - + params = dict((k, params[k]) for k in params if params[k] is not None) params.update({API.TOKEN_PARAM: '***', 'format': 'json'}) info("%s/%s : %s", API.SITE, action, params) params[API.TOKEN_PARAM] = "%s,%s" % (Config.ID, Config.TOKEN) @@ -161,7 +161,7 @@ def update_record(domain, value, record_type="A"): if record["value"] != value: debug(sub, record) res = request('Record.Modify', record_id=did, record_line=record["line"].replace("Default", "default").encode( - "utf-8"), value=value, sub_domain=sub, domain_id=domainid, record_type=record_type) + "utf-8"), value=value, sub_domain=sub, domain_id=domainid, record_type=record_type, ttl=Config.TTL) if res: get_records.records[domainid][did]["value"] = value result[did] = res.get("record") diff --git a/run.py b/run.py index cfd65e1..181c9eb 100755 --- a/run.py +++ b/run.py @@ -51,7 +51,7 @@ def get_config(key=None, default=None, path="config.json"): error(' Config file `%s` does not exist!' % path) with open(path, 'w') as configfile: configure = { - "$schema": "https://ddns.newfuture.cc/schema/v2.json", + "$schema": "https://ddns.newfuture.cc/schema/v2.8.json", "id": "YOUR ID or EAMIL for DNS Provider", "token": "YOUR TOKEN or KEY for DNS Provider", "dns": "dnspod", @@ -65,6 +65,7 @@ def get_config(key=None, default=None, path="config.json"): ], "index4": "default", "index6": "default", + "ttl": None, "proxy": None, "debug": False, } @@ -159,7 +160,9 @@ def main(): # Dynamicly import the dns module as configuration dns_provider = str(get_config('dns', 'dnspod').lower()) dns = getattr(__import__('dns', fromlist=[dns_provider]), dns_provider) - dns.Config.ID, dns.Config.TOKEN = get_config('id'), get_config('token') + dns.Config.ID = get_config('id') + dns.Config.TOKEN = get_config('token') + dns.Config.TTL = get_config('ttl') if get_config('debug'): ip.DEBUG = get_config('debug') basicConfig( diff --git a/schema.json b/schema.json index 79b1b87..9fb37fa 100644 --- a/schema.json +++ b/schema.json @@ -117,6 +117,20 @@ false ] }, + "ttl": { + "$id": "/properties/ttl", + "type": [ + "number", + "null" + ], + "title": "TTL", + "description": "设置DNS TTL,默认不填读取DNS服务商的配置", + "default": null, + "examples": [ + 600, + null + ] + }, "proxy": { "$id": "/properties/proxy", "type": [ diff --git a/schema/v2.8.json b/schema/v2.8.json new file mode 100644 index 0000000..e7e8ba0 --- /dev/null +++ b/schema/v2.8.json @@ -0,0 +1,166 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema", + "$id": "https://ddns.newfuture.cc/schema/v2.8.json", + "description": "DNS 配置文件 https://github.com/NewFuture/DDNS", + "type": "object", + "properties": { + "$schema": { + "type": "string", + "title": "please use https://ddns.newfuture.cc/schema/v2.8.json", + "description": "请更换为 https://ddns.newfuture.cc/schema/v2.8.json", + "default": "https://ddns.newfuture.cc/schema/v2.8.json", + "enum": [ + "https://ddns.newfuture.cc/schema/v2.8.json", + "http://ddns.newfuture.cc/schema/v2.8.json", + "./schema/v2.8.json" + ] + }, + "id": { + "$id": "/properties/id", + "type": "string", + "title": "ID or Email", + "description": "DNS服务API认证的ID或者邮箱" + }, + "token": { + "$id": "/properties/token", + "type": "string", + "title": "API Token", + "description": "DNS服务商的访问Token或者Key" + }, + "dns": { + "$id": "/properties/dns", + "type": "string", + "title": "DNS Provider", + "description": "dns服务商:阿里为alidns,DNS.COM为dnscom,DNSPOD国际版为(dnspod_com),cloudflare,HE.net为he", + "default": "dnspod", + "examples": [ + "dnspod", + "alidns", + "cloudflare" + ], + "enum": [ + "dnspod", + "alidns", + "cloudflare", + "dnspod_com", + "dnscom", + "he" + ] + }, + "ipv4": { + "$id": "/properties/ipv4", + "title": "IPv4 domain list", + "description": "待更新的IPv4 域名列表", + "type": "array", + "uniqueItems": true, + "items": { + "$id": "/properties/ipv4/items", + "title": "ipv4 domain for DDNS", + "type": "string", + "pattern": "^([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,18}$", + "examples": [ + "newfuture.cc", + "ipv4.example.newfuture.cc" + ] + } + }, + "ipv6": { + "$id": "/properties/ipv6", + "type": "array", + "title": "IPv6 domain list", + "description": "待更新的IPv6 域名列表", + "uniqueItems": true, + "items": { + "$id": "/properties/ipv6/items", + "title": "The ipv6 domain for DDNS", + "type": "string", + "pattern": "^([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}$", + "examples": [ + "newfuture.cc", + "ipv6.example.newfuture.cc" + ] + } + }, + "index4": { + "$id": "/properties/index4", + "type": [ + "string", + "integer", + "boolean" + ], + "title": "IPv4 address Setting", + "description": "本机 IPv4 获取方式设置", + "default": "default", + "examples": [ + "default", + "public", + 0, + 1, + "192\\\\.168\\\\..*", + false + ] + }, + "index6": { + "$id": "/properties/index6", + "type": [ + "string", + "integer", + "boolean" + ], + "title": "IPv6 address Setting", + "description": "本机 IPv6 获取方式设置", + "default": "default", + "examples": [ + "default", + "public", + 0, + 1, + "2404:f801:10:.*", + false + ] + }, + "ttl": { + "$id": "/properties/ttl", + "type": [ + "number", + "null" + ], + "title": "TTL", + "description": "设置DNS TTL,默认不填读取DNS服务商的配置", + "default": null, + "examples": [ + 600, + null + ] + }, + "proxy": { + "$id": "/properties/proxy", + "type": [ + "string", + "null" + ], + "title": "HTTP Proxy Setting", + "description": "DIRECT表示直连,多个代理分号(;)分割逐个尝试直到成功", + "pattern": "^[a-zA-Z0-9\\-;_:\\.]*$", + "examples": [ + "127.0.0.1:1080;DIRECT" + ] + }, + "debug": { + "$id": "/properties/debug", + "type": "boolean", + "title": "Enable Debug Mode", + "description": "是否启用调试模式显示更多信息", + "default": false, + "examples": [ + false, + true + ] + } + }, + "required": [ + "id", + "token" + ], + "additionalProperties": false +} \ No newline at end of file diff --git a/schema/v2.json b/schema/v2.json index 76a7014..644af91 100644 --- a/schema/v2.json +++ b/schema/v2.json @@ -31,10 +31,10 @@ "$id": "/properties/dns", "type": "string", "title": "DNS Provider", - "description": "dns服务商:阿里为alidns,DNS.COM为dnscom,DNSPOD国际版为dnspod_com,cloudflare,HE.net为he", + "description": "dns服务商:阿里为alidns,DNS.COM为dnscom,DNSPOD国际版为dnspod_com,cloudflare", "default": "dnspod", "examples": ["dnspod", "alidns", "cloudflare"], - "enum": ["dnspod", "alidns", "cloudflare", "dnspod_com", "dnscom", "he"] + "enum": ["dnspod", "alidns", "cloudflare", "dnspod_com", "dnscom"] }, "ipv4": { "$id": "/properties/ipv4",