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
use std::{
io,
sync::Arc,
};
use futures::Future;
use rustls::ClientConfig;
use tokio_io::{AsyncRead, AsyncWrite};
use tokio_postgres::tls::{ChannelBinding, MakeTlsConnect, TlsConnect};
use tokio_rustls::{client::TlsStream, TlsConnector};
use webpki::{DNSName, DNSNameRef};
pub struct MakeRustlsConnect {
config: Arc<ClientConfig>,
}
impl MakeRustlsConnect {
pub fn new(config: ClientConfig) -> Self {
Self { config: Arc::new(config) }
}
}
impl<S> MakeTlsConnect<S> for MakeRustlsConnect
where
S: AsyncRead + AsyncWrite + Send + 'static
{
type Stream = TlsStream<S>;
type TlsConnect = RustlsConnect;
type Error = io::Error;
fn make_tls_connect(&mut self, hostname: &str) -> Result<RustlsConnect, Self::Error> {
DNSNameRef::try_from_ascii_str(hostname)
.map(|dns_name| RustlsConnect {
hostname: dns_name.to_owned(),
connector: Arc::clone(&self.config).into(),
})
.map_err(|_| io::ErrorKind::InvalidInput.into())
}
}
pub struct RustlsConnect {
hostname: DNSName,
connector: TlsConnector,
}
impl<S> TlsConnect<S> for RustlsConnect
where
S: AsyncRead + AsyncWrite + Send + 'static
{
type Stream = TlsStream<S>;
type Error = io::Error;
type Future = Box<dyn Future<Item=(Self::Stream, ChannelBinding), Error=Self::Error> + Send>;
fn connect(self, stream: S) -> Self::Future {
Box::new(
self.connector.connect(self.hostname.as_ref(), stream)
.map(|s| (s, ChannelBinding::none()))
)
}
}
#[cfg(test)]
mod tests {
use futures::{Future, Stream};
use tokio::runtime::current_thread;
#[test]
fn it_works() {
let config = rustls::ClientConfig::new();
let tls = super::MakeRustlsConnect::new(config);
current_thread::block_on_all(
tokio_postgres::connect("sslmode=require host=localhost user=postgres", tls)
.map(|(client, connection)| {
tokio::spawn(
connection.map_err(|e| panic!("{:?}", e))
);
client
})
.and_then(|mut client| {
client.prepare("SELECT 1")
.map(|s| (client, s))
})
.and_then(|(mut client, statement)| {
client.query(&statement, &[]).collect()
})
).unwrap();
}
}