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.

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