forked from rehiy/dnspod-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ardnspod
421 lines (312 loc) · 11.4 KB
/
ardnspod
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
#!/bin/sh
#
###################################################################
# AnripDdns v6.4.0
#
# Dynamic DNS using DNSPod API
#
# Author: Rehiy, https://github.com/rehiy
# https://www.rehiy.com/?s=dnspod
#
# Collaborators: https://github.com/rehiy/dnspod-shell/graphs/contributors
#
# Usage: please refer to `ddnspod.sh`
#
################################################################### params ##
export arToken
# The url to be used for querying public ip address.
export arIp4QueryUrl="http://ipv4.rehi.org/ip"
export arIp6QueryUrl="http://ipv6.rehi.org/ip"
# The temp file to store the last record ip
export arLastRecordFile=/tmp/ardnspod_last_record
# The error code to return when a ddns record is not changed
# By default, report unchanged event as success
export arErrCodeUnchanged=0
################################################################### logger ##
# Output log to stderr
arLog() {
>&2 echo "$@"
}
################################################################### http client ##
# Use curl or wget open url
# Args: url postdata
arRequest() {
local url="$1"
local data="$2"
local params=""
local agent="AnripDdns/6.4.0([email protected])"
if type curl >/dev/null 2>&1; then
if echo $url | grep -q https; then
params="$params -k"
fi
if [ -n "$data" ]; then
params="$params -d $data"
fi
curl -s -A "$agent" $params $url
return $?
fi
if type wget >/dev/null 2>&1; then
if echo $url | grep -q https; then
params="$params --no-check-certificate"
fi
if [ -n "$data" ]; then
params="$params --post-data $data"
fi
wget -qO- -U "$agent" $params $url
return $?
fi
return 1
}
################################################################### ipv4 util ##
# Get regular expression for IPv4 LAN addresses
arLanIp4() {
local lanIps="^$"
lanIps="$lanIps|(^10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$)" # RFC1918
lanIps="$lanIps|(^100\.(6[4-9]|[7-9][0-9])\.[0-9]{1,3}\.[0-9]{1,3}$)" # RFC6598 100.64.x.x - 100.99.x.x
lanIps="$lanIps|(^100\.1([0-1][0-9]|2[0-7])\.[0-9]{1,3}\.[0-9]{1,3}$)" # RFC6598 100.100.x.x - 100.127.x.x
lanIps="$lanIps|(^127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$)" # RFC1122
lanIps="$lanIps|(^169\.254\.[0-9]{1,3}\.[0-9]{1,3}$)" # RFC3927
lanIps="$lanIps|(^172\.(1[6-9]|2[0-9]|3[0-1])\.[0-9]{1,3}\.[0-9]{1,3}$)" # RFC1918
lanIps="$lanIps|(^192\.0\.2\.[0-9]{1,3}$)" # RFC5737
lanIps="$lanIps|(^192\.168\.[0-9]{1,3}\.[0-9]{1,3}$)" # RFC1918
lanIps="$lanIps|(^198\.1[8-9]\.[0-9]{1,3}\.[0-9]{1,3}$)" # RFC2544
lanIps="$lanIps|(^198\.51\.100\.[0-9]{1,3}$)" # RFC5737
lanIps="$lanIps|(^203\.0\.113\.[0-9]{1,3}$)" # RFC5737
lanIps="$lanIps|(^2[4-5][0-9]\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$)" # RFC1112
echo $lanIps
}
# Get IPv4 by ip route or network
arWanIp4() {
local hostIp
local lanIps=$(arLanIp4)
case $(uname) in
'Linux')
hostIp=$(ip -o -4 route get 100.64.0.1 | grep -oE 'src [0-9\.]+' | awk '{print $2}' | grep -Ev "$lanIps")
;;
Darwin|FreeBSD)
hostIp=$(ifconfig | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}' | grep -Ev "$lanIps")
;;
esac
if [ -z "$hostIp" ]; then
hostIp=$(arRequest $arIp4QueryUrl)
fi
if [ -z "$hostIp" ]; then
return 2
fi
if [ -z "$(echo $hostIp | grep -E '^[0-9\.]+$')" ]; then
arLog "> arWanIp4 - Invalid ip address"
return 1
fi
echo $hostIp
}
# Get IPv4 from a specific interface
# Args: interface
arDevIp4() {
local hostIp
local lanIps=$(arLanIp4)
case $(uname) in
'Linux')
hostIp=$(ip -o -4 addr show dev $1 primary | grep -oE 'inet [0-9.]+' | awk '{print $2}' | grep -Ev "$lanIps" | head -n 1)
;;
esac
if [ -z "$hostIp" ]; then
arLog "> arDevIp4 - Can't get ip address"
return 1
fi
if [ -z "$(echo $hostIp | grep -E '^[0-9\.]+$')" ]; then
arLog "> arDevIp4 - Invalid ip address"
return 1
fi
echo $hostIp
}
################################################################### ipv6 util ##
# Get regular expression for IPv6 LAN addresses
arLanIp6() {
local lanIps="(^$)"
lanIps="$lanIps|(^::1$)" # RFC4291
lanIps="$lanIps|(^64:[fF][fF]9[bB]:)" # RFC6052, RFC8215
lanIps="$lanIps|(^100::)" # RFC6666
lanIps="$lanIps|(^2001:2:0?:)" # RFC5180
lanIps="$lanIps|(^2001:[dD][bB]8:)" # RFC3849
lanIps="$lanIps|(^[fF][cdCD][0-9a-fA-F]{2}:)" # RFC4193 Unique local addresses
lanIps="$lanIps|(^[fF][eE][8-9a-bA-B][0-9a-fA-F]:)" # RFC4291 Link-local addresses
echo $lanIps
}
# Get IPv6 by ip route or network
arWanIp6() {
local hostIp
local lanIps=$(arLanIp6)
case $(uname) in
'Linux')
hostIp=$(ip -o -6 route get 100::1 | grep -oE 'src [0-9a-fA-F:]+' | awk '{print $2}' | grep -Ev "$lanIps")
;;
esac
if [ -z "$hostIp" ]; then
hostIp=$(arRequest $arIp6QueryUrl)
fi
if [ -z "$hostIp" ]; then
arLog "> arWanIp6 - Can't get ip address"
return 1
fi
if [ -z "$(echo $hostIp | grep -E '^[0-9a-fA-F:]+$')" ]; then
arLog "> arWanIp6 - Invalid ip address"
return 1
fi
echo $hostIp
}
# Get IPv6 from a specific interface
# Args: interface
arDevIp6() {
local hostIp
local lanIps=$(arLanIp6)
case $(uname) in
'Linux')
# Try obtain home address (a speical permanent address for mobile devices)
hostIp=$(ip -o -6 addr show dev $1 scope global home | grep -oE 'inet6 [0-9a-fA-F:]+' | awk '{print $2}' | grep -Ev "$lanIps")
if [ -z "$hostIp" ]; then # Try obtain permanent address
hostIp=$(ip -o -6 addr show dev $1 scope global permanent | grep -oE 'inet6 [0-9a-fA-F:]+' | awk '{print $2}' | grep -Ev "$lanIps")
fi
if [ -z "$hostIp" ]; then # Try obtain non-deprecated primary (non-temporary) non-mngtmpaddr (mngtmpaddr is template for temporary address creation) address then
hostIp=$(ip -o -6 addr show dev $1 scope global -deprecated primary | grep -v mngtmpaddr | grep -oE 'inet6 [0-9a-fA-F:]+' | awk '{print $2}' | grep -Ev "$lanIps")
fi
if [ -z "$hostIp" ]; then # Try obtain non-deprecated primary address then
hostIp=$(ip -o -6 addr show dev $1 scope global -deprecated primary | grep -oE 'inet6 [0-9a-fA-F:]+' | awk '{print $2}' | grep -Ev "$lanIps")
fi
if [ -z "$hostIp" ]; then # Try obtain non-deprecated address any at last
hostIp=$(ip -o -6 addr show dev $1 scope global -deprecated | grep -oE 'inet6 [0-9a-fA-F:]+' | awk '{print $2}' | grep -Ev "$lanIps")
fi
hostIp=$(echo "$hostIp" | head -n 1) # Fetch at most one address
;;
esac
if [ -z "$hostIp" ]; then
arLog "> arDevIp6 - Can't get ip address"
return 1
fi
if [ -z "$(echo $hostIp | grep -E '^[0-9a-fA-F:]+$')" ]; then
arLog "> arDevIp6 - Invalid ip address"
return 1
fi
echo $hostIp
}
################################################################### dnspod api ##
# Dnspod Bridge
# Args: interface data
arDdnsApi() {
local dnsapi="https://dnsapi.cn/${1:?'Info.Version'}"
local params="login_token=$arToken&format=json&lang=en&$2"
arRequest "$dnsapi" "$params"
}
# Fetch Record Id
# Args: domain subdomain recordType
arDdnsLookup() {
local errMsg
local recordId
if [ "$2" != "@" ]; then
# No sub_domain for root domain
subDomainRule="&sub_domain=$2"
fi
# Get Record Id
recordId=$(arDdnsApi "Record.List" "domain=$1${subDomainRule}&record_type=$3")
recordId=$(echo $recordId | sed 's/.*"id":"\([0-9]*\)".*/\1/')
if ! [ "$recordId" -gt 0 ] 2>/dev/null ;then
errMsg=$(echo $recordId | sed 's/.*"message":"\([^\"]*\)".*/\1/')
arLog "> arDdnsLookup - $errMsg"
return 1
fi
echo $recordId
}
# Update Record Value
# Args: domain subdomain recordId recordType [hostIp]
arDdnsUpdate() {
local errMsg
local recordRs
local recordCd
local recordIp
local lastRecordIp
local lastRecordIpFile="$arLastRecordFile.$3"
# fetch last ip
if [ -f $lastRecordIpFile ]; then
lastRecordIp=$(cat $lastRecordIpFile)
fi
# fetch from api
if [ -z "$lastRecordIp" ]; then
recordRs=$(arDdnsApi "Record.Info" "domain=$1&record_id=$3")
recordCd=$(echo $recordRs | sed 's/.*{"code":"\([0-9]*\)".*/\1/')
lastRecordIp=$(echo $recordRs | sed 's/.*,"value":"\([0-9a-fA-F\.\:]*\)".*/\1/')
fi
# update ip
if [ -z "$5" ]; then
recordRs=$(arDdnsApi "Record.Ddns" "domain=$1&sub_domain=$2&record_id=$3&record_type=$4&record_line=%e9%bb%98%e8%ae%a4")
else
if [ "$5" = "$lastRecordIp" ]; then
arLog "> arDdnsUpdate - unchanged: $lastRecordIp" # unchanged event
return $arErrCodeUnchanged
fi
recordRs=$(arDdnsApi "Record.Ddns" "domain=$1&sub_domain=$2&record_id=$3&record_type=$4&value=$5&record_line=%e9%bb%98%e8%ae%a4")
fi
# parse result
recordCd=$(echo $recordRs | sed 's/.*{"code":"\([0-9]*\)".*/\1/')
recordIp=$(echo $recordRs | sed 's/.*,"value":"\([0-9a-fA-F\.\:]*\)".*/\1/')
# check result
if [ "$recordCd" != "1" ]; then
errMsg=$(echo $recordRs | sed 's/.*,"message":"\([^"]*\)".*/\1/')
arLog "> arDdnsUpdate - error: $errMsg"
return 1
elif [ "$recordIp" = "$lastRecordIp" ]; then
arLog "> arDdnsUpdate - unchanged: $recordIp" # unchanged event
return $arErrCodeUnchanged
else
arLog "> arDdnsUpdate - updated: $recordIp" # updated event
if [ -n "$lastRecordIpFile" ]; then
echo $recordIp > $lastRecordIpFile
fi
return 0
fi
}
################################################################### task hub ##
# DDNS Check
# Args: domain subdomain [6|4] interface
arDdnsCheck() {
local errCode
local hostIp
local recordId
local recordType
arLog "=== Check $2.$1 ==="
arLog "Fetching Host Ip"
if [ "$3" = "6" ] && [ -n "$4" ]; then
recordType=AAAA
hostIp=$(arDevIp6 "$4")
elif [ "$3" = "4" ] && [ -n "$4" ]; then
recordType=A
hostIp=$(arDevIp4 "$4")
elif [ "$3" = "6" ]; then
recordType=AAAA
hostIp=$(arWanIp6)
else
recordType=A
hostIp=$(arWanIp4)
fi
errCode=$?
if [ $errCode -eq 0 ]; then
arLog "> Host Ip: $hostIp"
arLog "> Record Type: $recordType"
elif [ $errCode -eq 2 ]; then
arLog "> Host Ip: Auto"
arLog "> Record Type: $recordType"
else
arLog "$hostIp"
return $errCode
fi
arLog "Fetching RecordId"
recordId=$(arDdnsLookup "$1" "$2" "$recordType")
errCode=$?
if [ $errCode -eq 0 ]; then
arLog "> Record Id: $recordId"
else
arLog "$recordId"
return $errCode
fi
arLog "Updating Record value"
arDdnsUpdate "$1" "$2" "$recordId" "$recordType" "$hostIp"
}
################################################################### end ##