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.

150 lines
3.6 KiB

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