diff options
| -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 | ||||
| -rw-r--r-- | mullvad-daemon/src/rpc_info.rs | 8 |
4 files changed, 34 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) } diff --git a/mullvad-daemon/src/rpc_info.rs b/mullvad-daemon/src/rpc_info.rs index 25078ff899..64dad4258b 100644 --- a/mullvad-daemon/src/rpc_info.rs +++ b/mullvad-daemon/src/rpc_info.rs @@ -15,11 +15,19 @@ error_chain! { } } +#[cfg(unix)] +lazy_static! { + /// The path to the file where we write 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 write the RPC address static ref RPC_ADDRESS_FILE_PATH: PathBuf = ::std::env::temp_dir().join(".mullvad_rpc_address"); } + /// Writes down the RPC address to some API to a file. pub fn write(rpc_address: &str) -> Result<()> { open_file(RPC_ADDRESS_FILE_PATH.as_path()) |
