Skip to content

Instantly share code, notes, and snippets.

@LucaDev
Created March 16, 2017 22:01
Show Gist options
  • Save LucaDev/ffc132c360f60eb1b7121d930c231cea to your computer and use it in GitHub Desktop.
Save LucaDev/ffc132c360f60eb1b7121d930c231cea to your computer and use it in GitHub Desktop.
Cloudflare V4 API DynDNS Updater for Synology Cloudstation
#!/bin/sh
# cloudflareddns.sh - dynamic dns updater module for Synology
#
# Author:
# Luca Kröger (LucaDev on Github)
# Originally: Michael Wildman (http://mwild.me)
#
# Version:
# 0.3
#
# Description:
# copy to /sbin/cloudflaredns.sh
# make executable (chmod +x)
# add the following entry to /etc.defaults/ddns_provider.conf
#
# [CloudFlare]
# modulepath=/sbin/cloudflaredns.sh
# queryurl=https://www.cloudflare.com/api_json.html?a=rec_edit&tkn=__PASSWORD__&email=__USERNAME__&z=__HOSTNAME__&content=__MYIP__
#
# (note that URL is not actually used, DSM will pass us the variables)
# run a /zone to get the zone & rec id
# (see https://api.cloudflare.com/)
#
# Changelog:
# 0.3:
# - Update to Cloudflare V4 API
#
# 0.2:
# - Simplified this thing to its most basic requirements (curl + logging)
# - Now either returns 'good' or the result (no checking for cases -- the log was always more useful anyway!)
#
# Based on Brian Schmidt Pedersen (http://blog.briped.net) gratisdns.sh
# TODO
# read addition parameters from cloudflare api calls
#
# these variables are passed by DSM
# username is your email
__USERNAME__="$(echo ${@} | cut -d' ' -f1)"
# password is your cloudflare API key
__PASSWORD__="$(echo ${@} | cut -d' ' -f2)"
__HOSTNAME__="$(echo ${@} | cut -d' ' -f3)"
__MYIP__="$(echo ${@} | cut -d' ' -f4)"
# log location
__LOGFILE__="/var/log/cloudflareddns.log"
# additional parameters needed for CloudFlare
__REC__="my.domain.tld"
__RECID__="myrecordid"
__ZONE__="myzoneid"
log() {
__LOGTIME__=$(date +"%b %e %T")
if [ "${#}" -lt 1 ]; then
false
else
__LOGMSG__="${1}"
fi
if [ "${#}" -lt 2 ]; then
__LOGPRIO__=7
else
__LOGPRIO__=${2}
fi
logger -p ${__LOGPRIO__} -t "$(basename ${0})" "${__LOGMSG__}"
echo "${__LOGTIME__} $(basename ${0}) (${__LOGPRIO__}): ${__LOGMSG__}" >> ${__LOGFILE__}
}
__URL__="https://api.cloudflare.com/client/v4/zones/${__ZONE__}/dns_records/${__RECID__}"
# Update DNS record:
log "Updating with ${__MYIP__}..." 7
__RESPONSE__=$(curl --silent -X PUT "${__URL__}" -H "X-Auth-Email:${__USERNAME__}" \
-H "X-Auth-Key:${__PASSWORD__}" \
-H "Content-Type:application/json" \
--data '{"type":"A","name":"'${__REC__}'","content":"'${__MYIP__}'","data":{}}')
# Strip the result element from response json
__RESULT__=$(echo ${__RESPONSE__} | grep -o -E .success.:.[A-z]+.)
case ${__RESULT__} in
'"success":true,')
__STATUS__='good'
true
;;
*)
__STATUS__="${__RESULT__}"
log "__RESPONSE__=${__RESPONSE__}" 5
false
;;
esac
log "Status: ${__STATUS__}" 6
printf "%s" "${__STATUS__}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment