Skip to content

Commit

Permalink
Move Rest and availability handle into GeoIpHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
Serock3 committed Dec 7, 2023
1 parent dc12a62 commit ac2262b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
12 changes: 8 additions & 4 deletions mullvad-daemon/src/geoip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,34 +49,38 @@ static MULLVAD_CONNCHECK_HOST: Lazy<String> = Lazy::new(|| {
const LOCATION_RETRY_STRATEGY: Jittered<ExponentialBackoff> =
Jittered::jitter(ExponentialBackoff::new(Duration::from_secs(1), 4));

/// Handler for request to am.i.mullvad.net
/// Handler for request to am.i.mullvad.net, manages in-flight request and validity of responses.
pub struct GeoIpHandler {
/// Unique ID for each request, used to verify
pub request_id: usize,
/// Handle to abort any in-flight request.
request_handle: Option<JoinHandle<()>>,
availability: ApiAvailabilityHandle,
rest_service: RequestServiceHandle,
}

impl GeoIpHandler {
pub fn new() -> Self {
pub fn new(availability: ApiAvailabilityHandle, rest_service: RequestServiceHandle) -> Self {
Self {
request_id: 0,
request_handle: None,
availability,
rest_service,
}
}

pub fn send_geo_location_request(
&mut self,
use_ipv6: bool,
availability: ApiAvailabilityHandle,
rest_service: RequestServiceHandle,
daemon_event_sender: DaemonEventSender,
) {
// Increment request ID
self.request_id = self.request_id.wrapping_add(1);
let request_id_copy = self.request_id;

self.abort_current_request();
let rest_service = self.rest_service.clone();
let availability = self.availability.clone();
self.request_handle = Some(tokio::spawn(async move {
if let Ok(merged_location) =
get_geo_location_with_retry(use_ipv6, availability, rest_service).await
Expand Down
17 changes: 8 additions & 9 deletions mullvad-daemon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,11 @@ where
// Attempt to download a fresh relay list
relay_list_updater.update().await;

let location_handler = GeoIpHandler::new(
api_handle.availability.clone(),
api_runtime.rest_handle().await,
);

let daemon = Daemon {
tunnel_state: TunnelState::Disconnected(None),
target_state,
Expand Down Expand Up @@ -859,7 +864,7 @@ where
tunnel_state_machine_handle,
#[cfg(target_os = "windows")]
volume_update_tx,
location_handler: GeoIpHandler::new(),
location_handler,
};

api_availability.unsuspend();
Expand Down Expand Up @@ -1050,15 +1055,9 @@ where
}

let use_ipv6 = self.settings.tunnel_options.generic.enable_ipv6;
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,
availability,
rest_service,
location_sender,
);
self.location_handler
.send_geo_location_request(use_ipv6, location_sender);
}

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

0 comments on commit ac2262b

Please sign in to comment.