diff options
| author | Sebastian Holmin <sebastian.holmin@mullvad.net> | 2024-11-28 11:06:16 +0100 |
|---|---|---|
| committer | Sebastian Holmin <sebastian.holmin@mullvad.net> | 2024-12-02 13:39:37 +0100 |
| commit | ca17aee44561ebd23321c82f1f0d05476d96d49d (patch) | |
| tree | 7de740e7e03b2ceac92c41b3e38dc18e6e81df8d /talpid-core/src | |
| parent | 96bceb620d05b2a9d45c11fe9f42f7d90d5eb30a (diff) | |
| download | mullvadvpn-ca17aee44561ebd23321c82f1f0d05476d96d49d.tar.xz mullvadvpn-ca17aee44561ebd23321c82f1f0d05476d96d49d.zip | |
Replace generic with new type
Diffstat (limited to 'talpid-core/src')
| -rw-r--r-- | talpid-core/src/tunnel/mod.rs | 75 | ||||
| -rw-r--r-- | talpid-core/src/tunnel_state_machine/connecting_state.rs | 24 |
2 files changed, 42 insertions, 57 deletions
diff --git a/talpid-core/src/tunnel/mod.rs b/talpid-core/src/tunnel/mod.rs index 5a12b67eb3..63bd01c57a 100644 --- a/talpid-core/src/tunnel/mod.rs +++ b/talpid-core/src/tunnel/mod.rs @@ -14,6 +14,9 @@ use talpid_types::{ tunnel::ErrorStateCause, }; +#[cfg(not(target_os = "android"))] +use talpid_tunnel::EventHook; + const OPENVPN_LOG_FILENAME: &str = "openvpn.log"; const WIREGUARD_LOG_FILENAME: &str = "wireguard.log"; @@ -115,23 +118,19 @@ impl Error { } /// Abstraction for monitoring a generic VPN tunnel. -pub struct TunnelMonitor<F> { - monitor: InternalTunnelMonitor<F>, +pub struct TunnelMonitor { + monitor: InternalTunnelMonitor, } // TODO(emilsp) move most of the openvpn tunnel details to OpenVpnTunnelMonitor -impl<L, F> TunnelMonitor<L> -where - L: (Fn(talpid_tunnel::TunnelEvent) -> F) + Send + Clone + Sync + 'static, - F: std::future::Future<Output = ()> + Send + 'static, -{ +impl TunnelMonitor { /// Creates a new `TunnelMonitor` that connects to the given remote and notifies `on_event` /// on tunnel state changes. #[cfg_attr(any(target_os = "android", windows), allow(unused_variables))] pub fn start( tunnel_parameters: &TunnelParameters, log_dir: &Option<path::PathBuf>, - args: TunnelArgs<'_, L, F>, + args: TunnelArgs<'_>, ) -> Result<Self> { Self::ensure_ipv6_can_be_used_if_enabled(tunnel_parameters)?; let log_file = Self::prepare_tunnel_log_file(tunnel_parameters, log_dir)?; @@ -142,7 +141,7 @@ where config, log_file, args.resource_dir, - args.on_event, + args.event_hook, args.tunnel_close_rx, args.route_manager, )), @@ -155,13 +154,33 @@ where } } + /// Returns a path to an executable that communicates with relay servers. + /// Returns `None` if the executable is unknown. + #[cfg(windows)] + pub fn get_relay_client( + resource_dir: &path::Path, + params: &TunnelParameters, + ) -> Option<path::PathBuf> { + use talpid_types::net::proxy::CustomProxy; + + let resource_dir = resource_dir.to_path_buf(); + match params { + TunnelParameters::OpenVpn(params) => match ¶ms.proxy { + Some(CustomProxy::Shadowsocks(_)) => Some(std::env::current_exe().unwrap()), + Some(CustomProxy::Socks5Local(_)) => None, + Some(CustomProxy::Socks5Remote(_)) | None => Some(resource_dir.join("openvpn.exe")), + }, + _ => Some(std::env::current_exe().unwrap()), + } + } + fn start_wireguard_tunnel( #[cfg(not(any(target_os = "linux", target_os = "windows")))] params: &wireguard_types::TunnelParameters, #[cfg(any(target_os = "linux", target_os = "windows"))] params: &wireguard_types::TunnelParameters, log: Option<path::PathBuf>, - args: TunnelArgs<'_, L, F>, + args: TunnelArgs<'_>, ) -> Result<Self> { let monitor = talpid_wireguard::WireguardMonitor::start(params, log.as_deref(), args)?; Ok(TunnelMonitor { @@ -174,12 +193,12 @@ where config: &openvpn_types::TunnelParameters, log: Option<path::PathBuf>, resource_dir: &path::Path, - on_event: L, + event_hook: EventHook, tunnel_close_rx: oneshot::Receiver<()>, route_manager: RouteManagerHandle, ) -> Result<Self> { let monitor = talpid_openvpn::OpenVpnMonitor::start( - on_event, + event_hook, config, log, resource_dir, @@ -255,39 +274,13 @@ where } } -impl TunnelMonitor<()> { - /// Returns a path to an executable that communicates with relay servers. - /// Returns `None` if the executable is unknown. - #[cfg(windows)] - pub fn get_relay_client( - resource_dir: &path::Path, - params: &TunnelParameters, - ) -> Option<path::PathBuf> { - use talpid_types::net::proxy::CustomProxy; - - let resource_dir = resource_dir.to_path_buf(); - match params { - TunnelParameters::OpenVpn(params) => match ¶ms.proxy { - Some(CustomProxy::Shadowsocks(_)) => Some(std::env::current_exe().unwrap()), - Some(CustomProxy::Socks5Local(_)) => None, - Some(CustomProxy::Socks5Remote(_)) | None => Some(resource_dir.join("openvpn.exe")), - }, - _ => Some(std::env::current_exe().unwrap()), - } - } -} - -enum InternalTunnelMonitor<F> { +enum InternalTunnelMonitor { #[cfg(not(target_os = "android"))] OpenVpn(talpid_openvpn::OpenVpnMonitor), - Wireguard(talpid_wireguard::WireguardMonitor<F>), + Wireguard(talpid_wireguard::WireguardMonitor), } -impl<L, F> InternalTunnelMonitor<L> -where - L: (Fn(talpid_tunnel::TunnelEvent) -> F) + Send + Clone + Sync + 'static, - F: std::future::Future<Output = ()> + Send + 'static, -{ +impl InternalTunnelMonitor { fn wait(self) -> Result<()> { #[cfg(not(target_os = "android"))] let handle = tokio::runtime::Handle::current(); diff --git a/talpid-core/src/tunnel_state_machine/connecting_state.rs b/talpid-core/src/tunnel_state_machine/connecting_state.rs index 1f1ea1be4f..53ef61475e 100644 --- a/talpid-core/src/tunnel_state_machine/connecting_state.rs +++ b/talpid-core/src/tunnel_state_machine/connecting_state.rs @@ -19,7 +19,9 @@ use std::{ time::{Duration, Instant}, }; use talpid_routing::RouteManagerHandle; -use talpid_tunnel::{tun_provider::TunProvider, TunnelArgs, TunnelEvent, TunnelMetadata}; +use talpid_tunnel::{ + tun_provider::TunProvider, EventHook, TunnelArgs, TunnelEvent, TunnelMetadata, +}; use talpid_types::{ net::{AllowedClients, AllowedEndpoint, AllowedTunnelTraffic, TunnelParameters}, tunnel::{ErrorStateCause, FirewallPolicyError}, @@ -214,13 +216,7 @@ impl ConnectingState { retry_attempt: u32, ) -> Self { let (event_tx, event_rx) = mpsc::unbounded(); - let on_tunnel_event = move |event| { - let (tx, rx) = oneshot::channel(); - let _ = event_tx.unbounded_send((event, tx)); - async move { - let _ = rx.await; - } - }; + let event_hook = EventHook::new(event_tx); let route_manager = route_manager.clone(); let log_dir = log_dir.clone(); @@ -237,7 +233,7 @@ impl ConnectingState { let args = TunnelArgs { runtime, resource_dir: &resource_dir, - on_event: on_tunnel_event, + event_hook, tunnel_close_rx, tun_provider, retry_attempt, @@ -289,14 +285,10 @@ impl ConnectingState { } } - fn wait_for_tunnel_monitor<L, F>( - tunnel_monitor: TunnelMonitor<L>, + fn wait_for_tunnel_monitor( + tunnel_monitor: TunnelMonitor, retry_attempt: u32, - ) -> Option<ErrorStateCause> - where - L: (Fn(talpid_tunnel::TunnelEvent) -> F) + Send + Clone + Sync + 'static, - F: std::future::Future<Output = ()> + Send + 'static, - { + ) -> Option<ErrorStateCause> { match tunnel_monitor.wait() { Ok(_) => None, Err(error) => match error { |
