diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2018-04-23 17:38:51 -0300 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2018-05-03 08:23:30 -0300 |
| commit | ae6119b4e8c8526903a24a70a520db733f598afc (patch) | |
| tree | af0e3cf63dc4b242f15796aeb74052e27b78ad83 | |
| parent | 1ab9a3518c81cecce899e431d518f0a68d117b4a (diff) | |
| download | mullvadvpn-ae6119b4e8c8526903a24a70a520db733f598afc.tar.xz mullvadvpn-ae6119b4e8c8526903a24a70a520db733f598afc.zip | |
Move `DaemonRpcClient` to `mullvad-ipc-client`
| -rw-r--r-- | Cargo.lock | 5 | ||||
| -rw-r--r-- | mullvad-daemon/Cargo.toml | 1 | ||||
| -rw-r--r-- | mullvad-daemon/tests/common/mod.rs | 69 | ||||
| -rw-r--r-- | mullvad-daemon/tests/startup.rs | 6 | ||||
| -rw-r--r-- | mullvad-ipc-client/Cargo.toml | 2 | ||||
| -rw-r--r-- | mullvad-ipc-client/src/lib.rs | 64 |
6 files changed, 78 insertions, 69 deletions
diff --git a/Cargo.lock b/Cargo.lock index 227d472681..28c2ea5117 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -720,6 +720,7 @@ dependencies = [ "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.39 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", + "mullvad-ipc-client 0.1.0", "mullvad-rpc 0.1.0", "mullvad-types 0.1.0", "os_pipe 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -740,6 +741,10 @@ dependencies = [ [[package]] name = "mullvad-ipc-client" version = "0.1.0" +dependencies = [ + "serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", + "talpid-ipc 0.1.0", +] [[package]] name = "mullvad-rpc" diff --git a/mullvad-daemon/Cargo.toml b/mullvad-daemon/Cargo.toml index 581784c8ce..21a54429a8 100644 --- a/mullvad-daemon/Cargo.toml +++ b/mullvad-daemon/Cargo.toml @@ -45,3 +45,4 @@ 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/tests/common/mod.rs b/mullvad-daemon/tests/common/mod.rs index fc6d5283cd..84bd84a3ed 100644 --- a/mullvad-daemon/tests/common/mod.rs +++ b/mullvad-daemon/tests/common/mod.rs @@ -2,45 +2,24 @@ #[cfg(unix)] extern crate libc; +#[cfg(not(unix))] +extern crate mullvad_ipc_client; use std::fs::File; use std::io::{BufRead, BufReader, Write}; -use std::path::{Path, PathBuf}; +use std::path::Path; use std::sync::{mpsc, Arc, Mutex}; use std::thread; use std::time::Duration; use duct; use os_pipe::{pipe, PipeReader}; -use serde::{Deserialize, Serialize}; -use talpid_ipc::WsIpcClient; - -pub use self::platform_specific::*; #[cfg(unix)] -mod platform_specific { - use super::*; - - pub static DAEMON_EXECUTABLE_PATH: &str = "../target/debug/mullvad-daemon"; - - pub fn rpc_file_path() -> PathBuf { - Path::new("/tmp/.mullvad_rpc_address").to_path_buf() - } -} +pub static DAEMON_EXECUTABLE_PATH: &str = "../target/debug/mullvad-daemon"; #[cfg(not(unix))] -mod platform_specific { - use super::*; - - pub static DAEMON_EXECUTABLE_PATH: &str = r"..\target\debug\mullvad-daemon.exe"; - - pub fn rpc_file_path() -> PathBuf { - let windows_directory = ::std::env::var_os("WINDIR").unwrap(); - PathBuf::from(windows_directory) - .join("Temp") - .join(".mullvad_rpc_address") - } -} +pub static DAEMON_EXECUTABLE_PATH: &str = r"..\target\debug\mullvad-daemon.exe"; fn prepare_relay_list<T: AsRef<Path>>(path: T) { let path = path.as_ref(); @@ -53,42 +32,6 @@ fn prepare_relay_list<T: AsRef<Path>>(path: T) { } } -pub struct DaemonRpcClient { - address: String, -} - -impl DaemonRpcClient { - fn new() -> Result<Self, String> { - let rpc_file = File::open(rpc_file_path()) - .map_err(|error| format!("failed to open RPC address file: {}", error))?; - let reader = BufReader::new(rpc_file); - let mut lines = reader.lines(); - let address = lines - .next() - .ok_or("RPC address file is empty".to_string())? - .map_err(|error| format!("failed to read address from RPC address file: {}", error))?; - - Ok(DaemonRpcClient { address }) - } - - pub fn shutdown(&self) -> Result<(), String> { - self.call("shutdown", &[] as &[u8; 0]) - } - - pub fn call<A, O>(&self, method: &str, args: &A) -> Result<O, String> - where - A: Serialize, - O: for<'de> Deserialize<'de>, - { - let mut rpc_client = WsIpcClient::new(self.address.clone()) - .map_err(|error| format!("unable to create RPC client: {}", error))?; - - rpc_client - .call(method, args) - .map_err(|error| format!("RPC request failed: {}", error)) - } -} - pub struct DaemonRunner { process: Option<duct::Handle>, output: Arc<Mutex<BufReader<PipeReader>>>, @@ -154,6 +97,8 @@ impl DaemonRunner { #[cfg(not(unix))] fn request_clean_shutdown(&mut self, _: &mut duct::Handle) -> bool { + use self::mullvad_ipc_client::DaemonRpcClient; + if let Ok(rpc_client) = DaemonRpcClient::new() { rpc_client.shutdown().is_ok() } else { diff --git a/mullvad-daemon/tests/startup.rs b/mullvad-daemon/tests/startup.rs index f3cb7f8a50..51a24c0577 100644 --- a/mullvad-daemon/tests/startup.rs +++ b/mullvad-daemon/tests/startup.rs @@ -1,7 +1,7 @@ #[macro_use] extern crate duct; +extern crate mullvad_ipc_client; extern crate os_pipe; -extern crate serde; extern crate talpid_ipc; mod common; @@ -10,7 +10,9 @@ use std::fs::{self, Metadata}; use std::io; use std::time::Duration; -use common::{rpc_file_path, DaemonRunner}; +use mullvad_ipc_client::rpc_file_path; + +use common::DaemonRunner; use platform_specific::*; diff --git a/mullvad-ipc-client/Cargo.toml b/mullvad-ipc-client/Cargo.toml index 7404e3b12f..b6018ed774 100644 --- a/mullvad-ipc-client/Cargo.toml +++ b/mullvad-ipc-client/Cargo.toml @@ -6,3 +6,5 @@ description = "RPC client for Mullvad daemon" license = "GPL-3.0" [dependencies] +serde = "1.0" +talpid-ipc = { path = "../talpid-ipc" } diff --git a/mullvad-ipc-client/src/lib.rs b/mullvad-ipc-client/src/lib.rs index 31e1bb209f..22704693e8 100644 --- a/mullvad-ipc-client/src/lib.rs +++ b/mullvad-ipc-client/src/lib.rs @@ -1,7 +1,61 @@ -#[cfg(test)] -mod tests { - #[test] - fn it_works() { - assert_eq!(2 + 2, 4); +extern crate serde; +extern crate talpid_ipc; + +use std::fs::File; +use std::io::{BufRead, BufReader}; +use std::path::PathBuf; + +use serde::{Deserialize, Serialize}; + +use talpid_ipc::WsIpcClient; + +pub struct DaemonRpcClient { + address: String, +} + +impl DaemonRpcClient { + pub fn new() -> Result<Self, String> { + let rpc_file = File::open(rpc_file_path()) + .map_err(|error| format!("failed to open RPC address file: {}", error))?; + let reader = BufReader::new(rpc_file); + let mut lines = reader.lines(); + let address = lines + .next() + .ok_or("RPC address file is empty".to_string())? + .map_err(|error| format!("failed to read address from RPC address file: {}", error))?; + + Ok(DaemonRpcClient { address }) + } + + pub fn shutdown(&self) -> Result<(), String> { + self.call("shutdown", &[] as &[u8; 0]) + } + + pub fn call<A, O>(&self, method: &str, args: &A) -> Result<O, String> + where + A: Serialize, + O: for<'de> Deserialize<'de>, + { + let mut rpc_client = WsIpcClient::new(self.address.clone()) + .map_err(|error| format!("unable to create RPC client: {}", error))?; + + rpc_client + .call(method, args) + .map_err(|error| format!("RPC request failed: {}", error)) } } + +#[cfg(unix)] +pub fn rpc_file_path() -> PathBuf { + use std::path::Path; + + Path::new("/tmp/.mullvad_rpc_address").to_path_buf() +} + +#[cfg(windows)] +pub fn rpc_file_path() -> PathBuf { + let windows_directory = ::std::env::var_os("WINDIR").unwrap(); + PathBuf::from(windows_directory) + .join("Temp") + .join(".mullvad_rpc_address") +} |
