feat: v0.1.0 init commit
This commit is contained in:
5
.gitignore
vendored
5
.gitignore
vendored
@@ -1,3 +1,4 @@
|
|||||||
|
.idea/
|
||||||
# ---> macOS
|
# ---> macOS
|
||||||
# General
|
# General
|
||||||
.DS_Store
|
.DS_Store
|
||||||
@@ -32,10 +33,6 @@ Temporary Items
|
|||||||
# will have compiled files and executables
|
# will have compiled files and executables
|
||||||
debug/
|
debug/
|
||||||
target/
|
target/
|
||||||
|
|
||||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
|
||||||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
|
||||||
Cargo.lock
|
|
||||||
|
|
||||||
# These are backup files generated by rustfmt
|
# These are backup files generated by rustfmt
|
||||||
**/*.rs.bk
|
**/*.rs.bk
|
||||||
|
|||||||
1643
Cargo.lock
generated
Normal file
1643
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
20
Cargo.toml
Normal file
20
Cargo.toml
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
[package]
|
||||||
|
name = "simple-rust-http3-proxy"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
tokio = { version = "1", features = ["full"] }
|
||||||
|
s2n-quic = "1.9"
|
||||||
|
clap = "2.33"
|
||||||
|
rust_util = "0.6"
|
||||||
|
|
||||||
|
[profile.release]
|
||||||
|
codegen-units = 1
|
||||||
|
opt-level = 'z'
|
||||||
|
lto = true
|
||||||
|
panic = 'abort'
|
||||||
|
|
||||||
|
|
||||||
33
src/client.rs
Normal file
33
src/client.rs
Normal 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
5
src/config.rs
Normal 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
42
src/main.rs
Normal 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
39
src/server.rs
Normal 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(())
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user