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