diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2018-05-03 14:40:41 -0300 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2018-05-03 14:40:41 -0300 |
| commit | 3ca2971a0aeaa8151e714f031f8a836601b10242 (patch) | |
| tree | 7167570866415fae6edf03337892228fc9a98540 | |
| parent | aaba78aa2ea2360a801a28be7451ddda8b8008ff (diff) | |
| parent | 9756ed37cc1e095cfc0c48dbd5748758a09324c3 (diff) | |
| download | mullvadvpn-3ca2971a0aeaa8151e714f031f8a836601b10242.tar.xz mullvadvpn-3ca2971a0aeaa8151e714f031f8a836601b10242.zip | |
Merge branch 'mullvad-daemon-using-ipc-client'
| -rw-r--r-- | mullvad-daemon/Cargo.toml | 2 | ||||
| -rw-r--r-- | mullvad-daemon/src/main.rs | 1 | ||||
| -rw-r--r-- | mullvad-daemon/src/rpc_address_file.rs | 46 | ||||
| -rw-r--r-- | mullvad-daemon/src/rpc_uniqueness_check.rs | 39 |
4 files changed, 33 insertions, 55 deletions
diff --git a/mullvad-daemon/Cargo.toml b/mullvad-daemon/Cargo.toml index 21a54429a8..4eae21a271 100644 --- a/mullvad-daemon/Cargo.toml +++ b/mullvad-daemon/Cargo.toml @@ -27,6 +27,7 @@ tokio-core = "0.1" tokio-timer = "0.1" regex = "0.2" +mullvad-ipc-client = { path = "../mullvad-ipc-client" } mullvad-types = { path = "../mullvad-types" } mullvad-rpc = { path = "../mullvad-rpc" } talpid-core = { path = "../talpid-core" } @@ -45,4 +46,3 @@ windows-service = { path = "../windows-service" } assert_matches = "1.0" duct = "0.10" os_pipe = "0.6" -mullvad-ipc-client = { path = "../mullvad-ipc-client" } diff --git a/mullvad-daemon/src/main.rs b/mullvad-daemon/src/main.rs index 897a920737..e0059b2091 100644 --- a/mullvad-daemon/src/main.rs +++ b/mullvad-daemon/src/main.rs @@ -35,6 +35,7 @@ extern crate tokio_core; extern crate tokio_timer; extern crate uuid; +extern crate mullvad_ipc_client; extern crate mullvad_rpc; extern crate mullvad_types; extern crate talpid_core; diff --git a/mullvad-daemon/src/rpc_address_file.rs b/mullvad-daemon/src/rpc_address_file.rs index fc3bc1cf1e..2280c6377f 100644 --- a/mullvad-daemon/src/rpc_address_file.rs +++ b/mullvad-daemon/src/rpc_address_file.rs @@ -1,9 +1,14 @@ use std::fs::{self, File, OpenOptions}; -use std::io::{self, BufRead, BufReader, Write}; +use std::io::{self, Write}; use std::path::{Path, PathBuf}; +use mullvad_ipc_client::rpc_file_path; + error_chain! { errors { + UnknownFilePath { + description("Failed to find path for RPC connection info file") + } WriteFailed(path: PathBuf) { description("Failed to write RPC connection info to file") display("Failed to write RPC connection info to {}", path.to_string_lossy()) @@ -15,55 +20,34 @@ error_chain! { } } -#[cfg(unix)] -lazy_static! { - /// The path to the file where we write the RPC connection info - 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 connection info - static ref RPC_ADDRESS_FILE_PATH: PathBuf = { - let windows_directory = ::std::env::var_os("WINDIR").unwrap(); - PathBuf::from(windows_directory).join("Temp").join(".mullvad_rpc_address") - }; -} - - -/// Reads the address of the RPC connection from the RPC info file. -pub fn read() -> io::Result<String> { - let file = File::open(RPC_ADDRESS_FILE_PATH.as_path())?; - let mut reader = BufReader::new(file); - let mut address = String::new(); - reader.read_line(&mut address)?; - Ok(address) -} - /// Writes down the RPC connection info to some API to a file. pub fn write(rpc_address: &str, shared_secret: &str) -> Result<()> { // Avoids opening an existing file owned by another user and writing sensitive data to it. remove()?; - open_file(RPC_ADDRESS_FILE_PATH.as_path()) + let file_path = rpc_file_path().chain_err(|| ErrorKind::UnknownFilePath)?; + + open_file(&file_path) .and_then(|mut file| write!(file, "{}\n{}\n", rpc_address, shared_secret)) - .chain_err(|| ErrorKind::WriteFailed(RPC_ADDRESS_FILE_PATH.to_owned()))?; + .chain_err(|| ErrorKind::WriteFailed(file_path.clone()))?; debug!( "Wrote RPC connection info to {}", - RPC_ADDRESS_FILE_PATH.to_string_lossy() + file_path.to_string_lossy() ); Ok(()) } /// Removes the RPC file, if it exists. pub fn remove() -> Result<()> { - if let Err(error) = fs::remove_file(RPC_ADDRESS_FILE_PATH.as_path()) { + let file_path = rpc_file_path().chain_err(|| ErrorKind::UnknownFilePath)?; + + if let Err(error) = fs::remove_file(&file_path) { if error.kind() == io::ErrorKind::NotFound { // No previously existing file Ok(()) } else { - Err(error).chain_err(|| ErrorKind::RemoveFailed(RPC_ADDRESS_FILE_PATH.to_owned())) + Err(error).chain_err(|| ErrorKind::RemoveFailed(file_path)) } } else { Ok(()) diff --git a/mullvad-daemon/src/rpc_uniqueness_check.rs b/mullvad-daemon/src/rpc_uniqueness_check.rs index e2479a27d3..fd889cdddf 100644 --- a/mullvad-daemon/src/rpc_uniqueness_check.rs +++ b/mullvad-daemon/src/rpc_uniqueness_check.rs @@ -1,9 +1,6 @@ -use std::result; +use error_chain::ChainedError; -use mullvad_types::states::DaemonState; -use talpid_ipc::WsIpcClient; - -use rpc_address_file; +use mullvad_ipc_client::DaemonRpcClient; /// Checks if there is another instance of the daemon running. @@ -11,27 +8,23 @@ use rpc_address_file; /// Tries to connect to another daemon and perform a simple RPC call. If it fails, assumes the /// other daemon has stopped. pub fn is_another_instance_running() -> bool { - if let Ok(address) = rpc_address_file::read() { - match call_other_instance(address) { + match DaemonRpcClient::new() { + Ok(client) => match client.get_state() { Ok(_) => true, - Err(message) => { - info!("{}; assuming it has stopped", message); + Err(error) => { + let chained_error = error.chain_err(|| { + "Failed to communicate with another daemon instance, assuming it has stopped" + }); + info!("{}", chained_error.display_chain()); false } + }, + Err(error) => { + let chained_error = error.chain_err(|| { + "Failed to load RPC address for another daemon instance, assuming there isn't one" + }); + debug!("{}", chained_error.display_chain()); + false } - } else { - false } } - -fn call_other_instance(address: String) -> result::Result<(), String> { - let method = "get_state"; - let args: [u8; 0] = []; - // TODO: Authenticate with server - let mut rpc_client = - WsIpcClient::new(address).map_err(|_| "Failed to connect to other daemon")?; - let _: DaemonState = rpc_client - .call(method, &args) - .map_err(|_| "Failed to execute RPC call to other daemon")?; - Ok(()) -} |
