-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynb.sh
executable file
·1085 lines (1011 loc) · 29.3 KB
/
dynb.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
## Copyright (c) 2021 Eduard Veit
## All rights reserved. This program and the accompanying materials
## are made available under the terms of the MIT license
## which accompanies this distribution, and is available at
## https://opensource.org/licenses/MIT
###################
## Configuration ##
###################
#DYNB_DYN_DOMAIN=
# service provider could be deSEC, duckdns, dynv6, inwx
#DYNB_SERVICE_PROVIDER=
## update method options: domrobot, dyndns
#DYNB_UPDATE_METHOD=
# ip mode could be either: 4, 6 or dual for dualstack
#DYNB_IP_MODE=
# If you are using the DomRobot RPC-API enter your credentials for the web interface login here
# If you are using the DynDNS2 protocol enter your credentials here
#DYNB_USERNAME=
#DYNB_PASSWORD=
# or use a token
#DYNB_TOKEN=
# TTL (time to live) for the DNS record
# This setting is only relevant for API based record updates (not DnyDNS2!)
# minimum allowed TTL value by inwx is 300 (5 minutes)
TTL=300
# The IP-Check sites (some sites have different urls for v4 and v6)
# Pro tip: use your own ip check server for privacy
# it could be as simple as that...
# create an index.php with <?php echo $_SERVER'REMOTE_ADDR'; ?>
#DYNB_IPv4_CHECK_SITE=
#DYNB_IPv6_CHECK_SITE=
# An exernal DNS check server prevents wrong info from local DNS servers/resolvers
#DYNB_DNS_CHECK_SERVER=9.9.9.9
# if you are actively using multiple network interfaces you might want to specify this
# normally the default value is okay
#_network_interface=eth0
_network_interface=
######################################################
## You don't need to change the following variables ##
_INWX_JSON_API_URL=https://api.domrobot.com/jsonrpc/
_internet_connectivity_test_server=https://www.google.de
_default_check_ip_servers=("ip64.ev21.de" "api64.ipify.org" "api.my-ip.io/ip" "ip.anysrc.net/plain")
_ipv4_checker=
_ipv6_checker=
_DNS_checkServer=
_remote_IPv4=
_remote_IPv6=
_dns_records=
_main_domain=
_is_IPv4_enabled=false
_is_IPv6_enabled=false
_interface_str=
_eventTime=0
_errorCounter=0
_response=
_statusHostname=
_statusUsername=
_statusPassword=
_version=0.5.3
_userAgent="DynB/$_version github.com/EV21/dynb"
_configFile=$HOME/.local/share/dynb/.env
_statusFile=/tmp/dynb.status
_debug=false
_minimum_looptime=60
_loopMode=false
_remote_ip=
_dns_ip=
_has_remote_ip_error=
_has_remote_ip4=false
_has_remote_ip6=false
# Ansi color code variables
yellow_color="\e[0;33m"
green_color="\e[0;92m"
expand_bg="\e[K"
red_color_bg="\e[0;101m${expand_bg}"
bold="\e[1m"
reset_color_modification="\e[0m"
REGEX_IPv4="^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$"
REGEX_IPv6="^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$"
function is_IPv4_address
{
local ip=$1
if [[ $ip =~ $REGEX_IPv4 ]]
then return 0
else return 1
fi
}
function is_IPv6_address
{
local ip=$1
if [[ $ip =~ $REGEX_IPv6 ]]
then return 0
else return 1
fi
}
## is_ip_address A B
# parameters required
# 1. param: ip_version
# 2. param: ip_address
function is_ip_address
{
local ip_version=$1
local ip_address=$2
case $ip_version in
4)
is_IPv4_address "$ip_address"
result=$?
;;
6)
is_IPv6_address "$ip_address"
result=$?
;;
esac
return "$result"
}
function loopMode
{
if [[ $_loopMode == "true" ]]
then return 0
else return 1
fi
}
function debugMode
{
if [[ $_debug == "true" ]]
then return 0
else return 1
fi
}
function infoMessage
{
echo -e "${green_color}$(logtime) INFO: $*${reset_color_modification}"
}
function debugMessage
{
if debugMode
then echo -e "${yellow_color}$(logtime) DEBUG: ${*}${reset_color_modification}"
fi
}
function errorMessage
{
echo -e "${red_color_bg}${bold}$(logtime) ERROR: $*${reset_color_modification}" >&2
}
function logtime
{
LOGTIME=$(date "+%Y-%m-%d %H:%M:%S")
echo "[$LOGTIME]"
}
# The main domain as an identifier for the dns zone is required for the updateRecord call
function getMainDomain
{
request=$(
echo "{}" |
jq '(.method="nameserver.list")' |
jq "(.params.user=\"$DYNB_USERNAME\")" |
jq "(.params.pass=\"$DYNB_PASSWORD\")"
)
_response=$(
curl --silent \
"$_interface_str" \
--user-agent "$_userAgent" \
--header "Content-Type: application/json" \
--request POST $_INWX_JSON_API_URL \
--data "$request" | jq ".resData.domains[] | select(inside(.domain=\"$DYNB_DYN_DOMAIN\"))"
)
_main_domain=$(echo "$_response" | jq --raw-output '.domain')
}
function fetchDNSRecords
{
request=$(
echo "{}" |
jq '(.method="'nameserver.info'")' |
jq "(.params.user=\"$DYNB_USERNAME\")" |
jq "(.params.pass=\"$DYNB_PASSWORD\")" |
jq "(.params.domain=\"$_main_domain\")" |
jq "(.params.name=\"$DYNB_DYN_DOMAIN\")"
)
_response=$(
curl --silent \
"$_interface_str" \
--user-agent "$_userAgent" \
--header "Content-Type: application/json" \
--request POST $_INWX_JSON_API_URL \
--data "$request"
)
_dns_records=$(echo "$_response" | jq '.resData.record[]')
}
## getRecordID
# requires parameter A or AAAA
# result to stdout
function getRecordID
{
echo "$_dns_records" |
jq "select(.type == \"${1}\") | .id"
}
## do_dig_request A B
# sets variable: _dns_ip
#
# requires parameter
# 1. param: dns server address
# 2. param: A or AAAA
function do_dig_request
{
local dns_server=$1
local record_type=$2
dig_response=$(dig @"$dns_server" in "$record_type" +short "$DYNB_DYN_DOMAIN" 2>&1)
dig_exitcode=$?
if [[ $dig_exitcode -gt 0 ]]
then
errorMessage "DNS request for $record_type @ $dns_server failed with exit code: $dig_exitcode $dig_response"
unset _dns_ip
return 1
else
case $record_type in
A) is_ip_address 4 "$dig_response"
;;
AAAA) is_ip_address 6 "$dig_response"
;;
esac
if test $? -gt 0
then
test -n "$dig_response" || debugMessage "dig response: $dig_response"
unset _dns_ip
return 1
fi
fi
# If the dns resolver lists multiple records in the answer section we filter the first line
# using short option "-n" and not "--lines" because of alpines limited BusyBox head command
_dns_ip=$(echo "$dig_response" | head -n 1)
return 0
}
## getDNSIP A
# sets variable: _dns_ip
#
# requires parameter
# 1. param: A or AAAA
function getDNSIP() {
local record_type=$1
if [[ $DYNB_UPDATE_METHOD == domrobot ]]
then
_dns_ip=$(echo "$_dns_records" |
jq --raw-output "select(.type == \"${record_type}\") | .content")
else
for current_dns_server in "${provider_dns_servers[@]}"
do
debugMessage "try dig DNS request with record type $record_type @$current_dns_server"
if do_dig_request "$current_dns_server" "$record_type"
then break
fi
done
fi
}
## getRemoteIP A
# sets variable: _remote_ip,
#
# requires parameter
# 1. param: 4 or 6 for ip version
#
# result to stdout
function getRemoteIP
{
local ip_version=$1
if test -n "$_provider_check_ip"
then ip_check_servers=("$_provider_check_ip" "${_default_check_ip_servers[@]}")
fi
case $ip_version in
4)
if test -n "$_ipv4_checker"
then ip_check_servers=("$_ipv4_checker" "${_default_check_ip_servers[@]}")
else ip_check_servers=("${_default_check_ip_servers[@]}")
fi
;;
6)
if test -n "$_ipv6_checker"
then ip_check_servers=("$_ipv6_checker" "${_default_check_ip_servers[@]}")
else ip_check_servers=("${_default_check_ip_servers[@]}")
fi
;;
esac
for current_check_server in "${ip_check_servers[@]}"
do
debugMessage "try getting remote IPv$ip_version via $current_check_server"
response=$(curl --silent "$_interface_str" --user-agent "$_userAgent" \
--ipv"${ip_version}" --location "${current_check_server}")
curls_status_code=$?
# shellcheck disable=2181
if [[ $curls_status_code -gt 0 ]]
then
errorMessage "Remote IPv$ip_version request @ ${current_check_server} failed with curl status code: $curls_status_code"
_has_remote_ip_error=true
return_value=1
else
if is_ip_address "$ip_version" "$response"
then
_has_remote_ip_error=false
_remote_ip="$response"
return_value=0
break
else
errorMessage "The response from the IP check server $current_check_server is not an IPv$ip_version address: $response"
_has_remote_ip_error=true
return_value=1
fi
fi
done
case $ip_version in
4)
if [[ $_has_remote_ip_error == true ]]
then _has_remote_ip4=false
else _has_remote_ip4=true
fi
;;
6)
if [[ $_has_remote_ip_error == true ]]
then export _has_remote_ip6=false
else export _has_remote_ip6=true
fi
;;
esac
return "$return_value"
}
# requires parameter
# 1. param: 4 or 6 as ip version
function updateRecord
{
local ip_version=$1
if [[ ${ip_version} == 4 ]]
then
ID=$(getRecordID A)
IP=$_remote_IPv4
fi
if [[ ${ip_version} == 6 ]]
then
ID=$(getRecordID AAAA)
IP=$_remote_IPv6
fi
if [[ $IP != "" ]]
then
request=$(
echo "{}" |
jq '(.method="nameserver.updateRecord")' |
jq "(.params.user=\"$DYNB_USERNAME\")" |
jq "(.params.pass=\"$DYNB_PASSWORD\")" |
jq "(.params.id=\"$ID\")" |
jq "(.params.content=\"$IP\")" |
jq "(.params.ttl=\"$TTL\")"
)
_response=$(
curl --silent \
"$_interface_str" \
--user-agent "$_userAgent" \
--header "Content-Type: application/json" \
--request POST $_INWX_JSON_API_URL \
--data "$request"
)
infoMessage "$(echo "$_response" | jq --raw-output '.msg')\n Domain: $DYNB_DYN_DOMAIN\n New IPv${1}: $IP"
fi
}
function prepare_request_parameters
{
# default parameter values
ipv4_parameter_name=myip
ipv6_parameter_name=myipv6
curl_parameters=("--user-agent" "$_userAgent")
case $DYNB_SERVICE_PROVIDER in
[Ii][Nn][Ww][Xx]*)
# inwx.de
# in case of dualstack use you need to request both parameters with the same request
# otherwise inwx will delete the not requested record type
curl_parameters+=("--user" "$DYNB_USERNAME:$DYNB_PASSWORD")
curl_parameters+=("--get") # inwx will ignore the ipv6 parameter if you don't put it into the url
dyndns_update_url="https://dyndns.inwx.com/nic/update"
provider_dns_servers=("ns.inwx.de" "ns2.inwx.de" "ns3.inwx.eu")
;;
[Dd][Yy][Nn][Uu]*)
curl_parameters+=("--user" "$DYNB_USERNAME:$DYNB_PASSWORD")
curl_parameters+=("--get")
dyndns_update_url="https://api.dynu.com/nic/update"
provider_dns_servers=("NS11.dynu.com" "NS10.dynu.com" "NS12.dynu.com")
;;
[Dd][Ee][Ss][Ee][Cc]* | [Dd][Ee][Dd][Yy][Nn]* )
# deSEC.de / dedyn.io
curl_parameters+=("--header" "Authorization: Token $DYNB_TOKEN")
curl_parameters+=("--get")
curl_parameters+=("--data-urlencode" "hostname=$DYNB_DYN_DOMAIN")
dyndns_update_url="https://update.dedyn.io"
provider_dns_servers=("ns1.desec.io" "ns2.desec.org")
_provider_check_ip="https://checkip.dedyn.io" # checkipv4 and checkipv6 is also available
;;
[Dd][Yy][Nn][Vv]6*)
# dynv6.com
ipv4_parameter_name=ipv4
ipv6_parameter_name=ipv6
curl_parameters+=("--get")
curl_parameters+=("--data-urlencode" "zone=$DYNB_DYN_DOMAIN")
curl_parameters+=("--data-urlencode" "token=$DYNB_TOKEN")
dyndns_update_url="https://dynv6.com/api/update"
provider_dns_servers=("ns1.dynv6.com" "ns2.dynv6.com" "ns3.dynv6.com")
;;
[Dd][Uu][Cc][Kk][Dd][Nn][Ss]*)
# DuckDNS.org
ipv4_parameter_name=ip
ipv6_parameter_name=ipv6
curl_parameters+=("--get")
curl_parameters+=("--data-urlencode" "domains=$DYNB_DYN_DOMAIN")
curl_parameters+=("--data-urlencode" "token=$DYNB_TOKEN")
dyndns_update_url="https://www.duckdns.org/update"
provider_dns_servers=("ns1.duckdns.org" "ns2.duckdns.org")
;;
[Dd][Dd][Nn][Ss][Ss]*)
# ddnss.de
ipv4_parameter_name=ip
ipv6_parameter_name=ip6
curl_parameters+=("--get")
curl_parameters+=("--data-urlencode" "host=$DYNB_DYN_DOMAIN")
curl_parameters+=("--data-urlencode" "key=$DYNB_TOKEN")
dyndns_update_url="https://ddnss.de/upd.php"
provider_dns_servers=("ns1.ddnss.de" "ns2.ddnss.de" "ns3.ddnss.de")
;;
[Ii][Pp][Vv]64*)
# IPv64.net
ipv4_parameter_name=ip
ipv6_parameter_name=ip6
curl_parameters+=("--request" "POST")
curl_parameters+=("--header" "Authorization: Bearer $DYNB_TOKEN")
curl_parameters+=("--data-urlencode" "domain=$DYNB_DYN_DOMAIN")
dyndns_update_url="https://ipv64.net/nic/update"
provider_dns_servers=("ns1.IPv64.net" "ns2.IPv64.net")
;;
*)
errorMessage "$DYNB_SERVICE_PROVIDER is not supported"
exit 1
;;
esac
if test -n "$_DNS_checkServer"
then provider_dns_servers=("$_DNS_checkServer" "${provider_dns_servers[@]}")
fi
}
function prepare_ip_flag_parameters
{
debugMessage "IPv4 enabled: $_is_IPv4_enabled; IPv6 enabled: $_is_IPv6_enabled; has remote IPv4: $_has_remote_ip4; has remote IPv6: $_has_remote_ip6"
if [[ $_is_IPv4_enabled == true ]] && [[ $_is_IPv6_enabled == true ]] && [[ $_has_remote_ip4 == true ]] && [[ $_has_remote_ip6 == true ]]
then
ip_flag_parameters=("--data-urlencode" "${ipv4_parameter_name}=${_remote_IPv4}" "--data-urlencode" "${ipv6_parameter_name}=${_remote_IPv6}")
fi
if [[ $_is_IPv4_enabled == true ]] && [[ $_is_IPv6_enabled == false ]] || [[ $_is_IPv4_enabled == true ]] && [[ $_is_IPv6_enabled == true ]] && [[ $_has_remote_ip4 == true ]] && [[ $_has_remote_ip6 == false ]]
then
ip_flag_parameters=("--data-urlencode" "${ipv4_parameter_name}=${_remote_IPv4}")
fi
if [[ $_is_IPv4_enabled == false ]] && [[ $_is_IPv6_enabled == true ]] || [[ $_is_IPv4_enabled == true ]] && [[ $_is_IPv6_enabled == true ]] && [[ $_has_remote_ip4 == false ]] && [[ $_has_remote_ip6 == true ]]
then
ip_flag_parameters=("--data-urlencode" "${ipv6_parameter_name}=${_remote_IPv6}")
fi
}
function send_request
{
local _response
debugMessage "curl parameters: ${curl_parameters[*]} ${dyndns_update_url}"
_response=$(
curl --silent "$_interface_str" \
"${curl_parameters[@]}" \
"${dyndns_update_url}")
_curl_exit_code=$?
analyse_response
status_code=$?
return $status_code
}
function analyse_response
{
case $_response in
good* | OK* | "addresses updated" | *Updated*hostname* | *'"info":"good"'*)
if [[ $_response == "good 127.0.0.1" ]]; then
errorMessage "$_response: Request ignored."
return 1
else
infoMessage "The DynDNS update has been executed."
debugMessage "Response: $_response"
return 0
fi
;;
*nochg*)
infoMessage "Nothing has changed, IP addresses are still up to date."
debugMessage "Response: $_response"
return 1
;;
400* | *'Bad Request'*)
errorMessage "Bad Request."
debugMessage "Response: $_response"
return 1
;;
*'Too Many Requests'*)
errorMessage "Too Many Request."
debugMessage "Response: $_response"
return 1
;;
abuse)
errorMessage "Username is blocked due to abuse."
debugMessage "Response: $_response"
return 1
;;
*badauth* | 401 | *Unauthorized*)
errorMessage "Invalid token or username password combination."
debugMessage "Response: $_response"
return 1
;;
badagent)
errorMessage "Client disabled. Something is very wrong!"
debugMessage "Response: $_response"
return 1
;;
!donator)
errorMessage "$_response: An update request was sent, including a feature that is not available to that particular user such as offline options."
return 1
;;
!yours)
errorMessage "$_response: The domain does not belong to your user account"
return 1
;;
notfqdn)
errorMessage "$_response: Hostname $DYNB_DYN_DOMAIN is invalid"
return 1
;;
*nohost*)
errorMessage "Hostname supplied does not exist under specified account, enter new login credentials before performing an additional request."
debugMessage "Response: $_response"
return 1
;;
numhost)
errorMessage "$_response: Too many hostnames have been specified for this update"
return 1
;;
dnserr)
errorMessage "$_response: There is an internal error in the dyndns update system. Retry update no sooner than 30 minutes."
return 1
;;
servererror | 911 | 5*)
errorMessage "$_response: A fatal error on provider side such as a database outage. Retry update no sooner than 30 minutes."
return 1
;;
*)
if test $_curl_exit_code -gt 0
then
analyse_curl_exit_code $_curl_exit_code
return $?
elif test -n "$_response" && [[ "$_response" == "$_previous_response_was" ]]; then
errorMessage "An unknown response code has been received: $_response"
return 1
else
errorMessage "unknown respnse code: $_response"
return 0
fi
;;
esac
}
function analyse_curl_exit_code
{
local current_curl_exit_code=$1
case $current_curl_exit_code in
6)
errorMessage "curl exit code: $_curl_exit_code; Couldn't resolve host: $dyndns_update_url"
;;
7)
errorMessage "curl exit code: $_curl_exit_code; Failed to connect to host: $dyndns_update_url"
;;
22)
errorMessage "curl exit code: $_curl_exit_code HTTP error code being 400 or above"
;;
*)
errorMessage "curl exit code: $_curl_exit_code"
;;
esac
return "$current_curl_exit_code"
}
# using DynDNS2 protocol
function dynupdate
{
prepare_ip_flag_parameters
curl_parameters+=("${ip_flag_parameters[@]}")
send_request
request_status=$?
return $request_status
}
function setStatus
{
echo "_curl_exit_code_was=$1; _previous_response_was=$2 _eventTime=$3; _errorCounter=$4; _statusHostname=$5; _statusUsername=$6; _statusPassword=$7" >/tmp/dynb.status
}
declare _previous_response_was
declare _curl_exit_code_was
# handle errors from past update requests
function checkStatus
{
local check_string
if [[ -z "${_previous_response_was}" ]]
then check_string=$_curl_exit_code_was
else check_string=$_previous_response_was
fi
case $check_string in
*nochg*)
if [[ _errorCounter -gt 1 ]]
then
errorMessage "The update client was spamming unnecessary update requests, something might be wrong with your IP-Check site."
errorMessage "Fix your config and then delete $_statusFile or restart your docker container"
return 1
fi
;;
nohost | !yours)
if [[ "$_statusHostname" == "$DYNB_DYN_DOMAIN" && ("$_statusUsername" == "$DYNB_USERNAME" || $_statusUsername == "$DYNB_TOKEN") ]]; then
errorMessage "Hostname supplied does not exist under specified account, enter new login credentials before performing an additional request."
return 1
else delete_status_file
fi
return 0
;;
*badauth* | 401 | *Unauthorized*)
if [[ "$_statusUsername" == "$DYNB_USERNAME" && ("$_statusPassword" == "$DYNB_PASSWORD" || $_statusPassword == "$DYNB_TOKEN") ]]; then
errorMessage "Invalid username password combination."
return 1
else delete_status_file
fi
return 0
;;
badagent)
errorMessage "Client is deactivated by provider."
echo "Please file an issue at GitHub or try another client :)"
return 1
;;
!donator)
errorMessage "An update request was sent, including a feature that is not available to that particular user such as offline options."
echo "Fix your config and then manually remove $_statusFile to reset the client blockade"
echo "If it still fails file an issue at github or try another client :)"
return 1
;;
abuse)
errorMessage "Username is blocked due to abuse."
echo "Fix your config and then manually remove $_statusFile to reset the client blockade"
echo "If it still fails file an issue at github or try another client :)"
return 1
;;
servererror | 911 | 5* | *'Too Many Requests'*)
if check_delay 30
then
delete_status_file
return 0
else
errorMessage "$_previous_response_was: The provider currently has a fatal error."
return 1
fi
;;
*'Bad Request'*)
if [[ "$_statusUsername" == "$DYNB_USERNAME" && ("$_statusPassword" == "$DYNB_PASSWORD" || $_statusPassword == "$DYNB_TOKEN") ]]
then
errorMessage "Bad Request: Please check your credentials, maybe your token is invalid."
return 1
else delete_status_file
fi
return 0
;;
*)
if [[ $_errorCounter -gt 1 ]] && [[ "$_curl_exit_code_was" -eq 0 ]]
then
errorMessage "An unknown response code has repeatedly been received. $_response"
return 1
elif [[ $_errorCounter -gt 1 ]] && [[ "$_curl_exit_code_was" -eq 7 ]]
then
if check_delay 15
then
delete_status_file
return 0
else return 1
fi
else return 0
fi
;;
esac
}
function check_delay
{
local minutes=$1
local seconds=$(( minutes * 60 ))
delta=$(($(date +%s) - _eventTime))
if [[ $delta -lt $seconds ]]
then
errorMessage "DynB only executes an update when $minutes minutes have passed since the last failed request, $(date --date=@$delta -u +%M) minutes have already passed."
if loopMode
then sleep $seconds
else return 1
fi
else return 0
fi
}
# requires parameter
# 1. param: 4 or 6 for IP version
function ipHasChanged
{
local ip_version=$1
getRemoteIP "$ip_version"
if test $? -gt 0
then return 1
fi
case ${ip_version} in
4)
getDNSIP A
_remote_IPv4=$_remote_ip
debugMessage "IPv4 from remote IP check server: $_remote_IPv4, IPv4 from DNS: $_dns_ip"
;;
6)
getDNSIP AAAA
_remote_IPv6=$_remote_ip
debugMessage "IPv6 from remote IP check server: $_remote_IPv6, IPv6 from DNS: $_dns_ip"
;;
esac
if [[ "$_remote_ip" == "$_dns_ip" ]]
then return 1
else
case ${ip_version} in
4) infoMessage "Remote IPv4: $_remote_IPv4 DNS IPv4: $_dns_ip";;
6) infoMessage "Remote IPv6: $_remote_IPv6 DNS IPv6: $_dns_ip";;
esac
return 0
fi
}
function handleParameters
{
# shellcheck disable=SC2154
if [[ $_arg_version == "on" ]]
then echo $_version; exit 0
fi
# shellcheck disable=SC2154
if [[ $_arg_link == "on" ]]
then ln --verbose --symbolic "$(realpath "$0")" "$HOME/.local/bin/dynb"; exit 0
fi
# shellcheck disable=SC2154
if [[ $_arg_reset == "on" ]] && test -f "$_statusFile"
then rm --verbose "$_statusFile"; exit 0
fi
# shellcheck disable=SC2154
if [[ $_arg_debug == "on" ]] || [[ $DYNB_DEBUG == true ]]
then _debug=true
fi
# shellcheck disable=SC2154
if [[ $_arg_update_method != "" ]]
then DYNB_UPDATE_METHOD=$_arg_update_method
fi
# shellcheck disable=SC2154
if [[ $_arg_ip_mode != "" ]]
then DYNB_IP_MODE=$_arg_ip_mode
fi
# shellcheck disable=SC2154
if [[ $_arg_domain != "" ]]
then DYNB_DYN_DOMAIN=$_arg_domain
fi
# shellcheck disable=SC2154
if [[ $_arg_service_provider != "" ]]
then DYNB_SERVICE_PROVIDER=$_arg_service_provider
fi
# shellcheck disable=SC2154
if [[ $_arg_username != "" ]]
then DYNB_USERNAME=$_arg_username
fi
# shellcheck disable=SC2154
if [[ $_arg_password != "" ]]
then DYNB_PASSWORD=$_arg_password
fi
# shellcheck disable=SC2154
if [[ $_arg_token != "" ]]
then DYNB_TOKEN=$_arg_token
fi
# shellcheck disable=SC2154
if [[ $_arg_interval != "" ]]
then DYNB_INTERVAL=$_arg_interval
fi
if [[ -z $DYNB_INTERVAL ]]
then _loopMode=0
elif [[ $DYNB_INTERVAL -lt _minimum_looptime ]]
then
DYNB_INTERVAL=$_minimum_looptime
_loopMode=true
else _loopMode=true
fi
if [[ $_network_interface != "" ]]
then _interface_str="--interface $_network_interface"
fi
if [[ $DYNB_IP_MODE == d* ]]
then
_is_IPv4_enabled=true
_is_IPv6_enabled=true
fi
if [[ $DYNB_IP_MODE == *4* ]]
then _is_IPv4_enabled=true
fi
if [[ $DYNB_IP_MODE == *6* ]]
then _is_IPv6_enabled=true
fi
# shellcheck disable=SC2154
if [[ -n $DYNB_IPv4_CHECK_SITE ]]
then _ipv4_checker=$DYNB_IPv4_CHECK_SITE
fi
# shellcheck disable=SC2154
if [[ -n $DYNB_IPv6_CHECK_SITE ]]
then _ipv6_checker=$DYNB_IPv6_CHECK_SITE
fi
if [[ -n $DYNB_DNS_CHECK_SERVER ]]
then _DNS_checkServer=$DYNB_DNS_CHECK_SERVER
fi
return 0
}
function checkDependencies
{
failCounter=0
for i in curl dig; do
if ! command -v $i >/dev/null 2>&1
then
errorMessage "could not find \"$i\", DynB depends on it. "
((failCounter++))
fi
done
[[ -x $(command -v jq 2>/dev/null) ]] || {
if [[ $DYNB_UPDATE_METHOD != dyndns* ]]
then
echo "This script depends on jq and it is not available." >&2
((failCounter++))
fi
}
if [[ failCounter -gt 0 ]]
then exit 1
fi
}
function doUnsets
{
unset _network_interface
unset _DNS_checkServer
unset _dns_records
unset _has_getopt
unset _help_message
unset _INWX_JSON_API_URL
unset _ipv4_checker
unset _ipv6_checker
unset _is_IPv4_enabled
unset _is_IPv6_enabled
unset _main_domain
unset _remote_IPv4
unset _remote_IPv6
unset _version
unset DYNB_DYN_DOMAIN
unset DYNB_USERNAME
unset DYNB_PASSWORD
unset DYNB_TOKEN
unset DYNB_SERVICE_PROVIDER
unset DYNB_IP_MODE
unset DYNB_INTERVAL
unset DYNB_IPv4_CHECK_SITE
unset DYNB_IPv6_CHECK_SITE
unset DYNB_DNS_CHECK_SERVER
unset DYNB_DEBUG
}
function doDomrobotUpdates
{
getMainDomain
fetchDNSRecords
if [[ $_is_IPv4_enabled == true ]]
then
if ipHasChanged 4
then updateRecord 4
else debugMessage "Skip IPv4 record update, it is already up to date"
fi
fi
if [[ $_is_IPv6_enabled == true ]]
then
if ipHasChanged 6
then updateRecord 6
else debugMessage "Skip IPv6 record update, it is already up to date"
fi
fi
}
function delete_status_file
{
if test -f "$_statusFile"
then
debugMessage "Delete status file with previous errors"
rm "$_statusFile"
unset _curl_exit_code_was
unset _previous_response_was
unset _eventTime
unset _errorCounter
unset _statusHostname
unset _statusUsername
unset _statusPassword
fi
}
function doDynDNS2Updates
{
prepare_request_parameters
changed=0
if [[ $_is_IPv4_enabled == true ]] && ipHasChanged 4
then ((changed += 1))
fi
if [[ $_is_IPv6_enabled == true ]] && ipHasChanged 6
then ((changed += 1))
fi
if [[ $changed -gt 0 ]]
then
if checkStatus
then
debugMessage "checkStatus has no errors, try update"
if dynupdate
then
debugMessage "DynDNS2 update success"
delete_status_file
else
debugMessage "Save new status after dynupdate has failed"
setStatus "$_curl_exit_code" "$_response" "$(date +%s)" $((_errorCounter += 1)) "$DYNB_DYN_DOMAIN" "${DYNB_USERNAME}" "${DYNB_PASSWORD}${DYNB_TOKEN}"
fi
else debugMessage "Skip DynDNS2 update, checkStatus fetched previous error."
fi
else debugMessage "Skip DynDNS2 update"
fi
}
function doUpdates
{
if [[ $DYNB_UPDATE_METHOD == "domrobot" ]]
then doDomrobotUpdates
elif [[ $DYNB_UPDATE_METHOD == "dyndns" ]]
then doDynDNS2Updates
fi
}
function ipv6_is_not_working
{
execute_connectivity_check 6
return $?
}
function ipv4_is_not_working
{
execute_connectivity_check 4
return $?
}