From 3ac6a8b603dbd6ec603965e103cdd034691e9d36 Mon Sep 17 00:00:00 2001 From: cosminperram Date: Sun, 23 Oct 2022 14:03:59 +0300 Subject: [PATCH] Changed uses to have a better structure --- CHANGELOG.md | 3 +- examples/master_querant.rs | 4 +- src/games/aliens.rs | 7 +-- src/games/asrd.rs | 7 +-- src/games/csgo.rs | 7 +-- src/games/css.rs | 7 +-- src/games/dods.rs | 7 +-- src/games/gm.rs | 7 +-- src/games/hl2dm.rs | 7 +-- src/games/ins.rs | 7 +-- src/games/insmic.rs | 7 +-- src/games/inss.rs | 7 +-- src/games/l4d.rs | 7 +-- src/games/l4d2.rs | 7 +-- src/games/tf2.rs | 7 +-- src/games/ts.rs | 7 +-- src/lib.rs | 1 - src/protocols/valve.rs | 104 +++++++++++++++++++------------------ 18 files changed, 113 insertions(+), 97 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 097976a..d744674 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,8 @@ Who knows what the future holds... # 0.0.4 - ??/??/???? Queries now support DNS resolve. -Changed Valve Protocol parameters to (ip, port, app, gather_settings), changes include: +Changed uses a bit, from `use gamedig::valve::ValveProtocol::query` to `use gamedig::protocols::valve::query`. +Changed Valve Protocol Query parameters to (ip, port, app, gather_settings), changes include: - the app is now optional, being None means to anonymously query the server. - gather_settings is now also an optional, being None means all query settings. diff --git a/examples/master_querant.rs b/examples/master_querant.rs index 7cc9c8f..72a961d 100644 --- a/examples/master_querant.rs +++ b/examples/master_querant.rs @@ -1,7 +1,7 @@ use std::env; use gamedig::{aliens, asrd, csgo, css, dods, gm, hl2dm, ins, insmic, inss, l4d, l4d2, tf2, ts}; -use gamedig::valve::ValveProtocol; +use gamedig::protocols::valve; fn main() { let args: Vec = env::args().collect(); @@ -38,7 +38,7 @@ fn main() { "l4d" => println!("{:?}", l4d::query(ip, port)), "l4d2" => println!("{:?}", l4d2::query(ip, port)), "ts" => println!("{:?}", ts::query(ip, port)), - "_" => println!("{:?}", ValveProtocol::query(ip, 27015, None, None)), + "_" => println!("{:?}", valve::query(ip, 27015, None, None)), _ => panic!("Undefined game: {}", args[1]) }; } diff --git a/src/games/aliens.rs b/src/games/aliens.rs index 15a2127..fa96d5d 100644 --- a/src/games/aliens.rs +++ b/src/games/aliens.rs @@ -1,5 +1,6 @@ -use crate::{GDResult, valve}; -use crate::valve::{ValveProtocol, App, Server, ServerRule, ServerPlayer}; +use crate::GDResult; +use crate::protocols::valve; +use crate::protocols::valve::{App, Server, ServerRule, ServerPlayer}; #[derive(Debug)] pub struct Player { @@ -71,7 +72,7 @@ impl Response { } pub fn query(address: &str, port: Option) -> GDResult { - let valve_response = ValveProtocol::query(address, match port { + let valve_response = valve::query(address, match port { None => 27015, Some(port) => port }, Some(App::ALIENS), None)?; diff --git a/src/games/asrd.rs b/src/games/asrd.rs index cceb0cd..1eefbe1 100644 --- a/src/games/asrd.rs +++ b/src/games/asrd.rs @@ -1,5 +1,6 @@ -use crate::{GDResult, valve}; -use crate::valve::{ValveProtocol, App, Server, ServerRule, ServerPlayer}; +use crate::GDResult; +use crate::protocols::valve; +use crate::protocols::valve::{App, Server, ServerRule, ServerPlayer}; #[derive(Debug)] pub struct Player { @@ -71,7 +72,7 @@ impl Response { } pub fn query(address: &str, port: Option) -> GDResult { - let valve_response = ValveProtocol::query(address, match port { + let valve_response = valve::query(address, match port { None => 27015, Some(port) => port }, Some(App::ASRD), None)?; diff --git a/src/games/csgo.rs b/src/games/csgo.rs index d0cdca9..38f62cf 100644 --- a/src/games/csgo.rs +++ b/src/games/csgo.rs @@ -1,5 +1,6 @@ -use crate::{GDResult, valve}; -use crate::valve::{ValveProtocol, App, GatheringSettings, ServerPlayer, Server}; +use crate::GDResult; +use crate::protocols::valve; +use crate::protocols::valve::{App, Server, ServerPlayer, GatheringSettings}; #[derive(Debug)] pub struct Player { @@ -69,7 +70,7 @@ impl Response { } pub fn query(address: &str, port: Option) -> GDResult { - let valve_response = ValveProtocol::query(address, match port { + let valve_response = valve::query(address, match port { None => 27015, Some(port) => port }, Some(App::CSGO), Some(GatheringSettings { diff --git a/src/games/css.rs b/src/games/css.rs index bcc2ca0..db47618 100644 --- a/src/games/css.rs +++ b/src/games/css.rs @@ -1,5 +1,6 @@ -use crate::{GDResult, valve}; -use crate::valve::{ValveProtocol, App, Server, ServerRule, ServerPlayer}; +use crate::GDResult; +use crate::protocols::valve; +use crate::protocols::valve::{App, Server, ServerRule, ServerPlayer}; #[derive(Debug)] pub struct Player { @@ -71,7 +72,7 @@ impl Response { } pub fn query(address: &str, port: Option) -> GDResult { - let valve_response = ValveProtocol::query(address, match port { + let valve_response = valve::query(address, match port { None => 27015, Some(port) => port }, Some(App::CSS), None)?; diff --git a/src/games/dods.rs b/src/games/dods.rs index 11ea284..42fe9a5 100644 --- a/src/games/dods.rs +++ b/src/games/dods.rs @@ -1,5 +1,6 @@ -use crate::{GDResult, valve}; -use crate::valve::{ValveProtocol, App, Server, ServerRule, ServerPlayer}; +use crate::GDResult; +use crate::protocols::valve; +use crate::protocols::valve::{App, Server, ServerRule, ServerPlayer}; #[derive(Debug)] pub struct Player { @@ -71,7 +72,7 @@ impl Response { } pub fn query(address: &str, port: Option) -> GDResult { - let valve_response = ValveProtocol::query(address, match port { + let valve_response = valve::query(address, match port { None => 27015, Some(port) => port }, Some(App::DODS), None)?; diff --git a/src/games/gm.rs b/src/games/gm.rs index f28e6c4..cd08373 100644 --- a/src/games/gm.rs +++ b/src/games/gm.rs @@ -1,5 +1,6 @@ -use crate::{GDResult, valve}; -use crate::valve::{ValveProtocol, App, Server, ServerRule, ServerPlayer}; +use crate::GDResult; +use crate::protocols::valve; +use crate::protocols::valve::{App, Server, ServerRule, ServerPlayer}; #[derive(Debug)] pub struct Player { @@ -71,7 +72,7 @@ impl Response { } pub fn query(address: &str, port: Option) -> GDResult { - let valve_response = ValveProtocol::query(address, match port { + let valve_response = valve::query(address, match port { None => 27015, Some(port) => port }, Some(App::GM), None)?; diff --git a/src/games/hl2dm.rs b/src/games/hl2dm.rs index 3b4dd67..75c40c0 100644 --- a/src/games/hl2dm.rs +++ b/src/games/hl2dm.rs @@ -1,5 +1,6 @@ -use crate::{GDResult, valve}; -use crate::valve::{ValveProtocol, App, Server, ServerRule, ServerPlayer}; +use crate::GDResult; +use crate::protocols::valve; +use crate::protocols::valve::{App, Server, ServerRule, ServerPlayer}; #[derive(Debug)] pub struct Player { @@ -71,7 +72,7 @@ impl Response { } pub fn query(address: &str, port: Option) -> GDResult { - let valve_response = ValveProtocol::query(address, match port { + let valve_response = valve::query(address, match port { None => 27015, Some(port) => port }, Some(App::HL2DM), None)?; diff --git a/src/games/ins.rs b/src/games/ins.rs index 146f8d4..bca0d06 100644 --- a/src/games/ins.rs +++ b/src/games/ins.rs @@ -1,5 +1,6 @@ -use crate::{GDResult, valve}; -use crate::valve::{ValveProtocol, App, Server, ServerRule, ServerPlayer}; +use crate::GDResult; +use crate::protocols::valve; +use crate::protocols::valve::{App, Server, ServerRule, ServerPlayer}; #[derive(Debug)] pub struct Player { @@ -71,7 +72,7 @@ impl Response { } pub fn query(address: &str, port: Option) -> GDResult { - let valve_response = ValveProtocol::query(address, match port { + let valve_response = valve::query(address, match port { None => 27015, Some(port) => port }, Some(App::INS), None)?; diff --git a/src/games/insmic.rs b/src/games/insmic.rs index 4166f74..1146e36 100644 --- a/src/games/insmic.rs +++ b/src/games/insmic.rs @@ -1,5 +1,6 @@ -use crate::{GDResult, valve}; -use crate::valve::{ValveProtocol, App, Server, ServerRule, ServerPlayer}; +use crate::GDResult; +use crate::protocols::valve; +use crate::protocols::valve::{App, Server, ServerRule, ServerPlayer}; #[derive(Debug)] pub struct Player { @@ -71,7 +72,7 @@ impl Response { } pub fn query(address: &str, port: Option) -> GDResult { - let valve_response = ValveProtocol::query(address, match port { + let valve_response = valve::query(address, match port { None => 27015, Some(port) => port }, Some(App::INSMIC), None)?; diff --git a/src/games/inss.rs b/src/games/inss.rs index bf4d168..f202002 100644 --- a/src/games/inss.rs +++ b/src/games/inss.rs @@ -1,5 +1,6 @@ -use crate::{GDResult, valve}; -use crate::valve::{ValveProtocol, App, Server, ServerRule, ServerPlayer}; +use crate::GDResult; +use crate::protocols::valve; +use crate::protocols::valve::{App, Server, ServerRule, ServerPlayer}; #[derive(Debug)] pub struct Player { @@ -71,7 +72,7 @@ impl Response { } pub fn query(address: &str, port: Option) -> GDResult { - let valve_response = ValveProtocol::query(address, match port { + let valve_response = valve::query(address, match port { None => 27131, Some(port) => port }, Some(App::INSS), None)?; diff --git a/src/games/l4d.rs b/src/games/l4d.rs index 0c1ed00..4b945fa 100644 --- a/src/games/l4d.rs +++ b/src/games/l4d.rs @@ -1,5 +1,6 @@ -use crate::{GDResult, valve}; -use crate::valve::{ValveProtocol, App, Server, ServerRule, ServerPlayer}; +use crate::GDResult; +use crate::protocols::valve; +use crate::protocols::valve::{App, Server, ServerRule, ServerPlayer}; #[derive(Debug)] pub struct Player { @@ -71,7 +72,7 @@ impl Response { } pub fn query(address: &str, port: Option) -> GDResult { - let valve_response = ValveProtocol::query(address, match port { + let valve_response = valve::query(address, match port { None => 27015, Some(port) => port }, Some(App::L4D), None)?; diff --git a/src/games/l4d2.rs b/src/games/l4d2.rs index d549568..ca3d8d1 100644 --- a/src/games/l4d2.rs +++ b/src/games/l4d2.rs @@ -1,5 +1,6 @@ -use crate::{GDResult, valve}; -use crate::valve::{ValveProtocol, App, Server, ServerRule, ServerPlayer}; +use crate::GDResult; +use crate::protocols::valve; +use crate::protocols::valve::{App, Server, ServerRule, ServerPlayer}; #[derive(Debug)] pub struct Player { @@ -71,7 +72,7 @@ impl Response { } pub fn query(address: &str, port: Option) -> GDResult { - let valve_response = ValveProtocol::query(address, match port { + let valve_response = valve::query(address, match port { None => 27015, Some(port) => port }, Some(App::L4D2), None)?; diff --git a/src/games/tf2.rs b/src/games/tf2.rs index 1bab6d0..7834d99 100644 --- a/src/games/tf2.rs +++ b/src/games/tf2.rs @@ -1,5 +1,6 @@ -use crate::{GDResult, valve}; -use crate::valve::{ValveProtocol, App, Server, ServerRule, ServerPlayer}; +use crate::GDResult; +use crate::protocols::valve; +use crate::protocols::valve::{App, Server, ServerRule, ServerPlayer}; #[derive(Debug)] pub struct Player { @@ -71,7 +72,7 @@ impl Response { } pub fn query(address: &str, port: Option) -> GDResult { - let valve_response = ValveProtocol::query(address, match port { + let valve_response = valve::query(address, match port { None => 27015, Some(port) => port }, Some(App::TF2), None)?; diff --git a/src/games/ts.rs b/src/games/ts.rs index 6a8b99d..b5edb3b 100644 --- a/src/games/ts.rs +++ b/src/games/ts.rs @@ -1,5 +1,6 @@ -use crate::{GDResult, valve}; -use crate::valve::{ValveProtocol, App, ServerPlayer, Server, ServerRule}; +use crate::GDResult; +use crate::protocols::valve; +use crate::protocols::valve::{App, Server, ServerRule, ServerPlayer}; #[derive(Debug)] pub struct Player { @@ -83,7 +84,7 @@ impl Response { } pub fn query(address: &str, port: Option) -> GDResult { - let valve_response = ValveProtocol::query(address, match port { + let valve_response = valve::query(address, match port { None => 27015, Some(port) => port }, Some(App::TS), None)?; diff --git a/src/lib.rs b/src/lib.rs index 56c1905..4e2fc58 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,5 +21,4 @@ pub mod games; mod utils; pub use errors::*; -pub use protocols::*; pub use games::*; diff --git a/src/protocols/valve.rs b/src/protocols/valve.rs index b431ecf..663da90 100644 --- a/src/protocols/valve.rs +++ b/src/protocols/valve.rs @@ -162,13 +162,6 @@ pub struct GatheringSettings { pub rules: bool } -pub struct ValveProtocol { - socket: UdpSocket, - complete_address: String -} - -static DEFAULT_PACKET_SIZE: usize = 2048; - #[derive(Debug, Clone)] struct Packet { pub header: u32, @@ -298,6 +291,13 @@ impl SplitPacket { } } +struct ValveProtocol { + socket: UdpSocket, + complete_address: String +} + +static DEFAULT_PACKET_SIZE: usize = 2048; + impl ValveProtocol { fn new(address: &str, port: u16) -> GDResult { Ok(Self { @@ -342,7 +342,7 @@ impl ValveProtocol { } /// Ask for a specific request only. - pub fn get_request_data(&self, appid: u32, kind: Request) -> GDResult> { + fn get_request_data(&self, appid: u32, kind: Request) -> GDResult> { let request_initial_packet = Packet::initial(kind.clone()).to_bytes(); self.send(&request_initial_packet)?; @@ -360,7 +360,7 @@ impl ValveProtocol { } /// Get the server information's. - pub fn get_server_info(&self, initial_appid: u32) -> GDResult { + fn get_server_info(&self, initial_appid: u32) -> GDResult { let buf = self.get_request_data(initial_appid, Request::INFO)?; let mut pos = 0; @@ -452,7 +452,7 @@ impl ValveProtocol { } /// Get the server player's. - pub fn get_server_players(&self, appid: u32) -> GDResult> { + fn get_server_players(&self, appid: u32) -> GDResult> { let buf = self.get_request_data(appid, Request::PLAYERS)?; let mut pos = 0; @@ -480,7 +480,7 @@ impl ValveProtocol { } /// Get the server rules's. - pub fn get_server_rules(&self, appid: u32) -> GDResult>> { + fn get_server_rules(&self, appid: u32) -> GDResult>> { if appid == App::CSGO as u32 { //cause csgo wont respond to this since feb 21 2014 update return Ok(None); } @@ -500,44 +500,46 @@ impl ValveProtocol { Ok(Some(rules)) } - - /// Query any app. - pub fn query(address: &str, port: u16, app: Option, gather_settings: Option) -> Result { - let client = ValveProtocol::new(address, port)?; - - let mut query_app_id = match app { - None => 0, - Some(app) => app as u32 - }; - - let info = client.get_server_info(query_app_id)?; - - if query_app_id != 0 { - if info.appid != query_app_id { - 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() { - false => (true, true), - true => { - let settings = gather_settings.unwrap(); - (settings.players, settings.rules) - } - }; - - Ok(Response { - info, - players: match gather_players { - false => None, - true => Some(client.get_server_players(query_app_id)?) - }, - rules: match gather_rules { - false => None, - true => client.get_server_rules(query_app_id)? - } - }) - } +} + +/// Query a server, you need to provide the address, the port and optionally, the app and the +/// gather settings, the app being *None* means to anonymously query the server, and the gather +/// settings being *None* means to get the players and the rules. +pub fn query(address: &str, port: u16, app: Option, gather_settings: Option) -> Result { + let client = ValveProtocol::new(address, port)?; + + let mut query_app_id = match app { + None => 0, + Some(app) => app as u32 + }; + + let info = client.get_server_info(query_app_id)?; + + if query_app_id != 0 { + if info.appid != query_app_id { + 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() { + false => (true, true), + true => { + let settings = gather_settings.unwrap(); + (settings.players, settings.rules) + } + }; + + Ok(Response { + info, + players: match gather_players { + false => None, + true => Some(client.get_server_players(query_app_id)?) + }, + rules: match gather_rules { + false => None, + true => client.get_server_rules(query_app_id)? + } + }) }