public static boolean isSocks4(ByteBuf msg, int actualReadableBytes) { // first byte has to be 4 int i = msg.readerIndex(); if (SocksVersion.valueOf(msg.getByte(i++)) != SocksVersion.SOCKS4a) { return false; } // second byte has to be 1 or 2 Socks4CommandType commandType = Socks4CommandType.valueOf(msg.getByte(i++)); if (!(commandType.equals(Socks4CommandType.CONNECT) || commandType.equals(Socks4CommandType.BIND))) { return false; } if (-1 == (i = consumeFields(msg, i + 2))) { return false; } // end of available bytes reached // if not, it is probably not SOCKS4 // do this check last so that any waiting for data is already done return i == actualReadableBytes; }
@Override protected void channelRead0(final ChannelHandlerContext ctx, final Socks4CommandRequest commandRequest) { if (commandRequest.type().equals(Socks4CommandType.CONNECT)) { forwardConnection(ctx, new Socks4ConnectHandler(server, mockServerLogger, commandRequest.dstAddr(), commandRequest.dstPort()), commandRequest.dstAddr(), commandRequest.dstPort()); ctx.fireChannelRead(commandRequest); } else { ctx.writeAndFlush(new DefaultSocks4CommandResponse(Socks4CommandStatus.REJECTED_OR_FAILED)).addListener(ChannelFutureListener.CLOSE); } } }
public static boolean isSocks4(ByteBuf msg, int actualReadableBytes) { // first byte has to be 4 int i = msg.readerIndex(); if (SocksVersion.valueOf(msg.getByte(i++)) != SocksVersion.SOCKS4a) { return false; } // second byte has to be 1 or 2 Socks4CommandType commandType = Socks4CommandType.valueOf(msg.getByte(i++)); if (!(commandType.equals(Socks4CommandType.CONNECT) || commandType.equals(Socks4CommandType.BIND))) { return false; } if (-1 == (i = consumeFields(msg, i + 2))) { return false; } // end of available bytes reached // if not, it is probably not SOCKS4 // do this check last so that any waiting for data is already done return i == actualReadableBytes; }
@Override protected void channelRead0(final ChannelHandlerContext ctx, final Socks4CommandRequest commandRequest) { if (commandRequest.type().equals(Socks4CommandType.CONNECT)) { forwardConnection(ctx, new Socks4ConnectHandler(server, mockServerLogger, commandRequest.dstAddr(), commandRequest.dstPort()), commandRequest.dstAddr(), commandRequest.dstPort()); ctx.fireChannelRead(commandRequest); } else { ctx.writeAndFlush(new DefaultSocks4CommandResponse(Socks4CommandStatus.REJECTED_OR_FAILED)).addListener(ChannelFutureListener.CLOSE); } } }
if (request.type().equals(Socks4CommandType.CONNECT)) { try { handleConnectRequest(ctx, request.dstAddr(), request.dstPort(),
if (request.type().equals(Socks4CommandType.CONNECT)) { try { handleConnectRequest(ctx, request.dstAddr(), request.dstPort(),