[FEATURE][EXPERIMENTAL] session теперь UUID для совместимости с JS

This commit is contained in:
Gravit 2020-08-08 12:33:05 +07:00
parent 2c8c640130
commit e304bd7285
No known key found for this signature in database
GPG key ID: 98A079490768CCE5
10 changed files with 34 additions and 30 deletions

View file

@ -3,17 +3,14 @@
import pro.gravit.launcher.NeedGarbageCollection; import pro.gravit.launcher.NeedGarbageCollection;
import pro.gravit.launchserver.socket.Client; import pro.gravit.launchserver.socket.Client;
import java.util.HashMap; import java.util.*;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class SessionManager implements NeedGarbageCollection { public class SessionManager implements NeedGarbageCollection {
public static final long SESSION_TIMEOUT = 3 * 60 * 60 * 1000; // 3 часа 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) { 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); return clientSet.get(session);
} }
public Client getOrNewClient(long session) { public Client getOrNewClient(UUID session) {
return clientSet.computeIfAbsent(session, Client::new); return clientSet.computeIfAbsent(session, Client::new);
} }
public Client removeClient(long session) { public Client removeClient(UUID session) {
return clientSet.remove(session); return clientSet.remove(session);
} }
public void updateClient(long session) { public void updateClient(UUID session) {
Client c = clientSet.get(session); Client c = clientSet.get(session);
if (c != null) { if (c != null) {
c.up(); c.up();

View file

@ -10,9 +10,10 @@
import pro.gravit.utils.helper.LogHelper; import pro.gravit.utils.helper.LogHelper;
import java.util.Map; import java.util.Map;
import java.util.UUID;
public class Client { public class Client {
public long session; public UUID session;
public String auth_id; public String auth_id;
public long timestamp; public long timestamp;
public AuthResponse.ConnectTypes type; public AuthResponse.ConnectTypes type;
@ -29,7 +30,7 @@ public class Client {
public transient Map<String, Object> properties; public transient Map<String, Object> properties;
public Client(long session) { public Client(UUID session) {
this.session = session; this.session = session;
timestamp = System.currentTimeMillis(); timestamp = System.currentTimeMillis();
type = null; type = null;

View file

@ -50,7 +50,7 @@ public void channelActive(ChannelHandlerContext ctx) {
if (LogHelper.isDevEnabled()) { if (LogHelper.isDevEnabled()) {
LogHelper.dev("New client %s", IOHelper.getIP(ctx.channel().remoteAddress())); LogHelper.dev("New client %s", IOHelper.getIP(ctx.channel().remoteAddress()));
} }
client = new Client(0); client = new Client(null);
Channel ch = ctx.channel(); Channel ch = ctx.channel();
service.registerClient(ch); service.registerClient(ch);
ctx.executor().schedule(() -> { ctx.executor().schedule(() -> {

View file

@ -92,8 +92,8 @@ public void execute(ChannelHandlerContext ctx, Client clientData) throws Excepti
result.accessToken = aresult.accessToken; result.accessToken = aresult.accessToken;
result.permissions = clientData.permissions; result.permissions = clientData.permissions;
if (getSession) { if (getSession) {
if (clientData.session == 0) { if (clientData.session == null) {
clientData.session = random.nextLong(); clientData.session = UUID.randomUUID();
server.sessionManager.addClient(clientData); server.sessionManager.addClient(clientData);
} }
result.session = clientData.session; result.session = clientData.session;

View file

@ -24,7 +24,7 @@ public void execute(ChannelHandlerContext ctx, Client client) throws Exception {
return; return;
} }
if (username == null) { if (username == null) {
if (client.session == 0 && exitAll) { if (client.session == null && exitAll) {
sendError("Session invalid"); sendError("Session invalid");
return; return;
} }
@ -33,10 +33,10 @@ public void execute(ChannelHandlerContext ctx, Client client) throws Exception {
sendError("Exit internal error"); sendError("Exit internal error");
return; return;
} }
Client newClient = new Client(0); Client newClient = new Client(null);
newClient.checkSign = client.checkSign; newClient.checkSign = client.checkSign;
handler.setClient(newClient); handler.setClient(newClient);
if (client.session != 0) server.sessionManager.removeClient(client.session); if (client.session != null) server.sessionManager.removeClient(client.session);
if (exitAll) { if (exitAll) {
service.channels.forEach((channel) -> { service.channels.forEach((channel) -> {
if (channel == null || channel.pipeline() == null) return; if (channel == null || channel.pipeline() == null) return;
@ -48,10 +48,10 @@ public void execute(ChannelHandlerContext ctx, Client client) throws Exception {
} else { } else {
if (chClient.session != client.session) return; if (chClient.session != client.session) return;
} }
Client newCusClient = new Client(0); Client newCusClient = new Client(null);
newCusClient.checkSign = chClient.checkSign; newCusClient.checkSign = chClient.checkSign;
wsHandler.setClient(newCusClient); 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); ExitRequestEvent event = new ExitRequestEvent(ExitRequestEvent.ExitReason.SERVER);
event.requestUUID = RequestEvent.eventUUID; event.requestUUID = RequestEvent.eventUUID;
wsHandler.service.sendObject(channel, event); wsHandler.service.sendObject(channel, event);
@ -65,10 +65,10 @@ public void execute(ChannelHandlerContext ctx, Client client) throws Exception {
if (wsHandler == null) return; if (wsHandler == null) return;
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(0); Client newCusClient = new Client(null);
newCusClient.checkSign = chClient.checkSign; newCusClient.checkSign = chClient.checkSign;
wsHandler.setClient(newCusClient); 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); ExitRequestEvent event = new ExitRequestEvent(ExitRequestEvent.ExitReason.SERVER);
event.requestUUID = RequestEvent.eventUUID; event.requestUUID = RequestEvent.eventUUID;
wsHandler.service.sendObject(channel, event); wsHandler.service.sendObject(channel, event);

View file

@ -7,9 +7,11 @@
import pro.gravit.launchserver.socket.handlers.WebSocketFrameHandler; import pro.gravit.launchserver.socket.handlers.WebSocketFrameHandler;
import pro.gravit.launchserver.socket.response.SimpleResponse; import pro.gravit.launchserver.socket.response.SimpleResponse;
import java.util.UUID;
public class RestoreSessionResponse extends SimpleResponse { public class RestoreSessionResponse extends SimpleResponse {
@LauncherNetworkAPI @LauncherNetworkAPI
public long session; public UUID session;
@Override @Override
public String getType() { public String getType() {

View file

@ -195,7 +195,7 @@ public static class ClientParams {
//======== //========
public long session; public UUID session;
public transient HashedDir assetHDir; public transient HashedDir assetHDir;

View file

@ -5,6 +5,8 @@
import pro.gravit.launcher.events.RequestEvent; import pro.gravit.launcher.events.RequestEvent;
import pro.gravit.launcher.profiles.PlayerProfile; import pro.gravit.launcher.profiles.PlayerProfile;
import java.util.UUID;
public class AuthRequestEvent extends RequestEvent { public class AuthRequestEvent extends RequestEvent {
@LauncherNetworkAPI @LauncherNetworkAPI
@ -16,7 +18,7 @@ public class AuthRequestEvent extends RequestEvent {
@LauncherNetworkAPI @LauncherNetworkAPI
public String protectToken; public String protectToken;
@LauncherNetworkAPI @LauncherNetworkAPI
public long session; public UUID session;
public AuthRequestEvent() { public AuthRequestEvent() {
} }
@ -34,7 +36,7 @@ public AuthRequestEvent(ClientPermissions permissions, PlayerProfile playerProfi
this.protectToken = protectToken; 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.permissions = permissions;
this.playerProfile = playerProfile; this.playerProfile = playerProfile;
this.accessToken = accessToken; this.accessToken = accessToken;

View file

@ -11,16 +11,16 @@
public abstract class Request<R extends WebSocketEvent> implements WebSocketRequest { public abstract class Request<R extends WebSocketEvent> implements WebSocketRequest {
public static StdWebSocketService service; public static StdWebSocketService service;
private static long session = SecurityHelper.secureRandom.nextLong(); private static UUID session = UUID.randomUUID();
@LauncherNetworkAPI @LauncherNetworkAPI
public final UUID requestUUID = UUID.randomUUID(); public final UUID requestUUID = UUID.randomUUID();
private transient final AtomicBoolean started = new AtomicBoolean(false); private transient final AtomicBoolean started = new AtomicBoolean(false);
public static long getSession() { public static UUID getSession() {
return Request.session; return Request.session;
} }
public static void setSession(long session) { public static void setSession(UUID session) {
Request.session = session; Request.session = session;
} }

View file

@ -5,11 +5,13 @@
import pro.gravit.launcher.request.Request; import pro.gravit.launcher.request.Request;
import pro.gravit.launcher.request.websockets.WebSocketRequest; import pro.gravit.launcher.request.websockets.WebSocketRequest;
import java.util.UUID;
public class RestoreSessionRequest extends Request<RestoreSessionRequestEvent> implements WebSocketRequest { public class RestoreSessionRequest extends Request<RestoreSessionRequestEvent> implements WebSocketRequest {
@LauncherNetworkAPI @LauncherNetworkAPI
public final long session; public final UUID session;
public RestoreSessionRequest(long session) { public RestoreSessionRequest(UUID session) {
this.session = session; this.session = session;
} }