Browse Source

Merge pull request #420 from chris5560/master

ddns-scripts: add retry loop to verify dns and proxy
sbyx 10 years ago
parent
commit
d88ade1411

+ 1
- 1
net/ddns-scripts/Makefile View File

@@ -2,7 +2,7 @@ include $(TOPDIR)/rules.mk
2 2
 
3 3
 PKG_NAME:=ddns-scripts
4 4
 PKG_VERSION:=2.0.1
5
-PKG_RELEASE:=8
5
+PKG_RELEASE:=9
6 6
 PKG_LICENSE:=GPL-2.0
7 7
 
8 8
 PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)

+ 18
- 14
net/ddns-scripts/files/usr/lib/ddns/dynamic_dns_functions.sh View File

@@ -63,6 +63,7 @@ ERR_LOCAL_IP=0		# error counter on getting local ip
63 63
 ERR_REG_IP=0		# error counter on getting DNS registered ip
64 64
 ERR_SEND=0		# error counter on sending update to DNS provider
65 65
 ERR_UPDATE=0		# error counter on different local and registered ip
66
+ERR_VERIFY=0		# error counter verifying proxy- and dns-servers
66 67
 
67 68
 # format to show date information in log and luci-app-ddns default ISO 8601 format
68 69
 DATE_FORMAT=$(uci -q get ddns.global.date_format) || DATE_FORMAT="%F %R"
@@ -122,10 +123,10 @@ start_daemon_for_all_ddns_sections()
122 123
 	local __SECTIONID=""
123 124
 	local __IFACE=""
124 125
 
125
-	config_cb() 
126
+	config_cb()
126 127
 	{
127 128
 		# only look for section type "service", ignore everything else
128
-		[ "$1" == "service" ] && __SECTIONS="$__SECTIONS $2"
129
+		[ "$1" = "service" ] && __SECTIONS="$__SECTIONS $2"
129 130
 	}
130 131
 	config_load "ddns"
131 132
 
@@ -214,7 +215,7 @@ __urlencode() {
214 215
 	return 0
215 216
 }
216 217
 
217
-# extract update_url for given DDNS Provider from
218
+# extract url or script for given DDNS Provider from
218 219
 # file /usr/lib/ddns/services for IPv4 or from
219 220
 # file /usr/lib/ddns/services_ipv6 for IPv6
