2020-08-31 14:22:46 +00:00
|
|
|
use argh::FromArgs;
|
2021-09-28 17:01:37 +00:00
|
|
|
use rustls_pemfile::{certs, rsa_private_keys};
|
2019-05-21 16:54:10 +00:00
|
|
|
use std::fs::File;
|
2020-08-31 14:22:46 +00:00
|
|
|
use std::io::{self, BufReader};
|
2017-02-22 03:42:32 +00:00
|
|
|
use std::net::ToSocketAddrs;
|
2020-08-31 14:22:46 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use std::sync::Arc;
|
|
|
|
use tokio::io::{copy, sink, split, AsyncWriteExt};
|
2019-11-27 16:11:02 +00:00
|
|
|
use tokio::net::TcpListener;
|
2021-09-28 17:01:37 +00:00
|
|
|
use tokio_rustls::rustls::{self, Certificate, PrivateKey};
|
2019-05-21 16:54:10 +00:00
|
|
|
use tokio_rustls::TlsAcceptor;
|
|
|
|
|
2020-08-31 14:22:46 +00:00
|
|
|
/// Tokio Rustls server example
|
|
|
|
#[derive(FromArgs)]
|
2019-05-21 16:54:10 +00:00
|
|
|
struct Options {
|
2020-08-31 14:22:46 +00:00
|
|
|
/// bind addr
|
|
|
|
#[argh(positional)]
|
2019-05-21 16:54:10 +00:00
|
|
|
addr: String,
|
|
|
|
|
|
|
|
/// cert file
|
2020-08-31 14:22:46 +00:00
|
|
|
#[argh(option, short = 'c')]
|
2019-05-21 16:54:10 +00:00
|
|
|
cert: PathBuf,
|
|
|
|
|
|
|
|
/// key file
|
2020-08-31 14:22:46 +00:00
|
|
|
#[argh(option, short = 'k')]
|
2019-05-21 16:54:10 +00:00
|
|
|
key: PathBuf,
|
|
|
|
|
|
|
|
/// echo mode
|
2020-08-31 14:22:46 +00:00
|
|
|
#[argh(switch, short = 'e')]
|
|
|
|
echo_mode: bool,
|
2017-02-22 03:42:32 +00:00
|
|
|
}
|
|
|
|
|
2019-05-21 16:54:10 +00:00
|
|
|
fn load_certs(path: &Path) -> io::Result<Vec<Certificate>> {
|
|
|
|
certs(&mut BufReader::new(File::open(path)?))
|
|
|
|
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid cert"))
|
2021-09-28 17:01:37 +00:00
|
|
|
.map(|mut certs| certs.drain(..).map(Certificate).collect())
|
2017-02-22 03:42:32 +00:00
|
|
|
}
|
|
|
|
|
2019-05-21 16:54:10 +00:00
|
|
|
fn load_keys(path: &Path) -> io::Result<Vec<PrivateKey>> {
|
|
|
|
rsa_private_keys(&mut BufReader::new(File::open(path)?))
|
|
|
|
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid key"))
|
2021-09-28 17:01:37 +00:00
|
|
|
.map(|mut keys| keys.drain(..).map(PrivateKey).collect())
|
2017-02-22 03:42:32 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 14:22:46 +00:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> io::Result<()> {
|
|
|
|
let options: Options = argh::from_env();
|
2017-02-22 03:42:32 +00:00
|
|
|
|
2020-08-31 14:22:46 +00:00
|
|
|
let addr = options
|
|
|
|
.addr
|
|
|
|
.to_socket_addrs()?
|
2019-05-21 16:54:10 +00:00
|
|
|
.next()
|
|
|
|
.ok_or_else(|| io::Error::from(io::ErrorKind::AddrNotAvailable))?;
|
|
|
|
let certs = load_certs(&options.cert)?;
|
|
|
|
let mut keys = load_keys(&options.key)?;
|
2020-08-31 14:22:46 +00:00
|
|
|
let flag_echo = options.echo_mode;
|
2017-02-22 03:42:32 +00:00
|
|
|
|
2021-09-28 17:01:37 +00:00
|
|
|
let config = rustls::ServerConfig::builder()
|
|
|
|
.with_safe_defaults()
|
|
|
|
.with_no_client_auth()
|
|
|
|
.with_single_cert(certs, keys.remove(0))
|
2019-05-21 16:54:10 +00:00
|
|
|
.map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?;
|
|
|
|
let acceptor = TlsAcceptor::from(Arc::new(config));
|
|
|
|
|
2020-10-16 10:26:32 +00:00
|
|
|
let listener = TcpListener::bind(&addr).await?;
|
2019-05-21 16:54:10 +00:00
|
|
|
|
2020-08-31 14:22:46 +00:00
|
|
|
loop {
|
|
|
|
let (stream, peer_addr) = listener.accept().await?;
|
|
|
|
let acceptor = acceptor.clone();
|
2019-05-21 16:54:10 +00:00
|
|
|
|
2020-08-31 14:22:46 +00:00
|
|
|
let fut = async move {
|
|
|
|
let mut stream = acceptor.accept(stream).await?;
|
2019-05-21 16:54:10 +00:00
|
|
|
|
2020-08-31 14:22:46 +00:00
|
|
|
if flag_echo {
|
|
|
|
let (mut reader, mut writer) = split(stream);
|
|
|
|
let n = copy(&mut reader, &mut writer).await?;
|
|
|
|
writer.flush().await?;
|
|
|
|
println!("Echo: {} - {}", peer_addr, n);
|
|
|
|
} else {
|
|
|
|
let mut output = sink();
|
|
|
|
stream
|
|
|
|
.write_all(
|
2019-05-21 16:54:10 +00:00
|
|
|
&b"HTTP/1.0 200 ok\r\n\
|
2020-08-31 14:22:46 +00:00
|
|
|
Connection: close\r\n\
|
|
|
|
Content-length: 12\r\n\
|
|
|
|
\r\n\
|
|
|
|
Hello world!"[..],
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
stream.shutdown().await?;
|
|
|
|
copy(&mut stream, &mut output).await?;
|
|
|
|
println!("Hello: {}", peer_addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(()) as io::Result<()>
|
|
|
|
};
|
|
|
|
|
|
|
|
tokio::spawn(async move {
|
|
|
|
if let Err(err) = fut.await {
|
|
|
|
eprintln!("{:?}", err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-02-22 03:42:32 +00:00
|
|
|
}
|