diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java index 0062d39e..c0f7fbb1 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java @@ -275,6 +275,7 @@ public class LauncherConf public class NettyConfig { public boolean fileServerEnabled; public boolean sendExceptionEnabled; + public boolean ipForwarding; public String launcherURL; public String downloadURL; public String launcherEXEURL; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/LauncherNettyServer.java b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/LauncherNettyServer.java index 9da937b3..3a0f6f0e 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/LauncherNettyServer.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/LauncherNettyServer.java @@ -40,13 +40,15 @@ public LauncherNettyServer() { @Override public void initChannel(NioSocketChannel ch) { ChannelPipeline pipeline = ch.pipeline(); + NettyConnectContext context = new NettyConnectContext(); //p.addLast(new LoggingHandler(LogLevel.INFO)); pipeline.addLast(new HttpServerCodec()); pipeline.addLast(new HttpObjectAggregator(65536)); pipeline.addLast(new WebSocketServerCompressionHandler()); pipeline.addLast(new WebSocketServerProtocolHandler(WEBSOCKET_PATH, null, true)); if (LaunchServer.server.config.netty.fileServerEnabled) pipeline.addLast(new FileServerHandler(LaunchServer.server.updatesDir, true)); - pipeline.addLast(new WebSocketFrameHandler()); + if (LaunchServer.server.config.netty.ipForwarding) pipeline.addLast(new NettyIpForwardHandler(context)); + pipeline.addLast(new WebSocketFrameHandler(context)); } }); if(config.proxy != null && config.proxy.enabled) diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/NettyConnectContext.java b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/NettyConnectContext.java new file mode 100644 index 00000000..c1a66c8a --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/NettyConnectContext.java @@ -0,0 +1,5 @@ +package ru.gravit.launchserver.websocket; + +public class NettyConnectContext { + public String ip = null; +} diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/NettyIpForwardHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/NettyIpForwardHandler.java new file mode 100644 index 00000000..a97c8c30 --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/NettyIpForwardHandler.java @@ -0,0 +1,41 @@ +package ru.gravit.launchserver.websocket; + +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandler; +import io.netty.channel.ChannelInboundHandlerAdapter; +import io.netty.handler.codec.http.DefaultHttpRequest; +import io.netty.handler.codec.http.HttpHeaders; +import ru.gravit.utils.helper.LogHelper; + +public class NettyIpForwardHandler extends ChannelInboundHandlerAdapter { + private NettyConnectContext context; + + public NettyIpForwardHandler(NettyConnectContext context) { + super(); + this.context = context; + } + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + super.channelRead(ctx, msg); + if(context.ip != null) return; + if(msg instanceof DefaultHttpRequest) + { + DefaultHttpRequest http = (DefaultHttpRequest) msg; + HttpHeaders headers = http.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; + } + } + } +} diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/NettyServerSocketHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/NettyServerSocketHandler.java index d33ef123..318a29ad 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/NettyServerSocketHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/NettyServerSocketHandler.java @@ -100,7 +100,7 @@ public void run() { } catch (IOException e) { e.printStackTrace(); }*/ - LogHelper.info("Starting server socket thread"); + LogHelper.info("Starting netty server socket thread"); //SSLEngine engine = sc.createSSLEngine(); //engine.setUseClientMode(false); WebSocketFrameHandler.server = LaunchServer.server; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/WebSocketFrameHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/WebSocketFrameHandler.java index 01d88376..11ff7414 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/WebSocketFrameHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/WebSocketFrameHandler.java @@ -17,6 +17,12 @@ public class WebSocketFrameHandler extends SimpleChannelInboundHandler