java.lang.Object
net.i2p.i2ptunnel.socks.SOCKSServer
net.i2p.i2ptunnel.socks.SOCKS4aServer
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 Summary
FieldsModifier and TypeFieldDescriptionprivate final ConcurrentHashMap<String, String> Sticky proxy cache: host -> selected proxy.private static final intLongest hostname a request may carry.private booleanTrue once the tunnel setup has finished.Fields inherited from class SOCKSServer
_context, _log, addressType, clientSock, connHostName, connPort, propsModifier and TypeFieldDescriptionprotected final I2PAppContextThe application context, used for the log manager and property lookups.protected final LogThe logger for this class.protected intSOCKS address type (e.g.protected final SocketThe connected socket back to the local SOCKS client.protected StringHostname requested by the SOCKS client.protected intPort requested by the SOCKS client.protected final PropertiesThe tunnel configuration, holding the IP to domain mappings.Constructor Summary
ConstructorsConstructorDescriptionSOCKS4aServer(I2PAppContext ctx, Socket clientSock, Properties props) Create a SOCKS4a server that communicates with the client using the specified socket.Method Summary
Modifier and TypeMethodDescriptionprotected voidConfirm to the client that the connection has succeeded.Get a socket for sending/receiving 8-bit clean data to/from the client.Get an I2PSocket for sending/receiving 8-bit clean data to/from the destination of the SOCKS connection.private voidmanageRequest(DataInputStream in, DataOutputStream out) SOCKS4a request management.private static StringReads a NUL-terminated string from the request.private StringselectProxy(List<String> proxies, String host) Select a proxy, preferring a cached one for the same host.private voidsendRequestReply(int replyCode, InetAddress inetAddr, int bindPort, DataOutputStream out) Send the specified reply to a request of the client.protected voidPerform server initialization (expecially regarding protected variables).Methods inherited from class SOCKSServer
getMappedDomainNameForIP, getOutproxyPlugin, httpsConnect, outproxyConnectModifier and TypeMethodDescriptionprotected StringIP to domain name mapping support.protected OutproxyLooks up the registered outproxy plugin to route through, if any.voidhttpsConnect(I2PSocket destSock, OutputStream pout, String connHostName, int connPort, String configUser, String configPW) Act as a https client to connect to a CONNECT outproxy.protected I2PSocketoutproxyConnect(I2PSOCKSTunnel tun, String proxy) Act as a SOCKS 5 client to connect to an outproxy Caller must send success or error to local socks client.
Field Details
MAX_STRING_LEN
private static final int MAX_STRING_LENLongest hostname a request may carry.- See Also:
setupCompleted
private boolean setupCompletedTrue once the tunnel setup has finished._proxyCache
Sticky proxy cache: host -> selected proxy.
Constructor Details
SOCKS4aServer
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 usingSOCKSServerFactory.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 superclassclientSock- the already-connected client socket, whose input stream has had its VER field strippedprops- non-null
Method Details
getClientSocket
Description copied from class:SOCKSServerGet a socket for sending/receiving 8-bit clean data to/from the client.- Specified by:
getClientSocketin classSOCKSServer- Returns:
- a Socket connected with the client
- Throws:
SOCKSException- if the initial handshake fails before the socket can be returned
setupServer
Description copied from class:SOCKSServerPerform server initialization (expecially regarding protected variables).- Specified by:
setupServerin classSOCKSServer- Throws:
SOCKSException- if the client's request cannot be read off the socket
manageRequest
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 byteout- 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
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
Description copied from class:SOCKSServerConfirm to the client that the connection has succeeded.- Specified by:
confirmConnectionin classSOCKSServer- 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 sendinetAddr- the IPv4 address to report in the replybindPort- the port to report in the reply, 0-65535out- the client output stream- Throws:
IOException- if the write to the client fails
getDestinationI2PSocket
Get an I2PSocket for sending/receiving 8-bit clean data to/from the destination of the SOCKS connection.- Specified by:
getDestinationI2PSocketin classSOCKSServer- 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
Select a proxy, preferring a cached one for the same host.- Parameters:
proxies- the configured proxy destinations, non-emptyhost- 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