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.

91 lines
2.6 KiB

7 years ago
7 years ago
7 years ago
  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. DuckDNS_Domain=$(echo "$fulldomain" | _lower_case | _egrep_o '.[^.]*.duckdns.org' | cut -d . -f 2)
  15. if [ -z "$DuckDNS_Domain" ]; then
  16. _err "Error extracting the domain."
  17. return 1
  18. fi
  19. if [ -z "$DuckDNS_Token" ]; then
  20. DuckDNS_Token=""
  21. _err "The token for your DuckDNS account is necessary."
  22. _err "You can look it up in your DuckDNS account."
  23. return 1
  24. fi
  25. # Now save the credentials.
  26. _saveaccountconf DuckDNS_Domain "$DuckDNS_Domain"
  27. _saveaccountconf DuckDNS_Token "$DuckDNS_Token"
  28. # Unfortunately, DuckDNS does not seems to support lookup domain through API
  29. # So I assume your credentials (which are your domain and token) are correct
  30. # If something goes wrong, we will get a KO response from DuckDNS
  31. # Now add the TXT record to DuckDNS
  32. _info "Trying to add TXT record"
  33. if _duckdns_rest GET "$API_Params&txt=$txtvalue" && [ "$response" = "OK" ]; then
  34. _info "TXT record has been successfully added to your DuckDNS domain."
  35. _info "Note that all subdomains under this domain uses the same TXT record."
  36. return 0
  37. else
  38. _err "Errors happened during adding the TXT record."
  39. return 1
  40. fi
  41. }
  42. #Usage: fulldomain txtvalue
  43. #Remove the txt record after validation.
  44. dns_duckdns_rm() {
  45. fulldomain=$1
  46. txtvalue=$2
  47. # Now remove the TXT record from DuckDNS
  48. _info "Trying to remove TXT record"
  49. if _duckdns_rest GET "$API_Params&txt=&clear=true" && [ "$response" = "OK" ]; then
  50. _info "TXT record has been successfully removed from your DuckDNS domain."
  51. return 0
  52. else
  53. _err "Errors happened during removing the TXT record."
  54. return 1
  55. fi
  56. }
  57. #################### Private functions below ##################################
  58. #Usage: method URI
  59. _duckdns_rest() {
  60. method=$1
  61. param="$2"
  62. _debug param "$param"
  63. url="$DuckDNS_API?$param"
  64. _debug url "$url"
  65. # DuckDNS uses GET to update domain info
  66. if [ "$method" = "GET" ]; then
  67. response="$(_get "$url")"
  68. else
  69. _err "Unsupported method"
  70. return 1
  71. fi
  72. _debug2 response "$response"
  73. return 0
  74. }