From bf061bfaf25d1d5f1c50e96d07aba531f624dc4a Mon Sep 17 00:00:00 2001 From: Gravit Date: Mon, 1 Oct 2018 16:42:42 +0700 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=BE=D1=85=D1=80=D0=B0=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D1=85=20?= =?UTF-8?q?=D0=B2=20=D1=81=D0=B5=D1=81=D1=81=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../socket/websocket/WebSocketFrameHandler.java | 10 ++++++++-- .../socket/websocket/WebSocketService.java | 5 +++-- .../socket/websocket/json/EchoResponse.java | 5 +++-- .../socket/websocket/json/JsonResponseInterface.java | 3 ++- .../socket/websocket/json/SimpleResponse.java | 3 ++- .../socket/websocket/json/auth/AuthResponse.java | 12 +++++++----- 6 files changed, 25 insertions(+), 13 deletions(-) diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/WebSocketFrameHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/WebSocketFrameHandler.java index 7e0d37a0..ec879c5a 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/WebSocketFrameHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/WebSocketFrameHandler.java @@ -2,36 +2,42 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.group.ChannelGroup; import io.netty.channel.group.DefaultChannelGroup; import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; import io.netty.handler.codec.http.websocketx.WebSocketFrame; +import io.netty.util.AttributeKey; +import io.netty.util.AttributeMap; import io.netty.util.concurrent.GlobalEventExecutor; import ru.gravit.launchserver.LaunchServer; +import ru.gravit.launchserver.socket.Client; import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.LogHelper; - public class WebSocketFrameHandler extends SimpleChannelInboundHandler { public static final ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); public static LaunchServer server; public static GsonBuilder builder = new GsonBuilder(); public static WebSocketService service = new WebSocketService(LaunchServer.server,builder); + private Client client; static { service.registerResponses(); } @Override public void channelActive(ChannelHandlerContext ctx) { LogHelper.debug("New client %s", IOHelper.getIP(ctx.channel().remoteAddress())); + client = new Client(0); channels.add(ctx.channel()); } @Override protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) throws Exception { // ping and pong frames already handled + if (frame instanceof TextWebSocketFrame) { - service.process(ctx, (TextWebSocketFrame) frame); + service.process(ctx, (TextWebSocketFrame) frame, client); } else { String message = "unsupported frame type: " + frame.getClass().getName(); throw new UnsupportedOperationException(message); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/WebSocketService.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/WebSocketService.java index d0b82ebd..5ffc701c 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/WebSocketService.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/WebSocketService.java @@ -5,6 +5,7 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; import ru.gravit.launchserver.LaunchServer; +import ru.gravit.launchserver.socket.Client; import ru.gravit.launchserver.socket.websocket.json.EchoResponse; import ru.gravit.launchserver.socket.websocket.json.JsonResponseAdapter; import ru.gravit.launchserver.socket.websocket.json.JsonResponseInterface; @@ -26,12 +27,12 @@ public WebSocketService(LaunchServer server, GsonBuilder gson) { private final Gson gson; private final GsonBuilder gsonBuiler; - void process(ChannelHandlerContext ctx, TextWebSocketFrame frame) + void process(ChannelHandlerContext ctx, TextWebSocketFrame frame, Client client) { String request = frame.text(); JsonResponseInterface response = gson.fromJson(request, JsonResponseInterface.class); try { - response.execute(this,ctx); + response.execute(this,ctx,client); } catch (Exception e) { LogHelper.error(e); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/EchoResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/EchoResponse.java index 22fae64d..979594a1 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/EchoResponse.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/EchoResponse.java @@ -1,6 +1,7 @@ package ru.gravit.launchserver.socket.websocket.json; import io.netty.channel.ChannelHandlerContext; +import ru.gravit.launchserver.socket.Client; import ru.gravit.launchserver.socket.websocket.WebSocketService; import ru.gravit.utils.helper.LogHelper; @@ -17,8 +18,8 @@ public String getType() { } @Override - public void execute(WebSocketService service,ChannelHandlerContext ctx) { - LogHelper.info("Echo: %s",echo); + public void execute(WebSocketService service,ChannelHandlerContext ctx, Client client) { + LogHelper.info("Echo: %s, isAuth %s",echo,client.isAuth ? "true" : "false"); service.sendObject(ctx,new Result(echo)); } public class Result diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/JsonResponseInterface.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/JsonResponseInterface.java index 37c0ad95..20ea0a38 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/JsonResponseInterface.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/JsonResponseInterface.java @@ -2,9 +2,10 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.http.websocketx.WebSocketFrame; +import ru.gravit.launchserver.socket.Client; import ru.gravit.launchserver.socket.websocket.WebSocketService; public interface JsonResponseInterface { String getType(); - void execute(WebSocketService service,ChannelHandlerContext ctx) throws Exception; + void execute(WebSocketService service,ChannelHandlerContext ctx, Client client) throws Exception; } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/SimpleResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/SimpleResponse.java index db90577d..8c6f34f7 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/SimpleResponse.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/SimpleResponse.java @@ -1,6 +1,7 @@ package ru.gravit.launchserver.socket.websocket.json; import io.netty.channel.ChannelHandlerContext; +import ru.gravit.launchserver.socket.Client; import ru.gravit.launchserver.socket.websocket.WebSocketService; public class SimpleResponse implements JsonResponseInterface { @@ -10,7 +11,7 @@ public String getType() { } @Override - public void execute(WebSocketService service, ChannelHandlerContext ctx) throws Exception { + public void execute(WebSocketService service, ChannelHandlerContext ctx, Client client) throws Exception { } } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/auth/AuthResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/auth/AuthResponse.java index 31661c7e..9469acba 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/auth/AuthResponse.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/auth/AuthResponse.java @@ -9,6 +9,7 @@ import ru.gravit.launchserver.auth.hwid.HWIDException; import ru.gravit.launchserver.auth.provider.AuthProvider; import ru.gravit.launchserver.auth.provider.AuthProviderResult; +import ru.gravit.launchserver.socket.Client; import ru.gravit.launchserver.socket.websocket.WebSocketService; import ru.gravit.launchserver.socket.websocket.json.JsonResponseInterface; import ru.gravit.utils.helper.IOHelper; @@ -37,7 +38,7 @@ public String getType() { } @Override - public void execute(WebSocketService service, ChannelHandlerContext ctx) throws Exception { + public void execute(WebSocketService service, ChannelHandlerContext ctx, Client clientData) throws Exception { try { String ip = IOHelper.getIP(ctx.channel().remoteAddress()); if (LaunchServer.server.limiter.isLimit(ip)) { @@ -56,13 +57,14 @@ public void execute(WebSocketService service, ChannelHandlerContext ctx) throws if (!p.object.isWhitelistContains(login)) { throw new AuthException(LaunchServer.server.config.whitelistRejectString); } - //clientData.profile = p.object; + clientData.profile = p.object; } } - //if(clientData.profile == null) { - // throw new AuthException("You profile not found"); - //} + if(clientData.profile == null) { + throw new AuthException("You profile not found"); + } LaunchServer.server.config.hwidHandler.check(hwid, result.username); + clientData.isAuth = true; service.sendObject(ctx,new WebSocketService.SuccessResult("auth")); } catch (AuthException | HWIDException e) {