@Override
  protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    ChannelPipeline p = ctx.pipeline();
    SocksProtocolVersion version = SocksProtocolVersion.valueOf(in.readByte());
    System.out.println(version);
    in.resetReaderIndex();
    switch (version) {
      case SOCKS4a:
        p.addLast(new Socks4CmdRequestDecoder());
        p.addLast(Socks4MessageEncoder.INSTANCE);

        break;
      case SOCKS5:
        p.addLast(new Socks5InitRequestDecoder());
        p.addLast(Socks5MessageEncoder.INSTANCE);

        break;
      case UNKNOWN:
        in.clear();
        ctx.close();
        return;
    }
    p.addLast(SocksServerHandler.INSTANCE);
    p.remove(this);
  }
Exemplo n.º 2
0
 static void removeIfExists(
     ChannelPipeline pipeline, Class<? extends ChannelHandler> handlerClass) {
   ChannelHandler channelHandler = pipeline.get(handlerClass);
   if (channelHandler != null) {
     pipeline.remove(channelHandler);
   }
 }
  private static void ensureThatExceptionHandlerIsLast(ChannelPipeline pipeline) {
    ChannelInboundHandler exceptionHandler = ChannelExceptionHandler.getInstance();
    if (pipeline.last() != exceptionHandler || pipeline.context(exceptionHandler) == null) {
      return;
    }

    pipeline.remove(exceptionHandler);
    pipeline.addLast(exceptionHandler);
  }
 public static void modifyPipeline() {
   ChannelPipeline pipeline = null;
   // Creates a FirstHandler instance
   FirstHandler firstHandler = new FirstHandler();
   // Adds this instance to the ChannelPipeline as “handler1”
   pipeline.addLast("handler1", firstHandler);
   // Adds an instance of a SecondHandler to the ChannelPipeline in the first slot,
   // as “handler2”. It will be placed before the existing “handler1”.
   pipeline.addFirst("handler2", new SecondHandler());
   // Adds a ThirdHandler instance to the ChannelPipeline in the last slot as “handler3”.
   pipeline.addLast("handler3", new ThirdHandler());
   // Removes “handler3” by name.
   pipeline.remove("handler3");
   // Removes the FirstHandler by reference (it’s unique, so its name is not needed).
   pipeline.remove(firstHandler);
   // Replaces the SecondHandler (“handler2”) with a FourthHandler: “handler4”.
   pipeline.replace("handler2", "handler4", new FourthHandler());
 }
 protected void decode(ChannelHandlerContext context, ByteBuf buffer) throws Exception {
   ChannelPipeline pipeline = context.pipeline();
   if (detectSsl && SslHandler.isEncrypted(buffer)) {
     SSLEngine engine = SSL_SERVER_CONTEXT.getValue().createSSLEngine();
     engine.setUseClientMode(false);
     pipeline.addLast(
         new SslHandler(engine),
         new ChunkedWriteHandler(),
         new PortUnificationServerHandler(delegatingHttpRequestHandler, false, detectGzip));
   } else {
     int magic1 = buffer.getUnsignedByte(buffer.readerIndex());
     int magic2 = buffer.getUnsignedByte(buffer.readerIndex() + 1);
     if (detectGzip && magic1 == 31 && magic2 == 139) {
       pipeline.addLast(
           new JZlibEncoder(ZlibWrapper.GZIP),
           new JdkZlibDecoder(ZlibWrapper.GZIP),
           new PortUnificationServerHandler(delegatingHttpRequestHandler, detectSsl, false));
     } else if (isHttp(magic1, magic2)) {
       NettyUtil.initHttpHandlers(pipeline);
       pipeline.addLast(delegatingHttpRequestHandler);
       if (BuiltInServer.LOG.isDebugEnabled()) {
         pipeline.addLast(
             new ChannelOutboundHandlerAdapter() {
               @Override
               public void write(
                   ChannelHandlerContext context, Object message, ChannelPromise promise)
                   throws Exception {
                 if (message instanceof HttpResponse) {
                   //                BuiltInServer.LOG.debug("OUT HTTP:\n" + message);
                   HttpResponse response = (HttpResponse) message;
                   BuiltInServer.LOG.debug(
                       "OUT HTTP: "
                           + response.getStatus().code()
                           + " "
                           + response.headers().get("Content-type"));
                 }
                 super.write(context, message, promise);
               }
             });
       }
     } else if (magic1 == 'C' && magic2 == 'H') {
       buffer.skipBytes(2);
       pipeline.addLast(new CustomHandlerDelegator());
     } else {
       BuiltInServer.LOG.warn("unknown request, first two bytes " + magic1 + " " + magic2);
       context.close();
     }
   }
   // must be after new channels handlers addition (netty bug?)
   ensureThatExceptionHandlerIsLast(pipeline);
   pipeline.remove(this);
   context.fireChannelRead(buffer);
 }
  /**
   * Handle the web socket handshake for the web socket specification <a href=
   * "http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17">HyBi versions 13-17</a>.
   * Versions 13-17 share the same wire protocol.
   *
   * <p>Browser request to the server:
   *
   * <pre>
   * GET /chat HTTP/1.1
   * Host: server.example.com
   * Upgrade: websocket
   * Connection: Upgrade
   * Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
   * Sec-WebSocket-Origin: http://example.com
   * Sec-WebSocket-Protocol: chat, superchat
   * Sec-WebSocket-Version: 13
   * </pre>
   *
   * <p>Server response:
   *
   * <pre>
   * HTTP/1.1 101 Switching Protocols
   * Upgrade: websocket
   * Connection: Upgrade
   * Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
   * Sec-WebSocket-Protocol: chat
   * </pre>
   *
   * @param channel Channel
   * @param req HTTP request
   */
  @Override
  public ChannelFuture handshake(Channel channel, HttpRequest req) {

    if (logger.isDebugEnabled()) {
      logger.debug(String.format("Channel %s WS Version 13 server handshake", channel.getId()));
    }

    HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.SWITCHING_PROTOCOLS);

    String key = req.getHeader(Names.SEC_WEBSOCKET_KEY);
    if (key == null) {
      throw new WebSocketHandshakeException("not a WebSocket request: missing key");
    }
    String acceptSeed = key + WEBSOCKET_13_ACCEPT_GUID;
    byte[] sha1 = WebSocketUtil.sha1(acceptSeed.getBytes(CharsetUtil.US_ASCII));
    String accept = WebSocketUtil.base64(sha1);

    if (logger.isDebugEnabled()) {
      logger.debug(
          String.format("WS Version 13 Server Handshake key: %s. Response: %s.", key, accept));
    }

    res.setStatus(HttpResponseStatus.SWITCHING_PROTOCOLS);
    res.addHeader(Names.UPGRADE, WEBSOCKET.toLowerCase());
    res.addHeader(Names.CONNECTION, Names.UPGRADE);
    res.addHeader(Names.SEC_WEBSOCKET_ACCEPT, accept);
    String protocol = req.getHeader(Names.SEC_WEBSOCKET_PROTOCOL);
    if (protocol != null) {
      res.addHeader(Names.SEC_WEBSOCKET_PROTOCOL, selectSubprotocol(protocol));
    }

    ChannelFuture future = channel.write(res);

    // Upgrade the connection and send the handshake response.
    ChannelPipeline p = channel.getPipeline();
    if (p.get(HttpChunkAggregator.class) != null) {
      p.remove(HttpChunkAggregator.class);
    }

    p.replace(
        HttpRequestDecoder.class,
        "wsdecoder",
        new WebSocket13FrameDecoder(true, allowExtensions, this.getMaxFramePayloadLength()));
    p.replace(HttpResponseEncoder.class, "wsencoder", new WebSocket13FrameEncoder(false));

    return future;
  }
 @Override
 protected void channelRead0(ChannelHandlerContext context, ByteBuf message) throws Exception {
   ByteBuf buffer = getBufferIfSufficient(message, UUID_LENGTH, context);
   if (buffer == null) {
     message.release();
   } else {
     UUID uuid = new UUID(buffer.readLong(), buffer.readLong());
     for (BinaryRequestHandler customHandler : BinaryRequestHandler.EP_NAME.getExtensions()) {
       if (uuid.equals(customHandler.getId())) {
         ChannelPipeline pipeline = context.pipeline();
         pipeline.addLast(customHandler.getInboundHandler());
         ensureThatExceptionHandlerIsLast(pipeline);
         pipeline.remove(this);
         context.fireChannelRead(buffer);
         break;
       }
     }
   }
 }
Exemplo n.º 8
0
  public void sendLoginSuccess(int status, Player player) {
    ByteBuf buf = channel.alloc().buffer(11);
    buf.writeByte(player.getRights());
    buf.writeByte(0);
    buf.writeByte(0);
    buf.writeByte(0);
    buf.writeByte(0);
    buf.writeByte(0);
    buf.writeByte(0);
    buf.writeShort(player.getId());
    buf.writeByte(1);
    buf.writeByte(1);

    ChannelPipeline pipeline = channel.pipeline();

    GameSession session = new GameSession(server, channel, player);
    RsChannelHandler handler = pipeline.get(RsChannelHandler.class);
    handler.setSession(session);

    pipeline.remove(ReadTimeoutHandler.class); // TODO a different timeout mechanism is used in-game

    LoginResponse response = new LoginResponse(status, buf);
    channel.write(response);

    pipeline.addFirst(
        new GameFrameEncoder(outRandom),
        new GameMessageEncoder(server.getCodecRepository()),
        new GameFrameDecoder(inRandom),
        new GameMessageDecoder(server.getCodecRepository()));

    if (displayMode == 0 || displayMode == 1)
      player.getInterfaceSet().setDisplayMode(DisplayMode.FIXED);
    else player.getInterfaceSet().setDisplayMode(DisplayMode.RESIZABLE);

    session.init();
  }
