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.

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