diff options
| -rw-r--r-- | mullvad_daemon/src/main.rs | 11 | ||||
| -rw-r--r-- | mullvad_daemon/src/rpc_info.rs | 47 | ||||
| -rw-r--r-- | talpid_ipc/src/http_ipc/connection_info.rs | 43 |
3 files changed, 58 insertions, 43 deletions
diff --git a/mullvad_daemon/src/main.rs b/mullvad_daemon/src/main.rs index fa1db7765c..1177e05bd7 100644 --- a/mullvad_daemon/src/main.rs +++ b/mullvad_daemon/src/main.rs @@ -22,6 +22,7 @@ extern crate talpid_ipc; mod management_interface; mod states; +mod rpc_info; use management_interface::{ManagementInterfaceServer, TunnelCommand}; use states::{SecurityState, TargetState}; @@ -151,6 +152,8 @@ impl Daemon { "Mullvad management interface listening on {}", server.address() ); + rpc_info::write(server.address()).chain_err(|| ErrorKind::ManagementInterfaceError( + "Failed to write RPC address to file"))?; Ok(server) } @@ -309,6 +312,14 @@ impl Daemon { } } +impl Drop for Daemon { + fn drop(self: &mut Daemon) { + if let Err(e) = rpc_info::remove() { + log_error("Unable to clean up rpc address file", e); + } + } +} + fn log_error<E>(msg: &str, error: E) where E: error_chain::ChainedError diff --git a/mullvad_daemon/src/rpc_info.rs b/mullvad_daemon/src/rpc_info.rs new file mode 100644 index 0000000000..f1dae58506 --- /dev/null +++ b/mullvad_daemon/src/rpc_info.rs @@ -0,0 +1,47 @@ +use std::fs::{self, File, OpenOptions}; +use std::io::{self, Write}; +use std::path::{Path, PathBuf}; + +error_chain! { + errors { + WriteFailed(path: PathBuf) { + description("Failed to write RCP address to file") + display("Failed to write RPC address to {}", path.to_string_lossy()) + } + RemoveFailed(path: PathBuf) { + description("Failed to remove file") + display("Failed to remove {}", path.to_string_lossy()) + } + } +} + +lazy_static! { + /// The path to the file where we write the RPC address + static ref RPC_ADDRESS_FILE_PATH: &'static Path = Path::new("./.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) + .and_then(|mut file| file.write_all(rpc_address.as_bytes())) + .chain_err(|| ErrorKind::WriteFailed(RPC_ADDRESS_FILE_PATH.to_owned()))?; + + debug!( + "Wrote RPC address to {}", + RPC_ADDRESS_FILE_PATH.to_string_lossy() + ); + Ok(()) +} + +pub fn remove() -> Result<()> { + fs::remove_file(*RPC_ADDRESS_FILE_PATH) + .chain_err(|| ErrorKind::RemoveFailed(RPC_ADDRESS_FILE_PATH.to_owned())) +} + +fn open_file(path: &Path) -> io::Result<File> { + OpenOptions::new() + .write(true) + .truncate(true) + .create(true) + .open(path) +} diff --git a/talpid_ipc/src/http_ipc/connection_info.rs b/talpid_ipc/src/http_ipc/connection_info.rs deleted file mode 100644 index 1c4b3fac32..0000000000 --- a/talpid_ipc/src/http_ipc/connection_info.rs +++ /dev/null @@ -1,43 +0,0 @@ -use std::fs::{File, OpenOptions}; -use std::io::Write; -use std::path::PathBuf; - -/// The path to the file where we write the connection infp -const IPC_CONNECTION_INFO_FILE: &'static str = "./.ipc_connection_info"; - -error_chain! { - errors { - OpenFileFailed(file_name: PathBuf) { - description("Could not open file") - display("Could not open {}", file_name.to_string_lossy()) - } - WriteConnectionInfoFailed(file_name: PathBuf) { - description("Could not write connection info file") - display("Could not write to {}", file_name.to_string_lossy()) - } - } -} - -pub fn write(connection_info: &str) -> Result<()> { - let file_location = PathBuf::from(IPC_CONNECTION_INFO_FILE); - let mut file = open_file(&file_location)?; - let res = file.write_all(connection_info.as_bytes()) - .chain_err(|| ErrorKind::WriteConnectionInfoFailed(file_location.clone()),); - - debug!( - "Wrote IPC connection info ({}) to {}", - connection_info, - file_location.to_string_lossy() - ); - - res -} - -fn open_file(file_name: &PathBuf) -> Result<File> { - OpenOptions::new() - .write(true) - .truncate(true) - .create(true) - .open(file_name) - .chain_err(|| ErrorKind::OpenFileFailed(file_name.to_owned())) -} |