Exemplo n.º 9
0
  NetSocket createNetSocket() {
    DefaultNetSocket socket =
        new DefaultNetSocket(vertx, channel, context, server.tcpHelper, false);
    Map<Channel, DefaultNetSocket> connectionMap = new HashMap<Channel, DefaultNetSocket>(1);
    connectionMap.put(channel, socket);

    // Flush out all pending data
    endReadAndFlush();

    // remove old http handlers and replace the old handler with one that handle plain sockets
    ChannelPipeline pipeline = channel.pipeline();
    ChannelHandler compressor = pipeline.get(HttpChunkContentCompressor.class);
    if (compressor != null) {
      pipeline.remove(compressor);
    }

    pipeline.remove("httpDecoder");
    if (pipeline.get("chunkedWriter") != null) {
      pipeline.remove("chunkedWriter");
    }

    channel
        .pipeline()
        .replace(
            "handler",
            "handler",
            new VertxNetHandler(server.vertx, connectionMap) {
              @Override
              public void exceptionCaught(ChannelHandlerContext chctx, Throwable t)
                  throws Exception {
                // remove from the real mapping
                server.connectionMap.remove(channel);
                super.exceptionCaught(chctx, t);
              }

              @Override
              public void channelInactive(ChannelHandlerContext chctx) throws Exception {
                // remove from the real mapping
                server.connectionMap.remove(channel);
                super.channelInactive(chctx);
              }

              @Override
              public void channelRead(ChannelHandlerContext chctx, Object msg) throws Exception {
                if (msg instanceof HttpContent) {
                  ReferenceCountUtil.release(msg);
                  return;
                }
                super.channelRead(chctx, msg);
              }
            });

    // check if the encoder can be removed yet or not.
    if (lastWriteFuture == null) {
      channel.pipeline().remove("httpEncoder");
    } else {
      lastWriteFuture.addListener(
          new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
              channel.pipeline().remove("httpEncoder");
            }
          });
    }
    return socket;
  }
