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.

148 lines
4.6 KiB

4 years ago
4 years ago
4 years ago
4 years ago
2 months ago
2 months ago
4 years ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_kappernet_info='kapper.net
  4. Site: kapper.net
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_kappernet
  6. Options:
  7. KAPPERNETDNS_Key API Key
  8. KAPPERNETDNS_Secret API Secret
  9. Issues: github.com/acmesh-official/acme.sh/issues/2977
  10. '
  11. ###############################################################################
  12. # called with
  13. # fullhostname: something.example.com
  14. # txtvalue: someacmegenerated string
  15. dns_kappernet_add() {
  16. fullhostname=$1
  17. txtvalue=$2
  18. KAPPERNETDNS_Key="${KAPPERNETDNS_Key:-$(_readaccountconf_mutable KAPPERNETDNS_Key)}"
  19. KAPPERNETDNS_Secret="${KAPPERNETDNS_Secret:-$(_readaccountconf_mutable KAPPERNETDNS_Secret)}"
  20. KAPPERNETDNS_Api="https://dnspanel.kapper.net/API/1.2?APIKey=$KAPPERNETDNS_Key&APISecret=$KAPPERNETDNS_Secret"
  21. if [ -z "$KAPPERNETDNS_Key" ] || [ -z "$KAPPERNETDNS_Secret" ]; then
  22. _err "Please specify your kapper.net api key and secret."
  23. _err "If you have not received yours - send your mail to"
  24. _err "support@kapper.net to get your key and secret."
  25. return 1
  26. fi
  27. #store the api key and email to the account conf file.
  28. _saveaccountconf_mutable KAPPERNETDNS_Key "$KAPPERNETDNS_Key"
  29. _saveaccountconf_mutable KAPPERNETDNS_Secret "$KAPPERNETDNS_Secret"
  30. _debug "Checking Domain ..."
  31. if ! _get_root "$fullhostname"; then
  32. _err "invalid domain"
  33. return 1
  34. fi
  35. _debug _sub_domain "SUBDOMAIN: $_sub_domain"
  36. _debug _domain "DOMAIN: $_domain"
  37. _info "Trying to add TXT DNS Record"
  38. data="%7B%22name%22%3A%22$fullhostname%22%2C%22type%22%3A%22TXT%22%2C%22content%22%3A%22$txtvalue%22%2C%22ttl%22%3A%22300%22%2C%22prio%22%3A%22%22%7D"
  39. if _kappernet_api GET "action=new&subject=$_domain&data=$data"; then
  40. if _contains "$response" "{\"OK\":true"; then
  41. _info "Waiting 1 second for DNS to spread the new record"
  42. _sleep 1
  43. return 0
  44. else
  45. _err "Error creating a TXT DNS Record: $fullhostname TXT $txtvalue"
  46. _err "Error Message: $response"
  47. return 1
  48. fi
  49. fi
  50. _err "Failed creating TXT Record"
  51. }
  52. ###############################################################################
  53. # called with
  54. # fullhostname: something.example.com
  55. dns_kappernet_rm() {
  56. fullhostname=$1
  57. txtvalue=$2
  58. KAPPERNETDNS_Key="${KAPPERNETDNS_Key:-$(_readaccountconf_mutable KAPPERNETDNS_Key)}"
  59. KAPPERNETDNS_Secret="${KAPPERNETDNS_Secret:-$(_readaccountconf_mutable KAPPERNETDNS_Secret)}"
  60. KAPPERNETDNS_Api="https://dnspanel.kapper.net/API/1.2?APIKey=$KAPPERNETDNS_Key&APISecret=$KAPPERNETDNS_Secret"
  61. if [ -z "$KAPPERNETDNS_Key" ] || [ -z "$KAPPERNETDNS_Secret" ]; then
  62. _err "Please specify your kapper.net api key and secret."
  63. _err "If you have not received yours - send your mail to"
  64. _err "support@kapper.net to get your key and secret."
  65. return 1
  66. fi
  67. #store the api key and email to the account conf file.
  68. _saveaccountconf_mutable KAPPERNETDNS_Key "$KAPPERNETDNS_Key"
  69. _saveaccountconf_mutable KAPPERNETDNS_Secret "$KAPPERNETDNS_Secret"
  70. _info "Trying to remove the TXT Record: $fullhostname containing $txtvalue"
  71. data="%7B%22name%22%3A%22$fullhostname%22%2C%22type%22%3A%22TXT%22%2C%22content%22%3A%22$txtvalue%22%2C%22ttl%22%3A%22300%22%2C%22prio%22%3A%22%22%7D"
  72. if _kappernet_api GET "action=del&subject=$fullhostname&data=$data"; then
  73. if _contains "$response" "{\"OK\":true"; then
  74. return 0
  75. else
  76. _err "Error deleting DNS Record: $fullhostname containing $txtvalue"
  77. _err "Problem: $response"
  78. return 1
  79. fi
  80. fi
  81. _err "Problem deleting TXT DNS record"
  82. }
  83. #################### Private functions below ##################################
  84. # called with hostname
  85. # e.g._acme-challenge.www.domain.com returns
  86. # _sub_domain=_acme-challenge.www
  87. # _domain=domain.com
  88. _get_root() {
  89. domain=$1
  90. i=2
  91. p=1
  92. while true; do
  93. h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  94. if [ -z "$h" ]; then
  95. #not valid
  96. return 1
  97. fi
  98. if ! _kappernet_api GET "action=list&subject=$h"; then
  99. return 1
  100. fi
  101. if _contains "$response" '"OK":false'; then
  102. _debug "$h not found"
  103. else
  104. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
  105. _domain="$h"
  106. return 0
  107. fi
  108. p="$i"
  109. i=$(_math "$i" + 1)
  110. done
  111. return 1
  112. }
  113. ################################################################################
  114. # calls the kapper.net DNS Panel API
  115. # with
  116. # method
  117. # param
  118. _kappernet_api() {
  119. method=$1
  120. param="$2"
  121. _debug param "PARAMETER=$param"
  122. url="$KAPPERNETDNS_Api&$param"
  123. _debug url "URL=$url"
  124. if [ "$method" = "GET" ]; then
  125. response="$(_get "$url")"
  126. else
  127. _err "Unsupported method or missing Secret/Key"
  128. return 1
  129. fi
  130. _debug2 response "$response"
  131. return 0
  132. }