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.

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