[FIX] Исправление записи сообщения NettyIpForwardHandler 3

This commit is contained in:
Gravit 2019-05-09 21:46:59 +07:00
parent 66f0a8b9d1
commit c8e9918cd9
No known key found for this signature in database
GPG key ID: 061981E1E85D3216

View file

@ -1,14 +1,14 @@
package ru.gravit.launchserver.websocket; package ru.gravit.launchserver.websocket;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandler; import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.DefaultHttpRequest;
import io.netty.handler.codec.http.HttpHeaders; import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.HttpRequest;
import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.LogHelper;
public class NettyIpForwardHandler extends ChannelInboundHandlerAdapter { import java.util.List;
public class NettyIpForwardHandler extends MessageToMessageDecoder<HttpRequest> {
private NettyConnectContext context; private NettyConnectContext context;
public NettyIpForwardHandler(NettyConnectContext context) { public NettyIpForwardHandler(NettyConnectContext context) {
@ -17,33 +17,23 @@ public NettyIpForwardHandler(NettyConnectContext context) {
} }
@Override @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { protected void decode(ChannelHandlerContext ctx, HttpRequest msg, List<Object> out) throws Exception {
//super.channelRead(ctx, msg); if (context.ip != null) {
if(context.ip != null) out.add(msg);
{
ctx.writeAndFlush(msg);
return; return;
} }
if(msg instanceof HttpRequest) HttpHeaders headers = msg.headers();
{ String realIP = null;
HttpRequest http = (HttpRequest) msg; if (headers.contains("X-Forwarded-For")) {
HttpHeaders headers = http.headers(); realIP = headers.get("X-Forwarded-For");
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");
} }
else LogHelper.error("IpForwarding error. Real message class %s", msg.getClass().getName()); if (headers.contains("X-Real-IP")) {
ctx.writeAndFlush(msg); 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);
} }
} }