-
Notifications
You must be signed in to change notification settings - Fork 381
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add non-blocking DNS resolver for Android API requests
- Loading branch information
Showing
21 changed files
with
480 additions
and
265 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#![cfg(target_os = "android")] | ||
//! See [AndroidDnsResolver]. | ||
use async_trait::async_trait; | ||
use hickory_resolver::{ | ||
config::{NameServerConfigGroup, ResolverConfig, ResolverOpts}, | ||
TokioAsyncResolver, | ||
}; | ||
use mullvad_api::DnsResolver; | ||
use std::{io, net::IpAddr}; | ||
use talpid_core::connectivity_listener::ConnectivityListener; | ||
|
||
/// A non-blocking DNS resolver. The default resolver uses `getaddrinfo`, which often prevents the | ||
/// tokio runtime from being dropped, since it waits indefinitely on blocking threads. This is | ||
/// particularly bad on Android, so we use a non-blocking resolver instead. | ||
pub struct AndroidDnsResolver { | ||
connectivity_listener: ConnectivityListener, | ||
} | ||
|
||
impl AndroidDnsResolver { | ||
pub fn new(connectivity_listener: ConnectivityListener) -> Self { | ||
Self { | ||
connectivity_listener, | ||
} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl DnsResolver for AndroidDnsResolver { | ||
async fn resolve(&self, host: String) -> io::Result<Vec<IpAddr>> { | ||
let ips = self | ||
.connectivity_listener | ||
.current_dns_servers() | ||
.map_err(|err| { | ||
io::Error::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 lookup = resolver | ||
.lookup_ip(host) | ||
.await | ||
.map_err(|err| io::Error::other(format!("lookup_ip failed: {err}")))?; | ||
|
||
Ok(lookup.into_iter().collect()) | ||
} | ||
} |
Oops, something went wrong.