This commit is contained in:
TheClashFruit 2023-09-19 21:29:07 +02:00
commit 38b700adcf
Signed by: TheClashFruit
GPG key ID: CF4A319B9A73290C
12 changed files with 395 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

8
.idea/.gitignore vendored Normal file
View file

@ -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

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JetClientState">
<option name="id" value="4dbffdc2-dcb3-48cb-a72b-74a6e78749be" />
</component>
</project>

11
.idea/TCPRelay.iml Normal file
View file

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="EMPTY_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

7
.idea/discord.xml Normal file
View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DiscordProjectSettings">
<option name="show" value="PROJECT_FILES" />
<option name="description" value="" />
</component>
</project>

8
.idea/modules.xml Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/TCPRelay.iml" filepath="$PROJECT_DIR$/.idea/TCPRelay.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

176
Cargo.lock generated Normal file
View file

@ -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"

11
Cargo.toml Normal file
View file

@ -0,0 +1,11 @@
[package]
name = "TCPRelay"
version = "0.1.0"
edition = "2021"
authors = ["TheClashFruit <admin@theclashfruit.me>"]
# 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"

17
src/commands/mod.rs Normal file
View file

@ -0,0 +1,17 @@
use clap::{ArgMatches, Command};
use anyhow::Result;
pub fn builtin() -> Vec<Command> {
vec![
relay::cli(),
]
}
pub fn builtin_exec(cmd: &str) -> Option<fn(&ArgMatches) -> Result<()>> {
Some(match cmd {
"relay" => relay::exec,
_ => return None
})
}
pub mod relay;

102
src/commands/relay.rs Normal file
View file

@ -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::<String>("source").unwrap();
let port_exit = args.get_one::<String>("exit").unwrap();
let host = match args.get_one::<String>("host") {
Some(host) => host,
None => "127.0.0.1"
};
let bind = match args.get_one::<String>("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;
}
}
}
})
}

42
src/main.rs Normal file
View file

@ -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
}
}