3 #Created by RaidenII, to use DuckDNS's API to add/remove text records
6 # Pass credentials before "acme.sh --issue --dns dns_duckdns ..."
8 # export DuckDNS_Token="aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"
11 # Due to the fact that DuckDNS uses StartSSL as cert provider, --insecure may need to be used with acme.sh
13 DuckDNS_API
="https://www.duckdns.org/update"
15 ######## Public functions #####################
17 #Usage: dns_duckdns_add _acme-challenge.domain.duckdns.org "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
22 DuckDNS_Token
="${DuckDNS_Token:-$(_readaccountconf_mutable DuckDNS_Token)}"
23 if [ -z "$DuckDNS_Token" ]; then
24 _err
"You must export variable: DuckDNS_Token"
25 _err
"The token for your DuckDNS account is necessary."
26 _err
"You can look it up in your DuckDNS account."
30 # Now save the credentials.
31 _saveaccountconf_mutable DuckDNS_Token
"$DuckDNS_Token"
33 # Unfortunately, DuckDNS does not seems to support lookup domain through API
34 # So I assume your credentials (which are your domain and token) are correct
35 # If something goes wrong, we will get a KO response from DuckDNS
37 if ! _duckdns_get_domain
; then
41 # Now add the TXT record to DuckDNS
42 _info
"Trying to add TXT record"
43 if _duckdns_rest GET
"domains=$_duckdns_domain&token=$DuckDNS_Token&txt=$txtvalue"; then
44 if [ "$response" = "OK" ]; then
45 _info
"TXT record has been successfully added to your DuckDNS domain."
46 _info
"Note that all subdomains under this domain uses the same TXT record."
49 _err
"Errors happened during adding the TXT record, response=$response"
53 _err
"Errors happened during adding the TXT record."
58 #Usage: fulldomain txtvalue
59 #Remove the txt record after validation.
64 DuckDNS_Token
="${DuckDNS_Token:-$(_readaccountconf_mutable DuckDNS_Token)}"
65 if [ -z "$DuckDNS_Token" ]; then
66 _err
"You must export variable: DuckDNS_Token"
67 _err
"The token for your DuckDNS account is necessary."
68 _err
"You can look it up in your DuckDNS account."
72 if ! _duckdns_get_domain
; then
76 # Now remove the TXT record from DuckDNS
77 _info
"Trying to remove TXT record"
78 if _duckdns_rest GET
"domains=$_duckdns_domain&token=$DuckDNS_Token&txt=&clear=true"; then
79 if [ "$response" = "OK" ]; then
80 _info
"TXT record has been successfully removed from your DuckDNS domain."
83 _err
"Errors happened during removing the TXT record, response=$response"
87 _err
"Errors happened during removing the TXT record."
92 #################### Private functions below ##################################
94 # fulldomain may be 'domain.duckdns.org' (if using --domain-alias) or '_acme-challenge.domain.duckdns.org'
95 # either way, return 'domain'. (duckdns does not allow further subdomains and restricts domains to [a-z0-9-].)
96 _duckdns_get_domain
() {
98 # We'll extract the domain/username from full domain
99 _duckdns_domain
="$(printf "%s
" "$fulldomain" | _lower_case | _egrep_o '^(_acme-challenge\.)?[a-z0-9-]*\.duckdns\.org' | sed 's/^\(_acme-challenge\.\)\?\([a-z0-9-]*\)\.duckdns\.org/\2/')"
101 if [ -z "$_duckdns_domain" ]; then
102 _err
"Error extracting the domain."
113 _debug param
"$param"
114 url
="$DuckDNS_API?$param"
117 # DuckDNS uses GET to update domain info
118 if [ "$method" = "GET" ]; then
119 response
="$(_get "$url")"
121 _err
"Unsupported method"
125 _debug2 response
"$response"