diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2018-03-07 01:37:25 -0300 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2018-03-09 10:44:48 -0300 |
| commit | e47d62159dbda1e931a0835b71ccba0d415354bd (patch) | |
| tree | 018ebea4ea99b4f0a75f8b0aa93651fa262b4e01 | |
| parent | fe44fdcab990e0ee681385aaf65143a60bfaffb7 (diff) | |
| download | mullvadvpn-e47d62159dbda1e931a0835b71ccba0d415354bd.tar.xz mullvadvpn-e47d62159dbda1e931a0835b71ccba0d415354bd.zip | |
Separate `rpc_info` module in two
Rename `rpc_info` into `rpc_address_file` and move RPC client code used
for checking for other instances to a separate `rpc_uniqueness_check`
module.
| -rw-r--r-- | mullvad-daemon/src/main.rs | 11 | ||||
| -rw-r--r-- | mullvad-daemon/src/rpc_address_file.rs (renamed from mullvad-daemon/src/rpc_info.rs) | 43 | ||||
| -rw-r--r-- | mullvad-daemon/src/rpc_uniqueness_check.rs | 34 |
3 files changed, 48 insertions, 40 deletions
diff --git a/mullvad-daemon/src/main.rs b/mullvad-daemon/src/main.rs index f07138b8ff..2f3c25598b 100644 --- a/mullvad-daemon/src/main.rs +++ b/mullvad-daemon/src/main.rs @@ -47,7 +47,8 @@ mod geoip; mod logging; mod management_interface; mod relays; -mod rpc_info; +mod rpc_uniqueness_check; +mod rpc_address_file; mod settings; mod shutdown; @@ -288,7 +289,7 @@ impl Daemon { require_auth: bool, ) -> Result<ManagementInterfaceServer> { ensure!( - !rpc_info::is_another_instance_running(), + !rpc_uniqueness_check::is_another_instance_running(), ErrorKind::DaemonIsAlreadyRunning ); @@ -307,7 +308,7 @@ impl Daemon { ); let written_shared_secret = shared_secret.unwrap_or(String::from("")); - rpc_info::write(server.address(), &written_shared_secret).chain_err(|| { + rpc_address_file::write(server.address(), &written_shared_secret).chain_err(|| { ErrorKind::ManagementInterfaceError("Failed to write RPC connection info to file") })?; Ok(server) @@ -778,7 +779,9 @@ impl DaemonShutdownHandle { impl Drop for Daemon { fn drop(self: &mut Daemon) { - if let Err(e) = rpc_info::remove().chain_err(|| "Unable to clean up rpc address file") { + if let Err(e) = + rpc_address_file::remove().chain_err(|| "Unable to clean up rpc address file") + { error!("{}", e.display_chain()); } } diff --git a/mullvad-daemon/src/rpc_info.rs b/mullvad-daemon/src/rpc_address_file.rs index 43a573d8bb..788ddd6df7 100644 --- a/mullvad-daemon/src/rpc_info.rs +++ b/mullvad-daemon/src/rpc_address_file.rs @@ -1,10 +1,6 @@ use std::fs::{self, File, OpenOptions}; use std::io::{self, BufRead, BufReader, Write}; use std::path::{Path, PathBuf}; -use std::result; - -use mullvad_types::states::DaemonState; -use talpid_ipc::WsIpcClient; error_chain! { errors { @@ -32,17 +28,13 @@ lazy_static! { } -/// Checks if there is another instance of the daemon running. -/// -/// 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 Err(message) = call_other_daemon() { - info!("{}; assuming it has stopped", message); - false - } else { - true - } +/// 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. @@ -75,27 +67,6 @@ pub fn remove() -> Result<()> { } } -fn call_other_daemon() -> result::Result<(), String> { - let method = "get_state"; - let args: [u8; 0] = []; - let address = read_rpc_file().map_err(|_| "Failed to read RPC address file of other daemon")?; - // 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(()) -} - -fn read_rpc_file() -> 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) -} - fn open_file(path: &Path) -> io::Result<File> { let file = OpenOptions::new() .write(true) diff --git a/mullvad-daemon/src/rpc_uniqueness_check.rs b/mullvad-daemon/src/rpc_uniqueness_check.rs new file mode 100644 index 0000000000..f06daa8ba3 --- /dev/null +++ b/mullvad-daemon/src/rpc_uniqueness_check.rs @@ -0,0 +1,34 @@ +use std::result; + +use mullvad_types::states::DaemonState; +use talpid_ipc::WsIpcClient; + +use rpc_address_file; + + +/// Checks if there is another instance of the daemon running. +/// +/// 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 Err(message) = call_other_daemon() { + info!("{}; assuming it has stopped", message); + false + } else { + true + } +} + +fn call_other_daemon() -> result::Result<(), String> { + let method = "get_state"; + let args: [u8; 0] = []; + let address = + rpc_address_file::read().map_err(|_| "Failed to read RPC address file of other daemon")?; + // 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(()) +} |
