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; }
public static Socks4CommandType valueOf(byte b) { switch (b) { case 0x01: return CONNECT; case 0x02: return BIND; } return new Socks4CommandType(b); }
@Override protected void encode(ChannelHandlerContext ctx, Socks4CommandRequest msg, ByteBuf out) throws Exception { out.writeByte(msg.version().byteValue()); out.writeByte(msg.type().byteValue()); out.writeShort(msg.dstPort()); if (NetUtil.isValidIpV4Address(msg.dstAddr())) { out.writeBytes(NetUtil.createByteArrayFromIpAddressString(msg.dstAddr())); ByteBufUtil.writeAscii(out, msg.userId()); out.writeByte(0); } else { out.writeBytes(IPv4_DOMAIN_MARKER); ByteBufUtil.writeAscii(out, msg.userId()); out.writeByte(0); ByteBufUtil.writeAscii(out, msg.dstAddr()); out.writeByte(0); } } }
type = Socks4CommandType.valueOf(in.readByte()); dstPort = in.readUnsignedShort(); dstAddr = NetUtil.intToIpAddress(in.readInt());
@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); } } }
type = Socks4CommandType.valueOf(in.readByte()); dstPort = in.readUnsignedShort(); dstAddr = NetUtil.intToIpAddress(in.readInt());
@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; }
type = Socks4CommandType.valueOf(in.readByte()); dstPort = in.readUnsignedShort(); dstAddr = NetUtil.intToIpAddress(in.readInt());
public static Socks4CommandType valueOf(byte b) { switch (b) { case 0x01: return CONNECT; case 0x02: return BIND; } return new Socks4CommandType(b); }
@Override protected void encode(ChannelHandlerContext ctx, Socks4CommandRequest msg, ByteBuf out) throws Exception { out.writeByte(msg.version().byteValue()); out.writeByte(msg.type().byteValue()); out.writeShort(msg.dstPort()); if (NetUtil.isValidIpV4Address(msg.dstAddr())) { out.writeBytes(NetUtil.createByteArrayFromIpAddressString(msg.dstAddr())); ByteBufUtil.writeAscii(out, msg.userId()); out.writeByte(0); } else { out.writeBytes(IPv4_DOMAIN_MARKER); ByteBufUtil.writeAscii(out, msg.userId()); out.writeByte(0); ByteBufUtil.writeAscii(out, msg.dstAddr()); out.writeByte(0); } } }
if (request.type().equals(Socks4CommandType.CONNECT)) { try { handleConnectRequest(ctx, request.dstAddr(), request.dstPort(),
public static Socks4CommandType valueOf(byte b) { switch (b) { case 0x01: return CONNECT; case 0x02: return BIND; } return new Socks4CommandType(b); }
@Override protected void encode(ChannelHandlerContext ctx, Socks4CommandRequest msg, ByteBuf out) throws Exception { out.writeByte(msg.version().byteValue()); out.writeByte(msg.type().byteValue()); out.writeShort(msg.dstPort()); if (NetUtil.isValidIpV4Address(msg.dstAddr())) { out.writeBytes(NetUtil.createByteArrayFromIpAddressString(msg.dstAddr())); ByteBufUtil.writeAscii(out, msg.userId()); out.writeByte(0); } else { out.writeBytes(IPv4_DOMAIN_MARKER); ByteBufUtil.writeAscii(out, msg.userId()); out.writeByte(0); ByteBufUtil.writeAscii(out, msg.dstAddr()); out.writeByte(0); } } }
if (request.type().equals(Socks4CommandType.CONNECT)) { try { handleConnectRequest(ctx, request.dstAddr(), request.dstPort(),