#socks5 #socks5-proxy #async-read #async-networking #proxy #sock #async

socks5-proto

Fundamental abstractions and async read / write functions for SOCKS5 protocol

3 unstable releases

0.4.1 Apr 10, 2024
0.4.0 Jul 10, 2023
0.3.3 Jan 29, 2023
0.3.2 Jul 30, 2022
0.1.0 May 1, 2022

#2043 in Network programming

Download history 189/week @ 2024-07-23 278/week @ 2024-07-30 382/week @ 2024-08-06 226/week @ 2024-08-13 328/week @ 2024-08-20 832/week @ 2024-08-27 693/week @ 2024-09-03 684/week @ 2024-09-10 604/week @ 2024-09-17 683/week @ 2024-09-24 1299/week @ 2024-10-01 807/week @ 2024-10-08 963/week @ 2024-10-15 950/week @ 2024-10-22 1446/week @ 2024-10-29 1226/week @ 2024-11-05

4,673 downloads per month
Used in 5 crates

GPL-3.0-or-later

29KB
688 lines

socks5-proto

This crate provides fundamental abstractions and async read / write functions for SOCKS5 protocol.

Version Documentation License

Check out socks5-server for a fine-grained relatively low-level asynchronized SOCKS5 server library.

Example

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
    }
}

License

GNU General Public License v3.0

Dependencies

~2.4–8MB
~65K SLoC