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.

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