Gumdrop provides a SOCKS proxy server and a composable SOCKS client for tunneling other protocol handlers through a proxy. The server supports SOCKS4, SOCKS4a, and SOCKS5 (RFC 1928), including username/password authentication (RFC 1929) and GSS-API authentication (RFC 1961). CONNECT, BIND, and UDP ASSOCIATE are implemented with asynchronous relaying consistent with the rest of the framework.
SocksServer.Composer methodsSocksListener settings
A SocksServer, built with SocksServer.compose(),
owns one or more SocksListener instances. Each accepted
connection is handled by SocksProtocolHandler, which negotiates
the SOCKS version, performs optional authentication, and establishes relays
via SocksRelay, SocksBindRelay, or
SocksUdpRelay as appropriate. A composed
SocksServerSessionProvider supplies a
SocksSessionHandler (combined ConnectHandler +
BindHandler) per connection for application-level authorisation.
The server classes are in org.bluezoo.gumdrop.socks.server, the
listener, protocol handler and relays in org.bluezoo.gumdrop.socks,
and the client in org.bluezoo.gumdrop.socks.client.
SocksServer — listeners, configuration, and
compose(); destination filtering, relay limits, idle timeout; do
not subclass for application logicSocksServerSessionProvider — mints a
SocksSessionHandler per connection; with no provider,
compose() is a ready-to-use open proxy (accept CONNECT/BIND that
pass filtering; optional realm for SOCKS5)SocksListener — TCP listener; defaults to port 1080
(plaintext) or 1081 (TLS)SocksClientHandler — wraps another
ProtocolHandler and tunnels it through a SOCKS proxy (CONNECT,
BIND or UDP ASSOCIATE)SocksClientConfig — version preference, credentials,
handshake timeout
SocksServer server = SocksServer.compose()
.listener(new SocksListener().port(1080).bindWildcard())
.server();
gumdrop.addServer(server);
Realm socksRealm = new BasicRealm();
// ... realm configuration ...
SocksServer server = SocksServer.compose()
.realm(socksRealm)
.listener(new SocksListener().port(1080).bindWildcard())
.server();
When a realm is set on the server, it is applied to each
SocksListener that does not already define its own
realm. SOCKS5 clients must complete RFC 1929
username/password authentication. SOCKS4 carries an unauthenticated userid
field in the request only.
sockss)
TlsConfig tls = TlsConfig.keystore(Path.of("keystore.p12"), "secret");
SocksServer server = SocksServer.compose()
.listener(new SocksListener().port(1081).bindWildcard()
.secure(true).tls(tls))
.server();
If no port is set, the listener uses 1080 for cleartext and
1081 when secure(true).
SocksServer server = SocksServer.compose()
.allowedDestinations(CidrNetwork.parseList("203.0.113.0/24,2001:db8::/32"))
.blockedDestinations(CidrNetwork.parseList("127.0.0.0/8,::1/128"))
.maxRelays(1000)
.relayIdleTimeoutMs(5 * 60 * 1000)
.listener(new SocksListener().port(1080).bindWildcard())
.server();
Blocked destinations are evaluated first; then, if allowed destinations are
set, the destination must match an allow rule. maxRelays caps
concurrent relay sessions (0 = unlimited).
relayIdleTimeoutMs(long) sets the idle timeout of a relay in
milliseconds; the allowed and blocked destinations are lists of
CidrNetwork (CidrNetwork.parseList("10.0.0.0/8,192.168.0.0/16")
reads a comma-separated list). These are the destinations a client may ask the
proxy to reach; to restrict which clients may connect to the proxy at all, use
the listener's allowedNetworks and blockedNetworks
(see Security).
SocksServer.Composer methods| Method | Default | Description |
|---|---|---|
listener(SocksListener) |
— | Adds a listener (at least one required) |
sessionProvider(SocksServerSessionProvider) |
none (open proxy) | Session provider minting a
SocksSessionHandler per connection |
sessionPerConnection(Supplier<SocksSessionHandler>) |
— | Convenience for a fresh handler instance per connection |
realm(Realm) |
none | Optional authentication realm for SOCKS5 (username/password and GSS-API) |
allowedDestinations(List<CidrNetwork>) |
none | If set, only these networks may be reached |
blockedDestinations(List<CidrNetwork>) |
none | Matching destinations are denied |
maxRelays(int) |
0 | Maximum concurrent relays; 0 =
unlimited |
relayIdleTimeoutMs(long) |
300000 | Idle time before a relay is closed, in milliseconds |
SocksListener settings| Method | Default | Description |
|---|---|---|
port(int) |
1080, or 1081 if secure(true) | TCP port |
path(Path) |
none | UNIX domain socket path (mutually exclusive with
port) |
realm(Realm) |
from server | Per-listener realm override |
gssapiServer(GssapiServer) |
none | GSS-API server for Kerberos authentication (RFC 1961) |
To configure Kerberos from code, use
SocksListener.configureGSSAPI(Path keytabPath,
String servicePrincipal), which constructs a
GssapiServer for that listener and returns the listener.
Every listener takes secure(boolean) and tls(TlsConfig)
(see TLS & DTLS); bindWildcard() or
addresses(...) to choose where to bind; the timeouts
idleTimeoutMs, readTimeoutMs and
connectionTimeoutMs; maxNetInSize; and the connection
limits (maxConnections, maxConnectionsPerIP,
rateLimit), network filters (allowedNetworks,
blockedNetworks) and authentication lockout
(maxAuthFailures, authLockoutTimeMs) described in
Security. A SOCKS5 client that fails
username/password authentication too often is locked out and refused before the
realm is asked.
Compose a SocksServerSessionProvider (or use
sessionPerConnection with a Supplier) to mint a
SocksSessionHandler per accepted connection. The handler
implements both ConnectHandler and BindHandler,
approving or rejecting individual requests via ConnectState and
BindState (see org.bluezoo.gumdrop.socks.server).
With no session provider, the default behaviour applies (accept after
filtering and relay limits).
Both state objects also expose getSecurityInfo(): the TLS session
of the client's connection (protocol, cipher suite, and the client certificate
chain under mutual TLS), or null for a cleartext client. A handler
can use it to authorise by client certificate. The user name a SOCKS5 client
authenticated with is request.getAuthenticatedUser().
SocksServer server = SocksServer.compose()
.listener(new SocksListener().port(1080).bindWildcard())
.sessionPerConnection(new Supplier<SocksSessionHandler>() {
@Override
public SocksSessionHandler get() {
return new MySocksSessionHandler();
}
})
.server();
private static final class MySocksSessionHandler implements SocksSessionHandler {
@Override
public void handleConnect(ConnectState state, SocksRequest request,
Endpoint clientEndpoint) {
if (isBlocked(request.getAddress())) {
state.deny(SocksConstants.SOCKS5_REPLY_NOT_ALLOWED);
} else {
state.allow();
}
}
@Override
public void handleBind(BindState state, SocksRequest request,
Endpoint clientEndpoint) {
state.allow();
}
}
Use SocksClientHandler as the outer handler when connecting
with ClientEndpoint to the proxy; it performs the SOCKS
handshake then delegates to your inner protocol handler (for example SMTP or
HTTP client handlers).
In the client examples on this page, gumdrop is the Gumdrop instance returned by Gumdrop.boot(); a client is given it when it connects.
// Connect to smtp.example.com:587 via SOCKS proxy at proxy:1080
ClientEndpoint client = new ClientEndpoint(factory, "proxy", 1080);
client.connect(gumdrop, new SocksClientHandler(
"smtp.example.com", 587,
new SmtpClientProtocolHandler(callback)));
// With SOCKS5 username/password
SocksClientConfig config = new SocksClientConfig("user", "pass");
client.connect(gumdrop, new SocksClientHandler(
"smtp.example.com", 587, config,
new SmtpClientProtocolHandler(callback)));
SocksClientConfig is configured with version(...)
(AUTO, SOCKS4 or SOCKS5, the default),
username(...) and password(...) (or the two-argument
constructor), and handshakeTimeoutMs(...) (default 30 s); each
returns the config.
SocksClientConfig config = new SocksClientConfig()
.version(SocksClientConfig.Version.SOCKS5)
.username("user")
.password("pass")
.handshakeTimeoutMs(10000);
The client also does BIND (the proxy listens on your behalf and reports the
address it is listening on, so you can tell the remote peer, for example in an
FTP PORT command) and UDP ASSOCIATE (SOCKS5 only):
// BIND: the listener is told the proxy's bound address
client.connect(gumdrop, new SocksClientHandler(
"ftp.example.com", 0, config,
new SocksClientHandler.BindListener() {
@Override
public void bound(InetSocketAddress boundAddress) {
sendPortCommandToPeer(boundAddress);
}
},
new MyPeerProtocolHandler(callback)));
// UDP ASSOCIATE: datagrams go through the proxy's relay
SocksClientHandler assoc = new SocksClientHandler(config, udpTransportFactory,
new SocksClientHandler.UdpAssociateListener() {
@Override
public void associated(InetSocketAddress relayAddress) {
// now assoc.sendDatagram(destination, payload) may be called
}
@Override
public void receive(InetSocketAddress source, ByteBuffer payload) { }
@Override
public void error(Exception cause) { }
});
client.connect(gumdrop, assoc);
When the runtime has a TelemetryConfig (see
Telemetry and logging),
SocksServerMetrics records counters and histograms for
connections (socks.server.connections,
active_connections, session.duration),
CONNECT/BIND requests and UDP associations, relays and relay bytes,
authentication outcomes (socks.server.authentications and its
.success/.failure), and blocked destinations
(socks.server.destinations_blocked). See
Telemetry for OTLP setup.
← Back to Main Page | MQTT | Security | Telemetry
Gumdrop SOCKS Proxy Server & Client