diff --git a/Cargo.toml b/Cargo.toml index c19e6da..b197c65 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,9 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[dependencies] +rand = "^0.8" + [dependencies.tokio] version = "^1.0" features = ["net", "time"] diff --git a/src/lib.rs b/src/lib.rs index d48cf12..e4dc09c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,15 +44,9 @@ use tokio::{ pub const SESSION_ID_MASK: u32 = 0x0F0F0F0F; const REQUEST_HEADER: [u8; 2] = [0xFE, 0xFD]; -static mut SESSION_ID_COUNTER: u16 = 0; - fn gen_session_id() -> u32 { - unsafe { - SESSION_ID_COUNTER = SESSION_ID_COUNTER.wrapping_add(1); - } - let mut session_id_bytes = [0; 4]; - for (i, b) in unsafe { SESSION_ID_COUNTER }.to_be_bytes().iter().enumerate() { + for (i, b) in rand::random::().to_be_bytes().iter().enumerate() { session_id_bytes[i * 2] = b >> 4; session_id_bytes[i * 2 + 1] = b & 0x0F; }