-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathduckdns.sh
executable file
·479 lines (398 loc) · 13.2 KB
/
duckdns.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#!/bin/sh
set -e
set -u
# ipify API docs: https://ipify.org
# duckdns API docs: https://duckdns.org/spec.jsp
DUCKDNS_SH_COMMAND=${DUCKDNS_SH_COMMAND:-${0:-duckdns.sh}}
DUCKDNS_SH_SUBCOMMAND="${DUCKDNS_SH_SUBCOMMAND:-${1-}}"
DUCKDNS_SH_SUBDOMAIN="${DUCKDNS_SH_SUBDOMAIN:-${2-}}"
DUCKDNS_SH_ARG_1="${DUCKDNS_SH_ARG_1:-${3-}}"
DUCKDNS_SH_ARG_2="${DUCKDNS_SH_ARG_2:-${4-}}"
cmd_auth() { (
if [ -z "${DUCKDNS_SH_SUBDOMAIN}" ]; then
echo ""
echo "You must provide a subdomain to authorize."
echo ""
printf "For reference, you already have tokens for"
fn_list
return 1
fi
if fn_check_env; then
echo "'${DUCKDNS_SH_SUBDOMAIN}.duckdns.org' is already ready to update"
echo "(to delete: 'rm ~/.config/duckdns.sh/${DUCKDNS_SH_SUBDOMAIN}.env')"
exit 0
fi
fn_read_token
echo "Created '~/.config/duckdns.sh/${DUCKDNS_SH_SUBDOMAIN}.env'"
); }
cmd_clear() { (
fn_require_subdomain
fn_duckdns_clear
); }
cmd_ip() { (
fn_require_subdomain
fn_require_dig
echo dig +short A "${DUCKDNS_SH_SUBDOMAIN}.duckdns.org"
my_domain_ipv4="$(
dig +short A "${DUCKDNS_SH_SUBDOMAIN}.duckdns.org"
)"
echo "${my_domain_ipv4:-(NONE)}"
echo ""
echo dig +short AAAA "${DUCKDNS_SH_SUBDOMAIN}.duckdns.org"
my_domain_ipv6="$(
dig +short AAAA "${DUCKDNS_SH_SUBDOMAIN}.duckdns.org"
)"
echo "${my_domain_ipv6:-(NONE)}"
echo ""
); }
cmd_launcher_install() { (
fn_require_subdomain
fn_require_curl
if ! command -v serviceman > /dev/null; then
curl --max-time 5.5 -fsSL https://webi.sh/serviceman | sh
# shellcheck disable=SC1090
. ~/.config/envman/PATH.env
fi
# macOS
if command -v launchctl > /dev/null; then
serviceman add --user --name sh.duckdns."${DUCKDNS_SH_SUBDOMAIN}" -- \
"${DUCKDNS_SH_COMMAND}" run "${DUCKDNS_SH_SUBDOMAIN}"
exit 0
fi
# Linux (Ubuntu, Debian, etc)
if command -v systemctl > /dev/null; then
echo ""
echo Running command: sudo env PATH="\$PATH" \
serviceman add --system --path="\$PATH" --username "$(whoami)" --name "${DUCKDNS_SH_SUBDOMAIN}.duckdns-sh" -- \
"${DUCKDNS_SH_COMMAND}" run "${DUCKDNS_SH_SUBDOMAIN}"
echo ""
sudo env PATH="$PATH" \
serviceman add --system --path="$PATH" --username "$(whoami)" --name "${DUCKDNS_SH_SUBDOMAIN}.duckdns-sh" -- \
"${DUCKDNS_SH_COMMAND}" run "${DUCKDNS_SH_SUBDOMAIN}"
echo ""
echo Running command: sudo systemctl restart systemd-journald
echo ""
sudo systemctl restart systemd-journald
exit 0
fi
echo "'launchctl' (macOS) and 'systemd' (Linux) are the only currently supported launchers"
exit 1
); }
cmd_launcher_uninstall() { (
fn_require_subdomain
# macOS
if command -v launchctl > /dev/null; then
echo launchctl unload -w ~/Library/LaunchAgents/sh.duckdns."${DUCKDNS_SH_SUBDOMAIN}".plist
launchctl unload -w ~/Library/LaunchAgents/sh.duckdns."${DUCKDNS_SH_SUBDOMAIN}".plist
echo "Disabled login launcher."
exit 0
fi
# Linux (Ubuntu, Debian, etc)
if command -v systemctl > /dev/null; then
echo Running command: sudo systemctl stop "${DUCKDNS_SH_SUBDOMAIN}.duckdns-sh"
sudo systemctl stop "${DUCKDNS_SH_SUBDOMAIN}.duckdns-sh"
echo Running command: sudo systemctl disable "${DUCKDNS_SH_SUBDOMAIN}.duckdns-sh"
sudo systemctl disable "${DUCKDNS_SH_SUBDOMAIN}.duckdns-sh"
echo "Disabled system launcher."
exit 0
fi
echo "'launchctl' (macOS) and 'systemd' (Linux) are the only currently supported launchers"
exit 1
); }
cmd_list() { (
echo ""
fn_list
); }
cmd_myip() { (
fn_require_curl
echo curl -fsSL 'https://api.ipify.org?format=text'
my_current_ipv4="$(
curl --max-time 5.5 -fsSL 'https://api.ipify.org?format=text'
)"
echo "${my_current_ipv4:-(NONE)}"
echo ""
echo curl -fsSL 'https://api64.ipify.org?format=text'
my_current_ipv6="$(
curl --max-time 5.5 -fsSL 'https://api64.ipify.org?format=text'
)"
if [ "${my_current_ipv4}" = "${my_current_ipv6}" ]; then
my_current_ipv6=""
fi
echo "${my_current_ipv6:-(NONE)}"
); }
cmd_run() { (
fn_require_subdomain
fn_require_env
fn_require_curl
fn_require_dig
my_minutes=1
my_wait="$((my_minutes * 60))"
while true; do
fn_update_ips
echo ""
echo ""
echo "Waiting ${my_minutes}m to check '${DUCKDNS_SH_SUBDOMAIN}' again..."
sleep "${my_wait}"
echo ""
done
); }
cmd_set() { (
fn_require_subdomain
fn_require_env
fn_require_curl
if [ -n "${DUCKDNS_SH_ARG_2}" ]; then
fn_duckdns_update "${DUCKDNS_SH_ARG_1}" "${DUCKDNS_SH_ARG_2}"
return 0
fi
case "${DUCKDNS_SH_ARG_1}" in
*:[a-fA-F0-9]*)
fn_duckdns_update '' "${DUCKDNS_SH_ARG_1}"
;;
*[0-9].[0-9]*)
fn_duckdns_update "${DUCKDNS_SH_ARG_1}" ''
;;
*)
echo "'${DUCKDNS_SH_ARG_1}' is not a valid IPv4 or IPv6 address"
return 1
;;
esac
) }
cmd_update() { (
fn_require_subdomain
fn_require_env
fn_require_curl
fn_require_dig
fn_update_ips
); }
fn_check_env() { (
if ! [ -e ~/.config/duckdns.sh/"${DUCKDNS_SH_SUBDOMAIN}.env" ]; then
exit 1
fi
# shellcheck disable=SC1090
. ~/.config/duckdns.sh/"${DUCKDNS_SH_SUBDOMAIN}.env"
if [ -z "${DUCKDNS_TOKEN-}" ]; then
exit 1
fi
); }
fn_duckdns_clear() { (
# shellcheck disable=SC1090
. ~/.config/duckdns.sh/"${DUCKDNS_SH_SUBDOMAIN}.env"
echo curl -fsSL "https://www.duckdns.org/update?domains=${DUCKDNS_SH_SUBDOMAIN}&token=****&clear=true"
printf "Clearing IP address(es)... "
curl --max-time 5.5 -fsSL "https://www.duckdns.org/update?domains=${DUCKDNS_SH_SUBDOMAIN}&token=${DUCKDNS_TOKEN}&clear=true"
# &verbose=true
echo ""
); }
fn_duckdns_update() { (
my_ipv4="${1}"
my_ipv6="${2-}"
my_ipv4_param=""
if [ -n "${my_ipv4}" ]; then
my_ipv4_param="&ip=${my_ipv4}"
fi
my_ipv6_param=""
if [ -n "${my_ipv6}" ]; then
my_ipv6_param="&ipv6=${my_ipv6}"
fi
echo curl -fsSL "https://www.duckdns.org/update?domains=${DUCKDNS_SH_SUBDOMAIN}&token=****${my_ipv4_param}${my_ipv6_param}"
# &verbose=true
if [ -n "${my_ipv6_param}" ]; then
if [ -n "${my_ipv4_param}" ]; then
printf "Updating IPv4 (%s) and IPv6 (%s) ... " "${my_ipv4}" "${my_ipv6}"
else
printf "Updating IPv6 (%s)... " "${my_ipv6}"
fi
else
if [ -n "${my_ipv4_param}" ]; then
printf "Updating IPv4 (%s)... " "${my_ipv4}"
else
echo >&2 "at least one of ipv4 or ipv6 is required to update"
return 1
fi
fi
# shellcheck disable=SC1090
. ~/.config/duckdns.sh/"${DUCKDNS_SH_SUBDOMAIN}.env"
curl --max-time 5.5 -fsSL "https://www.duckdns.org/update?domains=${DUCKDNS_SH_SUBDOMAIN}&token=${DUCKDNS_TOKEN}${my_ipv4_param}${my_ipv6_param}"
# duckdns.org will respond with "OK", but we still need a newline
echo ""
); }
fn_list() { (
# shellcheck disable=SC2088
echo '~/.config/duckdns.sh/:'
if ! [ -e ~/.config/duckdns.sh ]; then
echo " (directory does not exist)"
fi
for my_domainenv in ~/.config/duckdns.sh/*; do
my_domainenv="$(basename "$my_domainenv" ".env")"
# handle special case of 0 matches (* becomes literal)
if [ '*' = "${my_domainenv}" ]; then
echo " (no subdomains have been configured)"
continue
fi
echo " ${my_domainenv}"
done
echo ""
); }
fn_read_secret() { (
my_prompt="${1}"
# always read from the tty even when redirected:
# || exit only needed for bash
exec < /dev/tty || exit
# save current tty settings:
tty_settings="$(stty -g)" || exit
# schedule restore of the settings on exit of that subshell
# or on receiving SIGINT or SIGTERM:
trap 'stty "$tty_settings"' EXIT INT TERM
# disable terminal local echo
stty -echo || exit
# prompt on tty
printf "%s" "${my_prompt}" > /dev/tty
# read password as one line, record exit status
IFS= read -r my_password
ret=$?
# display a newline to visually acknowledge the entered password
echo > /dev/tty
# return the password for $REPLY
printf '%s\n' "$my_password"
exit "$ret"
); }
fn_read_token() { (
mkdir -p ~/.config/duckdns.sh/
echo "(the token will NOT BE SHOWN - just HIT ENTER after you paste it)"
my_token="$(
fn_read_secret 'Duck DNS Token> '
)"
echo "DUCKDNS_TOKEN=${my_token}" >> ~/.config/duckdns.sh/"${DUCKDNS_SH_SUBDOMAIN}.env"
); }
fn_require_curl() { (
if ! command -v curl > /dev/null; then
echo "command 'curl' not found: please install 'curl'"
return 1
fi
); }
fn_require_dig() { (
if ! command -v dig > /dev/null; then
echo "command 'dig' not found: please install 'dig' (part of 'dnsutils')"
return 1
fi
); }
fn_require_env() { (
if ! [ -e ~/.config/duckdns.sh/"${DUCKDNS_SH_SUBDOMAIN}.env" ]; then
echo "Missing '~/.config/duckdns.sh/${DUCKDNS_SH_SUBDOMAIN}.env'"
exit 1
fi
# shellcheck disable=SC1090
. ~/.config/duckdns.sh/"${DUCKDNS_SH_SUBDOMAIN}.env"
if [ -z "${DUCKDNS_TOKEN-}" ]; then
echo "Missing 'DUCKDNS_TOKEN=<uuid>' from '~/.config/duckdns.sh/${DUCKDNS_SH_SUBDOMAIN}.env'"
exit 1
fi
); }
fn_require_subdomain() { (
if [ -z "${DUCKDNS_SH_SUBDOMAIN}" ]; then
printf "Missing <subdomain> argument. Try one of these in "
fn_list
return 1
fi
); }
fn_update_ips() { (
echo dig +short A "${DUCKDNS_SH_SUBDOMAIN}.duckdns.org"
my_domain_ipv4="$(
dig +short A "${DUCKDNS_SH_SUBDOMAIN}.duckdns.org"
)"
echo "${DUCKDNS_SH_SUBDOMAIN}.duckdns.org A ${my_domain_ipv4:-(NONE)}"
echo ""
echo dig +short AAAA "${DUCKDNS_SH_SUBDOMAIN}.duckdns.org"
my_domain_ipv6="$(
dig +short AAAA "${DUCKDNS_SH_SUBDOMAIN}.duckdns.org"
)"
echo "${DUCKDNS_SH_SUBDOMAIN}.duckdns.org AAAA ${my_domain_ipv6:-(NONE)}"
echo ""
echo curl -fsSL 'https://api.ipify.org?format=text'
my_current_ipv4="$(
curl --max-time 5.5 -fsSL 'https://api.ipify.org?format=text'
)"
echo "IPv4 ${my_current_ipv4:-(NONE)}"
echo ""
echo curl -fsSL 'https://api64.ipify.org?format=text'
my_current_ipv6="$(
curl --max-time 5.5 -fsSL 'https://api64.ipify.org?format=text'
)"
if [ "${my_current_ipv4}" = "${my_current_ipv6}" ]; then
my_current_ipv6=""
fi
echo "IPv6 ${my_current_ipv6:-(NONE)}"
echo ""
# if either ip changed to be empty, clear both
if [ "${my_current_ipv4}" != "${my_domain_ipv4}" ]; then
if [ -z "${my_current_ipv4}" ]; then
fn_duckdns_clear
my_domain_ipv4=""
my_domain_ipv6=""
fi
fi
if [ "${my_current_ipv6}" != "${my_domain_ipv6}" ]; then
if [ -z "${my_current_ipv6}" ]; then
fn_duckdns_clear
my_domain_ipv4=""
my_domain_ipv6=""
fi
fi
# Note: at least one of the IPv4 or IPv6 *must* exist
# (otherwise we wouldn't even be able to get a empty response)
if [ "${my_current_ipv4}" != "${my_domain_ipv4}" ]; then
if [ "${my_current_ipv6}" != "${my_domain_ipv6}" ]; then
fn_duckdns_update "${my_current_ipv4}" "${my_current_ipv6}"
else
fn_duckdns_update "${my_current_ipv4}" ""
fi
else
if [ "${my_current_ipv6}" != "${my_domain_ipv6}" ]; then
fn_duckdns_update "" "${my_current_ipv6}"
else
echo "No change detected."
fi
fi
); }
main() { (
case "${DUCKDNS_SH_SUBCOMMAND}" in
auth) cmd_auth ;;
clear) cmd_clear ;;
disable) cmd_launcher_uninstall ;;
enable) cmd_launcher_install ;;
ip) cmd_ip ;;
list) cmd_list ;;
myip) cmd_myip ;;
run) cmd_run ;;
set) cmd_set ;;
update) cmd_update ;;
__noop__) ;;
help | --help | -h) fn_help ;;
*)
fn_help
exit 1
;;
esac
) }
fn_help() { (
echo ""
echo "USAGE"
echo " duckdns.sh <subcommand> [arguments...]"
echo ""
echo "SUBCOMMANDS"
echo " myip - show this device's ip(s)"
echo " ip <subdomain> - show subdomain's ip(s)"
echo ""
echo " list - show subdomains"
echo " auth <subdomain> - add Duck DNS token"
echo " update <subdomain> - update subdomain to device ip"
echo " set <subdomain> <ip> [ipv6] - set ipv4 and/or ipv6 explicitly"
echo " clear <subdomain> - unset ip(s)"
echo " run <subdomain> - check ip and update every 5m"
echo " enable <subdomain> - enable on boot (Linux) or login (macOS)"
echo " disable <subdomain> - disable on boot or login"
echo ""
echo " help - show this menu"
echo ""
); }
main