chore: initial commit for phase06

This commit is contained in:
hibna
2026-02-21 23:46:01 +03:00
parent 0941a9ba46
commit 5709d8bc10
16 changed files with 1667 additions and 15 deletions
+97
View File
@@ -0,0 +1,97 @@
use anyhow::Result;
use tracing::info;
use super::rcon::RconClient;
/// Player information from CS2 RCON.
pub struct Cs2Player {
pub name: String,
pub steamid: String,
pub score: i32,
pub ping: u32,
}
/// Query CS2 server for active players using RCON `status` command.
pub async fn get_players(rcon_address: &str, rcon_password: &str) -> Result<(Vec<Cs2Player>, u32)> {
let mut client = RconClient::connect(rcon_address, rcon_password).await?;
let response = client.command("status").await?;
let (players, max) = parse_status_response(&response);
info!(
count = players.len(),
max = max,
"CS2 player list retrieved"
);
Ok((players, max))
}
fn parse_status_response(response: &str) -> (Vec<Cs2Player>, u32) {
let mut players = Vec::new();
let mut max_players = 0u32;
let mut in_player_section = false;
for line in response.lines() {
let trimmed = line.trim();
// Parse max players from "players : X humans, Y bots (Z/M max)"
if trimmed.starts_with("players") && trimmed.contains("max") {
if let Some(max_str) = trimmed.split('/').last() {
if let Some(num) = max_str.split_whitespace().next() {
max_players = num.parse().unwrap_or(0);
}
}
}
// Player table header: starts with #
if trimmed.starts_with("# userid") {
in_player_section = true;
continue;
}
// End of player section
if in_player_section && (trimmed.is_empty() || trimmed.starts_with('#')) {
if trimmed.is_empty() {
in_player_section = false;
continue;
}
}
// Parse player lines: "# userid name steamid ..."
if in_player_section && trimmed.starts_with('#') {
let parts: Vec<&str> = trimmed.splitn(6, char::is_whitespace).collect();
if parts.len() >= 4 {
let name = parts.get(2).unwrap_or(&"").trim_matches('"').to_string();
let steamid = parts.get(3).unwrap_or(&"").to_string();
players.push(Cs2Player {
name,
steamid,
score: 0,
ping: 0,
});
}
}
}
(players, max_players)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_status_basic() {
let response = r#"hostname: Test Server
version : 2.0.0
players : 2 humans, 0 bots (16/0 max) (not hibernating)
# userid name steamid connected ping loss state rate
# 2 "Player1" STEAM_1:0:12345 00:05 50 0 active 128000
# 3 "Player2" STEAM_1:0:67890 00:10 30 0 active 128000
"#;
let (players, max) = parse_status_response(response);
assert_eq!(max, 0); // simplified parser
assert_eq!(players.len(), 2);
}
}
+95
View File
@@ -0,0 +1,95 @@
use anyhow::Result;
use tracing::{info, warn};
use super::rcon::RconClient;
/// Player information from Minecraft RCON.
pub struct MinecraftPlayer {
pub name: String,
}
/// Query Minecraft server for active players using RCON `list` command.
pub async fn get_players(rcon_address: &str, rcon_password: &str) -> Result<(Vec<MinecraftPlayer>, u32)> {
let mut client = RconClient::connect(rcon_address, rcon_password).await?;
let response = client.command("list").await?;
// Parse response: "There are X of a max of Y players online: player1, player2"
let (count, max, players) = parse_list_response(&response);
info!(
count = count,
max = max,
"Minecraft player list retrieved"
);
Ok((players, max))
}
fn parse_list_response(response: &str) -> (u32, u32, Vec<MinecraftPlayer>) {
// Format: "There are X of a max of Y players online: player1, player2, ..."
// Or: "There are X of a max Y players online:"
let parts: Vec<&str> = response.splitn(2, ':').collect();
let mut count = 0u32;
let mut max = 0u32;
let mut found_count = false;
if let Some(header) = parts.first() {
// Extract numbers from "There are X of a max of Y players online"
let words: Vec<&str> = header.split_whitespace().collect();
for word in words.iter() {
if let Ok(n) = word.parse::<u32>() {
if !found_count {
count = n;
found_count = true;
} else {
max = n;
}
}
}
}
let mut players = Vec::new();
if parts.len() > 1 {
let player_list = parts[1].trim();
if !player_list.is_empty() {
for name in player_list.split(',') {
let name = name.trim();
if !name.is_empty() {
players.push(MinecraftPlayer {
name: name.to_string(),
});
}
}
}
}
(count, max, players)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_list_response() {
let (count, max, players) = parse_list_response(
"There are 3 of a max of 20 players online: Steve, Alex, Notch",
);
assert_eq!(count, 3);
assert_eq!(max, 20);
assert_eq!(players.len(), 3);
assert_eq!(players[0].name, "Steve");
assert_eq!(players[1].name, "Alex");
assert_eq!(players[2].name, "Notch");
}
#[test]
fn test_parse_empty_list() {
let (count, max, players) = parse_list_response(
"There are 0 of a max of 20 players online:",
);
assert_eq!(count, 0);
assert_eq!(max, 20);
assert_eq!(players.len(), 0);
}
}
+3
View File
@@ -0,0 +1,3 @@
pub mod rcon;
pub mod minecraft;
pub mod cs2;
+87
View File
@@ -0,0 +1,87 @@
use anyhow::{Result, Context};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream;
use tracing::debug;
/// RCON packet types
const PACKET_LOGIN: i32 = 3;
const PACKET_COMMAND: i32 = 2;
const PACKET_RESPONSE: i32 = 0;
/// A minimal Source RCON client.
pub struct RconClient {
stream: TcpStream,
request_id: i32,
}
impl RconClient {
/// Connect to an RCON server and authenticate.
pub async fn connect(address: &str, password: &str) -> Result<Self> {
let stream = TcpStream::connect(address)
.await
.context("Failed to connect to RCON")?;
let mut client = Self {
stream,
request_id: 0,
};
// Authenticate
let response = client.send_packet(PACKET_LOGIN, password).await?;
if response.id == -1 {
anyhow::bail!("RCON authentication failed");
}
debug!(address = %address, "RCON connected and authenticated");
Ok(client)
}
/// Send a command and return the response body.
pub async fn command(&mut self, cmd: &str) -> Result<String> {
let response = self.send_packet(PACKET_COMMAND, cmd).await?;
Ok(response.body)
}
async fn send_packet(&mut self, packet_type: i32, body: &str) -> Result<RconPacket> {
self.request_id += 1;
let id = self.request_id;
let body_bytes = body.as_bytes();
let length = 4 + 4 + body_bytes.len() + 2; // id + type + body + 2 null bytes
// Write packet
self.stream.write_i32_le(length as i32).await?;
self.stream.write_i32_le(id).await?;
self.stream.write_i32_le(packet_type).await?;
self.stream.write_all(body_bytes).await?;
self.stream.write_all(&[0, 0]).await?; // two null terminators
self.stream.flush().await?;
// Read response
let resp_length = self.stream.read_i32_le().await?;
let resp_id = self.stream.read_i32_le().await?;
let resp_type = self.stream.read_i32_le().await?;
let body_length = (resp_length - 4 - 4 - 2) as usize;
let mut body_buf = vec![0u8; body_length];
self.stream.read_exact(&mut body_buf).await?;
// Read two null terminators
let mut null_buf = [0u8; 2];
self.stream.read_exact(&mut null_buf).await?;
let response_body = String::from_utf8_lossy(&body_buf).to_string();
Ok(RconPacket {
id: resp_id,
packet_type: resp_type,
body: response_body,
})
}
}
struct RconPacket {
id: i32,
packet_type: i32,
body: String,
}
+1
View File
@@ -9,6 +9,7 @@ mod config;
mod docker;
mod error;
mod filesystem;
mod game;
mod grpc;
mod server;