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.

184 lines
4.7 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
9 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
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, sleeping 10 seconds"
  62. sleep 10
  63. #todo: check if the record takes effect
  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 bellow ##################################
  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. if [ -z "$h" ]; then
  119. #not valid
  120. return 1
  121. fi
  122. if ! _cf_rest GET "zones?name=$h"; then
  123. return 1
  124. fi
  125. if printf "%s" "$response" | grep "\"name\":\"$h\"" >/dev/null; then
  126. _domain_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":\"[^\"]*\"" | head -n 1 | cut -d : -f 2 | tr -d \")
  127. if [ "$_domain_id" ]; then
  128. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  129. _domain=$h
  130. return 0
  131. fi
  132. return 1
  133. fi
  134. p=$i
  135. i=$(_math "$i" + 1)
  136. done
  137. return 1
  138. }
  139. _cf_rest() {
  140. m=$1
  141. ep="$2"
  142. data="$3"
  143. _debug "$ep"
  144. _H1="X-Auth-Email: $CF_Email"
  145. _H2="X-Auth-Key: $CF_Key"
  146. _H3="Content-Type: application/json"
  147. if [ "$m" != "GET" ]; then
  148. _debug data "$data"
  149. response="$(_post "$data" "$CF_Api/$ep" "" "$m")"
  150. else
  151. response="$(_get "$CF_Api/$ep")"
  152. fi
  153. if [ "$?" != "0" ]; then
  154. _err "error $ep"
  155. return 1
  156. fi
  157. _debug2 response "$response"
  158. return 0
  159. }