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.

151 lines
3.9 KiB

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