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.

149 lines
3.6 KiB

6 years ago
  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/{/\n&/g')"
  80. while true; do
  81. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  82. _debug h "$h"
  83. if [ -z "$h" ]; then
  84. #not valid
  85. return 1
  86. fi
  87. hostedzone="$(echo "$response" | tr "{" "\n" | _egrep_o "\"name\":\s*\"$h\".*}")"
  88. if [ "$hostedzone" ]; then
  89. _domain_id=$(printf "%s\n" "$hostedzone" | _egrep_o "\"id\":\s*[0-9]+" | _head_n 1 | cut -d : -f 2 | tr -d \ )
  90. if [ "$_domain_id" ]; then
  91. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  92. _domain=$h
  93. return 0
  94. fi
  95. return 1
  96. fi
  97. p=$i
  98. i=$(_math "$i" + 1)
  99. done
  100. fi
  101. return 1
  102. }
  103. #method uri qstr data
  104. _vscale_rest() {
  105. mtd="$1"
  106. ep="$2"
  107. data="$3"
  108. _debug mtd "$mtd"
  109. _debug ep "$ep"
  110. export _H1="Accept: application/json"
  111. export _H2="Content-Type: application/json"
  112. export _H3="X-Token: ${VSCALE_API_KEY}"
  113. if [ "$mtd" != "GET" ]; then
  114. # both POST and DELETE.
  115. _debug data "$data"
  116. response="$(_post "$data" "$VSCALE_API_URL/$ep" "" "$mtd")"
  117. else
  118. response="$(_get "$VSCALE_API_URL/$ep")"
  119. fi
  120. if [ "$?" != "0" ]; then
  121. _err "error $ep"
  122. return 1
  123. fi
  124. _debug2 response "$response"
  125. return 0
  126. }