SocksServerConnectHandler Class — netty Architecture
Architecture documentation for the SocksServerConnectHandler class in SocksServerConnectHandler.java from the netty codebase.
Entity Profile
Dependency Diagram
graph TD bf2e2d64_3556_080a_9419_3c76234f742a["SocksServerConnectHandler"] a74866c1_d76a_1383_1729_532b767093fa["SocksServerConnectHandler.java"] bf2e2d64_3556_080a_9419_3c76234f742a -->|defined in| a74866c1_d76a_1383_1729_532b767093fa 81e7ae8a_cfa9_44fc_c226_d63eb601795a["channelRead0()"] bf2e2d64_3556_080a_9419_3c76234f742a -->|method| 81e7ae8a_cfa9_44fc_c226_d63eb601795a 2f452ed0_7744_43c7_1386_787717e8b111["exceptionCaught()"] bf2e2d64_3556_080a_9419_3c76234f742a -->|method| 2f452ed0_7744_43c7_1386_787717e8b111
Relationship Graph
Source Code
example/src/main/java/io/netty/example/socksproxy/SocksServerConnectHandler.java lines 36–135
@ChannelHandler.Sharable
public final class SocksServerConnectHandler extends SimpleChannelInboundHandler<SocksMessage> {
private final Bootstrap b = new Bootstrap();
@Override
public void channelRead0(final ChannelHandlerContext ctx, final SocksMessage message) throws Exception {
if (message instanceof Socks4CommandRequest) {
final Socks4CommandRequest request = (Socks4CommandRequest) message;
Promise<Channel> promise = ctx.executor().newPromise();
promise.addListener(
(FutureListener<Channel>) future -> {
final Channel outboundChannel = future.getNow();
if (future.isSuccess()) {
ChannelFuture responseFuture = ctx.channel().writeAndFlush(
new DefaultSocks4CommandResponse(Socks4CommandStatus.SUCCESS));
responseFuture.addListener(channelFuture -> {
ctx.pipeline().remove(SocksServerConnectHandler.this);
outboundChannel.pipeline().addLast(new RelayHandler(ctx.channel()));
ctx.pipeline().addLast(new RelayHandler(outboundChannel));
});
} else {
ctx.channel().writeAndFlush(
new DefaultSocks4CommandResponse(Socks4CommandStatus.REJECTED_OR_FAILED));
SocksServerUtils.closeOnFlush(ctx.channel());
}
});
final Channel inboundChannel = ctx.channel();
b.group(inboundChannel.eventLoop())
.channel(NioSocketChannel.class)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000)
.option(ChannelOption.SO_KEEPALIVE, true)
.handler(new DirectClientHandler(promise));
b.connect(request.dstAddr(), request.dstPort()).addListener(future -> {
if (future.isSuccess()) {
// Connection established use handler provided results
} else {
// Close the connection if the connection attempt has failed.
ctx.channel().writeAndFlush(
new DefaultSocks4CommandResponse(Socks4CommandStatus.REJECTED_OR_FAILED)
);
SocksServerUtils.closeOnFlush(ctx.channel());
}
});
} else if (message instanceof Socks5CommandRequest) {
final Socks5CommandRequest request = (Socks5CommandRequest) message;
Promise<Channel> promise = ctx.executor().newPromise();
promise.addListener(
(FutureListener<Channel>) future -> {
final Channel outboundChannel = future.getNow();
if (future.isSuccess()) {
ChannelFuture responseFuture =
ctx.channel().writeAndFlush(new DefaultSocks5CommandResponse(
Socks5CommandStatus.SUCCESS,
request.dstAddrType(),
request.dstAddr(),
request.dstPort()));
responseFuture.addListener(f -> {
ctx.pipeline().remove(SocksServerConnectHandler.this);
outboundChannel.pipeline().addLast(new RelayHandler(ctx.channel()));
ctx.pipeline().addLast(new RelayHandler(outboundChannel));
});
} else {
ctx.channel().writeAndFlush(new DefaultSocks5CommandResponse(
Socks5CommandStatus.FAILURE, request.dstAddrType()));
SocksServerUtils.closeOnFlush(ctx.channel());
}
});
final Channel inboundChannel = ctx.channel();
b.group(inboundChannel.eventLoop())
.channel(NioSocketChannel.class)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000)
.option(ChannelOption.SO_KEEPALIVE, true)
.handler(new DirectClientHandler(promise));
b.connect(request.dstAddr(), request.dstPort()).addListener(future -> {
Source
Frequently Asked Questions
What is the SocksServerConnectHandler class?
SocksServerConnectHandler is a class in the netty codebase, defined in example/src/main/java/io/netty/example/socksproxy/SocksServerConnectHandler.java.
Where is SocksServerConnectHandler defined?
SocksServerConnectHandler is defined in example/src/main/java/io/netty/example/socksproxy/SocksServerConnectHandler.java at line 36.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free