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.

142 lines
4.1 KiB

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