mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-11-15 11:39:11 +03:00
[FEATURE] Улучшение API
This commit is contained in:
parent
2e238f3dc2
commit
4917f19b81
9 changed files with 149 additions and 30 deletions
|
@ -9,6 +9,7 @@
|
||||||
import pro.gravit.launchserver.socket.response.auth.AuthResponse;
|
import pro.gravit.launchserver.socket.response.auth.AuthResponse;
|
||||||
import pro.gravit.utils.helper.LogHelper;
|
import pro.gravit.utils.helper.LogHelper;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@ -62,4 +63,16 @@ public static class TrustLevel {
|
||||||
public byte[] publicKey;
|
public byte[] publicKey;
|
||||||
public HardwareReportRequest.HardwareInfo hardwareInfo;
|
public HardwareReportRequest.HardwareInfo hardwareInfo;
|
||||||
}
|
}
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public<T> T getProperty(String name)
|
||||||
|
{
|
||||||
|
if(properties == null) properties = new HashMap<>();
|
||||||
|
return (T) properties.get(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public<T> void setProperty(String name, T object)
|
||||||
|
{
|
||||||
|
if(properties == null) properties = new HashMap<>();
|
||||||
|
properties.put(name, object);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
import pro.gravit.launcher.events.request.ErrorRequestEvent;
|
import pro.gravit.launcher.events.request.ErrorRequestEvent;
|
||||||
import pro.gravit.launcher.request.WebSocketEvent;
|
import pro.gravit.launcher.request.WebSocketEvent;
|
||||||
import pro.gravit.launchserver.LaunchServer;
|
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.SimpleResponse;
|
||||||
import pro.gravit.launchserver.socket.response.WebSocketServerResponse;
|
import pro.gravit.launchserver.socket.response.WebSocketServerResponse;
|
||||||
import pro.gravit.launchserver.socket.response.auth.*;
|
import pro.gravit.launchserver.socket.response.auth.*;
|
||||||
|
@ -36,6 +37,8 @@
|
||||||
|
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.util.concurrent.atomic.AtomicLong;
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
public class WebSocketService {
|
public class WebSocketService {
|
||||||
public static final ProviderMap<WebSocketServerResponse> providers = new ProviderMap<>();
|
public static final ProviderMap<WebSocketServerResponse> providers = new ProviderMap<>();
|
||||||
|
@ -65,6 +68,16 @@ public WebSocketService(ChannelGroup channels, LaunchServer server) {
|
||||||
this.gson = Launcher.gsonManager.gson;
|
this.gson = Launcher.gsonManager.gson;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void forEachActiveChannels(BiConsumer<Channel, WebSocketFrameHandler> 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() {
|
public static void registerResponses() {
|
||||||
providers.register("auth", AuthResponse.class);
|
providers.register("auth", AuthResponse.class);
|
||||||
providers.register("checkServer", CheckServerResponse.class);
|
providers.register("checkServer", CheckServerResponse.class);
|
||||||
|
@ -89,6 +102,7 @@ public static void registerResponses() {
|
||||||
providers.register("serverStatus", ServerStatusResponse.class);
|
providers.register("serverStatus", ServerStatusResponse.class);
|
||||||
providers.register("pingServerReport", PingServerReportResponse.class);
|
providers.register("pingServerReport", PingServerReportResponse.class);
|
||||||
providers.register("pingServer", PingServerResponse.class);
|
providers.register("pingServer", PingServerResponse.class);
|
||||||
|
providers.register("currentUser", CurrentUserResponse.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void process(ChannelHandlerContext ctx, TextWebSocketFrame frame, Client client, String ip) {
|
public void process(ChannelHandlerContext ctx, TextWebSocketFrame frame, Client client, String ip) {
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
package pro.gravit.launchserver.socket.response.auth;
|
package pro.gravit.launchserver.socket.response.auth;
|
||||||
|
|
||||||
|
import io.netty.channel.Channel;
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
import pro.gravit.launcher.ClientPermissions;
|
import pro.gravit.launcher.ClientPermissions;
|
||||||
import pro.gravit.launcher.events.RequestEvent;
|
import pro.gravit.launcher.events.RequestEvent;
|
||||||
|
@ -38,42 +39,39 @@ public void execute(ChannelHandlerContext ctx, Client client) throws Exception {
|
||||||
handler.setClient(newClient);
|
handler.setClient(newClient);
|
||||||
if (client.session != null) server.sessionManager.removeClient(client.session);
|
if (client.session != null) server.sessionManager.removeClient(client.session);
|
||||||
if (exitAll) {
|
if (exitAll) {
|
||||||
service.channels.forEach((channel) -> {
|
service.forEachActiveChannels(((channel, webSocketFrameHandler) -> {
|
||||||
if (channel == null || channel.pipeline() == null) return;
|
Client client1 = webSocketFrameHandler.getClient();
|
||||||
WebSocketFrameHandler wsHandler = channel.pipeline().get(WebSocketFrameHandler.class);
|
|
||||||
if (wsHandler == null || wsHandler == handler) return;
|
|
||||||
Client chClient = wsHandler.getClient();
|
|
||||||
if (client.isAuth && client.username != null) {
|
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 {
|
} else {
|
||||||
if (chClient.session != client.session) return;
|
if (client1.session != client.session) return;
|
||||||
}
|
}
|
||||||
Client newCusClient = new Client(null);
|
exit(webSocketFrameHandler, channel, ExitRequestEvent.ExitReason.SERVER);
|
||||||
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);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
sendResult(new ExitRequestEvent(ExitRequestEvent.ExitReason.CLIENT));
|
sendResult(new ExitRequestEvent(ExitRequestEvent.ExitReason.CLIENT));
|
||||||
} else {
|
} else {
|
||||||
service.channels.forEach((channel -> {
|
service.forEachActiveChannels(((channel, webSocketFrameHandler) -> {
|
||||||
if (channel == null || channel.pipeline() == null) return;
|
Client client1 = webSocketFrameHandler.getClient();
|
||||||
WebSocketFrameHandler wsHandler = channel.pipeline().get(WebSocketFrameHandler.class);
|
if(client1 != null && client.isAuth && client.username != null && client1.username.equals(username))
|
||||||
if (wsHandler == null) return;
|
{
|
||||||
|
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();
|
Client chClient = wsHandler.getClient();
|
||||||
if (!chClient.isAuth || !username.equals(chClient.username)) return;
|
if (!chClient.isAuth || !username.equals(chClient.username)) return;
|
||||||
Client newCusClient = new Client(null);
|
Client newCusClient = new Client(null);
|
||||||
newCusClient.checkSign = chClient.checkSign;
|
newCusClient.checkSign = chClient.checkSign;
|
||||||
wsHandler.setClient(newCusClient);
|
wsHandler.setClient(newCusClient);
|
||||||
if (chClient.session != null) server.sessionManager.removeClient(chClient.session);
|
if (chClient.session != null) server.sessionManager.removeClient(chClient.session);
|
||||||
ExitRequestEvent event = new ExitRequestEvent(ExitRequestEvent.ExitReason.SERVER);
|
ExitRequestEvent event = new ExitRequestEvent(reason);
|
||||||
event.requestUUID = RequestEvent.eventUUID;
|
event.requestUUID = RequestEvent.eventUUID;
|
||||||
wsHandler.service.sendObject(channel, event);
|
wsHandler.service.sendObject(channel, event);
|
||||||
}));
|
|
||||||
sendResult(new ExitRequestEvent(ExitRequestEvent.ExitReason.NO_EXIT));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
public class RestoreSessionResponse extends SimpleResponse {
|
public class RestoreSessionResponse extends SimpleResponse {
|
||||||
@LauncherNetworkAPI
|
@LauncherNetworkAPI
|
||||||
public UUID session;
|
public UUID session;
|
||||||
|
public boolean needUserInfo;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getType() {
|
public String getType() {
|
||||||
|
@ -19,7 +20,7 @@ public String getType() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(ChannelHandlerContext ctx, Client client) {
|
public void execute(ChannelHandlerContext ctx, Client client) throws Exception {
|
||||||
Client rClient = server.sessionManager.getClient(session);
|
Client rClient = server.sessionManager.getClient(session);
|
||||||
if (rClient == null) {
|
if (rClient == null) {
|
||||||
sendError("Session invalid");
|
sendError("Session invalid");
|
||||||
|
@ -27,6 +28,13 @@ public void execute(ChannelHandlerContext ctx, Client client) {
|
||||||
}
|
}
|
||||||
WebSocketFrameHandler frameHandler = ctx.pipeline().get(WebSocketFrameHandler.class);
|
WebSocketFrameHandler frameHandler = ctx.pipeline().get(WebSocketFrameHandler.class);
|
||||||
frameHandler.setClient(rClient);
|
frameHandler.setClient(rClient);
|
||||||
|
if(needUserInfo)
|
||||||
|
{
|
||||||
|
sendResult(new RestoreSessionRequestEvent(CurrentUserResponse.collectUserInfoFromClient(rClient)));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
sendResult(new RestoreSessionRequestEvent());
|
sendResult(new RestoreSessionRequestEvent());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -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";
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,6 +3,15 @@
|
||||||
import pro.gravit.launcher.events.RequestEvent;
|
import pro.gravit.launcher.events.RequestEvent;
|
||||||
|
|
||||||
public class RestoreSessionRequestEvent extends RequestEvent {
|
public class RestoreSessionRequestEvent extends RequestEvent {
|
||||||
|
public CurrentUserRequestEvent.UserInfo userInfo;
|
||||||
|
|
||||||
|
public RestoreSessionRequestEvent() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public RestoreSessionRequestEvent(CurrentUserRequestEvent.UserInfo userInfo) {
|
||||||
|
this.userInfo = userInfo;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getType() {
|
public String getType() {
|
||||||
return "restoreSession";
|
return "restoreSession";
|
||||||
|
|
|
@ -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<CurrentUserRequestEvent> {
|
||||||
|
@Override
|
||||||
|
public String getType() {
|
||||||
|
return "currentUser";
|
||||||
|
}
|
||||||
|
}
|
|
@ -10,11 +10,17 @@
|
||||||
public class RestoreSessionRequest extends Request<RestoreSessionRequestEvent> implements WebSocketRequest {
|
public class RestoreSessionRequest extends Request<RestoreSessionRequestEvent> implements WebSocketRequest {
|
||||||
@LauncherNetworkAPI
|
@LauncherNetworkAPI
|
||||||
public final UUID session;
|
public final UUID session;
|
||||||
|
public boolean needUserInfo;
|
||||||
|
|
||||||
public RestoreSessionRequest(UUID session) {
|
public RestoreSessionRequest(UUID session) {
|
||||||
this.session = session;
|
this.session = session;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public RestoreSessionRequest(UUID session, boolean needUserInfo) {
|
||||||
|
this.session = session;
|
||||||
|
this.needUserInfo = needUserInfo;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getType() {
|
public String getType() {
|
||||||
return "restoreSession";
|
return "restoreSession";
|
||||||
|
|
Loading…
Reference in a new issue