java.lang.Object
net.i2p.i2ptunnel.socks.SOCKSServer
net.i2p.i2ptunnel.socks.SOCKS4aServer

class SOCKS4aServerextends SOCKSServer
SOCKS4a server implementation for handling SOCKS version 4/4a connections.

This class processes SOCKS4a protocol requests, supporting both IPv4 addresses and domain name resolution (the "4a" extension). It validates requests, maps IP addresses to domain names when configured, and forwards connections to I2P destinations or outproxies.

Handles CONNECT commands, rejects unsupported BIND commands, and provides proper error responses. The server integrates with I2P naming service for destination resolution and supports security restrictions on localhost access.

  • Field Details

    • MAX_STRING_LEN

      private static final int MAX_STRING_LEN
      Longest hostname a request may carry.
      See Also:
    • setupCompleted

      private boolean setupCompleted
      True once the tunnel setup has finished.
    • _proxyCache

      private final ConcurrentHashMap<String,String> _proxyCache
      Sticky proxy cache: host -> selected proxy.
  • Constructor Details

    • SOCKS4aServer

      public SOCKS4aServer(I2PAppContext ctx, Socket clientSock, Properties props)
      Create a SOCKS4a server that communicates with the client using the specified socket. This method should not be invoked directly: new SOCKS4aServer objects should be created by using SOCKSServerFactory.createSOCKSServer(net.i2p.I2PAppContext, java.net.Socket, java.util.Properties). It is assumed that the SOCKS VER field has been stripped from the input stream of the client socket.
      Parameters:
      ctx - application context, passed to the SOCKSServer superclass
      clientSock - the already-connected client socket, whose input stream has had its VER field stripped
      props - non-null
  • Method Details

    • getClientSocket

      public Socket getClientSocket() throws SOCKSException
      Description copied from class: SOCKSServer
      Get a socket for sending/receiving 8-bit clean data to/from the client.
      Specified by:
      getClientSocket in class SOCKSServer
      Returns:
      a Socket connected with the client
      Throws:
      SOCKSException - if the initial handshake fails before the socket can be returned
    • setupServer

      protected void setupServer() throws SOCKSException
      Description copied from class: SOCKSServer
      Perform server initialization (expecially regarding protected variables).
      Specified by:
      setupServer in class SOCKSServer
      Throws:
      SOCKSException - if the client's request cannot be read off the socket
    • manageRequest

      private void manageRequest(DataInputStream in, DataOutputStream out) throws IOException
      SOCKS4a request management. This method assumes that all the stuff preceding or enveloping the actual request has been stripped out of the input/output streams.
      Parameters:
      in - the client input stream, positioned at the request command byte
      out - the client output stream for the failure reply
      Throws:
      IOException - if a stream read or write fails, or if the request has an unsupported command or an invalid port number
    • readString

      private static String readString(DataInputStream in) throws IOException
      Reads a NUL-terminated string from the request.
      Parameters:
      in - the stream to read
      Returns:
      the string read, without the terminator
      Throws:
      IOException - if the stream ends early
    • confirmConnection

      protected void confirmConnection() throws SOCKSException
      Description copied from class: SOCKSServer
      Confirm to the client that the connection has succeeded.
      Specified by:
      confirmConnection in class SOCKSServer
      Throws:
      SOCKSException - if the success reply cannot be written back to the client
    • sendRequestReply

      private void sendRequestReply(int replyCode, InetAddress inetAddr, int bindPort, DataOutputStream out) throws IOException
      Send the specified reply to a request of the client. The reply carries the given IPv4 address and port.
      Parameters:
      replyCode - the Reply constant to send
      inetAddr - the IPv4 address to report in the reply
      bindPort - the port to report in the reply, 0-65535
      out - the client output stream
      Throws:
      IOException - if the write to the client fails
    • getDestinationI2PSocket

      public I2PSocket getDestinationI2PSocket(I2PSOCKSTunnel t) throws SOCKSException
      Get an I2PSocket for sending/receiving 8-bit clean data to/from the destination of the SOCKS connection.
      Specified by:
      getDestinationI2PSocket in class SOCKSServer
      Parameters:
      t - the streaming tunnel to build the destination socket over
      Returns:
      an I2PSocket connected with the destination
      Throws:
      SOCKSException - if the requested host name or port was never established
    • selectProxy

      private String selectProxy(List<String> proxies, String host)
      Select a proxy, preferring a cached one for the same host.
      Parameters:
      proxies - the configured proxy destinations, non-empty
      host - the destination host whose earlier choice is reused if it is still in the list
      Returns:
      the proxy to use: the cached one for this host if still listed, otherwise a random pick that is then cached