Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
danieloppenlander committed Jan 15, 2024
1 parent 160b009 commit 2d8f888
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM python:3.12
RUN apt-get update && apt -y install cron python3 pip
RUN pip install requests
WORKDIR /app
COPY crontab /etc/cron.d/crontab
COPY update-cloudflare.py ./update-cloudflare.py
RUN chmod 0644 /etc/cron.d/crontab
RUN /usr/bin/crontab /etc/cron.d/crontab
CMD ["cron", "-f"]
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Usage

2 changes: 2 additions & 0 deletions crontab
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Run update-cloudflare.py every 5 minutes
*/10 * * * * python /app/update-cloudflare.py > /proc/1/fd/1 2>/proc/1/fd/2
96 changes: 96 additions & 0 deletions update-cloudflare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/python

import requests
import os

def main():

# Load environment variables
ZONE_NAME = os.getenv("ZONE_NAME")
NAME = os.getenv("NAME")
IS_PROXIED = os.getenv("IS_PROXIED").lower() == 'true'
EMAIL = os.getenv("EMAIL")
API_KEY = os.getenv("API_KEY")

auth_headers = {
'X-Auth-Email': EMAIL,
'X-Auth-Key': API_KEY,
}

# Get my IP
public_ip = get_public_ip()

# Get zone ID
zone = get_first_zone_by_name(ZONE_NAME, auth_headers)

# Get record ID
record = get_first_record_by_name(zone['id'], NAME, auth_headers)

# Check if IP has changed
if record['content'] == public_ip:
print('IP has not changed.')
return

# Create new record
new_record = {
'content': public_ip,
'name': NAME,
'proxied': IS_PROXIED,
'type': 'A',
'comment': 'Updated automatically.',
}

# Update record
put_record(zone['id'], record['id'], new_record, auth_headers)

# Get public IP
def get_public_ip():
public_ip_response = requests.get('https://checkip.amazonaws.com')
if public_ip_response.status_code != 200:
raise Exception(f'Could not get public IP: {public_ip_response.text}')

return public_ip_response.text.strip()

# Get zone from a zone name
def get_first_zone_by_name(zone_name, headers):
zone_id_response = requests.get(
f'https://api.cloudflare.com/client/v4/zones?name={zone_name}&status=active',
headers=headers
)
if zone_id_response.status_code != 200:
raise Exception(f'Could not get zone ID: {zone_id_response.json()}')
if len(zone_id_response.json()['result']) == 0:
raise Exception(f'Zone not found: {zone_name}')
return False

return zone_id_response.json()['result'][0]

# Get record from a zone ID and record name
def get_first_record_by_name(zone_id, name, headers):
record_id_response = requests.get(
f'https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records?type=A&name={name}',
headers=headers
)
if record_id_response.status_code != 200:
print(f'Could not get record ID: {record_id_response.json()}')
return False
if len(record_id_response.json()['result']) == 0:
print(f'Record not found: {name}')
return False

return record_id_response.json()['result'][0]

# Udates the record with the new one specified
def put_record(zone_id, record_id, record, headers):
update_response = requests.put(
f'https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records/{record_id}',
headers=headers,
json=record
)
if update_response.status_code != 200:
print(f'Could not update record: {update_response.json()}')
return False


if __name__ == "__main__":
main()

0 comments on commit 2d8f888

Please sign in to comment.