1 use connect::tls::rustls::ClientConfig;
2 use connect::{ConnectDatagram, Connection, SinkExt, StreamExt};
7 async fn main() -> anyhow::Result<()> {
10 // get ip address and domain from cmd line args
11 let (ip_addr, domain, cafile_path) = parse_args();
13 // construct `rustls` client config
14 let cafile = std::fs::read(cafile_path)?;
16 let mut client_pem = std::io::Cursor::new(cafile);
18 let mut client_config = ClientConfig::new();
21 .add_pem_file(&mut client_pem)
22 .map_err(|_| std::io::Error::new(std::io::ErrorKind::InvalidInput, "invalid cert"))?;
24 // create a client connection to the server
25 let mut conn = Connection::tls_client(ip_addr, &domain, client_config.into()).await?;
27 // send a message to the server
28 let msg = String::from("Hello world");
29 info!("Sending message: {}", msg);
31 let envelope = ConnectDatagram::new(65535, msg.into_bytes())?;
32 conn.writer().send(envelope).await?;
34 // wait for the server to reply with an ack
35 if let Some(mut reply) = conn.reader().next().await {
36 let data = reply.take_data().unwrap();
37 let msg = String::from_utf8(data)?;
39 info!("Received message: {}", msg);
45 fn parse_args() -> (String, String, String) {
46 let args: Vec<String> = env::args().collect();
48 let ip_address = match args.get(1) {
51 error!("Need to pass IP address to connect to as first command line argument");
56 let domain = match args.get(2) {
59 error!("Need to pass domain name as second command line argument");
64 let cafile_path = match args.get(3) {
67 error!("Need to pass path to cafile as third command line argument");
73 ip_address.to_string(),
75 cafile_path.to_string(),