-
-
Save Sizzl/73018e45140cfdcc8ff7 to your computer and use it in GitHub Desktop.
CloudFlare dynamic dns updater module for Synology
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| # cloudflareddns.sh - dynamic dns updater module for Synology | |
| # | |
| # Author: | |
| # Michael Wildman (http://mwild.me) | |
| # | |
| # Version: | |
| # 0.1.0 | |
| # | |
| # Description: | |
| # To install: | |
| # 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__ | |
| # | |
| # Based on Brian Schmidt Pedersen (http://blog.briped.net) gratisdns.sh | |
| # TODO | |
| # read addition parameters from cloudflare api calls | |
| # | |
| { # Read the supplied arguments into variables used in this script. | |
| # username should be your email | |
| __USERNAME__="$(echo ${@} | cut -d' ' -f1)" | |
| # password is your API key | |
| __PASSWORD__="$(echo ${@} | cut -d' ' -f2)" | |
| __HOSTNAME__="$(echo ${@} | cut -d' ' -f3)" | |
| __MYIP__="$(echo ${@} | cut -d' ' -f4)" | |
| # Where to store the IP used for the last DNS update: | |
| __IP_CACHE__="/tmp/cloudflareddns_ip.txt" | |
| # Where to store the logfile: | |
| __LOGFILE__="/var/log/cloudflareddns.log" | |
| # Additional parameters needed for CloudFlare | |
| # you will need to create the record then run rec_load_all to get the id | |
| __RECTYPE__="A" | |
| __RECID__="" | |
| __RECNAME__="" | |
| __TTL__="1" | |
| __SERVICEMODE__="0" | |
| } | |
| log() { | |
| # Severity Levels: | |
| # 0 Emergency System is unusable. | |
| # A "panic" condition usually affecting multiple apps/servers/sites. | |
| # At this level it would usually notify all tech staff on call. | |
| # 1 Alert Action must be taken immediately. | |
| # Should be corrected immediately, therefore notify staff who can fix | |
| # the problem.An example would be the loss of a backup ISP connection | |
| # 2 Critical Critical conditions. | |
| # Should be corrected immediately, but indicates failure in a primary | |
| # system, an example is a loss of primary ISP connection. | |
| # 3 Error Error conditions. | |
| # Non-urgent failures, these should be relayed to developers or | |
| # admins; each item must be resolved within a given time. | |
| # 4 Warning Warning conditions. | |
| # Warning messages, not an error, but indication that an error will | |
| # occur if action is not taken, e.g. file system 85% full - each item | |
| # must be resolved within a given time. | |
| # 5 Notice Normal but significant condition. | |
| # Events that are unusual but not error conditions - might be | |
| # summarized in an email to developers or admins to spot potential | |
| # problems - no immediate action required. | |
| # 6 Informational Informational messages. | |
| # Normal operational messages - may be harvested for reporting, | |
| # measuring throughput, etc - no action required. | |
| # 7 Debug Debug-level messages. | |
| # Info useful to developers for debugging the application, not useful | |
| # during operations. | |
| __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__} | |
| } | |
| update_ddns() { | |
| if [ -z "${1}" ]; then | |
| log "update_ddns(): Missing Argument. URL string required." 3 | |
| false | |
| else | |
| __URL__="${1}" | |
| fi | |
| # Update DNS record: | |
| log "update_ddns(): Updating with ${__URL__}." 7 | |
| __RESPONSE__=$(curl --silent "${__URL__}") | |
| # Strip the result element from response json | |
| __RESULT__=$(echo ${__RESPONSE__} | grep -o -E .result.:.[A-z]+.) | |
| if [ "${?}" -ne "0" ]; then | |
| log "update_ddns(): cURL returned error code ${?} while trying to update DDNS." 3 | |
| false | |
| fi | |
| log "update_ddns(): __RESULT__=${__RESULT__}" 7 | |
| case ${__RESULT__} in | |
| '"result":"success"') | |
| echo ${__MYIP__} > ${__IP_CACHE__} | |
| __STATUS__='good' | |
| true | |
| ;; | |
| *) | |
| __STATUS__="${__RESULT__}" | |
| log "update_dns(): __RESPONSE__=${__RESPONSE__}" 5 | |
| false | |
| ;; | |
| esac | |
| log "update_ddns(): DDNSd Status: ${__STATUS__}" 6 | |
| } | |
| { # Get the last known DDNS IP | |
| if [ ! -f ${__IP_CACHE__} ]; then | |
| # If the file wasn't found, create it to avoid errors. | |
| echo "127.0.0.1" > ${__IP_CACHE__} | |
| fi | |
| __OLDIP__=$(cat ${__IP_CACHE__}) | |
| } | |
| if [ "${__MYIP__}" == "0.0.0.0" ] || [ "${__MYIP__}" == "0.0.0.0" ]; then | |
| log "IP was '0.0.0.0', which is wrong. Not updating." 5 | |
| elif [ "${__OLDIP__}" == "${__MYIP__}" ]; then | |
| log "IP not changed. ${__MYIP__}. Not updating." 6 | |
| __STATUS__="nochg" | |
| else | |
| __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__}"" | |
| # Set the response and status variables, so they're available globally. That way I can read them outside the update_ddns() function. | |
| __RESULT__="No result from DDNS." | |
| __RESPONSE__="No response returned from DDNS provider." | |
| __STATUS__="No status returned from update_ddns()." | |
| log "IP changed. ${__OLDIP__} > ${__MYIP__}. Attempting to update DNS." 6 | |
| update_ddns "${__URL__}" | |
| fi | |
| printf "%s" "${__STATUS__}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment