diff options
| author | David Lönnhager <david.l@mullvad.net> | 2022-04-06 17:40:15 +0200 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2022-04-26 10:43:40 +0200 |
| commit | e4209f2613bff4d3d387ad7e524dca3b0c9fcf4d (patch) | |
| tree | e32455a2aba8af678e5b40e5c6af03f27c4b91b8 | |
| parent | 59524b645d6a363ec681b7d4758398c2d8c5d6bb (diff) | |
| download | mullvadvpn-e4209f2613bff4d3d387ad7e524dca3b0c9fcf4d.tar.xz mullvadvpn-e4209f2613bff4d3d387ad7e524dca3b0c9fcf4d.zip | |
Make relay selector not depend on the updater
| -rw-r--r-- | mullvad-daemon/src/lib.rs | 19 | ||||
| -rw-r--r-- | mullvad-relay-selector/src/lib.rs | 49 | ||||
| -rw-r--r-- | mullvad-relay-selector/src/updater.rs | 27 |
3 files changed, 35 insertions, 60 deletions
diff --git a/mullvad-daemon/src/lib.rs b/mullvad-daemon/src/lib.rs index dd53781821..97c87914c2 100644 --- a/mullvad-daemon/src/lib.rs +++ b/mullvad-daemon/src/lib.rs @@ -34,7 +34,10 @@ use mullvad_api::{ availability::ApiAvailabilityHandle, proxy::{ApiConnectionMode, ProxyConfig}, }; -use mullvad_relay_selector::{RelaySelector, RelaySelectorResult}; +use mullvad_relay_selector::{ + updater::{RelayListUpdater, RelayListUpdaterHandle}, + RelaySelector, RelaySelectorResult, +}; use mullvad_types::{ account::{AccountData, AccountToken, VoucherSubmission}, device::{Device, DeviceConfig, DeviceData, DeviceEvent, DeviceId, RemoveDeviceEvent}, @@ -593,6 +596,7 @@ pub struct Daemon<L: EventListener> { api_handle: mullvad_api::rest::MullvadRestHandle, version_updater_handle: version_check::VersionUpdaterHandle, relay_selector: RelaySelector, + relay_list_updater: RelayListUpdaterHandle, last_generated_relays: Option<LastSelectedRelays>, app_version_info: Option<AppVersionInfo>, shutdown_tasks: Vec<Pin<Box<dyn Future<Output = ()>>>>, @@ -754,12 +758,12 @@ where relay_list_listener.notify_relay_list(relay_list.clone()); }; - let relay_selector = RelaySelector::new( + let relay_selector = RelaySelector::new(&resource_dir, &cache_dir); + let mut relay_list_updater = RelayListUpdater::new( + relay_selector.clone(), api_handle.clone(), - on_relay_list_update, - &resource_dir, &cache_dir, - api_availability.clone(), + on_relay_list_update, ); let app_version_info = version_check::load_cache(&cache_dir).await; @@ -774,7 +778,7 @@ where tokio::spawn(version_updater.run()); // Attempt to download a fresh relay list - relay_selector.update().await; + relay_list_updater.update().await; let daemon = Daemon { tunnel_command_tx, @@ -796,6 +800,7 @@ where api_handle, version_updater_handle, relay_selector, + relay_list_updater, last_generated_relays: None, app_version_info, shutdown_tasks: vec![], @@ -1740,7 +1745,7 @@ where } async fn on_update_relay_locations(&mut self) { - self.relay_selector.update().await; + self.relay_list_updater.update().await; } fn on_login_account(&mut self, tx: ResponseTx<(), Error>, account_token: String) { diff --git a/mullvad-relay-selector/src/lib.rs b/mullvad-relay-selector/src/lib.rs index 081ecdced7..7375de9c26 100644 --- a/mullvad-relay-selector/src/lib.rs +++ b/mullvad-relay-selector/src/lib.rs @@ -3,7 +3,6 @@ use chrono::{DateTime, Local}; use ipnetwork::IpNetwork; -use mullvad_api::{availability::ApiAvailabilityHandle, rest::MullvadRestHandle}; use mullvad_types::{ endpoint::{MullvadEndpoint, MullvadWireguardEndpoint}, location::{Coordinates, Location}, @@ -31,13 +30,10 @@ use talpid_types::{ ErrorExt, }; -use self::{ - matcher::{RelayMatcher, TunnelMatcher, WireguardMatcher}, - updater::{RelayListUpdater, RelayListUpdaterHandle}, -}; +use self::matcher::{RelayMatcher, TunnelMatcher, WireguardMatcher}; mod matcher; -mod updater; +pub mod updater; const DATE_TIME_FORMAT_STR: &str = "%Y-%m-%d %H:%M:%S%.3f"; const RELAYS_FILENAME: &str = "relays.json"; @@ -205,21 +201,14 @@ impl ParsedRelays { } } +#[derive(Clone)] pub struct RelaySelector { parsed_relays: Arc<Mutex<ParsedRelays>>, - updater: Option<RelayListUpdaterHandle>, } impl RelaySelector { - /// Returns a new `RelaySelector` backed by relays cached on disk. Use the `update` method - /// to refresh the relay list from the internet. - pub fn new( - api_handle: MullvadRestHandle, - on_update: impl Fn(&RelayList) + Send + 'static, - resource_dir: &Path, - cache_dir: &Path, - api_availability: ApiAvailabilityHandle, - ) -> Self { + /// Returns a new `RelaySelector` backed by relays cached on disk. + pub fn new(resource_dir: &Path, cache_dir: &Path) -> Self { let cache_path = cache_dir.join(RELAYS_FILENAME); let resource_path = resource_dir.join(RELAYS_FILENAME); let unsynchronized_parsed_relays = Self::read_relays_from_disk(&cache_path, &resource_path) @@ -238,32 +227,7 @@ impl RelaySelector { ); let parsed_relays = Arc::new(Mutex::new(unsynchronized_parsed_relays)); - let updater = RelayListUpdater::new( - api_handle, - cache_path, - parsed_relays.clone(), - Box::new(on_update), - api_availability, - ); - - RelaySelector { - parsed_relays, - updater: Some(updater), - } - } - - /// Download the newest relay list. - pub async fn update(&self) { - if let Some(mut updater) = self.updater.clone() { - if let Err(err) = updater.update_relay_list().await { - log::error!( - "{}", - err.display_chain_with_msg( - "Unable to send update command to relay list updater" - ) - ); - } - } + RelaySelector { parsed_relays } } /// Returns all countries and cities. The cities in the object returned does not have any @@ -1237,7 +1201,6 @@ mod test { RELAYS.clone(), SystemTime::now(), ))), - updater: None, } } diff --git a/mullvad-relay-selector/src/updater.rs b/mullvad-relay-selector/src/updater.rs index 35f431125a..fe5088704d 100644 --- a/mullvad-relay-selector/src/updater.rs +++ b/mullvad-relay-selector/src/updater.rs @@ -32,11 +32,18 @@ pub struct RelayListUpdaterHandle { } impl RelayListUpdaterHandle { - pub async fn update_relay_list(&mut self) -> Result<(), Error> { - self.tx + pub async fn update(&mut self) { + if let Err(error) = self + .tx .send(()) .await .map_err(|_| Error::DownloaderShutDown) + { + log::error!( + "{}", + error.display_chain_with_msg("Unable to send update command to relay list updater") + ); + } } } @@ -50,20 +57,20 @@ pub struct RelayListUpdater { } impl RelayListUpdater { - pub(super) fn new( + pub fn new( + selector: super::RelaySelector, api_handle: MullvadRestHandle, - cache_path: PathBuf, - parsed_relays: Arc<Mutex<ParsedRelays>>, - on_update: Box<dyn Fn(&RelayList) + Send + 'static>, - api_availability: ApiAvailabilityHandle, + cache_dir: &Path, + on_update: impl Fn(&RelayList) + Send + 'static, ) -> RelayListUpdaterHandle { let (tx, cmd_rx) = mpsc::channel(1); + let api_availability = api_handle.availability.clone(); let api_client = RelayListProxy::new(api_handle); let updater = RelayListUpdater { api_client, - cache_path, - parsed_relays, - on_update, + cache_path: cache_dir.join(super::RELAYS_FILENAME), + parsed_relays: selector.parsed_relays.clone(), + on_update: Box::new(on_update), last_check: UNIX_EPOCH, api_availability, }; |
