Add ipv6 & logging support to ddns
This commit is contained in:
@@ -2,60 +2,101 @@
|
||||
|
||||
source /run/secrets.env
|
||||
|
||||
# Separate caches for IPv4 and IPv6
|
||||
log() {
|
||||
echo "$(date '+%Y-%m-%d %H:%M:%S') [cloudflare-ddns] $*"
|
||||
}
|
||||
|
||||
log "Starting DDNS update"
|
||||
|
||||
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')
|
||||
|
||||
log "Detected IPv4: ${IP_V4:-none}"
|
||||
log "Detected IPv6: ${IP_V6:-none}"
|
||||
|
||||
arr=("" "git." "www." "acs.")
|
||||
|
||||
if [[ -n "$IP_V4" ]]; then
|
||||
old_ip_v4=$([[ -f "$cache_v4" ]] && cat "$cache_v4" || echo "")
|
||||
|
||||
log "Old IPv4: ${old_ip_v4:-none}"
|
||||
|
||||
if [[ "$IP_V4" != "$old_ip_v4" ]]; then
|
||||
log "IPv4 changed, updating A records"
|
||||
|
||||
for sub in "${arr[@]}"; do
|
||||
name="${sub}syedm.dev"
|
||||
|
||||
log "Checking A record for $name"
|
||||
|
||||
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==\"A\") | .id")
|
||||
-H "Content-Type: application/json" | @jq@/bin/jq \
|
||||
-r ".result[] | select(.name==\"${name}\" and .type==\"A\") | .id")
|
||||
|
||||
if [ -n "$RECORD_ID" ]; then
|
||||
log "Updating $name -> $IP_V4"
|
||||
|
||||
@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\":\"A\",\"name\":\"${sub}syedm.dev\",\"content\":\"$IP_V4\",\"ttl\":3600,\"proxied\":false}"
|
||||
-d "{\"type\":\"A\",\"name\":\"$name\",\"content\":\"$IP_V4\",\"ttl\":3600,\"proxied\":false}"
|
||||
|
||||
log "Updated $name"
|
||||
else
|
||||
log "No A record found for $name"
|
||||
fi
|
||||
done
|
||||
|
||||
echo "$IP_V4" > "$cache_v4"
|
||||
else
|
||||
log "IPv4 unchanged"
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ -n "$IP_V6" ]]; then
|
||||
old_ip_v6=$([[ -f "$cache_v6" ]] && cat "$cache_v6" || echo "")
|
||||
|
||||
log "Old IPv6: ${old_ip_v6:-none}"
|
||||
|
||||
if [[ "$IP_V6" != "$old_ip_v6" ]]; then
|
||||
log "IPv6 changed, updating AAAA records"
|
||||
|
||||
for sub in "${arr[@]}"; do
|
||||
name="${sub}syedm.dev"
|
||||
|
||||
log "Checking AAAA record for $name"
|
||||
|
||||
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")
|
||||
-H "Content-Type: application/json" | @jq@/bin/jq \
|
||||
-r ".result[] | select(.name==\"${name}\" and .type==\"AAAA\") | .id")
|
||||
|
||||
if [ -n "$RECORD_ID" ]; then
|
||||
log "Updating $name -> $IP_V6"
|
||||
|
||||
@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}"
|
||||
-d "{\"type\":\"AAAA\",\"name\":\"$name\",\"content\":\"$IP_V6\",\"ttl\":3600,\"proxied\":false}"
|
||||
|
||||
log "Updated $name"
|
||||
else
|
||||
log "No AAAA record found for $name"
|
||||
fi
|
||||
done
|
||||
|
||||
echo "$IP_V6" > "$cache_v6"
|
||||
else
|
||||
log "IPv6 unchanged"
|
||||
fi
|
||||
fi
|
||||
|
||||
log "DDNS update finished"
|
||||
|
||||
Reference in New Issue
Block a user