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.

185 lines
4.8 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. _debug "First detect the root zone"
  75. if ! _get_root "$fulldomain"; then
  76. _err "invalid domain"
  77. return 1
  78. fi
  79. _debug _domain_id "$_domain_id"
  80. _debug _sub_domain "$_sub_domain"
  81. _debug _domain "$_domain"
  82. _debug "Getting txt records"
  83. _cf_rest GET "zones/${_domain_id}/dns_records?type=TXT&name=$fulldomain&content=$txtvalue"
  84. if ! printf "%s" "$response" | grep \"success\":true >/dev/null; then
  85. _err "Error"
  86. return 1
  87. fi
  88. count=$(printf "%s\n" "$response" | _egrep_o "\"count\":[^,]*" | cut -d : -f 2)
  89. _debug count "$count"
  90. if [ "$count" = "0" ]; then
  91. _info "Don't need to remove."
  92. else
  93. record_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \" | head -n 1)
  94. _debug "record_id" "$record_id"
  95. if [ -z "$record_id" ]; then
  96. _err "Can not get record id to remove."
  97. return 1
  98. fi
  99. if ! _cf_rest DELETE "zones/$_domain_id/dns_records/$record_id"; then
  100. _err "Delete record error."
  101. return 1
  102. fi
  103. _contains "$response" '"success":true'
  104. fi
  105. }
  106. #################### Private functions below ##################################
  107. #_acme-challenge.www.domain.com
  108. #returns
  109. # _sub_domain=_acme-challenge.www
  110. # _domain=domain.com
  111. # _domain_id=sdjkglgdfewsdfg
  112. _get_root() {
  113. domain=$1
  114. i=2
  115. p=1
  116. while true; do
  117. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  118. _debug h "$h"
  119. if [ -z "$h" ]; then
  120. #not valid
  121. return 1
  122. fi
  123. if ! _cf_rest GET "zones?name=$h"; then
  124. return 1
  125. fi
  126. if _contains "$response" "\"name\":\"$h\"" >/dev/null; then
  127. _domain_id=$(printf "%s\n" "$response" | _egrep_o "\[.\"id\":\"[^\"]*\"" | head -n 1 | cut -d : -f 2 | tr -d \")
  128. if [ "$_domain_id" ]; then
  129. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  130. _domain=$h
  131. return 0
  132. fi
  133. return 1
  134. fi
  135. p=$i
  136. i=$(_math "$i" + 1)
  137. done
  138. return 1
  139. }
  140. _cf_rest() {
  141. m=$1
  142. ep="$2"
  143. data="$3"
  144. _debug "$ep"
  145. export _H1="X-Auth-Email: $CF_Email"
  146. export _H2="X-Auth-Key: $CF_Key"
  147. export _H3="Content-Type: application/json"
  148. if [ "$m" != "GET" ]; then
  149. _debug data "$data"
  150. response="$(_post "$data" "$CF_Api/$ep" "" "$m")"
  151. else
  152. response="$(_get "$CF_Api/$ep")"
  153. fi
  154. if [ "$?" != "0" ]; then
  155. _err "error $ep"
  156. return 1
  157. fi
  158. _debug2 response "$response"
  159. return 0
  160. }