From 2ad7e57d652f14ed95f5174c5ca621f3b9030aa6 Mon Sep 17 00:00:00 2001 From: Timothy Miller <46549361+timothymiller@users.noreply.github.com> Date: Wed, 15 Feb 2023 14:07:17 -0500 Subject: [PATCH] Added support for secondary IP checks if primary fails (Fixes #111) Updated requests module version --- cloudflare-ddns.py | 48 ++++++++++++++++++++++++++++++++++------------ requirements.txt | 2 +- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/cloudflare-ddns.py b/cloudflare-ddns.py index a99238b..0cb3dd6 100755 --- a/cloudflare-ddns.py +++ b/cloudflare-ddns.py @@ -66,9 +66,20 @@ def getIPs(): global shown_ipv4_warning if not shown_ipv4_warning: shown_ipv4_warning = True - print("🧩 IPv4 not detected") - if purgeUnknownRecords: - deleteEntries("A") + print("🧩 IPv4 not detected via 1.1.1.1, trying 1.0.0.1") + # Try secondary IP check + try: + a = requests.get( + "https://1.0.0.1/cdn-cgi/trace").text.split("\n") + a.pop() + a = dict(s.split("=") for s in a)["ip"] + except Exception: + global shown_ipv4_warning_secondary + if not shown_ipv4_warning_secondary: + shown_ipv4_warning_secondary = True + print("🧩 IPv4 not detected via 1.0.0.1. Verify your ISP or DNS provider isn't blocking Cloudflare's IPs.") + if purgeUnknownRecords: + deleteEntries("A") if ipv6_enabled: try: aaaa = requests.get( @@ -79,16 +90,26 @@ def getIPs(): global shown_ipv6_warning if not shown_ipv6_warning: shown_ipv6_warning = True - print("🧩 IPv6 not detected") - if purgeUnknownRecords: - deleteEntries("AAAA") + print("🧩 IPv6 not detected via 1.1.1.1, trying 1.0.0.1") + try: + aaaa = requests.get( + "https://[2606:4700:4700::1001]/cdn-cgi/trace").text.split("\n") + aaaa.pop() + aaaa = dict(s.split("=") for s in aaaa)["ip"] + except Exception: + global shown_ipv6_warning_secondary + if not shown_ipv6_warning_secondary: + shown_ipv6_warning_secondary = True + print("🧩 IPv6 not detected via 1.0.0.1. Verify your ISP or DNS provider isn't blocking Cloudflare's IPs.") + if purgeUnknownRecords: + deleteEntries("AAAA") ips = {} - if(a is not None): + if (a is not None): ips["ipv4"] = { "type": "A", "ip": a } - if(aaaa is not None): + if (aaaa is not None): ips["ipv6"] = { "type": "AAAA", "ip": aaaa @@ -177,7 +198,7 @@ def cf_api(endpoint, method, config, headers={}, data=False): "X-Auth-Key": config['authentication']['api_key']['api_key'], } try: - if(data == False): + if (data == False): response = requests.request( method, "https://api.cloudflare.com/client/v4/" + endpoint, headers=headers) else: @@ -205,7 +226,9 @@ def updateIPs(ips): if __name__ == '__main__': shown_ipv4_warning = False + shown_ipv4_warning_secondary = False shown_ipv6_warning = False + shown_ipv6_warning_secondary = False ipv4_enabled = True ipv6_enabled = True purgeUnknownRecords = False @@ -244,13 +267,14 @@ if __name__ == '__main__': if ttl < 30: ttl = 30 # print("⚙️ TTL is too low - defaulting to 30 seconds") - if(len(sys.argv) > 1): - if(sys.argv[1] == "--repeat"): + if (len(sys.argv) > 1): + if (sys.argv[1] == "--repeat"): if ipv4_enabled and ipv6_enabled: print( "🕰️ Updating IPv4 (A) & IPv6 (AAAA) records every " + str(ttl) + " seconds") elif ipv4_enabled and not ipv6_enabled: - print("🕰️ Updating IPv4 (A) records every " + str(ttl) + " seconds") + print("🕰️ Updating IPv4 (A) records every " + + str(ttl) + " seconds") elif ipv6_enabled and not ipv4_enabled: print("🕰️ Updating IPv6 (AAAA) records every " + str(ttl) + " seconds") diff --git a/requirements.txt b/requirements.txt index 5e77405..bf0d9d4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -requests==2.28.1 \ No newline at end of file +requests==2.28.2 \ No newline at end of file