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.

138 lines
5.5 KiB

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