Last active
March 18, 2025 01:03
-
-
Save Cynosphere/7ac45bd91175071f04989479d974aa26 to your computer and use it in GitHub Desktop.
sourcequery nodejs
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
const dgram = require("dgram"); | |
const ip = process.argv[2]; | |
const port = process.argv[3] || 27015; | |
if (!ip) { | |
console.log("giv ip"); | |
process.exit(1); | |
} | |
function dumpPacket(view) { | |
let bytes = ""; | |
let str = ""; | |
let allBytes = ""; | |
for (let i = 0; i < view.byteLength; i++) { | |
if ((i > 0 && i % 16 == 0) || i == view.byteLength - 1) { | |
console.log(bytes.padEnd(48, " ") + " " + str); | |
allBytes += bytes; | |
bytes = ""; | |
str = ""; | |
} | |
const byte = view.getUint8(i); | |
bytes += byte.toString(16).padStart(2, "0").toUpperCase() + " "; | |
str += byte < 0x10 || byte == 0x7f ? "." : String.fromCharCode(byte); | |
} | |
console.log(allBytes); | |
} | |
const REQUEST_INFO = [ | |
0xff, 0xff, 0xff, 0xff, 0x54, 0x53, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x20, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x20, | |
0x51, 0x75, 0x65, 0x72, 0x79, 0x00, | |
]; | |
const REQUEST_RULES = [0xff, 0xff, 0xff, 0xff, 0x56]; | |
const REQUEST_PLAYER = [0xff, 0xff, 0xff, 0xff, 0x55]; | |
const rulePackets = []; | |
let chunks = -1; | |
function processRules() { | |
const data = rulePackets.flat(); | |
const dataArray = new Uint8Array(data); | |
const rulesMsg = new DataView(dataArray.buffer); | |
//dumpPacket(rulesMsg); | |
if (rulesMsg.getUint8(4) == 0x45) { | |
let idx = 5; | |
const rule_count = rulesMsg.getInt16(idx, true); | |
idx += 2; | |
console.log("cvar count:", rule_count); | |
const rules = []; | |
for (i = 0; i < rule_count; i++) { | |
if (idx >= rulesMsg.byteLength) break; | |
const _name = []; | |
while (rulesMsg.getUint8(idx) != 0) { | |
_name.push(rulesMsg.getUint8(idx)); | |
idx++; | |
} | |
idx++; | |
const name = String.fromCharCode(..._name); | |
if (idx >= rulesMsg.byteLength) break; | |
const _value = []; | |
while (rulesMsg.getUint8(idx) != 0) { | |
_value.push(rulesMsg.getUint8(idx)); | |
idx++; | |
} | |
idx++; | |
const value = String.fromCharCode(..._value); | |
rules.push({name, value}); | |
} | |
console.log("====== CVars ======"); | |
for (const rule of rules) { | |
console.log(`${rule.name} = ${rule.value}`); | |
} | |
} else { | |
console.log("malformed rules packet o7"); | |
} | |
} | |
function handlerRules(message, resolve, reject, wrapper, client) { | |
const msg = new DataView(message.buffer); | |
//console.log("rules", message); | |
if (msg.getUint8(4) == 0x41) { | |
const challenge = [msg.getUint8(5), msg.getUint8(6), msg.getUint8(7), msg.getUint8(8)]; | |
//console.log("got challenge", challenge); | |
client.send(Buffer.from([...REQUEST_RULES, ...challenge]), port, ip, function (err) { | |
if (err) { | |
client.close(); | |
reject(err); | |
} | |
}); | |
} else if (msg.getUint8(0) == 0xfe && msg.getUint8(1) == 0xff && msg.getUint8(2) == 0xff && msg.getUint8(3) == 0xff) { | |
chunks = msg.getUint8(8); | |
const chunkIdx = msg.getUint8(9); | |
const chunk = []; | |
for (let i = 12; i < msg.byteLength; i++) { | |
chunk.push(msg.getUint8(i)); | |
} | |
rulePackets[chunkIdx] = chunk; | |
} | |
if (chunks != -1 && rulePackets.length == chunks) { | |
client.close(); | |
processRules(); | |
client.off("message", wrapper); | |
resolve(true); | |
} | |
} | |
function a2sRules() { | |
//console.log("requesting rules"); | |
return new Promise((resolve, reject) => { | |
const client = dgram.createSocket("udp4"); | |
client.send(Buffer.from([...REQUEST_RULES, 0xff, 0xff, 0xff, 0xff]), port, ip, function (err) { | |
if (err) { | |
client.close(); | |
reject(err); | |
} | |
}); | |
function wrapper(message) { | |
handlerRules(message, resolve, reject, wrapper, client); | |
} | |
client.on("message", wrapper); | |
}); | |
} | |
function handlerInfo(message, resolve, reject, wrapper, client) { | |
const msg = new DataView(message.buffer); | |
//console.log("info", message); | |
if (msg.getUint8(4) == 0x41) { | |
const challenge = [msg.getUint8(5), msg.getUint8(6), msg.getUint8(7), msg.getUint8(8)]; | |
//console.log("got challenge", challenge); | |
client.send(Buffer.from([...REQUEST_INFO, ...challenge]), port, ip, function (err) { | |
if (err) { | |
client.close(); | |
reject(err); | |
} | |
}); | |
} else if (msg.getUint8(4) == 0x49) { | |
//console.log("got query response"); | |
//dumpPacket(msg); | |
const protocolVer = msg.getUint8(5); | |
console.log("protocol version:", protocolVer); | |
let idx = 6; | |
const name = []; | |
while (msg.getUint8(idx) != 0) { | |
name.push(msg.getUint8(idx)); | |
idx++; | |
} | |
console.log("name:", Buffer.from(name).toString("utf8")); | |
idx++; | |
const map = []; | |
while (msg.getUint8(idx) != 0) { | |
map.push(msg.getUint8(idx)); | |
idx++; | |
} | |
console.log("map:", Buffer.from(map).toString("utf8")); | |
idx++; | |
const folder = []; | |
while (msg.getUint8(idx) != 0) { | |
folder.push(msg.getUint8(idx)); | |
idx++; | |
} | |
console.log("folder:", Buffer.from(folder).toString("utf8")); | |
idx++; | |
const game = []; | |
while (msg.getUint8(idx) != 0) { | |
game.push(msg.getUint8(idx)); | |
idx++; | |
} | |
console.log("game:", Buffer.from(game).toString("utf8")); | |
idx++; | |
console.log("steam id:", msg.getInt16(idx, true)); | |
idx += 2; | |
console.log("players:", msg.getUint8(idx)); | |
idx++; | |
console.log("max players:", msg.getUint8(idx)); | |
idx++; | |
console.log("bots:", msg.getUint8(idx)); | |
idx++; | |
console.log("type:", String.fromCharCode(msg.getUint8(idx))); | |
idx++; | |
console.log("os:", String.fromCharCode(msg.getUint8(idx))); | |
idx++; | |
console.log("visibility:", msg.getUint8(idx)); | |
idx++; | |
console.log("vac:", msg.getUint8(idx)); | |
idx++; | |
const version = []; | |
while (msg.getUint8(idx) != 0) { | |
version.push(msg.getUint8(idx)); | |
idx++; | |
} | |
console.log("version:", Buffer.from(version).toString("utf8")); | |
idx++; | |
const edf = msg.getUint8(idx); | |
idx++; | |
console.log("edf is:", edf); | |
console.log("\n====== EDF Values ======"); | |
if (edf & 0x80) { | |
console.log("port:", msg.getInt16(idx, true)); | |
idx += 2; | |
} | |
if (edf & 0x10) { | |
console.log("steamid:", msg.getBigUint64(idx, true)); | |
idx += 8; | |
} | |
if (edf & 0x40) { | |
console.log("sourcetv port:", msg.getInt16(idx, true)); | |
idx += 2; | |
const stvname = []; | |
while (msg.getUint8(idx) != 0) { | |
stvname.push(msg.getUint8(idx)); | |
idx++; | |
} | |
console.log("sourcetv name:", Buffer.from(stvname).toString("utf8")); | |
idx++; | |
} | |
if (edf & 0x20) { | |
const tags = []; | |
while (msg.getUint8(idx) != 0) { | |
tags.push(msg.getUint8(idx)); | |
idx++; | |
} | |
console.log("tags:", Buffer.from(tags).toString("utf8")); | |
idx++; | |
} | |
if (edf & 0x01) { | |
console.log("extended app id:", msg.getBigUint64(idx, true)); | |
idx += 8; | |
} | |
client.close(); | |
client.off("message", wrapper); | |
resolve(true); | |
} | |
} | |
function a2sInfo() { | |
//console.log("requesting info"); | |
return new Promise((resolve, reject) => { | |
const client = dgram.createSocket("udp4"); | |
client.send(Buffer.from(REQUEST_INFO), port, ip, function (err) { | |
if (err) { | |
client.close(); | |
reject(err); | |
} | |
}); | |
function wrapper(message) { | |
handlerInfo(message, resolve, reject, wrapper, client); | |
} | |
client.on("message", wrapper); | |
}); | |
} | |
function handlerPlayer(message, resolve, reject, wrapper, client) { | |
const msg = new DataView(message.buffer); | |
//console.log("player", message); | |
if (msg.getUint8(4) == 0x41) { | |
const challenge = [msg.getUint8(5), msg.getUint8(6), msg.getUint8(7), msg.getUint8(8)]; | |
//console.log("got challenge", challenge); | |
client.send(Buffer.from([...REQUEST_PLAYER, ...challenge]), port, ip, function (err) { | |
if (err) { | |
client.close(); | |
reject(err); | |
} | |
}); | |
} else if (msg.getUint8(4) == 0x44) { | |
const numPlayers = msg.getUint8(5); | |
let idx = 6; | |
console.log("players:", numPlayers); | |
console.log("====== Players ======"); | |
const players = []; | |
let longestName = 0; | |
let longestScore = 0; | |
for (let i = 0; i < numPlayers; i++) { | |
//const index = msg.getUint8(idx); | |
idx++; | |
const _name = []; | |
while (msg.getUint8(idx) != 0) { | |
_name.push(msg.getUint8(idx)); | |
idx++; | |
} | |
idx++; | |
const name = Buffer.from(_name).toString("utf8"); | |
if (name.length > longestName) longestName = name.length; | |
const score = msg.getInt32(idx); | |
idx += 4; | |
const scoreStr = score.toString(); | |
if (scoreStr.length > longestScore) longestScore = scoreStr.length; | |
const _time = []; | |
for (let t = 0; t < 4; t++) { | |
_time.push(msg.getUint8(idx)); | |
idx++; | |
} | |
const time = Buffer.from(_time).readFloatLE(0); | |
//console.log(`${index} | ${String.fromCharCode(...name)} | ${score} | ${time}`); | |
players.push({name, score, time}); | |
} | |
for (const {name, score, time} of players) { | |
console.log(`${name.padEnd(longestName, " ")} | ${score.toString().padEnd(longestScore, " ")} | ${time}`); | |
} | |
client.close(); | |
client.off("message", wrapper); | |
resolve(true); | |
} | |
} | |
function a2sPlayer() { | |
//console.log("requesting players"); | |
return new Promise((resolve, reject) => { | |
const client = dgram.createSocket("udp4"); | |
client.send(Buffer.from([...REQUEST_PLAYER, 0xff, 0xff, 0xff, 0xff]), port, ip, function (err) { | |
if (err) { | |
client.close(); | |
reject(err); | |
} | |
}); | |
function wrapper(message) { | |
handlerPlayer(message, resolve, reject, wrapper, client); | |
} | |
client.on("message", wrapper); | |
}); | |
} | |
(async () => { | |
await a2sInfo(); | |
console.log(""); | |
await a2sRules(); | |
console.log(""); | |
await a2sPlayer(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment