diff --git a/Cargo.lock b/Cargo.lock index eb6a60b..0b67fec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -189,6 +189,7 @@ dependencies = [ "tracing", "tracing-appender", "tracing-subscriber", + "uuid", ] [[package]] @@ -330,6 +331,18 @@ dependencies = [ "slab", ] +[[package]] +name = "getrandom" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43a49c392881ce6d5c3b8cb70f98717b7c07aabbdff06687b9030dbfbe2725f8" +dependencies = [ + "cfg-if", + "libc", + "wasi 0.13.3+wasi-0.2.2", + "windows-targets", +] + [[package]] name = "gimli" version = "0.31.1" @@ -426,7 +439,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd" dependencies = [ "libc", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", "windows-sys 0.52.0", ] @@ -909,6 +922,15 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "uuid" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ced87ca4be083373936a67f8de945faa23b6b42384bd5b64434850802c6dccd0" +dependencies = [ + "getrandom", +] + [[package]] name = "valuable" version = "0.1.1" @@ -921,6 +943,15 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "wasi" +version = "0.13.3+wasi-0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26816d2e1a4a36a2940b96c5296ce403917633dff8f3440e9b236ed6f6bacad2" +dependencies = [ + "wit-bindgen-rt", +] + [[package]] name = "winapi" version = "0.3.9" @@ -1033,3 +1064,12 @@ checksum = "59690dea168f2198d1a3b0cac23b8063efcd11012f10ae4698f284808c8ef603" dependencies = [ "memchr", ] + +[[package]] +name = "wit-bindgen-rt" +version = "0.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3268f3d866458b787f390cf61f4bbb563b922d091359f9608842999eaee3943c" +dependencies = [ + "bitflags", +] diff --git a/Cargo.toml b/Cargo.toml index 42d7893..34a36bf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,3 +37,4 @@ toml = "0.8.19" tracing = { version = "0.1.37", features = ["log"] } tracing-subscriber = { version = "0.3.17", features = ["tracing-log"] } tracing-appender = "0.2.2" +uuid = { version = "1.13.1", features = ["v4"] } diff --git a/src/protocol/packets.rs b/src/protocol/packets.rs index 7e577e5..0a845d4 100644 --- a/src/protocol/packets.rs +++ b/src/protocol/packets.rs @@ -2,6 +2,8 @@ // Inspired by https://github.com/iceiix/stevenarella. +use tracing::trace; + /// Enum representation of a packet's direction. #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub enum PacketDirection { @@ -62,7 +64,9 @@ macro_rules! packets { ClientState::Disconnected => false, } })(packet_body)?; + trace!("Parsing packet: {:?} {:?} {:02x} ({} bytes)", direction, client_state, *packet_id, packet_body.len()); let (_, packet) = Packet::body_parser(client_state, direction, packet_id)(packet_body)?; + // trace!("Parsed packet: {:?}", packet); Ok((input, packet)) } } diff --git a/src/protocol/parsing.rs b/src/protocol/parsing.rs index fa5f2e1..7c4a027 100644 --- a/src/protocol/parsing.rs +++ b/src/protocol/parsing.rs @@ -114,24 +114,13 @@ pub trait Parsable { impl Parsable for Option { #[tracing::instrument] fn parse(data: &[u8]) -> IResult<&[u8], Self> { - let (data, exists) = bool::parse(data)?; - if exists { - let (data, thing) = T::parse(data)?; - Ok((data, Some(thing))) - } else { - Ok((data, None)) - } + nom::combinator::opt(T::parse)(data) } #[tracing::instrument] fn serialize(&self) -> Vec { match self { - Some(t) => { - let mut output = vec![]; - output.extend(true.serialize()); - output.extend(t.serialize()); - output - } - None => false.serialize(), + Some(t) => t.serialize(), + None => Vec::new(), } } } @@ -151,6 +140,16 @@ impl Parsable for Vec { } } +impl Parsable for uuid::Uuid { + #[tracing::instrument] + fn parse(data: &[u8]) -> IResult<&[u8], Self> { + map_res(take(16usize), uuid::Uuid::from_slice)(data) + } + #[tracing::instrument] + fn serialize(&self) -> Vec { + self.as_bytes().to_vec() + } +} impl Parsable for serde_json::Value { #[tracing::instrument] fn parse(data: &[u8]) -> IResult<&[u8], Self> { diff --git a/src/protocol/types.rs b/src/protocol/types.rs index 978d5d8..a0ce8dd 100644 --- a/src/protocol/types.rs +++ b/src/protocol/types.rs @@ -1,7 +1,8 @@ use crate::protocol::parsing::{IResult, Parsable}; +pub use uuid::Uuid; -/// Alias for a u128. -pub type Uuid = u128; +// /// Alias for a u128. +// pub type Uuid = u128; pub use crate::protocol::parsing::VarInt; /// Alias for a `serde_json::Value`. pub type Json = serde_json::Value; diff --git a/src/server/mod.rs b/src/server/mod.rs index be03275..655fd06 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -15,6 +15,7 @@ use tokio::net::{TcpListener, ToSocketAddrs}; use tokio::{sync::RwLock, task::JoinHandle}; use tokio_util::sync::CancellationToken; use tracing::{error, trace}; +use crate::protocol::types::Uuid; /// The main state and logic of the program. #[derive(Debug)] @@ -270,7 +271,7 @@ impl App for Server { // TODO: Get the user from the stored database. // TODO: Encryption/compression. client.queue_packet(LoginSuccess { - uuid: login_start.uuid.unwrap_or(0u128), + uuid: login_start.uuid.unwrap_or(Uuid::nil()), username: login_start.name.clone(), properties: vec![], });