mirror of
https://github.com/openwrt/packages.git
synced 2025-12-21 23:34:31 +04:00
ddns-scripts: refactor verify_proxy()
no calls to external utilities - use variable substitution. no regex. Signed-off-by: Paul Donald <newtwen+github@gmail.com>
This commit is contained in:
committed by
Florian Eckert
parent
38e02fbd57
commit
88dc8e8b88
@@ -598,7 +598,7 @@ verify_dns() {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
# analyze and verify given proxy string
|
# analyse and verify given proxy string
|
||||||
# $1 Proxy-String to verify
|
# $1 Proxy-String to verify
|
||||||
verify_proxy() {
|
verify_proxy() {
|
||||||
# complete entry user:password@host:port
|
# complete entry user:password@host:port
|
||||||
@@ -606,49 +606,62 @@ verify_proxy() {
|
|||||||
# host and port only host:port
|
# host and port only host:port
|
||||||
# host only host ERROR unsupported
|
# host only host ERROR unsupported
|
||||||
# IPv4 address instead of host 123.234.234.123
|
# IPv4 address instead of host 123.234.234.123
|
||||||
# IPv6 address instead of host [xxxx:....:xxxx] in square bracket
|
# IPv6 address instead of host [xxxx:....:xxxx] in square brackets
|
||||||
local __TMP __HOST __PORT
|
# local user password
|
||||||
local __ERR=255 # last error buffer
|
local host port rest error_count err_code
|
||||||
local __CNT=0 # error counter
|
|
||||||
|
err_code=255 # last error buffer
|
||||||
|
error_count=0 # error counter
|
||||||
|
|
||||||
[ $# -ne 1 ] && write_log 12 "Error calling 'verify_proxy()' - wrong number of parameters"
|
[ $# -ne 1 ] && write_log 12 "Error calling 'verify_proxy()' - wrong number of parameters"
|
||||||
write_log 7 "Verify Proxy server 'http://$1'"
|
write_log 7 "Verify Proxy server 'http://$1'"
|
||||||
|
|
||||||
# try to split user:password "@" host:port
|
if [ "${1#*'@'}" != "$1" ]; then
|
||||||
__TMP=$(echo $1 | awk -F "@" '{print $2}')
|
# Format: user:password@host:port or user:password@[ipv6]:port
|
||||||
# no "@" found - only host:port is given
|
# user="${1%%:*}" # currently unused
|
||||||
[ -z "$__TMP" ] && __TMP="$1"
|
# rest="${1#*:}"
|
||||||
# now lets check for IPv6 address
|
# password="${rest%%@*}" # currently unused
|
||||||
__HOST=$(echo $__TMP | grep -m 1 -o "$IPV6_REGEX")
|
|
||||||
# IPv6 host address found read port
|
# Extract the host:port part
|
||||||
if [ -n "$__HOST" ]; then
|
rest="${rest#*@}"
|
||||||
# IPv6 split at "]:"
|
|
||||||
__PORT=$(echo $__TMP | awk -F "]:" '{print $2}')
|
|
||||||
else
|
else
|
||||||
__HOST=$(echo $__TMP | awk -F ":" '{print $1}')
|
# Format: host:port or [ipv6]:port
|
||||||
__PORT=$(echo $__TMP | awk -F ":" '{print $2}')
|
rest="$1"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "${rest#*'['}" != "$rest" ]; then
|
||||||
|
# Format: [ipv6]:port
|
||||||
|
host="${rest%%]*}"
|
||||||
|
host="${host#[}" # Remove the leading '['
|
||||||
|
|
||||||
|
port="${rest##*:}"
|
||||||
|
else
|
||||||
|
host="${rest%%:*}"
|
||||||
|
port="${rest#*:}"
|
||||||
fi
|
fi
|
||||||
# No Port detected - EXITING
|
# No Port detected - EXITING
|
||||||
[ -z "$__PORT" ] && {
|
[ -z "$port" ] && {
|
||||||
[ -n "$LUCI_HELPER" ] && return 5
|
[ -n "$LUCI_HELPER" ] && return 5
|
||||||
write_log 14 "Invalid Proxy server Error '5' - proxy port missing"
|
write_log 14 "Invalid Proxy server Error '5' - proxy port missing"
|
||||||
}
|
}
|
||||||
|
|
||||||
while [ $__ERR -gt 0 ]; do
|
while [ "$err_code" -gt 0 ]; do
|
||||||
verify_host_port "$__HOST" "$__PORT"
|
verify_host_port "$host" "$port"
|
||||||
__ERR=$?
|
err_code=$?
|
||||||
if [ -n "$LUCI_HELPER" ]; then # no retry if called by LuCI helper script
|
[ -n "$LUCI_HELPER" ] && return "$err_code" # no retry if called by LuCI helper script
|
||||||
return $__ERR
|
|
||||||
elif [ $__ERR -gt 0 -a $VERBOSE -gt 1 ]; then # VERBOSE > 1 then NO retry
|
if [ "$err_code" -gt 0 ]; then
|
||||||
|
[ "$VERBOSE" -gt 1 ] && {
|
||||||
write_log 4 "Verify Proxy server '$1' failed - Verbose Mode: $VERBOSE - NO retry on error"
|
write_log 4 "Verify Proxy server '$1' failed - Verbose Mode: $VERBOSE - NO retry on error"
|
||||||
return $__ERR
|
return "$err_code"
|
||||||
elif [ $__ERR -gt 0 ]; then
|
}
|
||||||
__CNT=$(( $__CNT + 1 )) # increment error counter
|
|
||||||
|
error_count=$(( error_count + 1 ))
|
||||||
# if error count > retry_max_count leave here
|
# if error count > retry_max_count leave here
|
||||||
[ $retry_max_count -gt 0 -a $__CNT -gt $retry_max_count ] && \
|
[ "$retry_max_count" -gt 0 ] && [ $error_count -gt "$retry_max_count" ] && \
|
||||||
write_log 14 "Verify Proxy server '$1' failed after $retry_max_count retries"
|
write_log 14 "Verify Proxy server '$1' failed after $retry_max_count retries"
|
||||||
|
|
||||||
write_log 4 "Verify Proxy server '$1' failed - retry $__CNT/$retry_max_count in $RETRY_SECONDS seconds"
|
write_log 4 "Verify Proxy server '$1' failed - retry $error_count/$retry_max_count in $RETRY_SECONDS seconds"
|
||||||
sleep $RETRY_SECONDS &
|
sleep $RETRY_SECONDS &
|
||||||
PID_SLEEP=$!
|
PID_SLEEP=$!
|
||||||
wait $PID_SLEEP # enable trap-handler
|
wait $PID_SLEEP # enable trap-handler
|
||||||
|
|||||||
Reference in New Issue
Block a user