use std::fmt;
use std::future::Future;
use std::sync::Arc;
use std::time::Duration;
use tokio::io::{AsyncRead, AsyncWrite};
use tokio::net::TcpStream;
use tokio::time::timeout;
use tokio_rustls::TlsConnector;
use tokio_rustls::client::TlsStream;
use tokio_rustls::rustls::crypto::ring;
use tokio_rustls::rustls::pki_types::ServerName;
use tokio_rustls::rustls::{ClientConfig, RootCertStore};
use crate::common::errors::TransportError;
pub const CONNECT_TIMEOUT: Duration = Duration::from_secs(15);
pub trait Connect: Send + Sync + 'static {
type Stream: AsyncRead + AsyncWrite + Unpin + Send + 'static;
fn connect(
&self,
host: &str,
port: u16,
) -> impl Future<Output = Result<Self::Stream, TransportError>> + Send;
}
async fn connect_tcp(host: &str, port: u16) -> Result<TcpStream, TransportError> {
let stream = timeout(CONNECT_TIMEOUT, TcpStream::connect((host, port)))
.await
.map_err(|_| TransportError::ConnectTimeout(CONNECT_TIMEOUT))??;
stream.set_nodelay(true)?;
Ok(stream)
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Plain;
impl Connect for Plain {
type Stream = TcpStream;
async fn connect(&self, host: &str, port: u16) -> Result<Self::Stream, TransportError> {
connect_tcp(host, port).await
}
}
#[derive(Clone)]
pub struct Tls {
connector: TlsConnector,
}
impl fmt::Debug for Tls {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Tls").finish_non_exhaustive()
}
}
impl Tls {
pub fn new() -> Result<Self, TransportError> {
let mut roots = RootCertStore::empty();
roots.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
let config = ClientConfig::builder_with_provider(Arc::new(ring::default_provider()))
.with_safe_default_protocol_versions()
.map_err(|error| TransportError::Tls(error.to_string()))?
.with_root_certificates(roots)
.with_no_client_auth();
Ok(Self {
connector: TlsConnector::from(Arc::new(config)),
})
}
}
impl Connect for Tls {
type Stream = TlsStream<TcpStream>;
async fn connect(&self, host: &str, port: u16) -> Result<Self::Stream, TransportError> {
let name = ServerName::try_from(host.to_owned())
.map_err(|_| TransportError::InvalidHost(host.to_owned()))?;
let stream = connect_tcp(host, port).await?;
timeout(CONNECT_TIMEOUT, self.connector.connect(name, stream))
.await
.map_err(|_| TransportError::ConnectTimeout(CONNECT_TIMEOUT))?
.map_err(|error| TransportError::Tls(error.to_string()))
}
}
#[cfg(test)]
mod tests {
use super::{Connect, Tls};
use crate::common::errors::TransportError;
#[tokio::test]
async fn tls_rejects_a_host_that_is_not_a_server_name() {
let tls = Tls::new().expect("tls");
assert!(format!("{tls:?}").contains("Tls"));
assert_eq!(
tls.connect("not a host", 1).await.err(),
Some(TransportError::InvalidHost("not a host".to_owned()))
);
}
}