summaryrefslogtreecommitdiffhomepage
path: root/mullvad-api/src/address_cache.rs
diff options
context:
space:
mode:
authorDavid Lönnhager <david.l@mullvad.net>2024-11-27 15:09:05 +0100
committerDavid Lönnhager <david.l@mullvad.net>2024-12-02 16:00:38 +0100
commit94ce8fb753f7441243d9281416632ce7ed4b6cd6 (patch)
tree8fa86e98b4b50ecd70ebf6468d222cad8e6cf552 /mullvad-api/src/address_cache.rs
parente07c12a5f14a11051fa086c97cc22413e431a1c8 (diff)
downloadmullvadvpn-94ce8fb753f7441243d9281416632ce7ed4b6cd6.tar.xz
mullvadvpn-94ce8fb753f7441243d9281416632ce7ed4b6cd6.zip
Remove DNS fallback except for conncheck
Diffstat (limited to 'mullvad-api/src/address_cache.rs')
-rw-r--r--mullvad-api/src/address_cache.rs20
1 files changed, 18 insertions, 2 deletions
diff --git a/mullvad-api/src/address_cache.rs b/mullvad-api/src/address_cache.rs
index a48db1a0e2..0898f8da1f 100644
--- a/mullvad-api/src/address_cache.rs
+++ b/mullvad-api/src/address_cache.rs
@@ -1,6 +1,8 @@
//! This module keeps track of the last known good API IP address and reads and stores it on disk.
use super::API;
+use crate::DnsResolver;
+use async_trait::async_trait;
use std::{io, net::SocketAddr, path::Path, sync::Arc};
use tokio::{
fs,
@@ -23,6 +25,17 @@ pub enum Error {
Write(#[source] io::Error),
}
+/// A DNS resolver which resolves using `AddressCache`.
+#[async_trait]
+impl DnsResolver for AddressCache {
+ async fn resolve(&self, host: String) -> Result<Vec<SocketAddr>, io::Error> {
+ self.resolve_hostname(&host)
+ .await
+ .map(|addr| vec![addr])
+ .ok_or(io::Error::other("host does not match API host"))
+ }
+}
+
#[derive(Clone)]
pub struct AddressCache {
inner: Arc<Mutex<AddressCacheInner>>,
@@ -42,7 +55,10 @@ impl AddressCache {
/// Initialize cache using `read_path`, and write changes to `write_path`.
pub async fn from_file(read_path: &Path, write_path: Option<Box<Path>>) -> Result<Self, Error> {
log::debug!("Loading API addresses from {}", read_path.display());
- Ok(Self::new_inner(read_address_file(read_path).await?, write_path))
+ Ok(Self::new_inner(
+ read_address_file(read_path).await?,
+ write_path,
+ ))
}
fn new_inner(address: SocketAddr, write_path: Option<Box<Path>>) -> Self {
@@ -56,7 +72,7 @@ impl AddressCache {
}
/// Returns the address if the hostname equals `API.host`. Otherwise, returns `None`.
- pub async fn resolve_hostname(&self, hostname: &str) -> Option<SocketAddr> {
+ async fn resolve_hostname(&self, hostname: &str) -> Option<SocketAddr> {
if hostname.eq_ignore_ascii_case(API.host()) {
Some(self.get_address().await)
} else {