-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
46 lines (42 loc) · 1.46 KB
/
lib.rs
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
pub mod tls;
mod stream;
pub use tokio_tungstenite::{WebSocketStream, tungstenite};
pub use crate::stream::MaybeTlsStream;
use std::io;
use tokio::io::{AsyncRead, AsyncWrite};
use tokio_rustls::{TlsConnector, TlsAcceptor};
use tungstenite::protocol::{Role, WebSocketConfig};
pub async fn connect<S>(
stream: S,
tls_connector: Option<&TlsConnector>,
tls_domain: Option<&str>,
config: Option<WebSocketConfig>
) -> io::Result<WebSocketStream<MaybeTlsStream<S>>>
where
S: AsyncRead + AsyncWrite + Unpin,
{
let stream = if let Some(connector) = tls_connector {
let domain = tokio_rustls::rustls::ServerName::try_from(tls_domain.unwrap())
.map_err(|_| io::Error::new(io::ErrorKind::Other, "invalid dns name"))?;
MaybeTlsStream::ClientTls(connector.connect(domain, stream).await?)
} else {
MaybeTlsStream::Plain(stream)
};
Ok(WebSocketStream::from_raw_socket(stream, Role::Client, config).await)
}
pub async fn accept<S>(
stream: S,
tls_acceptor: Option<&TlsAcceptor>,
config: Option<WebSocketConfig>
) -> io::Result<WebSocketStream<MaybeTlsStream<S>>>
where
S: AsyncRead + AsyncWrite + Unpin,
{
let stream = if let Some(acceptor) = tls_acceptor {
let sess_acceptor = acceptor.clone();
MaybeTlsStream::ServerTls(sess_acceptor.accept(stream).await?)
} else {
MaybeTlsStream::Plain(stream)
};
Ok(WebSocketStream::from_raw_socket(stream, Role::Server, config).await)
}