Skip to content

Instantly share code, notes, and snippets.

@m-wild
Forked from briped/gratisdns.sh
Last active June 1, 2022 20:18
Show Gist options
  • Star 43 You must be signed in to star a gist
  • Fork 16 You must be signed in to fork a gist
  • Save m-wild/f1d2579f3c922e8bb4a0 to your computer and use it in GitHub Desktop.
Save m-wild/f1d2579f3c922e8bb4a0 to your computer and use it in GitHub Desktop.
CloudFlare dynamic dns updater module for Synology
#!/bin/sh
# cloudflareddns.sh - dynamic dns updater module for Synology
#
# Author:
# Michael Wildman (http://mwild.me)
#
# Version:
# 0.2
#
# 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 rec_load_all query to get the record id
# (see https://www.cloudflare.com/docs/client-api.html)
#
# Changelog:
# 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
__RECTYPE__="A"
__RECID__=""
__RECNAME__=""
__TTL__="1"
__SERVICEMODE__="0"
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://www.cloudflare.com/api_json.html?a=rec_edit&tkn=${__PASSWORD__}&email=${__USERNAME__}&z=${__HOSTNAME__}&type=${__RECTYPE__}&id=${__RECID__}&name=${__RECNAME__}&content=${__MYIP__}&ttl=${__TTL__}&service_mode=${__SERVICEMODE__}"
# Update DNS record:
log "Updating with ${__MYIP__}..." 7
__RESPONSE__=$(curl --silent "${__URL__}")
# Strip the result element from response json
__RESULT__=$(echo ${__RESPONSE__} | grep -o -E .result.:.[A-z]+.)
case ${__RESULT__} in
'"result":"success"')
__STATUS__='good'
true
;;
*)
__STATUS__="${__RESULT__}"
log "__RESPONSE__=${__RESPONSE__}" 5
false
;;
esac
log "Status: ${__STATUS__}" 6
printf "%s" "${__STATUS__}"
@mrikirill
Copy link

@dvershinin yep mate, you're right, didn't think about this case. I'm going to fix it. Feel free to send an issue in my project)

@dvershinin
Copy link

@mrikirill just above your comment, I mention cloudflareddns which addresses all these edge case from the start.

@scyto
Copy link

scyto commented Feb 23, 2022

nice script
personally i went this route as it's less invasive https://hub.docker.com/r/oznu/cloudflare-ddns/#!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment