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.

181 lines
4.4 KiB

9 months ago
9 months ago
  1. #!/usr/bin/env sh
  2. # Alviy domain api
  3. #
  4. # Get API key and secret from https://cloud.alviy.com/token
  5. #
  6. # Alviy_token="some-secret-key"
  7. #
  8. # Ex.: acme.sh --issue --staging --dns dns_alviy -d "*.s.example.com" -d "s.example.com"
  9. Alviy_Api="https://cloud.alviy.com/api/v1"
  10. ######## Public functions #####################
  11. #Usage: dns_alviy_add _acme-challenge.www.domain.com "content"
  12. dns_alviy_add() {
  13. fulldomain=$1
  14. txtvalue=$2
  15. Alviy_token="${Alviy_token:-$(_readaccountconf_mutable Alviy_token)}"
  16. if [ -z "$Alviy_token" ]; then
  17. Alviy_token=""
  18. _err "Please specify Alviy token."
  19. return 1
  20. fi
  21. #save the api key and email to the account conf file.
  22. _saveaccountconf_mutable Alviy_token "$Alviy_token"
  23. _debug "First detect the root zone"
  24. if ! _get_root "$fulldomain"; then
  25. _err "invalid domain"
  26. return 1
  27. fi
  28. _debug _sub_domain "$_sub_domain"
  29. _debug _domain "$_domain"
  30. _debug "Getting existing records"
  31. if _alviy_txt_exists "$_domain" "$fulldomain" "$txtvalue"; then
  32. _info "This record already exists, skipping"
  33. return 0
  34. fi
  35. _add_data="{\"content\":\"$txtvalue\",\"type\":\"TXT\"}"
  36. _debug2 _add_data "$_add_data"
  37. _info "Adding record"
  38. if _alviy_rest POST "zone/$_domain/domain/$fulldomain/" "$_add_data"; then
  39. _debug "Checking updated records of '${fulldomain}'"
  40. if ! _alviy_txt_exists "$_domain" "$fulldomain" "$txtvalue"; then
  41. _err "TXT record '${txtvalue}' for '${fulldomain}', value wasn't set!"
  42. return 1
  43. fi
  44. else
  45. _err "Add txt record error, value '${txtvalue}' for '${fulldomain}' was not set."
  46. return 1
  47. fi
  48. _sleep 10
  49. _info "Added TXT record '${txtvalue}' for '${fulldomain}'."
  50. return 0
  51. }
  52. #fulldomain
  53. dns_alviy_rm() {
  54. fulldomain=$1
  55. txtvalue=$2
  56. Alviy_token="${Alviy_token:-$(_readaccountconf_mutable Alviy_token)}"
  57. _debug "First detect the root zone"
  58. if ! _get_root "$fulldomain"; then
  59. _err "invalid domain"
  60. return 1
  61. fi
  62. _debug _sub_domain "$_sub_domain"
  63. _debug _domain "$_domain"
  64. if ! _alviy_txt_exists "$_domain" "$fulldomain" "$txtvalue"; then
  65. _info "The record does not exist, skip"
  66. return 0
  67. fi
  68. _add_data=""
  69. uuid=$(echo "$response" | tr "{" "\n" | grep "$txtvalue" | tr "," "\n" | grep uuid | cut -d \" -f4)
  70. # delete record
  71. _debug "Delete TXT record for '${fulldomain}'"
  72. if ! _alviy_rest DELETE "zone/$_domain/record/$uuid" "{\"confirm\":1}"; then
  73. _err "Cannot delete empty TXT record for '$fulldomain'"
  74. return 1
  75. fi
  76. _info "The record '$fulldomain'='$txtvalue' deleted"
  77. }
  78. #################### Private functions below ##################################
  79. #_acme-challenge.www.domain.com
  80. #returns
  81. # _sub_domain=_acme-challenge.www
  82. # _domain=domain.com
  83. _get_root() {
  84. domain=$1
  85. i=2
  86. p=1
  87. while true; do
  88. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  89. if [ -z "$h" ]; then
  90. #not valid
  91. return 1
  92. fi
  93. if ! _alviy_rest GET "zone/$h"; then
  94. return 1
  95. fi
  96. if _contains "$response" '"code":"NOT_FOUND"'; then
  97. _debug "$h not found"
  98. else
  99. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  100. _domain="$h"
  101. return 0
  102. fi
  103. p="$i"
  104. i=$(_math "$i" + 1)
  105. done
  106. return 1
  107. }
  108. _alviy_txt_exists() {
  109. zone=$1
  110. domain=$2
  111. content_data=$3
  112. _debug "Getting existing records"
  113. if ! _alviy_rest GET "zone/$zone/domain/$domain/TXT/"; then
  114. _info "The record does not exist"
  115. return 1
  116. fi
  117. if ! _contains "$response" "$3"; then
  118. _info "The record has other value"
  119. return 1
  120. fi
  121. # GOOD code return - TRUE function
  122. return 0
  123. }
  124. _alviy_rest() {
  125. method=$1
  126. path="$2"
  127. content_data="$3"
  128. _debug "$path"
  129. export _H1="Authorization: Bearer $Alviy_token"
  130. export _H2="Content-Type: application/json"
  131. if [ "$content_data" ] || [ "$method" = "DELETE" ]; then
  132. _debug "data ($method): " "$content_data"
  133. response="$(_post "$content_data" "$Alviy_Api/$path" "" "$method")"
  134. else
  135. response="$(_get "$Alviy_Api/$path")"
  136. fi
  137. _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
  138. if [ "$_code" = "401" ]; then
  139. _err "It seems that your api key or secret is not correct."
  140. return 1
  141. fi
  142. if [ "$_code" != "200" ]; then
  143. _err "API call error ($method): $path Response code $_code"
  144. fi
  145. if [ "$?" != "0" ]; then
  146. _err "error on rest call ($method): $path. Response:"
  147. _err "$response"
  148. return 1
  149. fi
  150. _debug2 response "$response"
  151. return 0
  152. }