dns_vultr.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  89. _debug h "$h"
  90. if [ -z "$h" ]; 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. _domain=$_domain
  100. return 0
  101. else
  102. _err 'Invalid domain'
  103. return 1
  104. fi
  105. else
  106. _err "$response"
  107. return 1
  108. fi
  109. i=$(_math "$i" + 1)
  110. done
  111. return 1
  112. }
  113. _vultr_rest() {
  114. m=$1
  115. ep="$2"
  116. data="$3"
  117. _debug "$ep"
  118. api_key_trimmed=$(echo $VULTR_API_KEY | tr -d '"')
  119. export _H1="Api-Key: $api_key_trimmed"
  120. export _H2='Content-Type: application/x-www-form-urlencoded'
  121. if [ "$m" != "GET" ]; then
  122. _debug data "$data"
  123. response="$(_post "$data" "$VULTR_Api/$ep" "" "$m")"
  124. else
  125. response="$(_get "$VULTR_Api/$ep")"
  126. fi
  127. if [ "$?" != "0" ]; then
  128. _err "Error $ep"
  129. return 1
  130. fi
  131. _debug2 response "$response"
  132. return 0
  133. }