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.

234 lines
6.4 KiB

9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
7 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
9 years ago
9 years ago
  1. #!/usr/bin/env sh
  2. #
  3. #CF_Key="sdfsdfsdfljlbjkljlkjsdfoiwje"
  4. #
  5. #CF_Email="xxxx@sss.com"
  6. #CF_Token="xxxx"
  7. #CF_Account_ID="xxxx"
  8. #CF_Zone_ID="xxxx"
  9. CF_Api="https://api.cloudflare.com/client/v4"
  10. ######## Public functions #####################
  11. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  12. dns_cf_add() {
  13. fulldomain=$1
  14. txtvalue=$2
  15. CF_Token="${CF_Token:-$(_readaccountconf_mutable CF_Token)}"
  16. CF_Account_ID="${CF_Account_ID:-$(_readaccountconf_mutable CF_Account_ID)}"
  17. CF_Zone_ID="${CF_Zone_ID:-$(_readaccountconf_mutable CF_Zone_ID)}"
  18. CF_Key="${CF_Key:-$(_readaccountconf_mutable CF_Key)}"
  19. CF_Email="${CF_Email:-$(_readaccountconf_mutable CF_Email)}"
  20. if [ "$CF_Token" ]; then
  21. _saveaccountconf_mutable CF_Token "$CF_Token"
  22. _saveaccountconf_mutable CF_Account_ID "$CF_Account_ID"
  23. _saveaccountconf_mutable CF_Zone_ID "$CF_Zone_ID"
  24. else
  25. if [ -z "$CF_Key" ] || [ -z "$CF_Email" ]; then
  26. CF_Key=""
  27. CF_Email=""
  28. _err "You didn't specify a Cloudflare api key and email yet."
  29. _err "You can get yours from here https://dash.cloudflare.com/profile."
  30. return 1
  31. fi
  32. if ! _contains "$CF_Email" "@"; then
  33. _err "It seems that the CF_Email=$CF_Email is not a valid email address."
  34. _err "Please check and retry."
  35. return 1
  36. fi
  37. #save the api key and email to the account conf file.
  38. _saveaccountconf_mutable CF_Key "$CF_Key"
  39. _saveaccountconf_mutable CF_Email "$CF_Email"
  40. fi
  41. _debug "First detect the root zone"
  42. if ! _get_root "$fulldomain"; then
  43. _err "invalid domain"
  44. return 1
  45. fi
  46. _debug _domain_id "$_domain_id"
  47. _debug _sub_domain "$_sub_domain"
  48. _debug _domain "$_domain"
  49. _debug "Getting txt records"
  50. _cf_rest GET "zones/${_domain_id}/dns_records?type=TXT&name=$fulldomain"
  51. if ! printf "%s" "$response" | grep \"success\":true >/dev/null; then
  52. _err "Error"
  53. return 1
  54. fi
  55. # For wildcard cert, the main root domain and the wildcard domain have the same txt subdomain name, so
  56. # we can not use updating anymore.
  57. # count=$(printf "%s\n" "$response" | _egrep_o "\"count\":[^,]*" | cut -d : -f 2)
  58. # _debug count "$count"
  59. # if [ "$count" = "0" ]; then
  60. _info "Adding record"
  61. if _cf_rest POST "zones/$_domain_id/dns_records" "{\"type\":\"TXT\",\"name\":\"$fulldomain\",\"content\":\"$txtvalue\",\"ttl\":120}"; then
  62. if _contains "$response" "$txtvalue"; then
  63. _info "Added, OK"
  64. return 0
  65. elif _contains "$response" "The record already exists"; then
  66. _info "Already exists, OK"
  67. return 0
  68. else
  69. _err "Add txt record error."
  70. return 1
  71. fi
  72. fi
  73. _err "Add txt record error."
  74. return 1
  75. }
  76. #fulldomain txtvalue
  77. dns_cf_rm() {
  78. fulldomain=$1
  79. txtvalue=$2
  80. CF_Token="${CF_Token:-$(_readaccountconf_mutable CF_Token)}"
  81. CF_Account_ID="${CF_Account_ID:-$(_readaccountconf_mutable CF_Account_ID)}"
  82. CF_Zone_ID="${CF_Zone_ID:-$(_readaccountconf_mutable CF_Zone_ID)}"
  83. CF_Key="${CF_Key:-$(_readaccountconf_mutable CF_Key)}"
  84. CF_Email="${CF_Email:-$(_readaccountconf_mutable CF_Email)}"
  85. _debug "First detect the root zone"
  86. if ! _get_root "$fulldomain"; then
  87. _err "invalid domain"
  88. return 1
  89. fi
  90. _debug _domain_id "$_domain_id"
  91. _debug _sub_domain "$_sub_domain"
  92. _debug _domain "$_domain"
  93. _debug "Getting txt records"
  94. _cf_rest GET "zones/${_domain_id}/dns_records?type=TXT&name=$fulldomain&content=$txtvalue"
  95. if ! printf "%s" "$response" | grep \"success\":true >/dev/null; then
  96. _err "Error: $response"
  97. return 1
  98. fi
  99. count=$(printf "%s\n" "$response" | _egrep_o "\"count\":[^,]*" | cut -d : -f 2)
  100. _debug count "$count"
  101. if [ "$count" = "0" ]; then
  102. _info "Don't need to remove."
  103. else
  104. record_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \" | head -n 1)
  105. _debug "record_id" "$record_id"
  106. if [ -z "$record_id" ]; then
  107. _err "Can not get record id to remove."
  108. return 1
  109. fi
  110. if ! _cf_rest DELETE "zones/$_domain_id/dns_records/$record_id"; then
  111. _err "Delete record error."
  112. return 1
  113. fi
  114. _contains "$response" '"success":true'
  115. fi
  116. }
  117. #################### Private functions below ##################################
  118. #_acme-challenge.www.domain.com
  119. #returns
  120. # _sub_domain=_acme-challenge.www
  121. # _domain=domain.com
  122. # _domain_id=sdjkglgdfewsdfg
  123. _get_root() {
  124. domain=$1
  125. i=1
  126. p=1
  127. # Use Zone ID directly if provided
  128. if [ "$CF_Zone_ID" ]; then
  129. if ! _cf_rest GET "zones/$CF_Zone_ID"; then
  130. return 1
  131. else
  132. if _contains "$response" '"success":true'; then
  133. _domain=$(printf "%s\n" "$response" | _egrep_o "\"name\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \" | head -n 1)
  134. if [ "$_domain" ]; then
  135. _cutlength=$((${#domain} - ${#_domain} - 1))
  136. _sub_domain=$(printf "%s" "$domain" | cut -c "1-$_cutlength")
  137. _domain_id=$CF_Zone_ID
  138. return 0
  139. else
  140. return 1
  141. fi
  142. else
  143. return 1
  144. fi
  145. fi
  146. fi
  147. while true; do
  148. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  149. _debug h "$h"
  150. if [ -z "$h" ]; then
  151. #not valid
  152. return 1
  153. fi
  154. if [ "$CF_Account_ID" ]; then
  155. if ! _cf_rest GET "zones?name=$h&account.id=$CF_Account_ID"; then
  156. return 1
  157. fi
  158. else
  159. if ! _cf_rest GET "zones?name=$h"; then
  160. return 1
  161. fi
  162. fi
  163. if _contains "$response" "\"name\":\"$h\"" || _contains "$response" '"total_count":1'; then
  164. _domain_id=$(echo "$response" | _egrep_o "\[.\"id\":\"[^\"]*\"" | _head_n 1 | cut -d : -f 2 | tr -d \")
  165. if [ "$_domain_id" ]; then
  166. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  167. _domain=$h
  168. return 0
  169. fi
  170. return 1
  171. fi
  172. p=$i
  173. i=$(_math "$i" + 1)
  174. done
  175. return 1
  176. }
  177. _cf_rest() {
  178. m=$1
  179. ep="$2"
  180. data="$3"
  181. _debug "$ep"
  182. email_trimmed=$(echo "$CF_Email" | tr -d '"')
  183. key_trimmed=$(echo "$CF_Key" | tr -d '"')
  184. token_trimmed=$(echo "$CF_Token" | tr -d '"')
  185. export _H1="Content-Type: application/json"
  186. if [ "$token_trimmed" ]; then
  187. export _H2="Authorization: Bearer $token_trimmed"
  188. else
  189. export _H2="X-Auth-Email: $email_trimmed"
  190. export _H3="X-Auth-Key: $key_trimmed"
  191. fi
  192. if [ "$m" != "GET" ]; then
  193. _debug data "$data"
  194. response="$(_post "$data" "$CF_Api/$ep" "" "$m")"
  195. else
  196. response="$(_get "$CF_Api/$ep")"
  197. fi
  198. if [ "$?" != "0" ]; then
  199. _err "error $ep"
  200. return 1
  201. fi
  202. _debug2 response "$response"
  203. return 0
  204. }