-
Notifications
You must be signed in to change notification settings - Fork 1
/
nc-ddns.sh
executable file
·192 lines (149 loc) · 3.9 KB
/
nc-ddns.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
#!/bin/bash
#
# (C) Marco Vedovati <[email protected]> - 2020
# SPDX-License-Identifier: Apache-2.0
#
# Namecheap Dynamic DNS updater
#
set -euo pipefail
declare -r myName="nc-ddns"
declare configFile=
declare -r cachedIpFile="${XDG_RUNTIME_DIR:-/tmp}/ddns-ip"
declare -r urlPrefix="https://dynamicdns.park-your-domain.com/update"
declare -r respSuccessPattern="<ErrCount>0</ErrCount>"
declare -A urlParams=( [host]="" [domain]="" [password]="" [ip]="" )
declare dryRun=
declare debug=
info() {
[ -z "$debug" ] || echo "INFO: $@"
}
get_ip() {
curl -fsSL "icanhazip.com"
}
update_ip() {
local fullUrl="${urlPrefix}"
local sep=
for key in "${!urlParams[@]}"; do
value="${urlParams[$key]}"
[ -n "$key" ] && [ -n "$value" ] || { echo "ERR: empty key/value pair: \"$key\":\"$value"\"; exit -1; }
fullUrl+="${sep:-?}$key=$value"
sep="&"
done
info "cURL URL: $fullUrl"
curlCmd=( \
curl \
-fsSL \
"$fullUrl" \
)
if [ -n "$dryRun" ]; then
echo "DRY-RUN: ${curlCmd[@]}"
return
fi
local response
response="$(${curlCmd[@]})" ||
{ echo "ERR: cURL command for IP update failed"; exit -1; }
info "got response: $response"
[[ $response =~ $respSuccessPattern ]] ||
{ echo "ERR: cURL response is not successful: \"$response\""; exit -1; }
}
read_config() {
local ip="$1"
[ -n "$ip" ] || { echo "ERR: need an IP"; exit -1; }
if [ -f "$configFile" ]; then
. "$configFile"
else
echo "WARN: missing DDNS info file. Using config from env variables"
if [ -z ${NC_DDNS_HOST:-} ] ||
[ -z ${NC_DDNS_DOMAIN:-} ] ||
[ -z ${NC_DDNS_PASSWORD:-} ]; then
echo "ERR: could not get DDNS confg from env variables"
exit -1
fi
fi
urlParams[host]="$NC_DDNS_HOST"
urlParams[domain]="$NC_DDNS_DOMAIN"
urlParams[password]="$NC_DDNS_PASSWORD"
urlParams[ip]="$1"
}
ip_is_cached() {
local ip="$1"
[ -n "$ip" ] || { echo "ERR: need an IP"; exit -1; }
local cachedIp=
if [ -f "$cachedIpFile" ]; then
# read return > 0 when EOF is met...
read -r cachedIp < "$cachedIpFile" || true
if [ -z "$cachedIp" ]; then
echo "WARN: cached IP file content is invalid"
else
info "cached IP: $cachedIp"
fi
else
echo "WARN: missing DDNS cached IP file: \"$cachedIpFile\""
fi
[ "$cachedIp" = "$ip" ]
}
resolve_ddns_ip() {
local fullDomain="${urlParams[host]}.${urlParams[domain]}"
#info "full domain: ${fullDomain}"
dig +short "${fullDomain}" || { echo "ERR: dig lookup for domain name failed"; exit -1; }
}
main() {
parse_args "$@"
local currIp=`get_ip`
[ -n "$currIp" ] || { echo "ERR: failed to get IP"; exit -1; }
info "current IP: $currIp"
read_config "$currIp"
if ip_is_cached "$currIp"; then
info "IP hasn't changed, nothing to do"
local resolvedIp="$(resolve_ddns_ip)"
[ -n "$resolvedIp" ] || { echo "ERR: unable to resolve IP"; exit -1; }
if [ "$resolvedIp" != "$currIp" ]; then
echo "WARN: current IP \"$currIp\" does not match resolved IP \"$resolvedIp\", invalidating cache"
rm -f "$cachedIpFile"
fi
exit 0
fi
update_ip
echo -n "$currIp" > "$cachedIpFile"
echo "SUCCESS"
}
parse_args() {
OPTIND=1
while getopts c:dfhn name
do
#echo "ARG: $name"
case $name in
c)
configFile="$OPTARG"
info "overriding config file path: ${configFile}"
;;
d)
debug=1
info "Debug info ON"
;;
f)
info "forcing update by deleting cached IP file";
rm -f "$cachedIpFile"
;;
n)
dryRun=1
info "Dry run"
;;
h | ?)
printf "\nUsage: %s [-c config-file] [-d] [−n] [-h] [−f]\n" "$myName"
printf "
Options:
-c : Configuration file path (default: get config via env variables)
-d : print debug info (default: false)
-f : Force update (invalidate the cached IP)
-h : help
-n : Dry-run
\n"
exit 2
;;
esac
done
shift "$(( $OPTIND - 1 ))"
[ -z "$*" ] || { echo "ERR: non-option arguments found: \"$*\""; exit 2; }
}
main "$@"