diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 5185c76..b12c82e 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -51,8 +51,8 @@ enum Action { #[arg(short, long)] ip: String, - /// Optional query port number for the server. If not provided the default - /// port for the game is used. + /// Optional query port number for the server. If not provided the + /// default port for the game is used. #[arg(short, long)] port: Option, @@ -195,39 +195,51 @@ fn set_hostname_if_missing(host: &str, extra_options: &mut Option(output_mode: OutputMode, format: OutputFormat, result: &T) { match format { - OutputFormat::Debug => match output_mode { - OutputMode::Generic => output_result_debug(result.as_json()), - OutputMode::ProtocolSpecific => output_result_debug(result.as_original()), - }, + OutputFormat::Debug => { + match output_mode { + OutputMode::Generic => output_result_debug(result.as_json()), + OutputMode::ProtocolSpecific => output_result_debug(result.as_original()), + } + } #[cfg(feature = "json")] - OutputFormat::JsonPretty => match output_mode { - OutputMode::Generic => output_result_json_pretty(result.as_json()), - OutputMode::ProtocolSpecific => output_result_json_pretty(result.as_original()), - }, + OutputFormat::JsonPretty => { + match output_mode { + OutputMode::Generic => output_result_json_pretty(result.as_json()), + OutputMode::ProtocolSpecific => output_result_json_pretty(result.as_original()), + } + } #[cfg(feature = "json")] - OutputFormat::Json => match output_mode { - OutputMode::Generic => output_result_json(result.as_json()), - OutputMode::ProtocolSpecific => output_result_json(result.as_original()), - }, + OutputFormat::Json => { + match output_mode { + OutputMode::Generic => output_result_json(result.as_json()), + OutputMode::ProtocolSpecific => output_result_json(result.as_original()), + } + } #[cfg(feature = "xml")] - OutputFormat::Xml => match output_mode { - OutputMode::Generic => output_result_xml(result.as_json()), - //BUG: In this case we get a writer error with all serde write methods some reason with xml 0.6.0 - //BUG: gamedig-cli.exe query -g -i -p -f xml --output-mode protocol-specific - //BUG: Writer: emitter error: document start event has already been emitted - //BUG: With xml 0.5.1 we get unsupported operation: 'serialize_unit_variant' - OutputMode::ProtocolSpecific => panic!("XML format is not supported for protocol specific output"), - }, + OutputFormat::Xml => { + match output_mode { + OutputMode::Generic => output_result_xml(result.as_json()), + // BUG: In this case we get a writer error with all serde write methods some reason with xml 0.6.0 + // BUG: gamedig-cli.exe query -g -i -p -f xml --output-mode protocol-specific + // BUG: Writer: emitter error: document start event has already been emitted + // BUG: With xml 0.5.1 we get unsupported operation: 'serialize_unit_variant' + OutputMode::ProtocolSpecific => panic!("XML format is not supported for protocol specific output"), + } + } #[cfg(feature = "bson")] - OutputFormat::BsonHex => match output_mode { - OutputMode::Generic => output_result_bson_hex(result.as_json()), - OutputMode::ProtocolSpecific => output_result_bson_hex(result.as_original()), - }, + OutputFormat::BsonHex => { + match output_mode { + OutputMode::Generic => output_result_bson_hex(result.as_json()), + OutputMode::ProtocolSpecific => output_result_bson_hex(result.as_original()), + } + } #[cfg(feature = "bson")] - OutputFormat::BsonBase64 => match output_mode { - OutputMode::Generic => output_result_bson_base64(result.as_json()), - OutputMode::ProtocolSpecific => output_result_bson_base64(result.as_original()), - }, + OutputFormat::BsonBase64 => { + match output_mode { + OutputMode::Generic => output_result_bson_base64(result.as_json()), + OutputMode::ProtocolSpecific => output_result_bson_base64(result.as_original()), + } + } } } diff --git a/crates/lib/src/capture/mod.rs b/crates/lib/src/capture/mod.rs index 13bf7af..a1995f8 100644 --- a/crates/lib/src/capture/mod.rs +++ b/crates/lib/src/capture/mod.rs @@ -38,6 +38,4 @@ pub fn setup_capture(file_path: Option) { /// /// # Errors /// Returns an Error if the writer is already set. -fn attach(writer: Box) { - crate::socket::capture::set_writer(writer); -} +fn attach(writer: Box) { crate::socket::capture::set_writer(writer); } diff --git a/crates/lib/src/capture/packet.rs b/crates/lib/src/capture/packet.rs index 83910d0..71e8136 100644 --- a/crates/lib/src/capture/packet.rs +++ b/crates/lib/src/capture/packet.rs @@ -31,7 +31,8 @@ pub(crate) enum Protocol { /// Trait for handling different types of IP addresses (IPv4, IPv6). pub(crate) trait IpAddress: Sized { - /// Creates an instance from a standard `IpAddr`, returning `None` if the types are incompatible. + /// Creates an instance from a standard `IpAddr`, returning `None` if the + /// types are incompatible. fn from_std(ip: IpAddr) -> Option; } @@ -67,12 +68,15 @@ impl CapturePacket<'_> { (local, remote) } - /// Retrieves IP addresses of a specific type (IPv4 or IPv6) based on the packet's direction. + /// Retrieves IP addresses of a specific type (IPv4 or IPv6) based on the + /// packet's direction. /// - /// Panics if the IP type of the addresses does not match the requested type. + /// Panics if the IP type of the addresses does not match the requested + /// type. /// /// Returns: - /// - (T, T): Tuple of (source IP, destination IP) of the specified type in order. + /// - (T, T): Tuple of (source IP, destination IP) of the specified type in + /// order. pub(super) fn ipvt_by_direction(&self) -> (T, T) { let (local, remote) = ( T::from_std(self.local_address.ip()).expect("Incorrect IP type for local address"), @@ -84,7 +88,8 @@ impl CapturePacket<'_> { } impl Direction { - /// Orders two elements (source and destination) based on the packet's direction. + /// Orders two elements (source and destination) based on the packet's + /// direction. /// /// Returns: /// - (T, T): Ordered tuple (source, destination). @@ -124,9 +129,7 @@ mod tests { use std::str::FromStr; // Helper function to create a SocketAddr from a string - fn socket_addr(addr: &str) -> SocketAddr { - SocketAddr::from_str(addr).unwrap() - } + fn socket_addr(addr: &str) -> SocketAddr { SocketAddr::from_str(addr).unwrap() } #[test] fn test_ports_by_direction() { diff --git a/crates/lib/src/capture/pcap.rs b/crates/lib/src/capture/pcap.rs index 0eb8b21..0613619 100644 --- a/crates/lib/src/capture/pcap.rs +++ b/crates/lib/src/capture/pcap.rs @@ -11,7 +11,13 @@ use pnet_packet::{ use std::{io::Write, net::IpAddr, time::Instant}; use super::packet::{ - CapturePacket, Direction, Protocol, HEADER_SIZE_ETHERNET, HEADER_SIZE_IP4, HEADER_SIZE_IP6, HEADER_SIZE_UDP, + CapturePacket, + Direction, + Protocol, + HEADER_SIZE_ETHERNET, + HEADER_SIZE_IP4, + HEADER_SIZE_IP6, + HEADER_SIZE_UDP, PACKET_SIZE, }; @@ -80,7 +86,7 @@ impl Pcap { self.write_transport_payload( info, IpNextHeaderProtocols::Tcp, - &buf[..buf_size + payload.len()], + &buf[.. buf_size + payload.len()], vec![], ); @@ -113,7 +119,7 @@ impl Pcap { self.write_transport_payload( &info, IpNextHeaderProtocols::Tcp, - &buf[..buf_size], + &buf[.. buf_size], vec![EnhancedPacketOption::Comment("Generated TCP ACK".into())], ); } @@ -131,7 +137,7 @@ impl Pcap { self.write_transport_payload( info, IpNextHeaderProtocols::Udp, - &buf[..buf_size + payload.len()], + &buf[.. buf_size + payload.len()], vec![], ); } @@ -238,7 +244,7 @@ impl Pcap { self.write_transport_payload( &info, IpNextHeaderProtocols::Tcp, - &buf[..buf_size], + &buf[.. buf_size], options.clone(), ); @@ -261,7 +267,7 @@ impl Pcap { self.write_transport_payload( &info, IpNextHeaderProtocols::Tcp, - &buf[..buf_size], + &buf[.. buf_size], options.clone(), ); @@ -280,7 +286,12 @@ impl Pcap { tcp.packet_size() }; - self.write_transport_payload(&info, IpNextHeaderProtocols::Tcp, &buf[..buf_size], options); + self.write_transport_payload( + &info, + IpNextHeaderProtocols::Tcp, + &buf[.. buf_size], + options, + ); self.state.has_sent_handshake = true; } @@ -314,7 +325,7 @@ impl Pcap { self.write_transport_payload( info, IpNextHeaderProtocols::Tcp, - &buf[..buf_size], + &buf[.. buf_size], vec![EnhancedPacketOption::Comment("Generated TCP FIN".into())], ); diff --git a/crates/lib/src/socket.rs b/crates/lib/src/socket.rs index 9dd1e91..4548720 100644 --- a/crates/lib/src/socket.rs +++ b/crates/lib/src/socket.rs @@ -22,8 +22,7 @@ pub trait Socket { /// # Returns /// A result containing the socket instance or an error. fn new(address: &SocketAddr, timeout_settings: &Option) -> GDResult - where - Self: Sized; + where Self: Sized; /// Apply read and write timeouts to the socket. /// @@ -112,12 +111,8 @@ impl Socket for TcpSocketImpl { Ok(buf) } - fn port(&self) -> u16 { - self.address.port() - } - fn local_addr(&self) -> std::io::Result { - self.socket.local_addr() - } + fn port(&self) -> u16 { self.address.port() } + fn local_addr(&self) -> std::io::Result { self.socket.local_addr() } } /// Implementation of a UDP socket. @@ -165,15 +160,11 @@ impl Socket for UdpSocketImpl { .recv_from(&mut buf) .map_err(|e| PacketReceive.context(e))?; - Ok(buf[..number_of_bytes_received].to_vec()) + Ok(buf[.. number_of_bytes_received].to_vec()) } - fn port(&self) -> u16 { - self.address.port() - } - fn local_addr(&self) -> std::io::Result { - self.socket.local_addr() - } + fn port(&self) -> u16 { self.address.port() } + fn local_addr(&self) -> std::io::Result { self.socket.local_addr() } } /// Things used for capturing packets. @@ -219,17 +210,13 @@ pub mod capture { /// Represents the TCP protocol provider. pub(crate) struct ProtocolTCP; impl ProtocolProvider for ProtocolTCP { - fn protocol() -> Protocol { - Protocol::TCP - } + fn protocol() -> Protocol { Protocol::TCP } } /// Represents the UDP protocol provider. pub(crate) struct ProtocolUDP; impl ProtocolProvider for ProtocolUDP { - fn protocol() -> Protocol { - Protocol::UDP - } + fn protocol() -> Protocol { Protocol::UDP } } /// A socket wrapper that allows capturing packets. @@ -247,8 +234,9 @@ pub mod capture { impl Socket for WrappedCaptureSocket { /// Creates a new wrapped socket for capturing packets. /// - /// Initializes a new socket of type `I`, wrapping it to enable packet capturing. - /// Capturing is protocol-specific, as indicated by the `ProtocolProvider`. + /// Initializes a new socket of type `I`, wrapping it to enable packet + /// capturing. Capturing is protocol-specific, as indicated by + /// the `ProtocolProvider`. /// /// # Arguments /// * `address` - The address to connect the socket to. @@ -257,9 +245,7 @@ pub mod capture { /// # Returns /// A `GDResult` containing either the wrapped socket or an error. fn new(address: &SocketAddr, timeout_settings: &Option) -> GDResult - where - Self: Sized, - { + where Self: Sized { let v = Self { inner: I::new(address, timeout_settings)?, remote_address: *address, @@ -282,8 +268,8 @@ pub mod capture { /// Sends data over the socket and captures the packet. /// - /// The method sends data using the inner socket and captures the sent packet - /// if a capture writer is set. + /// The method sends data using the inner socket and captures the sent + /// packet if a capture writer is set. /// /// # Arguments /// * `data` - Data to be sent. @@ -307,8 +293,8 @@ pub mod capture { /// Receives data from the socket and captures the packet. /// - /// The method receives data using the inner socket and captures the incoming packet - /// if a capture writer is set. + /// The method receives data using the inner socket and captures the + /// incoming packet if a capture writer is set. /// /// # Arguments /// * `size` - Optional size of data to receive. @@ -353,9 +339,7 @@ pub mod capture { /// /// # Returns /// The remote port number. - fn port(&self) -> u16 { - self.inner.port() - } + fn port(&self) -> u16 { self.inner.port() } /// Returns the local SocketAddr of the wrapped socket. /// @@ -363,9 +347,7 @@ pub mod capture { /// /// # Returns /// The local SocketAddr. - fn local_addr(&self) -> std::io::Result { - self.inner.local_addr() - } + fn local_addr(&self) -> std::io::Result { self.inner.local_addr() } } // this seems a bad way to do this, but its safe @@ -388,18 +370,20 @@ pub mod capture { } } - /// A specialized `WrappedCaptureSocket` for UDP, using `UdpSocketImpl` as the inner socket - /// and `ProtocolUDP` as the protocol provider. + /// A specialized `WrappedCaptureSocket` for UDP, using `UdpSocketImpl` as + /// the inner socket and `ProtocolUDP` as the protocol provider. /// - /// This type captures and processes UDP packets, wrapping around standard UDP socket - /// functionalities with additional packet capture capabilities. + /// This type captures and processes UDP packets, wrapping around standard + /// UDP socket functionalities with additional packet capture + /// capabilities. pub(crate) type CapturedUdpSocket = WrappedCaptureSocket; - /// A specialized `WrappedCaptureSocket` for TCP, using `TcpSocketImpl` as the inner socket - /// and `ProtocolTCP` as the protocol provider. + /// A specialized `WrappedCaptureSocket` for TCP, using `TcpSocketImpl` as + /// the inner socket and `ProtocolTCP` as the protocol provider. /// - /// This type captures and processes TCP packets, wrapping around standard TCP socket - /// functionalities with additional packet capture capabilities. + /// This type captures and processes TCP packets, wrapping around standard + /// TCP socket functionalities with additional packet capture + /// capabilities. pub(crate) type CapturedTcpSocket = WrappedCaptureSocket; }