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.

184 lines
5.0 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. CLOUDNS_AUTH_ID="${CLOUDNS_AUTH_ID:-$(_readaccountconf_mutable CLOUDNS_AUTH_ID)}"
  82. CLOUDNS_AUTH_PASSWORD="${CLOUDNS_AUTH_PASSWORD:-$(_readaccountconf_mutable CLOUDNS_AUTH_PASSWORD)}"
  83. if [ -z "$CLOUDNS_AUTH_ID" ] || [ -z "$CLOUDNS_AUTH_PASSWORD" ]; then
  84. CLOUDNS_AUTH_ID=""
  85. CLOUDNS_AUTH_PASSWORD=""
  86. _err "You don't specify cloudns api id and password yet."
  87. _err "Please create you id and password and try again."
  88. return 1
  89. fi
  90. if [ -z "$CLOUDNS_AUTH_ID" ]; then
  91. _err "CLOUDNS_AUTH_ID is not configured"
  92. return 1
  93. fi
  94. if [ -z "$CLOUDNS_AUTH_PASSWORD" ]; then
  95. _err "CLOUDNS_AUTH_PASSWORD is not configured"
  96. return 1
  97. fi
  98. _dns_cloudns_http_api_call "dns/login.json" ""
  99. if ! _contains "$response" "\"status\":\"Success\""; then
  100. _err "Invalid CLOUDNS_AUTH_ID or CLOUDNS_AUTH_PASSWORD. Please check your login credentials."
  101. return 1
  102. fi
  103. #save the api id and password to the account conf file.
  104. _saveaccountconf_mutable CLOUDNS_AUTH_ID "$CLOUDNS_AUTH_ID"
  105. _saveaccountconf_mutable CLOUDNS_AUTH_PASSWORD "$CLOUDNS_AUTH_PASSWORD"
  106. CLOUDNS_INIT_CHECK_COMPLETED=1
  107. return 0
  108. }
  109. _dns_cloudns_get_zone_name() {
  110. i=2
  111. while true; do
  112. zoneForCheck=$(printf "%s" "$1" | cut -d . -f $i-100)
  113. if [ -z "$zoneForCheck" ]; then
  114. return 1
  115. fi
  116. _debug zoneForCheck "$zoneForCheck"
  117. _dns_cloudns_http_api_call "dns/get-zone-info.json" "domain-name=$zoneForCheck"
  118. if ! _contains "$response" "\"status\":\"Failed\""; then
  119. echo "$zoneForCheck"
  120. return 0
  121. fi
  122. i=$(_math "$i" + 1)
  123. done
  124. return 1
  125. }
  126. _dns_cloudns_get_record_id() {
  127. _dns_cloudns_http_api_call "dns/records.json" "domain-name=$1&host=$2&type=TXT"
  128. if _contains "$response" "\"id\":"; then
  129. echo "$response" | cut -d '"' -f 2
  130. return 0
  131. fi
  132. return 1
  133. }
  134. _dns_cloudns_http_api_call() {
  135. method=$1
  136. _debug CLOUDNS_AUTH_ID "$CLOUDNS_AUTH_ID"
  137. _debug CLOUDNS_AUTH_PASSWORD "$CLOUDNS_AUTH_PASSWORD"
  138. if [ -z "$2" ]; then
  139. data="auth-id=$CLOUDNS_AUTH_ID&auth-password=$CLOUDNS_AUTH_PASSWORD"
  140. else
  141. data="auth-id=$CLOUDNS_AUTH_ID&auth-password=$CLOUDNS_AUTH_PASSWORD&$2"
  142. fi
  143. response="$(_get "$CLOUDNS_API/$method?$data")"
  144. _debug2 response "$response"
  145. return 0
  146. }