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.

221 lines
6.1 KiB

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