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.

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