Skip to content

Commit

Permalink
Make SNI hostname implicit
Browse files Browse the repository at this point in the history
  • Loading branch information
dlon committed Dec 2, 2024
1 parent dff45ab commit d74092d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 19 deletions.
18 changes: 6 additions & 12 deletions mullvad-api/src/https_client_with_sni.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ impl TryFrom<ApiConnectionMode> for InnerConnectionMode {
#[derive(Clone)]
pub struct HttpsConnectorWithSni {
inner: Arc<Mutex<HttpsConnectorWithSniInner>>,
sni_hostname: Option<String>,
abort_notify: Arc<tokio::sync::Notify>,
dns_resolver: Arc<dyn DnsResolver>,
#[cfg(target_os = "android")]
Expand All @@ -303,7 +302,6 @@ pub type SocketBypassRequest = (RawFd, oneshot::Sender<()>);

impl HttpsConnectorWithSni {
pub fn new(
sni_hostname: Option<String>,
dns_resolver: Arc<dyn DnsResolver>,
#[cfg(target_os = "android")] socket_bypass_tx: Option<mpsc::Sender<SocketBypassRequest>>,
) -> (Self, HttpsConnectorWithSniHandle) {
Expand Down Expand Up @@ -350,7 +348,6 @@ impl HttpsConnectorWithSni {
(
HttpsConnectorWithSni {
inner,
sni_hostname,
abort_notify,
dns_resolver,
#[cfg(target_os = "android")]
Expand Down Expand Up @@ -432,13 +429,6 @@ impl Service<Uri> for HttpsConnectorWithSni {
}

fn call(&mut self, uri: Uri) -> Self::Future {
let sni_hostname = self
.sni_hostname
.clone()
.or_else(|| uri.host().map(str::to_owned))
.ok_or_else(|| {
io::Error::new(io::ErrorKind::InvalidInput, "invalid url, missing host")
});
let inner = self.inner.clone();
let abort_notify = self.abort_notify.clone();
#[cfg(target_os = "android")]
Expand All @@ -452,8 +442,12 @@ impl Service<Uri> for HttpsConnectorWithSni {
"invalid url, not https",
));
}

let hostname = sni_hostname?;
let Some(hostname) = uri.host().map(str::to_owned) else {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"invalid url, missing host",
));
};
let addr = Self::resolve_address(&*dns_resolver, uri).await?;

// Loop until we have established a connection. This starts over if a new endpoint
Expand Down
5 changes: 0 additions & 5 deletions mullvad-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,6 @@ impl Runtime {
connection_mode_provider: T,
) -> rest::MullvadRestHandle {
let service = self.new_request_service(
Some(API.host().to_string()),
connection_mode_provider,
Arc::new(self.address_cache.clone()),
#[cfg(target_os = "android")]
Expand All @@ -466,7 +465,6 @@ impl Runtime {
/// This is only to be used in test code
pub fn static_mullvad_rest_handle(&self, hostname: String) -> rest::MullvadRestHandle {
let service = self.new_request_service(
Some(hostname.clone()),
ApiConnectionMode::Direct.into_provider(),
Arc::new(self.address_cache.clone()),
#[cfg(target_os = "android")]
Expand All @@ -481,7 +479,6 @@ impl Runtime {
/// Returns a new request service handle
pub fn rest_handle(&self, dns_resolver: impl DnsResolver) -> rest::RequestServiceHandle {
self.new_request_service(
None,
ApiConnectionMode::Direct.into_provider(),
Arc::new(dns_resolver),
#[cfg(target_os = "android")]
Expand All @@ -492,13 +489,11 @@ impl Runtime {
/// Creates a new request service and returns a handle to it.
fn new_request_service<T: ConnectionModeProvider + 'static>(
&self,
sni_hostname: Option<String>,
connection_mode_provider: T,
dns_resolver: Arc<dyn DnsResolver>,
#[cfg(target_os = "android")] socket_bypass_tx: Option<mpsc::Sender<SocketBypassRequest>>,
) -> rest::RequestServiceHandle {
rest::RequestService::spawn(
sni_hostname,
self.api_availability.clone(),
connection_mode_provider,
dns_resolver,
Expand Down
2 changes: 0 additions & 2 deletions mullvad-api/src/rest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,12 @@ pub(crate) struct RequestService<T: ConnectionModeProvider> {
impl<T: ConnectionModeProvider + 'static> RequestService<T> {
/// Constructs a new request service.
pub fn spawn(
sni_hostname: Option<String>,
api_availability: ApiAvailability,
connection_mode_provider: T,
dns_resolver: Arc<dyn DnsResolver>,
#[cfg(target_os = "android")] socket_bypass_tx: Option<mpsc::Sender<SocketBypassRequest>>,
) -> RequestServiceHandle {
let (connector, connector_handle) = HttpsConnectorWithSni::new(
sni_hostname,
dns_resolver,
#[cfg(target_os = "android")]
socket_bypass_tx.clone(),
Expand Down

0 comments on commit d74092d

Please sign in to comment.