3 use crate::schema::hello_world::HelloWorld;
4 use async_std::{io, task};
5 use connect::tls::rustls::internal::pemfile::{certs, rsa_private_keys};
6 use connect::tls::rustls::{NoClientAuth, ServerConfig};
7 use connect::tls::TlsServer;
8 use connect::{SinkExt, StreamExt};
12 use std::io::BufReader;
15 async fn main() -> anyhow::Result<()> {
18 // Get ip address from cmd line args
19 let (ip_address, cert_path, key_path) = parse_args();
21 let certs = certs(&mut BufReader::new(File::open(cert_path)?))
22 .map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid cert"))?;
24 let mut keys = rsa_private_keys(&mut BufReader::new(File::open(key_path)?))
25 .map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid key"))?;
27 let mut config = ServerConfig::new(NoClientAuth::new());
29 .set_single_cert(certs, keys.remove(0))
30 .map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?;
33 let mut server = TlsServer::new(ip_address, config.into())?;
35 // handle server connections
36 // wait for a connection to come in and be accepted
37 while let Some(mut conn) = server.next().await {
38 info!("Handling connection from {}", conn.peer_addr());
40 task::spawn(async move {
41 while let Some(msg) = conn.reader().next().await {
42 if msg.is::<HelloWorld>() {
43 if let Ok(Some(contents)) = msg.unpack::<HelloWorld>() {
45 "Received a message \"{}\" from {}",
46 contents.get_message(),
53 .expect("Could not send message back to source connection");
54 info!("Sent message back to original sender");
57 error!("Received a message of unknown type")
66 fn parse_args() -> (String, String, String) {
67 let args: Vec<String> = env::args().collect();
69 let ip_address = match args.get(1) {
72 error!("Need to pass IP address to connect to as first command line argument");
77 let cert_path = match args.get(2) {
80 error!("Need to pass path to cert file as second command line argument");
85 let key_path = match args.get(3) {
88 error!("Need to pass path to key file as third command line argument");
94 ip_address.to_string(),
95 cert_path.to_string(),