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.

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