diff options
| author | David Lönnhager <david.l@mullvad.net> | 2021-01-22 13:10:09 +0100 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2021-01-22 13:10:09 +0100 |
| commit | ca739e5738c53eef48adbd4b5a9fb3078a9a76ca (patch) | |
| tree | 1b2ac9ca55c6eaaab26a55a36875fa4b1a85164d | |
| parent | 53cc20e5710307db90e9ca64b0e2912019f90627 (diff) | |
| parent | 60451971666c3d44bb049c7689765440ae2b14fb (diff) | |
| download | mullvadvpn-ca739e5738c53eef48adbd4b5a9fb3078a9a76ca.tar.xz mullvadvpn-ca739e5738c53eef48adbd4b5a9fb3078a9a76ca.zip | |
Merge branch 'improve-default-version-cache'
| -rw-r--r-- | CHANGELOG.md | 1 | ||||
| -rw-r--r-- | mullvad-daemon/src/lib.rs | 16 | ||||
| -rw-r--r-- | mullvad-daemon/src/management_interface.rs | 5 | ||||
| -rw-r--r-- | mullvad-daemon/src/version_check.rs | 73 | ||||
| -rw-r--r-- | mullvad-jni/src/daemon_interface.rs | 4 |
5 files changed, 70 insertions, 29 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index fcdb7b37ca..78dacd3c01 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,7 @@ Line wrap the file at 100 chars. Th ### Fixed - Fix Turkish translations for on/off in the bridge settings. They were inverted, so it was confusing to change the setting. +- Stop returning bogus version information when there is no version cache. #### Linux - Fix missing app window icon in Xfce. diff --git a/mullvad-daemon/src/lib.rs b/mullvad-daemon/src/lib.rs index ae76b2eafa..abf5d9962a 100644 --- a/mullvad-daemon/src/lib.rs +++ b/mullvad-daemon/src/lib.rs @@ -208,7 +208,7 @@ pub enum DaemonCommand { /// Verify if the currently set wireguard key is valid. VerifyWireguardKey(oneshot::Sender<bool>), /// Get information about the currently running and latest app versions - GetVersionInfo(oneshot::Sender<AppVersionInfo>), + GetVersionInfo(oneshot::Sender<Option<AppVersionInfo>>), /// Get current version of the app GetCurrentVersion(oneshot::Sender<AppVersion>), /// Remove settings and clear the cache @@ -467,7 +467,7 @@ pub struct Daemon<L: EventListener> { relay_selector: relays::RelaySelector, last_generated_relay: Option<Relay>, last_generated_bridge_relay: Option<Relay>, - app_version_info: AppVersionInfo, + app_version_info: Option<AppVersionInfo>, shutdown_callbacks: Vec<Box<dyn FnOnce()>>, /// oneshot channel that completes once the tunnel state machine has been shut down tunnel_state_machine_shutdown_signal: oneshot::Receiver<()>, @@ -1098,7 +1098,7 @@ where GenerateWireguardKey(tx) => self.on_generate_wireguard_key(tx).await, GetWireguardKey(tx) => self.on_get_wireguard_key(tx).await, VerifyWireguardKey(tx) => self.on_verify_wireguard_key(tx).await, - GetVersionInfo(tx) => self.on_get_version_info(tx), + GetVersionInfo(tx) => self.on_get_version_info(tx).await, GetCurrentVersion(tx) => self.on_get_current_version(tx), #[cfg(not(target_os = "android"))] FactoryReset(tx) => self.on_factory_reset(tx).await, @@ -1221,7 +1221,7 @@ where } fn handle_new_app_version_info(&mut self, app_version_info: AppVersionInfo) { - self.app_version_info = app_version_info.clone(); + self.app_version_info = Some(app_version_info.clone()); self.event_listener.notify_app_version(app_version_info); } @@ -1468,7 +1468,13 @@ where } } - fn on_get_version_info(&mut self, tx: oneshot::Sender<AppVersionInfo>) { + async fn on_get_version_info(&mut self, tx: oneshot::Sender<Option<AppVersionInfo>>) { + if self.app_version_info.is_none() { + log::debug!("No version cache found. Fetching new info"); + let mut handle = self.version_updater_handle.clone(); + handle.run_version_check().await; + } + Self::oneshot_send( tx, self.app_version_info.clone(), diff --git a/mullvad-daemon/src/management_interface.rs b/mullvad-daemon/src/management_interface.rs index 4fe5b3aee1..ccde44ba8f 100644 --- a/mullvad-daemon/src/management_interface.rs +++ b/mullvad-daemon/src/management_interface.rs @@ -150,8 +150,9 @@ impl ManagementService for ManagementServiceImpl { let (tx, rx) = oneshot::channel(); self.send_command_to_daemon(DaemonCommand::GetVersionInfo(tx))?; - rx.await - .map_err(|_| Status::internal("internal error")) + let version_info = rx.await.map_err(|_| Status::internal("internal error"))?; + version_info + .ok_or(Status::not_found("no version cache")) .map(|version_info| convert_version_info(&version_info)) .map(Response::new) } diff --git a/mullvad-daemon/src/version_check.rs b/mullvad-daemon/src/version_check.rs index 8f52f5694a..acb2239d37 100644 --- a/mullvad-daemon/src/version_check.rs +++ b/mullvad-daemon/src/version_check.rs @@ -85,24 +85,47 @@ pub(crate) struct VersionUpdater { version_proxy: AppVersionProxy, cache_path: PathBuf, update_sender: DaemonEventSender<AppVersionInfo>, - last_app_version_info: AppVersionInfo, + last_app_version_info: Option<AppVersionInfo>, platform_version: String, next_update_time: Instant, show_beta_releases: bool, - rx: Option<mpsc::Receiver<bool>>, + rx: Option<mpsc::Receiver<VersionUpdaterCommand>>, } #[derive(Clone)] pub(crate) struct VersionUpdaterHandle { - tx: mpsc::Sender<bool>, + tx: mpsc::Sender<VersionUpdaterCommand>, +} + +enum VersionUpdaterCommand { + SetShowBetaReleases(bool), + RunVersionCheck, } impl VersionUpdaterHandle { pub async fn set_show_beta_releases(&mut self, show_beta_releases: bool) { - if self.tx.send(show_beta_releases).await.is_err() { + if self + .tx + .send(VersionUpdaterCommand::SetShowBetaReleases( + show_beta_releases, + )) + .await + .is_err() + { log::error!("Version updater already down, can't send new `show_beta_releases` state"); } } + + pub async fn run_version_check(&mut self) { + if self + .tx + .send(VersionUpdaterCommand::RunVersionCheck) + .await + .is_err() + { + log::error!("Version updater already down"); + } + } } impl VersionUpdater { @@ -110,7 +133,7 @@ impl VersionUpdater { mut rpc_handle: MullvadRestHandle, cache_dir: PathBuf, update_sender: DaemonEventSender<AppVersionInfo>, - last_app_version_info: AppVersionInfo, + last_app_version_info: Option<AppVersionInfo>, show_beta_releases: bool, ) -> (Self, VersionUpdaterHandle) { rpc_handle.factory.timeout = DOWNLOAD_TIMEOUT; @@ -158,6 +181,13 @@ impl VersionUpdater { } async fn write_cache(&self) -> Result<(), Error> { + let last_app_version_info = match self.last_app_version_info.as_ref() { + Some(version_info) => version_info, + None => { + log::debug!("The version cache is empty -- not writing"); + return Ok(()); + } + }; log::debug!( "Writing version check cache to {}", self.cache_path.display() @@ -165,7 +195,7 @@ impl VersionUpdater { let mut file = File::create(&self.cache_path) .await .map_err(Error::WriteVersionCache)?; - let cached_app_version = CachedAppVersionInfo::from(self.last_app_version_info.clone()); + let cached_app_version = CachedAppVersionInfo::from(last_app_version_info.clone()); let mut buf = serde_json::to_vec_pretty(&cached_app_version).map_err(Error::Serialize)?; let mut read_buf: &[u8] = buf.as_mut(); @@ -237,15 +267,22 @@ impl VersionUpdater { loop { futures::select! { - show_beta_releases = rx.next() => { - match show_beta_releases { - Some(show_beta_releases ) => { + command = rx.next() => { + match command { + Some(VersionUpdaterCommand::SetShowBetaReleases(show_beta_releases)) => { self.show_beta_releases = show_beta_releases; - }, + } + Some(VersionUpdaterCommand::RunVersionCheck) => { + if self.update_sender.is_closed() { + return; + } + let download_future = self.create_update_future().fuse(); + version_check = download_future; + } // time to shut down None => { return; - }, + } } }, @@ -277,7 +314,7 @@ impl VersionUpdater { return; } - self.last_app_version_info = new_version_info; + self.last_app_version_info = Some(new_version_info); if let Err(err) = self.write_cache().await { log::error!("Failed to save version cache to disk: {}", err); @@ -309,21 +346,15 @@ fn try_load_cache(cache_dir: &Path) -> Result<AppVersionInfo, Error> { } } -pub fn load_cache(cache_dir: &Path) -> AppVersionInfo { +pub fn load_cache(cache_dir: &Path) -> Option<AppVersionInfo> { match try_load_cache(cache_dir) { - Ok(app_version_info) => app_version_info, + Ok(app_version_info) => Some(app_version_info), Err(error) => { log::warn!( "{}", error.display_chain_with_msg("Unable to load cached version info") ); - // If we don't have a cache, start out with sane defaults. - AppVersionInfo { - supported: !*IS_DEV_BUILD, - latest_stable: PRODUCT_VERSION.to_owned(), - latest_beta: PRODUCT_VERSION.to_owned(), - suggested_upgrade: None, - } + None } } } diff --git a/mullvad-jni/src/daemon_interface.rs b/mullvad-jni/src/daemon_interface.rs index d3ab959b33..4b54ca4c58 100644 --- a/mullvad-jni/src/daemon_interface.rs +++ b/mullvad-jni/src/daemon_interface.rs @@ -144,7 +144,9 @@ impl DaemonInterface { self.send_command(DaemonCommand::GetVersionInfo(tx))?; - block_on(rx).map_err(|_| Error::NoResponse) + block_on(rx) + .map_err(|_| Error::NoResponse)? + .ok_or(Error::NoResponse) } pub fn reconnect(&self) -> Result<()> { |
