SocksLib

repository·master·Indexed 43 days ago

https://github.com/fengyouchao/sockslib

A Java library for implementing the SOCKS5 protocol, supporting both client and server implementations. Features include TCP/UDP proxying, authentication, proxy chaining, and SSL-enabled connections via SSLSocks5 and SocksServerBuilder. Requires JDK 8+.

Tokens
917
Snippets
3
Records
4
Agent score
5%

What's inside SocksLib

  1. Setup SocksLib environment and dependencies

    master

    To use SocksLib, ensure you are using JDK 8+. You must include the following libraries in your project's CLASSPATH:

    • log4j-slf4j-impl-2.4.jar
    • slf4j-api-1.7.12.jar
    • log4j-api-2.4.jar
    • log4j-core-2.4.jar
    • guava-18.0.jar
  2. Use SOCKS5 Client for CONNECT, BIND, and UDP ASSOCIATE

    master

    SocksLib provides several ways to use a SOCKS5 client depending on the required operation:

    CONNECT

    Use Socks5 to define the proxy and SocksSocket to establish a connection to a remote address.

    BIND

    Use SocksServerSocket to listen for incoming connections through the proxy.

    UDP ASSOCIATE

    Use Socks5DatagramSocket for UDP-based communication through the proxy.

    // CONNECT
    SocksProxy proxy = new Socks5(new InetSocketAddress("localhost", 1080));
    Socket socket = new SocksSocket(proxy, new InetSocketAddress("whois.internic.net", 43));
    
    // BIND
    SocksServerSocket serverSocket = new SocksServerSocket(proxy, inetAddress, 8080);
    InetAddress bindAddress = serverSocket.getBindAddress();
    int bindPort  = serverSocket.getBindPort();
    Socket socket = serverSocket.accept();
    
    // UDP ASSOCIATE
    DatagramSocket socket = new Socks5DatagramSocket(proxy);
  3. Implement a SOCKS5 Server

    master

    SocksLib allows you to build both anonymous and SSL-enabled SOCKS5 servers using the SocksServerBuilder.

    // Anonymous SOCKS5 Server
    SocksProxyServer proxyServer = SocksServerBuilder.buildAnonymousSocks5Server(); 
    proxyServer.start(); // Starts a server bound to port 1080
    
    // SSL SOCKS5 Server
    SSLConfigurationBuilder builder = SSLConfigurationBuilder.newBuilder();
    builder.setKeyStorePath("server-keystore.jks");
    builder.setKeyStorePassword("123456");
    builder.setClientAuth(false);
    SocksProxyServer socksProxyServer = SocksServerBuilder.buildAnonymousSSLSocks5Server(1081, builder.build());
    socksProxyServer.start();
  4. Connect to a SOCKS5 server via SSL

    master

    You can establish a secure connection to a SOCKS5 server using SSLSocks5. This requires configuring an SSLConfigurationBuilder with your trust keystore details.

    SSLConfigurationBuilder builder = SSLConfigurationBuilder.newBuilder();
    builder.setTrustKeyStorePath("client-trust-keystore.jks");
    builder.setTrustKeyStorePassword("123456");
    SocksProxy proxy = new SSLSocks5(new InetSocketAddress("localhost", 1081), builder.build());
    Socket socket = new SocksSocket(proxy, new InetSocketAddress("whois.internic.net", 43));