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
3.9 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.1?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 KAPPERNETDNS_Key "$KAPPERNETDNS_Key"
  24. _saveaccountconf 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 KAPPERNETDNS_Key "$KAPPERNETDNS_Key"
  62. _saveaccountconf KAPPERNETDNS_Secret "$KAPPERNETDNS_Secret"
  63. _info "Trying to remove the TXT Record: $fullhostname"
  64. if _kappernet_api GET "action=del&subject=$fullhostname"; then
  65. if _contains "$response" "{\"OK\":true"; then
  66. return 0
  67. else
  68. _err "Error deleting DNS Record: $fullhostname"
  69. _err "Problem: $response"
  70. return 1
  71. fi
  72. fi
  73. _err "Problem creating TXT DNS record"
  74. }
  75. #################### Private functions below ##################################
  76. # called with hostname
  77. # e.g._acme-challenge.www.domain.com returns
  78. # _sub_domain=_acme-challenge.www
  79. # _domain=domain.com
  80. _get_root() {
  81. domain=$1
  82. i=2
  83. p=1
  84. while true; do
  85. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  86. if [ -z "$h" ]; then
  87. #not valid
  88. return 1
  89. fi
  90. if ! _kappernet_api GET "action=list&subject=$h"; then
  91. return 1
  92. fi
  93. if _contains "$response" '"OK":false'; then
  94. _debug "$h not found"
  95. else
  96. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  97. _domain="$h"
  98. return 0
  99. fi
  100. p="$i"
  101. i=$(_math "$i" + 1)
  102. done
  103. return 1
  104. }
  105. ################################################################################
  106. # calls the kapper.net DNS Panel API
  107. # with
  108. # method
  109. # param
  110. _kappernet_api() {
  111. method=$1
  112. param="$2"
  113. _debug param "PARAMETER=$param"
  114. url="$KAPPERNETDNS_Api&$param"
  115. _debug url "URL=$url"
  116. if [ "$method" = "GET" ]; then
  117. response="$(_get "$url")"
  118. else
  119. _err "Unsupported method"
  120. return 1
  121. fi
  122. _debug2 response "$response"
  123. return 0
  124. }