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.

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