[FEATURE] Улучшение API

This commit is contained in:
Gravit 2020-08-27 21:25:14 +07:00
parent 2e238f3dc2
commit 4917f19b81
No known key found for this signature in database
GPG key ID: 98A079490768CCE5
9 changed files with 149 additions and 30 deletions

View file

@ -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> 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);
}
}

View file

@ -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<WebSocketServerResponse> providers = new ProviderMap<>();
@ -65,6 +68,16 @@ public WebSocketService(ChannelGroup channels, LaunchServer server) {
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() {
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) {

View file

@ -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;
}
}

View file

@ -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);
}
}

View file

@ -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());
}
}
}

View file

@ -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";
}
}

View file

@ -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";

View file

@ -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";
}
}

View file

@ -10,11 +10,17 @@
public class RestoreSessionRequest extends Request<RestoreSessionRequestEvent> 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";