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.

167 lines
3.9 KiB

4 years ago
4 years ago
3 months ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_vultr_info='vultr.com
  4. Site: vultr.com
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_vultr
  6. Options:
  7. VULTR_API_KEY API Key
  8. Issues: github.com/acmesh-official/acme.sh/issues/2374
  9. Author:
  10. '
  11. VULTR_Api="https://api.vultr.com/v2"
  12. ######## Public functions #####################
  13. #
  14. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  15. dns_vultr_add() {
  16. fulldomain=$1
  17. txtvalue=$2
  18. _debug fulldomain "$fulldomain"
  19. _debug txtvalue "$txtvalue"
  20. VULTR_API_KEY="${VULTR_API_KEY:-$(_readaccountconf_mutable VULTR_API_KEY)}"
  21. if test -z "$VULTR_API_KEY"; then
  22. VULTR_API_KEY=''
  23. _err 'VULTR_API_KEY was not exported'
  24. return 1
  25. fi
  26. _saveaccountconf_mutable VULTR_API_KEY "$VULTR_API_KEY"
  27. _debug 'First detect the root zone'
  28. if ! _get_root "$fulldomain"; then
  29. return 1
  30. fi
  31. _debug _sub_domain "$_sub_domain"
  32. _debug _domain "$_domain"
  33. _debug 'Getting txt records'
  34. _vultr_rest GET "domains/$_domain/records"
  35. if printf "%s\n" "$response" | grep -- "\"type\":\"TXT\",\"name\":\"$fulldomain\"" >/dev/null; then
  36. _err 'Error'
  37. return 1
  38. fi
  39. if ! _vultr_rest POST "domains/$_domain/records" "{\"name\":\"$_sub_domain\",\"data\":\"$txtvalue\",\"type\":\"TXT\"}"; then
  40. _err "$response"
  41. return 1
  42. fi
  43. _debug2 _response "$response"
  44. return 0
  45. }
  46. #fulldomain txtvalue
  47. dns_vultr_rm() {
  48. fulldomain=$1
  49. txtvalue=$2
  50. _debug fulldomain "$fulldomain"
  51. _debug txtvalue "$txtvalue"
  52. VULTR_API_KEY="${VULTR_API_KEY:-$(_readaccountconf_mutable VULTR_API_KEY)}"
  53. if test -z "$VULTR_API_KEY"; then
  54. VULTR_API_KEY=""
  55. _err 'VULTR_API_KEY was not exported'
  56. return 1
  57. fi
  58. _saveaccountconf_mutable VULTR_API_KEY "$VULTR_API_KEY"
  59. _debug 'First detect the root zone'
  60. if ! _get_root "$fulldomain"; then
  61. return 1
  62. fi
  63. _debug _sub_domain "$_sub_domain"
  64. _debug _domain "$_domain"
  65. _debug 'Getting txt records'
  66. _vultr_rest GET "domains/$_domain/records"
  67. if printf "%s\n" "$response" | grep -- "\"type\":\"TXT\",\"name\":\"$fulldomain\"" >/dev/null; then
  68. _err 'Error'
  69. return 1
  70. fi
  71. _record_id="$(echo "$response" | tr '{}' '\n' | grep '"TXT"' | grep -- "$txtvalue" | tr ',' '\n' | grep -i 'id' | cut -d : -f 2 | tr -d '"')"
  72. _debug _record_id "$_record_id"
  73. if [ "$_record_id" ]; then
  74. _info "Successfully retrieved the record id for ACME challenge."
  75. else
  76. _info "Empty record id, it seems no such record."
  77. return 0
  78. fi
  79. if ! _vultr_rest DELETE "domains/$_domain/records/$_record_id"; then
  80. _err "$response"
  81. return 1
  82. fi
  83. _debug2 _response "$response"
  84. return 0
  85. }
  86. #################### Private functions below ##################################
  87. #_acme-challenge.www.domain.com
  88. #returns
  89. # _sub_domain=_acme-challenge.www
  90. # _domain=domain.com
  91. # _domain_id=sdjkglgdfewsdfg
  92. _get_root() {
  93. domain=$1
  94. i=1
  95. while true; do
  96. _domain=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  97. _debug h "$_domain"
  98. if [ -z "$_domain" ]; then
  99. return 1
  100. fi
  101. if ! _vultr_rest GET "domains"; then
  102. return 1
  103. fi
  104. if printf "%s\n" "$response" | grep -E '^\{.*\}' >/dev/null; then
  105. if _contains "$response" "\"domain\":\"$_domain\""; then
  106. _sub_domain="$(echo "$fulldomain" | sed "s/\\.$_domain\$//")"
  107. return 0
  108. else
  109. _debug "Go to next level of $_domain"
  110. fi
  111. else
  112. _err "$response"
  113. return 1
  114. fi
  115. i=$(_math "$i" + 1)
  116. done
  117. return 1
  118. }
  119. _vultr_rest() {
  120. m=$1
  121. ep="$2"
  122. data="$3"
  123. _debug "$ep"
  124. api_key_trimmed=$(echo "$VULTR_API_KEY" | tr -d '"')
  125. export _H1="Authorization: Bearer $api_key_trimmed"
  126. export _H2='Content-Type: application/json'
  127. if [ "$m" != "GET" ]; then
  128. _debug data "$data"
  129. response="$(_post "$data" "$VULTR_Api/$ep" "" "$m")"
  130. else
  131. response="$(_get "$VULTR_Api/$ep")"
  132. fi
  133. if [ "$?" != "0" ]; then
  134. _err "Error $ep"
  135. return 1
  136. fi
  137. _debug2 response "$response"
  138. return 0
  139. }