Procházet zdrojové kódy

fix: check localhost ip first on fallback (#4158)

jusito před 3 roky
rodič
revize
2fcc919d82
1 změnil soubory, kde provedl 18 přidání a 13 odebrání
  1. 18 13
      lgsm/functions/check_ip.sh

+ 18 - 13
lgsm/functions/check_ip.sh

@@ -26,32 +26,37 @@ for ethtool_command in "${ethtool_commands_array[@]}"; do
 	fi
 done
 
-getip=$(${ipcommand} -o -4 addr | awk '{print $4}' | grep -oe '\([0-9]\{1,3\}\.\?\)\{4\}' | sort -u | grep -v 127.0.0)
-getipwc=$(${ipcommand} -o -4 addr | awk '{print $4}' | grep -oe '\([0-9]\{1,3\}\.\?\)\{4\}' | sort -u | grep -vc 127.0.0)
+mapfile -t current_ips < <(${ipcommand} -o -4 addr | awk '{print $4}' | grep -oe '\([0-9]\{1,3\}\.\?\)\{4\}' | sort -u | grep -v 127.0.0)
+
+function fn_is_valid_ip() {
+	local ip="${1}"
+	# excluding 0.* ips also
+	grep -qEe '^[1-9]+[0-9]*\.[0-9]+\.[0-9]+\.[0-9]+$' <<< "${ip}"
+}
 
 # Check if server has multiple IP addresses
 
 # If the IP variable has been set by user.
-if [ -n "${ip}" ] && [ "${ip}" != "0.0.0.0" ]; then
-	queryips=("${ip}")
-	webadminip=("${ip}")
-	telnetip=("${ip}")
+if fn_is_valid_ip "${ip}"; then
+	queryips=( "${ip}" )
+	webadminip=( "${ip}" )
+	telnetip=( "${ip}" )
 # If game config does have an IP set.
-elif [ -n "${configip}" ] && [ "${configip}" != "0.0.0.0" ]; then
-	queryips=("${configip}")
+elif fn_is_valid_ip "${configip}";then
+	queryips=( "${configip}" )
 	ip="${configip}"
 	webadminip=("${configip}")
 	telnetip=("${configip}")
 # If there is only 1 server IP address.
 # Some IP details can automaticly use the one IP
-elif [ "${getipwc}" == "1" ]; then
-	queryips=($(echo "${getip}"))
+elif [ "${#current_ips[@]}" == "1" ]; then
+	queryips=( "127.0.0.1" "${current_ips[@]}" )
 	ip="0.0.0.0"
-	webadminip=("${getip}")
-	telnetip=("${getip}")
+	webadminip=("${current_ips[@]}")
+	telnetip=("${current_ips[@]}")
 # If no ip is set by the user and server has more than one IP.
 else
-	queryips=($(echo "${getip}"))
+	queryips=( "127.0.0.1" "${current_ips[@]}" )
 	ip="0.0.0.0"
 	webadminip=("${ip}")
 	telnetip=("${ip}")