From c3878b28cef356bad13fdfc90df42435ca7b4a9a Mon Sep 17 00:00:00 2001 From: James Turland Date: Thu, 5 Sep 2024 12:35:05 +0100 Subject: [PATCH] ddns --- DynamicDNS/config | 20 +++++++++++++++++++ DynamicDNS/docker-compose.yaml | 12 ++++++++++++ DynamicDNS/script.sh | 35 ++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 DynamicDNS/config create mode 100644 DynamicDNS/docker-compose.yaml create mode 100644 DynamicDNS/script.sh diff --git a/DynamicDNS/config b/DynamicDNS/config new file mode 100644 index 0000000..4a2521c --- /dev/null +++ b/DynamicDNS/config @@ -0,0 +1,20 @@ +## WARNING: set deamon at least to 600 seconds if you use checkip or you could +## get banned from their service. +daemon=600 # check every 600 seconds (10mins) +ssl=yes # use ssl-support +use=web # acquire current IP address via web URL + +# Override IP address provider since SSL=yes currently breaks +# the default (non-SSL) provider in my version of ddclient. +# GitHub issue: https://github.com/ddclient/ddclient/issues/597 +web=checkip.dyndns.org/ +web-skip='Current IP Address:' # Probably not needed but doesn't hurt + +## +## Cloudflare +protocol=cloudflare, \ +zone=example.com, \ +ttl=1, \ +login=email, \ +password=YOUR_API_TOKEN \ +example.com,sub1.example.com,sub2.example.com,sub3.example.com \ No newline at end of file diff --git a/DynamicDNS/docker-compose.yaml b/DynamicDNS/docker-compose.yaml new file mode 100644 index 0000000..38cafca --- /dev/null +++ b/DynamicDNS/docker-compose.yaml @@ -0,0 +1,12 @@ +--- +services: + ddclient: + image: lscr.io/linuxserver/ddclient:latest + container_name: ddclient + environment: + - PUID=1000 + - PGID=1000 + - TZ=Etc/UTC + volumes: + - /home/ubuntu/docker/ddns/config:/config + restart: unless-stopped diff --git a/DynamicDNS/script.sh b/DynamicDNS/script.sh new file mode 100644 index 0000000..9e23ba2 --- /dev/null +++ b/DynamicDNS/script.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +# Cloudflare API details +ZONE_ID="your_zone_id" +RECORD_ID="your_record_id" +API_TOKEN="your_cloudflare_api_token" +RECORD_NAME="your_domain.com" + +# Get the current external IP +CURRENT_IP=$(curl -s http://ipv4.icanhazip.com/) + +# Get the IP stored in Cloudflare +CLOUDFLARE_IP=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECORD_ID" \ +-H "Authorization: Bearer $API_TOKEN" \ +-H "Content-Type: application/json" | jq -r '.result.content') + +# Compare the IPs +if [ "$CURRENT_IP" != "$CLOUDFLARE_IP" ]; then + echo "IP has changed from $CLOUDFLARE_IP to $CURRENT_IP. Updating DNS record..." + + # Update the Cloudflare DNS record + UPDATE_RESPONSE=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECORD_ID" \ + -H "Authorization: Bearer $API_TOKEN" \ + -H "Content-Type: application/json" \ + --data '{"type":"A","name":"'"$RECORD_NAME"'","content":"'"$CURRENT_IP"'","ttl":120,"proxied":false}') + + # Check if the update was successful + if echo "$UPDATE_RESPONSE" | jq -r '.success' | grep -q true; then + echo "DNS record updated successfully." + else + echo "Failed to update DNS record. Response from Cloudflare: $UPDATE_RESPONSE" + fi +else + echo "IP has not changed. No update needed." +fi