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
6.0 KiB

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