summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--talpid-core/src/tunnel/openvpn/mod.rs20
-rw-r--r--talpid-core/src/tunnel/openvpn/windows.rs1
2 files changed, 18 insertions, 3 deletions
diff --git a/talpid-core/src/tunnel/openvpn/mod.rs b/talpid-core/src/tunnel/openvpn/mod.rs
index fd186026d4..38a1d26d22 100644
--- a/talpid-core/src/tunnel/openvpn/mod.rs
+++ b/talpid-core/src/tunnel/openvpn/mod.rs
@@ -30,7 +30,7 @@ use std::{
#[cfg(target_os = "linux")]
use std::{collections::HashSet, net::IpAddr};
#[cfg(windows)]
-use std::{ffi::OsStr, os::windows::ffi::OsStrExt, time::Instant};
+use std::{ffi::OsStr, os::windows::ffi::OsStrExt, sync::Mutex, time::Instant};
use talpid_types::net::openvpn;
#[cfg(any(windows, target_os = "linux"))]
use talpid_types::ErrorExt;
@@ -60,12 +60,27 @@ mod windows;
#[cfg(windows)]
lazy_static! {
+ static ref WINTUN_DLL: Mutex<Option<Arc<windows::WintunDll>>> = Mutex::new(None);
static ref ADAPTER_ALIAS: U16CString = U16CString::from_str("Mullvad").unwrap();
static ref ADAPTER_POOL: U16CString = U16CString::from_str("Mullvad").unwrap();
static ref ADAPTER_GUID_STR: String = windows::string_from_guid(&ADAPTER_GUID);
}
#[cfg(windows)]
+fn get_wintun_dll(resource_dir: &Path) -> Result<Arc<windows::WintunDll>> {
+ let mut dll = (*WINTUN_DLL).lock().expect("Wintun mutex poisoned");
+ match &*dll {
+ Some(dll) => Ok(dll.clone()),
+ None => {
+ let new_dll =
+ Arc::new(windows::WintunDll::new(resource_dir).map_err(Error::WintunDllError)?);
+ *dll = Some(new_dll.clone());
+ Ok(new_dll)
+ }
+ }
+}
+
+#[cfg(windows)]
const ADAPTER_GUID: GUID = GUID {
Data1: 0xAFE43773,
Data2: 0xE1F8,
@@ -347,8 +362,7 @@ impl OpenVpnMonitor<OpenVpnCommand> {
#[cfg(windows)]
let wintun_adapter = {
- let dll =
- Arc::new(windows::WintunDll::new(resource_dir).map_err(Error::WintunDllError)?);
+ let dll = get_wintun_dll(resource_dir)?;
{
match windows::WintunAdapter::open(dll.clone(), &*ADAPTER_ALIAS, &*ADAPTER_POOL) {
diff --git a/talpid-core/src/tunnel/openvpn/windows.rs b/talpid-core/src/tunnel/openvpn/windows.rs
index 1c691794e0..f87efc3967 100644
--- a/talpid-core/src/tunnel/openvpn/windows.rs
+++ b/talpid-core/src/tunnel/openvpn/windows.rs
@@ -50,6 +50,7 @@ pub struct WintunDll {
func_delete: WintunDeleteAdapterFn,
}
+unsafe impl Send for WintunDll {}
unsafe impl Sync for WintunDll {}
type RebootRequired = bool;