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.

92 lines
2.6 KiB

  1. #!/usr/bin/env sh
  2. #Created by RaidenII, to use DuckDNS's API to add/remove text records
  3. #06/27/2017
  4. # Currently only support single domain access
  5. # Due to the fact that DuckDNS uses StartSSL as cert provider, --insecure must be used with acme.sh
  6. DuckDNS_API="https://www.duckdns.org/update"
  7. API_Params="domains=$DuckDNS_domain&token=$DuckDNS_token"
  8. ######## Public functions #####################
  9. #Usage: dns_duckdns_add _acme-challenge.domain.duckdns.org "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  10. dns_duckdns_add() {
  11. fulldomain=$1
  12. txtvalue=$2
  13. # We'll extract the domain/username from full domain
  14. IFS='.' read -r -a fqdn <<< "$fulldomain"
  15. DuckDNS_domain="${fqdn[-3]}"
  16. if [ -z "$DuckDNS_domain" ]; then
  17. _err "Error extracting the domain."
  18. return 1
  19. fi
  20. if [ -z "$DuckDNS_token" ]; then
  21. DuckDNS_token=""
  22. _err "The token for your DuckDNS account is necessary."
  23. _err "You can look it up in your DuckDNS account."
  24. return 1
  25. fi
  26. # Now save the credentials.
  27. _saveaccountconf DuckDNS_domain "$DuckDNS_domain"
  28. _saveaccountconf DuckDNS_token "$DuckDNS_token"
  29. # Unfortunately, DuckDNS does not seems to support lookup domain through API
  30. # So I assume your credentials (which are your domain and token) are correct
  31. # If something goes wrong, we will get a KO response from DuckDNS
  32. # Now add the TXT record to DuckDNS
  33. _info "Trying to add TXT record"
  34. if _duckdns_rest GET "$API_Params&txt=$txtvalue" && [ $response == "OK" ]; then
  35. _info "TXT record has been successfully added to your DuckDNS domain."
  36. _info "Note that all subdomains under this domain uses the same TXT record."
  37. return 0
  38. else
  39. _err "Errors happened during adding the TXT record."
  40. return 1
  41. fi
  42. }
  43. #Usage: fulldomain txtvalue
  44. #Remove the txt record after validation.
  45. dns_duckdns_rm() {
  46. fulldomain=$1
  47. txtvalue=$2
  48. # Now remove the TXT record from DuckDNS
  49. _info "Trying to remove TXT record"
  50. if _duckdns_rest GET "$API_Params&txt=''&clear=true" && [ $response == "OK" ]; then
  51. _info "TXT record has been successfully removed from your DuckDNS domain."
  52. return 0
  53. else
  54. _err "Errors happened during removing the TXT record."
  55. return 1
  56. fi
  57. }
  58. #################### Private functions below ##################################
  59. #Usage: method URI
  60. _duckdns_rest() {
  61. method=$1
  62. param="$2"
  63. _debug param "$param"
  64. url="$DuckDNS_API?$param"
  65. _debug url "$url"
  66. # DuckDNS uses GET to update domain info
  67. if [ $method == "GET" ]; then
  68. response="$(_get "$url")"
  69. else
  70. _err "Unsupported method"
  71. return 1
  72. fi
  73. _debug response "$response"
  74. return 0
  75. }