diff options
| -rw-r--r-- | mullvad-daemon/src/main.rs | 14 | ||||
| -rw-r--r-- | talpid-core/src/tunnel/mod.rs | 22 |
2 files changed, 25 insertions, 11 deletions
diff --git a/mullvad-daemon/src/main.rs b/mullvad-daemon/src/main.rs index e92d6aa7ae..57421b3e01 100644 --- a/mullvad-daemon/src/main.rs +++ b/mullvad-daemon/src/main.rs @@ -261,11 +261,15 @@ impl Daemon { } fn handle_tunnel_event(&mut self, tunnel_event: TunnelEvent) -> Result<()> { - info!("Tunnel event: {:?}", tunnel_event); - if self.state == TunnelState::Connecting && tunnel_event == TunnelEvent::Up { - self.tunnel_interface = Some(String::from("utun1")); - self.set_security_policy()?; - self.set_state(TunnelState::Connected) + debug!("Tunnel event: {:?}", tunnel_event); + if self.state == TunnelState::Connecting { + if let TunnelEvent::Up { tunnel_interface } = tunnel_event { + self.tunnel_interface = Some(tunnel_interface); + self.set_security_policy()?; + self.set_state(TunnelState::Connected) + } else { + Ok(()) + } } else if self.state == TunnelState::Connected && tunnel_event == TunnelEvent::Down { self.kill_tunnel() } else { diff --git a/talpid-core/src/tunnel/mod.rs b/talpid-core/src/tunnel/mod.rs index 489fdc8a94..a2ab25a487 100644 --- a/talpid-core/src/tunnel/mod.rs +++ b/talpid-core/src/tunnel/mod.rs @@ -5,6 +5,7 @@ use openvpn_plugin::types::OpenVpnPluginEvent; use process::openvpn::OpenVpnCommand; +use std::collections::HashMap; use std::env; use std::ffi::{OsStr, OsString}; use std::fs; @@ -42,10 +43,13 @@ pub use self::errors::*; /// Possible events from the VPN tunnel and the child process managing it. -#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] +#[derive(Debug, Clone, Eq, PartialEq, Hash)] pub enum TunnelEvent { /// Sent when the tunnel comes up and is ready for traffic. - Up, + Up { + /// The name of the device which the tunnel is running on. + tunnel_interface: String, + }, /// Sent when the tunnel goes down. Down, } @@ -53,9 +57,15 @@ pub enum TunnelEvent { impl TunnelEvent { /// Converts an `OpenVpnPluginEvent` to a `TunnelEvent`. /// Returns `None` if there is no corresponding `TunnelEvent`. - fn from_openvpn_event(event: &OpenVpnPluginEvent) -> Option<TunnelEvent> { + fn from_openvpn_event(event: &OpenVpnPluginEvent, + env: &HashMap<String, String>) + -> Option<TunnelEvent> { match *event { - OpenVpnPluginEvent::Up => Some(TunnelEvent::Up), + OpenVpnPluginEvent::Up => { + let tunnel_interface = + env.get("dev").expect("No \"dev\" in tunnel up event").to_owned(); + Some(TunnelEvent::Up { tunnel_interface }) + } OpenVpnPluginEvent::RoutePredown => Some(TunnelEvent::Down), _ => None, } @@ -81,12 +91,12 @@ impl TunnelMonitor { let cmd = Self::create_openvpn_cmd(remote, user_pass_file.as_ref()); let user_pass_file_path = user_pass_file.to_path_buf(); - let on_openvpn_event = move |event, _env| { + let on_openvpn_event = move |event, env| { if event == OpenVpnPluginEvent::Up { // The user-pass file has been read. Try to delete it early. let _ = fs::remove_file(&user_pass_file_path); } - match TunnelEvent::from_openvpn_event(&event) { + match TunnelEvent::from_openvpn_event(&event, &env) { Some(tunnel_event) => on_event(tunnel_event), None => debug!("Ignoring OpenVpnEvent {:?}", event), } |
