use crate::protocols::tls::{AutoFlushableStream, S2NConnectionBuilder, TlsStream};
use crate::protocols::IO;
use pingora_error::ErrorType::TLSHandshakeFailure;
use pingora_error::{Error, Result};
use pingora_s2n::TlsConnector;
pub async fn handshake<S: IO>(
connector: &TlsConnector<S2NConnectionBuilder>,
domain: &str,
stream: S,
) -> Result<TlsStream<S>> {
let auto_flushable_stream = AutoFlushableStream::new(stream, true);
let mut s2n_stream = connector
.connect(domain, auto_flushable_stream)
.await
.map_err(|e| {
let context = format!("TLS connect() failed: {e}, SNI: {domain}");
Error::explain(TLSHandshakeFailure, context)
})?;
s2n_stream.get_mut().set_auto_flush(false);
Ok(TlsStream::from_s2n_stream(s2n_stream))
}