All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.netflix.netty.common.SourceAddressChannelHandler Maven / Gradle / Ivy

There is a newer version: 2.5.13
Show newest version
/*
 * Copyright 2018 Netflix, Inc.
 *
 *      Licensed under the Apache License, Version 2.0 (the "License");
 *      you may not use this file except in compliance with the License.
 *      You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 *      Unless required by applicable law or agreed to in writing, software
 *      distributed under the License is distributed on an "AS IS" BASIS,
 *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *      See the License for the specific language governing permissions and
 *      limitations under the License.
 */

package com.netflix.netty.common;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.AttributeKey;

import java.net.InetSocketAddress;
import java.net.SocketAddress;

/**
 * Stores the source IP address as an attribute of the channel. This has the advantage of allowing
 * us to overwrite it if we have more info (eg. ELB sends a HAProxyMessage with info of REAL source
 * host + port).
 *
 * User: [email protected]
 * Date: 4/14/16
 * Time: 4:29 PM
 */
public class SourceAddressChannelHandler extends ChannelInboundHandlerAdapter
{
    public static final AttributeKey ATTR_SOURCE_INET_ADDR = AttributeKey.newInstance("_source_inet_addr");
    public static final AttributeKey ATTR_SOURCE_ADDRESS = AttributeKey.newInstance("_source_address");
    public static final AttributeKey ATTR_SOURCE_PORT = AttributeKey.newInstance("_source_port");
    public static final AttributeKey ATTR_LOCAL_INET_ADDR = AttributeKey.newInstance("_local_inet_addr");
    public static final AttributeKey ATTR_LOCAL_ADDRESS = AttributeKey.newInstance("_local_address");
    public static final AttributeKey ATTR_LOCAL_PORT = AttributeKey.newInstance("_local_port");

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception
    {
        InetSocketAddress sourceAddress = sourceAddress(ctx.channel());
        ctx.channel().attr(ATTR_SOURCE_INET_ADDR).setIfAbsent(sourceAddress);
        ctx.channel().attr(ATTR_SOURCE_ADDRESS).setIfAbsent(sourceAddress.getAddress().getHostAddress());
        ctx.channel().attr(ATTR_SOURCE_PORT).setIfAbsent(sourceAddress.getPort());

        InetSocketAddress localAddress = localAddress(ctx.channel());
        ctx.channel().attr(ATTR_LOCAL_INET_ADDR).setIfAbsent(localAddress);
        ctx.channel().attr(ATTR_LOCAL_ADDRESS).setIfAbsent(localAddress.getAddress().getHostAddress());
        ctx.channel().attr(ATTR_LOCAL_PORT).setIfAbsent(localAddress.getPort());

        super.channelActive(ctx);
    }

    private InetSocketAddress sourceAddress(Channel channel)
    {
        SocketAddress remoteSocketAddr = channel.remoteAddress();
        if (null != remoteSocketAddr && InetSocketAddress.class.isAssignableFrom(remoteSocketAddr.getClass())) {
            InetSocketAddress inetSocketAddress = (InetSocketAddress) remoteSocketAddr;
            if (inetSocketAddress.getAddress() != null) {
                return inetSocketAddress;
            }
        }
        return null;
    }

    private InetSocketAddress localAddress(Channel channel)
    {
        SocketAddress localSocketAddress = channel.localAddress();
        if (null != localSocketAddress && InetSocketAddress.class.isAssignableFrom(localSocketAddress.getClass())) {
            InetSocketAddress inetSocketAddress = (InetSocketAddress) localSocketAddress;
            if (inetSocketAddress.getAddress() != null) {
                return inetSocketAddress;
            }
        }
        return null;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy