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.

177 lines
4.3 KiB

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