summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDavid Lönnhager <david.l@mullvad.net>2021-10-25 16:03:32 +0200
committerDavid Lönnhager <david.l@mullvad.net>2021-10-26 16:00:17 +0200
commit706e7f624869715dec4851105d80f1bb10e95b7a (patch)
tree179b7464b30667393959229d3f4a208fb5a6e9c0
parentdbe2b5f587fcd4f0928b5f8e126966c222145a8c (diff)
downloadmullvadvpn-706e7f624869715dec4851105d80f1bb10e95b7a.tar.xz
mullvadvpn-706e7f624869715dec4851105d80f1bb10e95b7a.zip
Simplify and remove unused account history code
-rw-r--r--mullvad-daemon/src/account_history.rs97
-rw-r--r--mullvad-daemon/src/lib.rs7
2 files changed, 25 insertions, 79 deletions
diff --git a/mullvad-daemon/src/account_history.rs b/mullvad-daemon/src/account_history.rs
index 397351a31d..7effa94741 100644
--- a/mullvad-daemon/src/account_history.rs
+++ b/mullvad-daemon/src/account_history.rs
@@ -1,5 +1,4 @@
-use crate::settings::SettingsPersister;
-use mullvad_types::{account::AccountToken, wireguard::WireguardData};
+use mullvad_types::account::AccountToken;
use regex::Regex;
use std::{
fs,
@@ -7,7 +6,6 @@ use std::{
path::Path,
sync::{Arc, Mutex},
};
-use talpid_types::ErrorExt;
pub type Result<T> = std::result::Result<T, Error>;
@@ -42,7 +40,7 @@ lazy_static::lazy_static! {
impl AccountHistory {
pub async fn new(
settings_dir: &Path,
- settings: &mut SettingsPersister,
+ current_token: Option<AccountToken>,
) -> Result<AccountHistory> {
let mut options = fs::OpenOptions::new();
#[cfg(unix)]
@@ -56,52 +54,28 @@ impl AccountHistory {
// a share mode of zero ensures exclusive access to the file to *this* process
options.share_mode(0);
}
- let path = settings_dir.join(ACCOUNT_HISTORY_FILE);
- let (file, token) = if path.is_file() {
- log::info!("Opening account history file in {}", path.display());
- let mut reader = options
- .write(true)
- .read(true)
- .open(path)
- .map(io::BufReader::new)
- .map_err(Error::Read)?;
- let mut buffer = String::new();
- let token: Option<AccountToken> = match reader.read_to_string(&mut buffer) {
- Ok(0) => None,
- Ok(_) if ACCOUNT_REGEX.is_match(&buffer) => Some(buffer),
- Ok(_) | Err(_) => {
- log::warn!("Failed to parse account history. Trying old formats",);
- match Self::try_format_v2(&mut reader)? {
- Some((token, migrated_data)) => {
- if let Err(error) = settings.set_wireguard(migrated_data).await {
- log::error!(
- "{}",
- error.display_chain_with_msg(
- "Failed to migrate WireGuard key from account history"
- )
- );
- }
- Some(token)
- }
- None => Self::try_format_v1(&mut reader)?,
- }
- }
- };
+ let path = settings_dir.join(ACCOUNT_HISTORY_FILE);
+ log::info!("Opening account history file in {}", path.display());
+ let mut reader = options
+ .write(true)
+ .create(true)
+ .read(true)
+ .open(path)
+ .map(io::BufReader::new)
+ .map_err(Error::Read)?;
- (reader.into_inner(), token)
- } else {
- log::info!("Creating account history file in {}", path.display());
- (
- options
- .write(true)
- .create(true)
- .open(path)
- .map_err(Error::Read)?,
- settings.get_account_token(),
- )
+ let mut buffer = String::new();
+ let token: Option<AccountToken> = match reader.read_to_string(&mut buffer) {
+ Ok(_) if ACCOUNT_REGEX.is_match(&buffer) => Some(buffer),
+ Ok(0) => current_token,
+ Ok(_) | Err(_) => {
+ log::warn!("Failed to parse account history");
+ current_token
+ }
};
- let file = io::BufWriter::new(file);
+
+ let file = io::BufWriter::new(reader.into_inner());
let mut history = AccountHistory {
file: Arc::new(Mutex::new(file)),
token,
@@ -112,35 +86,6 @@ impl AccountHistory {
Ok(history)
}
- fn try_format_v1(reader: &mut io::BufReader<fs::File>) -> Result<Option<AccountToken>> {
- #[derive(Deserialize)]
- struct OldFormat {
- accounts: Vec<AccountToken>,
- }
- reader.seek(io::SeekFrom::Start(0)).map_err(Error::Read)?;
- Ok(serde_json::from_reader(reader)
- .map(|old_format: OldFormat| old_format.accounts.first().cloned())
- .unwrap_or_else(|_| None))
- }
-
- fn try_format_v2(
- reader: &mut io::BufReader<fs::File>,
- ) -> Result<Option<(AccountToken, Option<WireguardData>)>> {
- #[derive(Serialize, Deserialize, Clone, Debug)]
- pub struct AccountEntry {
- pub account: AccountToken,
- pub wireguard: Option<WireguardData>,
- }
- reader.seek(io::SeekFrom::Start(0)).map_err(Error::Read)?;
- Ok(serde_json::from_reader(reader)
- .map(|entries: Vec<AccountEntry>| {
- entries
- .first()
- .map(|entry| (entry.account.clone(), entry.wireguard.clone()))
- })
- .unwrap_or_else(|_| None))
- }
-
/// Gets the account token in the history
pub fn get(&self) -> Option<AccountToken> {
self.token.clone()
diff --git a/mullvad-daemon/src/lib.rs b/mullvad-daemon/src/lib.rs
index c4785c1de8..7597f01020 100644
--- a/mullvad-daemon/src/lib.rs
+++ b/mullvad-daemon/src/lib.rs
@@ -625,9 +625,10 @@ where
settings.show_beta_releases,
);
tokio::spawn(version_updater.run());
- let account_history = account_history::AccountHistory::new(&settings_dir, &mut settings)
- .await
- .map_err(Error::LoadAccountHistory)?;
+ let account_history =
+ account_history::AccountHistory::new(&settings_dir, settings.get_account_token())
+ .await
+ .map_err(Error::LoadAccountHistory)?;
// Restore the tunnel to a previous state
let target_cache = cache_dir.join(TARGET_START_STATE_FILE);