diff options
Diffstat (limited to 'mullvad-api/src')
| -rw-r--r-- | mullvad-api/src/address_cache.rs | 2 | ||||
| -rw-r--r-- | mullvad-api/src/lib.rs | 20 | ||||
| -rw-r--r-- | mullvad-api/src/rest.rs | 61 |
3 files changed, 10 insertions, 73 deletions
diff --git a/mullvad-api/src/address_cache.rs b/mullvad-api/src/address_cache.rs index f164e15f09..aa9a155bec 100644 --- a/mullvad-api/src/address_cache.rs +++ b/mullvad-api/src/address_cache.rs @@ -1,3 +1,5 @@ +//! This module keeps track of the last known good API IP address and reads and stores it on disk. + use super::API; use std::{io, net::SocketAddr, path::Path, sync::Arc}; use tokio::{ diff --git a/mullvad-api/src/lib.rs b/mullvad-api/src/lib.rs index 6114bec90a..e78535dfc0 100644 --- a/mullvad-api/src/lib.rs +++ b/mullvad-api/src/lib.rs @@ -308,7 +308,7 @@ impl ApiEndpoint { /// A type that helps with the creation of API connections. pub struct Runtime { handle: tokio::runtime::Handle, - pub address_cache: AddressCache, + address_cache: AddressCache, api_availability: availability::ApiAvailability, #[cfg(target_os = "android")] socket_bypass_tx: Option<mpsc::Sender<SocketBypassRequest>>, @@ -437,12 +437,7 @@ impl Runtime { let token_store = access::AccessTokenStore::new(service.clone()); let factory = rest::RequestFactory::new(API.host(), Some(token_store)); - rest::MullvadRestHandle::new( - service, - factory, - self.address_cache.clone(), - self.availability_handle(), - ) + rest::MullvadRestHandle::new(service, factory, self.availability_handle()) } /// This is only to be used in test code @@ -456,12 +451,7 @@ impl Runtime { let token_store = access::AccessTokenStore::new(service.clone()); let factory = rest::RequestFactory::new(hostname, Some(token_store)); - rest::MullvadRestHandle::new( - service, - factory, - self.address_cache.clone(), - self.availability_handle(), - ) + rest::MullvadRestHandle::new(service, factory, self.availability_handle()) } /// Returns a new request service handle @@ -481,6 +471,10 @@ impl Runtime { pub fn availability_handle(&self) -> ApiAvailabilityHandle { self.api_availability.handle() } + + pub fn address_cache(&self) -> &AddressCache { + &self.address_cache + } } #[derive(Clone)] diff --git a/mullvad-api/src/rest.rs b/mullvad-api/src/rest.rs index e4a1235177..0fd826abc4 100644 --- a/mullvad-api/src/rest.rs +++ b/mullvad-api/src/rest.rs @@ -30,10 +30,6 @@ pub use hyper::StatusCode; const USER_AGENT: &str = "mullvad-app"; -const API_IP_CHECK_INITIAL: Duration = Duration::from_secs(15 * 60); -const API_IP_CHECK_INTERVAL: Duration = Duration::from_secs(24 * 60 * 60); -const API_IP_CHECK_ERROR_INTERVAL: Duration = Duration::from_secs(15 * 60); - pub type Result<T> = std::result::Result<T, Error>; const DEFAULT_TIMEOUT: Duration = Duration::from_secs(10); @@ -616,68 +612,13 @@ impl MullvadRestHandle { pub(crate) fn new( service: RequestServiceHandle, factory: RequestFactory, - address_cache: AddressCache, availability: ApiAvailabilityHandle, ) -> Self { - let handle = Self { + Self { service, factory, availability, - }; - #[cfg(feature = "api-override")] - { - if crate::API.disable_address_cache { - return handle; - } } - handle.spawn_api_address_fetcher(address_cache); - handle - } - - fn spawn_api_address_fetcher(&self, address_cache: AddressCache) { - let handle = self.clone(); - let availability = self.availability.clone(); - - tokio::spawn(async move { - let api_proxy = crate::ApiProxy::new(handle); - let mut next_delay = API_IP_CHECK_INITIAL; - - loop { - talpid_time::sleep(next_delay).await; - - if let Err(error) = availability.wait_background().await { - log::error!("Failed while waiting for API: {}", error); - continue; - } - match api_proxy.clone().get_api_addrs().await { - Ok(new_addrs) => { - if let Some(addr) = new_addrs.first() { - log::debug!( - "Fetched new API address {:?}. Fetching again in {} hours", - addr, - API_IP_CHECK_INTERVAL.as_secs() / (60 * 60) - ); - if let Err(err) = address_cache.set_address(*addr).await { - log::error!("Failed to save newly updated API address: {}", err); - } - } else { - log::error!("API returned no API addresses"); - } - - next_delay = API_IP_CHECK_INTERVAL; - } - Err(err) => { - log::error!( - "Failed to fetch new API addresses: {}. Retrying in {} seconds", - err, - API_IP_CHECK_ERROR_INTERVAL.as_secs() - ); - - next_delay = API_IP_CHECK_ERROR_INTERVAL; - } - } - } - }); } pub fn service(&self) -> RequestServiceHandle { |
