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.

208 lines
5.7 KiB

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