Просмотр исходного кода

feat: optimise gamedig and master server query (#4206)

* feat: update gamedig cli commands

will speed up querying of servers

* feat: master server query will now use extip first

extip more likely to succeed thus will speed up query

* minor tweaks

* bug

* ports alphabetical

a

* reduce gsquery timeout to 2s

* display ss command output
Daniel Gibbs 3 лет назад
Родитель
Сommit
fae50c4c65

+ 5 - 5
lgsm/modules/check_ip.sh

@@ -10,7 +10,7 @@ moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
 
 info_game.sh
 
-ip_commands_array=("/bin/ip" "/usr/sbin/ip" "ip")
+ip_commands_array=("ip" "/bin/ip" "/usr/sbin/ip")
 for ip_command in "${ip_commands_array[@]}"; do
 	if [ "$(command -v "${ip_command}" 2> /dev/null)" ]; then
 		ipcommand="${ip_command}"
@@ -18,7 +18,7 @@ for ip_command in "${ip_commands_array[@]}"; do
 	fi
 done
 
-ethtool_commands_array=("/bin/ethtool" "/usr/sbin/ethtool" "ethtool")
+ethtool_commands_array=("ethtool" "/bin/ethtool" "/usr/sbin/ethtool")
 for ethtool_command in "${ethtool_commands_array[@]}"; do
 	if [ "$(command -v "${ethtool_command}" 2> /dev/null)" ]; then
 		ethtoolcommand="${ethtool_command}"
@@ -34,21 +34,21 @@ function fn_is_valid_ip() {
 	grep -qEe '^[1-9]+[0-9]*\.[0-9]+\.[0-9]+\.[0-9]+$' <<< "${ip}"
 }
 
-# Check if server has multiple IP addresses
+# Check if server has multiple IP addresses.
 
 # If the IP variable has been set by user.
 if fn_is_valid_ip "${ip}"; then
 	queryips=("${ip}")
 	httpip=("${ip}")
 	telnetip=("${ip}")
-# If game config does have an IP set.
+# If the game config has an IP set.
 elif fn_is_valid_ip "${configip}"; then
 	queryips=("${configip}")
 	ip="${configip}"
 	httpip=("${configip}")
 	telnetip=("${configip}")
 # If there is only 1 server IP address.
-# Some IP details can automaticly use the one IP
+# Some IP details can automatically use the one IP.
 elif [ "${#current_ips[@]}" == "1" ]; then
 	queryips=("127.0.0.1" "${current_ips[@]}")
 	ip="0.0.0.0"

+ 64 - 53
lgsm/modules/command_dev_query_raw.sh

@@ -51,17 +51,20 @@ echo -e "=================================================================="
 			echo -e "Game+3:"
 		fi
 	fi
-
-	if [ -v port401 ]; then
-		echo -e "Game+400: \t${port401} \t$(ss -tupl | grep -c "${port401}") \t$(ss -tupl | grep "${port401}" | grep tcp | awk '{ print $2 }') \t$(ss -tupl | grep "${port401}" | grep udp | awk '{ print $2 }')"
-	else
-		echo -e "Game+400:"
+	if [ "${shortname}" == "pvr" ]; then
+		if [ -v port401 ]; then
+			echo -e "Game+400: \t${port401} \t$(ss -tupl | grep -c "${port401}") \t$(ss -tupl | grep "${port401}" | grep tcp | awk '{ print $2 }') \t$(ss -tupl | grep "${port401}" | grep udp | awk '{ print $2 }')"
+		else
+			echo -e "Game+400:"
+		fi
 	fi
 
-	if [ -v portipv6 ]; then
-		echo -e "Game ipv6: \t${portipv6} \t$(ss -tupl | grep -c "${portipv6}") \t$(ss -tupl | grep "${portipv6}" | grep tcp | awk '{ print $2 }') \t$(ss -tupl | grep "${portipv6}" | grep udp | awk '{ print $2 }')"
-	else
-		echo -e "Game ipv6:"
+	if [ "${shortname}" == "mcb" ]; then
+		if [ -v portipv6 ]; then
+			echo -e "Game ipv6: \t${portipv6} \t$(ss -tupl | grep -c "${portipv6}") \t$(ss -tupl | grep "${portipv6}" | grep tcp | awk '{ print $2 }') \t$(ss -tupl | grep "${portipv6}" | grep udp | awk '{ print $2 }')"
+		else
+			echo -e "Game ipv6:"
+		fi
 	fi
 
 	if [ -v queryport ]; then
@@ -70,6 +73,42 @@ echo -e "=================================================================="
 		echo -e "Query:"
 	fi
 
+	if [ -v apiport ]; then
+		echo -e "Game: \t${apiport} \t$(ss -tupl | grep -c "${apiport}") \t$(ss -tupl | grep "${apiport}" | grep tcp | awk '{ print $2 }') \t$(ss -tupl | grep "${apiport}" | grep udp | awk '{ print $2 }')"
+	else
+		echo -e "API:"
+	fi
+
+	if [ -v appport ]; then
+		echo -e "App: \t${appport} \t$(ss -tupl | grep -c "${appport}") \t$(ss -tupl | grep "${appport}" | grep tcp | awk '{ print $2 }') \t$(ss -tupl | grep "${appport}" | grep udp | awk '{ print $2 }')"
+	else
+		echo -e "App:"
+	fi
+
+	if [ -v battleeyeport ]; then
+		echo -e "BattleEye: \t${battleeyeport} \t$(ss -tupl | grep -c "${battleeyeport}") \t$(ss -tupl | grep "${battleeyeport}" | grep tcp | awk '{ print $2 }') \t$(ss -tupl | grep "${battleeyeport}" | grep udp | awk '{ print $2 }')"
+	else
+		echo -e "BattleEye:"
+	fi
+
+	if [ -v beaconport ]; then
+		echo -e "Beacon: \t${beaconport} \t$(ss -tupl | grep -c "${beaconport}") \t$(ss -tupl | grep "${beaconport}" | grep tcp | awk '{ print $2 }') \t$(ss -tupl | grep "${beaconport}" | grep udp | awk '{ print $2 }')"
+	else
+		echo -e "Beacon:"
+	fi
+
+	if [ -v clientport ]; then
+		echo -e "Client: \t${clientport} \t$(ss -tupl | grep -c "${clientport}") \t$(ss -tupl | grep "${clientport}" | grep tcp | awk '{ print $2 }') \t$(ss -tupl | grep "${clientport}" | grep udp | awk '{ print $2 }')"
+	else
+		echo -e "Client:"
+	fi
+
+	if [ -v fileport ]; then
+		echo -e "File: \t${fileport} \t$(ss -tupl | grep -c "${fileport}") \t$(ss -tupl | grep "${fileport}" | grep tcp | awk '{ print $2 }') \t$(ss -tupl | grep "${fileport}" | grep udp | awk '{ print $2 }')"
+	else
+		echo -e "File:"
+	fi
+
 	if [ -v httpport ]; then
 		echo -e "HTTP: \t${httpport} \t$(ss -tupl | grep -c "${httpport}") \t$(ss -tupl | grep "${httpport}" | grep tcp | awk '{ print $2 }') \t$(ss -tupl | grep "${httpport}" | grep udp | awk '{ print $2 }')"
 	else
@@ -77,7 +116,7 @@ echo -e "=================================================================="
 	fi
 
 	if [ -v httpqueryport ]; then
-		echo -e "HTTP Query: \t${httpqueryport} \t$(ss -tupl | grep -c "${httpqueryport}") \t$(ss -tupl | grep" ${httpqueryport}" | grep tcp | awk '{ print $2 }') \t$(ss -tupl | grep ${httpqueryport} | grep udp | awk '{ print $2 }')"
+		echo -e "HTTP Query: \t${httpqueryport} \t$(ss -tupl | grep -c "${httpqueryport}") \t$(ss -tupl | grep" ${httpqueryport}" | grep tcp | awk '{ print $2 }') \t$(ss -tupl | grep "${httpqueryport}" | grep udp | awk '{ print $2 }')"
 	else
 		echo -e "HTTP Query:"
 	fi
@@ -88,16 +127,10 @@ echo -e "=================================================================="
 		echo -e "Web Interface:"
 	fi
 
-	if [ -v clientport ]; then
-		echo -e "Client: \t${clientport} \t$(ss -tupl | grep -c "${clientport}") \t$(ss -tupl | grep "${clientport}" | grep tcp | awk '{ print $2 }') \t$(ss -tupl | grep "${clientport}" | grep udp | awk '{ print $2 }')"
-	else
-		echo -e "Client:"
-	fi
-
-	if [ -v rconport ]; then
-		echo -e "RCON: \t${rconport} \t$(ss -tupl | grep -c "${rconport}") \t$(ss -tupl | grep "${rconport}" | grep tcp | awk '{ print $2 }') \t$(ss -tupl | grep "${rconport}" | grep udp | awk '{ print $2 }')"
+	if [ -v masterport ]; then
+		echo -e "Game: Master: \t${masterport} \t$(ss -tupl | grep -c "${masterport}") \t$(ss -tupl | grep "${masterport}" | grep tcp | awk '{ print $2 }') \t$(ss -tupl | grep "${masterport}" | grep udp | awk '{ print $2 }')"
 	else
-		echo -e "RCON:"
+		echo -e "Game: Master:"
 	fi
 
 	if [ -v rawport ]; then
@@ -106,10 +139,10 @@ echo -e "=================================================================="
 		echo -e "RAW UDP Socket:"
 	fi
 
-	if [ -v masterport ]; then
-		echo -e "Game: Master: \t${masterport} \t$(ss -tupl | grep -c "${masterport}") \t$(ss -tupl | grep "${masterport}" | grep tcp | awk '{ print $2 }') \t$(ss -tupl | grep "${masterport}" | grep udp | awk '{ print $2 }')"
+	if [ -v rconport ]; then
+		echo -e "RCON: \t${rconport} \t$(ss -tupl | grep -c "${rconport}") \t$(ss -tupl | grep "${rconport}" | grep tcp | awk '{ print $2 }') \t$(ss -tupl | grep "${rconport}" | grep udp | awk '{ print $2 }')"
 	else
-		echo -e "Game: Master:"
+		echo -e "RCON:"
 	fi
 
 	if [ -v steamport ]; then
@@ -130,34 +163,22 @@ echo -e "=================================================================="
 		echo -e "Steam: Auth:"
 	fi
 
-	if [ -v beaconport ]; then
-		echo -e "Beacon: \t${beaconport} \t$(ss -tupl | grep -c "${beaconport}") \t$(ss -tupl | grep "${beaconport}" | grep tcp | awk '{ print $2 }') \t$(ss -tupl | grep "${beaconport}" | grep udp | awk '{ print $2 }')"
-	else
-		echo -e "Beacon:"
-	fi
-
-	if [ -v appport ]; then
-		echo -e "App: \t${appport} \t$(ss -tupl | grep -c "${appport}") \t$(ss -tupl | grep "${appport}" | grep tcp | awk '{ print $2 }') \t$(ss -tupl | grep "${appport}" | grep udp | awk '{ print $2 }')"
-	else
-		echo -e "App:"
-	fi
-
 	if [ -v telnetport ]; then
 		echo -e "Telnet: \t${telnetport} \t$(ss -tupl | grep -c "${telnetport}") \t$(ss -tupl | grep "${telnetport}" | grep tcp | awk '{ print $2 }') \t$(ss -tupl | grep "${telnetport}" | grep udp | awk '{ print $2 }')"
 	else
 		echo -e "Telnet:"
 	fi
 
-	if [ -v sourcetvport ]; then
-		echo -e "SourceTV: \t${sourcetvport} \t$(ss -tupl | grep -c "${sourcetvport}") \t$(ss -tupl | grep "${sourcetvport}" | grep tcp | awk '{ print $2 }') \t$(ss -tupl | grep "${sourcetvport}" | grep udp | awk '{ print $2 }')"
+	if [ -v statsport ]; then
+		echo -e "Stats: \t${battleeyeport} \t$(ss -tupl | grep -c "${statsport}") \t$(ss -tupl | grep "${statsport}" | grep tcp | awk '{ print $2 }') \t$(ss -tupl | grep "${statsport}" | grep udp | awk '{ print $2 }')"
 	else
-		echo -e "SourceTV:"
+		echo -e "Stats:"
 	fi
 
-	if [ -v fileport ]; then
-		echo -e "File: \t${fileport} \t$(ss -tupl | grep -c "${fileport}") \t$(ss -tupl | grep "${fileport}" | grep tcp | awk '{ print $2 }') \t$(ss -tupl | grep "${fileport}" | grep udp | awk '{ print $2 }')"
+	if [ -v sourcetvport ]; then
+		echo -e "SourceTV: \t${sourcetvport} \t$(ss -tupl | grep -c "${sourcetvport}") \t$(ss -tupl | grep "${sourcetvport}" | grep tcp | awk '{ print $2 }') \t$(ss -tupl | grep "${sourcetvport}" | grep udp | awk '{ print $2 }')"
 	else
-		echo -e "File:"
+		echo -e "SourceTV:"
 	fi
 
 	if [ -v udplinkport ]; then
@@ -178,23 +199,13 @@ echo -e "=================================================================="
 		echo -e "Voice (Unused):"
 	fi
 
-	if [ -v battleeyeport ]; then
-		echo -e "BattleEye: \t${battleeyeport} \t$(ss -tupl | grep -c "${battleeyeport}") \t$(ss -tupl | grep "${battleeyeport}" | grep tcp | awk '{ print $2 }') \t$(ss -tupl | grep "${battleeyeport}" | grep udp | awk '{ print $2 }')"
-	else
-		echo -e "BattleEye:"
-	fi
-
-	if [ -v statsport ]; then
-		echo -e "Stats: \t${battleeyeport} \t$(ss -tupl | grep -c "${statsport}") \t$(ss -tupl | grep "${statsport}" | grep tcp | awk '{ print $2 }') \t$(ss -tupl | grep "${statsport}" | grep udp | awk '{ print $2 }')"
-	else
-		echo -e "Stats:"
-	fi
-
-} | column -s $'\t' -t
+} \
+	| column -s $'\t' -t
 echo -e ""
 echo -e "${lightgreen}SS Output${default}"
 echo -e "================================="
 fn_info_message_ports
+eval "${portcommand}"
 echo -e ""
 echo -e "${lightgreen}Query Port - Raw Output${default}"
 echo -e "=================================================================="

+ 7 - 6
lgsm/modules/info_game.sh

@@ -2383,13 +2383,14 @@ if [ -z "${displaymasterserver}" ]; then
 	if [ "$(command -v jq 2> /dev/null)" ]; then
 		if [ -n "${ip}" ] && [ -n "${port}" ]; then
 			if [ "${steammaster}" == "true" ] || [ "${commandname}" == "DEV-QUERY-RAW" ]; then
-				# Will query server IP addresses first.
-				for queryip in "${queryips[@]}"; do
-					masterserver="$(curl --connect-timeout 10 -m 3 -s "https://api.steampowered.com/ISteamApps/GetServersAtAddress/v0001?addr=${queryip}&format=json" | jq --arg port "${port}" --arg queryport "${queryport}" 'if .response.servers != null then .response.servers[] | select((.gameport == ($port|tonumber) or .gameport == ($queryport|tonumber))) | .addr else empty end' | wc -l 2> /dev/null)"
-				done
-				# Should that not work it will try the external IP.
+				# Query external IP first as most liky to succeed.
+				masterserver="$(curl --connect-timeout 10 -m 3 -s "https://api.steampowered.com/ISteamApps/GetServersAtAddress/v0001?addr=${extip}&format=json" | jq --arg port "${port}" --arg queryport "${queryport}" 'if .response.servers != null then .response.servers[] | select((.gameport == ($port|tonumber) or .gameport == ($queryport|tonumber))) | .addr else empty end' | wc -l 2> /dev/null)"
+
 				if [ "${masterserver}" == "0" ]; then
