diff options
Diffstat (limited to 'mullvad_daemon/src')
| -rw-r--r-- | mullvad_daemon/src/main.rs | 11 | ||||
| -rw-r--r-- | mullvad_daemon/src/rpc_info.rs | 47 |
2 files changed, 58 insertions, 0 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) +} |
