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.

139 lines
2.8 KiB

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