use std::net::TcpStream;
use anyhow::Result;
use async_native_tls::{Certificate, TlsConnector};
use smol::{future, io, Async, Unblock};
fn main() -> Result<()> {
let mut builder = native_tls::TlsConnector::builder();
builder.add_root_certificate(Certificate::from_pem(include_bytes!("certificate.pem"))?);
let tls = TlsConnector::from(builder);
smol::run(async {
let stdin = Unblock::new(std::io::stdin());
let mut stdout = Unblock::new(std::io::stdout());
let stream = Async::<TcpStream>::connect(([127, 0, 0, 1], 7001)).await?;
let stream = tls.connect("127.0.0.1", stream).await?;
println!("Connected to {}", stream.get_ref().get_ref().peer_addr()?);
println!("Type a message and hit enter!\n");
let stream = async_dup::Mutex::new(stream);
future::try_join(
io::copy(stdin, &mut &stream),
io::copy(&stream, &mut stdout),
)
.await?;
Ok(())
})
}