netty获取websocket通过nginx转发后的真实IP

简单介绍:netty做服务器的websocket项目,需要取得用户的真实IP,线上项目前面加了Nginx代理;

没有Nginx的的话,netty可以通过一下代码获取客户端IP,加了取到的是Nginx主机的IP:

InetSocketAddress address = (InetSocketAddress) channel.remoteAddress();
String ip = address.getAddress().getHostAddress();

所以需要在Nginx转发真实IP

http {
    include       mime.types;
    default_type  application/octet-stream;
    server {
		listen 39200;
		server_name www.aaa.com:39200;
		#防XSS攻擊
		add_header X-Xss-Protection 1;
		location / {
			# WebSocket Header
			proxy_http_version 1.1;
			proxy_set_header Upgrade websocket;
			proxy_set_header Connection "Upgrade";
			
			# 将客户端的 Host 和 IP 信息一并转发到对应节点  
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_set_header Host $http_host;
			
			# 客户端与服务端无交互 60s 后自动断开连接,请根据实际业务场景设置
			proxy_read_timeout 300s ;
			# 执行代理访问真实服务器
			proxy_pass http://192.168.0.41:39200;
		}
	}
}

首先看看服务器用到的handler:

pipeline.addLast(new HttpServerCodec());// HTTP编码解码器
pipeline.addLast(new HttpObjectAggregator(wsHttpObjectAggregator));// 把HTTP头、HTTP体拼成完整的HTTP请求
pipeline.addLast(new WebSocketServerProtocolHandler("/"));//完成http到websocket的升级,相当于只是建立连接过程中用到
pipeline.addLast(new NettyWsTextHandler());//自定义消息处理类

NettyWsTextHandler:唯一自己实现的处理消息类:

public class NettyWsTextHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {
	@Override
	public void channelActive(ChannelHandlerContext session) throws Exception {
		LoggerUtil.info("Server  channelActive: {}", session);
	}
	@Override
	protected void channelRead0(ChannelHandlerContext session, TextWebSocketFrame msg) throws Exception {
    	LoggerUtil.info("message: {}", msg.text());
	}
}

这步的消息已经是websocket的数据了,拿不到http里面的header信息,比如nginx转发的X-Real-IP

所以需要加一个handle专门用来获取http数据,如下:

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpHeaders;

public class HttpHeadersHandler extends ChannelInboundHandlerAdapter {
	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		if (msg instanceof FullHttpRequest) {
			HttpHeaders headers = ((FullHttpRequest) msg).headers();
			String ip = headers.get("X-Real-IP");
			System.out.println("Realip:" + ip);
		}
		ctx.fireChannelRead(msg);
	}
}

要注意,集成:ChannelInboundHandlerAdapter 不要集成:SimpleChannelInboundHandler  后者需要维护引用计数

添加后的handler管道如下:

ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new HttpServerCodec());// HTTP编码解码器
pipeline.addLast(new HttpObjectAggregator(wsHttpObjectAggregator));// 把HTTP头、HTTP体拼成完整的HTTP请求
// 获取header需要在http到websocket升级完成之前,升级后就取不到了
pipeline.addLast(new HttpHeadersHandler());
pipeline.addLast(new WebSocketServerProtocolHandler(wsPath));// 完成握手,http到websocket升级
pipeline.addLast(new NettyWsTextHandler());

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值