mirror of
https://github.com/mikedilger/chorus.git
synced 2026-05-03 06:51:42 +00:00
MaybeTlsStream type
This commit is contained in:
parent
8701ae69dd
commit
445cc89530
@ -4,6 +4,7 @@ pub mod config;
|
|||||||
pub mod error;
|
pub mod error;
|
||||||
pub mod globals;
|
pub mod globals;
|
||||||
pub mod store;
|
pub mod store;
|
||||||
|
pub mod tls;
|
||||||
pub mod types;
|
pub mod types;
|
||||||
pub mod web;
|
pub mod web;
|
||||||
|
|
||||||
|
|||||||
58
src/tls.rs
Normal file
58
src/tls.rs
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
use std::{
|
||||||
|
pin::Pin,
|
||||||
|
task::{Context, Poll},
|
||||||
|
};
|
||||||
|
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
|
||||||
|
|
||||||
|
/// A stream that might be protected with TLS.
|
||||||
|
#[allow(clippy::large_enum_variant)] // not great though
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum MaybeTlsStream<S> {
|
||||||
|
/// Unencrypted socket stream.
|
||||||
|
Plain(S),
|
||||||
|
/// Encrypted socket stream using `rustls`.
|
||||||
|
Rustls(tokio_rustls::client::TlsStream<S>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S: AsyncRead + AsyncWrite + Unpin> AsyncRead for MaybeTlsStream<S> {
|
||||||
|
fn poll_read(
|
||||||
|
self: Pin<&mut Self>,
|
||||||
|
cx: &mut Context<'_>,
|
||||||
|
buf: &mut ReadBuf<'_>,
|
||||||
|
) -> Poll<std::io::Result<()>> {
|
||||||
|
match self.get_mut() {
|
||||||
|
MaybeTlsStream::Plain(ref mut s) => Pin::new(s).poll_read(cx, buf),
|
||||||
|
MaybeTlsStream::Rustls(s) => Pin::new(s).poll_read(cx, buf),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S: AsyncRead + AsyncWrite + Unpin> AsyncWrite for MaybeTlsStream<S> {
|
||||||
|
fn poll_write(
|
||||||
|
self: Pin<&mut Self>,
|
||||||
|
cx: &mut Context<'_>,
|
||||||
|
buf: &[u8],
|
||||||
|
) -> Poll<Result<usize, std::io::Error>> {
|
||||||
|
match self.get_mut() {
|
||||||
|
MaybeTlsStream::Plain(ref mut s) => Pin::new(s).poll_write(cx, buf),
|
||||||
|
MaybeTlsStream::Rustls(s) => Pin::new(s).poll_write(cx, buf),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), std::io::Error>> {
|
||||||
|
match self.get_mut() {
|
||||||
|
MaybeTlsStream::Plain(ref mut s) => Pin::new(s).poll_flush(cx),
|
||||||
|
MaybeTlsStream::Rustls(s) => Pin::new(s).poll_flush(cx),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn poll_shutdown(
|
||||||
|
self: Pin<&mut Self>,
|
||||||
|
cx: &mut Context<'_>,
|
||||||
|
) -> Poll<Result<(), std::io::Error>> {
|
||||||
|
match self.get_mut() {
|
||||||
|
MaybeTlsStream::Plain(ref mut s) => Pin::new(s).poll_shutdown(cx),
|
||||||
|
MaybeTlsStream::Rustls(s) => Pin::new(s).poll_shutdown(cx),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user