220 221
 get_service_data() {
@@ -387,25 +388,27 @@ __verify_host_port() {
387 388
 	# command error
388 389
 	[ $__ERR -gt 0 ] && {
389 390
 		verbose_echo "\n!!!!!!!!! ERROR =: BusyBox nslookup Error '$__ERR'\n$(eval $__ERRPROG)\n"
390
-		syslog_err "DNS Resolver Error - BusyBox nslookup Error: '$__ERR'"
391
+		syslog_err "DNS Resolver Error - BusyBox nslookup Error '$__ERR'"
391 392
 		return 2
392 393
 	} || {
393 394
 		# we need to run twice because multi-line output needs to be directly piped to grep because
394 395
 		# pipe returns return code of last prog in pipe but we need errors from nslookup command
395
-		__IPV4=$(eval $__RUNPROG | sed '1,2d' | grep -o "Name:\|Address.*" | grep -m 1 -o "$IPV4_REGEX")
396
-		__IPV6=$(eval $__RUNPROG | sed '1,2d' | grep -o "Name:\|Address.*" | grep -m 1 -o "$IPV6_REGEX")
396
+		__IPV4=$(eval $__RUNPROG | sed -ne "3,\$ { s/^Address [0-9]*: \($IPV4_REGEX\).*$/\\1/p }")
397
+		__IPV6=$(eval $__RUNPROG | sed -ne "3,\$ { s/^Address [0-9]*: \($IPv6_REGEX\).*$/\\1/p }")
397 398
 	}
398 399
 
399 400
 	# check IP version if forced
400 401
 	if [ $force_ipversion -ne 0 ]; then
401
-		[ $use_ipv6 -eq 0 -a -z "$__IPV4" ] && return 4
402
-		[ $use_ipv6 -eq 1 -a -z "$__IPV6" ] && return 4
402
+		__ERR=0
403
+		[ $use_ipv6 -eq 0 -a -z "$__IPV4" ] && __ERR=4
404
+		[ $use_ipv6 -eq 1 -a -z "$__IPV6" ] && __ERR=6
405
+		[ $__ERR -gt 0 ] && critical_error "Invalid host: Error '4' - Force IP Version IPv$__ERR not supported"
403 406
 	fi
404 407
 
405 408
 	# verify nc command
406 409
 	# busybox nc compiled without -l option "NO OPT l!" -> critical error
407 410
 	nc --help 2>&1 | grep -iq "NO OPT l!" && \
408
-		critical_error "Busybox nc: netcat compiled with errors"
411
+		critical_error "Busybox nc: netcat compiled without -l option, error 'NO OPT l!'"
409 412
 	# busybox nc compiled with extensions
410 413
 	nc --help 2>&1 | grep -q "\-w" && __NCEXT="TRUE"
411 414
 
@@ -428,7 +431,7 @@ __verify_host_port() {
428 431
 		__ERR=$?
429 432
 		[ $__ERR -eq 0 ] && return 0
430 433
 		verbose_echo "\n!!!!!!!!! ERROR =: BusyBox nc Error '$__ERR'\n$(eval $__ERRPROG)\n"
431
-		syslog_err "host verify Error - BusyBox nc Error: '$__ERR'"
434
+		syslog_err "host verify Error - BusyBox nc Error '$__ERR'"
432 435
 		return 3
433 436
 	else		# nc compiled without extensions (no timeout support)
434 437
 		__RUNPROG="__timeout 2 -- nc $__IP $__PORT </dev/null >/dev/null 2>&1"
@@ -437,7 +440,7 @@ __verify_host_port() {
437 440
 		__ERR=$?
438 441
 		[ $__ERR -eq 0 ] && return 0
439 442
 		verbose_echo "\n!!!!!!!!! ERROR =: BusyBox nc Error '$__ERR' (timeout)"
440
-		syslog_err "host verify Error - BusyBox nc Error: '$__ERR' (timeout)"
443
+		syslog_err "host verify Error - BusyBox nc Error '$__ERR' (timeout)"
441 444
 		return 3
442 445
 	fi
443 446
 }
@@ -454,8 +457,9 @@ verify_dns() {
454 457
 verify_proxy() {
455 458
 	# $1	Proxy-String to verify
456 459
 	#		complete entry		user:password@host:port
460
+	# 					inside user and password NO '@' of ":" allowed 
457 461
 	#		host and port only	host:port
458
-	#		host only		host		unsupported
462
+	#		host only		host		ERROR unsupported
459 463
 	#		IPv4 address instead of host	123.234.234.123
460 464
 	#		IPv6 address instead of host	[xxxx:....:xxxx]	in square bracket
461 465
 	local __TMP __HOST __PORT
@@ -477,8 +481,8 @@ verify_proxy() {
477 481
 		__HOST=$(echo $__TMP | awk -F ":" '{print $1}')
478 482
 		__PORT=$(echo $__TMP | awk -F ":" '{print $2}')
479 483
 	fi
480
-	# No Port detected ERROR 5
481
-	[ -z "$__PORT" ] && return 5
484
+	# No Port detected
485
+	[ -z "$__PORT" ] && critical_error "Invalid Proxy server Error '5' - proxy port missing"
482 486
 
483 487
 	__verify_host_port "$__HOST" "$__PORT"
484 488
 }

+ 63
- 24
net/ddns-scripts/files/usr/lib/ddns/dynamic_dns_updater.sh View File

@@ -213,36 +213,75 @@ fi
213 213
 verbose_echo "       waiting =: 10 seconds for interfaces to fully come up"
214 214
 sleep 10
215 215
 
216
-# verify DNS server
217
-[ -n "$dns_server" ] && {
218
-	verbose_echo "******* VERIFY =: DNS server '$dns_server'"
216
+# verify DNS server: 
217
+# do with retry's because there might be configurations
218
+# not directly could connect to outside dns when interface is already up
219
+ERR_VERIFY=0	# reset err counter
220
+while [ -n "$dns_server" ]; do
221
+	[ $ERR_VERIFY -eq 0 ] && verbose_echo "******* VERIFY =: DNS server '$dns_server'"
219 222
 	verify_dns "$dns_server"
220
-	case $? in
221
-		0)	;;	# everything OK
222
-		2)	critical_error "Invalid DNS server Error: '2' - nslookup can not resolve host";;
223
-		3)	critical_error "Invalid DNS server Error: '3' - nc (netcat) can not connect";;
224
-		4)	critical_error "Invalid DNS server Error: '4' - Forced IP Version don't matched";;
225
-		*)	critical_error "Invalid DNS server Error: '1' - unspecific error";;
223
+	ERR_LAST=$?			# save return value
224
+	[ $ERR_LAST -eq 0 ] && break	# everything ok leave while loop
225
+	ERR_VERIFY=$(( $ERR_VERIFY + 1 ))
226
+	# if error count > retry_count leave here with critical error
227
+	[ $ERR_VERIFY -gt $retry_count ] && {
228
+		case $ERR_LAST in
229
+			2)	critical_error "Invalid DNS server Error: '2' - nslookup can not resolve host";;
230
+			3)	critical_error "Invalid DNS server Error: '3' - nc (netcat) can not connect";;
231
+			*)	critical_error "Invalid DNS server Error: '$ERR_LAST' - unspecific error";;
232
+		esac
233
+	}
234
+	case $ERR_LAST in
235
+		2)	syslog_err "Invalid DNS server Error: '2' - nslookup can not resolve host - retry $ERR_VERIFY/$retry_count in $RETRY_SECONDS seconds\n";;
236
+		3)	syslog_err "Invalid DNS server Error: '3' - nc (netcat) can not connect - retry $ERR_VERIFY/$retry_count in $RETRY_SECONDS seconds\n";;
237
+		*)	syslog_err "Invalid DNS server Error: '$ERR_LAST' - unspecific error - retry $ERR_VERIFY/$retry_count in $RETRY_SECONDS seconds\n";;
226 238
 	esac
