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.

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