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.

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