]> git.lizzy.rs Git - connect-rs.git/blob - examples/tls-client/src/main.rs
refactor to have datagram already serialized in memory
[connect-rs.git] / examples / tls-client / src / main.rs
1 use connect::{ConnectDatagram, Connection, SinkExt, StreamExt};
2 use log::*;
3 use rustls::ClientConfig;
4 use std::env;
5
6 #[async_std::main]
7 async fn main() -> anyhow::Result<()> {
8     env_logger::init();
9
10     // get ip address and domain from cmd line args
11     let (ip_addr, domain, cafile_path) = parse_args();
12
13     // construct `rustls` client config
14     let client_config = create_client_config(cafile_path)?;
15
16     // create a client connection to the server
17     let mut conn = Connection::tls_client(ip_addr, &domain, client_config.into()).await?;
18
19     // send a message to the server
20     let msg = String::from("Hello world");
21     info!("Sending message: {}", msg);
22
23     let envelope = ConnectDatagram::with_tag(65535, msg.into_bytes())?;
24     conn.writer().send(envelope).await?;
25
26     // wait for the server to reply with an ack
27     if let Some(reply) = conn.reader().next().await {
28         let data = reply.data().to_vec();
29         let msg = String::from_utf8(data)?;
30
31         info!("Received message: {}", msg);
32     }
33
34     Ok(())
35 }
36
37 fn create_client_config(path: String) -> anyhow::Result<ClientConfig> {
38     let cafile = std::fs::read(path)?;
39
40     let mut client_pem = std::io::Cursor::new(cafile);
41
42     let mut client_config = ClientConfig::new();
43     client_config
44         .root_store
45         .add_pem_file(&mut client_pem)
46         .map_err(|_| std::io::Error::new(std::io::ErrorKind::InvalidInput, "invalid cert"))?;
47
48     Ok(client_config)
49 }
50
51 fn parse_args() -> (String, String, String) {
52     let args: Vec<String> = env::args().collect();
53
54     let ip_address = match args.get(1) {
55         Some(addr) => addr,
56         None => {
57             error!("Need to pass IP address to connect to as first command line argument");
58             panic!();
59         }
60     };
61
62     let domain = match args.get(2) {
63         Some(d) => d,
64         None => {
65             error!("Need to pass domain name as second command line argument");
66             panic!();
67         }
68     };
69
70     let cafile_path = match args.get(3) {
71         Some(d) => d,
72         None => {
73             error!("Need to pass path to cafile as third command line argument");
74             panic!();
75         }
76     };
77
78     (
79         ip_address.to_string(),
80         domain.to_string(),
81         cafile_path.to_string(),
82     )
83 }