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.

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