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.

197 lines
5.1 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
8 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. #!/usr/bin/env sh
  2. #
  3. # Cloudflare.com API
  4. # https://api.cloudflare.com/
  5. #
  6. # Pass credentials before "acme.sh --issue --dns dns_cf ..."
  7. # --
  8. # export CF_Key="sdfsdfsdfljlbjkljlkjsdfoiwje"
  9. # export CF_Email="xxxx@sss.com"
  10. # --
  11. CF_API="https://api.cloudflare.com/client/v4"
  12. ######## Public functions #####################
  13. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  14. dns_cf_add() {
  15. fulldomain="$1"
  16. txtvalue="$2"
  17. CF_Key="${CF_Key:-$(_readaccountconf_mutable CF_Key)}"
  18. CF_Email="${CF_Email:-$(_readaccountconf_mutable CF_Email)}"
  19. if [ -z "$CF_Key" ] || [ -z "$CF_Email" ]; then
  20. CF_Key=""
  21. CF_Email=""
  22. _err "You don't specify cloudflare api key and email yet."
  23. _err "Please create you key and try again."
  24. return 1
  25. fi
  26. if ! _contains "$CF_Email" "@"; then
  27. _err "It seems that the CF_Email=$CF_Email is not a valid email address."
  28. _err "Please check and retry."
  29. return 1
  30. fi
  31. #save the api key and email to the account conf file.
  32. _saveaccountconf_mutable CF_Key "$CF_Key"
  33. _saveaccountconf_mutable CF_Email "$CF_Email"
  34. _debug "First detect the root zone"
  35. if ! _cf_get_zone; then
  36. return 1
  37. fi
  38. _debug _cf_zone "$_cf_zone"
  39. _debug _cf_zone_id "$_cf_zone_id"
  40. _debug "Getting TXT records"
  41. _cf_rest GET "zones/$_cf_zone_id/dns_records?type=TXT&name=$fulldomain"
  42. if ! _contains "$response" '"success":true'; then
  43. _err "Error getting TXT records"
  44. return 1
  45. fi
  46. count="$(printf "%s\n" "$response" | _egrep_o '"count" *: *[^,]*' | _head_n 1 | sed 's#^"count" *: *##')"
  47. _debug count "$count"
  48. if [ "$count" = "0" ]; then
  49. _info "Adding record"
  50. if _cf_rest POST "zones/$_cf_zone_id/dns_records" "{\"type\":\"TXT\",\"name\":\"$fulldomain\",\"content\":\"$txtvalue\",\"ttl\":120}"; then
  51. if _contains "$response" "$fulldomain"; then
  52. _info "Added, OK"
  53. return 0
  54. fi
  55. fi
  56. _err "Add TXT record error."
  57. else
  58. _info "Updating record"
  59. record_id="$(printf "%s\n" "$response" | _egrep_o '"id" *: *"[^"]*' | _head_n 1 | sed 's#^"id" *: *"##')"
  60. _debug record_id "$record_id"
  61. if _cf_rest PUT "zones/$_cf_zone_id/dns_records/$record_id" "{\"id\":\"$record_id\",\"type\":\"TXT\",\"name\":\"$fulldomain\",\"content\":\"$txtvalue\",\"zone_id\":\"$_cf_zone_id\",\"zone_name\":\"$_cf_zone\"}"; then
  62. _info "Updated, OK"
  63. return 0
  64. fi
  65. _err "Update TXT record error"
  66. fi
  67. return 1
  68. }
  69. #fulldomain txtvalue
  70. dns_cf_rm() {
  71. fulldomain="$1"
  72. txtvalue="$2"
  73. CF_Key="${CF_Key:-$(_readaccountconf_mutable CF_Key)}"
  74. CF_Email="${CF_Email:-$(_readaccountconf_mutable CF_Email)}"
  75. if [ -z "$CF_Key" ] || [ -z "$CF_Email" ]; then
  76. CF_Key=""
  77. CF_Email=""
  78. _err "You don't specify cloudflare api key and email yet."
  79. _err "Please create you key and try again."
  80. return 1
  81. fi
  82. _debug "First detect the root zone"
  83. if ! _cf_get_zone; then
  84. return 1
  85. fi
  86. _debug _cf_zone "$_cf_zone"
  87. _debug _cf_zone_id "$_cf_zone_id"
  88. _debug "Getting TXT records"
  89. _cf_rest GET "zones/$_cf_zone_id/dns_records?type=TXT&name=$fulldomain&content=$txtvalue"
  90. if ! _contains "$response" '"success":true'; then
  91. _err "Error getting TXT records"
  92. return 1
  93. fi
  94. count="$(printf "%s\n" "$response" | _egrep_o '"count" *: *[^,]*' | _head_n 1 | sed 's#^"count" *: *##')"
  95. _debug count "$count"
  96. if [ "$count" = "0" ]; then
  97. _info "Don't need to remove."
  98. return 0
  99. fi
  100. record_id="$(printf "%s\n" "$response" | _egrep_o '"id" *: *"[^"]*' | _head_n 1 | sed 's#^"id" *: *"##')"
  101. _debug record_id "$record_id"
  102. if [ -z "$record_id" ]; then
  103. _err "Can not get record id to remove."
  104. return 1
  105. fi
  106. if _cf_rest DELETE "zones/$_cf_zone_id/dns_records/$record_id"; then
  107. if _contains "$response" '"success":true'; then
  108. return 0
  109. fi
  110. fi
  111. _err "Delete record error."
  112. return 1
  113. }
  114. #################### Private functions below ##################################
  115. #fulldomain=_acme-challenge.www.domain.com
  116. #returns
  117. # _cf_zone=domain.com
  118. # _cf_zone_id=sdjkglgdfewsdfg
  119. _cf_get_zone() {
  120. i=2
  121. while true; do
  122. domain="$(printf "%s" "$fulldomain" | cut -d . -f "$i-100")"
  123. if [ -z "$domain" ]; then
  124. break
  125. fi
  126. if ! _cf_rest GET "zones?name=$domain"; then
  127. break
  128. fi
  129. _debug domain "$domain"
  130. if _contains "$response" "\"name\":\"$domain\""; then
  131. _cf_zone_id="$(printf "%s\n" "$response" | _egrep_o '\[."id" *: *"[^"]*' | _head_n 1 | sed 's#^\[."id" *: *"##')"
  132. if [ -z "$_cf_zone_id" ]; then
  133. break
  134. fi
  135. _cf_zone="$domain"
  136. return 0
  137. fi
  138. i=$(_math "$i" + 1)
  139. done
  140. _cf_zone=""
  141. _cf_zone_id=""
  142. _err "get zone failed"
  143. return 1
  144. }
  145. _cf_rest() {
  146. method="$1"
  147. request="$2"
  148. data="$3"
  149. export _H1="X-Auth-Email: $CF_Email"
  150. export _H2="X-Auth-Key: $CF_Key"
  151. export _H3="Content-Type: application/json"
  152. if [ "$method" = "GET" ]; then
  153. _debug request "$request"
  154. response="$(_get "$CF_API/$request" "" "")"
  155. else
  156. _debug data "$data"
  157. _debug request "$request"
  158. response="$(_post "$data" "$CF_API/$request" "" "$method")"
  159. fi
  160. if [ "$?" != "0" ]; then
  161. _err "error for request: $request"
  162. return 1
  163. fi
  164. _debug2 response "$response"
  165. return 0
  166. }