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.

151 lines
3.7 KiB

1 month ago
6 years ago
1 month ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_vscale_info='vscale.io
  4. Site: vscale.io
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_vscale
  6. Options:
  7. VSCALE_API_KEY API Key
  8. Author: Alex Loban <https://github.com/LAV45>
  9. '
  10. VSCALE_API_URL="https://api.vscale.io/v1"
  11. ######## Public functions #####################
  12. #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  13. dns_vscale_add() {
  14. fulldomain=$1
  15. txtvalue=$2
  16. if [ -z "$VSCALE_API_KEY" ]; then
  17. VSCALE_API_KEY=""
  18. _err "You didn't specify the VSCALE api key yet."
  19. _err "Please create you key and try again."
  20. return 1
  21. fi
  22. _saveaccountconf VSCALE_API_KEY "$VSCALE_API_KEY"
  23. _debug "First detect the root zone"
  24. if ! _get_root "$fulldomain"; then
  25. _err "invalid domain"
  26. return 1
  27. fi
  28. _debug _domain_id "$_domain_id"
  29. _debug _sub_domain "$_sub_domain"
  30. _debug _domain "$_domain"
  31. _vscale_tmpl_json="{\"type\":\"TXT\",\"name\":\"$_sub_domain.$_domain\",\"content\":\"$txtvalue\"}"
  32. if _vscale_rest POST "domains/$_domain_id/records/" "$_vscale_tmpl_json"; then
  33. response=$(printf "%s\n" "$response" | _egrep_o "{\"error\": \".+\"" | cut -d : -f 2)
  34. if [ -z "$response" ]; then
  35. _info "txt record updated success."
  36. return 0
  37. fi
  38. fi
  39. return 1
  40. }
  41. #fulldomain txtvalue
  42. dns_vscale_rm() {
  43. fulldomain=$1
  44. txtvalue=$2
  45. _debug "First detect the root zone"
  46. if ! _get_root "$fulldomain"; then
  47. _err "invalid domain"
  48. return 1
  49. fi
  50. _debug _domain_id "$_domain_id"
  51. _debug _sub_domain "$_sub_domain"
  52. _debug _domain "$_domain"
  53. _debug "Getting txt records"
  54. _vscale_rest GET "domains/$_domain_id/records/"
  55. if [ -n "$response" ]; then
  56. record_id=$(printf "%s\n" "$response" | _egrep_o "\"TXT\", \"id\": [0-9]+, \"name\": \"$_sub_domain.$_domain\"" | cut -d : -f 2 | tr -d ", \"name\"")
  57. _debug record_id "$record_id"
  58. if [ -z "$record_id" ]; then
  59. _err "Can not get record id to remove."
  60. return 1
  61. fi
  62. if _vscale_rest DELETE "domains/$_domain_id/records/$record_id" && [ -z "$response" ]; then
  63. _info "txt record deleted success."
  64. return 0
  65. fi
  66. _debug response "$response"
  67. return 1
  68. fi
  69. return 1
  70. }
  71. #################### Private functions below ##################################
  72. #_acme-challenge.www.domain.com
  73. #returns
  74. # _sub_domain=_acme-challenge.www
  75. # _domain=domain.com
  76. # _domain_id=12345
  77. _get_root() {
  78. domain=$1
  79. i=2
  80. p=1
  81. if _vscale_rest GET "domains/"; then
  82. response="$(echo "$response" | tr -d "\n" | sed 's/{/\n&/g')"
  83. while true; do
  84. h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  85. _debug h "$h"
  86. if [ -z "$h" ]; then
  87. #not valid
  88. return 1
  89. fi
  90. hostedzone="$(echo "$response" | tr "{" "\n" | _egrep_o "\"name\":\s*\"$h\".*}")"
  91. if [ "$hostedzone" ]; then
  92. _domain_id=$(printf "%s\n" "$hostedzone" | _egrep_o "\"id\":\s*[0-9]+" | _head_n 1 | cut -d : -f 2 | tr -d \ )
  93. if [ "$_domain_id" ]; then
  94. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
  95. _domain=$h
  96. return 0
  97. fi
  98. return 1
  99. fi
  100. p=$i
  101. i=$(_math "$i" + 1)
  102. done
  103. fi
  104. return 1
  105. }
  106. #method uri qstr data
  107. _vscale_rest() {
  108. mtd="$1"
  109. ep="$2"
  110. data="$3"
  111. _debug mtd "$mtd"
  112. _debug ep "$ep"
  113. export _H1="Accept: application/json"
  114. export _H2="Content-Type: application/json"
  115. export _H3="X-Token: ${VSCALE_API_KEY}"
  116. if [ "$mtd" != "GET" ]; then
  117. # both POST and DELETE.
  118. _debug data "$data"
  119. response="$(_post "$data" "$VSCALE_API_URL/$ep" "" "$mtd")"
  120. else
  121. response="$(_get "$VSCALE_API_URL/$ep")"
  122. fi
  123. if [ "$?" != "0" ]; then
  124. _err "error $ep"
  125. return 1
  126. fi
  127. _debug2 response "$response"
  128. return 0
  129. }