mirror of
https://github.com/GravitLauncher/Launcher
synced 2025-01-09 09:09:46 +03:00
[FIX] Еще один вариант реализации NettyIpForwardHandler
This commit is contained in:
parent
c8e9918cd9
commit
236b433d5f
1 changed files with 23 additions and 1 deletions
|
@ -1,6 +1,7 @@
|
||||||
package ru.gravit.launchserver.websocket;
|
package ru.gravit.launchserver.websocket;
|
||||||
|
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
|
import io.netty.handler.codec.MessageToMessageCodec;
|
||||||
import io.netty.handler.codec.MessageToMessageDecoder;
|
import io.netty.handler.codec.MessageToMessageDecoder;
|
||||||
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;
|
||||||
|
@ -8,7 +9,7 @@
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class NettyIpForwardHandler extends MessageToMessageDecoder<HttpRequest> {
|
public class NettyIpForwardHandler extends MessageToMessageCodec<HttpRequest, HttpRequest> {
|
||||||
private NettyConnectContext context;
|
private NettyConnectContext context;
|
||||||
|
|
||||||
public NettyIpForwardHandler(NettyConnectContext context) {
|
public NettyIpForwardHandler(NettyConnectContext context) {
|
||||||
|
@ -16,6 +17,27 @@ public NettyIpForwardHandler(NettyConnectContext context) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void encode(ChannelHandlerContext ctx, HttpRequest msg, List<Object> 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
|
@Override
|
||||||
protected void decode(ChannelHandlerContext ctx, HttpRequest msg, List<Object> out) throws Exception {
|
protected void decode(ChannelHandlerContext ctx, HttpRequest msg, List<Object> out) throws Exception {
|
||||||
if (context.ip != null) {
|
if (context.ip != null) {
|
||||||
|
|
Loading…
Reference in a new issue