You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
5.8 KiB

  1. #
  2. #.Distributed under the terms of the GNU General Public License (GPL) version 2.0
  3. #
  4. # script for sending updates to cloudflare.com
  5. #.2014-2015 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
  6. # many thanks to Paul for testing and feedback during development
  7. #
  8. # This script is parsed by dynamic_dns_functions.sh inside send_update() function
  9. #
  10. # using following options from /etc/config/ddns
  11. # option username - your cloudflare e-mail
  12. # option password - cloudflare api key, you can get it from cloudflare.com/my-account/
  13. # option domain - your full hostname to update, in cloudflare its subdomain.domain
  14. # i.e. myhost.example.com where myhost is the subdomain and example.com is your domain
  15. #
  16. # variable __IP already defined with the ip-address to use for update
  17. #
  18. [ $use_https -eq 0 ] && write_log 14 "Cloudflare only support updates via Secure HTTP (HTTPS). Please correct configuration!"
  19. [ -z "$username" ] && write_log 14 "Service section not configured correctly! Missing 'username'"
  20. [ -z "$password" ] && write_log 14 "Service section not configured correctly! Missing 'password'"
  21. local __RECID __URL __KEY __KEYS __FOUND __SUBDOM __DOMAIN __TLD
  22. # split given Host/Domain into TLD, registrable domain, and subdomain
  23. split_FQDN $domain __TLD __DOMAIN __SUBDOM
  24. [ $? -ne 0 -o -z "$__DOMAIN" ] && \
  25. write_log 14 "Wrong Host/Domain configuration ($domain). Please correct configuration!"
  26. # put together what we need
  27. __DOMAIN="$__DOMAIN.$__TLD"
  28. # parse OpenWrt script with
  29. # functions for parsing and generating json
  30. . /usr/share/libubox/jshn.sh
  31. # function copied from /usr/share/libubox/jshn.sh
  32. # from BB14.09 for backward compatibility to AA12.09
  33. grep -i "json_get_keys" /usr/share/libubox/jshn.sh >/dev/null 2>&1 || json_get_keys() {
  34. local __dest="$1"
  35. local _tbl_cur
  36. if [ -n "$2" ]; then
  37. json_get_var _tbl_cur "$2"
  38. else
  39. _json_get_var _tbl_cur JSON_CUR
  40. fi
  41. local __var="${JSON_PREFIX}KEYS_${_tbl_cur}"
  42. eval "export -- \"$__dest=\${$__var}\"; [ -n \"\${$__var+x}\" ]"
  43. }
  44. # function to "sed" unwanted string parts from DATFILE
  45. cleanup() {
  46. # based on the sample output on cloudflare.com homepage we need to do some cleanup
  47. sed -i 's/^[ \t]*//;s/[ \t]*$//' $DATFILE # remove invisible chars at beginning and end of lines
  48. sed -i '/^-$/d' $DATFILE # remove lines with "-" (dash)
  49. sed -i '/^$/d' $DATFILE # remove empty lines
  50. sed -i "#'##g" $DATFILE # remove "'" (single quote)
  51. }
  52. [ -n "$rec_id" ] && __RECID="$rec_id" || {
  53. # build url according to cloudflare client api at https://www.cloudflare.com/docs/client-api.html
  54. # to "rec_load_all" to detect rec_id needed for update
  55. __URL="https://www.cloudflare.com/api_json.html" # https://www.cloudflare.com/api_json.html
  56. __URL="${__URL}?a=rec_load_all" # -d 'a=rec_load_all'
  57. __URL="${__URL}&tkn=$password" # -d 'tkn=8afbe6dea02407989af4dd4c97bb6e25'
  58. __URL="${__URL}&email=$username" # -d 'email=sample@example.com'
  59. __URL="${__URL}&z=$__DOMAIN" # -d 'z=example.com'
  60. # lets request the data
  61. do_transfer "$__URL" || return 1
  62. cleanup # cleanup dat file
  63. json_load "$(cat $DATFILE)" # lets extract data
  64. __FOUND=0 # found record indicator
  65. json_get_var __RES "result" # cloudflare result of last request
  66. json_get_var __MSG "msg" # cloudflare error message
  67. [ "$__RES" != "success" ] && {
  68. write_log 4 "'rec_load_all' failed with error: \n$__MSG"
  69. return 1
  70. }
  71. json_select "response"
  72. json_select "recs"
  73. json_select "objs"
  74. json_get_keys __KEYS
  75. for __KEY in $__KEYS; do
  76. local __ZONE __DISPLAY __NAME __TYPE
  77. json_select "$__KEY"
  78. # json_get_var __ZONE "zone_name" # for debugging
  79. # json_get_var __DISPLAY "display_name" # for debugging
  80. json_get_var __NAME "name"
  81. json_get_var __TYPE "type"
  82. if [ "$__NAME" = "$domain" ]; then
  83. # we must verify IPv4 and IPv6 because there might be both for the same host
  84. [ \( $use_ipv6 -eq 0 -a "$__TYPE" = "A" \) -o \( $use_ipv6 -eq 1 -a "$__TYPE" = "AAAA" \) ] && {
  85. __FOUND=1 # mark found
  86. break # found leave for loop
  87. }
  88. fi
  89. json_select ..
  90. done
  91. [ $__FOUND -eq 0 ] && {
  92. # we don't need to continue trying to update cloudflare because record to update does not exist
  93. # user has to setup record first outside ddns-scripts
  94. write_log 14 "No valid record found at Cloudflare setup. Please create first!"
  95. }
  96. json_get_var __RECID "rec_id" # last thing to do get rec_id
  97. json_cleanup # cleanup
  98. write_log 7 "rec_id '$__RECID' detected for host/domain '$domain'"
  99. }
  100. # build url according to cloudflare client api at https://www.cloudflare.com/docs/client-api.html
  101. # for "rec_edit" to update IP address
  102. __URL="https://www.cloudflare.com/api_json.html" # https://www.cloudflare.com/api_json.html
  103. __URL="${__URL}?a=rec_edit" # -d 'a=rec_edit'
  104. __URL="${__URL}&tkn=$password" # -d 'tkn=8afbe6dea02407989af4dd4c97bb6e25'
  105. __URL="${__URL}&id=$__RECID" # -d 'id=9001'
  106. __URL="${__URL}&email=$username" # -d 'email=sample@example.com'
  107. __URL="${__URL}&z=$__DOMAIN" # -d 'z=example.com'
  108. [ $use_ipv6 -eq 0 ] && __URL="${__URL}&type=A" # -d 'type=A' (IPv4)
  109. [ $use_ipv6 -eq 1 ] && __URL="${__URL}&type=AAAA" # -d 'type=AAAA' (IPv6)
  110. # handle subdomain or domain record
  111. [ -n "$__SUBDOM" ] && __URL="${__URL}&name=$__SUBDOM" # -d 'name=sub' (HOST/SUBDOMAIN)
  112. [ -z "$__SUBDOM" ] && __URL="${__URL}&name=$__DOMAIN" # -d 'name=example.com'(DOMAIN)
  113. __URL="${__URL}&content=$__IP" # -d 'content=1.2.3.4'
  114. __URL="${__URL}&service_mode=0" # -d 'service_mode=0'
  115. __URL="${__URL}&ttl=1" # -d 'ttl=1'
  116. # lets do the update
  117. do_transfer "$__URL" || return 1
  118. cleanup # cleanup tmp file
  119. json_load "$(cat $DATFILE)" # lets extract data
  120. json_get_var __RES "result" # cloudflare result of last request
  121. json_get_var __MSG "msg" # cloudflare error message
  122. [ "$__RES" != "success" ] && {
  123. write_log 4 "'rec_edit' failed with error:\n$__MSG"
  124. return 1
  125. }
  126. write_log 7 "Update of rec_id '$__RECID' successful"
  127. return 0