From 236b433d5fcdfd6973ccd77aee6a302f6421a14c Mon Sep 17 00:00:00 2001 From: Gravit Date: Thu, 9 May 2019 22:01:54 +0700 Subject: [PATCH] =?UTF-8?q?[FIX]=20=D0=95=D1=89=D0=B5=20=D0=BE=D0=B4=D0=B8?= =?UTF-8?q?=D0=BD=20=D0=B2=D0=B0=D1=80=D0=B8=D0=B0=D0=BD=D1=82=20=D1=80?= =?UTF-8?q?=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D0=B8=20NettyI?= =?UTF-8?q?pForwardHandler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../websocket/NettyIpForwardHandler.java | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/NettyIpForwardHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/NettyIpForwardHandler.java index 2393cc80..1adc5087 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/NettyIpForwardHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/NettyIpForwardHandler.java @@ -1,6 +1,7 @@ package ru.gravit.launchserver.websocket; import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToMessageCodec; import io.netty.handler.codec.MessageToMessageDecoder; import io.netty.handler.codec.http.HttpHeaders; import io.netty.handler.codec.http.HttpRequest; @@ -8,7 +9,7 @@ import java.util.List; -public class NettyIpForwardHandler extends MessageToMessageDecoder { +public class NettyIpForwardHandler extends MessageToMessageCodec { private NettyConnectContext context; public NettyIpForwardHandler(NettyConnectContext context) { @@ -16,6 +17,27 @@ public NettyIpForwardHandler(NettyConnectContext context) { this.context = context; } + @Override + protected void encode(ChannelHandlerContext ctx, HttpRequest msg, List out) throws Exception { + if (context.ip != null) { + out.add(msg); + return; + } + HttpHeaders headers = msg.headers(); + String realIP = null; + if (headers.contains("X-Forwarded-For")) { + realIP = headers.get("X-Forwarded-For"); + } + if (headers.contains("X-Real-IP")) { + realIP = headers.get("X-Real-IP"); + } + if (realIP != null) { + LogHelper.dev("Real IP address %s", realIP); + context.ip = realIP; + } else LogHelper.error("IpForwarding error. Headers not found"); + out.add(msg); + } + @Override protected void decode(ChannelHandlerContext ctx, HttpRequest msg, List out) throws Exception { if (context.ip != null) {