简体   繁体   中英

Netty - getRemoteAddress returns different port every time

I am using LittleProxy which implements Netty . I am trying to return the IP address and port of the source of an HTTP request. I use the getRemoteAddress method, but the returned port is always different. It seems to add up after every request? I must be missing some insight here.

After some searching, I tried downcasting it to an InetSocketAddress. But the results are the same:

private void processRequest(final ChannelHandlerContext ctx, 
        final MessageEvent me) {

        final HttpRequest request = (HttpRequest) me.getMessage();
        //THOMAS EDIT FOR OUTPUT REMOTE ID AND PORT
        InetSocketAddress inetAddr = (InetSocketAddress)me.getRemoteAddress();        
        System.out.println("Source IP: " + inetAddr.getAddress());
        System.out.println("Source Port: " + inetAddr.getPort());

Netty behaves like a Gateway or NAT. the IpAddress and Ports it give are its own port numbers.

what you can try is to get the HTTPHeader X-Forwarded-For

Option 2:

use ChannelHandlerContext instead of MessageEvent

ctx.getChannel().getRemoteAddress();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM