diff --git a/Cargo.toml b/Cargo.toml index 852b5c4..2940f6e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,6 +52,7 @@ serial_test = "2.0" test-log = "0.2" [target.'cfg(target_os="windows")'.dependencies] +rand = "0.8" windows = { version = "0.51", features = [ "Win32_Storage_FileSystem", "Win32_NetworkManagement_IpHelper", @@ -60,4 +61,3 @@ windows = { version = "0.51", features = [ "Win32_Foundation", ] } wintun = "0.3" -mio = { version = "0.8", features = ["net", "os-ext", "os-poll"] } diff --git a/src/wintuninterface.rs b/src/wintuninterface.rs index 7de1417..a888aa9 100644 --- a/src/wintuninterface.rs +++ b/src/wintuninterface.rs @@ -13,29 +13,11 @@ use std::{ sync::Arc, vec::Vec, }; -use windows::{ - core::PCWSTR, - Win32::{ - Security::Cryptography::{CryptAcquireContextW, CryptGenRandom, CryptReleaseContext, PROV_RSA_FULL}, - Storage::FileSystem::FILE_FLAG_OVERLAPPED, - }, -}; - -fn generate_random_bytes(len: usize) -> Result, windows::core::Error> { - let mut buf = vec![0u8; len]; - unsafe { - let mut h_prov = 0_usize; - let null = PCWSTR::null(); - CryptAcquireContextW(&mut h_prov, null, null, PROV_RSA_FULL, 0)?; - CryptGenRandom(h_prov, &mut buf)?; - CryptReleaseContext(h_prov, 0)?; - }; - Ok(buf) -} +use windows::Win32::Storage::FileSystem::FILE_FLAG_OVERLAPPED; fn server() -> io::Result<(NamedPipe, String)> { - let num = generate_random_bytes(8).unwrap(); - let num = num.iter().fold(0, |acc, &x| acc * 256 + x as u64); + use rand::Rng; + let num: u64 = rand::thread_rng().gen(); let name = format!(r"\\.\pipe\my-pipe-{}", num); let pipe = NamedPipe::new(&name)?; Ok((pipe, name)) @@ -48,7 +30,7 @@ fn client(name: &str) -> io::Result { unsafe { Ok(NamedPipe::from_raw_handle(file.into_raw_handle())) } } -fn pipe() -> io::Result<(NamedPipe, NamedPipe)> { +pub(crate) fn pipe() -> io::Result<(NamedPipe, NamedPipe)> { let (pipe, name) = server()?; Ok((pipe, client(&name)?)) }