From ef4e3a57876f4c1a32375c810ecf98ef5643e596 Mon Sep 17 00:00:00 2001 From: Timothy Miller <46549361+timothymiller@users.noreply.github.com> Date: Sat, 30 Jul 2022 21:20:41 -0400 Subject: [PATCH] Added configurable TTL option, plus documentation --- README.md | 1 + cloudflare-ddns.py | 11 ++++++++++- config-example.json | 3 ++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4422f42..e834a69 100755 --- a/README.md +++ b/README.md @@ -78,6 +78,7 @@ Some ISP provided modems only allow port forwarding over IPv4 or IPv6. In this c "zone_id": "The ID of the zone that will get the records. From your dashboard click into the zone. Under the overview tab, scroll down and the zone ID is listed in the right rail", "subdomains": "Array of subdomains you want to update the A & where applicable, AAAA records. IMPORTANT! Only write subdomain name. Do not include the base domain name. (e.g. foo or an empty string to update the base domain)", "proxied": false (defaults to false. Make it true if you want CDN/SSL benefits from cloudflare. This usually disables SSH) +"ttl": "Defaults to 300 seconds. Longer TTLs speed up DNS lookups by increasing the chance of cached results, but a longer TTL also means that updates to your records take longer to go into effect. You can choose a TTL between 30 seconds and 1 day. For more information, see [Cloudflare's TTL documentation](https://developers.cloudflare.com/dns/manage-dns-records/reference/ttl/)", ``` ## 📠 Hosting multiple subdomains on the same IP? diff --git a/cloudflare-ddns.py b/cloudflare-ddns.py index 938811a..f4eecff 100755 --- a/cloudflare-ddns.py +++ b/cloudflare-ddns.py @@ -96,6 +96,7 @@ def getIPs(): def commitRecord(ip): + global ttl for option in config["cloudflare"]: subdomains = option["subdomains"] response = cf_api("zones/" + option['zone_id'], "GET", option) @@ -103,7 +104,6 @@ def commitRecord(ip): time.sleep(5) return base_domain_name = response["result"]["name"] - ttl = 300 # default Cloudflare TTL for subdomain in subdomains: name = subdomain["name"].lower().strip() fqdn = base_domain_name @@ -229,6 +229,15 @@ if __name__ == '__main__': except: purgeUnknownRecords = False print("⚙️ No config detected for 'purgeUnknownRecords' - defaulting to False") + try: + ttl = int(config["ttl"]) + except: + ttl = 300 + print( + "⚙️ No config detected for 'ttl' - defaulting to 300 seconds (5 minutes)") + if ttl < 30: + ttl = 30 # default Cloudflare TTL + print("⚙️ TTL is too low - defaulting to 60 seconds (1 minute)") if(len(sys.argv) > 1): if(sys.argv[1] == "--repeat"): delay = 5*60 diff --git a/config-example.json b/config-example.json index 99d537f..38f1097 100755 --- a/config-example.json +++ b/config-example.json @@ -23,5 +23,6 @@ ], "a": true, "aaaa": true, - "purgeUnknownRecords": false + "purgeUnknownRecords": false, + "ttl": 300 }