diff --git a/Cargo.lock b/Cargo.lock index 18c0263..843cfe4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1662,6 +1662,7 @@ version = "0.1.0" dependencies = [ "anyhow", "base64 0.12.3", + "clap", "futures", "reqwest", "rmp-serde", diff --git a/crates/ptth_forwarding/Cargo.toml b/crates/ptth_forwarding/Cargo.toml index fc9c862..565a332 100644 --- a/crates/ptth_forwarding/Cargo.toml +++ b/crates/ptth_forwarding/Cargo.toml @@ -9,6 +9,7 @@ edition = "2018" [dependencies] anyhow = "1.0.34" base64 = "0.12.3" +clap = "2.33.3" futures = "0.3.7" reqwest = { version = "0.10.8", features = ["stream"] } rmp-serde = "0.14.4" diff --git a/crates/ptth_forwarding/src/main.rs b/crates/ptth_forwarding/src/main.rs index 57ccd0e..917b5be 100644 --- a/crates/ptth_forwarding/src/main.rs +++ b/crates/ptth_forwarding/src/main.rs @@ -1,16 +1,43 @@ +use clap::{App, SubCommand}; use reqwest::Client; use tokio::{ io::AsyncWriteExt, - net::TcpStream, + net::{ + TcpStream, + TcpListener, + }, stream::StreamExt, }; #[tokio::main] async fn main () -> anyhow::Result <()> { + let matches = App::new ("ptth_forwarding") + .author ("Trish") + .about ("Terminator for PTTH port forwarding") + .subcommand ( + SubCommand::with_name ("server") + .about ("Run this on the host with the TCP server") + ) + .subcommand ( + SubCommand::with_name ("client") + .about ("Run this on the host with the TCP client") + ) + .get_matches (); + let client = Client::builder () .build ()?; - let mut tcp_stream = TcpStream::connect ("127.0.0.1:4010").await?; + let mut tcp_stream = if let Some (matches) = matches.subcommand_matches ("server") { + TcpStream::connect ("127.0.0.1:4010").await? + } + else if let Some (matches) = matches.subcommand_matches ("client") { + let mut listener = TcpListener::bind ("127.0.0.1:4020").await?; + let (stream, _addr) = listener.accept ().await?; + stream + } + else { + panic! ("Must use server or client subcommand."); + }; let resp = client.get ("http://127.0.0.1:4003/").send ().await?; let mut downstream = resp.bytes_stream ();