Skip to content

Commit

Permalink
TEST: simplify set_config
Browse files Browse the repository at this point in the history
  • Loading branch information
dlon committed Jan 22, 2025
1 parent 870179d commit e24c2ae
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 67 deletions.
9 changes: 2 additions & 7 deletions talpid-wireguard/src/connectivity/mock.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::future::Future;
use std::pin::Pin;
use tokio::time::Instant;

use super::check::{CancelToken, ConnState, PingState};
Expand Down Expand Up @@ -111,11 +109,8 @@ impl Tunnel for MockTunnel {
(self.on_get_stats)()
}

fn set_config(
&mut self,
_config: Config,
) -> Pin<Box<dyn Future<Output = std::result::Result<(), TunnelError>> + Send>> {
Box::pin(async { Ok(()) })
async fn set_config(&mut self, _config: Config) -> std::result::Result<(), TunnelError> {
Ok(())
}

#[cfg(daita)]
Expand Down
7 changes: 1 addition & 6 deletions talpid-wireguard/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")]
Expand Down Expand Up @@ -1012,10 +1010,7 @@ pub(crate) trait Tunnel: Send + Sync {
fn get_interface_name(&self) -> String;
fn stop(self: Box<Self>) -> std::result::Result<(), TunnelError>;
async fn get_tunnel_stats(&self) -> std::result::Result<stats::StatsMap, TunnelError>;
fn set_config<'a>(
&'a mut self,
_config: Config,
) -> Pin<Box<dyn Future<Output = std::result::Result<(), TunnelError>> + 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"))]
Expand Down
26 changes: 9 additions & 17 deletions talpid-wireguard/src/wireguard_kernel/netlink_tunnel.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
use std::pin::Pin;

use futures::Future;
use talpid_tunnel_config_client::DaitaSettings;

use crate::config::MULLVAD_INTERFACE_NAME;
Expand Down Expand Up @@ -114,20 +111,15 @@ impl Tunnel for NetlinkTunnel {
Ok(Stats::parse_device_message(&device))
}

fn set_config(
&mut self,
config: Config,
) -> Pin<Box<dyn Future<Output = std::result::Result<(), TunnelError>> + 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.
Expand Down
26 changes: 11 additions & 15 deletions talpid-wireguard/src/wireguard_kernel/nm_tunnel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -93,22 +92,19 @@ impl Tunnel for NetworkManagerTunnel {
Ok(Stats::parse_device_message(&device))
}

fn set_config(
&mut self,
config: Config,
) -> Pin<Box<dyn Future<Output = std::result::Result<(), TunnelError>> + 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.
Expand Down
36 changes: 14 additions & 22 deletions talpid-wireguard/src/wireguard_nt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand Down Expand Up @@ -1079,27 +1076,22 @@ impl Tunnel for WgNtTunnel {
Ok(())
}

fn set_config(
&mut self,
config: Config,
) -> Pin<Box<dyn Future<Output = std::result::Result<(), super::TunnelError>> + 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(&current_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(&current_config).map_err(|error| {
log::error!(
"{}",
error.display_chain_with_msg("Failed to set wg-nt tunnel config")
);
super::TunnelError::SetConfigError
})
}

Expand Down

0 comments on commit e24c2ae

Please sign in to comment.