From 6a059e680692102f782ee3a84b6174068a843929 Mon Sep 17 00:00:00 2001 From: Firq Date: Sun, 27 Nov 2022 18:03:11 +0100 Subject: [PATCH 1/8] Added error handling if key is not in A or AAAA --- cloudflare_script.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/cloudflare_script.py b/cloudflare_script.py index 83a2d9f..ce8c9d3 100644 --- a/cloudflare_script.py +++ b/cloudflare_script.py @@ -31,7 +31,16 @@ def main(): for dnsrecord in dns_records: if ipversion == 'skipping': print(f"DNS Name: {dnsrecord['name']} | ID: {dnsrecord['id']} | Type : {dnsrecord['type']} | IP: {dnsrecord['content']}") - dns_names[dnsrecord['type']].append(dnsrecord["name"]) + + if dnsrecord['type'] in ['A', 'AAAA']: + dns_names[dnsrecord['type']].append(dnsrecord["name"]) + else: + print(f"Cannot resolve record {dnsrecord['name']} because of type {dnsrecord['type']}") + print(f"------ Full DNS record ------\n" + f"{dnsrecord}" + f"\n---------------------------" + ) + with open('cloudflare.json', 'r') as file: values = json.load(file) @@ -55,7 +64,7 @@ def main(): if fullname in dns_names['A']: new_dnsrecords = {"name": fullname, "type": "A", "content": ip_address_new} print(f"Sending request {new_dnsrecords}") - r = cf.zones.dns_records.put(zone_id, dns_record_id, data=new_dnsrecords) + r = cf.zones.dns_records.patch(zone_id, dns_record_id, data=new_dnsrecords) print(r) case '6': @@ -75,7 +84,7 @@ def main(): if fullname in dns_names['AAAA']: new_dnsrecords = {"name": fullname, "type": "AAAA", "content": ip_address_new} print(f"Sending request {new_dnsrecords}") - r = cf.zones.dns_records.put(zone_id, dns_record_id, data=new_dnsrecords) + r = cf.zones.dns_records.patch(zone_id, dns_record_id, data=new_dnsrecords) print(r) case 'skipping': print("Done") From 66a85c320df86ae039dbb14782676b6484258d06 Mon Sep 17 00:00:00 2001 From: Neshura Date: Sun, 27 Nov 2022 18:35:36 +0100 Subject: [PATCH 2/8] Fix due to bad push on branch feature/dockerize Included here to minimize need for merge requests --- set_ip.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/set_ip.sh b/set_ip.sh index 149b3f5..12e4881 100644 --- a/set_ip.sh +++ b/set_ip.sh @@ -15,6 +15,7 @@ else else ipv4=$(curl -k -s https://am.i.mullvad.net/ip) ipv6=$(curl -k -s https://ipv6.am.i.mullvad.net/) + source venv/bin/activate python3 ./cloudflare_script.py 4 "$ipv4" python3 ./cloudflare_script.py 6 "$ipv6" fi From aaa3da5cbb06ccf08b0202fa6e308414abb10ce3 Mon Sep 17 00:00:00 2001 From: Neshura Date: Sun, 27 Nov 2022 18:39:34 +0100 Subject: [PATCH 3/8] Added ipv4 specification to bash script --- set_ip.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/set_ip.sh b/set_ip.sh index 12e4881..155e0a6 100644 --- a/set_ip.sh +++ b/set_ip.sh @@ -13,7 +13,7 @@ else if [ ! -e cloudflare.json ] || [ ! -e config.ini ]; then echo "Cloudflare config not found, is the script run in the correct directory?" else - ipv4=$(curl -k -s https://am.i.mullvad.net/ip) + ipv4=$(curl -k -s https://am.i.mullvad.net/ip -4) ipv6=$(curl -k -s https://ipv6.am.i.mullvad.net/) source venv/bin/activate python3 ./cloudflare_script.py 4 "$ipv4" From 536a9ceaa14cd06e32bbe505b2eec49af537f317 Mon Sep 17 00:00:00 2001 From: Neshura Date: Sun, 27 Nov 2022 18:42:46 +0100 Subject: [PATCH 4/8] Revert change from put to patch for now Syntax between put and path seems to be somewhat different, change was causing the script to fail --- cloudflare_script.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cloudflare_script.py b/cloudflare_script.py index ce8c9d3..1085a18 100644 --- a/cloudflare_script.py +++ b/cloudflare_script.py @@ -64,7 +64,7 @@ def main(): if fullname in dns_names['A']: new_dnsrecords = {"name": fullname, "type": "A", "content": ip_address_new} print(f"Sending request {new_dnsrecords}") - r = cf.zones.dns_records.patch(zone_id, dns_record_id, data=new_dnsrecords) + r = cf.zones.dns_records.put(zone_id, dns_record_id, data=new_dnsrecords) print(r) case '6': @@ -84,7 +84,7 @@ def main(): if fullname in dns_names['AAAA']: new_dnsrecords = {"name": fullname, "type": "AAAA", "content": ip_address_new} print(f"Sending request {new_dnsrecords}") - r = cf.zones.dns_records.patch(zone_id, dns_record_id, data=new_dnsrecords) + r = cf.zones.dns_records.put(zone_id, dns_record_id, data=new_dnsrecords) print(r) case 'skipping': print("Done") From 56e144005b6b9569fd7d168dfda06bc3f811661c Mon Sep 17 00:00:00 2001 From: Neshura Date: Sun, 27 Nov 2022 18:42:46 +0100 Subject: [PATCH 5/8] Revert "Revert change from put to patch for now" This reverts commit 536a9ceaa14cd06e32bbe505b2eec49af537f317. --- cloudflare_script.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cloudflare_script.py b/cloudflare_script.py index 1085a18..ce8c9d3 100644 --- a/cloudflare_script.py +++ b/cloudflare_script.py @@ -64,7 +64,7 @@ def main(): if fullname in dns_names['A']: new_dnsrecords = {"name": fullname, "type": "A", "content": ip_address_new} print(f"Sending request {new_dnsrecords}") - r = cf.zones.dns_records.put(zone_id, dns_record_id, data=new_dnsrecords) + r = cf.zones.dns_records.patch(zone_id, dns_record_id, data=new_dnsrecords) print(r) case '6': @@ -84,7 +84,7 @@ def main(): if fullname in dns_names['AAAA']: new_dnsrecords = {"name": fullname, "type": "AAAA", "content": ip_address_new} print(f"Sending request {new_dnsrecords}") - r = cf.zones.dns_records.put(zone_id, dns_record_id, data=new_dnsrecords) + r = cf.zones.dns_records.patch(zone_id, dns_record_id, data=new_dnsrecords) print(r) case 'skipping': print("Done") From 61f952f04419f0c8cb59b47683a5964c951fa72a Mon Sep 17 00:00:00 2001 From: Neshura Date: Sun, 27 Nov 2022 18:47:47 +0100 Subject: [PATCH 6/8] Added venv to gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8f38060 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ + +venv/ From 439716ed67756547999862e641632e89751e1ddf Mon Sep 17 00:00:00 2001 From: Neshura Date: Sun, 27 Nov 2022 18:48:38 +0100 Subject: [PATCH 7/8] Try Catch around the ip put New Bug found, uggly patched with this, details in #3 --- cloudflare_script.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/cloudflare_script.py b/cloudflare_script.py index ce8c9d3..bc35ff7 100644 --- a/cloudflare_script.py +++ b/cloudflare_script.py @@ -64,8 +64,11 @@ def main(): if fullname in dns_names['A']: new_dnsrecords = {"name": fullname, "type": "A", "content": ip_address_new} print(f"Sending request {new_dnsrecords}") - r = cf.zones.dns_records.patch(zone_id, dns_record_id, data=new_dnsrecords) - print(r) + try; + r = cf.zones.dns_records.patch(zone_id, dns_record_id, data=new_dnsrecords) + print(r) + except: + print("Error pushing entry") case '6': print("Updating IPv6") @@ -84,8 +87,11 @@ def main(): if fullname in dns_names['AAAA']: new_dnsrecords = {"name": fullname, "type": "AAAA", "content": ip_address_new} print(f"Sending request {new_dnsrecords}") - r = cf.zones.dns_records.patch(zone_id, dns_record_id, data=new_dnsrecords) - print(r) + try: + r = cf.zones.dns_records.patch(zone_id, dns_record_id, data=new_dnsrecords) + print(r) + except: + print("Error pushing entry") case 'skipping': print("Done") case other: From 2b0dae763b8acedcffc90a963cc70e690e091b09 Mon Sep 17 00:00:00 2001 From: Neshura Date: Sun, 27 Nov 2022 18:50:01 +0100 Subject: [PATCH 8/8] Uncaught syntax error --- cloudflare_script.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloudflare_script.py b/cloudflare_script.py index bc35ff7..2b7a836 100644 --- a/cloudflare_script.py +++ b/cloudflare_script.py @@ -64,7 +64,7 @@ def main(): if fullname in dns_names['A']: new_dnsrecords = {"name": fullname, "type": "A", "content": ip_address_new} print(f"Sending request {new_dnsrecords}") - try; + try: r = cf.zones.dns_records.patch(zone_id, dns_record_id, data=new_dnsrecords) print(r) except: