Created
December 7, 2024 20:00
-
-
Save bananatron/ed2af4cd0d092c5f3b655eff742c0244 to your computer and use it in GitHub Desktop.
Q3 Server Status Check
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra' | |
require 'socket' | |
require 'timeout' | |
# Configuration | |
set :bind, '0.0.0.0' | |
set :port, 4567 | |
# Helper to check server status | |
def quake_server_status(host = 'localhost', port = 27960, timeout = 2) | |
begin | |
# Quake 3 server status query packet (getStatus) | |
# This is a standard query packet for Quake 3 servers | |
query_packet = "\xFF\xFF\xFF\xFFgetstatus\x00" | |
# Create UDP socket | |
socket = UDPSocket.new | |
socket.bind('0.0.0.0', 0) # Bind to any available local port | |
socket.connect(host, port) | |
# Set socket timeout | |
socket.send(query_packet, 0) | |
# Wait for response | |
Timeout.timeout(timeout) do | |
response = socket.recv(4096) | |
# Check if we got a valid response | |
return !response.empty? && response.length > 10 | |
end | |
rescue Errno::ECONNREFUSED, Timeout::Error, SocketError => e | |
# Connection refused or timeout | |
false | |
ensure | |
socket.close if socket | |
end | |
end | |
# Route for status | |
get '/' do | |
# Allow specifying host and port via query parameters | |
host = params['host'] || 'localhost' | |
port = (params['port'] || 27960).to_i | |
status = quake_server_status(host, port) | |
erb :index, locals: { status: status, host: host, port: port } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment