|
@@ -34,13 +34,15 @@ VERBOSE_MODE=1 # default mode is log to console, but easily changed with parame
|
34
|
34
|
|
35
|
35
|
# directory to store run information to.
|
36
|
36
|
RUNDIR=$(uci -q get ddns.global.run_dir) || RUNDIR="/var/run/ddns"
|
|
37
|
+[ -d $RUNDIR ] || mkdir -p -m755 $RUNDIR
|
37
|
38
|
# NEW # directory to store log files
|
38
|
39
|
LOGDIR=$(uci -q get ddns.global.log_dir) || LOGDIR="/var/log/ddns"
|
|
40
|
+[ -d $LOGDIR ] || mkdir -p -m755 $LOGDIR
|
39
|
41
|
LOGFILE="" # NEW # logfile can be enabled as new option
|
40
|
42
|
PIDFILE="" # pid file
|
41
|
43
|
UPDFILE="" # store UPTIME of last update
|
42
|
|
-DATFILE="/tmp/ddns_$$.dat" # save stdout data of WGet and other extern programs called
|
43
|
|
-ERRFILE="/tmp/ddns_$$.err" # save stderr output of WGet and other extern programs called
|
|
44
|
+DATFILE="" # save stdout data of WGet and other extern programs called
|
|
45
|
+ERRFILE="" # save stderr output of WGet and other extern programs called
|
44
|
46
|
|
45
|
47
|
# number of lines to before rotate logfile
|
46
|
48
|
LOGLINES=$(uci -q get ddns.global.log_lines) || LOGLINES=250
|
|
@@ -224,7 +226,6 @@ write_log() {
|
224
|
226
|
[ $VERBOSE_MODE -gt 0 -o $__EXIT -gt 0 ] && echo -e "$__MSG"
|
225
|
227
|
# write to logfile
|
226
|
228
|
if [ ${use_logfile:-1} -eq 1 -o $VERBOSE_MODE -gt 1 ]; then
|
227
|
|
- [ -d $LOGDIR ] || mkdir -p -m 755 $LOGDIR
|
228
|
229
|
echo -e "$__MSG" >> $LOGFILE
|
229
|
230
|
# VERBOSE_MODE > 1 then NO loop so NO truncate log to $LOGLINES lines
|
230
|
231
|
[ $VERBOSE_MODE -gt 1 ] || sed -i -e :a -e '$q;N;'$LOGLINES',$D;ba' $LOGFILE
|
|
@@ -302,8 +303,7 @@ get_service_data() {
|
302
|
303
|
awk ' gsub("\x27", "\"") { if ($1~/^[^\"]*$/) $1="\""$1"\"" }; { if ( $NF~/^[^\"]*$/) $NF="\""$NF"\"" }; { print $0 }')
|
303
|
304
|
|
304
|
305
|
IFS=$__NEWLINE_IFS
|
305
|
|
- for __LINE in $__SERVICES
|
306
|
|
- do
|
|
306
|
+ for __LINE in $__SERVICES; do
|
307
|
307
|
#grep out proper parts of data and use echo to remove quotes
|
308
|
308
|
__NAME=$(echo $__LINE | grep -o "^[\t ]*\"[^\"]*\"" | xargs -r -n1 echo)
|
309
|
309
|
__DATA=$(echo $__LINE | grep -o "\"[^\"]*\"[\t ]*$" | xargs -r -n1 echo)
|
|
@@ -459,7 +459,7 @@ verify_host_port() {
|
459
|
459
|
# command error
|
460
|
460
|
[ $__ERR -gt 0 ] && {
|
461
|
461
|
write_log 3 "DNS Resolver Error - BusyBox nslookup Error '$__ERR'"
|
462
|
|
- write_log 7 "Error:\n$(cat $ERRFILE)"
|
|
462
|
+ write_log 7 "$(cat $ERRFILE)"
|
463
|
463
|
return 2
|
464
|
464
|
}
|
465
|
465
|
# extract IP address
|
|
@@ -499,7 +499,7 @@ verify_host_port() {
|
499
|
499
|
__ERR=$?
|
500
|
500
|
[ $__ERR -eq 0 ] && return 0
|
501
|
501
|
write_log 3 "Connect error - BusyBox nc (netcat) Error '$__ERR'"
|
502
|
|
- write_log 7 "Error:\n$(cat $ERRFILE)"
|
|
502
|
+ write_log 7 "$(cat $ERRFILE)"
|
503
|
503
|
return 3
|
504
|
504
|
else # nc compiled without extensions (no timeout support)
|
505
|
505
|
__RUNPROG="timeout 2 -- /usr/bin/nc $__IP $__PORT </dev/null >$DATFILE 2>$ERRFILE"
|
|
@@ -533,7 +533,7 @@ verify_dns() {
|
533
|
533
|
elif [ $__ERR -ne 0 ]; then
|
534
|
534
|
__CNT=$(( $__CNT + 1 )) # increment error counter
|
535
|
535
|
# if error count > retry_count leave here
|
536
|
|
- [ $__CNT -gt $retry_count ] && \
|
|
536
|
+ [ $retry_count -gt 0 -a $__CNT -gt $retry_count ] && \
|
537
|
537
|
write_log 14 "Verify DNS server '$1' failed after $retry_count retries"
|
538
|
538
|
|
539
|
539
|
write_log 4 "Verify DNS server '$1' failed - retry $__CNT/$retry_count in $RETRY_SECONDS seconds"
|
|
@@ -593,7 +593,7 @@ verify_proxy() {
|
593
|
593
|
elif [ $__ERR -gt 0 ]; then
|
594
|
594
|
__CNT=$(( $__CNT + 1 )) # increment error counter
|
595
|
595
|
# if error count > retry_count leave here
|
596
|
|
- [ $__CNT -gt $retry_count ] && \
|
|
596
|
+ [ $retry_count -gt 0 -a $__CNT -gt $retry_count ] && \
|
597
|
597
|
write_log 14 "Verify Proxy server '$1' failed after $retry_count retries"
|
598
|
598
|
|
599
|
599
|
write_log 4 "Verify Proxy server '$1' failed - retry $__CNT/$retry_count in $RETRY_SECONDS seconds"
|
|
@@ -637,7 +637,7 @@ do_transfer() {
|
637
|
637
|
# disable proxy if no set (there might be .wgetrc or .curlrc or wrong environment set)
|
638
|
638
|
[ -z "$proxy" ] && __PROG="$__PROG --no-proxy"
|
639
|
639
|
|
640
|
|
- __RUNPROG="$__PROG $__URL" # build final command
|
|
640
|
+ __RUNPROG="$__PROG '$__URL'" # build final command
|
641
|
641
|
__PROG="GNU Wget" # reuse for error logging
|
642
|
642
|
|
643
|
643
|
# 2nd choice is cURL IPv4/IPv6/HTTPS
|
|
@@ -671,7 +671,7 @@ do_transfer() {
|
671
|
671
|
write_log 13 "cURL: libcurl compiled without Proxy support"
|
672
|
672
|
fi
|
673
|
673
|
|
674
|
|
- __RUNPROG="$__PROG $__URL" # build final command
|
|
674
|
+ __RUNPROG="$__PROG '$__URL'" # build final command
|
675
|
675
|
__PROG="cURL" # reuse for error logging
|
676
|
676
|
|
677
|
677
|
# busybox Wget (did not support neither IPv6 nor HTTPS)
|
|
@@ -686,8 +686,8 @@ do_transfer() {
|
686
|
686
|
# disable proxy if no set (there might be .wgetrc or .curlrc or wrong environment set)
|
687
|
687
|
[ -z "$proxy" ] && __PROG="$__PROG -Y off"
|
688
|
688
|
|
689
|
|
- __RUNPROG="$__PROG $__URL 2>$ERRFILE" # build final command
|
690
|
|
- __PROG="Busybox Wget" # reuse for error logging
|
|
689
|
+ __RUNPROG="$__PROG '$__URL' 2>$ERRFILE" # build final command
|
|
690
|
+ __PROG="Busybox Wget" # reuse for error logging
|
691
|
691
|
|
692
|
692
|
else
|
693
|
693
|
write_log 13 "Neither 'Wget' nor 'cURL' installed or executable"
|
|
@@ -695,7 +695,7 @@ do_transfer() {
|
695
|
695
|
|
696
|
696
|
while : ; do
|
697
|
697
|
write_log 7 "#> $__RUNPROG"
|
698
|
|
- $__RUNPROG # DO transfer
|
|
698
|
+ eval $__RUNPROG # DO transfer
|
699
|
699
|
__ERR=$? # save error code
|
700
|
700
|
[ $__ERR -eq 0 ] && return 0 # no error leave
|
701
|
701
|
[ $LUCI_HELPER ] && return 1 # no retry if called by LuCI helper script
|
|
@@ -711,7 +711,7 @@ do_transfer() {
|
711
|
711
|
|
712
|
712
|
__CNT=$(( $__CNT + 1 )) # increment error counter
|
713
|
713
|
# if error count > retry_count leave here
|
714
|
|
- [ $__CNT -gt $retry_count ] && \
|
|
714
|
+ [ $retry_count -gt 0 -a $__CNT -gt $retry_count ] && \
|
715
|
715
|
write_log 14 "Transfer failed after $retry_count retries"
|
716
|
716
|
|
717
|
717
|
write_log 4 "Transfer failed - retry $__CNT/$retry_count in $RETRY_SECONDS seconds"
|
|
@@ -750,11 +750,11 @@ send_update() {
|
750
|
750
|
|
751
|
751
|
write_log 7 "DDNS Provider answered:\n$(cat $DATFILE)"
|
752
|
752
|
|
753
|
|
- # analyse provider answers
|
754
|
|
- # "good [IP_ADR]" = successful
|
755
|
|
- # "nochg [IP_ADR]" = no change but OK
|
756
|
|
- grep -E "good|nochg" $DATFILE >/dev/null 2>&1
|
757
|
|
- return $? # "0" if "good" or "nochg" found
|
|
753
|
+ return 0
|
|
754
|
+ # TODO analyse providers answer
|
|
755
|
+ # "good" or "nochg" = dyndns.com compatible API
|
|
756
|
+ # grep -i -E "good|nochg" $DATFILE >/dev/null 2>&1
|
|
757
|
+ # return $? # "0" if found
|
758
|
758
|
fi
|
759
|
759
|
}
|
760
|
760
|
|
|
@@ -846,6 +846,9 @@ get_local_ip () {
|
846
|
846
|
}
|
847
|
847
|
|
848
|
848
|
[ $LUCI_HELPER ] && return 1 # no retry if called by LuCI helper script
|
|
849
|
+
|
|
850
|
+ write_log 7 "Data detected:\n$(cat $DATFILE)"
|
|
851
|
+
|
849
|
852
|
[ $VERBOSE_MODE -gt 1 ] && {
|
850
|
853
|
# VERBOSE_MODE > 1 then NO retry
|
851
|
854
|
write_log 4 "Get local IP via '$ip_source' failed - Verbose Mode: $VERBOSE_MODE - NO retry on error"
|
|
@@ -854,9 +857,8 @@ get_local_ip () {
|
854
|
857
|
|
855
|
858
|
__CNT=$(( $__CNT + 1 )) # increment error counter
|
856
|
859
|
# if error count > retry_count leave here
|
857
|
|
- [ $__CNT -gt $retry_count ] && \
|
|
860
|
+ [ $retry_count -gt 0 -a $__CNT -gt $retry_count ] && \
|
858
|
861
|
write_log 14 "Get local IP via '$ip_source' failed after $retry_count retries"
|
859
|
|
-
|
860
|
862
|
write_log 4 "Get local IP via '$ip_source' failed - retry $__CNT/$retry_count in $RETRY_SECONDS seconds"
|
861
|
863
|
sleep $RETRY_SECONDS &
|
862
|
864
|
PID_SLEEP=$!
|
|
@@ -908,7 +910,7 @@ get_registered_ip() {
|
908
|
910
|
__ERR=$?
|
909
|
911
|
if [ $__ERR -ne 0 ]; then
|
910
|
912
|
write_log 3 "$__PROG error: '$__ERR'"
|
911
|
|
- write_log 7 "Error:\n$(cat $ERRFILE)"
|
|
913
|
+ write_log 7 "$(cat $ERRFILE)"
|
912
|
914
|
else
|
913
|
915
|
if [ "$__PROG" = "BIND host" ]; then
|
914
|
916
|
__DATA=$(cat $DATFILE | awk -F "address " '/has/ {print $2; exit}' )
|
|
@@ -934,7 +936,7 @@ get_registered_ip() {
|
934
|
936
|
|
935
|
937
|
__CNT=$(( $__CNT + 1 )) # increment error counter
|
936
|
938
|
# if error count > retry_count leave here
|
937
|
|
- [ $__CNT -gt $retry_count ] && \
|
|
939
|
+ [ $retry_count -gt 0 -a $__CNT -gt $retry_count ] && \
|
938
|
940
|
write_log 14 "Get registered/public IP for '$domain' failed after $retry_count retries"
|
939
|
941
|
|
940
|
942
|
write_log 4 "Get registered/public IP for '$domain' failed - retry $__CNT/$retry_count in $RETRY_SECONDS seconds"
|
|
@@ -966,18 +968,19 @@ trap_handler() {
|
966
|
968
|
[ $PID_SLEEP -ne 0 ] && kill -$1 $PID_SLEEP 2>/dev/null # kill pending sleep if exist
|
967
|
969
|
|
968
|
970
|
case $1 in
|
969
|
|
- 0) if [ $__ERR -eq 0 ]; then
|
|
971
|
+ 0) if [ $__ERR -eq 0 ]; then
|
970
|
972
|
write_log 5 "PID '$$' exit normal at $(eval $DATE_PROG)\n"
|
971
|
973
|
else
|
972
|
974
|
write_log 4 "PID '$$' exit WITH ERROR '$__ERR' at $(eval $DATE_PROG)\n"
|
973
|
975
|
fi ;;
|
974
|
|
- 1) write_log 6 "PID '$$' received 'SIGHUP' at $(eval $DATE_PROG)"
|
975
|
|
- eval "$0 $SECTION_ID $VERBOSE_MODE &" # reload config via restarting script
|
976
|
|
- exit 0 ;;
|
977
|
|
- 2) write_log 5 "PID '$$' terminated by 'SIGINT' at $(eval $DATE_PROG)\n";;
|
978
|
|
- 3) write_log 5 "PID '$$' terminated by 'SIGQUIT' at $(eval $DATE_PROG)\n";;
|
|
976
|
+ 1) write_log 6 "PID '$$' received 'SIGHUP' at $(eval $DATE_PROG)"
|
|
977
|
+ # reload config via starting the script again
|
|
978
|
+ eval "/usr/lib/ddns/dynamic_dns_updater.sh $SECTION_ID $VERBOSE_MODE &"
|
|
979
|
+ exit 0 ;; # and leave this one
|
|
980
|
+ 2) write_log 5 "PID '$$' terminated by 'SIGINT' at $(eval $DATE_PROG)\n";;
|
|
981
|
+ 3) write_log 5 "PID '$$' terminated by 'SIGQUIT' at $(eval $DATE_PROG)\n";;
|
979
|
982
|
15) write_log 5 "PID '$$' terminated by 'SIGTERM' at $(eval $DATE_PROG)\n";;
|
980
|
|
- *) write_log 13 "Unhandled signal '$1' in 'trap_handler()'";;
|
|
983
|
+ *) write_log 13 "Unhandled signal '$1' in 'trap_handler()'";;
|
981
|
984
|
esac
|
982
|
985
|
|
983
|
986
|
__PIDS=$(pgrep -P $$) # get my childs (pgrep prints with "newline")
|