use crate::protocols::tls::rustls::TlsStream;
use crate::protocols::IO;
use pingora_error::ErrorType::TLSHandshakeFailure;
use pingora_error::{Error, OrErr, Result};
use pingora_rustls::TlsConnector;
pub async fn handshake<S: IO>(
connector: &TlsConnector,
domain: &str,
io: S,
) -> Result<TlsStream<S>> {
let mut stream = TlsStream::from_connector(connector, domain, io)
.await
.or_err(TLSHandshakeFailure, "tls stream error")?;
let handshake_result = stream.connect().await;
match handshake_result {
Ok(()) => Ok(stream),
Err(e) => {
let context = format!("TLS connect() failed: {e}, SNI: {domain}");
Error::e_explain(TLSHandshakeFailure, context)
}
}
}