mirror of
https://github.com/tun2proxy/tun2proxy.git
synced 2025-06-22 09:00:54 +00:00
rand number
This commit is contained in:
parent
bdb7bec062
commit
4710e32873
2 changed files with 5 additions and 23 deletions
|
@ -52,6 +52,7 @@ serial_test = "2.0"
|
||||||
test-log = "0.2"
|
test-log = "0.2"
|
||||||
|
|
||||||
[target.'cfg(target_os="windows")'.dependencies]
|
[target.'cfg(target_os="windows")'.dependencies]
|
||||||
|
rand = "0.8"
|
||||||
windows = { version = "0.51", features = [
|
windows = { version = "0.51", features = [
|
||||||
"Win32_Storage_FileSystem",
|
"Win32_Storage_FileSystem",
|
||||||
"Win32_NetworkManagement_IpHelper",
|
"Win32_NetworkManagement_IpHelper",
|
||||||
|
@ -60,4 +61,3 @@ windows = { version = "0.51", features = [
|
||||||
"Win32_Foundation",
|
"Win32_Foundation",
|
||||||
] }
|
] }
|
||||||
wintun = "0.3"
|
wintun = "0.3"
|
||||||
mio = { version = "0.8", features = ["net", "os-ext", "os-poll"] }
|
|
||||||
|
|
|
@ -13,29 +13,11 @@ use std::{
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
vec::Vec,
|
vec::Vec,
|
||||||
};
|
};
|
||||||
use windows::{
|
use windows::Win32::Storage::FileSystem::FILE_FLAG_OVERLAPPED;
|
||||||
core::PCWSTR,
|
|
||||||
Win32::{
|
|
||||||
Security::Cryptography::{CryptAcquireContextW, CryptGenRandom, CryptReleaseContext, PROV_RSA_FULL},
|
|
||||||
Storage::FileSystem::FILE_FLAG_OVERLAPPED,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
fn generate_random_bytes(len: usize) -> Result<Vec<u8>, 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)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn server() -> io::Result<(NamedPipe, String)> {
|
fn server() -> io::Result<(NamedPipe, String)> {
|
||||||
let num = generate_random_bytes(8).unwrap();
|
use rand::Rng;
|
||||||
let num = num.iter().fold(0, |acc, &x| acc * 256 + x as u64);
|
let num: u64 = rand::thread_rng().gen();
|
||||||
let name = format!(r"\\.\pipe\my-pipe-{}", num);
|
let name = format!(r"\\.\pipe\my-pipe-{}", num);
|
||||||
let pipe = NamedPipe::new(&name)?;
|
let pipe = NamedPipe::new(&name)?;
|
||||||
Ok((pipe, name))
|
Ok((pipe, name))
|
||||||
|
@ -48,7 +30,7 @@ fn client(name: &str) -> io::Result<NamedPipe> {
|
||||||
unsafe { Ok(NamedPipe::from_raw_handle(file.into_raw_handle())) }
|
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()?;
|
let (pipe, name) = server()?;
|
||||||
Ok((pipe, client(&name)?))
|
Ok((pipe, client(&name)?))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue