diff --git a/client/Cargo.lock b/client/Cargo.lock index c2b7bfe..d39f2a8 100644 --- a/client/Cargo.lock +++ b/client/Cargo.lock @@ -1,19 +1,115 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + [[package]] name = "cfg-if" version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +[[package]] +name = "clap" +version = "3.2.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71655c45cb9845d3270c9d6df84ebe72b4dad3c2ba3f7023ad47c144e4e473a5" +dependencies = [ + "atty", + "bitflags", + "clap_derive", + "clap_lex", + "indexmap", + "once_cell", + "strsim", + "termcolor", + "textwrap", +] + +[[package]] +name = "clap_derive" +version = "3.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea0c8bce528c4be4da13ea6fead8965e95b6073585a2f05204bd8f4119f82a65" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" +dependencies = [ + "os_str_bytes", +] + [[package]] name = "client" version = "0.1.0" dependencies = [ + "clap", "net2", "socket2", ] +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + +[[package]] +name = "heck" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] +name = "indexmap" +version = "1.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399" +dependencies = [ + "autocfg", + "hashbrown", +] + [[package]] name = "libc" version = "0.2.105" @@ -31,6 +127,60 @@ dependencies = [ "winapi", ] +[[package]] +name = "once_cell" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860" + +[[package]] +name = "os_str_bytes" +version = "6.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee" + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro2" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" +dependencies = [ + "proc-macro2", +] + [[package]] name = "socket2" version = "0.4.2" @@ -41,6 +191,50 @@ dependencies = [ "winapi", ] +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + +[[package]] +name = "syn" +version = "1.0.103" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a864042229133ada95abf3b54fdc62ef5ccabe9515b64717bcb9a1919e59445d" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "termcolor" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "textwrap" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d" + +[[package]] +name = "unicode-ident" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + [[package]] name = "winapi" version = "0.3.9" @@ -57,6 +251,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/client/Cargo.toml b/client/Cargo.toml index 977650e..807e251 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -8,4 +8,5 @@ edition = "2018" [dependencies] net2 = "0.2.37" -socket2 = {version="0.4.2", features=["all"]} \ No newline at end of file +socket2 = {version="0.4.2", features=["all"]} +clap = { version = "3.1.18", features = ["derive"] } \ No newline at end of file diff --git a/client/src/main.rs b/client/src/main.rs index 8950de1..b72ff68 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -1,14 +1,39 @@ // use std::net::{SocketAddr, TcpListener}; // use socket2::{Socket, Domain, Type}; +use clap::Parser; use net2::TcpBuilder; use std::io::prelude::*; use std::sync::{Arc, Mutex}; +#[derive(Parser, Debug)] +#[clap(name = "Tcp-PunchHole-Client", version, author, about = "A Tcp-PunchHole-Client")] +struct Cli { + #[clap(long)] + ip: Option, + #[clap(long)] + port: Option, + #[clap(long, takes_value = false)] + enable_ipv6: bool +} + +#[derive(Clone, Copy, Debug)] +enum IPType{ + V4, + V6 +} + fn main() -> std::io::Result<()> { - let connection_builder = TcpBuilder::new_v4()?; + let cli = Cli::parse(); + + let port = cli.port.unwrap_or(3000.to_owned()); + let (ip_type, default_ip) = if cli.enable_ipv6 { (IPType::V6, "0:0:0:0:0:0:0:1") } else { (IPType::V4, "127.0.0.1") }; + let ip = cli.ip.unwrap_or(default_ip.to_owned()); + println!("[CONFIG] IP Type: {:?}, Addr: {}:{}", ip_type, ip, port); + + let connection_builder = tcp_builder(&ip_type)?; connection_builder.reuse_address(true).unwrap(); - let mut stream = connection_builder.connect("178.128.32.250:3000")?; + let mut stream = connection_builder.connect(format!("{}:{}", ip, port))?; let formatted_msg = format!( "{}:{}", @@ -41,7 +66,7 @@ fn main() -> std::io::Result<()> { "[LISTENING] on the same port used to connect to S {}", listen_on ); - listen(listen_on).unwrap(); + listen(listen_on, &ip_type).unwrap(); }); // PUBLIC @@ -54,7 +79,7 @@ fn main() -> std::io::Result<()> { let connect_to = ips.get(0).unwrap(); let laddr = cloned_stream.local_addr().unwrap().to_string(); - connect(&laddr, connect_to, connection_established, "public").unwrap(); + connect(&laddr, connect_to, connection_established, "public", &ip_type).unwrap(); }); // PRIVATE @@ -66,7 +91,7 @@ fn main() -> std::io::Result<()> { let connect_to = ips.get(1).unwrap(); let laddr = cloned_stream.local_addr().unwrap().to_string(); - connect(&laddr, connect_to, connection_established_clone, "private").unwrap(); + connect(&laddr, connect_to, connection_established_clone, "private", &ip_type).unwrap(); }); } @@ -78,8 +103,9 @@ fn connect( ip: &str, connection_established: Arc>, flag: &'static str, + ip_type: &IPType ) -> std::io::Result<()> { - let connection_builder = TcpBuilder::new_v4()?; + let connection_builder = tcp_builder(ip_type)?; connection_builder.reuse_address(true).unwrap(); connection_builder.bind(laddr).unwrap(); @@ -129,8 +155,8 @@ fn connect( Ok(()) } -fn listen(ip: String) -> std::io::Result<()> { - let server_builder = TcpBuilder::new_v4()?; +fn listen(ip: String, ip_type :&IPType) -> std::io::Result<()> { + let server_builder = tcp_builder(ip_type)?; println!("Listening b: {}", ip); server_builder .reuse_address(true) @@ -150,3 +176,10 @@ fn listen(ip: String) -> std::io::Result<()> { } Ok(()) } + +fn tcp_builder(ip_type: &IPType) -> std::io::Result { + match ip_type { + IPType::V4 => {TcpBuilder::new_v4()} + IPType::V6 => {TcpBuilder::new_v6()} + } +} diff --git a/server/Cargo.lock b/server/Cargo.lock index f0d2b8a..54094eb 100644 --- a/server/Cargo.lock +++ b/server/Cargo.lock @@ -1,5 +1,238 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "clap" +version = "3.2.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71655c45cb9845d3270c9d6df84ebe72b4dad3c2ba3f7023ad47c144e4e473a5" +dependencies = [ + "atty", + "bitflags", + "clap_derive", + "clap_lex", + "indexmap", + "once_cell", + "strsim", + "termcolor", + "textwrap", +] + +[[package]] +name = "clap_derive" +version = "3.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea0c8bce528c4be4da13ea6fead8965e95b6073585a2f05204bd8f4119f82a65" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" +dependencies = [ + "os_str_bytes", +] + +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + +[[package]] +name = "heck" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] +name = "indexmap" +version = "1.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399" +dependencies = [ + "autocfg", + "hashbrown", +] + +[[package]] +name = "libc" +version = "0.2.137" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc7fcc620a3bff7cdd7a365be3376c97191aeaccc2a603e600951e452615bf89" + +[[package]] +name = "once_cell" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860" + +[[package]] +name = "os_str_bytes" +version = "6.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee" + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro2" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" +dependencies = [ + "proc-macro2", +] + [[package]] name = "server" version = "0.1.0" +dependencies = [ + "clap", +] + +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + +[[package]] +name = "syn" +version = "1.0.103" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a864042229133ada95abf3b54fdc62ef5ccabe9515b64717bcb9a1919e59445d" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "termcolor" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "textwrap" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d" + +[[package]] +name = "unicode-ident" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/server/Cargo.toml b/server/Cargo.toml index a1d3366..a97ceb1 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -7,3 +7,4 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +clap = { version = "3.1.18", features = ["derive"] } \ No newline at end of file diff --git a/server/src/main.rs b/server/src/main.rs index 0a3d2d1..80bedab 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -1,9 +1,27 @@ +use clap::Parser; use std::collections::HashMap; use std::io::prelude::*; use std::net::{TcpListener, TcpStream}; use std::sync::{Arc, Mutex}; use std::sync::mpsc::{channel, Sender}; +#[derive(Parser, Debug)] +#[clap(name = "Tcp-PunchHole-Server", version, author, about = "A Tcp-PunchHole-Server")] +struct Cli { + #[clap(long)] + ip: Option, + #[clap(long)] + port: Option, + #[clap(long, takes_value = false)] + enable_ipv6: bool +} + +#[derive(Clone, Copy, Debug)] +enum IPType{ + V4, + V6 +} + #[derive(Debug, Clone)] struct Peer { pub local_address: String, @@ -18,7 +36,7 @@ fn handle_client(mut socket: TcpStream, peers: Arc>>, hosts_tx: loop { let mut buf = [0; 1024]; let size = socket.read(&mut buf); - + if buf.len() == 0 || size.is_err() { let mut lock = peers.lock().unwrap(); let mut iter = lock.iter(); @@ -36,18 +54,14 @@ fn handle_client(mut socket: TcpStream, peers: Arc>>, hosts_tx: return Ok(()); } - let buf = String::from_utf8(buf[..size.unwrap()].to_vec()).unwrap(); - - println!("[INCOMING] from {} => {}", socket.peer_addr().unwrap(), buf); - - let mut local_elements = buf.split(":"); // since it's a POC - it needs to be set and done so let us assume - // that the message looks like xxx.xxx.xxx.xxx:ppppp - let local_address = local_elements.next().unwrap(); - let local_port = local_elements.next().unwrap(); + // that the message looks like xxx.xxx.xxx.xxx:ppppp or x:x:x:x:x:x:x:x:ppppp + let (local_address, local_port) = ip_parser(String::from_utf8(buf[..size.unwrap()].to_vec()).unwrap()); + + println!("[INCOMING] from {} => [{}]:{}", socket.peer_addr().unwrap(), local_address, local_port); let peer = Peer { - local_address: local_address.to_string(), + local_address, local_port: local_port.parse::().unwrap(), remote_address: socket.peer_addr().unwrap().ip().to_string(), @@ -79,8 +93,21 @@ fn handle_client(mut socket: TcpStream, peers: Arc>>, hosts_tx: } } +fn ip_parser(buf: String) -> (String, String) { + let (ip, port) = buf.split_at(buf.rfind(":").unwrap()); + (ip.to_owned(), port.split_at(1).1.to_owned()) +} + fn main() -> std::io::Result<()> { - let listener = TcpListener::bind("178.128.32.250:3000")?; + + let cli = Cli::parse(); + + let port = cli.port.unwrap_or(3000.to_owned()); + let (ip_type, default_ip) = if cli.enable_ipv6 { (IPType::V6, "0:0:0:0:0:0:0:1") } else { (IPType::V4, "127.0.0.1") }; + let ip = cli.ip.unwrap_or(default_ip.to_owned()); + println!("[CONFIG] IP Type: {:?}, Addr: {}:{}", ip_type, ip, port); + + let listener = TcpListener::bind(format!("{}:{}", ip, port))?; let peers: Arc>> = Arc::new(Mutex::new(Vec::::new())); let connections: Arc>> = Arc::new(Mutex::new(HashMap::::new())); let (hosts_tx, hosts_rx) = channel::<(String, String)>();