Debian Codegraph @

crt/s2n/bindings/rust-examples/tokio-server-client/src/bin/client.rs

raw symbols SCIP
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 use clap::Parser;
5 use s2n_tls::{config::Config, security::DEFAULT_TLS13};
6 use s2n_tls_tokio::TlsConnector;
7 use std::{error::Error, fs};
8 use tokio::{io::AsyncWriteExt, net::TcpStream};
9
10 /// NOTE: this ca is to be used for demonstration purposes only!
11 const DEFAULT_CA: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/../certs/ca-cert.pem");
12
13 #[derive(Parser, Debug)]
14 struct Args {
15 #[clap(short, long, default_value_t = String::from(DEFAULT_CA))]
16 trust: String,
17 addr: String,
18 }
19
20 async fn run_client(trust_pem: &[u8], addr: &str) -> Result<(), Box<dyn Error>> {
21 // Set up the configuration for new connections.
22 // Minimally you will need a trust store.
23 let mut config = Config::builder();
24 config.set_security_policy(&DEFAULT_TLS13)?;
25 config.trust_pem(trust_pem)?;
26
27 // Create the TlsConnector based on the configuration.
28 let client = TlsConnector::new(config.build()?);
29
30 // Connect to the server.
31 let stream = TcpStream::connect(addr).await?;
32 let tls = client.connect("www.kangaroo.com", stream).await?;
33 println!("{:#?}", tls);
34
35 // Split the stream.
36 // This allows us to call read and write from different tasks.
37 let (mut reader, mut writer) = tokio::io::split(tls);
38
39 // Copy data from the server to stdout
40 tokio::spawn(async move {
41 let mut stdout = tokio::io::stdout();
42 tokio::io::copy(&mut reader, &mut stdout).await
43 });
44
45 // Send data from stdin to the server
46 let mut stdin = tokio::io::stdin();
47 tokio::io::copy(&mut stdin, &mut writer).await?;
48 writer.shutdown().await?;
49
50 Ok(())
51 }
52
53 #[tokio::main]
54 async fn main() -> Result<(), Box<dyn Error>> {
55 let args = Args::parse();
56 let trust_pem = fs::read(args.trust)?;
57 run_client(&trust_pem, &args.addr).await?;
58 Ok(())
59 }