Skip to content

Commit

Permalink
Revert bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Serock3 committed Dec 7, 2023
1 parent ae5f8e3 commit dc12a62
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
20 changes: 10 additions & 10 deletions mullvad-daemon/src/geoip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::time::Duration;
use futures::join;
use mullvad_api::{
self,
rest::{Error, MullvadRestHandle, RequestServiceHandle},
availability::ApiAvailabilityHandle,
rest::{Error, RequestServiceHandle},
};
use mullvad_types::location::{AmIMullvad, GeoIpLocation};
use once_cell::sync::Lazy;
Expand Down Expand Up @@ -64,12 +65,11 @@ impl GeoIpHandler {
}
}

/// Send geographical location request to am.i.mullvad.net and send the response as an
/// [`InternalDaemonEvent::LocationEvent`].
pub fn send_geo_location_request(
&mut self,
use_ipv6: bool,
api_handle: MullvadRestHandle,
availability: ApiAvailabilityHandle,
rest_service: RequestServiceHandle,
daemon_event_sender: DaemonEventSender,
) {
// Increment request ID
Expand All @@ -78,7 +78,9 @@ impl GeoIpHandler {

self.abort_current_request();
self.request_handle = Some(tokio::spawn(async move {
if let Ok(merged_location) = get_geo_location_with_retry(api_handle, use_ipv6).await {
if let Ok(merged_location) =
get_geo_location_with_retry(use_ipv6, availability, rest_service).await
{
let _ = daemon_event_sender.send(InternalDaemonEvent::LocationEvent((
request_id_copy,
merged_location,
Expand All @@ -97,17 +99,15 @@ impl GeoIpHandler {

/// Fetch the current `GeoIpLocation` with retrys
pub async fn get_geo_location_with_retry(
api_handle: MullvadRestHandle,
use_ipv6: bool,
availability: ApiAvailabilityHandle,
rest_service: RequestServiceHandle,
) -> Result<GeoIpLocation, Error> {
log::debug!("Fetching GeoIpLocation");
let rest_service = api_handle.service();
retry_future(
move || send_location_request(rest_service.clone(), use_ipv6),
move |result| match result {
Err(error) if error.is_network_error() => {
!api_handle.availability.get_state().is_offline()
}
Err(error) if error.is_network_error() => !availability.get_state().is_offline(),
_ => false,
},
LOCATION_RETRY_STRATEGY,
Expand Down
17 changes: 11 additions & 6 deletions mullvad-daemon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ where
if *self.target_state == TargetState::Secured {
self.connect_tunnel();
} else {
self.fetch_am_i_mullvad()
self.fetch_am_i_mullvad().await
}

while let Some(event) = self.rx.next().await {
Expand Down Expand Up @@ -1031,15 +1031,15 @@ where

self.tunnel_state = tunnel_state.clone();
self.event_listener.notify_new_state(tunnel_state);
self.fetch_am_i_mullvad();
self.fetch_am_i_mullvad().await;
}

/// Get the geographical location from am.i.mullvad.net. When it arrives,
/// update the "Out IP" field of the front ends by sending a
/// [`InternalDaemonEvent::LocationEvent`].
///
/// See [`Daemon::handle_location_event()`]
fn fetch_am_i_mullvad(&mut self) {
async fn fetch_am_i_mullvad(&mut self) {
// Always abort any ongoing request when entering a new tunnel state
self.location_handler.abort_current_request();

Expand All @@ -1050,10 +1050,15 @@ where
}

let use_ipv6 = self.settings.tunnel_options.generic.enable_ipv6;
let api_handle = self.api_handle.clone();
let rest_service = self.api_runtime.rest_handle().await;
let availability = self.api_handle.availability.clone();
let location_sender = self.tx.clone().to_specialized_sender();
self.location_handler
.send_geo_location_request(use_ipv6, api_handle, location_sender);
self.location_handler.send_geo_location_request(
use_ipv6,
availability,
rest_service,
location_sender,
);
}

async fn handle_location_event(&mut self, request_id: usize, fetched_location: GeoIpLocation) {
Expand Down

0 comments on commit dc12a62

Please sign in to comment.