ptth/crates/ptth_forwarding/src/main.rs

52 lines
1.2 KiB
Rust

use clap::{App, SubCommand};
use reqwest::Client;
use tokio::{
io::AsyncWriteExt,
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 = 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 ();
while let Some (Ok (item)) = downstream.next ().await {
println! ("Chunk: {:?}", item);
tcp_stream.write_all (&item).await?;
}
Ok (())
}