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.4 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
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 "$response" | grep -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 $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 "$response" | grep -o \"id\":\"[^\"]*\" | cut -d : -f 2 | tr -d \"| head -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. #################### Private functions bellow ##################################
  66. #_acme-challenge.www.domain.com
  67. #returns
  68. # _sub_domain=_acme-challenge.www
  69. # _domain=domain.com
  70. # _domain_id=sdjkglgdfewsdfg
  71. _get_root() {
  72. domain=$1
  73. i=2
  74. p=1
  75. while [ '1' ] ; do
  76. h=$(printf $domain | cut -d . -f $i-100)
  77. if [ -z "$h" ] ; then
  78. #not valid
  79. return 1;
  80. fi
  81. if ! _cf_rest GET "zones?name=$h" ; then
  82. return 1
  83. fi
  84. if printf $response | grep \"name\":\"$h\" >/dev/null ; then
  85. _domain_id=$(printf "$response" | grep -o \"id\":\"[^\"]*\" | head -1 | cut -d : -f 2 | tr -d \")
  86. if [ "$_domain_id" ] ; then
  87. _sub_domain=$(printf $domain | cut -d . -f 1-$p)
  88. _domain=$h
  89. return 0
  90. fi
  91. return 1
  92. fi
  93. p=$i
  94. i=$(expr $i + 1)
  95. done
  96. return 1
  97. }
  98. _cf_rest() {
  99. m=$1
  100. ep="$2"
  101. data="$3"
  102. _debug $ep
  103. _H1="X-Auth-Email: $CF_Email"
  104. _H2="X-Auth-Key: $CF_Key"
  105. _H3="Content-Type: application/json"
  106. if [ "$data" ] ; then
  107. _debug data "$data"
  108. response="$(_post "$data" "$CF_Api/$ep" "" $m)"
  109. else
  110. response="$(_get "$CF_Api/$ep")"
  111. fi
  112. if [ "$?" != "0" ] ; then
  113. _err "error $ep"
  114. return 1
  115. fi
  116. _debug2 response "$response"
  117. return 0
  118. }