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.

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