1 pub mod pb { 2 tonic::include_proto!("grpc.examples.echo"); 3 } 4 5 use pb::{echo_client::EchoClient, EchoRequest}; 6 use tonic::transport::{Certificate, Channel, ClientTlsConfig, Identity}; 7 8 #[tokio::main] 9 async fn main() -> Result<(), Box<dyn std::error::Error>> { 10 let server_root_ca_cert = tokio::fs::read("examples/data/tls/ca.pem").await?; 11 let server_root_ca_cert = Certificate::from_pem(server_root_ca_cert); 12 let client_cert = tokio::fs::read("examples/data/tls/client1.pem").await?; 13 let client_key = tokio::fs::read("examples/data/tls/client1.key").await?; 14 let client_identity = Identity::from_pem(client_cert, client_key); 15 16 let tls = ClientTlsConfig::new() 17 .domain_name("localhost") 18 .ca_certificate(server_root_ca_cert) 19 .identity(client_identity); 20 21 let channel = Channel::from_static("http://[::1]:50051") 22 .tls_config(tls) 23 .connect() 24 .await?; 25 26 let mut client = EchoClient::new(channel); 27 28 let request = tonic::Request::new(EchoRequest { 29 message: "hello".into(), 30 }); 31 32 let response = client.unary_echo(request).await?; 33 34 println!("RESPONSE={:?}", response); 35 36 Ok(()) 37 } 38