Restructured app format, goldsrc full support and added implementation for Day of Defeat and Counter-Strike: Condition Zero (#5)

* [valve_app_restructure] Initial change

* [valve_app_restructure] Some GoldSrc split packet changes

* [valve_app_restructure] Counter-Strike: Condition Zero implementation.

* [valve_app_restructure] Docs changes

* [valve_app_restructure] Added obsolete gold src response

* [valve_app_restructure] Day of Defeat implementation.
This commit is contained in:
CosminPerRam 2022-10-27 01:01:11 +03:00 committed by GitHub
parent 96c2c8a335
commit d3a1dba3c1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 276 additions and 116 deletions

View file

@ -1,6 +1,17 @@
Who knows what the future holds... Who knows what the future holds...
# 0.0.5 - ??/??/2022
Support for GoldSrc split packets and obsolete A2S_INFO response.
Changed the Valve Protocol app parameter to represent the engine responses.
It is now an enum of:
- `Source(Option<u32>)` - A Source response with optionally, the id (if the id is present and the response id is not the same, the query fails)
- `GoldSrc(bool)` - A GoldSrc response with the option to enforce the obsolete A2S_INFO response.
[Counter-Strike: Condition Zero](https://store.steampowered.com/app/80/CounterStrike_Condition_Zero/) implementation.
[Day of Defeat](https://store.steampowered.com/app/30/Day_of_Defeat/) implementation.
Games besides CSGO and TS now have the same response structure.
# 0.0.4 - 23/10/2022 # 0.0.4 - 23/10/2022
Queries now support DNS resolve. Queries now support DNS resolve.
Changed uses a bit, example: from `use gamedig::valve::ValveProtocol::query` to `use gamedig::protocols::valve::query`. Changed uses a bit, example: from `use gamedig::valve::ValveProtocol::query` to `use gamedig::protocols::valve::query`.

View file

@ -13,8 +13,10 @@
| ALIENS | Alien Swarm | Valve Protocol | Not tested. | | ALIENS | Alien Swarm | Valve Protocol | Not tested. |
| ASRD | Alien Swarm: Reactive Drop | Valve Protocol | | | ASRD | Alien Swarm: Reactive Drop | Valve Protocol | |
| INS | Insurgency | Valve Protocol | | | INS | Insurgency | Valve Protocol | |
| INSS | Insurgency: Sandstorm | Valve Protocol | Here you need to use the query port, not the server port. | | INSS | Insurgency: Sandstorm | Valve Protocol | Use the query port, not the server port. |
| INSMIC | Insurgency: Modern Infantry Combat | Valve Protocol | Not tested. | | INSMIC | Insurgency: Modern Infantry Combat | Valve Protocol | Not tested. |
| CSCZ | Counter-Strike: Condition Zero | Valve Protocol | |
| DOD | Day of Defeat | Valve Protocol | |
## Planned to add support: ## Planned to add support:
All Valve titles. All Valve titles.

View file

@ -1,8 +1,8 @@
# Supported protocols: # Supported protocols:
| Name | Documentation reference | Used by | Notes | | Name | Documentation reference | Notes |
|----------------|---------------------------------------------------------------------------|---------------------------------------------------------------------------------|----------------------------------------| |----------------|---------------------------------------------------------------------------|----------------------------------------|
| Valve Protocol | [Server Queries](https://developer.valvesoftware.com/wiki/Server_queries) | TF2, CSGO, TS, CSS, DODS, GM, HL2DM, L4D, L4D2, ALIENS, ASRD, INS, INSS, INSMIC | Multi-packet decompression not tested. | | Valve Protocol | [Server Queries](https://developer.valvesoftware.com/wiki/Server_queries) | Multi-packet decompression not tested. |
## Planned to add support: ## Planned to add support:
Minecraft protocol Minecraft protocol

View file

@ -1,9 +1,10 @@
use std::env; use std::env;
use gamedig::{aliens, asrd, csgo, css, dods, gm, hl2dm, ins, insmic, inss, l4d, l4d2, tf2, ts}; use gamedig::{aliens, asrd, cscz, csgo, css, dod, dods, GDResult, gm, hl2dm, ins, insmic, inss, l4d, l4d2, tf2, ts};
use gamedig::protocols::valve; use gamedig::protocols::valve;
use gamedig::protocols::valve::App;
fn main() { fn main() -> GDResult<()> {
let args: Vec<String> = env::args().collect(); let args: Vec<String> = env::args().collect();
if args.len() == 1 || args[1] == "help".to_string() { if args.len() == 1 || args[1] == "help".to_string() {
@ -11,10 +12,10 @@ fn main() {
println!(" <game> - any game, example: tf2"); println!(" <game> - any game, example: tf2");
println!(" <ip> - an ip, example: 192.168.0.0"); println!(" <ip> - an ip, example: 192.168.0.0");
println!(" <port> - an port, optional, example: 27015"); println!(" <port> - an port, optional, example: 27015");
return; return Ok(());
} else if args.len() < 3 { } else if args.len() < 3 {
println!("Minimum number of arguments: 3, try 'help' to see the details."); println!("Minimum number of arguments: 3, try 'help' to see the details.");
return; return Ok(());
} }
let ip = args[2].as_str(); let ip = args[2].as_str();
@ -24,21 +25,27 @@ fn main() {
}; };
match args[1].as_str() { match args[1].as_str() {
"aliens" => println!("{:?}", aliens::query(ip, port)), "aliens" => println!("{:?}", aliens::query(ip, port)?),
"asrd" => println!("{:?}", asrd::query(ip, port)), "asrd" => println!("{:?}", asrd::query(ip, port)?),
"csgo" => println!("{:?}", csgo::query(ip, port)), "csgo" => println!("{:?}", csgo::query(ip, port)?),
"css" => println!("{:?}", css::query(ip, port)), "css" => println!("{:?}", css::query(ip, port)?),
"dods" => println!("{:?}", dods::query(ip, port)), "dods" => println!("{:?}", dods::query(ip, port)?),
"gm" => println!("{:?}", gm::query(ip, port)), "gm" => println!("{:?}", gm::query(ip, port)?),
"hl2dm" => println!("{:?}", hl2dm::query(ip, port)), "hl2dm" => println!("{:?}", hl2dm::query(ip, port)?),
"tf2" => println!("{:?}", tf2::query(ip, port)), "tf2" => println!("{:?}", tf2::query(ip, port)?),
"insmic" => println!("{:?}", insmic::query(ip, port)), "insmic" => println!("{:?}", insmic::query(ip, port)?),
"ins" => println!("{:?}", ins::query(ip, port)), "ins" => println!("{:?}", ins::query(ip, port)?),
"inss" => println!("{:?}", inss::query(ip, port)), "inss" => println!("{:?}", inss::query(ip, port)?),
"l4d" => println!("{:?}", l4d::query(ip, port)), "l4d" => println!("{:?}", l4d::query(ip, port)?),
"l4d2" => println!("{:?}", l4d2::query(ip, port)), "l4d2" => println!("{:?}", l4d2::query(ip, port)?),
"ts" => println!("{:?}", ts::query(ip, port)), "ts" => println!("{:?}", ts::query(ip, port)?),
"_" => println!("{:?}", valve::query(ip, 27015, None, None)), "cscz" => println!("{:?}", cscz::query(ip, port)?),
"dod" => println!("{:?}", dod::query(ip, port)?),
"_src" => println!("{:?}", valve::query(ip, 27015, App::Source(None), None)?),
"_gld" => println!("{:?}", valve::query(ip, 27015, App::GoldSrc(false), None)?),
"_gld_f" => println!("{:?}", valve::query(ip, 27015, App::GoldSrc(true), None)?),
_ => panic!("Undefined game: {}", args[1]) _ => panic!("Undefined game: {}", args[1])
}; };
Ok(())
} }

View file

@ -2,7 +2,7 @@
use gamedig::games::tf2; use gamedig::games::tf2;
fn main() { fn main() {
let response = tf2::query("91.216.250.10", None); //or Some(27015), None is the default protocol port (which is 27015) let response = tf2::query("cosminperram.com", None); //or Some(27015), None is the default protocol port (which is 27015)
match response { match response {
Err(error) => println!("Couldn't query, error: {error}"), Err(error) => println!("Couldn't query, error: {error}"),
Ok(r) => println!("{:?}", r) Ok(r) => println!("{:?}", r)

View file

@ -1,10 +1,13 @@
//! The library's possible errors.
use core::fmt; use core::fmt;
use std::fmt::Formatter; use std::fmt::Formatter;
/// Result of Type and GDError. /// Result of Type and GDError.
pub type GDResult<T> = Result<T, GDError>; pub type GDResult<T> = Result<T, GDError>;
/// GameDigError, every error you can encounter using the library. /// GameDigError.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum GDError { pub enum GDError {
/// The received packet was bigger than the buffer size. /// The received packet was bigger than the buffer size.

View file

@ -1,12 +1,12 @@
use crate::GDResult; use crate::GDResult;
use crate::protocols::valve; use crate::protocols::valve;
use crate::protocols::valve::{App, game}; use crate::protocols::valve::{game, SteamID};
pub fn query(address: &str, port: Option<u16>) -> GDResult<game::Response> { pub fn query(address: &str, port: Option<u16>) -> GDResult<game::Response> {
let valve_response = valve::query(address, match port { let valve_response = valve::query(address, match port {
None => 27015, None => 27015,
Some(port) => port Some(port) => port
}, Some(App::ALIENS), None)?; }, SteamID::ALIENS.app(), None)?;
Ok(game::Response::new_from_valve_response(valve_response)) Ok(game::Response::new_from_valve_response(valve_response))
} }

View file

@ -1,12 +1,12 @@
use crate::GDResult; use crate::GDResult;
use crate::protocols::valve; use crate::protocols::valve;
use crate::protocols::valve::{App, game}; use crate::protocols::valve::{game, SteamID};
pub fn query(address: &str, port: Option<u16>) -> GDResult<game::Response> { pub fn query(address: &str, port: Option<u16>) -> GDResult<game::Response> {
let valve_response = valve::query(address, match port { let valve_response = valve::query(address, match port {
None => 27015, None => 27015,
Some(port) => port Some(port) => port
}, Some(App::ASRD), None)?; }, SteamID::ASRD.app(), None)?;
Ok(game::Response::new_from_valve_response(valve_response)) Ok(game::Response::new_from_valve_response(valve_response))
} }

12
src/games/cscz.rs Normal file
View file

@ -0,0 +1,12 @@
use crate::GDResult;
use crate::protocols::valve;
use crate::protocols::valve::{game, SteamID};
pub fn query(address: &str, port: Option<u16>) -> GDResult<game::Response> {
let valve_response = valve::query(address, match port {
None => 27015,
Some(port) => port
}, SteamID::CSCZ.app(), None)?;
Ok(game::Response::new_from_valve_response(valve_response))
}

View file

@ -1,6 +1,6 @@
use crate::GDResult; use crate::GDResult;
use crate::protocols::valve; use crate::protocols::valve;
use crate::protocols::valve::{App, Server, GatheringSettings, get_optional_extracted_data}; use crate::protocols::valve::{Server, GatheringSettings, get_optional_extracted_data, SteamID};
use crate::protocols::valve::game::Player; use crate::protocols::valve::game::Player;
#[derive(Debug)] #[derive(Debug)]
@ -54,7 +54,7 @@ pub fn query(address: &str, port: Option<u16>) -> GDResult<Response> {
let valve_response = valve::query(address, match port { let valve_response = valve::query(address, match port {
None => 27015, None => 27015,
Some(port) => port Some(port) => port
}, Some(App::CSGO), Some(GatheringSettings { }, SteamID::CSGO.app(), Some(GatheringSettings {
players: true, players: true,
rules: false // cause csgo doesnt reply with rules anymore rules: false // cause csgo doesnt reply with rules anymore
}))?; }))?;

View file

@ -1,12 +1,12 @@
use crate::GDResult; use crate::GDResult;
use crate::protocols::valve; use crate::protocols::valve;
use crate::protocols::valve::{App, game}; use crate::protocols::valve::{game, SteamID};
pub fn query(address: &str, port: Option<u16>) -> GDResult<game::Response> { pub fn query(address: &str, port: Option<u16>) -> GDResult<game::Response> {
let valve_response = valve::query(address, match port { let valve_response = valve::query(address, match port {
None => 27015, None => 27015,
Some(port) => port Some(port) => port
}, Some(App::CSS), None)?; }, SteamID::CSS.app(), None)?;
Ok(game::Response::new_from_valve_response(valve_response)) Ok(game::Response::new_from_valve_response(valve_response))
} }

12
src/games/dod.rs Normal file
View file

@ -0,0 +1,12 @@
use crate::GDResult;
use crate::protocols::valve;
use crate::protocols::valve::{game, SteamID};
pub fn query(address: &str, port: Option<u16>) -> GDResult<game::Response> {
let valve_response = valve::query(address, match port {
None => 27015,
Some(port) => port
}, SteamID::DOD.app(), None)?;
Ok(game::Response::new_from_valve_response(valve_response))
}

View file

@ -1,12 +1,12 @@
use crate::GDResult; use crate::GDResult;
use crate::protocols::valve; use crate::protocols::valve;
use crate::protocols::valve::{App, game}; use crate::protocols::valve::{game, SteamID};
pub fn query(address: &str, port: Option<u16>) -> GDResult<game::Response> { pub fn query(address: &str, port: Option<u16>) -> GDResult<game::Response> {
let valve_response = valve::query(address, match port { let valve_response = valve::query(address, match port {
None => 27015, None => 27015,
Some(port) => port Some(port) => port
}, Some(App::DODS), None)?; }, SteamID::DODS.app(), None)?;
Ok(game::Response::new_from_valve_response(valve_response)) Ok(game::Response::new_from_valve_response(valve_response))
} }

View file

@ -1,12 +1,12 @@
use crate::GDResult; use crate::GDResult;
use crate::protocols::valve; use crate::protocols::valve;
use crate::protocols::valve::{App, game}; use crate::protocols::valve::{game, SteamID};
pub fn query(address: &str, port: Option<u16>) -> GDResult<game::Response> { pub fn query(address: &str, port: Option<u16>) -> GDResult<game::Response> {
let valve_response = valve::query(address, match port { let valve_response = valve::query(address, match port {
None => 27015, None => 27015,
Some(port) => port Some(port) => port
}, Some(App::GM), None)?; }, SteamID::GM.app(), None)?;
Ok(game::Response::new_from_valve_response(valve_response)) Ok(game::Response::new_from_valve_response(valve_response))
} }

View file

@ -1,12 +1,12 @@
use crate::GDResult; use crate::GDResult;
use crate::protocols::valve; use crate::protocols::valve;
use crate::protocols::valve::{App, game}; use crate::protocols::valve::{game, SteamID};
pub fn query(address: &str, port: Option<u16>) -> GDResult<game::Response> { pub fn query(address: &str, port: Option<u16>) -> GDResult<game::Response> {
let valve_response = valve::query(address, match port { let valve_response = valve::query(address, match port {
None => 27015, None => 27015,
Some(port) => port Some(port) => port
}, Some(App::HL2DM), None)?; }, SteamID::HL2DM.app(), None)?;
Ok(game::Response::new_from_valve_response(valve_response)) Ok(game::Response::new_from_valve_response(valve_response))
} }

View file

@ -1,12 +1,12 @@
use crate::GDResult; use crate::GDResult;
use crate::protocols::valve; use crate::protocols::valve;
use crate::protocols::valve::{App, game}; use crate::protocols::valve::{game, SteamID};
pub fn query(address: &str, port: Option<u16>) -> GDResult<game::Response> { pub fn query(address: &str, port: Option<u16>) -> GDResult<game::Response> {
let valve_response = valve::query(address, match port { let valve_response = valve::query(address, match port {
None => 27015, None => 27015,
Some(port) => port Some(port) => port
}, Some(App::INS), None)?; }, SteamID::INS.app(), None)?;
Ok(game::Response::new_from_valve_response(valve_response)) Ok(game::Response::new_from_valve_response(valve_response))
} }

View file

@ -1,12 +1,12 @@
use crate::GDResult; use crate::GDResult;
use crate::protocols::valve; use crate::protocols::valve;
use crate::protocols::valve::{App, game}; use crate::protocols::valve::{game, SteamID};
pub fn query(address: &str, port: Option<u16>) -> GDResult<game::Response> { pub fn query(address: &str, port: Option<u16>) -> GDResult<game::Response> {
let valve_response = valve::query(address, match port { let valve_response = valve::query(address, match port {
None => 27015, None => 27015,
Some(port) => port Some(port) => port
}, Some(App::INSMIC), None)?; }, SteamID::INSMIC.app(), None)?;
Ok(game::Response::new_from_valve_response(valve_response)) Ok(game::Response::new_from_valve_response(valve_response))
} }

View file

@ -1,12 +1,12 @@
use crate::GDResult; use crate::GDResult;
use crate::protocols::valve; use crate::protocols::valve;
use crate::protocols::valve::{App, game}; use crate::protocols::valve::{game, SteamID};
pub fn query(address: &str, port: Option<u16>) -> GDResult<game::Response> { pub fn query(address: &str, port: Option<u16>) -> GDResult<game::Response> {
let valve_response = valve::query(address, match port { let valve_response = valve::query(address, match port {
None => 27131, None => 27131,
Some(port) => port Some(port) => port
}, Some(App::INSS), None)?; }, SteamID::INSS.app(), None)?;
Ok(game::Response::new_from_valve_response(valve_response)) Ok(game::Response::new_from_valve_response(valve_response))
} }

View file

@ -1,12 +1,12 @@
use crate::GDResult; use crate::GDResult;
use crate::protocols::valve; use crate::protocols::valve;
use crate::protocols::valve::{App, game}; use crate::protocols::valve::{game, SteamID};
pub fn query(address: &str, port: Option<u16>) -> GDResult<game::Response> { pub fn query(address: &str, port: Option<u16>) -> GDResult<game::Response> {
let valve_response = valve::query(address, match port { let valve_response = valve::query(address, match port {
None => 27015, None => 27015,
Some(port) => port Some(port) => port
}, Some(App::L4D), None)?; }, SteamID::L4D.app(), None)?;
Ok(game::Response::new_from_valve_response(valve_response)) Ok(game::Response::new_from_valve_response(valve_response))
} }

View file

@ -1,12 +1,12 @@
use crate::GDResult; use crate::GDResult;
use crate::protocols::valve; use crate::protocols::valve;
use crate::protocols::valve::{App, game}; use crate::protocols::valve::{game, SteamID};
pub fn query(address: &str, port: Option<u16>) -> GDResult<game::Response> { pub fn query(address: &str, port: Option<u16>) -> GDResult<game::Response> {
let valve_response = valve::query(address, match port { let valve_response = valve::query(address, match port {
None => 27015, None => 27015,
Some(port) => port Some(port) => port
}, Some(App::L4D2), None)?; }, SteamID::L4D2.app(), None)?;
Ok(game::Response::new_from_valve_response(valve_response)) Ok(game::Response::new_from_valve_response(valve_response))
} }

View file

@ -29,3 +29,7 @@ pub mod ins;
pub mod inss; pub mod inss;
/// Insurgency: Modern Infantry Combat /// Insurgency: Modern Infantry Combat
pub mod insmic; pub mod insmic;
/// Counter Strike: Condition Zero
pub mod cscz;
/// Day of Defeat
pub mod dod;

View file

@ -1,12 +1,12 @@
use crate::GDResult; use crate::GDResult;
use crate::protocols::valve; use crate::protocols::valve;
use crate::protocols::valve::{App, game}; use crate::protocols::valve::{game, SteamID};
pub fn query(address: &str, port: Option<u16>) -> GDResult<game::Response> { pub fn query(address: &str, port: Option<u16>) -> GDResult<game::Response> {
let valve_response = valve::query(address, match port { let valve_response = valve::query(address, match port {
None => 27015, None => 27015,
Some(port) => port Some(port) => port
}, Some(App::TF2), None)?; }, SteamID::TF2.app(), None)?;
Ok(game::Response::new_from_valve_response(valve_response)) Ok(game::Response::new_from_valve_response(valve_response))
} }

View file

@ -1,6 +1,6 @@
use crate::GDResult; use crate::GDResult;
use crate::protocols::valve; use crate::protocols::valve;
use crate::protocols::valve::{App, Server, ServerRule, ServerPlayer, get_optional_extracted_data}; use crate::protocols::valve::{Server, ServerRule, ServerPlayer, get_optional_extracted_data, SteamID};
#[derive(Debug)] #[derive(Debug)]
pub struct TheShipPlayer { pub struct TheShipPlayer {
@ -84,7 +84,7 @@ pub fn query(address: &str, port: Option<u16>) -> GDResult<Response> {
let valve_response = valve::query(address, match port { let valve_response = valve::query(address, match port {
None => 27015, None => 27015,
Some(port) => port Some(port) => port
}, Some(App::TS), None)?; }, SteamID::TS.app(), None)?;
Ok(Response::new_from_valve_response(valve_response)) Ok(Response::new_from_valve_response(valve_response))
} }

View file

@ -1,4 +1,7 @@
/// The implementation.
pub mod protocol; pub mod protocol;
/// All types used by the implementation.
pub mod types; pub mod types;
pub use protocol::*; pub use protocol::*;

View file

@ -1,8 +1,9 @@
use std::net::UdpSocket; use std::net::UdpSocket;
use bzip2_rs::decoder::Decoder; use bzip2_rs::decoder::Decoder;
use crate::{GDError, GDResult}; use crate::{GDError, GDResult};
use crate::protocols::valve::types::{App, Environment, ExtraData, GatheringSettings, Request, Response, Server, ServerInfo, ServerPlayer, ServerRule, TheShip}; use crate::protocols::valve::{App, ModData, SteamID};
use crate::utils::{buffer, complete_address}; use crate::protocols::valve::types::{Environment, ExtraData, GatheringSettings, Request, Response, Server, ServerInfo, ServerPlayer, ServerRule, TheShip};
use crate::utils::{buffer, complete_address, u8_lower_upper};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
struct Packet { struct Packet {
@ -73,18 +74,27 @@ struct SplitPacket {
} }
impl SplitPacket { impl SplitPacket {
fn new(_appid: u32, buf: &[u8]) -> GDResult<Self> { fn new(app: &App, buf: &[u8]) -> GDResult<Self> {
let mut pos = 0; let mut pos = 0;
let header = buffer::get_u32_le(&buf, &mut pos)?; let header = buffer::get_u32_le(&buf, &mut pos)?;
let id = buffer::get_u32_le(&buf, &mut pos)?; let id = buffer::get_u32_le(&buf, &mut pos)?;
let total = buffer::get_u8(&buf, &mut pos)?; let (total, number, size, compressed, decompressed_size, uncompressed_crc32) = match app {
let number = buffer::get_u8(&buf, &mut pos)?; App::GoldSrc(_) => {
let size = buffer::get_u16_le(&buf, &mut pos)?; //if game is CSS and if protocol is 7, queries with multi-packet responses will crash let (lower, upper) = u8_lower_upper(buffer::get_u8(&buf, &mut pos)?);
let compressed = ((id >> 31) & 1) == 1; (lower, upper, 0, false, None, None)
let (decompressed_size, uncompressed_crc32) = match compressed { }
false => (None, None), App::Source(_) => {
true => (Some(buffer::get_u32_le(&buf, &mut pos)?), Some(buffer::get_u32_le(&buf, &mut pos)?)) let total = buffer::get_u8(&buf, &mut pos)?;
let number = buffer::get_u8(&buf, &mut pos)?;
let size = buffer::get_u16_le(&buf, &mut pos)?; //if game is CSS and if protocol is 7, queries with multi-packet responses will crash
let compressed = ((id >> 31) & 1) == 1;
let (decompressed_size, uncompressed_crc32) = match compressed {
false => (None, None),
true => (Some(buffer::get_u32_le(&buf, &mut pos)?), Some(buffer::get_u32_le(&buf, &mut pos)?))
};
(total, number, size, compressed, decompressed_size, uncompressed_crc32)
}
}; };
Ok(Self { Ok(Self {
@ -100,8 +110,8 @@ impl SplitPacket {
}) })
} }
fn decompress(&self) -> GDResult<Vec<u8>> { fn get_payload(&self) -> GDResult<Vec<u8>> {
if !self.compressed { if self.compressed {
let mut decoder = Decoder::new(); let mut decoder = Decoder::new();
decoder.write(&self.payload).map_err(|e| GDError::Decompress(e.to_string()))?; decoder.write(&self.payload).map_err(|e| GDError::Decompress(e.to_string()))?;
@ -119,14 +129,6 @@ impl SplitPacket {
else { else {
Ok(decompressed_payload) Ok(decompressed_payload)
} }
} else { //already decompressed
Ok(self.payload.clone())
}
}
fn get_payload(&self) -> GDResult<Vec<u8>> {
if self.compressed {
Ok(self.decompress()?)
} else { } else {
Ok(self.payload.clone()) Ok(self.payload.clone())
} }
@ -164,15 +166,15 @@ impl ValveProtocol {
Ok(buf[..amt].to_vec()) Ok(buf[..amt].to_vec())
} }
fn receive(&self, appid: u32, buffer_size: usize) -> GDResult<Packet> { fn receive(&self, app: &App, buffer_size: usize) -> GDResult<Packet> {
let mut buf = self.receive_raw(buffer_size)?; let mut buf = self.receive_raw(buffer_size)?;
if buf[0] == 0xFE { //the packet is split if buf[0] == 0xFE { //the packet is split
let mut main_packet = SplitPacket::new(appid, &buf)?; let mut main_packet = SplitPacket::new(&app, &buf)?;
for _ in 1..main_packet.total { for _ in 1..main_packet.total {
buf = self.receive_raw(buffer_size)?; buf = self.receive_raw(buffer_size)?;
let chunk_packet = SplitPacket::new(appid, &buf)?; let chunk_packet = SplitPacket::new(&app, &buf)?;
main_packet.payload.extend(chunk_packet.payload); main_packet.payload.extend(chunk_packet.payload);
} }
@ -184,11 +186,11 @@ impl ValveProtocol {
} }
/// Ask for a specific request only. /// Ask for a specific request only.
fn get_request_data(&self, appid: u32, kind: Request) -> GDResult<Vec<u8>> { fn get_request_data(&self, app: &App, kind: Request) -> GDResult<Vec<u8>> {
let request_initial_packet = Packet::initial(kind.clone()).to_bytes(); let request_initial_packet = Packet::initial(kind.clone()).to_bytes();
self.send(&request_initial_packet)?; self.send(&request_initial_packet)?;
let packet = self.receive(appid, DEFAULT_PACKET_SIZE)?; let packet = self.receive(app, DEFAULT_PACKET_SIZE)?;
if packet.kind != 0x41 { //'A' if packet.kind != 0x41 { //'A'
return Ok(packet.payload.clone()); return Ok(packet.payload.clone());
@ -198,12 +200,79 @@ impl ValveProtocol {
let challenge_packet = Packet::challenge(kind.clone(), challenge).to_bytes(); let challenge_packet = Packet::challenge(kind.clone(), challenge).to_bytes();
self.send(&challenge_packet)?; self.send(&challenge_packet)?;
Ok(self.receive(appid, DEFAULT_PACKET_SIZE)?.payload) Ok(self.receive(app, DEFAULT_PACKET_SIZE)?.payload)
}
fn get_goldsrc_server_info(buf: &[u8]) -> GDResult<ServerInfo> {
let mut pos = 0;
buffer::get_u8(&buf, &mut pos)?; //get the header (useless info)
buffer::get_string(&buf, &mut pos)?; //get the server address (useless info)
let name = buffer::get_string(&buf, &mut pos)?;
let map = buffer::get_string(&buf, &mut pos)?;
let folder = buffer::get_string(&buf, &mut pos)?;
let game = buffer::get_string(&buf, &mut pos)?;
let players = buffer::get_u8(&buf, &mut pos)?;
let max_players = buffer::get_u8(&buf, &mut pos)?;
let protocol = buffer::get_u8(&buf, &mut pos)?;
let server_type = match buffer::get_u8(&buf, &mut pos)? {
68 => Server::Dedicated, //'D'
76 => Server::NonDedicated, //'L'
80 => Server::TV, //'P'
_ => Err(GDError::UnknownEnumCast)?
};
let environment_type = match buffer::get_u8(&buf, &mut pos)? {
76 => Environment::Linux, //'L'
87 => Environment::Windows, //'W'
_ => Err(GDError::UnknownEnumCast)?
};
let has_password = buffer::get_u8(&buf, &mut pos)? == 1;
let is_mod = buffer::get_u8(&buf, &mut pos)? == 1;
let mod_data = match is_mod {
false => None,
true => Some(ModData {
link: buffer::get_string(&buf, &mut pos)?,
download_link: buffer::get_string(&buf, &mut pos)?,
version: buffer::get_u32_le(&buf, &mut pos)?,
size: buffer::get_u32_le(&buf, &mut pos)?,
multiplayer_only: buffer::get_u8(&buf, &mut pos)? == 1,
has_own_dll: buffer::get_u8(&buf, &mut pos)? == 1
})
};
let vac_secured = buffer::get_u8(&buf, &mut pos)? == 1;
let bots = buffer::get_u8(&buf, &mut pos)?;
Ok(ServerInfo {
protocol,
name,
map,
folder,
game,
appid: 0, //not present in the obsolete response
players,
max_players,
bots,
server_type,
environment_type,
has_password,
vac_secured,
the_ship: None,
version: "".to_string(), //a version field only for the mod
extra_data: None,
is_mod,
mod_data
})
} }
/// Get the server information's. /// Get the server information's.
fn get_server_info(&self, initial_appid: u32) -> GDResult<ServerInfo> { fn get_server_info(&self, app: &App) -> GDResult<ServerInfo> {
let buf = self.get_request_data(initial_appid, Request::INFO)?; let buf = self.get_request_data(&app, Request::INFO)?;
if let App::GoldSrc(force) = app {
if *force {
return ValveProtocol::get_goldsrc_server_info(&buf);
}
}
let mut pos = 0; let mut pos = 0;
let protocol = buffer::get_u8(&buf, &mut pos)?; let protocol = buffer::get_u8(&buf, &mut pos)?;
@ -218,7 +287,7 @@ impl ValveProtocol {
let server_type = match buffer::get_u8(&buf, &mut pos)? { let server_type = match buffer::get_u8(&buf, &mut pos)? {
100 => Server::Dedicated, //'d' 100 => Server::Dedicated, //'d'
108 => Server::NonDedicated, //'l' 108 => Server::NonDedicated, //'l'
112 => Server::SourceTV, //'p' 112 => Server::TV, //'p'
_ => Err(GDError::UnknownEnumCast)? _ => Err(GDError::UnknownEnumCast)?
}; };
let environment_type = match buffer::get_u8(&buf, &mut pos)? { let environment_type = match buffer::get_u8(&buf, &mut pos)? {
@ -229,7 +298,7 @@ impl ValveProtocol {
}; };
let has_password = buffer::get_u8(&buf, &mut pos)? == 1; let has_password = buffer::get_u8(&buf, &mut pos)? == 1;
let vac_secured = buffer::get_u8(&buf, &mut pos)? == 1; let vac_secured = buffer::get_u8(&buf, &mut pos)? == 1;
let the_ship = match appid == App::TS as u32 { let the_ship = match *app == SteamID::TS.app() {
false => None, false => None,
true => Some(TheShip { true => Some(TheShip {
mode: buffer::get_u8(&buf, &mut pos)?, mode: buffer::get_u8(&buf, &mut pos)?,
@ -289,13 +358,15 @@ impl ValveProtocol {
vac_secured, vac_secured,
the_ship, the_ship,
version, version,
extra_data extra_data,
is_mod: false,
mod_data: None
}) })
} }
/// Get the server player's. /// Get the server player's.
fn get_server_players(&self, appid: u32) -> GDResult<Vec<ServerPlayer>> { fn get_server_players(&self, app: &App) -> GDResult<Vec<ServerPlayer>> {
let buf = self.get_request_data(appid, Request::PLAYERS)?; let buf = self.get_request_data(&app, Request::PLAYERS)?;
let mut pos = 0; let mut pos = 0;
let count = buffer::get_u8(&buf, &mut pos)?; let count = buffer::get_u8(&buf, &mut pos)?;
@ -307,11 +378,11 @@ impl ValveProtocol {
name: buffer::get_string(&buf, &mut pos)?, name: buffer::get_string(&buf, &mut pos)?,
score: buffer::get_u32_le(&buf, &mut pos)?, score: buffer::get_u32_le(&buf, &mut pos)?,
duration: buffer::get_f32_le(&buf, &mut pos)?, duration: buffer::get_f32_le(&buf, &mut pos)?,
deaths: match appid == App::TS as u32 { deaths: match *app == SteamID::TS.app() {
false => None, false => None,
true => Some(buffer::get_u32_le(&buf, &mut pos)?) true => Some(buffer::get_u32_le(&buf, &mut pos)?)
}, },
money: match appid == App::TS as u32 { money: match *app == SteamID::TS.app() {
false => None, false => None,
true => Some(buffer::get_u32_le(&buf, &mut pos)?) true => Some(buffer::get_u32_le(&buf, &mut pos)?)
} }
@ -322,12 +393,12 @@ impl ValveProtocol {
} }
/// Get the server rules's. /// Get the server rules's.
fn get_server_rules(&self, appid: u32) -> GDResult<Option<Vec<ServerRule>>> { fn get_server_rules(&self, app: &App) -> GDResult<Option<Vec<ServerRule>>> {
if appid == App::CSGO as u32 { //cause csgo wont respond to this since feb 21 2014 update if *app == SteamID::CSGO.app() { //cause csgo wont respond to this since feb 21 2014 update
return Ok(None); return Ok(None);
} }
let buf = self.get_request_data(appid, Request::RULES)?; let buf = self.get_request_data(&app, Request::RULES)?;
let mut pos = 0; let mut pos = 0;
let count = buffer::get_u16_le(&buf, &mut pos)?; let count = buffer::get_u16_le(&buf, &mut pos)?;
@ -344,25 +415,19 @@ impl ValveProtocol {
} }
} }
/// Query a server, you need to provide the address, the port and optionally, the app and the /// Query a server by providing the address, the port, the app and the gather settings, the settings
/// gather settings, the app being *None* means to anonymously query the server, and the gather /// being *None* means to also get the players and the rules.
/// settings being *None* means to get the players and the rules. pub fn query(address: &str, port: u16, app: App, gather_settings: Option<GatheringSettings>) -> GDResult<Response> {
pub fn query(address: &str, port: u16, app: Option<App>, gather_settings: Option<GatheringSettings>) -> Result<Response, GDError> {
let client = ValveProtocol::new(address, port)?; let client = ValveProtocol::new(address, port)?;
let mut query_app_id = match app { let info = client.get_server_info(&app)?;
None => 0,
Some(app) => app as u32
};
let info = client.get_server_info(query_app_id)?; if let App::Source(x) = &app {
if let Some(appid) = x {
if query_app_id != 0 { if *appid != info.appid {
if info.appid != query_app_id { return Err(GDError::BadGame(format!("Expected {}, found {} instead!", *appid, info.appid)));
return Err(GDError::BadGame(format!("Expected {}, found {} instead!", query_app_id, info.appid))); }
} }
} else {
query_app_id = info.appid;
} }
let (gather_players, gather_rules) = match gather_settings.is_some() { let (gather_players, gather_rules) = match gather_settings.is_some() {
@ -377,11 +442,11 @@ pub fn query(address: &str, port: u16, app: Option<App>, gather_settings: Option
info, info,
players: match gather_players { players: match gather_players {
false => None, false => None,
true => Some(client.get_server_players(query_app_id)?) true => Some(client.get_server_players(&app)?)
}, },
rules: match gather_rules { rules: match gather_rules {
false => None, false => None,
true => client.get_server_rules(query_app_id)? true => client.get_server_rules(&app)?
} }
}) })
} }

View file

@ -1,12 +1,10 @@
//! All types used by the Valve protocol
/// The type of the server. /// The type of the server.
#[derive(Debug)] #[derive(Debug)]
pub enum Server { pub enum Server {
Dedicated, Dedicated,
NonDedicated, NonDedicated,
SourceTV TV
} }
/// The Operating System that the server is on. /// The Operating System that the server is on.
@ -50,16 +48,20 @@ pub struct ServerInfo {
pub server_type: Server, pub server_type: Server,
/// The Operating System that the server is on. /// The Operating System that the server is on.
pub environment_type: Environment, pub environment_type: Environment,
/// Indicated whether the server requires a password. /// Indicates whether the server requires a password.
pub has_password: bool, pub has_password: bool,
/// Indicated whether the server uses VAC. /// Indicates whether the server uses VAC.
pub vac_secured: bool, pub vac_secured: bool,
/// [The ship](https://developer.valvesoftware.com/wiki/The_Ship) extra data /// [The ship](https://developer.valvesoftware.com/wiki/The_Ship) extra data
pub the_ship: Option<TheShip>, pub the_ship: Option<TheShip>,
/// Version of the game installed on the server. /// Version of the game installed on the server.
pub version: String, pub version: String,
/// Some extra data that the server might provide or not. /// Some extra data that the server might provide or not.
pub extra_data: Option<ExtraData> pub extra_data: Option<ExtraData>,
/// GoldSrc only: Indicates whether the hosted game is a mod.
pub is_mod: bool,
/// GoldSrc only: If the game is a mod, provide additional data.
pub mod_data: Option<ModData>
} }
/// A server player. /// A server player.
@ -109,6 +111,16 @@ pub struct ExtraData {
pub game_id: Option<u64> pub game_id: Option<u64>
} }
#[derive(Debug)]
pub struct ModData {
pub link: String,
pub download_link: String,
pub version: u32,
pub size: u32,
pub multiplayer_only: bool,
pub has_own_dll: bool
}
pub fn get_optional_extracted_data(data: Option<ExtraData>) -> (Option<u16>, Option<u64>, Option<u16>, Option<String>, Option<String>) { pub fn get_optional_extracted_data(data: Option<ExtraData>) -> (Option<u16>, Option<u64>, Option<u16>, Option<String>, Option<String>) {
match data { match data {
None => (None, None, None, None, None), None => (None, None, None, None, None),
@ -128,9 +140,13 @@ pub enum Request {
RULES = 0x56 RULES = 0x56
} }
/// Supported app id's /// Supported steam apps id's
#[derive(PartialEq, Clone)] #[derive(PartialEq, Clone)]
pub enum App { pub enum SteamID {
/// Day of Defeat
DOD = 30,
/// Counter-Strike: Condition Zero
CSCZ = 80,
/// Counter-Strike: Source /// Counter-Strike: Source
CSS = 240, CSS = 240,
/// Day of Defeat: Source /// Day of Defeat: Source
@ -161,6 +177,27 @@ pub enum App {
ASRD = 563560, ASRD = 563560,
} }
impl SteamID {
pub fn app(self) -> App {
match self {
SteamID::CSCZ | SteamID::DOD => App::GoldSrc(false),
x => App::Source(Some(x as u32))
}
}
}
/// App type
#[derive(PartialEq, Clone)]
pub enum App {
/// A Source game, the argument represents the wanted response steam app id, if its **None**,
/// let the query find it, if its **Some**, the query fails if the response id is not the
/// specified one.
Source(Option<u32>),
/// A GoldSrc game, the argument indicates whether to enforce getting the obsolete A2S_INFO
/// goldsrc response or not.
GoldSrc(bool)
}
/// What data to gather, purely used only with the query function. /// What data to gather, purely used only with the query function.
pub struct GatheringSettings { pub struct GatheringSettings {
pub players: bool, pub players: bool,

View file

@ -16,6 +16,10 @@ pub fn complete_address(address: &str, port: u16) -> GDResult<String> {
Ok(resolve_dns(address)? + ":" + &*port.to_string()) Ok(resolve_dns(address)? + ":" + &*port.to_string())
} }
pub fn u8_lower_upper(n: u8) -> (u8, u8) {
(n & 15, n >> 4)
}
pub mod buffer { pub mod buffer {
use super::*; use super::*;