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.

199 lines
5.3 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 don't specify cloudflare api key and email yet."
  18. _err "Please create you 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. _debug "First detect the root zone"
  30. if ! _get_root "$fulldomain"; then
  31. _err "invalid domain"
  32. return 1
  33. fi
  34. _debug _domain_id "$_domain_id"
  35. _debug _sub_domain "$_sub_domain"
  36. _debug _domain "$_domain"
  37. _debug "Getting txt records"
  38. _cf_rest GET "zones/${_domain_id}/dns_records?type=TXT&name=$fulldomain"
  39. if ! printf "%s" "$response" | grep \"success\":true >/dev/null; then
  40. _err "Error"
  41. return 1
  42. fi
  43. # For wildcard cert, the main root domain and the wildcard domain have the same txt subdomain name, so
  44. # we can not use updating anymore.
  45. # count=$(printf "%s\n" "$response" | _egrep_o "\"count\":[^,]*" | cut -d : -f 2)
  46. # _debug count "$count"
  47. # if [ "$count" = "0" ]; then
  48. _info "Adding record"
  49. if _cf_rest POST "zones/$_domain_id/dns_records" "{\"type\":\"TXT\",\"name\":\"$fulldomain\",\"content\":\"$txtvalue\",\"ttl\":120}"; then
  50. if printf -- "%s" "$response" | grep "$fulldomain" >/dev/null; then
  51. _info "Added, OK"
  52. return 0
  53. else
  54. _err "Add txt record error."
  55. return 1
  56. fi
  57. fi
  58. _err "Add txt record error."
  59. return 1
  60. # else
  61. # _info "Updating record"
  62. # record_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \" | head -n 1)
  63. # _debug "record_id" "$record_id"
  64. #
  65. # _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\"}"
  66. # if [ "$?" = "0" ]; then
  67. # _info "Updated, OK"
  68. # return 0
  69. # fi
  70. # _err "Update error"
  71. # return 1
  72. # fi
  73. }
  74. #fulldomain txtvalue
  75. dns_cf_rm() {
  76. fulldomain=$1
  77. txtvalue=$2
  78. CF_Key="${CF_Key:-$(_readaccountconf_mutable CF_Key)}"
  79. CF_Email="${CF_Email:-$(_readaccountconf_mutable CF_Email)}"
  80. if [ -z "$CF_Key" ] || [ -z "$CF_Email" ]; then
  81. CF_Key=""
  82. CF_Email=""
  83. _err "You don't specify cloudflare api key and email yet."
  84. _err "Please create you key and try again."
  85. return 1
  86. fi
  87. _debug "First detect the root zone"
  88. if ! _get_root "$fulldomain"; then
  89. _err "invalid domain"
  90. return 1
  91. fi
  92. _debug _domain_id "$_domain_id"
  93. _debug _sub_domain "$_sub_domain"
  94. _debug _domain "$_domain"
  95. _debug "Getting txt records"
  96. _cf_rest GET "zones/${_domain_id}/dns_records?type=TXT&name=$fulldomain&content=$txtvalue"
  97. if ! printf "%s" "$response" | grep \"success\":true >/dev/null; then
  98. _err "Error"
  99. return 1
  100. fi
  101. count=$(printf "%s\n" "$response" | _egrep_o "\"count\":[^,]*" | cut -d : -f 2)
  102. _debug count "$count"
  103. if [ "$count" = "0" ]; then
  104. _info "Don't need to remove."
  105. else
  106. record_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \" | head -n 1)
  107. _debug "record_id" "$record_id"
  108. if [ -z "$record_id" ]; then
  109. _err "Can not get record id to remove."
  110. return 1
  111. fi
  112. if ! _cf_rest DELETE "zones/$_domain_id/dns_records/$record_id"; then
  113. _err "Delete record error."
  114. return 1
  115. fi
  116. _contains "$response" '"success":true'
  117. fi
  118. }
  119. #################### Private functions below ##################################
  120. #_acme-challenge.www.domain.com
  121. #returns
  122. # _sub_domain=_acme-challenge.www
  123. # _domain=domain.com
  124. # _domain_id=sdjkglgdfewsdfg
  125. _get_root() {
  126. domain=$1
  127. i=2
  128. p=1
  129. while true; do
  130. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  131. _debug h "$h"
  132. if [ -z "$h" ]; then
  133. #not valid
  134. return 1
  135. fi
  136. if ! _cf_rest GET "zones?name=$h"; then
  137. return 1
  138. fi
  139. if _contains "$response" "\"name\":\"$h\"" >/dev/null; then
  140. _domain_id=$(printf "%s\n" "$response" | _egrep_o "\[.\"id\":\"[^\"]*\"" | head -n 1 | cut -d : -f 2 | tr -d \")
  141. if [ "$_domain_id" ]; then
  142. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  143. _domain=$h
  144. return 0
  145. fi
  146. return 1
  147. fi
  148. p=$i
  149. i=$(_math "$i" + 1)
  150. done
  151. return 1
  152. }
  153. _cf_rest() {
  154. m=$1
  155. ep="$2"
  156. data="$3"
  157. _debug "$ep"
  158. export _H1="X-Auth-Email: $CF_Email"
  159. export _H2="X-Auth-Key: $CF_Key"
  160. export _H3="Content-Type: application/json"
  161. if [ "$m" != "GET" ]; then
  162. _debug data "$data"
  163. response="$(_post "$data" "$CF_Api/$ep" "" "$m")"
  164. else
  165. response="$(_get "$CF_Api/$ep")"
  166. fi
  167. if [ "$?" != "0" ]; then
  168. _err "error $ep"
  169. return 1
  170. fi
  171. _debug2 response "$response"
  172. return 0
  173. }