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.

147 lines
4.7 KiB

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