From 9df62bc1d701165235b7d6a9a0736fbc4cb357fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20L=C3=B6nnhager?= Date: Thu, 21 Nov 2024 11:26:11 +0100 Subject: [PATCH] Use async-trait instead of Pin> --- Cargo.lock | 2 ++ mullvad-api/Cargo.toml | 1 + mullvad-api/src/lib.rs | 26 +++++++++------------- mullvad-daemon/Cargo.toml | 1 + mullvad-daemon/src/android_dns.rs | 37 ++++++++++++++----------------- test/Cargo.lock | 1 + 6 files changed, 32 insertions(+), 36 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 46702f49647d..97931f54f424 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2309,6 +2309,7 @@ dependencies = [ name = "mullvad-api" version = "0.0.0" dependencies = [ + "async-trait", "cbindgen", "chrono", "futures", @@ -2364,6 +2365,7 @@ name = "mullvad-daemon" version = "0.0.0" dependencies = [ "android_logger", + "async-trait", "chrono", "clap", "ctrlc", diff --git a/mullvad-api/Cargo.toml b/mullvad-api/Cargo.toml index e617d942b522..a822593600e0 100644 --- a/mullvad-api/Cargo.toml +++ b/mullvad-api/Cargo.toml @@ -15,6 +15,7 @@ workspace = true api-override = [] [dependencies] +async-trait = "0.1" libc = "0.2" chrono = { workspace = true } thiserror = { workspace = true } diff --git a/mullvad-api/src/lib.rs b/mullvad-api/src/lib.rs index 271ffa329226..63d1d8436c40 100644 --- a/mullvad-api/src/lib.rs +++ b/mullvad-api/src/lib.rs @@ -1,4 +1,5 @@ #![allow(rustdoc::private_intra_doc_links)] +use async_trait::async_trait; #[cfg(target_os = "android")] use futures::channel::mpsc; #[cfg(target_os = "android")] @@ -16,7 +17,6 @@ use std::{ net::{IpAddr, Ipv4Addr, SocketAddr}, ops::Deref, path::Path, - pin::Pin, sync::{Arc, OnceLock}, }; use talpid_types::ErrorExt; @@ -306,28 +306,21 @@ impl ApiEndpoint { } } +#[async_trait] pub trait DnsResolver: 'static + Send + Sync { - fn resolve( - &self, - host: String, - ) -> Pin>> + Send>>; + async fn resolve(&self, host: String) -> io::Result>; } pub struct DefaultDnsResolver; +#[async_trait] impl DnsResolver for DefaultDnsResolver { - fn resolve( - &self, - host: String, - ) -> Pin>> + Send>> { + async fn resolve(&self, host: String) -> io::Result> { use std::net::ToSocketAddrs; - - Box::pin(async move { - let addrs = tokio::task::spawn_blocking(move || (host, 0).to_socket_addrs()) - .await - .expect("DNS task panicked")?; - Ok(addrs.map(|addr| addr.ip()).collect()) - }) + let addrs = tokio::task::spawn_blocking(move || (host, 0).to_socket_addrs()) + .await + .expect("DNS task panicked")?; + Ok(addrs.map(|addr| addr.ip()).collect()) } } @@ -408,6 +401,7 @@ impl Runtime { if API.disable_address_cache { return Self::new_inner( handle, + dns_resolver, #[cfg(target_os = "android")] socket_bypass_tx, ); diff --git a/mullvad-daemon/Cargo.toml b/mullvad-daemon/Cargo.toml index b961cc2786cb..979aa86eac7f 100644 --- a/mullvad-daemon/Cargo.toml +++ b/mullvad-daemon/Cargo.toml @@ -51,6 +51,7 @@ tokio = { workspace = true, features = ["test-util"] } [target.'cfg(target_os="android")'.dependencies] android_logger = "0.8" +async-trait = "0.1" hickory-resolver = { version = "0.24.1" } [target.'cfg(unix)'.dependencies] diff --git a/mullvad-daemon/src/android_dns.rs b/mullvad-daemon/src/android_dns.rs index 12a8d4f3649f..57c5383c4dfa 100644 --- a/mullvad-daemon/src/android_dns.rs +++ b/mullvad-daemon/src/android_dns.rs @@ -3,12 +3,13 @@ //! dropped, since it waits indefinitely on blocking threads. This is particularly bad on Android, //! so we use a non-blocking resolver instead. +use async_trait::async_trait; use hickory_resolver::{ config::{NameServerConfigGroup, ResolverConfig, ResolverOpts}, TokioAsyncResolver, }; use mullvad_api::DnsResolver; -use std::{future::Future, io, net::IpAddr, pin::Pin}; +use std::{io, net::IpAddr}; pub struct AndroidDnsResolver { connectivity_listener: talpid_core::connectivity_listener::ConnectivityListener, @@ -24,30 +25,26 @@ impl AndroidDnsResolver { } } +#[async_trait] impl DnsResolver for AndroidDnsResolver { - fn resolve( - &self, - host: String, - ) -> Pin>> + Send>> { + async fn resolve(&self, host: String) -> io::Result> { let ips = self.connectivity_listener.current_dns_servers(); - Box::pin(async move { - let ips = ips.map_err(|err| { - io::Error::new( - io::ErrorKind::Other, - format!("Failed to retrieve current servers: {err}"), - ) - })?; - let group = NameServerConfigGroup::from_ips_clear(&ips, 53, false); + let ips = ips.map_err(|err| { + io::Error::new( + io::ErrorKind::Other, + format!("Failed to retrieve current servers: {err}"), + ) + })?; + let group = NameServerConfigGroup::from_ips_clear(&ips, 53, false); - let config = ResolverConfig::from_parts(None, vec![], group); - let resolver = TokioAsyncResolver::tokio(config, ResolverOpts::default()); + let config = ResolverConfig::from_parts(None, vec![], group); + let resolver = TokioAsyncResolver::tokio(config, ResolverOpts::default()); - let lookup = resolver.lookup_ip(host).await.map_err(|err| { - io::Error::new(io::ErrorKind::Other, format!("lookup_ip failed: {err}")) - })?; + let lookup = resolver.lookup_ip(host).await.map_err(|err| { + io::Error::new(io::ErrorKind::Other, format!("lookup_ip failed: {err}")) + })?; - Ok(lookup.into_iter().collect()) - }) + Ok(lookup.into_iter().collect()) } } diff --git a/test/Cargo.lock b/test/Cargo.lock index 1b319ad5c3ef..941a0a08da5d 100644 --- a/test/Cargo.lock +++ b/test/Cargo.lock @@ -1895,6 +1895,7 @@ dependencies = [ name = "mullvad-api" version = "0.0.0" dependencies = [ + "async-trait", "cbindgen", "chrono", "futures",