Skip to content

Instantly share code, notes, and snippets.

@sumimakito
Created May 8, 2022 10:38
Show Gist options
  • Save sumimakito/7135e822b7151699a2dff4452c136ffb to your computer and use it in GitHub Desktop.
Save sumimakito/7135e822b7151699a2dff4452c136ffb to your computer and use it in GitHub Desktop.
Cloudflare DDNS Provider for Synology
#!/bin/sh
_exit() {
echo $1
exit
}
if [[ "$#" -ne 4 ]]; then
_exit "badparam"
fi
ZONE_ID=$1
API_TOKEN=$2
HOSTNAME=$3
IPv6=$(ip -6 --brief addr show dev eth1 scope global | awk '{ print $3 }' | sed -e 's/\/.*//')
list_dns_records() {
RESULT=$(
curl -s -X GET "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records?type=AAAA&name=${HOSTNAME}&match=all" \
-H "Authorization: Bearer ${API_TOKEN}" \
-H "Content-Type: application/json"
)
if [[ $? -ne 0 ]]; then
_exit "badconn"
fi
RESULT_OK=$(echo $RESULT | jq '.success')
}
patch_dns_record() {
RESULT=$(
curl -s -X PATCH "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records/${1}" \
-H "Authorization: Bearer ${API_TOKEN}" \
-H "Content-Type: application/json" \
--data "${2}"
)
if [[ $? -ne 0 ]]; then
_exit "badconn"
fi
RESULT_OK=$(echo $RESULT | jq -r '.success')
}
list_dns_records
if [[ ! $RESULT_OK ]]; then
if [[ $(echo $RESULT | jq -r '.errors[0].code') -eq 10000 ]]; then
_exit "badauth"
fi
_exit "badagent"
fi
RECORDS=$(echo $RESULT | jq '.result')
if [[ $(echo $RECORDS | jq -r 'length') -le 0 ]]; then
_exit "nohost"
fi
RECORD=$(echo $RECORDS | jq '.[0]')
PATCHED_RECORD=$(echo $RECORD | jq -r "{ content: \"$IPv6\" } | tostring")
patch_dns_record $(echo $RECORD | jq -r '.id') $PATCHED_RECORD
if [[ ! $RESULT_OK ]]; then
if [[ $(echo $RESULT | jq -r '.errors[0].code') -eq 10000 ]]; then
_exit "badauth"
fi
_exit "badagent"
fi
_exit "good"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment