From bb7b1d23afdbce10f1fc80b6a92ab715d0175002 Mon Sep 17 00:00:00 2001 From: lubeilin <1791778603@qq.com> Date: Mon, 29 May 2023 19:05:11 +0800 Subject: [PATCH] =?UTF-8?q?tun=E5=A2=9E=E5=8A=A0=E9=87=8D=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- switch/src/tun_device/windows.rs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/switch/src/tun_device/windows.rs b/switch/src/tun_device/windows.rs index d82f670..811da2f 100644 --- a/switch/src/tun_device/windows.rs +++ b/switch/src/tun_device/windows.rs @@ -1,6 +1,7 @@ -use std::io; +use std::{io, thread}; use std::net::Ipv4Addr; use std::sync::Arc; +use std::time::Duration; use libloading::Library; use parking_lot::Mutex; @@ -75,6 +76,7 @@ pub fn create_tun( Ok(lib) => match TunDevice::open(lib, TUN_INTERFACE_NAME) { Ok(tun_device) => { let _ = tun_device.delete(); + thread::sleep(Duration::from_millis(5)); } Err(_) => {} }, @@ -92,11 +94,21 @@ pub fn create_tun( TUN_INTERFACE_NAME, ) { Ok(tun_device) => tun_device, - Err(e) => { - return Err(io::Error::new( - io::ErrorKind::Other, - format!("{:?}", e), - )); + Err(_) => { + thread::sleep(Duration::from_millis(200)); + match TunDevice::create( + Library::new("wintun.dll").unwrap(), + TUN_POOL_NAME, + TUN_INTERFACE_NAME, + ) { + Ok(tun_device) => tun_device, + Err(e) => { + return Err(io::Error::new( + io::ErrorKind::Other, + format!("{:?}", e), + )); + } + } } }; println!("name:{:?}", tun_device.get_name()?);