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.

157 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. # missing zone
  96. return 1;
  97. fi
  98. _debug zoneForCheck $zoneForCheck
  99. _dns_cloudns_http_api_call "dns/get-zone-info.json" "domain-name=$zoneForCheck"
  100. if ! _contains "$response" "\"status\":\"Failed\""; then
  101. echo $zoneForCheck
  102. return 0;
  103. fi
  104. i=$(expr $i + 1)
  105. done
  106. return 1;
  107. }
  108. _dns_cloudns_get_record_id() {
  109. _dns_cloudns_http_api_call "dns/records.json" "domain-name=$1&host=$2&type=TXT"
  110. if _contains "$response" "\"id\":"; then
  111. echo $response | awk 'BEGIN { FS="\"" } {print $2}'
  112. return 0
  113. fi
  114. return 1
  115. }
  116. _dns_cloudns_http_api_call () {
  117. method=$1
  118. _debug CLOUDNS_AUTH_ID "$CLOUDNS_AUTH_ID"
  119. _debug CLOUDNS_AUTH_PASSWORD "$CLOUDNS_AUTH_PASSWORD"
  120. data="auth-id=$CLOUDNS_AUTH_ID&auth-password=$CLOUDNS_AUTH_PASSWORD&$2"
  121. response="$(_get "$CLOUDNS_API/$method?$data")"
  122. _debug response "$response"
  123. return 1;
  124. }