Exemplo n.º 10
0
    @Override
    public final boolean trySend(HttpResponse message) {
      try {
        final HttpRequestWrapper nettyRequest = (HttpRequestWrapper) message.getRequest();
        final FullHttpRequest req = nettyRequest.req;
        final ChannelHandlerContext ctx = nettyRequest.ctx;

        final HttpResponseStatus status = HttpResponseStatus.valueOf(message.getStatus());

        if (message.getStatus() >= 400 && message.getStatus() < 600) {
          sendHttpResponse(
              ctx, req, new DefaultFullHttpResponse(req.getProtocolVersion(), status), false);
          close();
          return true;
        }

        if (message.getRedirectPath() != null) {
          sendHttpRedirect(ctx, req, message.getRedirectPath());
          close();
          return true;
        }

        FullHttpResponse res;
        if (message.getStringBody() != null)
          res =
              new DefaultFullHttpResponse(
                  req.getProtocolVersion(),
                  status,
                  Unpooled.wrappedBuffer(message.getStringBody().getBytes()));
        else if (message.getByteBufferBody() != null)
          res =
              new DefaultFullHttpResponse(
                  req.getProtocolVersion(),
                  status,
                  Unpooled.wrappedBuffer(message.getByteBufferBody()));
        else res = new DefaultFullHttpResponse(req.getProtocolVersion(), status);

        if (message.getCookies() != null) {
          final ServerCookieEncoder enc = ServerCookieEncoder.STRICT;
          for (final Cookie c : message.getCookies())
            HttpHeaders.setHeader(res, COOKIE, enc.encode(getNettyCookie(c)));
        }
        if (message.getHeaders() != null) {
          for (final Map.Entry<String, String> h : message.getHeaders().entries())
            HttpHeaders.setHeader(res, h.getKey(), h.getValue());
        }

        if (message.getContentType() != null) {
          String ct = message.getContentType();
          if (message.getCharacterEncoding() != null)
            ct = ct + "; charset=" + message.getCharacterEncoding().name();
          HttpHeaders.setHeader(res, CONTENT_TYPE, ct);
        }

        final boolean sseStarted = message.shouldStartActor();
        if (trackSession(sseStarted)) {
          final String sessionId = UUID.randomUUID().toString();
          res.headers()
              .add(SET_COOKIE, ServerCookieEncoder.STRICT.encode(SESSION_COOKIE_KEY, sessionId));
          startSession(sessionId, actorContext);
        }
        if (!sseStarted) {
          final String stringBody = message.getStringBody();
          long contentLength = 0L;
          if (stringBody != null) contentLength = stringBody.getBytes().length;
          else {
            final ByteBuffer byteBufferBody = message.getByteBufferBody();
            if (byteBufferBody != null) contentLength = byteBufferBody.remaining();
          }
          res.headers().add(CONTENT_LENGTH, contentLength);
        }

        final HttpStreamActorAdapter httpStreamActorAdapter;
        if (sseStarted)
          // This will copy the request content, which must still be referenceable, doing before the
          // request handler
          // unallocates it (unfortunately it is explicitly reference-counted in Netty)
          httpStreamActorAdapter = new HttpStreamActorAdapter(ctx, req);
        else httpStreamActorAdapter = null;

        sendHttpResponse(ctx, req, res, false);

        if (sseStarted) {
          if (httpResponseEncoderName != null) {
            ctx.pipeline().remove(httpResponseEncoderName);
          } else {
            final ChannelPipeline pl = ctx.pipeline();
            final List<String> handlerKeysToBeRemoved = new ArrayList<>();
            for (final Map.Entry<String, ChannelHandler> e : pl) {
              if (e.getValue() instanceof HttpResponseEncoder)
                handlerKeysToBeRemoved.add(e.getKey());
            }
            for (final String k : handlerKeysToBeRemoved) pl.remove(k);
          }

          try {
            message.getFrom().send(new HttpStreamOpened(httpStreamActorAdapter.ref(), message));
          } catch (SuspendExecution e) {
            throw new AssertionError(e);
          }
        }

        return true;
      } finally {
        if (actor != null) actor.unwatch();
      }
    }
 public static void removeTimeoutHandler(ChannelPipeline pipeline) {
   if (pipeline.get(READ_TIMEOUT_HANDLER_NAME) != null) {
     pipeline.remove(READ_TIMEOUT_HANDLER_NAME);
   }
 }