summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDavid Lönnhager <david.l@mullvad.net>2021-06-16 09:49:14 +0200
committerDavid Lönnhager <david.l@mullvad.net>2021-06-18 10:28:49 +0200
commit335cc80d9347f43264765a3dd359eda83c782bc8 (patch)
treee8001a1b410fe90873127080515665fba8643f02
parent12b60e710aef1d166fc9b93d6143d22374f68fab (diff)
downloadmullvadvpn-335cc80d9347f43264765a3dd359eda83c782bc8.tar.xz
mullvadvpn-335cc80d9347f43264765a3dd359eda83c782bc8.zip
Migrate WireGuard key from account history
-rw-r--r--mullvad-daemon/src/account_history.rs29
-rw-r--r--mullvad-daemon/src/lib.rs7
2 files changed, 29 insertions, 7 deletions
diff --git a/mullvad-daemon/src/account_history.rs b/mullvad-daemon/src/account_history.rs
index 57d1330e3a..9db71c4000 100644
--- a/mullvad-daemon/src/account_history.rs
+++ b/mullvad-daemon/src/account_history.rs
@@ -1,3 +1,4 @@
+use crate::settings::SettingsPersister;
use mullvad_types::{account::AccountToken, wireguard::WireguardData};
use regex::Regex;
use std::{
@@ -39,7 +40,11 @@ lazy_static::lazy_static! {
impl AccountHistory {
- pub async fn new(cache_dir: &Path, settings_dir: &Path) -> Result<AccountHistory> {
+ pub async fn new(
+ cache_dir: &Path,
+ settings_dir: &Path,
+ settings: &mut SettingsPersister,
+ ) -> Result<AccountHistory> {
Self::migrate_from_old_file_location(cache_dir, settings_dir).await;
let mut options = fs::OpenOptions::new();
@@ -71,7 +76,17 @@ impl AccountHistory {
Ok(_) | Err(_) => {
log::warn!("Failed to parse account history. Trying old formats",);
match Self::try_format_v2(&mut reader)? {
- Some(token) => Some(token),
+ 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)?,
}
}
@@ -130,7 +145,9 @@ impl AccountHistory {
.unwrap_or_else(|_| None))
}
- fn try_format_v2(reader: &mut io::BufReader<fs::File>) -> Result<Option<AccountToken>> {
+ 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,
@@ -138,7 +155,11 @@ impl AccountHistory {
}
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()))
+ .map(|entries: Vec<AccountEntry>| {
+ entries
+ .first()
+ .map(|entry| (entry.account.clone(), entry.wireguard.clone()))
+ })
.unwrap_or_else(|_| None))
}
diff --git a/mullvad-daemon/src/lib.rs b/mullvad-daemon/src/lib.rs
index bccd1dea77..b78fa9f26d 100644
--- a/mullvad-daemon/src/lib.rs
+++ b/mullvad-daemon/src/lib.rs
@@ -582,9 +582,10 @@ where
settings.show_beta_releases,
);
tokio::spawn(version_updater.run());
- let account_history = account_history::AccountHistory::new(&cache_dir, &settings_dir)
- .await
- .map_err(Error::LoadAccountHistory)?;
+ let account_history =
+ account_history::AccountHistory::new(&cache_dir, &settings_dir, &mut settings)
+ .await
+ .map_err(Error::LoadAccountHistory)?;
// Restore the tunnel to a previous state
let target_cache = cache_dir.join(TARGET_START_STATE_FILE);