Example usage for io.netty.handler.codec.haproxy HAProxyCommand PROXY

List of usage examples for io.netty.handler.codec.haproxy HAProxyCommand PROXY

Introduction

In this page you can find the example usage for io.netty.handler.codec.haproxy HAProxyCommand PROXY.

Prototype

HAProxyCommand PROXY

To view the source code for io.netty.handler.codec.haproxy HAProxyCommand PROXY.

Click Source Link

Document

The PROXY command represents a connection that was established on behalf of another node, and reflects the original connection endpoints.

Usage

From source file:org.dcache.xrootd.plugins.ProxyAccessLogHandler.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof HAProxyMessage) {
        HAProxyMessage proxyMessage = (HAProxyMessage) msg;
        if (proxyMessage.command() == HAProxyCommand.PROXY) {
            InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress();
            InetSocketAddress localAddress = (InetSocketAddress) ctx.channel().localAddress();
            String sourceAddress = proxyMessage.sourceAddress();
            String destinationAddress = proxyMessage.destinationAddress();

            if (proxyMessage.proxiedProtocol() == HAProxyProxiedProtocol.UNKNOWN) {
                NetLoggerBuilder log = new NetLoggerBuilder(INFO, "org.dcache.xrootd.connection.start")
                        .omitNullValues();
                log.add("session", CDC.getSession());
                log.add("socket.remote", remoteAddress);
                log.add("socket.local", localAddress);
                log.toLogger(logger);/*from  w  ww .j  a v  a  2  s. c om*/
                replaceWith(ctx, handler);
            } else if (!Objects.equals(destinationAddress, localAddress.getAddress().getHostAddress())) {
                /* The above check is a workaround for what looks like a bug in HAProxy - health checks
                 * should generate a LOCAL command, but it appears they do actually use PROXY.
                 */
                NetLoggerBuilder log = new NetLoggerBuilder(INFO, "org.dcache.xrootd.connection.start")
                        .omitNullValues();
                log.add("session", CDC.getSession());
                log.add("socket.remote", HostAndPort.fromParts(sourceAddress, proxyMessage.sourcePort()));
                log.add("socket.proxy",
                        HostAndPort.fromParts(destinationAddress, proxyMessage.destinationPort()));
                log.add("socket.local", localAddress);
                log.toLogger(logger);
                replaceWith(ctx, handler);
            }
        }
    }
    super.channelRead(ctx, msg);
}