-					masterserver="$(curl --connect-timeout 10 -m 3 -s "https://api.steampowered.com/ISteamApps/GetServersAtAddress/v0001?addr=${extip}&format=json" | jq --arg port "${port}" --arg queryport "${queryport}" 'if .response.servers != null then .response.servers[] | select((.gameport == ($port|tonumber) or .gameport == ($queryport|tonumber))) | .addr else empty end' | wc -l 2> /dev/null)"
+					# Loop though server IP addresses if external IP fails.
+					for queryip in "${queryips[@]}"; do
+						masterserver="$(curl --connect-timeout 10 -m 3 -s "https://api.steampowered.com/ISteamApps/GetServersAtAddress/v0001?addr=${queryip}&format=json" | jq --arg port "${port}" --arg queryport "${queryport}" 'if .response.servers != null then .response.servers[] | select((.gameport == ($port|tonumber) or .gameport == ($queryport|tonumber))) | .addr else empty end' | wc -l 2> /dev/null)"
+					done
 				fi
 				if [ "${masterserver}" == "0" ]; then
 					displaymasterserver="false"

+ 9 - 8
lgsm/modules/info_messages.sh

@@ -688,23 +688,24 @@ fn_info_message_ports_edit() {
 fn_info_message_ports() {
 	echo -e "${lightblue}Useful port diagnostic command:${default}"
 	if [ "${shortname}" == "armar" ]; then
-		echo -e "ss -tuplwn | grep enfMain"
+		portcommand="ss -tuplwn | grep enfMain"
 	elif [ "${shortname}" == "av" ]; then
-		echo -e "ss -tuplwn | grep AvorionServer"
+		portcommand="ss -tuplwn | grep AvorionServer"
 	elif [ "${shortname}" == "bf1942" ]; then
-		echo -e "ss -tuplwn | grep bf1942_lnxded"
+		portcommand="ss -tuplwn | grep bf1942_lnxded"
 	elif [ "${shortname}" == "mc" ] || [ "${shortname}" == "nec" ] || [ "${shortname}" == "pmc" ] || [ "${shortname}" == "rw" ] || [ "${shortname}" == "vpmc" ] || [ "${shortname}" == "wmc" ]; then
-		echo -e "ss -tuplwn | grep java"
+		portcommand="ss -tuplwn | grep java"
 	elif [ "${shortname}" == "terraria" ]; then
-		echo -e "ss -tuplwn | grep Main"
+		portcommand="ss -tuplwn | grep Main"
 	elif [ "${engine}" == "source" ]; then
-		echo -e "ss -tuplwn | grep srcds_linux"
+		portcommand="ss -tuplwn | grep srcds_linux"
 	elif [ "${engine}" == "goldsrc" ]; then
-		echo -e "ss -tuplwn | grep hlds_linux"
+		portcommand="ss -tuplwn | grep hlds_linux"
 	else
 		executableshort="$(basename "${executable}" | cut -c -15)"
-		echo -e "ss -tuplwn | grep ${executableshort}"
+		portcommand="ss -tuplwn | grep ${executableshort}"
 	fi
+	echo -e "${portcommand}"
 	echo -e ""
 }
 

+ 3 - 8
lgsm/modules/query_gamedig.sh

@@ -7,6 +7,7 @@
 # https://github.com/sonicsnes/node-gamedig
 
 moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
+# Default query status to failure. Will be changed to 0 if query is successful.
 querystatus="2"
 # Check if gamedig and jq are installed.
 if [ "$(command -v gamedig 2> /dev/null)" ] && [ "$(command -v jq 2> /dev/null)" ]; then
@@ -19,16 +20,10 @@ if [ "$(command -v gamedig 2> /dev/null)" ] && [ "$(command -v jq 2> /dev/null)"
 			queryport="${port}"
 		fi
 		# checks if query is working null = pass.
-		gamedigcmd=$(echo -e "gamedig --type \"${querytype}\" --host \"${queryip}\" --query_port \"${queryport}\"|jq")
-		gamedigraw=$(gamedig --type "${querytype}" --host "${queryip}" --query_port "${queryport}")
+		gamedigcmd=$(echo -e "gamedig --type \"${querytype}\" \"${queryip}:${queryport}\"|jq")
+		gamedigraw=$(gamedig --type "${querytype}" "${queryip}:${queryport}")
 		querystatus=$(echo "${gamedigraw}" | jq '.error|length')
 
-		if [ "${querystatus}" != "null" ]; then
-			gamedigcmd=$(echo -e "gamedig --type \"${querytype}\" --host \"${queryip}\" --port \"${queryport}\"|jq")
-			gamedigraw=$(gamedig --type "${querytype}" --host "${queryip}" --port "${queryport}")
-			querystatus=$(echo "${gamedigraw}" | jq '.error|length')
-		fi
-
 		if [ "${querytype}" == "teamspeak3" ]; then
 			fn_info_game_ts3
 		fi

+ 1 - 1
lgsm/modules/query_gsquery.py

@@ -13,7 +13,7 @@ import sys
 engine_types=('protocol-valve','protocol-quake3','protocol-quake3','protocol-gamespy1','protocol-unreal2','ut3','minecraft','minecraftbe','jc2mp','mumbleping','soldat','teeworlds')
 
 class gsquery:
-    server_response_timeout = 5
+    server_response_timeout = 2
     default_buffer_length = 1024
     sourcequery=('protocol-valve','avalanche3.0','barotrauma','madness','quakelive','realvirtuality','refractor','source','goldsrc','spark','starbound','unity3d','unreal4','wurm')
     idtech2query=('protocol-quake3','idtech2','quake','iw2.0')