forked from ntex-rs/ntex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrustls-client.rs
39 lines (32 loc) · 1.25 KB
/
rustls-client.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
use std::io;
use ntex::{codec, connect, io::types::PeerAddr, util::Bytes, util::Either};
use tls_rust::{ClientConfig, RootCertStore};
#[ntex::main]
async fn main() -> io::Result<()> {
std::env::set_var("RUST_LOG", "trace");
env_logger::init();
// rustls config
let cert_store =
RootCertStore::from_iter(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
let config = ClientConfig::builder()
.with_root_certificates(cert_store)
.with_no_client_auth();
// rustls connector
let connector = connect::rustls::Connector::new(config.clone());
//let io = connector.connect("www.rust-lang.org:443").await.unwrap();
let io = connector.connect("127.0.0.1:8443").await.unwrap();
println!("Connected to tls server {:?}", io.query::<PeerAddr>().get());
let result = io
.send(Bytes::from_static(b"GET /\r\n\r\n"), &codec::BytesCodec)
.await
.map_err(Either::into_inner)?;
println!("Send result: {:?}", result);
let resp = io
.recv(&codec::BytesCodec)
.await
.map_err(|e| e.into_inner())?
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "disconnected"))?;
println!("Received: {:?}", resp);
println!("disconnecting");
io.shutdown().await
}