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.

141 lines
3.8 KiB

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