From 956cef2cd6f488d8bdae177f04498e4d59f372d5 Mon Sep 17 00:00:00 2001 From: Gravit Date: Sun, 10 Feb 2019 17:54:19 +0700 Subject: [PATCH] =?UTF-8?q?[FEATURE]=20=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8?= =?UTF-8?q?=D0=B7=D0=BE=D0=B2=D0=B0=D0=BD=D0=B0=20=D0=BE=D0=B1=D1=80=D0=B0?= =?UTF-8?q?=D0=B1=D0=BE=D1=82=D0=BA=D0=B0=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BE?= =?UTF-8?q?=D0=BA=20=D0=B2=20WebSockets?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../socket/websocket/WebSocketService.java | 13 ---------- .../json/admin/ExecCommandResponse.java | 5 ++-- .../websocket/json/auth/AuthResponse.java | 3 ++- .../json/auth/CheckServerResponse.java | 5 ++-- .../json/auth/JoinServerResponse.java | 5 ++-- .../websocket/json/auth/ProfilesResponse.java | 3 ++- .../json/auth/SetProfileResponse.java | 7 +++--- .../json/update/LauncherResponse.java | 3 ++- .../json/update/UpdateListResponse.java | 3 ++- .../websockets/ClientWebSocketService.java | 1 + .../websockets/LegacyRequestBridge.java | 7 ++++++ .../request/websockets/WaitEventHandler.java | 6 ++++- .../events/request/ErrorRequestEvent.java | 25 +++++++++++++++++++ 13 files changed, 59 insertions(+), 27 deletions(-) create mode 100644 libLauncher/src/main/java/ru/gravit/launcher/events/request/ErrorRequestEvent.java 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 277ff002..1fd291bc 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 @@ -99,19 +99,6 @@ public void sendEvent(EventResult obj) { channels.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj))); } - public static class ErrorResult implements ResultInterface { - public ErrorResult(String error) { - this.error = error; - } - - public final String error; - - @Override - public String getType() { - return "error"; - } - } - public static class SuccessResult { public SuccessResult(String requesttype) { this.requesttype = requesttype; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/admin/ExecCommandResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/admin/ExecCommandResponse.java index 3ecd04d9..457f3860 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/admin/ExecCommandResponse.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/admin/ExecCommandResponse.java @@ -1,6 +1,7 @@ package ru.gravit.launchserver.socket.websocket.json.admin; import io.netty.channel.ChannelHandlerContext; +import ru.gravit.launcher.events.request.ErrorRequestEvent; import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.socket.Client; import ru.gravit.launchserver.socket.websocket.WebSocketService; @@ -17,11 +18,11 @@ public String getType() { @Override public void execute(WebSocketService service, ChannelHandlerContext ctx, Client client) { if (!client.isAuth) { - service.sendObject(ctx, new WebSocketService.ErrorResult("Access denied")); + service.sendObject(ctx, new ErrorRequestEvent("Access denied")); return; } if (!client.permissions.canAdmin) { - service.sendObject(ctx, new WebSocketService.ErrorResult("Access denied")); + service.sendObject(ctx, new ErrorRequestEvent("Access denied")); return; } LaunchServer.server.commandHandler.eval(cmd, false); 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 1ede6f4e..2c650135 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 @@ -3,6 +3,7 @@ import io.netty.channel.ChannelHandlerContext; import ru.gravit.launcher.OshiHWID; import ru.gravit.launcher.events.request.AuthRequestEvent; +import ru.gravit.launcher.events.request.ErrorRequestEvent; import ru.gravit.launcher.profiles.ClientProfile; import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.auth.AuthException; @@ -113,7 +114,7 @@ public void execute(WebSocketService service, ChannelHandlerContext ctx, Client result.playerProfile = ProfileByUUIDResponse.getProfile(LaunchServer.server,uuid,aresult.username,client); service.sendObject(ctx, result); } catch (AuthException | HWIDException e) { - service.sendObject(ctx, new WebSocketService.ErrorResult(e.getMessage())); + service.sendObject(ctx, new ErrorRequestEvent(e.getMessage())); } } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/auth/CheckServerResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/auth/CheckServerResponse.java index b9c2e051..80a13120 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/auth/CheckServerResponse.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/auth/CheckServerResponse.java @@ -2,6 +2,7 @@ import io.netty.channel.ChannelHandlerContext; import ru.gravit.launcher.events.request.CheckServerRequestEvent; +import ru.gravit.launcher.events.request.ErrorRequestEvent; import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.auth.AuthException; import ru.gravit.launchserver.response.profile.ProfileByUUIDResponse; @@ -28,11 +29,11 @@ public void execute(WebSocketService service, ChannelHandlerContext ctx, Client if(result.uuid != null) result.playerProfile = ProfileByUUIDResponse.getProfile(LaunchServer.server,result.uuid,username,client); } catch (AuthException e) { - service.sendObject(ctx, new WebSocketService.ErrorResult(e.getMessage())); + service.sendObject(ctx, new ErrorRequestEvent(e.getMessage())); return; } catch (Exception e) { LogHelper.error(e); - service.sendObject(ctx, new WebSocketService.ErrorResult("Internal authHandler error")); + service.sendObject(ctx, new ErrorRequestEvent("Internal authHandler error")); return; } service.sendObject(ctx, new CheckServerRequestEvent()); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/auth/JoinServerResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/auth/JoinServerResponse.java index 56eeb0d2..85aa3dd7 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/auth/JoinServerResponse.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/auth/JoinServerResponse.java @@ -1,6 +1,7 @@ package ru.gravit.launchserver.socket.websocket.json.auth; import io.netty.channel.ChannelHandlerContext; +import ru.gravit.launcher.events.request.ErrorRequestEvent; import ru.gravit.launcher.events.request.JoinServerRequestEvent; import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.auth.AuthException; @@ -25,11 +26,11 @@ public void execute(WebSocketService service, ChannelHandlerContext ctx, Client try { success = LaunchServer.server.config.authHandler.joinServer(username, accessToken, serverID); } catch (AuthException e) { - service.sendObject(ctx, new WebSocketService.ErrorResult(e.getMessage())); + service.sendObject(ctx, new ErrorRequestEvent(e.getMessage())); return; } catch (Exception e) { LogHelper.error(e); - service.sendObject(ctx, new WebSocketService.ErrorResult("Internal authHandler error")); + service.sendObject(ctx, new ErrorRequestEvent("Internal authHandler error")); return; } service.sendObject(ctx, new JoinServerRequestEvent(success)); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/auth/ProfilesResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/auth/ProfilesResponse.java index 8e3014d3..e4610cc4 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/auth/ProfilesResponse.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/auth/ProfilesResponse.java @@ -1,6 +1,7 @@ package ru.gravit.launchserver.socket.websocket.json.auth; import io.netty.channel.ChannelHandlerContext; +import ru.gravit.launcher.events.request.ErrorRequestEvent; import ru.gravit.launcher.events.request.ProfilesRequestEvent; import ru.gravit.launcher.profiles.ClientProfile; import ru.gravit.launchserver.LaunchServer; @@ -20,7 +21,7 @@ public String getType() { public void execute(WebSocketService service, ChannelHandlerContext ctx, Client client) throws Exception { if(!client.checkSign) { - service.sendObject(ctx, new WebSocketService.ErrorResult("Access denied")); + service.sendObject(ctx, new ErrorRequestEvent("Access denied")); return; } service.sendObject(ctx, new ProfilesRequestEvent((List) LaunchServer.server.getProfiles())); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/auth/SetProfileResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/auth/SetProfileResponse.java index a4490683..dfe4e156 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/auth/SetProfileResponse.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/auth/SetProfileResponse.java @@ -1,6 +1,7 @@ package ru.gravit.launchserver.socket.websocket.json.auth; import io.netty.channel.ChannelHandlerContext; +import ru.gravit.launcher.events.request.ErrorRequestEvent; import ru.gravit.launcher.profiles.ClientProfile; import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.socket.Client; @@ -20,14 +21,14 @@ public String getType() { public void execute(WebSocketService service, ChannelHandlerContext ctx, Client client) throws Exception { if(!client.isAuth) { - service.sendObject(ctx, new WebSocketService.ErrorResult("Access denied")); + service.sendObject(ctx, new ErrorRequestEvent("Access denied")); return; } Collection profiles = LaunchServer.server.getProfiles(); for (ClientProfile p : profiles) { if (p.getTitle().equals(this.client)) { if (!p.isWhitelistContains(client.username)) { - service.sendObject(ctx, new WebSocketService.ErrorResult(LaunchServer.server.config.whitelistRejectString)); + service.sendObject(ctx, new ErrorRequestEvent(LaunchServer.server.config.whitelistRejectString)); return; } client.profile = p; @@ -35,6 +36,6 @@ public void execute(WebSocketService service, ChannelHandlerContext ctx, Client break; } } - service.sendObject(ctx, new WebSocketService.ErrorResult("Profile not found")); + service.sendObject(ctx, new ErrorRequestEvent("Profile not found")); } } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/update/LauncherResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/update/LauncherResponse.java index 9b78bfd5..59deb921 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/update/LauncherResponse.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/update/LauncherResponse.java @@ -1,6 +1,7 @@ package ru.gravit.launchserver.socket.websocket.json.update; import io.netty.channel.ChannelHandlerContext; +import ru.gravit.launcher.events.request.ErrorRequestEvent; import ru.gravit.launcher.events.request.LauncherRequestEvent; import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.socket.Client; @@ -52,7 +53,7 @@ public void execute(WebSocketService service, ChannelHandlerContext ctx, Client } else { service.sendObjectAndClose(ctx, new LauncherRequestEvent(true, EXE_URL)); } - } else service.sendObject(ctx, new WebSocketService.ErrorResult("Request launcher type error")); + } else service.sendObject(ctx, new ErrorRequestEvent("Request launcher type error")); } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/update/UpdateListResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/update/UpdateListResponse.java index aac849f6..fa4d484a 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/update/UpdateListResponse.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/update/UpdateListResponse.java @@ -1,6 +1,7 @@ package ru.gravit.launchserver.socket.websocket.json.update; import io.netty.channel.ChannelHandlerContext; +import ru.gravit.launcher.events.request.ErrorRequestEvent; import ru.gravit.launcher.events.request.UpdateListRequestEvent; import ru.gravit.launcher.hasher.HashedDir; import ru.gravit.launchserver.LaunchServer; @@ -19,7 +20,7 @@ public String getType() { @Override public void execute(WebSocketService service, ChannelHandlerContext ctx, Client client) { if (!client.isAuth) { - service.sendObject(ctx, new WebSocketService.ErrorResult("Access denied")); + service.sendObject(ctx, new ErrorRequestEvent("Access denied")); return; } HashedDir hdir = LaunchServer.server.updatesDirMap.get(dir).object; diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientWebSocketService.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientWebSocketService.java index 8c90bd2b..580ee517 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientWebSocketService.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientWebSocketService.java @@ -86,6 +86,7 @@ public void registerResults() { registerResult("batchProfileByUsername", BatchProfileByUsernameRequestEvent.class); registerResult("profiles", ProfilesRequestEvent.class); registerResult("updateList", UpdateListRequestEvent.class); + registerResult("error", ErrorRequestEvent.class); } public void registerHandler(EventHandler eventHandler) diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/LegacyRequestBridge.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/LegacyRequestBridge.java index 522ebfe3..72728fcb 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/LegacyRequestBridge.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/LegacyRequestBridge.java @@ -2,6 +2,8 @@ import com.google.gson.GsonBuilder; import ru.gravit.launcher.Launcher; +import ru.gravit.launcher.events.request.ErrorRequestEvent; +import ru.gravit.launcher.request.RequestException; import ru.gravit.launcher.request.ResultInterface; import ru.gravit.utils.helper.LogHelper; @@ -24,6 +26,11 @@ public static ResultInterface sendRequest(RequestInterface request) throws IOExc } ResultInterface result = e.result; waitEventHandler.requests.remove(e); + if(e.result.getType().equals("error")) + { + ErrorRequestEvent errorRequestEvent = (ErrorRequestEvent) e.result; + throw new RequestException(errorRequestEvent.error); + } return result; } public static void initWebSockets(String address, int port) diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/WaitEventHandler.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/WaitEventHandler.java index 66475c71..cc1352b0 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/WaitEventHandler.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/WaitEventHandler.java @@ -1,6 +1,7 @@ package ru.gravit.launcher.request.websockets; import ru.gravit.launcher.request.ResultInterface; +import ru.gravit.utils.helper.LogHelper; import java.util.HashSet; @@ -8,10 +9,13 @@ public class WaitEventHandler implements ClientWebSocketService.EventHandler { public HashSet requests = new HashSet<>(); @Override public void process(ResultInterface result) { + LogHelper.debug("Processing event %s type", result.getType()); for(ResultEvent r : requests) { - if(r.type.equals(result.getType())) + LogHelper.subDebug("Processing %s", r.type); + if(r.type.equals(result.getType()) || result.getType().equals("error")) { + LogHelper.debug("Event %s type", r.type); synchronized (r) { r.result = result; diff --git a/libLauncher/src/main/java/ru/gravit/launcher/events/request/ErrorRequestEvent.java b/libLauncher/src/main/java/ru/gravit/launcher/events/request/ErrorRequestEvent.java new file mode 100644 index 00000000..4f27c76d --- /dev/null +++ b/libLauncher/src/main/java/ru/gravit/launcher/events/request/ErrorRequestEvent.java @@ -0,0 +1,25 @@ +package ru.gravit.launcher.events.request; + +import ru.gravit.launcher.request.ResultInterface; +import ru.gravit.utils.event.EventInterface; + +import java.util.UUID; + +public class ErrorRequestEvent implements ResultInterface, EventInterface { + public static UUID uuid = UUID.fromString("0af22bc7-aa01-4881-bdbb-dc62b3cdac96"); + public ErrorRequestEvent(String error) { + this.error = error; + } + + public final String error; + + @Override + public String getType() { + return "error"; + } + + @Override + public UUID getUUID() { + return null; + } +}