mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-11-15 11:39:11 +03:00
[FEATURE][EXPERIMENTAL] session теперь UUID для совместимости с JS
This commit is contained in:
parent
2c8c640130
commit
e304bd7285
10 changed files with 34 additions and 30 deletions
|
@ -3,17 +3,14 @@
|
|||
import pro.gravit.launcher.NeedGarbageCollection;
|
||||
import pro.gravit.launchserver.socket.Client;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class SessionManager implements NeedGarbageCollection {
|
||||
|
||||
public static final long SESSION_TIMEOUT = 3 * 60 * 60 * 1000; // 3 часа
|
||||
private final Map<Long, Client> clientSet = new HashMap<>(128);
|
||||
private final Map<UUID, Client> clientSet = new HashMap<>(128);
|
||||
|
||||
|
||||
public boolean addClient(Client client) {
|
||||
|
@ -31,21 +28,21 @@ public void garbageCollection() {
|
|||
}
|
||||
|
||||
|
||||
public Client getClient(long session) {
|
||||
public Client getClient(UUID session) {
|
||||
return clientSet.get(session);
|
||||
}
|
||||
|
||||
|
||||
public Client getOrNewClient(long session) {
|
||||
public Client getOrNewClient(UUID session) {
|
||||
return clientSet.computeIfAbsent(session, Client::new);
|
||||
}
|
||||
|
||||
public Client removeClient(long session) {
|
||||
public Client removeClient(UUID session) {
|
||||
return clientSet.remove(session);
|
||||
}
|
||||
|
||||
|
||||
public void updateClient(long session) {
|
||||
public void updateClient(UUID session) {
|
||||
Client c = clientSet.get(session);
|
||||
if (c != null) {
|
||||
c.up();
|
||||
|
|
|
@ -10,9 +10,10 @@
|
|||
import pro.gravit.utils.helper.LogHelper;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public class Client {
|
||||
public long session;
|
||||
public UUID session;
|
||||
public String auth_id;
|
||||
public long timestamp;
|
||||
public AuthResponse.ConnectTypes type;
|
||||
|
@ -29,7 +30,7 @@ public class Client {
|
|||
|
||||
public transient Map<String, Object> properties;
|
||||
|
||||
public Client(long session) {
|
||||
public Client(UUID session) {
|
||||
this.session = session;
|
||||
timestamp = System.currentTimeMillis();
|
||||
type = null;
|
||||
|
|
|
@ -50,7 +50,7 @@ public void channelActive(ChannelHandlerContext ctx) {
|
|||
if (LogHelper.isDevEnabled()) {
|
||||
LogHelper.dev("New client %s", IOHelper.getIP(ctx.channel().remoteAddress()));
|
||||
}
|
||||
client = new Client(0);
|
||||
client = new Client(null);
|
||||
Channel ch = ctx.channel();
|
||||
service.registerClient(ch);
|
||||
ctx.executor().schedule(() -> {
|
||||
|
|
|
@ -92,8 +92,8 @@ public void execute(ChannelHandlerContext ctx, Client clientData) throws Excepti
|
|||
result.accessToken = aresult.accessToken;
|
||||
result.permissions = clientData.permissions;
|
||||
if (getSession) {
|
||||
if (clientData.session == 0) {
|
||||
clientData.session = random.nextLong();
|
||||
if (clientData.session == null) {
|
||||
clientData.session = UUID.randomUUID();
|
||||
server.sessionManager.addClient(clientData);
|
||||
}
|
||||
result.session = clientData.session;
|
||||
|
|
|
@ -24,7 +24,7 @@ public void execute(ChannelHandlerContext ctx, Client client) throws Exception {
|
|||
return;
|
||||
}
|
||||
if (username == null) {
|
||||
if (client.session == 0 && exitAll) {
|
||||
if (client.session == null && exitAll) {
|
||||
sendError("Session invalid");
|
||||
return;
|
||||
}
|
||||
|
@ -33,10 +33,10 @@ public void execute(ChannelHandlerContext ctx, Client client) throws Exception {
|
|||
sendError("Exit internal error");
|
||||
return;
|
||||
}
|
||||
Client newClient = new Client(0);
|
||||
Client newClient = new Client(null);
|
||||
newClient.checkSign = client.checkSign;
|
||||
handler.setClient(newClient);
|
||||
if (client.session != 0) server.sessionManager.removeClient(client.session);
|
||||
if (client.session != null) server.sessionManager.removeClient(client.session);
|
||||
if (exitAll) {
|
||||
service.channels.forEach((channel) -> {
|
||||
if (channel == null || channel.pipeline() == null) return;
|
||||
|
@ -48,10 +48,10 @@ public void execute(ChannelHandlerContext ctx, Client client) throws Exception {
|
|||
} else {
|
||||
if (chClient.session != client.session) return;
|
||||
}
|
||||
Client newCusClient = new Client(0);
|
||||
Client newCusClient = new Client(null);
|
||||
newCusClient.checkSign = chClient.checkSign;
|
||||
wsHandler.setClient(newCusClient);
|
||||
if (chClient.session != 0) server.sessionManager.removeClient(chClient.session);
|
||||
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);
|
||||
|
@ -65,10 +65,10 @@ public void execute(ChannelHandlerContext ctx, Client client) throws Exception {
|
|||
if (wsHandler == null) return;
|
||||
Client chClient = wsHandler.getClient();
|
||||
if (!chClient.isAuth || !username.equals(chClient.username)) return;
|
||||
Client newCusClient = new Client(0);
|
||||
Client newCusClient = new Client(null);
|
||||
newCusClient.checkSign = chClient.checkSign;
|
||||
wsHandler.setClient(newCusClient);
|
||||
if (chClient.session != 0) server.sessionManager.removeClient(chClient.session);
|
||||
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);
|
||||
|
|
|
@ -7,9 +7,11 @@
|
|||
import pro.gravit.launchserver.socket.handlers.WebSocketFrameHandler;
|
||||
import pro.gravit.launchserver.socket.response.SimpleResponse;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class RestoreSessionResponse extends SimpleResponse {
|
||||
@LauncherNetworkAPI
|
||||
public long session;
|
||||
public UUID session;
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
|
|
|
@ -195,7 +195,7 @@ public static class ClientParams {
|
|||
|
||||
//========
|
||||
|
||||
public long session;
|
||||
public UUID session;
|
||||
|
||||
public transient HashedDir assetHDir;
|
||||
|
||||
|
|
|
@ -5,6 +5,8 @@
|
|||
import pro.gravit.launcher.events.RequestEvent;
|
||||
import pro.gravit.launcher.profiles.PlayerProfile;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class AuthRequestEvent extends RequestEvent {
|
||||
|
||||
@LauncherNetworkAPI
|
||||
|
@ -16,7 +18,7 @@ public class AuthRequestEvent extends RequestEvent {
|
|||
@LauncherNetworkAPI
|
||||
public String protectToken;
|
||||
@LauncherNetworkAPI
|
||||
public long session;
|
||||
public UUID session;
|
||||
|
||||
public AuthRequestEvent() {
|
||||
}
|
||||
|
@ -34,7 +36,7 @@ public AuthRequestEvent(ClientPermissions permissions, PlayerProfile playerProfi
|
|||
this.protectToken = protectToken;
|
||||
}
|
||||
|
||||
public AuthRequestEvent(ClientPermissions permissions, PlayerProfile playerProfile, String accessToken, String protectToken, long session) {
|
||||
public AuthRequestEvent(ClientPermissions permissions, PlayerProfile playerProfile, String accessToken, String protectToken, UUID session) {
|
||||
this.permissions = permissions;
|
||||
this.playerProfile = playerProfile;
|
||||
this.accessToken = accessToken;
|
||||
|
|
|
@ -11,16 +11,16 @@
|
|||
|
||||
public abstract class Request<R extends WebSocketEvent> implements WebSocketRequest {
|
||||
public static StdWebSocketService service;
|
||||
private static long session = SecurityHelper.secureRandom.nextLong();
|
||||
private static UUID session = UUID.randomUUID();
|
||||
@LauncherNetworkAPI
|
||||
public final UUID requestUUID = UUID.randomUUID();
|
||||
private transient final AtomicBoolean started = new AtomicBoolean(false);
|
||||
|
||||
public static long getSession() {
|
||||
public static UUID getSession() {
|
||||
return Request.session;
|
||||
}
|
||||
|
||||
public static void setSession(long session) {
|
||||
public static void setSession(UUID session) {
|
||||
Request.session = session;
|
||||
}
|
||||
|
||||
|
|
|
@ -5,11 +5,13 @@
|
|||
import pro.gravit.launcher.request.Request;
|
||||
import pro.gravit.launcher.request.websockets.WebSocketRequest;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class RestoreSessionRequest extends Request<RestoreSessionRequestEvent> implements WebSocketRequest {
|
||||
@LauncherNetworkAPI
|
||||
public final long session;
|
||||
public final UUID session;
|
||||
|
||||
public RestoreSessionRequest(long session) {
|
||||
public RestoreSessionRequest(UUID session) {
|
||||
this.session = session;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue