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.

211 lines
5.8 KiB

4 years ago
4 years ago
4 years ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_cloudns_info='ClouDNS.net
  4. Site: ClouDNS.net
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_cloudns
  6. Options:
  7. CLOUDNS_AUTH_ID Regular auth ID
  8. CLOUDNS_SUB_AUTH_ID Sub auth ID
  9. CLOUDNS_AUTH_PASSWORD Auth Password
  10. Author: Boyan Peychev <boyan@cloudns.net>
  11. '
  12. CLOUDNS_API="https://api.cloudns.net"
  13. DOMAIN_TYPE=
  14. DOMAIN_MASTER=
  15. ######## Public functions #####################
  16. #Usage: dns_cloudns_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  17. dns_cloudns_add() {
  18. _info "Using cloudns"
  19. if ! _dns_cloudns_init_check; then
  20. return 1
  21. fi
  22. zone="$(_dns_cloudns_get_zone_name "$1")"
  23. if [ -z "$zone" ]; then
  24. _err "Missing DNS zone at ClouDNS. Please log into your control panel and create the required DNS zone for the initial setup."
  25. return 1
  26. fi
  27. host="$(echo "$1" | sed "s/\.$zone\$//")"
  28. record=$2
  29. _debug zone "$zone"
  30. _debug host "$host"
  31. _debug record "$record"
  32. _info "Adding the TXT record for $1"
  33. _dns_cloudns_http_api_call "dns/add-record.json" "domain-name=$zone&record-type=TXT&host=$host&record=$record&ttl=60"
  34. if ! _contains "$response" "\"status\":\"Success\""; then
  35. _err "Record cannot be added."
  36. return 1
  37. fi
  38. _info "Added."
  39. return 0
  40. }
  41. #Usage: dns_cloudns_rm _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  42. dns_cloudns_rm() {
  43. _info "Using cloudns"
  44. if ! _dns_cloudns_init_check; then
  45. return 1
  46. fi
  47. if [ -z "$zone" ]; then
  48. zone="$(_dns_cloudns_get_zone_name "$1")"
  49. if [ -z "$zone" ]; then
  50. _err "Missing DNS zone at ClouDNS. Please log into your control panel and create the required DNS zone for the initial setup."
  51. return 1
  52. fi
  53. fi
  54. host="$(echo "$1" | sed "s/\.$zone\$//")"
  55. record=$2
  56. _dns_cloudns_get_zone_info "$zone"
  57. _debug "Type" "$DOMAIN_TYPE"
  58. _debug "Cloud Master" "$DOMAIN_MASTER"
  59. if _contains "$DOMAIN_TYPE" "cloud"; then
  60. zone=$DOMAIN_MASTER
  61. fi
  62. _debug "ZONE" "$zone"
  63. _dns_cloudns_http_api_call "dns/records.json" "domain-name=$zone&host=$host&type=TXT"
  64. if ! _contains "$response" "\"id\":"; then
  65. return 1
  66. fi
  67. for i in $(echo "$response" | tr '{' "\n" | grep -- "$record"); do
  68. record_id=$(echo "$i" | tr ',' "\n" | grep -E '^"id"' | sed -re 's/^\"id\"\:\"([0-9]+)\"$/\1/g')
  69. if [ -n "$record_id" ]; then
  70. _debug zone "$zone"
  71. _debug host "$host"
  72. _debug record "$record"
  73. _debug record_id "$record_id"
  74. _info "Deleting the TXT record for $1"
  75. _dns_cloudns_http_api_call "dns/delete-record.json" "domain-name=$zone&record-id=$record_id"
  76. if ! _contains "$response" "\"status\":\"Success\""; then
  77. _err "The TXT record for $1 cannot be deleted."
  78. else
  79. _info "Deleted."
  80. fi
  81. fi
  82. done
  83. return 0
  84. }
  85. #################### Private functions below ##################################
  86. _dns_cloudns_init_check() {
  87. if [ -n "$CLOUDNS_INIT_CHECK_COMPLETED" ]; then
  88. return 0
  89. fi
  90. CLOUDNS_AUTH_ID="${CLOUDNS_AUTH_ID:-$(_readaccountconf_mutable CLOUDNS_AUTH_ID)}"
  91. CLOUDNS_SUB_AUTH_ID="${CLOUDNS_SUB_AUTH_ID:-$(_readaccountconf_mutable CLOUDNS_SUB_AUTH_ID)}"
  92. CLOUDNS_AUTH_PASSWORD="${CLOUDNS_AUTH_PASSWORD:-$(_readaccountconf_mutable CLOUDNS_AUTH_PASSWORD)}"
  93. if [ -z "$CLOUDNS_AUTH_ID$CLOUDNS_SUB_AUTH_ID" ] || [ -z "$CLOUDNS_AUTH_PASSWORD" ]; then
  94. CLOUDNS_AUTH_ID=""
  95. CLOUDNS_SUB_AUTH_ID=""
  96. CLOUDNS_AUTH_PASSWORD=""
  97. _err "You don't specify cloudns api id and password yet."
  98. _err "Please create you id and password and try again."
  99. return 1
  100. fi
  101. if [ -z "$CLOUDNS_AUTH_ID" ] && [ -z "$CLOUDNS_SUB_AUTH_ID" ]; then
  102. _err "CLOUDNS_AUTH_ID or CLOUDNS_SUB_AUTH_ID is not configured"
  103. return 1
  104. fi
  105. if [ -z "$CLOUDNS_AUTH_PASSWORD" ]; then
  106. _err "CLOUDNS_AUTH_PASSWORD is not configured"
  107. return 1
  108. fi
  109. _dns_cloudns_http_api_call "dns/login.json" ""
  110. if ! _contains "$response" "\"status\":\"Success\""; then
  111. _err "Invalid CLOUDNS_AUTH_ID or CLOUDNS_AUTH_PASSWORD. Please check your login credentials."
  112. return 1
  113. fi
  114. # save the api id and password to the account conf file.
  115. _saveaccountconf_mutable CLOUDNS_AUTH_ID "$CLOUDNS_AUTH_ID"
  116. _saveaccountconf_mutable CLOUDNS_SUB_AUTH_ID "$CLOUDNS_SUB_AUTH_ID"
  117. _saveaccountconf_mutable CLOUDNS_AUTH_PASSWORD "$CLOUDNS_AUTH_PASSWORD"
  118. CLOUDNS_INIT_CHECK_COMPLETED=1
  119. return 0
  120. }
  121. _dns_cloudns_get_zone_info() {
  122. zone=$1
  123. _dns_cloudns_http_api_call "dns/get-zone-info.json" "domain-name=$zone"
  124. if ! _contains "$response" "\"status\":\"Failed\""; then
  125. DOMAIN_TYPE=$(echo "$response" | _egrep_o '"type":"[^"]*"' | cut -d : -f 2 | tr -d '"')
  126. if _contains "$DOMAIN_TYPE" "cloud"; then
  127. DOMAIN_MASTER=$(echo "$response" | _egrep_o '"cloud-master":"[^"]*"' | cut -d : -f 2 | tr -d '"')
  128. fi
  129. fi
  130. return 0
  131. }
  132. _dns_cloudns_get_zone_name() {
  133. i=2
  134. while true; do
  135. zoneForCheck=$(printf "%s" "$1" | cut -d . -f $i-100)
  136. if [ -z "$zoneForCheck" ]; then
  137. return 1
  138. fi
  139. _debug zoneForCheck "$zoneForCheck"
  140. _dns_cloudns_http_api_call "dns/get-zone-info.json" "domain-name=$zoneForCheck"
  141. if ! _contains "$response" "\"status\":\"Failed\""; then
  142. echo "$zoneForCheck"
  143. return 0
  144. fi
  145. i=$(_math "$i" + 1)
  146. done
  147. return 1
  148. }
  149. _dns_cloudns_http_api_call() {
  150. method=$1
  151. _debug CLOUDNS_AUTH_ID "$CLOUDNS_AUTH_ID"
  152. _debug CLOUDNS_SUB_AUTH_ID "$CLOUDNS_SUB_AUTH_ID"
  153. _debug CLOUDNS_AUTH_PASSWORD "$CLOUDNS_AUTH_PASSWORD"
  154. if [ -n "$CLOUDNS_SUB_AUTH_ID" ]; then
  155. auth_user="sub-auth-id=$CLOUDNS_SUB_AUTH_ID"
  156. else
  157. auth_user="auth-id=$CLOUDNS_AUTH_ID"
  158. fi
  159. if [ -z "$2" ]; then
  160. data="$auth_user&auth-password=$CLOUDNS_AUTH_PASSWORD"
  161. else
  162. data="$auth_user&auth-password=$CLOUDNS_AUTH_PASSWORD&$2"
  163. fi
  164. response="$(_get "$CLOUDNS_API/$method?$data")"
  165. _debug response "$response"
  166. return 0
  167. }