smol/examples/tls-server.rs

32 lines
939 B
Rust
Raw Normal View History

2020-04-21 15:51:07 +00:00
// TODO: document
2020-04-05 11:50:09 +00:00
use std::net::{TcpListener, TcpStream};
use anyhow::Result;
2020-04-06 20:19:54 +00:00
use async_native_tls::{Identity, TlsAcceptor, TlsStream};
2020-04-05 11:50:09 +00:00
use futures::io;
2020-04-21 15:51:07 +00:00
use piper::Mutex;
2020-04-06 20:19:54 +00:00
use smol::{Async, Task};
2020-04-05 11:50:09 +00:00
async fn echo(stream: TlsStream<Async<TcpStream>>) -> Result<()> {
println!("Copying");
2020-04-21 15:51:07 +00:00
let stream = Mutex::new(stream);
2020-04-05 11:50:09 +00:00
io::copy(&stream, &mut &stream).await?;
Ok(())
}
fn main() -> Result<()> {
2020-04-06 20:19:54 +00:00
let identity = Identity::from_pkcs12(include_bytes!("../identity.pfx"), "password")?;
let tls = TlsAcceptor::from(native_tls::TlsAcceptor::new(identity)?);
2020-04-05 11:50:09 +00:00
smol::run(async {
let listener = Async::<TcpListener>::bind("127.0.0.1:7001")?;
println!("Listening on {}", listener.get_ref().local_addr()?);
loop {
let (stream, _) = listener.accept().await?;
let stream = tls.accept(stream).await?;
Task::spawn(echo(stream)).unwrap().detach();
}
})
}