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