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.

151 lines
3.7 KiB

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
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, sleeping 10 seconds"
  48. sleep 10
  49. #todo: check if the record takes effect
  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, sleeping 10 seconds"
  64. sleep 10
  65. #todo: check if the record takes effect
  66. return 0
  67. fi
  68. _err "Update error"
  69. return 1
  70. fi
  71. }
  72. #fulldomain
  73. dns_cf_rm() {
  74. fulldomain=$1
  75. }
  76. #################### Private functions bellow ##################################
  77. #_acme-challenge.www.domain.com
  78. #returns
  79. # _sub_domain=_acme-challenge.www
  80. # _domain=domain.com
  81. # _domain_id=sdjkglgdfewsdfg
  82. _get_root() {
  83. domain=$1
  84. i=2
  85. p=1
  86. while true; do
  87. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  88. if [ -z "$h" ]; then
  89. #not valid
  90. return 1
  91. fi
  92. if ! _cf_rest GET "zones?name=$h"; then
  93. return 1
  94. fi
  95. if printf "%s" "$response" | grep "\"name\":\"$h\"" >/dev/null; then
  96. _domain_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":\"[^\"]*\"" | head -n 1 | cut -d : -f 2 | tr -d \")
  97. if [ "$_domain_id" ]; then
  98. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  99. _domain=$h
  100. return 0
  101. fi
  102. return 1
  103. fi
  104. p=$i
  105. i=$(_math "$i" + 1)
  106. done
  107. return 1
  108. }
  109. _cf_rest() {
  110. m=$1
  111. ep="$2"
  112. data="$3"
  113. _debug "$ep"
  114. _H1="X-Auth-Email: $CF_Email"
  115. _H2="X-Auth-Key: $CF_Key"
  116. _H3="Content-Type: application/json"
  117. if [ "$data" ]; then
  118. _debug data "$data"
  119. response="$(_post "$data" "$CF_Api/$ep" "" "$m")"
  120. else
  121. response="$(_get "$CF_Api/$ep")"
  122. fi
  123. if [ "$?" != "0" ]; then
  124. _err "error $ep"
  125. return 1
  126. fi
  127. _debug2 response "$response"
  128. return 0
  129. }