Search in sources :

Example 1 with HAProxyMessageDecoder

use of io.netty.handler.codec.haproxy.HAProxyMessageDecoder in project zuul by Netflix.

the class HAProxyMessageChannelHandlerTest method setClientDestPortForHAPM.

@Test
public void setClientDestPortForHAPM() {
    EmbeddedChannel channel = new EmbeddedChannel();
    // This is normally done by Server.
    channel.attr(Server.CONN_DIMENSIONS).set(Attrs.newInstance());
    // This is to emulate `ElbProxyProtocolChannelHandler`
    channel.pipeline().addLast(HAProxyMessageDecoder.class.getSimpleName(), new HAProxyMessageDecoder()).addLast(HAProxyMessageChannelHandler.class.getSimpleName(), new HAProxyMessageChannelHandler());
    ByteBuf buf = Unpooled.wrappedBuffer("PROXY TCP4 192.168.0.1 124.123.111.111 10008 443\r\n".getBytes(StandardCharsets.US_ASCII));
    channel.writeInbound(buf);
    Object result = channel.readInbound();
    assertNull(result);
    InetSocketAddress destAddress = channel.attr(SourceAddressChannelHandler.ATTR_PROXY_PROTOCOL_DESTINATION_ADDRESS).get();
    InetSocketAddress srcAddress = (InetSocketAddress) channel.attr(SourceAddressChannelHandler.ATTR_REMOTE_ADDR).get();
    assertEquals("124.123.111.111", destAddress.getHostString());
    assertEquals(443, destAddress.getPort());
    assertEquals("192.168.0.1", srcAddress.getHostString());
    assertEquals(10008, srcAddress.getPort());
    Attrs attrs = channel.attr(Server.CONN_DIMENSIONS).get();
    Integer port = HAProxyMessageChannelHandler.HAPM_DEST_PORT.get(attrs);
    assertEquals(443, port.intValue());
    String sourceIpVersion = HAProxyMessageChannelHandler.HAPM_SRC_IP_VERSION.get(attrs);
    assertEquals("v4", sourceIpVersion);
    String destIpVersion = HAProxyMessageChannelHandler.HAPM_DEST_IP_VERSION.get(attrs);
    assertEquals("v4", destIpVersion);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) HAProxyMessageDecoder(io.netty.handler.codec.haproxy.HAProxyMessageDecoder) Attrs(com.netflix.zuul.Attrs) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 2 with HAProxyMessageDecoder

use of io.netty.handler.codec.haproxy.HAProxyMessageDecoder in project vert.x by eclipse.

the class HttpServerWorker method handle.

@Override
public void handle(Channel ch) {
    if (HAProxyMessageCompletionHandler.canUseProxyProtocol(options.isUseProxyProtocol())) {
        IdleStateHandler idle;
        io.netty.util.concurrent.Promise<Channel> p = ch.eventLoop().newPromise();
        ch.pipeline().addLast(new HAProxyMessageDecoder());
        if (options.getProxyProtocolTimeout() > 0) {
            ch.pipeline().addLast("idle", idle = new IdleStateHandler(0, 0, options.getProxyProtocolTimeout(), options.getProxyProtocolTimeoutUnit()));
        } else {
            idle = null;
        }
        ch.pipeline().addLast(new HAProxyMessageCompletionHandler(p));
        p.addListener((GenericFutureListener<Future<Channel>>) future -> {
            if (future.isSuccess()) {
                if (idle != null) {
                    ch.pipeline().remove(idle);
                }
                configurePipeline(future.getNow());
            } else {
                handleException(future.cause());
            }
        });
    } else {
        configurePipeline(ch);
    }
}
Also used : HAProxyMessageCompletionHandler(io.vertx.core.net.impl.HAProxyMessageCompletionHandler) SniHandler(io.netty.handler.ssl.SniHandler) LoggingHandler(io.netty.handler.logging.LoggingHandler) FlashPolicyHandler(io.vertx.core.http.impl.cgbystrom.FlashPolicyHandler) ContextInternal(io.vertx.core.impl.ContextInternal) SslHandshakeCompletionHandler(io.vertx.core.net.impl.SslHandshakeCompletionHandler) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) Supplier(java.util.function.Supplier) Unpooled(io.netty.buffer.Unpooled) HttpServerMetrics(io.vertx.core.spi.metrics.HttpServerMetrics) IdleState(io.netty.handler.timeout.IdleState) io.netty.channel(io.netty.channel) VertxHandler(io.vertx.core.net.impl.VertxHandler) IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) VertxInternal(io.vertx.core.impl.VertxInternal) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener) SSLHelper(io.vertx.core.net.impl.SSLHelper) StandardCharsets(java.nio.charset.StandardCharsets) HAProxyMessageDecoder(io.netty.handler.codec.haproxy.HAProxyMessageDecoder) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) EventLoopContext(io.vertx.core.impl.EventLoopContext) SslHandler(io.netty.handler.ssl.SslHandler) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpContentDecompressor(io.netty.handler.codec.http.HttpContentDecompressor) Future(io.netty.util.concurrent.Future) Handler(io.vertx.core.Handler) HAProxyMessageCompletionHandler(io.vertx.core.net.impl.HAProxyMessageCompletionHandler) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) HAProxyMessageDecoder(io.netty.handler.codec.haproxy.HAProxyMessageDecoder) Future(io.netty.util.concurrent.Future)

Aggregations

HAProxyMessageDecoder (io.netty.handler.codec.haproxy.HAProxyMessageDecoder)2 Attrs (com.netflix.zuul.Attrs)1 ByteBuf (io.netty.buffer.ByteBuf)1 Unpooled (io.netty.buffer.Unpooled)1 io.netty.channel (io.netty.channel)1 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)1 HttpContentDecompressor (io.netty.handler.codec.http.HttpContentDecompressor)1 LoggingHandler (io.netty.handler.logging.LoggingHandler)1 SniHandler (io.netty.handler.ssl.SniHandler)1 SslHandler (io.netty.handler.ssl.SslHandler)1 ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)1 IdleState (io.netty.handler.timeout.IdleState)1 IdleStateEvent (io.netty.handler.timeout.IdleStateEvent)1 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)1 Future (io.netty.util.concurrent.Future)1 GenericFutureListener (io.netty.util.concurrent.GenericFutureListener)1 Handler (io.vertx.core.Handler)1 HttpServerOptions (io.vertx.core.http.HttpServerOptions)1 FlashPolicyHandler (io.vertx.core.http.impl.cgbystrom.FlashPolicyHandler)1 ContextInternal (io.vertx.core.impl.ContextInternal)1