1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use std::{future::Future, sync::Arc};
use monoio::{
io::{AsyncReadRent, AsyncWriteRent},
BufResult,
};
use rustls::{ServerConfig, ServerConnection};
use crate::{stream::Stream, TlsError};
#[derive(Debug)]
pub struct TlsStream<IO>(pub(crate) Stream<IO, ServerConnection>);
impl<IO> AsyncReadRent for TlsStream<IO>
where
IO: AsyncReadRent + AsyncWriteRent,
{
type ReadFuture<'a, T> = impl Future<Output = BufResult<usize, T>>
where
T: 'a, IO: 'a;
type ReadvFuture<'a, T> = impl Future<Output = BufResult<usize, T>>
where
T: 'a, IO: 'a;
fn read<T: monoio::buf::IoBufMut>(&mut self, buf: T) -> Self::ReadFuture<'_, T> {
self.0.read(buf)
}
fn readv<T: monoio::buf::IoVecBufMut>(&mut self, buf: T) -> Self::ReadvFuture<'_, T> {
self.0.readv(buf)
}
}
impl<IO> AsyncWriteRent for TlsStream<IO>
where
IO: AsyncReadRent + AsyncWriteRent,
{
type WriteFuture<'a, T> = impl Future<Output = BufResult<usize, T>>
where
T: 'a, IO: 'a;
type WritevFuture<'a, T> = impl Future<Output = BufResult<usize, T>>
where
T: 'a, IO: 'a;
type ShutdownFuture<'a> = impl Future<Output = Result<(), std::io::Error>>
where
IO: 'a;
fn write<T: monoio::buf::IoBuf>(&mut self, buf: T) -> Self::WriteFuture<'_, T> {
self.0.write(buf)
}
fn writev<T: monoio::buf::IoVecBuf>(&mut self, buf_vec: T) -> Self::WritevFuture<'_, T> {
self.0.writev(buf_vec)
}
fn shutdown(&mut self) -> Self::ShutdownFuture<'_> {
self.0.shutdown()
}
}
#[derive(Clone)]
pub struct TlsAcceptor {
inner: Arc<ServerConfig>,
}
impl From<Arc<ServerConfig>> for TlsAcceptor {
fn from(inner: Arc<ServerConfig>) -> TlsAcceptor {
TlsAcceptor { inner }
}
}
impl From<ServerConfig> for TlsAcceptor {
fn from(inner: ServerConfig) -> TlsAcceptor {
TlsAcceptor {
inner: Arc::new(inner),
}
}
}
impl TlsAcceptor {
pub async fn accept<IO>(&self, stream: IO) -> Result<TlsStream<IO>, TlsError>
where
IO: AsyncReadRent + AsyncWriteRent,
{
let session = ServerConnection::new(self.inner.clone())?;
let mut stream = Stream::new(stream, session);
stream.handshake().await?;
Ok(TlsStream(stream))
}
}