@Override protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> out) throws Exception { switch (state()) { case CHECK_PROTOCOL_VERSION: { if (byteBuf.readByte() != SocksProtocolVersion.SOCKS5.byteValue()) { out.add(SocksCommonUtils.UNKNOWN_SOCKS_RESPONSE); break; } checkpoint(State.READ_PREFERRED_AUTH_TYPE); } case READ_PREFERRED_AUTH_TYPE: { SocksAuthScheme authScheme = SocksAuthScheme.valueOf(byteBuf.readByte()); out.add(new SocksInitResponse(authScheme)); break; } default: { throw new Error(); } } ctx.pipeline().remove(this); }
@Override public void encodeAsByteBuf(ByteBuf byteBuf) { byteBuf.writeByte(protocolVersion().byteValue()); byteBuf.writeByte(authScheme.byteValue()); } }
private ByteBuf buildInit() { ByteBuf buf = Unpooled.buffer(); new SocksInitResponse(SocksAuthScheme.NO_AUTH).encodeAsByteBuf(buf); return buf; }
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> out) throws Exception { switch (state()) { case CHECK_PROTOCOL_VERSION: { if (byteBuf.readByte() != SocksProtocolVersion.SOCKS5.byteValue()) { out.add(SocksCommonUtils.UNKNOWN_SOCKS_RESPONSE); break; } checkpoint(State.READ_PREFERRED_AUTH_TYPE); } case READ_PREFERRED_AUTH_TYPE: { SocksAuthScheme authScheme = SocksAuthScheme.valueOf(byteBuf.readByte()); out.add(new SocksInitResponse(authScheme)); break; } default: { throw new Error(); } } ctx.pipeline().remove(this); }
@Override public void encodeAsByteBuf(ByteBuf byteBuf) { byteBuf.writeByte(protocolVersion().byteValue()); byteBuf.writeByte(authScheme.byteValue()); } }
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> out) throws Exception { switch (state()) { case CHECK_PROTOCOL_VERSION: { if (byteBuf.readByte() != SocksProtocolVersion.SOCKS5.byteValue()) { out.add(SocksCommonUtils.UNKNOWN_SOCKS_RESPONSE); break; } checkpoint(State.READ_PREFERRED_AUTH_TYPE); } case READ_PREFERRED_AUTH_TYPE: { SocksAuthScheme authScheme = SocksAuthScheme.valueOf(byteBuf.readByte()); out.add(new SocksInitResponse(authScheme)); break; } default: { throw new Error(); } } ctx.pipeline().remove(this); }
@Override public void encodeAsByteBuf(ByteBuf byteBuf) { byteBuf.writeByte(protocolVersion().byteValue()); byteBuf.writeByte(authScheme.byteValue()); } }
@Override public void channelRead0(ChannelHandlerContext ctx, SocksRequest socksRequest) throws Exception { switch (socksRequest.requestType()) { case INIT: { logger.info("localserver init"); ctx.pipeline().addFirst(new SocksCmdRequestDecoder()); ctx.write(new SocksInitResponse(SocksAuthScheme.NO_AUTH)); break; } case AUTH: logger.info("localserver auth"); ctx.pipeline().addFirst(new SocksCmdRequestDecoder()); ctx.write(new SocksAuthResponse(SocksAuthStatus.SUCCESS)); break; case CMD: SocksCmdRequest req = (SocksCmdRequest) socksRequest; if (req.cmdType() == SocksCmdType.CONNECT) { logger.info("localserver connect"); ctx.pipeline().addLast(new SocksServerConnectHandler()); ctx.pipeline().remove(this); ctx.fireChannelRead(socksRequest); } else { ctx.close(); } break; case UNKNOWN: ctx.close(); break; } }
case SOCKS4a: log.warn("\tBad Handshake! (socks version not supported: 4)"); ctx.write(new SocksInitResponse(SocksAuthScheme.UNKNOWN)); if (ctx.channel().isActive()) { ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); case INIT: ctx.pipeline().addFirst(new SocksCmdRequestDecoder()); ctx.write(new SocksInitResponse(SocksAuthScheme.NO_AUTH)); break; case AUTH: