Add IPv6 to ddns

This commit is contained in:
2026-07-08 19:27:55 +01:00
parent 315274e323
commit 737c5c8d8a
+38 -10
View File
@@ -2,22 +2,26 @@
source /run/secrets.env source /run/secrets.env
cache=/var/lib/cloudflare-ddns/old_ip # Separate caches for IPv4 and IPv6
IP=$(@curl@/bin/curl -s 'https://api.ipify.org') cache_v4=/var/lib/cloudflare-ddns/old_ip_v4
cache_v6=/var/lib/cloudflare-ddns/old_ip_v6
# Fetch current IPs
IP_V4=$(@curl@/bin/curl -4 -s 'https://api.ipify.org')
IP_V6=$(@curl@/bin/curl -6 -s 'https://api6.ipify.org')
arr=("" "git." "www." "acs.") arr=("" "git." "www." "acs.")
if [[ -f "$cache" ]]; then if [[ -n "$IP_V4" ]]; then
old_ip=$(cat "$cache") old_ip_v4=$([[ -f "$cache_v4" ]] && cat "$cache_v4" || echo "")
else
old_ip=""
fi
if [[ "$IP" != "$old_ip" ]]; then if [[ "$IP_V4" != "$old_ip_v4" ]]; then
for sub in "${arr[@]}"; do for sub in "${arr[@]}"; do
RECORD_ID=$(@curl@/bin/curl -s -X GET \ RECORD_ID=$(@curl@/bin/curl -s -X GET \
"https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records" \ "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records" \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
| @jq@/bin/jq -r ".result[] | select(.name==\"${sub}syedm.dev\" and .type==\"A\") | .id") | @jq@/bin/jq -r ".result[] | select(.name==\"${sub}syedm.dev\" and .type==\"A\") | .id")
if [ -n "$RECORD_ID" ]; then if [ -n "$RECORD_ID" ]; then
@@ -25,9 +29,33 @@ if [[ "$IP" != "$old_ip" ]]; then
"https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECORD_ID" \ "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECORD_ID" \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-d "{\"type\":\"A\",\"name\":\"${sub}syedm.dev\",\"content\":\"$IP\",\"ttl\":3600,\"proxied\":false}" -d "{\"type\":\"A\",\"name\":\"${sub}syedm.dev\",\"content\":\"$IP_V4\",\"ttl\":3600,\"proxied\":false}"
fi fi
done done
echo "$IP_V4" > "$cache_v4"
fi
fi
echo "$IP" > "$cache" if [[ -n "$IP_V6" ]]; then
old_ip_v6=$([[ -f "$cache_v6" ]] && cat "$cache_v6" || echo "")
if [[ "$IP_V6" != "$old_ip_v6" ]]; then
for sub in "${arr[@]}"; do
RECORD_ID=$(@curl@/bin/curl -s -X GET \
"https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records" \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
-H "Content-Type: application/json" \
| @jq@/bin/jq -r ".result[] | select(.name==\"${sub}syedm.dev\" and .type==\"AAAA\") | .id")
if [ -n "$RECORD_ID" ]; then
@curl@/bin/curl -s -X PUT \
"https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECORD_ID" \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
-H "Content-Type: application/json" \
-d "{\"type\":\"AAAA\",\"name\":\"${sub}syedm.dev\",\"content\":\"$IP_V6\",\"ttl\":3600,\"proxied\":false}"
fi
done
echo "$IP_V6" > "$cache_v6"
fi
fi fi