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.

170 lines
4.3 KiB

  1. #!/usr/bin/env sh
  2. # Author: Boyan Peychev <boyan at cloudns dot net>
  3. # Repository: https://github.com/ClouDNS/acme.sh/
  4. #CLOUDNS_AUTH_ID=XXXXX
  5. #CLOUDNS_AUTH_PASSWORD="YYYYYYYYY"
  6. CLOUDNS_API="https://api.cloudns.net"
  7. ######## Public functions #####################
  8. #Usage: dns_cloudns_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  9. dns_cloudns_add() {
  10. _info "Using cloudns"
  11. if ! _dns_cloudns_init_check; then
  12. return 1
  13. fi
  14. zone="$(_dns_cloudns_get_zone_name "$1")"
  15. if [ -z "$zone" ]; then
  16. _err "Missing DNS zone at ClouDNS. Please log into your control panel and create the required DNS zone for the initial setup."
  17. return 1
  18. fi
  19. host="$(echo "$1" | sed "s/\.$zone\$//")"
  20. record=$2
  21. record_id=$(_dns_cloudns_get_record_id "$zone" "$host")
  22. _debug zone "$zone"
  23. _debug host "$host"
  24. _debug record "$record"
  25. _debug record_id "$record_id"
  26. if [ -z "$record_id" ]; then
  27. _info "Adding the TXT record for $1"
  28. _dns_cloudns_http_api_call "dns/add-record.json" "domain-name=$zone&record-type=TXT&host=$host&record=$record&ttl=60"
  29. if ! _contains "$response" "\"status\":\"Success\""; then
  30. _err "Record cannot be added."
  31. return 1
  32. fi
  33. _info "Added."
  34. else
  35. _info "Updating the TXT record for $1"
  36. _dns_cloudns_http_api_call "dns/mod-record.json" "domain-name=$zone&record-id=$record_id&record-type=TXT&host=$host&record=$record&ttl=60"
  37. if ! _contains "$response" "\"status\":\"Success\""; then
  38. _err "The TXT record for $1 cannot be updated."
  39. return 1
  40. fi
  41. _info "Updated."
  42. fi
  43. return 0
  44. }
  45. #Usage: dns_cloudns_rm _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  46. dns_cloudns_rm() {
  47. _info "Using cloudns"
  48. if ! _dns_cloudns_init_check; then
  49. return 1
  50. fi
  51. if [ -z "$zone" ]; then
  52. zone="$(_dns_cloudns_get_zone_name "$1")"
  53. if [ -z "$zone" ]; then
  54. _err "Missing DNS zone at ClouDNS. Please log into your control panel and create the required DNS zone for the initial setup."
  55. return 1
  56. fi
  57. fi
  58. host="$(echo "$1" | sed "s/\.$zone\$//")"
  59. record=$2
  60. record_id=$(_dns_cloudns_get_record_id "$zone" "$host")
  61. _debug zone "$zone"
  62. _debug host "$host"
  63. _debug record "$record"
  64. _debug record_id "$record_id"
  65. if [ ! -z "$record_id" ]; then
  66. _info "Deleting the TXT record for $1"
  67. _dns_cloudns_http_api_call "dns/delete-record.json" "domain-name=$zone&record-id=$record_id"
  68. if ! _contains "$response" "\"status\":\"Success\""; then
  69. _err "The TXT record for $1 cannot be deleted."
  70. return 1
  71. fi
  72. _info "Deleted."
  73. fi
  74. return 0
  75. }
  76. #################### Private functions below ##################################
  77. _dns_cloudns_init_check() {
  78. if [ ! -z "$CLOUDNS_INIT_CHECK_COMPLETED" ]; then
  79. return 0
  80. fi
  81. if [ -z "$CLOUDNS_AUTH_ID" ]; then
  82. _err "CLOUDNS_AUTH_ID is not configured"
  83. return 1
  84. fi
  85. if [ -z "$CLOUDNS_AUTH_PASSWORD" ]; then
  86. _err "CLOUDNS_AUTH_PASSWORD is not configured"
  87. return 1
  88. fi
  89. _dns_cloudns_http_api_call "dns/login.json" ""
  90. if ! _contains "$response" "\"status\":\"Success\""; then
  91. _err "Invalid CLOUDNS_AUTH_ID or CLOUDNS_AUTH_PASSWORD. Please check your login credentials."
  92. return 1
  93. fi
  94. CLOUDNS_INIT_CHECK_COMPLETED=1
  95. return 0
  96. }
  97. _dns_cloudns_get_zone_name() {
  98. i=2
  99. while true; do
  100. zoneForCheck=$(printf "%s" "$1" | cut -d . -f $i-100)
  101. if [ -z "$zoneForCheck" ]; then
  102. return 1
  103. fi
  104. _debug zoneForCheck "$zoneForCheck"
  105. _dns_cloudns_http_api_call "dns/get-zone-info.json" "domain-name=$zoneForCheck"
  106. if ! _contains "$response" "\"status\":\"Failed\""; then
  107. echo "$zoneForCheck"
  108. return 0
  109. fi
  110. i=$(_math "$i" + 1)
  111. done
  112. return 1
  113. }
  114. _dns_cloudns_get_record_id() {
  115. _dns_cloudns_http_api_call "dns/records.json" "domain-name=$1&host=$2&type=TXT"
  116. if _contains "$response" "\"id\":"; then
  117. echo "$response" | cut -d '"' -f 2
  118. return 0
  119. fi
  120. return 1
  121. }
  122. _dns_cloudns_http_api_call() {
  123. method=$1
  124. _debug CLOUDNS_AUTH_ID "$CLOUDNS_AUTH_ID"
  125. _debug CLOUDNS_AUTH_PASSWORD "$CLOUDNS_AUTH_PASSWORD"
  126. if [ -z "$2" ]; then
  127. data="auth-id=$CLOUDNS_AUTH_ID&auth-password=$CLOUDNS_AUTH_PASSWORD"
  128. else
  129. data="auth-id=$CLOUDNS_AUTH_ID&auth-password=$CLOUDNS_AUTH_PASSWORD&$2"
  130. fi
  131. response="$(_get "$CLOUDNS_API/$method?$data")"
  132. _debug2 response "$response"
  133. return 0
  134. }