From fe57424e2ad62e3f0aae0f7c59322bd126bef76d Mon Sep 17 00:00:00 2001 From: Gravit Date: Thu, 9 May 2019 20:05:53 +0700 Subject: [PATCH] [FEATURE] IP Forwarding --- .../websocket/WebSocketFrameHandler.java | 3 +-- .../launchserver/websocket/WebSocketService.java | 12 +++++++++--- .../launchserver/websocket/json/SimpleResponse.java | 1 + .../websocket/json/auth/AuthResponse.java | 1 - 4 files changed, 11 insertions(+), 6 deletions(-) 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 11ff7414..75babfb7 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/WebSocketFrameHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/WebSocketFrameHandler.java @@ -46,9 +46,8 @@ public void channelActive(ChannelHandlerContext ctx) { @Override protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) { // ping and pong frames already handled - if (frame instanceof TextWebSocketFrame) { - service.process(ctx, (TextWebSocketFrame) frame, client); + service.process(ctx, (TextWebSocketFrame) frame, client, context.ip); } else if ((frame instanceof PingWebSocketFrame)) { frame.content().retain(); ctx.channel().writeAndFlush(new PongWebSocketFrame(frame.content())); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/WebSocketService.java b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/WebSocketService.java index 25451577..27564513 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/WebSocketService.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/WebSocketService.java @@ -35,6 +35,7 @@ import ru.gravit.launchserver.websocket.json.update.LauncherResponse; import ru.gravit.launchserver.websocket.json.update.UpdateListResponse; import ru.gravit.launchserver.websocket.json.update.UpdateResponse; +import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.LogHelper; import java.lang.reflect.Type; @@ -62,7 +63,7 @@ public WebSocketService(ChannelGroup channels, LaunchServer server, GsonBuilder private final GsonBuilder gsonBuiler; @SuppressWarnings("unchecked") - void process(ChannelHandlerContext ctx, TextWebSocketFrame frame, Client client) { + void process(ChannelHandlerContext ctx, TextWebSocketFrame frame, Client client, String ip) { String request = frame.text(); JsonResponseInterface response = gson.fromJson(request, JsonResponseInterface.class); if(server.config.netty.proxy.enabled) @@ -77,6 +78,8 @@ void process(ChannelHandlerContext ctx, TextWebSocketFrame frame, Client client) simpleResponse.server = server; simpleResponse.service = this; simpleResponse.ctx = ctx; + if(ip != null) simpleResponse.ip = ip; + else simpleResponse.ip = IOHelper.getIP(ctx.channel().remoteAddress()); origRequestUUID = simpleResponse.requestUUID; } LogHelper.debug("Proxy %s request", response.getType()); @@ -122,11 +125,12 @@ void process(ChannelHandlerContext ctx, TextWebSocketFrame frame, Client client) } sendObject(ctx, event); } + return; } } - process(ctx,response, client); + process(ctx,response, client, ip); } - void process(ChannelHandlerContext ctx, JsonResponseInterface response, Client client) + void process(ChannelHandlerContext ctx, JsonResponseInterface response, Client client, String ip) { if(response instanceof SimpleResponse) { @@ -134,6 +138,8 @@ void process(ChannelHandlerContext ctx, JsonResponseInterface response, Client c simpleResponse.server = server; simpleResponse.service = this; simpleResponse.ctx = ctx; + if(ip != null) simpleResponse.ip = ip; + else simpleResponse.ip = IOHelper.getIP(ctx.channel().remoteAddress()); } try { response.execute(ctx, client); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/SimpleResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/SimpleResponse.java index 9e3e4741..7b41fb48 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/SimpleResponse.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/SimpleResponse.java @@ -14,6 +14,7 @@ public abstract class SimpleResponse implements JsonResponseInterface { public transient LaunchServer server; public transient WebSocketService service; public transient ChannelHandlerContext ctx; + public transient String ip; public void sendResult(RequestEvent result) { result.requestUUID = requestUUID; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/auth/AuthResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/auth/AuthResponse.java index a824469e..c48d286d 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/auth/AuthResponse.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/auth/AuthResponse.java @@ -61,7 +61,6 @@ public String getType() { public void execute(ChannelHandlerContext ctx, Client clientData) throws Exception { try { AuthRequestEvent result = new AuthRequestEvent(); - String ip = IOHelper.getIP(ctx.channel().remoteAddress()); if ((authType == null || authType == ConnectTypes.CLIENT) && ( clientData == null || !clientData.checkSign )) { AuthProvider.authError("Don't skip Launcher Update"); return;