Add IPv6 to ddns
This commit is contained in:
@@ -2,32 +2,60 @@
|
|||||||
|
|
||||||
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=""
|
if [[ "$IP_V4" != "$old_ip_v4" ]]; then
|
||||||
fi
|
for sub in "${arr[@]}"; do
|
||||||
|
RECORD_ID=$(@curl@/bin/curl -s -X GET \
|
||||||
if [[ "$IP" != "$old_ip" ]]; then
|
"https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records" \
|
||||||
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==\"A\") | .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 "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}"
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
echo "$IP" > "$cache"
|
| @jq@/bin/jq -r ".result[] | select(.name==\"${sub}syedm.dev\" and .type==\"A\") | .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\":\"A\",\"name\":\"${sub}syedm.dev\",\"content\":\"$IP_V4\",\"ttl\":3600,\"proxied\":false}"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
echo "$IP_V4" > "$cache_v4"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user