update_cloudflare_com_v4.sh 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #!/bin/sh
  2. #
  3. #.Distributed under the terms of the GNU General Public License (GPL) version 2.0
  4. #
  5. # script for sending updates to cloudflare.com
  6. #.based on Ben Kulbertis cloudflare-update-record.sh found at http://gist.github.com/benkulbertis
  7. #.and on George Johnson's cf-ddns.sh found at https://github.com/gstuartj/cf-ddns.sh
  8. #.2016-2017 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
  9. # CloudFlare API documentation at https://api.cloudflare.com/
  10. #
  11. # This script is parsed by dynamic_dns_functions.sh inside send_update() function
  12. #
  13. # using following options from /etc/config/ddns
  14. # option username - your cloudflare e-mail
  15. # option password - cloudflare api key, you can get it from cloudflare.com/my-account/
  16. # option domain - "hostname@yourdomain.TLD" # syntax changed to remove split_FQDN() function and tld_names.dat.gz
  17. #
  18. # variable __IP already defined with the ip-address to use for update
  19. #
  20. # check parameters
  21. [ -z "$CURL_SSL" ] && write_log 14 "Cloudflare communication require cURL with SSL support. Please install"
  22. [ -z "$username" ] && write_log 14 "Service section not configured correctly! Missing key as 'username'"
  23. [ -z "$password" ] && write_log 14 "Service section not configured correctly! Missing secret as 'password'"
  24. [ $use_https -eq 0 ] && use_https=1 # force HTTPS
  25. # used variables
  26. local __HOST __DOMAIN __TYPE __URLBASE __PRGBASE __RUNPROG __DATA __IPV6 __ZONEID __RECID
  27. local __URLBASE="https://api.cloudflare.com/client/v4"
  28. # split __HOST __DOMAIN from $domain
  29. # given data:
  30. # @example.com for "domain record"
  31. # host.sub@example.com for a "host record"
  32. __HOST=$(printf %s "$domain" | cut -d@ -f1)
  33. __DOMAIN=$(printf %s "$domain" | cut -d@ -f2)
  34. # Cloudflare v4 needs:
  35. # __DOMAIN = the base domain i.e. example.com
  36. # __HOST = the FQDN of record to modify
  37. # i.e. example.com for the "domain record" or host.sub.example.com for "host record"
  38. # handling domain record then set __HOST = __DOMAIN
  39. [ -z "$__HOST" ] && __HOST=$__DOMAIN
  40. # handling host record then rebuild fqdn host@domain.tld => host.domain.tld
  41. [ "$__HOST" != "$__DOMAIN" ] && __HOST="${__HOST}.${__DOMAIN}"
  42. # set record type
  43. [ $use_ipv6 -eq 0 ] && __TYPE="A" || __TYPE="AAAA"
  44. # transfer function to use for godaddy
  45. # all needed variables are set global here
  46. # so we can use them directly
  47. cloudflare_transfer() {
  48. local __CNT=0
  49. local __ERR
  50. while : ; do
  51. write_log 7 "#> $__RUNPROG"
  52. eval "$__RUNPROG"
  53. __ERR=$? # save communication error
  54. [ $__ERR -eq 0 ] && break # no error break while
  55. write_log 3 "cURL Error: '$__ERR'"
  56. write_log 7 "$(cat $ERRFILE)" # report error
  57. [ $VERBOSE_MODE -gt 1 ] && {
  58. # VERBOSE_MODE > 1 then NO retry
  59. write_log 4 "Transfer failed - Verbose Mode: $VERBOSE_MODE - NO retry on error"
  60. break
  61. }
  62. __CNT=$(( $__CNT + 1 )) # increment error counter
  63. # if error count > retry_count leave here
  64. [ $retry_count -gt 0 -a $__CNT -gt $retry_count ] && \
  65. write_log 14 "Transfer failed after $retry_count retries"
  66. write_log 4 "Transfer failed - retry $__CNT/$retry_count in $RETRY_SECONDS seconds"
  67. sleep $RETRY_SECONDS &
  68. PID_SLEEP=$!
  69. wait $PID_SLEEP # enable trap-handler
  70. PID_SLEEP=0
  71. done
  72. # check for error
  73. grep -q '"success":true' $DATFILE || {
  74. write_log 4 "CloudFlare reported an error:"
  75. write_log 7 "$(cat $DATFILE)" # report error
  76. return 1 # HTTP-Fehler
  77. }
  78. }
  79. # Build base command to use
  80. __PRGBASE="$CURL -RsS -o $DATFILE --stderr $ERRFILE"
  81. # force network/interface-device to use for communication
  82. if [ -n "$bind_network" ]; then
  83. local __DEVICE
  84. network_get_physdev __DEVICE $bind_network || \
  85. write_log 13 "Can not detect local device using 'network_get_physdev $bind_network' - Error: '$?'"
  86. write_log 7 "Force communication via device '$__DEVICE'"
  87. __PRGBASE="$__PRGBASE --interface $__DEVICE"
  88. fi
  89. # force ip version to use
  90. if [ $force_ipversion -eq 1 ]; then
  91. [ $use_ipv6 -eq 0 ] && __PRGBASE="$__PRGBASE -4" || __PRGBASE="$__PRGBASE -6" # force IPv4/IPv6
  92. fi
  93. # set certificate parameters
  94. if [ "$cacert" = "IGNORE" ]; then # idea from Ticket #15327 to ignore server cert
  95. __PRGBASE="$__PRGBASE --insecure" # but not empty better to use "IGNORE"
  96. elif [ -f "$cacert" ]; then
  97. __PRGBASE="$__PRGBASE --cacert $cacert"
  98. elif [ -d "$cacert" ]; then
  99. __PRGBASE="$__PRGBASE --capath $cacert"
  100. elif [ -n "$cacert" ]; then # it's not a file and not a directory but given
  101. write_log 14 "No valid certificate(s) found at '$cacert' for HTTPS communication"
  102. fi
  103. # disable proxy if not set (there might be .wgetrc or .curlrc or wrong environment set)
  104. # or check if libcurl compiled with proxy support
  105. if [ -z "$proxy" ]; then
  106. __PRGBASE="$__PRGBASE --noproxy '*'"
  107. elif [ -z "$CURL_PROXY" ]; then
  108. # if libcurl has no proxy support and proxy should be used then force ERROR
  109. write_log 13 "cURL: libcurl compiled without Proxy support"
  110. fi
  111. # set headers
  112. __PRGBASE="$__PRGBASE --header 'X-Auth-Email: $username' "
  113. __PRGBASE="$__PRGBASE --header 'X-Auth-Key: $password' "
  114. __PRGBASE="$__PRGBASE --header 'Content-Type: application/json' "
  115. # __PRGBASE="$__PRGBASE --header 'Accept: application/json' "
  116. # read zone id for registered domain.TLD
  117. __RUNPROG="$__PRGBASE --request GET '$__URLBASE/zones?name=$__DOMAIN'"
  118. cloudflare_transfer || return 1
  119. # extract zone id
  120. __ZONEID=$(grep -o '"id":"[^"]*' $DATFILE | grep -o '[^"]*$' | head -1)
  121. [ -z "$__ZONEID" ] && {
  122. write_log 4 "Could not detect 'zone id' for domain.tld: '$__DOMAIN'"
  123. return 127
  124. }
  125. # read record id for A or AAAA record of host.domain.TLD
  126. __RUNPROG="$__PRGBASE --request GET '$__URLBASE/zones/$__ZONEID/dns_records?name=$__HOST&type=$__TYPE'"
  127. cloudflare_transfer || return 1
  128. # extract record id
  129. __RECID=$(grep -o '"id":"[^"]*' $DATFILE | grep -o '[^"]*$' | head -1)
  130. [ -z "$__RECID" ] && {
  131. write_log 4 "Could not detect 'record id' for host.domain.tld: '$__HOST'"
  132. return 127
  133. }
  134. # extract current stored IP
  135. __DATA=$(grep -o '"content":"[^"]*' $DATFILE | grep -o '[^"]*$' | head -1)
  136. # check data
  137. [ $use_ipv6 -eq 0 ] \
  138. && __DATA=$(printf "%s" "$__DATA" | grep -m 1 -o "$IPV4_REGEX") \
  139. || __DATA=$(printf "%s" "$__DATA" | grep -m 1 -o "$IPV6_REGEX")
  140. # we got data so verify
  141. [ -n "$__DATA" ] && {
  142. # expand IPv6 for compare
  143. if [ $use_ipv6 -eq 1 ]; then
  144. expand_ipv6 $__IP __IPV6
  145. expand_ipv6 $__DATA __DATA
  146. [ "$__DATA" = "$__IPV6" ] && { # IPv6 no update needed
  147. write_log 7 "IPv6 at CloudFlare.com already up to date"
  148. return 0
  149. }
  150. else
  151. [ "$__DATA" = "$__IP" ] && { # IPv4 no update needed
  152. write_log 7 "IPv4 at CloudFlare.com already up to date"
  153. return 0
  154. }
  155. fi
  156. }
  157. # update is needed
  158. # let's build data to send,
  159. # use file to work around " needed for json
  160. cat > $DATFILE << EOF
  161. {"id":"$__ZONEID","type":"$__TYPE","name":"$__HOST","content":"$__IP"}
  162. EOF
  163. # let's complete transfer command
  164. __RUNPROG="$__PRGBASE --request PUT --data @$DATFILE '$__URLBASE/zones/$__ZONEID/dns_records/$__RECID'"
  165. cloudflare_transfer || return 1
  166. return 0