Expand description
This crate provides both client and server implementations of the SOCKS5 protocol (RFC 1928) with support for various authentication methods.
§Quick Start
§Client Usage
use socks5x::Socks5Address;
use socks5x::client::Socks5Client;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Socks5Client::connect(
"127.0.0.1:1080",
None // Some(("username".to_string(), "password".to_string())),
)
.await?;
// Request connection to destination
let mut stream = client
.request_connect(Socks5Address::from("httpbin.org"), 80)
.await?;
// Use the stream normally - data is transparently proxied
let request = "GET /ip HTTP/1.1\r\nHost: httpbin.org\r\nConnection: close\r\n\r\n";
stream.write_all(request.as_bytes()).await?;
let mut response = Vec::new();
stream.read_to_end(&mut response).await?;
println!("Response: {}", String::from_utf8_lossy(&response));
Ok(())
}§Server Usage
use std::sync::Arc;
use socks5x::server::{ClientHandler, DefaultConnectionCreator};
use tokio::net::TcpListener;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let listener = TcpListener::bind("127.0.0.1:1080").await?;
println!("SOCKS5 proxy server listening on 127.0.0.1:1080");
let client_handler = Arc::new(ClientHandler::no_auth(DefaultConnectionCreator));
loop {
let (socket, addr) = listener.accept().await?;
println!("New client connection from: {}", addr);
let client_handler = client_handler.clone();
tokio::spawn(async move {
if let Err(e) = client_handler.handle(socket).await {
eprintln!("Error handling client {}: {}", addr, e);
} else {
println!("Client {} disconnected", addr);
}
});
}
}Modules§
Structs§
Enums§
- Socks5
Address - Address types supported by the SOCKS5 protocol.