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.

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