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.

156 lines
3.9 KiB

  1. #!/usr/bin/env sh
  2. #CLOUDNS_AUTH_ID=XXXXX
  3. #CLOUDNS_AUTH_PASSWORD="YYYYYYYYY"
  4. CLOUDNS_API="https://api.cloudns.net"
  5. ######## Public functions #####################
  6. #Usage: dns_cloudns_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  7. dns_cloudns_add() {
  8. _info "Using cloudns"
  9. if ! _dns_cloudns_init_check; then
  10. return 1
  11. fi
  12. zone="$(_dns_cloudns_get_zone_name $1)"
  13. if [ -z "$zone" ]; then
  14. _err "Missing DNS zone at ClouDNS. Please log into your control panel and create the required DNS zone for the initial setup."
  15. return 1
  16. fi
  17. host="$(echo "$1" | sed "s/\.$zone\$//")"
  18. record=$2
  19. record_id=$(_dns_cloudns_get_record_id "$zone" "$host")
  20. _debug zone "$zone"
  21. _debug host "$host"
  22. _debug record "$record"
  23. _debug record_id "$record_id"
  24. if [ -z "$record_id" ]; then
  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. else
  33. _info "Updating the TXT record for $1"
  34. _dns_cloudns_http_api_call "dns/mod-record.json" "domain-name=$zone&record-id=$record_id&record-type=TXT&host=$host&record=$record&ttl=60"
  35. if ! _contains "$response" "\"status\":\"Success\""; then
  36. _err "The TXT record for $1 cannot be updated."
  37. return 1
  38. fi
  39. _info "Updated."
  40. fi
  41. return 0
  42. }
  43. #Usage: dns_cloudns_rm _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  44. dns_cloudns_rm() {
  45. _info "Using cloudns"
  46. if ! _dns_cloudns_init_check; then
  47. return 1
  48. fi
  49. if [ -z "$zone" ]; then
  50. zone="$(_dns_cloudns_get_zone_name $1)"
  51. if [ -z "$zone" ]; then
  52. _err "Missing DNS zone at ClouDNS. Please log into your control panel and create the required DNS zone for the initial setup."
  53. return 1
  54. fi
  55. fi
  56. host="$(echo "$1" | sed "s/\.$zone\$//")"
  57. record=$2
  58. record_id=$(_dns_cloudns_get_record_id "$zone" "$host")
  59. _debug zone "$zone"
  60. _debug host "$host"
  61. _debug record "$record"
  62. _debug record_id "$record_id"
  63. if [ ! -z "$record_id" ]; then
  64. _info "Deleting the TXT record for $1"
  65. _dns_cloudns_http_api_call "dns/delete-record.json" "domain-name=$zone&record-id="
  66. if ! _contains "$response" "\"status\":\"Success\""; then
  67. _err "The TXT record for $1 cannot be deleted."
  68. return 1
  69. fi
  70. _info "Deleted."
  71. fi
  72. return 0
  73. }
  74. #################### Private functions below ##################################
  75. _dns_cloudns_init_check() {
  76. if [ ! -z "$CLOUDNS_INIT_CHECK_COMPLETED" ]; then
  77. return 0
  78. fi
  79. if [ -z "$CLOUDNS_AUTH_ID" ]; then
  80. _err "CLOUDNS_AUTH_ID is not configured"
  81. return 1
  82. fi
  83. if [ -z "$CLOUDNS_AUTH_PASSWORD" ]; then
  84. _err "CLOUDNS_AUTH_PASSWORD is not configured"
  85. return 1
  86. fi
  87. CLOUDNS_INIT_CHECK_COMPLETED=1
  88. return 0
  89. }
  90. _dns_cloudns_get_zone_name() {
  91. i=2
  92. while true; do
  93. zoneForCheck=$(printf "%s" "$1" | cut -d . -f $i-100)
  94. if [ -z "$zoneForCheck" ]; then
  95. return 1
  96. fi
  97. _debug zoneForCheck $zoneForCheck
  98. _dns_cloudns_http_api_call "dns/get-zone-info.json" "domain-name=$zoneForCheck"
  99. if ! _contains "$response" "\"status\":\"Failed\""; then
  100. echo "$zoneForCheck"
  101. return 0
  102. fi
  103. i=$(expr $i + 1)
  104. done
  105. return 1
  106. }
  107. _dns_cloudns_get_record_id() {
  108. _dns_cloudns_http_api_call "dns/records.json" "domain-name=$1&host=$2&type=TXT"
  109. if _contains "$response" "\"id\":"; then
  110. echo "$response" | awk 'BEGIN { FS="\"" } {print $2}'
  111. return 0
  112. fi
  113. return 1
  114. }
  115. _dns_cloudns_http_api_call() {
  116. method=$1
  117. _debug CLOUDNS_AUTH_ID "$CLOUDNS_AUTH_ID"
  118. _debug CLOUDNS_AUTH_PASSWORD "$CLOUDNS_AUTH_PASSWORD"
  119. data="auth-id=$CLOUDNS_AUTH_ID&auth-password=$CLOUDNS_AUTH_PASSWORD&$2"
  120. response="$(_get "$CLOUDNS_API/$method?$data")"
  121. _debug response "$response"
  122. return 1
  123. }