227
-}
239
+	[ $VERBOSE_MODE -gt 1 ] && {
240
+		# VERBOSE_MODE > 1 then NO retry
241
+		verbose_echo "\n!!!!!!!!! ERROR =: Verbose Mode - NO retry\n"
242
+		break
243
+	}
244
+	verbose_echo "******** RETRY =: DNS server '$dns_server' - retry $ERR_VERIFY/$retry_count in $RETRY_SECONDS seconds"
245
+	sleep $RETRY_SECONDS
246
+done
228 247
 
229 248
 # verify Proxy server and set environment
249
+# do with retry's because there might be configurations
250
+# not directly could connect to outside dns when interface is already up
251
+ERR_VERIFY=0	# reset err counter
230 252
 [ -n "$proxy" ] && {
231
-	verbose_echo "******* VERIFY =: Proxy server 'http://$proxy'"
253
+	[ $ERR_VERIFY -eq 0 ] && verbose_echo "******* VERIFY =: Proxy server 'http://$proxy'"
232 254
 	verify_proxy "$proxy"
233
-	case $? in
234
-		0)	# everything OK
235
-			export HTTP_PROXY="http://$proxy"
236
-			export HTTPS_PROXY="http://$proxy"
237
-			export http_proxy="http://$proxy"
238
-			export https_proxy="http://$proxy"
239
-			;;
240
-		2)	critical_error "Invalid Proxy server Error: '2' - nslookup can not resolve host";;
241
-		3)	critical_error "Invalid Proxy server Error: '3' - nc (netcat) can not connect";;
242
-		4)	critical_error "Invalid Proxy server Error: '4' - Forced IP Version don't matched";;
243
-		5)	critical_error "Invalid Proxy server Error: '5' - proxy port missing";;
244
-		*)	critical_error "Invalid Proxy server Error: '1' - unspecific error";;
255
+	ERR_LAST=$?			# save return value
256
+	[ $ERR_LAST -eq 0 ] && {
257
+		# everything ok set proxy and leave while loop
258
+		export HTTP_PROXY="http://$proxy"
259
+		export HTTPS_PROXY="http://$proxy"
260
+		export http_proxy="http://$proxy"
261
+		export https_proxy="http://$proxy"
262
+		break
263
+	}
264
+	ERR_VERIFY=$(( $ERR_VERIFY + 1 ))
265
+	# if error count > retry_count leave here with critical error
266
+	[ $ERR_VERIFY -gt $retry_count ] && {
267
+		case $ERR_LAST in
268
+			2)	critical_error "Invalid Proxy server Error '2' - nslookup can not resolve host";;
269
+			3)	critical_error "Invalid Proxy server Error '3' - nc (netcat) can not connect";;
270
+			*)	critical_error "Invalid Proxy server Error '$ERR_LAST' - unspecific error";;
271
+		esac
272
+	}
273
+	case $ERR_LAST in
274
+		2)	syslog_err "Invalid Proxy server Error '2' - nslookup can not resolve host - retry $ERR_VERIFY/$retry_count in $RETRY_SECONDS seconds\n";;
275
+		3)	syslog_err "Invalid Proxy server Error '3' - nc (netcat) can not connect - retry $ERR_VERIFY/$retry_count in $RETRY_SECONDS seconds\n";;
276
+		*)	syslog_err "Invalid Proxy server Error '$ERR_LAST' - unspecific error - retry $ERR_VERIFY/$retry_count in $RETRY_SECONDS seconds\n";;
245 277
 	esac
