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.

157 lines
4.1 KiB

2 years ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_ipv64_info='IPv64.net
  4. Site: IPv64.net
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_ipv64
  6. Options:
  7. IPv64_Token API Token
  8. Issues: github.com/acmesh-official/acme.sh/issues/4419
  9. Author: Roman Lumetsberger
  10. '
  11. IPv64_API="https://ipv64.net/api"
  12. ######## Public functions ######################
  13. #Usage: dns_ipv64_add _acme-challenge.domain.ipv64.net "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  14. dns_ipv64_add() {
  15. fulldomain=$1
  16. txtvalue=$2
  17. IPv64_Token="${IPv64_Token:-$(_readaccountconf_mutable IPv64_Token)}"
  18. if [ -z "$IPv64_Token" ]; then
  19. _err "You must export variable: IPv64_Token"
  20. _err "The API Key for your IPv64 account is necessary."
  21. _err "You can look it up in your IPv64 account."
  22. return 1
  23. fi
  24. # Now save the credentials.
  25. _saveaccountconf_mutable IPv64_Token "$IPv64_Token"
  26. if ! _get_root "$fulldomain"; then
  27. _err "invalid domain" "$fulldomain"
  28. return 1
  29. fi
  30. _debug _sub_domain "$_sub_domain"
  31. _debug _domain "$_domain"
  32. # convert to lower case
  33. _domain="$(echo "$_domain" | _lower_case)"
  34. _sub_domain="$(echo "$_sub_domain" | _lower_case)"
  35. # Now add the TXT record
  36. _info "Trying to add TXT record"
  37. if _ipv64_rest "POST" "add_record=$_domain&praefix=$_sub_domain&type=TXT&content=$txtvalue"; then
  38. _info "TXT record has been successfully added."
  39. return 0
  40. else
  41. _err "Errors happened during adding the TXT record, response=$_response"
  42. return 1
  43. fi
  44. }
  45. #Usage: fulldomain txtvalue
  46. #Usage: dns_ipv64_rm _acme-challenge.domain.ipv64.net "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  47. #Remove the txt record after validation.
  48. dns_ipv64_rm() {
  49. fulldomain=$1
  50. txtvalue=$2
  51. IPv64_Token="${IPv64_Token:-$(_readaccountconf_mutable IPv64_Token)}"
  52. if [ -z "$IPv64_Token" ]; then
  53. _err "You must export variable: IPv64_Token"
  54. _err "The API Key for your IPv64 account is necessary."
  55. _err "You can look it up in your IPv64 account."
  56. return 1
  57. fi
  58. if ! _get_root "$fulldomain"; then
  59. _err "invalid domain" "$fulldomain"
  60. return 1
  61. fi
  62. _debug _sub_domain "$_sub_domain"
  63. _debug _domain "$_domain"
  64. # convert to lower case
  65. _domain="$(echo "$_domain" | _lower_case)"
  66. _sub_domain="$(echo "$_sub_domain" | _lower_case)"
  67. # Now delete the TXT record
  68. _info "Trying to delete TXT record"
  69. if _ipv64_rest "DELETE" "del_record=$_domain&praefix=$_sub_domain&type=TXT&content=$txtvalue"; then
  70. _info "TXT record has been successfully deleted."
  71. return 0
  72. else
  73. _err "Errors happened during deleting the TXT record, response=$_response"
  74. return 1
  75. fi
  76. }
  77. #################### Private functions below ##################################
  78. #_acme-challenge.www.domain.com
  79. #returns
  80. # _sub_domain=_acme-challenge.www
  81. # _domain=domain.com
  82. _get_root() {
  83. domain="$1"
  84. i=1
  85. p=1
  86. _ipv64_get "get_domains"
  87. domain_data=$_response
  88. while true; do
  89. h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  90. if [ -z "$h" ]; then
  91. #not valid
  92. return 1
  93. fi
  94. #if _contains "$domain_data" "\""$h"\"\:"; then
  95. if _contains "$domain_data" "\"""$h""\"\:"; then
  96. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
  97. _domain="$h"
  98. return 0
  99. fi
  100. p=$i
  101. i=$(_math "$i" + 1)
  102. done
  103. return 1
  104. }
  105. #send get request to api
  106. # $1 has to set the api-function
  107. _ipv64_get() {
  108. url="$IPv64_API?$1"
  109. export _H1="Authorization: Bearer $IPv64_Token"
  110. _response=$(_get "$url")
  111. _response="$(echo "$_response" | _normalizeJson)"
  112. if _contains "$_response" "429 Too Many Requests"; then
  113. _info "API throttled, sleeping to reset the limit"
  114. _sleep 10
  115. _response=$(_get "$url")
  116. _response="$(echo "$_response" | _normalizeJson)"
  117. fi
  118. }
  119. _ipv64_rest() {
  120. url="$IPv64_API"
  121. export _H1="Authorization: Bearer $IPv64_Token"
  122. export _H2="Content-Type: application/x-www-form-urlencoded"
  123. _response=$(_post "$2" "$url" "" "$1")
  124. if _contains "$_response" "429 Too Many Requests"; then
  125. _info "API throttled, sleeping to reset the limit"
  126. _sleep 10
  127. _response=$(_post "$2" "$url" "" "$1")
  128. fi
  129. if ! _contains "$_response" "\"info\":\"success\""; then
  130. return 1
  131. fi
  132. _debug2 response "$_response"
  133. return 0
  134. }