diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2017-09-04 15:17:54 +0200 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2017-09-04 15:18:09 +0200 |
| commit | d2e86d5af3fb76d281d2a4e21298b4b1e36253dc (patch) | |
| tree | 944c4a816589715601d8bfcea502887151e70e51 /mullvad-cli | |
| parent | 3fac53b1d3e4d051b40b67232b67ae85886b0b19 (diff) | |
| download | mullvadvpn-d2e86d5af3fb76d281d2a4e21298b4b1e36253dc.tar.xz mullvadvpn-d2e86d5af3fb76d281d2a4e21298b4b1e36253dc.zip | |
Hardcode tmp path to /tmp on unix
Diffstat (limited to 'mullvad-cli')
| -rw-r--r-- | mullvad-cli/Cargo.toml | 1 | ||||
| -rw-r--r-- | mullvad-cli/src/main.rs | 2 | ||||
| -rw-r--r-- | mullvad-cli/src/rpc.rs | 34 |
3 files changed, 26 insertions, 11 deletions
diff --git a/mullvad-cli/Cargo.toml b/mullvad-cli/Cargo.toml index 88044fd725..e52788b47c 100644 --- a/mullvad-cli/Cargo.toml +++ b/mullvad-cli/Cargo.toml @@ -11,6 +11,7 @@ path = "src/main.rs" [dependencies] clap = "2.20" error-chain = "0.10" +lazy_static = "0.2" log = "0.3" env_logger = "0.4" serde = "1.0" diff --git a/mullvad-cli/src/main.rs b/mullvad-cli/src/main.rs index 08d5459dd7..00e9f5b0fe 100644 --- a/mullvad-cli/src/main.rs +++ b/mullvad-cli/src/main.rs @@ -8,6 +8,8 @@ extern crate clap; #[macro_use] extern crate error_chain; #[macro_use] +extern crate lazy_static; +#[macro_use] extern crate log; extern crate env_logger; extern crate serde; diff --git a/mullvad-cli/src/rpc.rs b/mullvad-cli/src/rpc.rs index 7397e822c4..d6569878cd 100644 --- a/mullvad-cli/src/rpc.rs +++ b/mullvad-cli/src/rpc.rs @@ -1,9 +1,8 @@ - - use {Result, ResultExt}; use serde; use std::fs::File; -use std::io::Read; +use std::io::{self, Read}; +use std::path::{Path, PathBuf}; use talpid_ipc::WsIpcClient; pub fn call<T, O>(method: &str, args: &T) -> Result<O> @@ -17,20 +16,33 @@ pub fn call_internal<T, O>(method: &str, args: &T) -> Result<O> where T: serde::Serialize, O: for<'de> serde::Deserialize<'de> { - let address = read_rpc_address()?; + let address = read_rpc_address().chain_err(|| "Unable to read RPC address")?; info!("Using RPC address {}", address); let mut rpc_client = WsIpcClient::new(address) .chain_err(|| "Unable to create RPC client")?; rpc_client.call(method, args).chain_err(|| format!("Unable to call RPC method {}", method)) } -fn read_rpc_address() -> Result<String> { - let path = ::std::env::temp_dir().join(".mullvad_rpc_address"); - debug!("Trying to read RPC address at {}", path.to_string_lossy()); +#[cfg(unix)] +lazy_static! { + /// The path to the file where we read the RPC address + static ref RPC_ADDRESS_FILE_PATH: PathBuf = Path::new("/tmp").join(".mullvad_rpc_address"); +} + +#[cfg(not(unix))] +lazy_static! { + /// The path to the file where we read the RPC address + static ref RPC_ADDRESS_FILE_PATH: PathBuf = ::std::env::temp_dir().join(".mullvad_rpc_address"); +} + +fn read_rpc_address() -> io::Result<String> { + debug!( + "Trying to read RPC address at {}", + RPC_ADDRESS_FILE_PATH.to_string_lossy() + ); let mut address = String::new(); - if let Ok(_) = File::open(path).and_then(|mut file| file.read_to_string(&mut address)) { - return Ok(address); - } - bail!("Unable to read RPC address"); + let mut file = File::open(&*RPC_ADDRESS_FILE_PATH)?; + file.read_to_string(&mut address)?; + Ok(address) } |
