From e24c2ae42af98f32c3cb609154796135cffff280 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20L=C3=B6nnhager?= Date: Wed, 22 Jan 2025 18:11:09 +0100 Subject: [PATCH] TEST: simplify set_config --- talpid-wireguard/src/connectivity/mock.rs | 9 ++--- talpid-wireguard/src/lib.rs | 7 +--- .../src/wireguard_kernel/netlink_tunnel.rs | 26 +++++--------- .../src/wireguard_kernel/nm_tunnel.rs | 26 ++++++-------- talpid-wireguard/src/wireguard_nt/mod.rs | 36 ++++++++----------- 5 files changed, 37 insertions(+), 67 deletions(-) diff --git a/talpid-wireguard/src/connectivity/mock.rs b/talpid-wireguard/src/connectivity/mock.rs index 5b7c98b18300..2a131cc607ed 100644 --- a/talpid-wireguard/src/connectivity/mock.rs +++ b/talpid-wireguard/src/connectivity/mock.rs @@ -1,5 +1,3 @@ -use std::future::Future; -use std::pin::Pin; use tokio::time::Instant; use super::check::{CancelToken, ConnState, PingState}; @@ -111,11 +109,8 @@ impl Tunnel for MockTunnel { (self.on_get_stats)() } - fn set_config( - &mut self, - _config: Config, - ) -> Pin> + Send>> { - Box::pin(async { Ok(()) }) + async fn set_config(&mut self, _config: Config) -> std::result::Result<(), TunnelError> { + Ok(()) } #[cfg(daita)] diff --git a/talpid-wireguard/src/lib.rs b/talpid-wireguard/src/lib.rs index 1b377e2f4f6f..fc048d4427a8 100644 --- a/talpid-wireguard/src/lib.rs +++ b/talpid-wireguard/src/lib.rs @@ -5,7 +5,6 @@ use self::config::Config; #[cfg(windows)] use futures::channel::mpsc; -use futures::future::Future; use obfuscation::ObfuscatorHandle; #[cfg(target_os = "android")] use std::borrow::Cow; @@ -15,7 +14,6 @@ use std::{ convert::Infallible, net::IpAddr, path::Path, - pin::Pin, sync::{mpsc as sync_mpsc, Arc, Mutex}, }; #[cfg(target_os = "linux")] @@ -1012,10 +1010,7 @@ pub(crate) trait Tunnel: Send + Sync { fn get_interface_name(&self) -> String; fn stop(self: Box) -> std::result::Result<(), TunnelError>; async fn get_tunnel_stats(&self) -> std::result::Result; - fn set_config<'a>( - &'a mut self, - _config: Config, - ) -> Pin> + Send + 'a>>; + async fn set_config(&mut self, _config: Config) -> std::result::Result<(), TunnelError>; #[cfg(daita)] /// A [`Tunnel`] capable of using DAITA. #[cfg(not(target_os = "windows"))] diff --git a/talpid-wireguard/src/wireguard_kernel/netlink_tunnel.rs b/talpid-wireguard/src/wireguard_kernel/netlink_tunnel.rs index 86285d80a2b7..87f7173b13ac 100644 --- a/talpid-wireguard/src/wireguard_kernel/netlink_tunnel.rs +++ b/talpid-wireguard/src/wireguard_kernel/netlink_tunnel.rs @@ -1,6 +1,3 @@ -use std::pin::Pin; - -use futures::Future; use talpid_tunnel_config_client::DaitaSettings; use crate::config::MULLVAD_INTERFACE_NAME; @@ -114,20 +111,15 @@ impl Tunnel for NetlinkTunnel { Ok(Stats::parse_device_message(&device)) } - fn set_config( - &mut self, - config: Config, - ) -> Pin> + Send + 'static>> { - let mut wg = self.netlink_connections.wg_handle.clone(); - let interface_index = self.interface_index; - Box::pin(async move { - wg.set_config(interface_index, &config) - .await - .map_err(|err| { - log::error!("Failed to set WireGuard device config: {}", err); - TunnelError::SetConfigError - }) - }) + async fn set_config(&mut self, config: Config) -> std::result::Result<(), TunnelError> { + self.netlink_connections + .wg_handle + .set_config(self.interface_index, &config) + .await + .map_err(|err| { + log::error!("Failed to set WireGuard device config: {}", err); + TunnelError::SetConfigError + }) } /// Outright fail to start - this tunnel type does not support DAITA. diff --git a/talpid-wireguard/src/wireguard_kernel/nm_tunnel.rs b/talpid-wireguard/src/wireguard_kernel/nm_tunnel.rs index ba3bca14befc..7edb713805b0 100644 --- a/talpid-wireguard/src/wireguard_kernel/nm_tunnel.rs +++ b/talpid-wireguard/src/wireguard_kernel/nm_tunnel.rs @@ -4,8 +4,7 @@ use super::{ super::stats::{Stats, StatsMap}, Config, Error as WgKernelError, Handle, Tunnel, TunnelError, }; -use futures::Future; -use std::{collections::HashMap, pin::Pin}; +use std::collections::HashMap; use talpid_dbus::{ dbus, network_manager::{ @@ -93,22 +92,19 @@ impl Tunnel for NetworkManagerTunnel { Ok(Stats::parse_device_message(&device)) } - fn set_config( - &mut self, - config: Config, - ) -> Pin> + Send>> { - let interface_name = self.interface_name.clone(); - let mut wg = self.netlink_connections.wg_handle.clone(); - Box::pin(async move { - let index = iface_index(&interface_name).map_err(|err| { - log::error!("Failed to fetch WireGuard device index: {}", err); - TunnelError::SetConfigError - })?; - wg.set_config(index, &config).await.map_err(|err| { + async fn set_config(&mut self, config: Config) -> std::result::Result<(), TunnelError> { + let index = iface_index(&self.interface_name).map_err(|err| { + log::error!("Failed to fetch WireGuard device index: {}", err); + TunnelError::SetConfigError + })?; + self.netlink_connections + .wg_handle + .set_config(index, &config) + .await + .map_err(|err| { log::error!("Failed to apply WireGuard config: {}", err); TunnelError::SetConfigError }) - }) } /// Outright fail to start - this tunnel type does not support DAITA. diff --git a/talpid-wireguard/src/wireguard_nt/mod.rs b/talpid-wireguard/src/wireguard_nt/mod.rs index 9243425cde87..81115f057c2d 100644 --- a/talpid-wireguard/src/wireguard_nt/mod.rs +++ b/talpid-wireguard/src/wireguard_nt/mod.rs @@ -12,14 +12,11 @@ use once_cell::sync::OnceCell; use std::{ffi::c_uchar, path::PathBuf}; use std::{ ffi::CStr, - fmt, - future::Future, - io, + fmt, io, mem::{self, MaybeUninit}, net::{IpAddr, Ipv4Addr, Ipv6Addr}, os::windows::io::RawHandle, path::Path, - pin::Pin, ptr, sync::{Arc, LazyLock, Mutex}, }; @@ -1079,27 +1076,22 @@ impl Tunnel for WgNtTunnel { Ok(()) } - fn set_config( - &mut self, - config: Config, - ) -> Pin> + Send>> { + async fn set_config(&mut self, config: Config) -> std::result::Result<(), super::TunnelError> { let device = self.device.clone(); let current_config = self.config.clone(); - Box::pin(async move { - let Some(device) = device else { - log::error!("Failed to set config: No tunnel device"); - return Err(super::TunnelError::SetConfigError); - }; - let mut current_config = current_config.lock().unwrap(); - *current_config = config; - device.set_config(¤t_config).map_err(|error| { - log::error!( - "{}", - error.display_chain_with_msg("Failed to set wg-nt tunnel config") - ); - super::TunnelError::SetConfigError - }) + let Some(device) = device else { + log::error!("Failed to set config: No tunnel device"); + return Err(super::TunnelError::SetConfigError); + }; + let mut current_config = current_config.lock().unwrap(); + *current_config = config; + device.set_config(¤t_config).map_err(|error| { + log::error!( + "{}", + error.display_chain_with_msg("Failed to set wg-nt tunnel config") + ); + super::TunnelError::SetConfigError }) }