use std::future::Future;
use std::io;
use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::task::{Context, Poll};
use rustls::ClientConfig;
use rustls::pki_types::{InvalidDnsNameError, ServerName};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use tokio_postgres::tls::{ChannelBinding, MakeTlsConnect, TlsConnect, TlsStream};
use tokio_rustls::TlsConnector;
use tokio_rustls::client::TlsStream as RustlsStream;
use crate::postgres_source::_helpers::tls::server_end_point;
#[derive(Clone)]
pub(crate) struct RustlsConnect {
pub(crate) config: Arc<ClientConfig>,
pub(crate) encrypted: Arc<AtomicBool>,
pub(crate) refused: Arc<AtomicBool>,
}
impl<S> MakeTlsConnect<S> for RustlsConnect
where
S: AsyncRead + AsyncWrite + Unpin + Send + 'static,
{
type Stream = Encrypted<S>;
type TlsConnect = Handshake;
type Error = io::Error;
fn make_tls_connect(&mut self, domain: &str) -> Result<Handshake, io::Error> {
self.encrypted.store(false, Ordering::Relaxed);
self.refused.store(false, Ordering::Relaxed);
Ok(Handshake {
connect: self.clone(),
name: ServerName::try_from(domain.to_owned()),
})
}
}
pub(crate) struct Handshake {
connect: RustlsConnect,
name: Result<ServerName<'static>, InvalidDnsNameError>,
}
impl<S> TlsConnect<S> for Handshake
where
S: AsyncRead + AsyncWrite + Unpin + Send + 'static,
{
type Stream = Encrypted<S>;
type Error = io::Error;
type Future = Pin<Box<dyn Future<Output = io::Result<Encrypted<S>>> + Send>>;
fn connect(self, stream: S) -> Self::Future {
let connector = TlsConnector::from(self.connect.config);
let RustlsConnect {
encrypted, refused, ..
} = self.connect;
Box::pin(async move {
let name = self
.name
.map_err(|error| io::Error::new(io::ErrorKind::InvalidInput, error))?;
let session = connector
.connect(name, stream)
.await
.inspect_err(|_| refused.store(true, Ordering::Relaxed))?;
encrypted.store(true, Ordering::Relaxed);
Ok(Encrypted(session))
})
}
}
pub(crate) struct Encrypted<S>(RustlsStream<S>);
impl<S: AsyncRead + AsyncWrite + Unpin> TlsStream for Encrypted<S> {
fn channel_binding(&self) -> ChannelBinding {
self.0
.get_ref()
.1
.peer_certificates()
.and_then(<[_]>::first)
.and_then(|certificate| server_end_point(certificate))
.map_or_else(ChannelBinding::none, ChannelBinding::tls_server_end_point)
}
}
impl<S: AsyncRead + AsyncWrite + Unpin> AsyncRead for Encrypted<S> {
fn poll_read(
mut self: Pin<&mut Self>,
context: &mut Context<'_>,
buffer: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
Pin::new(&mut self.0).poll_read(context, buffer)
}
}
impl<S: AsyncRead + AsyncWrite + Unpin> AsyncWrite for Encrypted<S> {
fn poll_write(
mut self: Pin<&mut Self>,
context: &mut Context<'_>,
buffer: &[u8],
) -> Poll<io::Result<usize>> {
Pin::new(&mut self.0).poll_write(context, buffer)
}
fn poll_flush(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.0).poll_flush(context)
}
fn poll_shutdown(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.0).poll_shutdown(context)
}
}