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.

161 lines
3.7 KiB

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