use rustls::pki_types::ServerName;
use tokio::net::TcpStream;
use super::{ConnectionSettings, GenericStream, get_tls_connector};
use crate::error::Error;
pub async fn get_client_stream(settings: ConnectionSettings<'_>) -> Result<GenericStream, Error> {
match settings {
ConnectionSettings::TlsTcpSocket {
host,
port,
certificate,
} => {
let address = format!("{host}:{port}");
let tcp_stream = TcpStream::connect(&address).await.map_err(|_| {
Error::Connection(format!(
"Failed to connect to the daemon on {address}. Did you start it?"
))
})?;
let tls_connector = get_tls_connector(certificate).await.map_err(|err| {
Error::Connection(format!("Failed to initialize tls connector:\n{err}."))
})?;
let stream = tls_connector
.connect(ServerName::try_from("pueue.local").unwrap(), tcp_stream)
.await
.map_err(|err| Error::Connection(format!("Failed to initialize tls {err}.")))?;
Ok(Box::new(stream))
}
}
}