feat: v0.1.0 init commit

This commit is contained in:
2022-08-21 01:05:48 +08:00
parent 82ddc6d8c8
commit 21c857bdf4
7 changed files with 1783 additions and 4 deletions

33
src/client.rs Normal file
View File

@@ -0,0 +1,33 @@
use std::fs;
use std::net::SocketAddr;
use rust_util::XResult;
use s2n_quic::Client;
use s2n_quic::client::Connect;
use crate::config::ListenConfig;
async fn run(listen_config: &ListenConfig) -> XResult<()> {
let cert_pem = opt_result!(fs::read_to_string(&listen_config.cert_pem_file),
"Read cert pem file: {}, failed: {}", &listen_config.key_pem_file);
let client = Client::builder()
.with_tls(cert_pem.as_str())?
.with_io("0.0.0.0:0")?
.start()?;
let addr: SocketAddr = "127.0.0.1:4433".parse()?;
let connect = Connect::new(addr).with_server_name("localhost");
let mut connection = client.connect(connect).await?;
// ensure the connection doesn't time out with inactivity
connection.keep_alive(true)?;
// open a new stream and split the receiving and sending sides
let stream = connection.open_bidirectional_stream().await?;
let (mut receive_stream, mut send_stream) = stream.split();
// spawn a task that copies responses from the server to stdout
tokio::spawn(async move {
let mut stdout = tokio::io::stdout();
let _ = tokio::io::copy(&mut receive_stream, &mut stdout).await;
});
// copy data from stdin and send it to the server
let mut stdin = tokio::io::stdin();
tokio::io::copy(&mut stdin, &mut send_stream).await?;
Ok(())
}