diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2017-11-14 05:26:05 +0100 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2017-11-14 06:10:44 +0100 |
| commit | 3068edb2ecb9d7440dbcd7523434400294b1e8de (patch) | |
| tree | 4f2fecf7e8e99428b47cbd5b4be1c11f732f2c6a | |
| parent | 434b5343f74fa35364f482e24248de4cf7e85d16 (diff) | |
| download | mullvadvpn-3068edb2ecb9d7440dbcd7523434400294b1e8de.tar.xz mullvadvpn-3068edb2ecb9d7440dbcd7523434400294b1e8de.zip | |
Move finding resource dir to top
| -rw-r--r-- | mullvad-daemon/src/main.rs | 21 | ||||
| -rw-r--r-- | talpid-core/src/tunnel/mod.rs | 74 |
2 files changed, 45 insertions, 50 deletions
diff --git a/mullvad-daemon/src/main.rs b/mullvad-daemon/src/main.rs index 5b17a67be4..a5ea07cabd 100644 --- a/mullvad-daemon/src/main.rs +++ b/mullvad-daemon/src/main.rs @@ -55,6 +55,7 @@ use mullvad_types::relay_endpoint::RelayEndpoint; use mullvad_types::states::{DaemonState, SecurityState, TargetState}; use rand::Rng; +use std::env; use std::io; use std::net::Ipv4Addr; use std::path::PathBuf; @@ -185,6 +186,7 @@ struct Daemon { // Just for testing. A cyclic iterator iterating over the hardcoded relays, // picking a new one for each retry. relay_iter: std::iter::Cycle<std::iter::Cloned<std::slice::Iter<'static, Endpoint>>>, + resource_dir: PathBuf, } impl Daemon { @@ -193,6 +195,7 @@ impl Daemon { let management_interface_broadcaster = Self::start_management_interface(tx.clone())?; let state = TunnelState::NotRunning; let target_state = TargetState::Unsecured; + let resource_dir = get_resource_dir(); Ok(Daemon { state, tunnel_close_handle: None, @@ -212,6 +215,7 @@ impl Daemon { tunnel_metadata: None, tunnel_log: tunnel_log, relay_iter: RELAYS.iter().cloned().cycle(), + resource_dir, }) } @@ -580,6 +584,7 @@ impl Daemon { tunnel_endpoint, account_token, self.tunnel_log.as_ref().map(PathBuf::as_path), + &self.resource_dir, on_tunnel_event, ).chain_err(|| ErrorKind::TunnelError("Unable to start tunnel monitor")) } @@ -734,3 +739,19 @@ fn randomize_port(protocol: TransportProtocol) -> u16 { .choose(&pool) .expect("no ports to randomize from") } + +fn get_resource_dir() -> PathBuf { + match env::current_exe() { + Ok(mut path) => { + path.pop(); + path + } + Err(e) => { + error!( + "Failed finding the directory of the executable. Using working directory: {}", + e + ); + PathBuf::from(".") + } + } +} diff --git a/talpid-core/src/tunnel/mod.rs b/talpid-core/src/tunnel/mod.rs index af0002c61d..0e12d89287 100644 --- a/talpid-core/src/tunnel/mod.rs +++ b/talpid-core/src/tunnel/mod.rs @@ -5,7 +5,6 @@ use openvpn_plugin::types::OpenVpnPluginEvent; use process::openvpn::OpenVpnCommand; use std::collections::HashMap; -use std::env; use std::ffi::{OsStr, OsString}; use std::fs; use std::io::{self, Write}; @@ -115,6 +114,7 @@ impl TunnelMonitor { remote: TunnelEndpoint, account_token: &str, log: Option<&Path>, + resource_dir: &Path, on_event: L, ) -> Result<Self> where @@ -126,9 +126,14 @@ impl TunnelMonitor { }; let user_pass_file = Self::create_user_pass_file(account_token) .chain_err(|| ErrorKind::CredentialsWriteError)?; - let cmd = Self::create_openvpn_cmd(remote, user_pass_file.as_ref(), log); - let user_pass_file_path = user_pass_file.to_path_buf(); + let cmd = Self::create_openvpn_cmd( + tunnel_endpoint.to_endpoint(), + user_pass_file.as_ref(), + log, + resource_dir, + ); + let user_pass_file_path = user_pass_file.to_path_buf(); let on_openvpn_event = move |event, env| { if event == OpenVpnPluginEvent::Up { // The user-pass file has been read. Try to delete it early. @@ -140,8 +145,11 @@ impl TunnelMonitor { } }; - let monitor = openvpn::OpenVpnMonitor::new(cmd, on_openvpn_event, Self::get_plugin_path()?) - .chain_err(|| ErrorKind::TunnelMonitoringError)?; + let monitor = openvpn::OpenVpnMonitor::new( + cmd, + on_openvpn_event, + Self::get_plugin_path(resource_dir)?, + ).chain_err(|| ErrorKind::TunnelMonitoringError)?; Ok(TunnelMonitor { monitor, _user_pass_file: user_pass_file, @@ -152,28 +160,25 @@ impl TunnelMonitor { remote: Endpoint, user_pass_file: &Path, log: Option<&Path>, + resource_dir: &Path, ) -> OpenVpnCommand { - let mut cmd = OpenVpnCommand::new(Self::get_openvpn_bin()); - if let Some(config) = Self::get_config_path() { + let mut cmd = OpenVpnCommand::new(Self::get_openvpn_bin(resource_dir)); + if let Some(config) = Self::get_config_path(resource_dir) { cmd.config(config); } cmd.remote(remote) .user_pass(user_pass_file) - .ca(Self::get_ca_path()) - .crl(Self::get_crl_path()); + .ca(resource_dir.join("ca.crt")) + .crl(resource_dir.join("crl.pem")); if let Some(log) = log { cmd.log(log); } cmd } - fn get_openvpn_bin() -> OsString { + fn get_openvpn_bin(resource_dir: &Path) -> OsString { let bin = OsStr::new("openvpn"); - let bundled_path = Self::get_install_dir() - .unwrap_or(PathBuf::from(".")) - .join("openvpn-binaries") - .join(bin); - + let bundled_path = resource_dir.join("openvpn-binaries").join(bin); if bundled_path.exists() { bundled_path.into_os_string() } else { @@ -182,24 +187,9 @@ impl TunnelMonitor { } } - fn get_ca_path() -> PathBuf { - Self::get_install_dir() - .unwrap_or(PathBuf::from(".")) - .join("ca.crt") - } - - fn get_crl_path() -> PathBuf { - Self::get_install_dir() - .unwrap_or(PathBuf::from(".")) - .join("crl.pem") - } - - fn get_plugin_path() -> Result<PathBuf> { + fn get_plugin_path(resource_dir: &Path) -> Result<PathBuf> { let lib_ext = Self::get_library_extension().chain_err(|| ErrorKind::PluginNotFound)?; - - let path = Self::get_install_dir() - .unwrap_or(PathBuf::from(".")) - .join(format!("libtalpid_openvpn_plugin.{}", lib_ext)); + let path = resource_dir.join(format!("libtalpid_openvpn_plugin.{}", lib_ext)); if path.exists() { debug!("Using OpenVPN plugin at {}", path.to_string_lossy()); @@ -221,11 +211,8 @@ impl TunnelMonitor { } } - fn get_config_path() -> Option<PathBuf> { - let path = Self::get_install_dir() - .unwrap_or(PathBuf::from(".")) - .join("openvpn.conf"); - + fn get_config_path(resource_dir: &Path) -> Option<PathBuf> { + let path = resource_dir.join("openvpn.conf"); if path.exists() { Some(path) } else { @@ -233,19 +220,6 @@ impl TunnelMonitor { } } - fn get_install_dir() -> Option<PathBuf> { - match env::current_exe() { - Ok(mut path) => { - path.pop(); - Some(path) - } - Err(e) => { - error!("Failed finding the directory of the executable: {}", e); - None - } - } - } - fn create_user_pass_file(account_token: &str) -> io::Result<mktemp::TempFile> { let temp_file = mktemp::TempFile::new(); debug!( |
