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.

224 lines
4.8 KiB

  1. #!/usr/bin/env sh
  2. #Secret
  3. #FastVps_Secret="2323f891ghfhf57480f923a3031d49gfdg94ca2768ea443f8262d6f78851379d5a6"
  4. #
  5. #Token
  6. FastVps_Token=""
  7. #
  8. #Endpoint
  9. FastVps_EndPoint="https://fastdns.fv.ee"
  10. #
  11. #Author: Voinkov Andrey.
  12. #Report Bugs here: https://github.com/exzm/acme.sh
  13. #
  14. ######## Public functions #####################
  15. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  16. dns_fastvps_add() {
  17. fulldomain=$1
  18. txtvalue=$2
  19. if [ -z "$FastVps_Secret" ]; then
  20. FastVps_Secret=""
  21. _err "FastVps secret is not specified."
  22. _err "Please create secret https://bill2fast.com/dns and try again."
  23. return 1
  24. fi
  25. #save the secret to the account conf file.
  26. _saveaccountconf FastVps_Secret "$FastVps_Secret"
  27. if [ -z "$FastVps_Token" ]; then
  28. _info "Getting FastVps token."
  29. if ! _fastvps_authentication; then
  30. _err "Can not get token."
  31. fi
  32. fi
  33. _debug "Detect root zone"
  34. if ! _get_root "$fulldomain"; then
  35. _err "Invalid domain."
  36. return 1
  37. fi
  38. _debug _node "$_node"
  39. _debug _domain_name "$_domain_name"
  40. _info "Creating TXT record."
  41. if ! _fastvps_rest POST "api/domains/$dnsId/records" "{\"name\":\"$_node\",\"type\":\"TXT\",\"content\":\"$txtvalue\",\"ttl\":90}"; then
  42. return 1
  43. fi
  44. if _contains "$response" "errors"; then
  45. _err "Could not add TXT record."
  46. return 1
  47. fi
  48. return 0
  49. }
  50. #Usage: rm _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  51. dns_fastvps_rm() {
  52. fulldomain=$1
  53. txtvalue=$2
  54. if [ -z "$FastVps_Secret" ]; then
  55. FastVps_Secret=""
  56. _err "Please create you API secret and try again."
  57. return 1
  58. fi
  59. #save the secret to the account conf file.
  60. _saveaccountconf FastVps_Secret "$FastVps_Secret"
  61. if [ -z "$FastVps_Token" ]; then
  62. _info "Getting FastVps token."
  63. if ! _fastvps_authentication; then
  64. _err "Can not get token."
  65. fi
  66. fi
  67. _debug "Detect root zone."
  68. if ! _get_root "$fulldomain"; then
  69. _err "Invalid domain."
  70. return 1
  71. fi
  72. _debug _node "$_node"
  73. _debug _domain_name "$_domain_name"
  74. _info "Checking for TXT record."
  75. if ! _get_recordid "$fulldomain" "$txtvalue"; then
  76. _err "Could not get TXT record id."
  77. return 1
  78. fi
  79. if [ "$_dns_record_id" = "" ]; then
  80. _err "TXT record not found."
  81. return 1
  82. fi
  83. _info "Removing TXT record."
  84. if ! _delete_txt_record "$_dns_record_id"; then
  85. _err "Could not remove TXT record $_dns_record_id."
  86. fi
  87. return 0
  88. }
  89. ######## Private functions below ##################################
  90. #_acme-challenge.www.domain.com
  91. #returns
  92. # _node=_acme-challenge.www
  93. # _domain_name=domain.com
  94. _get_root() {
  95. domain=$1
  96. i=2
  97. p=1
  98. while true; do
  99. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  100. _debug h "$h"
  101. if [ -z "$h" ]; then
  102. #not valid
  103. return 1
  104. fi
  105. if ! _fastvps_rest GET "api/domains/$h/name"; then
  106. return 1
  107. fi
  108. if _contains "$response" "\"name\":\"$h\"" >/dev/null; then
  109. dnsId=$(printf "%s" "$response" | grep -Po '(?<="id":)[^,"\\]*(?:\\.[^"\\]*)*')
  110. _domain_name=$h
  111. _node=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  112. return 0
  113. fi
  114. p=$i
  115. i=$(_math "$i" + 1)
  116. done
  117. return 1
  118. }
  119. _get_recordid() {
  120. fulldomain=$1
  121. txtvalue=$2
  122. if ! _fastvps_rest GET "api/domains/$dnsId/records"; then
  123. return 1
  124. fi
  125. if ! _contains "$response" "$txtvalue"; then
  126. _dns_record_id=0
  127. return 0
  128. fi
  129. _dns_record_id=$(printf "%s" "$response" | sed -e 's/[^{]*\({[^}]*}\)[^{]*/\1\n/g' | grep "\"content\":\"$txtvalue\"" | sed -e 's/.*"id":"\([^",]*\).*/\1/')
  130. return 0
  131. }
  132. _delete_txt_record() {
  133. _dns_record_id=$1
  134. if ! _fastvps_rest DELETE "/api/domains/$dnsId/records/$_dns_record_id"; then
  135. return 1
  136. fi
  137. if _contains "$response" "errors"; then
  138. return 1
  139. fi
  140. return 0
  141. }
  142. _fastvps_rest() {
  143. m=$1
  144. ep="$2"
  145. data="$3"
  146. _debug "$ep"
  147. export _H1="Authorization: Bearer $FastVps_Token"
  148. export _H2="Content-Type: application/json"
  149. if [ "$data" ] || [ "$m" = "DELETE" ]; then
  150. _debug data "$data"
  151. response="$(_post "$data" "$FastVps_EndPoint/$ep" "" "$m")"
  152. else
  153. _info "Getting $FastVps_EndPoint/$ep"
  154. response="$(_get "$FastVps_EndPoint/$ep")"
  155. fi
  156. if [ "$?" != "0" ]; then
  157. _err "error $ep"
  158. return 1
  159. fi
  160. _debug2 response "$response"
  161. return 0
  162. }
  163. _fastvps_authentication() {
  164. export _H1="Authenticate: $FastVps_Secret"
  165. export _H2="Content-Type: application/json"
  166. response="$(_post "" "$FastVps_EndPoint/login_token" "" "")"
  167. if [ "$?" != "0" ]; then
  168. _err "Authentication failed."
  169. return 1
  170. fi
  171. if _contains "$response" "token"; then
  172. FastVps_Token=$(printf "%s" "$response" | grep -Po '(?<="token":")[^"\\]*(?:\\.[^"\\]*)*')
  173. fi
  174. if _contains "$FastVps_Token" "null"; then
  175. FastVps_Token=""
  176. fi
  177. _info "Authentication success"
  178. _debug2 response "$response"
  179. return 0
  180. }