diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2018-05-17 02:43:06 +0200 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2018-05-18 15:28:07 +0200 |
| commit | 2f6ff40339c506620efbacb45ba648351467057b (patch) | |
| tree | 98135ec089f027600497bd794638c8d9183ae450 | |
| parent | c72ec78af15197df20265ee7fda160dd8a73b85a (diff) | |
| download | mullvadvpn-2f6ff40339c506620efbacb45ba648351467057b.tar.xz mullvadvpn-2f6ff40339c506620efbacb45ba648351467057b.zip | |
Make AccountHistory take cache_dir as argument
| -rw-r--r-- | mullvad-daemon/src/account_history.rs | 39 | ||||
| -rw-r--r-- | mullvad-daemon/src/main.rs | 10 | ||||
| -rw-r--r-- | mullvad-daemon/src/management_interface.rs | 19 |
3 files changed, 36 insertions, 32 deletions
diff --git a/mullvad-daemon/src/account_history.rs b/mullvad-daemon/src/account_history.rs index b9ac4908e3..d9047ad5f2 100644 --- a/mullvad-daemon/src/account_history.rs +++ b/mullvad-daemon/src/account_history.rs @@ -2,15 +2,12 @@ extern crate serde_json; use std::fs::File; use std::io; -use std::path::PathBuf; +use std::path::{PathBuf, Path}; use mullvad_types::account::AccountToken; error_chain! { errors { - DirectoryError { - description("Unable to create account history directory for program") - } ReadError(path: PathBuf) { description("Unable to read account history file") display("Unable to read account history from {}", path.display()) @@ -36,12 +33,15 @@ pub struct AccountHistory { impl AccountHistory { /// Loads account history from file. If no file is present it returns the defaults. - pub fn load() -> Result<AccountHistory> { - let history_path = Self::get_path()?; + pub fn load(cache_dir: &Path) -> Result<AccountHistory> { + let history_path = cache_dir.join(ACCOUNT_HISTORY_FILE); match File::open(&history_path) { - Ok(mut file) => { - info!("Loading account history from {}", history_path.display()); - Self::parse(&mut file) + Ok(file) => { + info!( + "Loading account history from {}", + history_path.display() + ); + Self::parse(&mut io::BufReader::new(file)) } Err(ref e) if e.kind() == io::ErrorKind::NotFound => { info!( @@ -59,7 +59,7 @@ impl AccountHistory { } /// Add account token to the account history removing duplicate entries - pub fn add_account_token(&mut self, account_token: AccountToken) -> Result<()> { + pub fn add_account_token(&mut self, account_token: AccountToken, cache_dir: &Path) -> Result<()> { self.accounts .retain(|existing_token| existing_token != &account_token); self.accounts.push(account_token); @@ -71,32 +71,27 @@ impl AccountHistory { .split_off(num_accounts - ACCOUNT_HISTORY_LIMIT); } - self.save() + self.save(cache_dir) } /// Remove account token from the account history - pub fn remove_account_token(&mut self, account_token: AccountToken) -> Result<()> { + pub fn remove_account_token(&mut self, account_token: AccountToken, cache_dir: &Path) -> Result<()> { self.accounts .retain(|existing_token| existing_token != &account_token); - self.save() + self.save(cache_dir) } /// Serializes the account history and saves it to the file it was loaded from. - fn save(&self) -> Result<()> { - let path = Self::get_path()?; + fn save(&self, cache_dir: &Path) -> Result<()> { + let path = cache_dir.join(ACCOUNT_HISTORY_FILE); debug!("Writing account history to {}", path.display()); let file = File::create(&path).chain_err(|| ErrorKind::WriteError(path.clone()))?; - serde_json::to_writer_pretty(file, self).chain_err(|| ErrorKind::WriteError(path)) + serde_json::to_writer_pretty(io::BufWriter::new(file), self).chain_err(|| ErrorKind::WriteError(path)) } - fn parse(file: &mut File) -> Result<AccountHistory> { + fn parse(file: &mut impl io::Read) -> Result<AccountHistory> { serde_json::from_reader(file).chain_err(|| ErrorKind::ParseError) } - - fn get_path() -> Result<PathBuf> { - let dir = ::cache::get_cache_dir().chain_err(|| ErrorKind::DirectoryError)?; - Ok(dir.join(ACCOUNT_HISTORY_FILE)) - } } diff --git a/mullvad-daemon/src/main.rs b/mullvad-daemon/src/main.rs index 87e88498a3..463b4173fe 100644 --- a/mullvad-daemon/src/main.rs +++ b/mullvad-daemon/src/main.rs @@ -241,7 +241,9 @@ impl Daemon { Self::create_relay_selector(rpc_handle.clone(), &resource_dir, &cache_dir); let (tx, rx) = mpsc::channel(); - let management_interface_broadcaster = Self::start_management_interface(tx.clone())?; + let management_interface_broadcaster = + Self::start_management_interface(tx.clone(), cache_dir)?; + let state = TunnelState::NotRunning; let target_state = TargetState::Unsecured; Ok(Daemon { @@ -291,9 +293,10 @@ impl Daemon { // Returns a handle that allows notifying all subscribers on events. fn start_management_interface( event_tx: mpsc::Sender<DaemonEvent>, + cache_dir: PathBuf, ) -> Result<management_interface::EventBroadcaster> { let multiplex_event_tx = IntoSender::from(event_tx.clone()); - let server = Self::start_management_interface_server(multiplex_event_tx)?; + let server = Self::start_management_interface_server(multiplex_event_tx, cache_dir)?; let event_broadcaster = server.event_broadcaster(); Self::spawn_management_interface_wait_thread(server, event_tx); Ok(event_broadcaster) @@ -301,10 +304,11 @@ impl Daemon { fn start_management_interface_server( event_tx: IntoSender<TunnelCommand, DaemonEvent>, + cache_dir: PathBuf, ) -> Result<ManagementInterfaceServer> { let shared_secret = uuid::Uuid::new_v4().to_string(); - let server = ManagementInterfaceServer::start(event_tx, shared_secret.clone()) + let server = ManagementInterfaceServer::start(event_tx, shared_secret.clone(), cache_dir) .chain_err(|| ErrorKind::ManagementInterfaceError("Failed to start server"))?; info!( "Mullvad management interface listening on {}", diff --git a/mullvad-daemon/src/management_interface.rs b/mullvad-daemon/src/management_interface.rs index df777d5ea8..bee8f1e0ae 100644 --- a/mullvad-daemon/src/management_interface.rs +++ b/mullvad-daemon/src/management_interface.rs @@ -20,8 +20,10 @@ use serde; use std::collections::hash_map::Entry; use std::collections::HashMap; +use std::path::PathBuf; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex, RwLock}; + use talpid_core::mpsc::IntoSender; use talpid_ipc; use talpid_types::net::TunnelOptions; @@ -210,11 +212,12 @@ impl ManagementInterfaceServer { pub fn start<T>( tunnel_tx: IntoSender<TunnelCommand, T>, shared_secret: String, + cache_dir: PathBuf, ) -> talpid_ipc::Result<Self> where T: From<TunnelCommand> + 'static + Send, { - let rpc = ManagementInterface::new(tunnel_tx, shared_secret); + let rpc = ManagementInterface::new(tunnel_tx, shared_secret, cache_dir); let subscriptions = rpc.subscriptions.clone(); let mut io = PubSubHandler::default(); @@ -284,14 +287,16 @@ struct ManagementInterface<T: From<TunnelCommand> + 'static + Send> { subscriptions: Arc<ActiveSubscriptions>, tx: Mutex<IntoSender<TunnelCommand, T>>, shared_secret: String, + cache_dir: PathBuf, } impl<T: From<TunnelCommand> + 'static + Send> ManagementInterface<T> { - pub fn new(tx: IntoSender<TunnelCommand, T>, shared_secret: String) -> Self { + pub fn new(tx: IntoSender<TunnelCommand, T>, shared_secret: String, cache_dir: PathBuf) -> Self { ManagementInterface { subscriptions: Default::default(), tx: Mutex::new(tx), shared_secret, + cache_dir, } } @@ -439,8 +444,8 @@ impl<T: From<TunnelCommand> + 'static + Send> ManagementInterfaceApi for Managem .and_then(|_| rx.map_err(|_| Error::internal_error())); if let Some(new_account_token) = account_token { - if let Err(e) = AccountHistory::load().and_then(|mut account_history| { - account_history.add_account_token(new_account_token) + if let Err(e) = AccountHistory::load(&self.cache_dir).and_then(|mut account_history| { + account_history.add_account_token(new_account_token, &self.cache_dir) }) { error!( "Unable to add an account into the account history: {}", @@ -556,7 +561,7 @@ impl<T: From<TunnelCommand> + 'static + Send> ManagementInterfaceApi for Managem trace!("get_account_history"); try_future!(self.check_auth(&meta)); Box::new(future::result( - AccountHistory::load() + AccountHistory::load(&self.cache_dir) .map(|account_history| account_history.get_accounts().to_vec()) .map_err(|error| { error!("Unable to get account history: {}", error.display_chain()); @@ -573,8 +578,8 @@ impl<T: From<TunnelCommand> + 'static + Send> ManagementInterfaceApi for Managem trace!("remove_account_from_history"); try_future!(self.check_auth(&meta)); Box::new(future::result( - AccountHistory::load() - .and_then(|mut account_history| account_history.remove_account_token(account_token)) + AccountHistory::load(&self.cache_dir) + .and_then(|mut history| history.remove_account_token(account_token, &self.cache_dir)) .map_err(|error| { error!( "Unable to remove account from history: {}", |
