From 84cd39e5ef6070da6f48d71a4b1a05b4191b57ad Mon Sep 17 00:00:00 2001 From: Ammar Hussein Date: Mon, 26 May 2025 19:33:47 -0700 Subject: [PATCH] initial commit --- .gitignore | 1 + main.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 .gitignore create mode 100644 main.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e4e5f6c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*~ \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..ed45ba9 --- /dev/null +++ b/main.py @@ -0,0 +1,42 @@ +import requests +import json +import os + +token = os.environ['cloudflare_api_token'] +zoneId = 'fb89a4a1259891a31e832a1ed0f0654e' +url = f'https://api.cloudflare.com/client/v4/zones/{zoneId}/dns_records' + +def get_public_ip(): + endpoint = 'https://ipinfo.io/json' + response = requests.get(endpoint, verify = True) + if response.status_code != 200: + exit(1) + data = response.json() + return data['ip'] + +ip = get_public_ip() +# Tuples of record name and record ids +records = [ + ('www.resisttechmonopolies.online', '955b7c70317584ca5e0b7a9cb940b30d'), + ('resisttechmonopolies.online', '8256721d2660e1585244ffdce95b148e'), + ('*.resisttechmonopolies.online', 'c0bf845471c9c39a2900a6e76d92afc8') +] +headers = { + 'Content-Type': 'application/json', + 'Authorization': f'Bearer {token}' +} +for record in records: + data = { + 'comment': 'Redirect to Sootie', + 'content': ip, + 'name': record[0], + 'proxied': False, + 'ttl': 1, + 'type': 'A' + } + res = requests.put(f'{url}/{record[1]}', json=data, headers=headers) + if res.status_code == 200: + print(f'Successfully updated {record[0]} to {ip}') + else: + print(f'Failed to update {record[0]} with code {res.status_code}') + print(f'Response Body: {res.content.decode()}')