Skip to content

tun2proxy/socks5-impl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

91 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

socks5-impl

Fundamental abstractions and async read / write functions for SOCKS5 protocol and Relatively low-level asynchronized SOCKS5 server implementation based on tokio.

This repo hosts at socks5-impl

Version Documentation License

Features

  • Fully asynchronized
  • Supports all SOCKS5 commands
    • CONNECT
    • BIND
    • ASSOCIATE
  • Customizable authentication
    • No authentication
    • Username / password
    • GSSAPI

Usage

The entry point of this crate is socks5_impl::server::Server.

Check examples for usage examples.

Example

This example uses the server feature. When that feature is disabled, the doctest still compiles with an empty fallback main.

#[cfg(feature = "server")]
use std::{net::SocketAddr, sync::Arc};
#[cfg(feature = "server")]
use socks5_impl::{
    Result,
    protocol::{Address, Reply},
    server::{auth, ClientConnection, IncomingConnection, Server},
};

#[cfg(feature = "server")]
#[tokio::main]
async fn main() -> Result<()> {
    let listen_addr: SocketAddr = "127.0.0.1:5000".parse()?;
    let auth = Arc::new(auth::NoAuth);
    let server = Server::bind(listen_addr, auth).await?;

    loop {
        let (conn, _) = server.accept().await?;
        tokio::spawn(async move {
            if let Err(err) = handle(conn).await {
                eprintln!("{err}");
            }
        });
    }
}

#[cfg(feature = "server")]
async fn handle(conn: IncomingConnection) -> Result<()> {
    let conn = conn.authenticate().await?;

    match conn.wait_request().await? {
        ClientConnection::Connect(connect, addr) => {
            let mut conn = connect.reply(Reply::Succeeded, Address::unspecified()).await?;
            let _ = addr;
            conn.shutdown().await?;
        }
        ClientConnection::Bind(bind, _) => {
            let mut conn = bind.reply(Reply::CommandNotSupported, Address::unspecified()).await?;
            conn.shutdown().await?;
        }
        ClientConnection::UdpAssociate(associate, _) => {
            let mut conn = associate.reply(Reply::CommandNotSupported, Address::unspecified()).await?;
            conn.shutdown().await?;
        }
    }

    Ok(())
}

#[cfg(not(feature = "server"))]
fn main() {}

License

GNU General Public License v3.0

About

SOCKS5 implement library, with some useful utilities such as dns-query, socks5-server, udp-client, echo-server, etc.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages