Skip to content

Commit

Permalink
feat(client, server): add parameter parser and IPV6 support
Browse files Browse the repository at this point in the history
Add three new parameters:ip, port, enable_ipv6 and IPV6 support
Make network hole punching test scenarios more flexible
  • Loading branch information
KKould committed Nov 26, 2022
1 parent fbcafbb commit 80d3d0e
Show file tree
Hide file tree
Showing 6 changed files with 518 additions and 20 deletions.
203 changes: 203 additions & 0 deletions client/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ edition = "2018"

[dependencies]
net2 = "0.2.37"
socket2 = {version="0.4.2", features=["all"]}
socket2 = {version="0.4.2", features=["all"]}
clap = { version = "3.1.18", features = ["derive"] }
49 changes: 41 additions & 8 deletions client/src/main.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
#[clap(long)]
port: Option<u16>,
#[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!(
"{}:{}",
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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();
});
}

Expand All @@ -78,8 +103,9 @@ fn connect(
ip: &str,
connection_established: Arc<Mutex<bool>>,
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();

Expand Down Expand Up @@ -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)
Expand All @@ -150,3 +176,10 @@ fn listen(ip: String) -> std::io::Result<()> {
}
Ok(())
}

fn tcp_builder(ip_type: &IPType) -> std::io::Result<TcpBuilder> {
match ip_type {
IPType::V4 => {TcpBuilder::new_v4()}
IPType::V6 => {TcpBuilder::new_v6()}
}
}
Loading

0 comments on commit 80d3d0e

Please sign in to comment.