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.

184 lines
5.0 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. _dns_cloudns_http_api_call "dns/records.json" "domain-name=$zone&host=$host&type=TXT"
  50. if ! _contains "$response" "\"id\":"; then
  51. return 1
  52. fi
  53. for i in $(echo "$response" | tr '{' "\n" | grep "$record"); do
  54. record_id=$(echo "$i" | tr ',' "\n" | grep -E '^"id"' | sed -re 's/^\"id\"\:\"([0-9]+)\"$/\1/g')
  55. if [ ! -z "$record_id" ]; then
  56. _debug zone "$zone"
  57. _debug host "$host"
  58. _debug record "$record"
  59. _debug record_id "$record_id"
  60. _info "Deleting the TXT record for $1"
  61. _dns_cloudns_http_api_call "dns/delete-record.json" "domain-name=$zone&record-id=$record_id"
  62. if ! _contains "$response" "\"status\":\"Success\""; then
  63. _err "The TXT record for $1 cannot be deleted."
  64. else
  65. _info "Deleted."
  66. fi
  67. fi
  68. done
  69. return 0
  70. }
  71. #################### Private functions below ##################################
  72. _dns_cloudns_init_check() {
  73. if [ ! -z "$CLOUDNS_INIT_CHECK_COMPLETED" ]; then
  74. return 0
  75. fi
  76. CLOUDNS_AUTH_ID="${CLOUDNS_AUTH_ID:-$(_readaccountconf_mutable CLOUDNS_AUTH_ID)}"
  77. CLOUDNS_SUB_AUTH_ID="${CLOUDNS_SUB_AUTH_ID:-$(_readaccountconf_mutable CLOUDNS_SUB_AUTH_ID)}"
  78. CLOUDNS_AUTH_PASSWORD="${CLOUDNS_AUTH_PASSWORD:-$(_readaccountconf_mutable CLOUDNS_AUTH_PASSWORD)}"
  79. if [ -z "$CLOUDNS_AUTH_ID$CLOUDNS_SUB_AUTH_ID" ] || [ -z "$CLOUDNS_AUTH_PASSWORD" ]; then
  80. CLOUDNS_AUTH_ID=""
  81. CLOUDNS_SUB_AUTH_ID=""
  82. CLOUDNS_AUTH_PASSWORD=""
  83. _err "You don't specify cloudns api id and password yet."
  84. _err "Please create you id and password and try again."
  85. return 1
  86. fi
  87. if [ -z "$CLOUDNS_AUTH_ID" ] && [ -z "$CLOUDNS_SUB_AUTH_ID" ]; then
  88. _err "CLOUDNS_AUTH_ID or CLOUDNS_SUB_AUTH_ID is not configured"
  89. return 1
  90. fi
  91. if [ -z "$CLOUDNS_AUTH_PASSWORD" ]; then
  92. _err "CLOUDNS_AUTH_PASSWORD is not configured"
  93. return 1
  94. fi
  95. _dns_cloudns_http_api_call "dns/login.json" ""
  96. if ! _contains "$response" "\"status\":\"Success\""; then
  97. _err "Invalid CLOUDNS_AUTH_ID or CLOUDNS_AUTH_PASSWORD. Please check your login credentials."
  98. return 1
  99. fi
  100. # save the api id and password to the account conf file.
  101. _saveaccountconf_mutable CLOUDNS_AUTH_ID "$CLOUDNS_AUTH_ID"
  102. _saveaccountconf_mutable CLOUDNS_SUB_AUTH_ID "$CLOUDNS_SUB_AUTH_ID"
  103. _saveaccountconf_mutable CLOUDNS_AUTH_PASSWORD "$CLOUDNS_AUTH_PASSWORD"
  104. CLOUDNS_INIT_CHECK_COMPLETED=1
  105. return 0
  106. }
  107. _dns_cloudns_get_zone_name() {
  108. i=2
  109. while true; do
  110. zoneForCheck=$(printf "%s" "$1" | cut -d . -f $i-100)
  111. if [ -z "$zoneForCheck" ]; then
  112. return 1
  113. fi
  114. _debug zoneForCheck "$zoneForCheck"
  115. _dns_cloudns_http_api_call "dns/get-zone-info.json" "domain-name=$zoneForCheck"
  116. if ! _contains "$response" "\"status\":\"Failed\""; then
  117. echo "$zoneForCheck"
  118. return 0
  119. fi
  120. i=$(_math "$i" + 1)
  121. done
  122. return 1
  123. }
  124. _dns_cloudns_http_api_call() {
  125. method=$1
  126. _debug CLOUDNS_AUTH_ID "$CLOUDNS_AUTH_ID"
  127. _debug CLOUDNS_SUB_AUTH_ID "$CLOUDNS_SUB_AUTH_ID"
  128. _debug CLOUDNS_AUTH_PASSWORD "$CLOUDNS_AUTH_PASSWORD"
  129. if [ ! -z "$CLOUDNS_SUB_AUTH_ID" ]; then
  130. auth_user="sub-auth-id=$CLOUDNS_SUB_AUTH_ID"
  131. else
  132. auth_user="auth-id=$CLOUDNS_AUTH_ID"
  133. fi
  134. if [ -z "$2" ]; then
  135. data="$auth_user&auth-password=$CLOUDNS_AUTH_PASSWORD"
  136. else
  137. data="$auth_user&auth-password=$CLOUDNS_AUTH_PASSWORD&$2"
  138. fi
  139. response="$(_get "$CLOUDNS_API/$method?$data")"
  140. _debug response "$response"
  141. return 0
  142. }