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.

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