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.

143 lines
3.5 KiB

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
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
  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. _err "You don't specify cloudflare api key and email yet."
  14. _err "Please create you key and try again."
  15. return 1
  16. fi
  17. #save the api key and email to the account conf file.
  18. _saveaccountconf CF_Key "$CF_Key"
  19. _saveaccountconf CF_Email "$CF_Email"
  20. _debug "First detect the root zone"
  21. if ! _get_root $fulldomain; then
  22. _err "invalid domain"
  23. return 1
  24. fi
  25. _debug _domain_id "$_domain_id"
  26. _debug _sub_domain "$_sub_domain"
  27. _debug _domain "$_domain"
  28. _debug "Getting txt records"
  29. _cf_rest GET "zones/${_domain_id}/dns_records?type=TXT&name=$fulldomain"
  30. if ! printf "$response" | grep \"success\":true >/dev/null; then
  31. _err "Error"
  32. return 1
  33. fi
  34. count=$(printf "%s\n" "$response" | _egrep_o \"count\":[^,]* | cut -d : -f 2)
  35. _debug count "$count"
  36. if [ "$count" = "0" ]; then
  37. _info "Adding record"
  38. if _cf_rest POST "zones/$_domain_id/dns_records" "{\"type\":\"TXT\",\"name\":\"$fulldomain\",\"content\":\"$txtvalue\",\"ttl\":120}"; then
  39. if printf -- "%s" "$response" | grep $fulldomain >/dev/null; then
  40. _info "Added, sleeping 10 seconds"
  41. sleep 10
  42. #todo: check if the record takes effect
  43. return 0
  44. else
  45. _err "Add txt record error."
  46. return 1
  47. fi
  48. fi
  49. _err "Add txt record error."
  50. else
  51. _info "Updating record"
  52. record_id=$(printf "%s\n" "$response" | _egrep_o \"id\":\"[^\"]*\" | cut -d : -f 2 | tr -d \" | head -n 1)
  53. _debug "record_id" $record_id
  54. _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\"}"
  55. if [ "$?" = "0" ]; then
  56. _info "Updated, sleeping 10 seconds"
  57. sleep 10
  58. #todo: check if the record takes effect
  59. return 0
  60. fi
  61. _err "Update error"
  62. return 1
  63. fi
  64. }
  65. #fulldomain
  66. dns_cf_rm() {
  67. fulldomain=$1
  68. }
  69. #################### Private functions bellow ##################################
  70. #_acme-challenge.www.domain.com
  71. #returns
  72. # _sub_domain=_acme-challenge.www
  73. # _domain=domain.com
  74. # _domain_id=sdjkglgdfewsdfg
  75. _get_root() {
  76. domain=$1
  77. i=2
  78. p=1
  79. while [ '1' ]; do
  80. h=$(printf $domain | cut -d . -f $i-100)
  81. if [ -z "$h" ]; then
  82. #not valid
  83. return 1
  84. fi
  85. if ! _cf_rest GET "zones?name=$h"; then
  86. return 1
  87. fi
  88. if printf $response | grep \"name\":\"$h\" >/dev/null; then
  89. _domain_id=$(printf "%s\n" "$response" | _egrep_o \"id\":\"[^\"]*\" | head -n 1 | cut -d : -f 2 | tr -d \")
  90. if [ "$_domain_id" ]; then
  91. _sub_domain=$(printf $domain | cut -d . -f 1-$p)
  92. _domain=$h
  93. return 0
  94. fi
  95. return 1
  96. fi
  97. p=$i
  98. i=$(expr $i + 1)
  99. done
  100. return 1
  101. }
  102. _cf_rest() {
  103. m=$1
  104. ep="$2"
  105. data="$3"
  106. _debug $ep
  107. _H1="X-Auth-Email: $CF_Email"
  108. _H2="X-Auth-Key: $CF_Key"
  109. _H3="Content-Type: application/json"
  110. if [ "$data" ]; then
  111. _debug data "$data"
  112. response="$(_post "$data" "$CF_Api/$ep" "" $m)"
  113. else
  114. response="$(_get "$CF_Api/$ep")"
  115. fi
  116. if [ "$?" != "0" ]; then
  117. _err "error $ep"
  118. return 1
  119. fi
  120. _debug2 response "$response"
  121. return 0
  122. }