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(())
}

5
src/config.rs Normal file
View File

@@ -0,0 +1,5 @@
pub struct ListenConfig {
pub listen: String,
pub key_pem_file: String,
pub cert_pem_file: String,
}

42
src/main.rs Normal file
View File

@@ -0,0 +1,42 @@
#[macro_use]
extern crate rust_util;
mod config;
mod client;
mod server;
use clap::{App, Arg};
use rust_util::XResult;
#[tokio::main]
async fn main() -> XResult<()> {
let matches = App::new("Simple Rust HTTP/3 Proxy")
.version(env!("CARGO_PKG_VERSION"))
.about(env!("CARGO_PKG_DESCRIPTION"))
.arg(Arg::with_name("version").short("V").long("version").help("Print version"))
.arg(Arg::with_name("verbose").short("v").long("verbose").help("Verbose"))
.arg(Arg::with_name("server").short("s").long("server").help("Run server mode"))
.arg(Arg::with_name("client").short("c").long("client").help("Run client mode"))
.arg(Arg::with_name("config").short("c").long("config").takes_value(true).help("HTTP/3 listen config file"))
.get_matches();
if matches.is_present("version") {
information!("{} v{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
return Ok(());
}
if matches.is_present("verbose") {
debugging!("HTTP/3 listen config: {:#?}", matches);
}
let server_mode = matches.is_present("server");
let client_mode = matches.is_present("client");
if server_mode && client_mode {
failure_and_exit!("Cannot run in both server and client mode");
}
if !server_mode && !client_mode {
failure_and_exit!("Must run in server on client mode")
}
// TODO
Ok(())
}

39
src/server.rs Normal file
View File

@@ -0,0 +1,39 @@
use std::fs;
use rust_util::XResult;
use s2n_quic::Server;
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 key_pem = opt_result!(fs::read_to_string(&listen_config.key_pem_file),
"Read key pem file :{}, failed: {}", &listen_config.key_pem_file);
let mut server = Server::builder()
.with_tls((cert_pem.as_str(), key_pem.as_str()))?
.with_io(listen_config.listen.as_str())?
.start()?;
while let Some(mut connection) = server.accept().await {
// spawn a new task for the connection
tokio::spawn(async move {
information!("Connection accepted from {:?}", connection.remote_addr());
while let Ok(Some(mut stream)) = connection.accept_bidirectional_stream().await {
// spawn a new task for the stream
tokio::spawn(async move {
information!("Stream opened from {:?}", stream.connection().remote_addr());
// echo any data back to the stream
// while let Ok(Some(data)) = stream.receive().await {
// stream.send(data).await.expect("stream should be open");
// }
// println!("Stream closed from {:?}", stream.connection().remote_addr());
// TODO ...
});
}
println!("Connection closed from {:?}", connection.remote_addr());
});
}
Ok(())
}