From 2176a9ab52be1c633acb5075b607467e27c2b07b Mon Sep 17 00:00:00 2001 From: Johannes Truschnigg Date: Sat, 12 Mar 2022 16:56:18 +0100 Subject: [PATCH] Improve parameter validation in wrapper --- lagdetect.sh | 70 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 3 deletions(-) diff --git a/lagdetect.sh b/lagdetect.sh index f37fe73..9b7f3cd 100755 --- a/lagdetect.sh +++ b/lagdetect.sh @@ -14,21 +14,85 @@ PING_INTERVAL=500 # DO NOT EDIT BELOW # ##################### +__is_num() { +case "${1}" in + '') + return 1 + ;; + *.*.*) + return 1 + ;; + *[!0123456789.]*) + return 1 + ;; + [0123456789]*) + return 0 + ;; + *) + return 1 + ;; +esac +} + + +__is_int() { +if __is_num "${1}" +then + case "${1}" in + *.*) + return 1 + ;; + *) + return 0 + ;; + esac +fi +return 1 +} + + for p in traceroute fping awk do type "${p}" >/dev/null || { printf 'FATAL: Required program "%s" not installed.\n' "${p}" >&2; exit 1; } done -if [ x"${PING_GW_IPV6}" = xyes ] + +if [ x"${PING_GW_IPV6:-no}" = xyes ] then PING_PEERS="$(traceroute -6 -n -m 2001:4860:4860::8888 | awk '{if("1" == $1){print $2}}') ${PING_PEERS}" fi -if [ x"${PING_GW_IPV4}" = xyes ] + +if [ x"${PING_GW_IPV4:-no}" = xyes ] +then + PING_PEERS="$(traceroute -4 -n -m 1 8.8.8.8 | awk '{if("1" == $1){print $2}}') ${PING_PEERS}" +fi + +if ! __is_num "${THRESHOLD}" then - PING_PEERS="$(traceroute -4 -n -m 1 1.1.1.1 | awk '{if("1" == $1){print $2}}') ${PING_PEERS}" + printf 'FATAL: Invalid value for THRESHOLD\n' + exit 1 fi +if ! __is_num "${FUDGE_MS}" +then + printf 'FATAL: Invalid value for FUDGE_MS\n' + exit 1 +fi + +if ! __is_int "${PING_SLOTS_PER_PEER}" +then + printf 'FATAL: Invalid value for PING_SLOTS_PER_PEER\n' + exit 1 +fi + +if ! __is_int "${PING_INTERVAL}" +then + printf 'FATAL: Invalid value for PING_INTERVAL\n' + exit 1 +fi + + set -u fping --loop --retry=1 --size=1280 --period="${PING_INTERVAL}" --random ${PING_PEERS} 2>/dev/null \ -- 2.39.5