forked from onnovalkering/socksx
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cfcf9ce
commit 27a2692
Showing
3 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[package] | ||
name = "client" | ||
version = "0.1.0" | ||
edition = "2021" | ||
description = "This is an example client for socksx package." | ||
|
||
[[bin]] | ||
name = "client" | ||
path = "client.rs" | ||
|
||
[workspace] | ||
|
||
[dependencies] | ||
anyhow = "1" | ||
clap = { version = "3.0.0-rc.4", features = ["derive", "env"] } | ||
socksx = "0.1.1" | ||
tokio = { version = "1", features = ["full"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Usage | ||
|
||
```bash | ||
cargo run --bin client -- --host 172.16.238.4 --port 1080 --dest_host 172.16.238.5 --dest_port 12345 --src_port 12346 | ||
``` | ||
Note: The ip addresses are just examples, you should use your own ip addresses. I created a docker network and assigned | ||
ip addresses to the containers. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use anyhow::Result; | ||
use clap::{App, Arg}; | ||
use socksx::{Socks5Client, Socks6Client}; | ||
use tokio::io::AsyncWriteExt; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
// Parse command-line arguments using the Clap library. | ||
let args = App::new("Client") | ||
.arg( | ||
Arg::new("VERSION") | ||
.short('s') | ||
.long("socks") | ||
.help("The SOCKS version to use") | ||
.possible_values(&["5", "6"]) | ||
.default_value("6"), | ||
) | ||
.arg( | ||
Arg::new("PROXY_HOST") | ||
.long("host") | ||
.help("The IP/hostname of the proxy") | ||
.default_value("127.0.0.1"), | ||
) | ||
.arg( | ||
Arg::new("PROXY_PORT") | ||
.long("port") | ||
.help("The port of the proxy server") | ||
.default_value("1080"), | ||
) | ||
.arg( | ||
Arg::new("DEST_HOST") | ||
.long("dest_host") | ||
.help("The IP/hostname of the destination") | ||
.default_value("127.0.0.1"), | ||
) | ||
.arg( | ||
Arg::new("DEST_PORT") | ||
.long("dest_port") | ||
.help("The port of the destination server") | ||
.default_value("12345"), | ||
) | ||
.get_matches(); | ||
|
||
// Extract values from command-line arguments. | ||
let proxy_host = args.value_of("PROXY_HOST").unwrap(); | ||
let proxy_port = args.value_of("PROXY_PORT").unwrap(); | ||
let proxy_addr = format!("{}:{}", proxy_host, proxy_port); | ||
|
||
let dest_host = args.value_of("DEST_HOST").unwrap(); | ||
let dest_port = args.value_of("DEST_PORT").unwrap(); | ||
let dest_addr = format!("{}:{}", dest_host, dest_port); | ||
|
||
// Determine the SOCKS version specified in the arguments. | ||
match args.value_of("VERSION") { | ||
Some("5") => connect_v5(proxy_addr, dest_addr).await, | ||
Some("6") => connect_v6(proxy_addr, dest_addr).await, | ||
Some(version) => panic!("Unsupported version: {}", version), | ||
None => unreachable!(), | ||
} | ||
} | ||
|
||
/// Connects to a destination using SOCKS5 protocol. | ||
async fn connect_v5( | ||
proxy_addr: String, | ||
dest_addr: String, | ||
) -> Result<()> { | ||
// Create a SOCKS5 client. | ||
let client = Socks5Client::new(proxy_addr, None).await?; | ||
|
||
// Connect to the destination. | ||
let (mut outgoing, _) = client.connect(dest_addr).await?; | ||
|
||
// Write a message to the destination. | ||
outgoing.write(String::from("Hello, world!\n").as_bytes()).await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Connects to a destination using SOCKS6 protocol. | ||
async fn connect_v6( | ||
proxy_addr: String, | ||
dest_addr: String, | ||
) -> Result<()> { | ||
// Create a SOCKS6 client. | ||
let client = Socks6Client::new(proxy_addr, None).await?; | ||
|
||
// Connect to the destination. | ||
let (mut outgoing, _) = client.connect(dest_addr, None, None).await?; | ||
|
||
// Write a message to the destination. | ||
outgoing.write(String::from("Hello, world!\n").as_bytes()).await?; | ||
|
||
Ok(()) | ||
} |