Initial commit
This commit is contained in:
57
src/main.rs
Normal file
57
src/main.rs
Normal file
@ -0,0 +1,57 @@
|
||||
use std::error::Error;
|
||||
use clap::Parser;
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
use tokio::net::TcpListener;
|
||||
|
||||
/// Simple program that proxify requests and save responses
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version, about, long_about = None)]
|
||||
struct Args {
|
||||
/// The address the server will listen to
|
||||
#[arg(short, long, default_value = "0.0.0.0:8000")]
|
||||
listen_address: String,
|
||||
|
||||
/// Upstream address this server will connect to
|
||||
#[arg(short, long, default_value = "httpbin.org")]
|
||||
upstream: String,
|
||||
|
||||
/// The path on the server this server will save requests and responses
|
||||
#[arg(short, long, default_value = "storage")]
|
||||
storage_path: String,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> { env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
||||
|
||||
let args = Args::parse();
|
||||
|
||||
log::info!("Will start to listen on {}", args.listen_address);
|
||||
let listener = TcpListener::bind(&args.listen_address).await?;
|
||||
|
||||
|
||||
loop {
|
||||
// Asynchronously wait for an inbound socket.
|
||||
let (mut socket, _) = listener.accept().await?;
|
||||
|
||||
tokio::spawn(async move {
|
||||
let mut buf = vec![0; 1024];
|
||||
|
||||
// In a loop, read data from the socket and write the data back.
|
||||
loop {
|
||||
let n = socket
|
||||
.read(&mut buf)
|
||||
.await
|
||||
.expect("failed to read data from socket");
|
||||
|
||||
if n == 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
socket
|
||||
.write_all(&buf[0..n])
|
||||
.await
|
||||
.expect("failed to write data to socket");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user