278
+	[ $VERBOSE_MODE -gt 1 ] && {
279
+		# VERBOSE_MODE > 1 then NO retry
280
+		verbose_echo "\n!!!!!!!!! ERROR =: Verbose Mode - NO retry\n"
281
+		break
282
+	}
283
+	verbose_echo "******** RETRY =: Proxy server 'http://$proxy' - retry $ERR_VERIFY/$retry_count in $RETRY_SECONDS seconds"
284
+	sleep $RETRY_SECONDS
246 285
 }
247 286
 
248 287
 # let's check if there is already an IP registered at the web
@@ -304,7 +343,7 @@ while : ; do
304 343
 		if [ $VERBOSE_MODE -gt 2 ]; then
305 344
 			verbose_echo "  VERBOSE MODE =: NO UPDATE send to DDNS provider"
306 345
 		elif [ "$LOCAL_IP" != "$REGISTERED_IP" ]; then
307
-			verbose_echo "******* UPDATE =: LOCAL: '$LOCAL_IP' <=> REGISTERED: '$REGISTERED_IP'"
346
+			verbose_echo "******* UPDATE =: LOCAL: '$LOCAL_IP' <> REGISTERED: '$REGISTERED_IP'"
308 347
 		else
309 348
 			verbose_echo "******* FORCED =: LOCAL: '$LOCAL_IP' == REGISTERED: '$REGISTERED_IP'"
310 349
 		fi

+ 1
- 1
net/ddns-scripts/files/usr/lib/ddns/services View File

@@ -71,7 +71,7 @@
71 71
 "duiadns.net"   "http://ipv4.duia.ro/dynamic.duia?host=[DOMAIN]&password=[PASSWORD]&ip4=[IP]"
72 72
 
73 73
 # Two-DNS - Simply. Connected. Everywhere.
74
-"Two-DNS" "http://[USERNAME]:[PASSWORD]@update.twodns.de/update?hostname=[DOMAIN]&ip=[IP]"
74
+"twodns.de" "http://[USERNAME]:[PASSWORD]@update.twodns.de/update?hostname=[DOMAIN]&ip=[IP]"
75 75
 
76 76
 # MyDNS.JP
77 77
 "mydns.jp"	"http://www.mydns.jp/directip.html?MID=[USERNAME]&PWD=[PASSWORD]&IPV4ADDR=[IP]"