diff options
| author | David Lönnhager <david.l@mullvad.net> | 2022-01-19 13:33:29 +0100 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2022-03-14 12:08:49 +0100 |
| commit | 55985af2f0944760b63c703242d02aa80d034ead (patch) | |
| tree | 5baeddad8a64cbda121602111f4d2e68ef97720f | |
| parent | 4147be42fb83495e2035d232b2a4a36d926c1a9d (diff) | |
| download | mullvadvpn-55985af2f0944760b63c703242d02aa80d034ead.tar.xz mullvadvpn-55985af2f0944760b63c703242d02aa80d034ead.zip | |
Only log out when setting device if id differs
| -rw-r--r-- | mullvad-daemon/src/device.rs | 33 | ||||
| -rw-r--r-- | mullvad-types/src/device.rs | 4 |
2 files changed, 33 insertions, 4 deletions
diff --git a/mullvad-daemon/src/device.rs b/mullvad-daemon/src/device.rs index 585855504d..bc5069929a 100644 --- a/mullvad-daemon/src/device.rs +++ b/mullvad-daemon/src/device.rs @@ -190,16 +190,41 @@ impl AccountManager { } pub async fn set(&mut self, data: DeviceData) -> Result<(), Error> { - self.logout(); + self.stop_key_rotation(); + let (result_tx, result_rx) = oneshot::channel(); let _ = self .cache_update_tx .unbounded_send((Some(data.clone()), result_tx)); - { + + let old_data = { let mut inner = self.inner.lock().unwrap(); - inner.data.replace(data.clone()); + inner.data.replace(data.clone()) + }; + + if let Err(error) = flatten_result(result_rx.await.map_err(Error::DeviceUpdaterCancelled)) { + // Delete the device if an I/O error occurred + self.logout(); + return Err(error); + } + + if let Some(old_data) = old_data { + // Log out the previous device if the id differs + if !old_data.device.eq_id(&data.device) { + let service = self.device_service.clone(); + tokio::spawn(async move { + if let Err(error) = service + .remove_device_with_backoff(old_data.token, old_data.device.id) + .await + { + log::error!( + "{}", + error.display_chain_with_msg("Failed to remove a previous device") + ); + } + }); + } } - result_rx.await.map_err(Error::DeviceUpdaterCancelled)??; self.start_key_rotation(); Ok(()) } diff --git a/mullvad-types/src/device.rs b/mullvad-types/src/device.rs index 05df853e0c..4b0123e6a9 100644 --- a/mullvad-types/src/device.rs +++ b/mullvad-types/src/device.rs @@ -40,6 +40,10 @@ impl Device { .collect::<Vec<String>>() .join(" ") } + + pub fn eq_id(&self, other: &Device) -> bool { + self.id == other.id + } } /// Ports associated with a device. |
