Crates.io | socks5-proto |
lib.rs | socks5-proto |
version | 0.4.1 |
source | src |
created_at | 2022-05-01 03:59:30.596595 |
updated_at | 2024-04-10 15:23:38.535714 |
description | Fundamental abstractions and async read / write functions for SOCKS5 protocol |
homepage | |
repository | https://github.com/EAimTY/socks5-server |
max_upload_size | |
id | 578443 |
size | 28,839 |
This crate provides fundamental abstractions and async read / write functions for SOCKS5 protocol.
Check out socks5-server for a fine-grained relatively low-level asynchronized SOCKS5 server library.
use socks5_proto::{
handshake::{
Method as HandshakeMethod, Request as HandshakeRequest, Response as HandshakeResponse,
},
Address, Error, ProtocolError, Reply, Request, Response,
};
use tokio::{io::AsyncWriteExt, net::TcpListener};
#[tokio::main]
async fn main() -> Result<(), Error> {
let listener = TcpListener::bind("127.0.0.1:5000").await?;
let (mut stream, _) = listener.accept().await?;
let hs_req = HandshakeRequest::read_from(&mut stream).await?;
if hs_req.methods.contains(&HandshakeMethod::NONE) {
let hs_resp = HandshakeResponse::new(HandshakeMethod::NONE);
hs_resp.write_to(&mut stream).await?;
} else {
let hs_resp = HandshakeResponse::new(HandshakeMethod::UNACCEPTABLE);
hs_resp.write_to(&mut stream).await?;
let _ = stream.shutdown().await;
return Err(Error::Protocol(
ProtocolError::NoAcceptableHandshakeMethod {
version: socks5_proto::SOCKS_VERSION,
chosen_method: HandshakeMethod::NONE,
methods: hs_req.methods,
},
));
}
let req = match Request::read_from(&mut stream).await {
Ok(req) => req,
Err(err) => {
let resp = Response::new(Reply::GeneralFailure, Address::unspecified());
resp.write_to(&mut stream).await?;
let _ = stream.shutdown().await;
return Err(err);
}
};
match req.command {
_ => todo!(), // process request
}
}
GNU General Public License v3.0