From 993510223d6f9c2d502635be301d9c89f9241107 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 --- examples/ping-tun.rs | 2 +- src/platform/windows/device.rs | 3 +-- src/platform/windows/mod.rs | 10 +++++++--- 3 files changed, 9 insertions(+), 6 deletions(-) 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..e8e3e09e 100644 --- a/src/platform/windows/mod.rs +++ b/src/platform/windows/mod.rs @@ -23,15 +23,19 @@ use crate::error::*; /// Windows-only interface configuration. #[derive(Copy, Clone, Default, Debug)] -pub struct Configuration {} +pub struct Configuration { + pub(crate) device_guid: Option, +} impl Configuration { - pub fn initialize(&mut self) { + pub fn initialize(&mut self, device_guid: Option) { log::trace!("Windows configuration initialize"); + self.device_guid = device_guid; } } /// Create a TUN device with the given name. pub fn create(configuration: &C) -> Result { - Device::new(configuration) + let guid = configuration.platform.device_guid; + Device::new(configuration, guid) }