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.

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