From 5813d1924db58647698767d4a2f21ded994599bd Mon Sep 17 00:00:00 2001 From: ssrlive <30760636+ssrlive@users.noreply.github.com> Date: Sat, 2 Dec 2023 18:16:24 +0800 Subject: [PATCH] add ablility to set device GUID for wintun --- Cargo.toml | 1 + examples/ping-tun.rs | 2 +- src/platform/windows/device.rs | 3 +-- src/platform/windows/mod.rs | 13 +++++++++++-- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e7353857..ce28c6b9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,7 @@ futures = "0.3" packet = "0.1" [features] +default = ["async", "tokio/rt-multi-thread"] async = ["tokio", "tokio-util", "bytes", "byteorder", "futures-core"] [[example]] diff --git a/examples/ping-tun.rs b/examples/ping-tun.rs index 98dc4fd3..ce914941 100644 --- a/examples/ping-tun.rs +++ b/examples/ping-tun.rs @@ -33,7 +33,7 @@ async fn main() -> Result<(), Box> { #[cfg(target_os = "windows")] config.platform(|config| { - config.initialize(); + config.initialize(Some(9099482345783245345345_u128)); }); let dev = tun::create_as_async(&config)?; diff --git a/src/platform/windows/device.rs b/src/platform/windows/device.rs index 4e1463a9..ec924650 100644 --- a/src/platform/windows/device.rs +++ b/src/platform/windows/device.rs @@ -30,10 +30,9 @@ pub struct Device { impl Device { /// Create a new `Device` for the given `Configuration`. - pub fn new(config: &Configuration) -> Result { + pub fn new(config: &Configuration, guid: Option) -> Result { let wintun = unsafe { wintun::load()? }; let tun_name = config.name.as_deref().unwrap_or("wintun"); - let guid = Some(9099482345783245345345_u128); let adapter = match wintun::Adapter::open(&wintun, tun_name) { Ok(a) => a, Err(_) => wintun::Adapter::create(&wintun, tun_name, tun_name, guid)?, diff --git a/src/platform/windows/mod.rs b/src/platform/windows/mod.rs index d5d6c4c7..a55bfbf9 100644 --- a/src/platform/windows/mod.rs +++ b/src/platform/windows/mod.rs @@ -21,17 +21,26 @@ pub use device::{Device, Queue}; use crate::configuration::Configuration as C; use crate::error::*; +static DEVICE_GUID: std::sync::OnceLock> = std::sync::OnceLock::new(); + /// Windows-only interface configuration. #[derive(Copy, Clone, Default, Debug)] pub struct Configuration {} impl Configuration { - pub fn initialize(&mut self) { + pub fn initialize(&mut self, device_guid: Option) { log::trace!("Windows configuration initialize"); + if let Err(err) = DEVICE_GUID.set(device_guid) { + log::error!("set device GUID error \"{:?}\"", err); + } } } /// Create a TUN device with the given name. pub fn create(configuration: &C) -> Result { - Device::new(configuration) + let guid = match DEVICE_GUID.get() { + Some(g) => *g, + None => None, + }; + Device::new(configuration, guid) }