@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); } } }
@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); } } }
@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); } } }
@Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { ConnectionInfo newConnectionInfo = new ConnectionInfo( connectionInfo.getClientAddr(), serverAddr); ctx.writeAndFlush(new DefaultSocks4CommandResponse( Socks4CommandStatus.SUCCESS, request.dstAddr(), request.dstPort())); onServerConnected(ctx, newConnectionInfo, future.channel()); } else { ctx.channel().writeAndFlush(new DefaultSocks4CommandResponse( Socks4CommandStatus.REJECTED_OR_FAILED, request.dstAddr(), request.dstPort())); ctx.close(); } } });
@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); } } }
@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); } } }
private void onSocksSuccess(ChannelHandlerContext ctx, Socks4CommandRequest request) { Address serverAddr = new Address(request.dstAddr(), request.dstPort()); createServerChannel(ctx, serverAddr).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { ConnectionInfo newConnectionInfo = new ConnectionInfo( connectionInfo.getClientAddr(), serverAddr); ctx.writeAndFlush(new DefaultSocks4CommandResponse( Socks4CommandStatus.SUCCESS, request.dstAddr(), request.dstPort())); onServerConnected(ctx, newConnectionInfo, future.channel()); } else { ctx.channel().writeAndFlush(new DefaultSocks4CommandResponse( Socks4CommandStatus.REJECTED_OR_FAILED, request.dstAddr(), request.dstPort())); ctx.close(); } } }); }