forked from compio-rs/compio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadapter.rs
More file actions
235 lines (218 loc) · 8.1 KB
/
adapter.rs
File metadata and controls
235 lines (218 loc) · 8.1 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
use std::{fmt::Debug, io};
use compio_io::{
AsyncRead, AsyncWrite,
compat::{AsyncStream, SyncStream},
};
use crate::TlsStream;
#[derive(Clone)]
enum TlsConnectorInner {
#[cfg(feature = "native-tls")]
NativeTls(native_tls::TlsConnector),
#[cfg(feature = "rustls")]
Rustls(futures_rustls::TlsConnector),
#[cfg(feature = "py-dynamic-openssl")]
PyDynamicOpenSsl(compio_py_dynamic_openssl::SSLContext),
#[cfg(not(any(
feature = "native-tls",
feature = "rustls",
feature = "py-dynamic-openssl"
)))]
None(std::convert::Infallible),
}
impl Debug for TlsConnectorInner {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
#[cfg(feature = "native-tls")]
Self::NativeTls(_) => f.debug_tuple("NativeTls").finish(),
#[cfg(feature = "rustls")]
Self::Rustls(_) => f.debug_tuple("Rustls").finish(),
#[cfg(feature = "py-dynamic-openssl")]
Self::PyDynamicOpenSsl(_) => f.debug_tuple("PyDynamicOpenSsl").finish(),
#[cfg(not(any(
feature = "native-tls",
feature = "rustls",
feature = "py-dynamic-openssl"
)))]
Self::None(f) => match *f {},
}
}
}
/// A wrapper around a [`native_tls::TlsConnector`] or [`rustls::ClientConfig`],
/// providing an async `connect` method.
#[derive(Debug, Clone)]
pub struct TlsConnector(TlsConnectorInner);
#[cfg(feature = "native-tls")]
impl From<native_tls::TlsConnector> for TlsConnector {
fn from(value: native_tls::TlsConnector) -> Self {
Self(TlsConnectorInner::NativeTls(value))
}
}
#[cfg(feature = "rustls")]
impl From<std::sync::Arc<rustls::ClientConfig>> for TlsConnector {
fn from(value: std::sync::Arc<rustls::ClientConfig>) -> Self {
Self(TlsConnectorInner::Rustls(value.into()))
}
}
#[cfg(feature = "py-dynamic-openssl")]
#[doc(hidden)]
impl From<compio_py_dynamic_openssl::SSLContext> for TlsConnector {
fn from(value: compio_py_dynamic_openssl::SSLContext) -> Self {
Self(TlsConnectorInner::PyDynamicOpenSsl(value))
}
}
impl TlsConnector {
/// Connects the provided stream with this connector, assuming the provided
/// domain.
///
/// This function will internally call `TlsConnector::connect` to connect
/// the stream and returns a future representing the resolution of the
/// connection operation. The returned future will resolve to either
/// `TlsStream<S>` or `Error` depending if it's successful or not.
///
/// This is typically used for clients who have already established, for
/// example, a TCP connection to a remote server. That stream is then
/// provided here to perform the client half of a connection to a
/// TLS-powered server.
pub async fn connect<S: AsyncRead + AsyncWrite + Unpin + 'static>(
&self,
domain: &str,
stream: S,
) -> io::Result<TlsStream<S>> {
match &self.0 {
#[cfg(feature = "native-tls")]
TlsConnectorInner::NativeTls(c) => {
handshake_native_tls(c.connect(domain, SyncStream::new(stream))).await
}
#[cfg(feature = "rustls")]
TlsConnectorInner::Rustls(c) => {
let client = c
.connect(
domain.to_string().try_into().map_err(io::Error::other)?,
Box::pin(AsyncStream::new(stream)),
)
.await?;
Ok(TlsStream::from(client))
}
#[cfg(feature = "py-dynamic-openssl")]
TlsConnectorInner::PyDynamicOpenSsl(c) => {
crate::py_ossl::handshake(c.connect(domain, SyncStream::new(stream))).await
}
#[cfg(not(any(
feature = "native-tls",
feature = "rustls",
feature = "py-dynamic-openssl"
)))]
TlsConnectorInner::None(f) => match *f {},
}
}
}
#[derive(Clone)]
enum TlsAcceptorInner {
#[cfg(feature = "native-tls")]
NativeTls(native_tls::TlsAcceptor),
#[cfg(feature = "rustls")]
Rustls(futures_rustls::TlsAcceptor),
#[cfg(feature = "py-dynamic-openssl")]
PyDynamicOpenSsl(compio_py_dynamic_openssl::SSLContext),
#[cfg(not(any(
feature = "native-tls",
feature = "rustls",
feature = "py-dynamic-openssl"
)))]
None(std::convert::Infallible),
}
/// A wrapper around a [`native_tls::TlsAcceptor`] or [`rustls::ServerConfig`],
/// providing an async `accept` method.
///
/// [`native_tls::TlsAcceptor`]: https://docs.rs/native-tls/latest/native_tls/struct.TlsAcceptor.html
/// [`rustls::ServerConfig`]: https://docs.rs/rustls/latest/rustls/server/struct.ServerConfig.html
#[derive(Clone)]
pub struct TlsAcceptor(TlsAcceptorInner);
#[cfg(feature = "native-tls")]
impl From<native_tls::TlsAcceptor> for TlsAcceptor {
fn from(value: native_tls::TlsAcceptor) -> Self {
Self(TlsAcceptorInner::NativeTls(value))
}
}
#[cfg(feature = "rustls")]
impl From<std::sync::Arc<rustls::ServerConfig>> for TlsAcceptor {
fn from(value: std::sync::Arc<rustls::ServerConfig>) -> Self {
Self(TlsAcceptorInner::Rustls(value.into()))
}
}
#[cfg(feature = "py-dynamic-openssl")]
impl From<compio_py_dynamic_openssl::SSLContext> for TlsAcceptor {
fn from(value: compio_py_dynamic_openssl::SSLContext) -> Self {
Self(TlsAcceptorInner::PyDynamicOpenSsl(value))
}
}
impl TlsAcceptor {
/// Accepts a new client connection with the provided stream.
///
/// This function will internally call `TlsAcceptor::accept` to connect
/// the stream and returns a future representing the resolution of the
/// connection operation. The returned future will resolve to either
/// `TlsStream<S>` or `Error` depending if it's successful or not.
///
/// This is typically used after a new socket has been accepted from a
/// `TcpListener`. That socket is then passed to this function to perform
/// the server half of accepting a client connection.
pub async fn accept<S: AsyncRead + AsyncWrite + Unpin + 'static>(
&self,
stream: S,
) -> io::Result<TlsStream<S>> {
match &self.0 {
#[cfg(feature = "native-tls")]
TlsAcceptorInner::NativeTls(c) => {
handshake_native_tls(c.accept(SyncStream::new(stream))).await
}
#[cfg(feature = "rustls")]
TlsAcceptorInner::Rustls(c) => {
let server = c.accept(Box::pin(AsyncStream::new(stream))).await?;
Ok(TlsStream::from(server))
}
#[cfg(feature = "py-dynamic-openssl")]
TlsAcceptorInner::PyDynamicOpenSsl(a) => {
crate::py_ossl::handshake(a.accept(SyncStream::new(stream))).await
}
#[cfg(not(any(
feature = "native-tls",
feature = "rustls",
feature = "py-dynamic-openssl"
)))]
TlsAcceptorInner::None(f) => match *f {},
}
}
}
#[cfg(feature = "native-tls")]
async fn handshake_native_tls<S: AsyncRead + AsyncWrite>(
mut res: Result<
native_tls::TlsStream<SyncStream<S>>,
native_tls::HandshakeError<SyncStream<S>>,
>,
) -> io::Result<TlsStream<S>> {
use native_tls::HandshakeError;
loop {
match res {
Ok(mut s) => {
let inner = s.get_mut();
if inner.has_pending_write() {
inner.flush_write_buf().await?;
}
return Ok(TlsStream::from(s));
}
Err(e) => match e {
HandshakeError::Failure(e) => return Err(io::Error::other(e)),
HandshakeError::WouldBlock(mut mid_stream) => {
let s = mid_stream.get_mut();
if s.has_pending_write() {
s.flush_write_buf().await?;
} else {
s.fill_read_buf().await?;
}
res = mid_stream.handshake();
}
},
}
}
}