diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/Client.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/Client.java index 5a108372..e452a4dc 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/Client.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/Client.java @@ -9,6 +9,7 @@ import pro.gravit.launchserver.socket.response.auth.AuthResponse; import pro.gravit.utils.helper.LogHelper; +import java.util.HashMap; import java.util.Map; import java.util.UUID; @@ -62,4 +63,16 @@ public static class TrustLevel { public byte[] publicKey; public HardwareReportRequest.HardwareInfo hardwareInfo; } + @SuppressWarnings("unchecked") + public T getProperty(String name) + { + if(properties == null) properties = new HashMap<>(); + return (T) properties.get(name); + } + + public void setProperty(String name, T object) + { + if(properties == null) properties = new HashMap<>(); + properties.put(name, object); + } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/WebSocketService.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/WebSocketService.java index fe2a8775..d9c5be81 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/WebSocketService.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/WebSocketService.java @@ -13,6 +13,7 @@ import pro.gravit.launcher.events.request.ErrorRequestEvent; import pro.gravit.launcher.request.WebSocketEvent; import pro.gravit.launchserver.LaunchServer; +import pro.gravit.launchserver.socket.handlers.WebSocketFrameHandler; import pro.gravit.launchserver.socket.response.SimpleResponse; import pro.gravit.launchserver.socket.response.WebSocketServerResponse; import pro.gravit.launchserver.socket.response.auth.*; @@ -36,6 +37,8 @@ import java.lang.reflect.Type; import java.util.concurrent.atomic.AtomicLong; +import java.util.function.BiConsumer; +import java.util.function.Supplier; public class WebSocketService { public static final ProviderMap providers = new ProviderMap<>(); @@ -65,6 +68,16 @@ public WebSocketService(ChannelGroup channels, LaunchServer server) { this.gson = Launcher.gsonManager.gson; } + public void forEachActiveChannels(BiConsumer callback) + { + channels.forEach((channel) -> { + if (channel == null || channel.pipeline() == null) return; + WebSocketFrameHandler wsHandler = channel.pipeline().get(WebSocketFrameHandler.class); + if (wsHandler == null) return; + callback.accept(channel, wsHandler); + }); + } + public static void registerResponses() { providers.register("auth", AuthResponse.class); providers.register("checkServer", CheckServerResponse.class); @@ -89,6 +102,7 @@ public static void registerResponses() { providers.register("serverStatus", ServerStatusResponse.class); providers.register("pingServerReport", PingServerReportResponse.class); providers.register("pingServer", PingServerResponse.class); + providers.register("currentUser", CurrentUserResponse.class); } public void process(ChannelHandlerContext ctx, TextWebSocketFrame frame, Client client, String ip) { diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/CurrentUserResponse.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/CurrentUserResponse.java new file mode 100644 index 00000000..8f445e28 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/CurrentUserResponse.java @@ -0,0 +1,37 @@ +package pro.gravit.launchserver.socket.response.auth; + +import io.netty.channel.ChannelHandlerContext; +import pro.gravit.launcher.events.request.CurrentUserRequestEvent; +import pro.gravit.launcher.request.uuid.ProfileByUUIDRequest; +import pro.gravit.launchserver.socket.Client; +import pro.gravit.launchserver.socket.response.SimpleResponse; +import pro.gravit.launchserver.socket.response.profile.ProfileByUUIDResponse; + +import java.io.IOException; +import java.util.UUID; + +public class CurrentUserResponse extends SimpleResponse { + @Override + public String getType() { + return "currentUser"; + } + + @Override + public void execute(ChannelHandlerContext ctx, Client client) throws Exception { + sendResult(new CurrentUserRequestEvent(collectUserInfoFromClient(client))); + } + + public static CurrentUserRequestEvent.UserInfo collectUserInfoFromClient(Client client) throws IOException + { + CurrentUserRequestEvent.UserInfo result = new CurrentUserRequestEvent.UserInfo(); + if(client.auth != null && client.isAuth && client.username != null) { + UUID uuid = client.auth.handler.usernameToUUID(client.username); + if(uuid != null) + { + result.playerProfile = ProfileByUUIDResponse.getProfile(uuid, client.username, client.profile == null ? null : client.profile.getTitle(), client.auth.textureProvider); + } + } + result.permissions = client.permissions; + return result; + } +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/ExitResponse.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/ExitResponse.java index 3d7c4839..ce288d69 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/ExitResponse.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/ExitResponse.java @@ -1,5 +1,6 @@ package pro.gravit.launchserver.socket.response.auth; +import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; import pro.gravit.launcher.ClientPermissions; import pro.gravit.launcher.events.RequestEvent; @@ -38,42 +39,39 @@ public void execute(ChannelHandlerContext ctx, Client client) throws Exception { handler.setClient(newClient); if (client.session != null) server.sessionManager.removeClient(client.session); if (exitAll) { - service.channels.forEach((channel) -> { - if (channel == null || channel.pipeline() == null) return; - WebSocketFrameHandler wsHandler = channel.pipeline().get(WebSocketFrameHandler.class); - if (wsHandler == null || wsHandler == handler) return; - Client chClient = wsHandler.getClient(); + service.forEachActiveChannels(((channel, webSocketFrameHandler) -> { + Client client1 = webSocketFrameHandler.getClient(); if (client.isAuth && client.username != null) { - if (!chClient.isAuth || !client.username.equals(chClient.username)) return; + if (!client1.isAuth || !client.username.equals(client1.username)) return; } else { - if (chClient.session != client.session) return; + if (client1.session != client.session) return; } - Client newCusClient = new Client(null); - newCusClient.checkSign = chClient.checkSign; - wsHandler.setClient(newCusClient); - if (chClient.session != null) server.sessionManager.removeClient(chClient.session); - ExitRequestEvent event = new ExitRequestEvent(ExitRequestEvent.ExitReason.SERVER); - event.requestUUID = RequestEvent.eventUUID; - wsHandler.service.sendObject(channel, event); - }); + exit(webSocketFrameHandler, channel, ExitRequestEvent.ExitReason.SERVER); + })); } sendResult(new ExitRequestEvent(ExitRequestEvent.ExitReason.CLIENT)); } else { - service.channels.forEach((channel -> { - if (channel == null || channel.pipeline() == null) return; - WebSocketFrameHandler wsHandler = channel.pipeline().get(WebSocketFrameHandler.class); - if (wsHandler == null) return; - Client chClient = wsHandler.getClient(); - if (!chClient.isAuth || !username.equals(chClient.username)) return; - Client newCusClient = new Client(null); - newCusClient.checkSign = chClient.checkSign; - wsHandler.setClient(newCusClient); - if (chClient.session != null) server.sessionManager.removeClient(chClient.session); - ExitRequestEvent event = new ExitRequestEvent(ExitRequestEvent.ExitReason.SERVER); - event.requestUUID = RequestEvent.eventUUID; - wsHandler.service.sendObject(channel, event); + service.forEachActiveChannels(((channel, webSocketFrameHandler) -> { + Client client1 = webSocketFrameHandler.getClient(); + if(client1 != null && client.isAuth && client.username != null && client1.username.equals(username)) + { + exit(webSocketFrameHandler, channel, ExitRequestEvent.ExitReason.SERVER); + } })); sendResult(new ExitRequestEvent(ExitRequestEvent.ExitReason.NO_EXIT)); } } + public void exit(WebSocketFrameHandler wsHandler, Channel channel, ExitRequestEvent.ExitReason reason) + { + + Client chClient = wsHandler.getClient(); + if (!chClient.isAuth || !username.equals(chClient.username)) return; + Client newCusClient = new Client(null); + newCusClient.checkSign = chClient.checkSign; + wsHandler.setClient(newCusClient); + if (chClient.session != null) server.sessionManager.removeClient(chClient.session); + ExitRequestEvent event = new ExitRequestEvent(reason); + event.requestUUID = RequestEvent.eventUUID; + wsHandler.service.sendObject(channel, event); + } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/RestoreSessionResponse.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/RestoreSessionResponse.java index 39ffa6f6..3896f3a9 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/RestoreSessionResponse.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/RestoreSessionResponse.java @@ -12,6 +12,7 @@ public class RestoreSessionResponse extends SimpleResponse { @LauncherNetworkAPI public UUID session; + public boolean needUserInfo; @Override public String getType() { @@ -19,7 +20,7 @@ public String getType() { } @Override - public void execute(ChannelHandlerContext ctx, Client client) { + public void execute(ChannelHandlerContext ctx, Client client) throws Exception { Client rClient = server.sessionManager.getClient(session); if (rClient == null) { sendError("Session invalid"); @@ -27,6 +28,13 @@ public void execute(ChannelHandlerContext ctx, Client client) { } WebSocketFrameHandler frameHandler = ctx.pipeline().get(WebSocketFrameHandler.class); frameHandler.setClient(rClient); - sendResult(new RestoreSessionRequestEvent()); + if(needUserInfo) + { + sendResult(new RestoreSessionRequestEvent(CurrentUserResponse.collectUserInfoFromClient(rClient))); + } + else + { + sendResult(new RestoreSessionRequestEvent()); + } } } diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/events/request/CurrentUserRequestEvent.java b/LauncherAPI/src/main/java/pro/gravit/launcher/events/request/CurrentUserRequestEvent.java new file mode 100644 index 00000000..16aa3f7e --- /dev/null +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/events/request/CurrentUserRequestEvent.java @@ -0,0 +1,23 @@ +package pro.gravit.launcher.events.request; + +import pro.gravit.launcher.ClientPermissions; +import pro.gravit.launcher.events.RequestEvent; +import pro.gravit.launcher.profiles.PlayerProfile; + +public class CurrentUserRequestEvent extends RequestEvent { + public static class UserInfo { + public ClientPermissions permissions; + public String accessToken; + public PlayerProfile playerProfile; + } + public UserInfo userInfo; + + public CurrentUserRequestEvent(UserInfo userInfo) { + this.userInfo = userInfo; + } + + @Override + public String getType() { + return "currentUser"; + } +} diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/events/request/RestoreSessionRequestEvent.java b/LauncherAPI/src/main/java/pro/gravit/launcher/events/request/RestoreSessionRequestEvent.java index eca76bb7..c3a4297c 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/events/request/RestoreSessionRequestEvent.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/events/request/RestoreSessionRequestEvent.java @@ -3,6 +3,15 @@ import pro.gravit.launcher.events.RequestEvent; public class RestoreSessionRequestEvent extends RequestEvent { + public CurrentUserRequestEvent.UserInfo userInfo; + + public RestoreSessionRequestEvent() { + } + + public RestoreSessionRequestEvent(CurrentUserRequestEvent.UserInfo userInfo) { + this.userInfo = userInfo; + } + @Override public String getType() { return "restoreSession"; diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/CurrentUserRequest.java b/LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/CurrentUserRequest.java new file mode 100644 index 00000000..4a4437f5 --- /dev/null +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/CurrentUserRequest.java @@ -0,0 +1,11 @@ +package pro.gravit.launcher.request.auth; + +import pro.gravit.launcher.events.request.CurrentUserRequestEvent; +import pro.gravit.launcher.request.Request; + +public class CurrentUserRequest extends Request { + @Override + public String getType() { + return "currentUser"; + } +} diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/RestoreSessionRequest.java b/LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/RestoreSessionRequest.java index 9dd64e2b..b1642b5f 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/RestoreSessionRequest.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/RestoreSessionRequest.java @@ -10,11 +10,17 @@ public class RestoreSessionRequest extends Request implements WebSocketRequest { @LauncherNetworkAPI public final UUID session; + public boolean needUserInfo; public RestoreSessionRequest(UUID session) { this.session = session; } + public RestoreSessionRequest(UUID session, boolean needUserInfo) { + this.session = session; + this.needUserInfo = needUserInfo; + } + @Override public String getType() { return "restoreSession";