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.

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