commit 38b700adcfc5fc9a4727bd2138c1cd13ee584f56 Author: TheClashFruit Date: Tue Sep 19 21:29:07 2023 +0200 help me diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/JetClient/state.xml b/.idea/JetClient/state.xml new file mode 100644 index 0000000..f1a48a7 --- /dev/null +++ b/.idea/JetClient/state.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/TCPRelay.iml b/.idea/TCPRelay.iml new file mode 100644 index 0000000..cf84ae4 --- /dev/null +++ b/.idea/TCPRelay.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/discord.xml b/.idea/discord.xml new file mode 100644 index 0000000..d8e9561 --- /dev/null +++ b/.idea/discord.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..6947a7e --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..de08e58 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,176 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "TCPRelay" +version = "0.1.0" +dependencies = [ + "anyhow", + "clap", +] + +[[package]] +name = "anstream" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f58811cfac344940f1a400b6e6231ce35171f614f26439e80f8c1465c5cc0c" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b84bf0a05bbb2a83e5eb6fa36bb6e87baa08193c35ff52bbf6b38d8af2890e46" + +[[package]] +name = "anstyle-parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "938874ff5980b03a87c5524b3ae5b59cf99b1d6bc836848df7bc5ada9643c333" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58f54d10c6dfa51283a066ceab3ec1ab78d13fae00aa49243a45e4571fb79dfd" +dependencies = [ + "anstyle", + "windows-sys", +] + +[[package]] +name = "anyhow" +version = "1.0.75" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" + +[[package]] +name = "clap" +version = "4.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1d7b8d5ec32af0fadc644bf1fd509a688c2103b185644bb1e29d164e0703136" +dependencies = [ + "clap_builder", +] + +[[package]] +name = "clap_builder" +version = "4.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5179bb514e4d7c2051749d8fcefa2ed6d06a9f4e6d69faf3805f5d80b8cf8d56" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_lex" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd7cc57abe963c6d3b9d8be5b06ba7c8957a930305ca90304f24ef040aa6f961" + +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..6e23047 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "TCPRelay" +version = "0.1.0" +edition = "2021" +authors = ["TheClashFruit "] + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +clap = "^4.2.1" +anyhow = "^1.0.57" \ No newline at end of file diff --git a/src/commands/mod.rs b/src/commands/mod.rs new file mode 100644 index 0000000..ffabba1 --- /dev/null +++ b/src/commands/mod.rs @@ -0,0 +1,17 @@ +use clap::{ArgMatches, Command}; +use anyhow::Result; + +pub fn builtin() -> Vec { + vec![ + relay::cli(), + ] +} + +pub fn builtin_exec(cmd: &str) -> Option Result<()>> { + Some(match cmd { + "relay" => relay::exec, + _ => return None + }) +} + +pub mod relay; \ No newline at end of file diff --git a/src/commands/relay.rs b/src/commands/relay.rs new file mode 100644 index 0000000..46ad929 --- /dev/null +++ b/src/commands/relay.rs @@ -0,0 +1,102 @@ +use std::io::{Read, Write}; +use std::net::{Shutdown, TcpListener, TcpStream}; +use std::thread::Thread; +use clap::{Arg, ArgMatches, Command}; +use anyhow::Result; + +pub fn cli() -> Command { + Command::new("relay") + .dont_collapse_args_in_usage(true) + .about("Relay TCP connections.") + .args(&[ + Arg::new("source") + .help("Source Port") + .short('s') + .long("source") + .required(true), + Arg::new("exit") + .help("Exit Port") + .short('e') + .long("exit") + .required(true), + Arg::new("host") + .help("Ip address of the server to relay to.") + .short('d') + .long("host"), + Arg::new("bind") + .help("Ip address to bind to.") + .short('b') + .long("bind") + ]) + .after_help("Run `tcp relay -h` for more detailed information.\n") +} + +pub fn exec(args: &ArgMatches) -> Result<()> { + let port_in = args.get_one::("source").unwrap(); + let port_exit = args.get_one::("exit").unwrap(); + + let host = match args.get_one::("host") { + Some(host) => host, + None => "127.0.0.1" + }; + + let bind = match args.get_one::("bind") { + Some(bind) => bind, + None => "0.0.0.0" + }; + + let listener = TcpListener::bind(format!("{}:{}", bind, port_in))?; + + println!("Listening | {}:{}", bind, port_in); + + for stream in listener.incoming() { + match stream { + Ok(mut client) => { + println!("New Connection | {}", client.peer_addr().unwrap()); + + let mut server = TcpStream::connect(format!("{}:{}", host, port_exit))?; + + println!("Connected to Remote Server | {}", server.peer_addr().unwrap()); + + let client_to_target = forward(client.try_clone().unwrap(), server.try_clone().unwrap()); + let target_to_client = forward(server, client); + + std::thread::spawn(move || { + client_to_target.join().unwrap(); + }); + + std::thread::spawn(move || { + target_to_client.join().unwrap(); + }); + } + Err(e) => { + eprintln!("Error (FUCK YOU) | {}", e); + } + } + } + + Ok(()) +} + +fn forward(mut source: TcpStream, mut destination: TcpStream) -> std::thread::JoinHandle<()> { + std::thread::spawn(move || { + let mut buffer = [0; 1024]; + loop { + match source.read(&mut buffer) { + Ok(0) => break, + Ok(n) => { + if let Err(e) = destination.write_all(&buffer[..n]) { + eprintln!("Error while fucking with data (write) | {}", e); + + break; + } + } + Err(e) => { + eprintln!("Error while fucking with data (read) | {}", e); + + break; + } + } + } + }) +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..d947f77 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,42 @@ +extern crate core; + +use std::process::{ExitCode}; +use anyhow::{Result, Context}; +use clap::{Command}; + +mod commands; + +fn try_main() -> Result<()> { + let args = Command::new("tcp") + .about("no u") + .arg_required_else_help(true) + .subcommand_required(true) + .subcommands(commands::builtin()) + .get_matches(); + + if let Some((subcommand, args)) = args.subcommand() { + let func = commands::builtin_exec(subcommand) + .context(format!("Subcommand {} does not exist", subcommand))?; + + func(args)?; + } + + Ok(()) +} + +fn main() -> ExitCode { + if let Err(err) = try_main() { + eprintln!("\nerror: {}", err); + eprintln!(); + eprintln!("Caused by:"); + err.chain().skip(1) + .for_each(|err_item| { + eprintln!(" {}", err_item); + }); + eprintln!(); + + ExitCode::FAILURE + } else { + ExitCode::SUCCESS + } +} \ No newline at end of file