[FEATURE] Реализация restoreSession

This commit is contained in:
Gravit 2019-04-04 13:50:04 +07:00
parent a0e2c28d81
commit ae3e824c91
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
11 changed files with 124 additions and 0 deletions

View file

@ -21,6 +21,10 @@ public class WebSocketFrameHandler extends SimpleChannelInboundHandler<WebSocket
static {
service.registerResponses();
}
public void setClient(Client client)
{
this.client = client;
}
@Override
public void channelActive(ChannelHandlerContext ctx) {

View file

@ -81,6 +81,7 @@ public void registerResponses() {
registerResponse("setProfile", SetProfileResponse.class);
registerResponse("addLogListener", AddLogListenerResponse.class);
registerResponse("update", UpdateResponse.class);
registerResponse("restoreSession", RestoreSessionResponse.class);
}
public void sendObject(ChannelHandlerContext ctx, Object obj) {

View file

@ -21,13 +21,17 @@
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import java.security.SecureRandom;
import java.util.Collection;
import java.util.Random;
import java.util.UUID;
public class AuthResponse implements JsonResponseInterface {
public transient static Random random = new SecureRandom();
public String login;
public String client;
public String customText;
public boolean getSession;
public String password;
public byte[] encryptedPassword;
@ -110,6 +114,12 @@ public void execute(WebSocketService service, ChannelHandlerContext ctx, Client
clientData.updateAuth();
result.accessToken = aresult.accessToken;
result.permissions = clientData.permissions;
if(getSession)
{
clientData.session = random.nextLong();
LaunchServer.server.sessionManager.addClient(clientData);
result.session = clientData.session;
}
result.playerProfile = ProfileByUUIDResponse.getProfile(LaunchServer.server, uuid, aresult.username, client, clientData.auth.textureProvider);
service.sendObject(ctx, result);
} catch (AuthException | HWIDException e) {

View file

@ -0,0 +1,32 @@
package ru.gravit.launchserver.socket.websocket.json.auth;
import io.netty.channel.ChannelHandlerContext;
import ru.gravit.launcher.LauncherNetworkAPI;
import ru.gravit.launcher.events.request.ErrorRequestEvent;
import ru.gravit.launcher.events.request.RestoreSessionRequestEvent;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.socket.Client;
import ru.gravit.launchserver.socket.websocket.WebSocketFrameHandler;
import ru.gravit.launchserver.socket.websocket.WebSocketService;
import ru.gravit.launchserver.socket.websocket.json.JsonResponseInterface;
public class RestoreSessionResponse implements JsonResponseInterface {
@LauncherNetworkAPI
public long session;
@Override
public String getType() {
return "restoreSession";
}
@Override
public void execute(WebSocketService service, ChannelHandlerContext ctx, Client client) throws Exception {
Client rClient = LaunchServer.server.sessionManager.getClient(session);
if(rClient == null)
{
service.sendObject(ctx, new ErrorRequestEvent("Session invalid"));
}
WebSocketFrameHandler frameHandler = ctx.pipeline().get(WebSocketFrameHandler.class);
frameHandler.setClient(rClient);
service.sendObject(ctx, new RestoreSessionRequestEvent());
}
}

View file

@ -11,6 +11,7 @@
import ru.gravit.launcher.profiles.ClientProfile;
import ru.gravit.launcher.profiles.PlayerProfile;
import ru.gravit.launcher.request.Request;
import ru.gravit.launcher.request.auth.RestoreSessionRequest;
import ru.gravit.launcher.request.update.LegacyLauncherRequest;
import ru.gravit.launcher.serialize.HInput;
import ru.gravit.launcher.serialize.HOutput;
@ -466,6 +467,12 @@ public static void main(String... args) throws Throwable {
PublicURLClassLoader.systemclassloader = classLoader;
// Start client with WatchService monitoring
boolean digest = !profile.isUpdateFastCheck();
LogHelper.debug("Restore sessions");
if(Launcher.getConfig().isNettyEnabled)
{
RestoreSessionRequest request = new RestoreSessionRequest(Request.getSession());
request.request();
}
LogHelper.debug("Starting JVM and client WatchService");
FileNameMatcher assetMatcher = profile.getAssetUpdateMatcher();
FileNameMatcher clientMatcher = profile.getClientUpdateMatcher();

View file

@ -102,6 +102,10 @@ public static HasherStore getDefaultHasherStore() {
@LauncherAPI
public static void setAuthParams(AuthRequestEvent event) {
if(event.session != 0)
{
Request.setSession(event.session);
}
LauncherGuardManager.guard.setProtectToken(event.protectToken);
}

View file

@ -23,6 +23,7 @@ public final class AuthRequest extends Request<AuthRequestEvent> implements Requ
private final String auth_id;
private final HWID hwid;
private final String customText;
private final boolean getSession;
@LauncherAPI
public AuthRequest(LauncherConfig config, String login, byte[] password, HWID hwid) {
@ -32,6 +33,7 @@ public AuthRequest(LauncherConfig config, String login, byte[] password, HWID hw
this.hwid = hwid;
customText = "";
auth_id = "";
getSession = true;
}
@LauncherAPI
@ -42,6 +44,7 @@ public AuthRequest(LauncherConfig config, String login, byte[] password, HWID hw
this.hwid = hwid;
this.auth_id = auth_id;
customText = "";
getSession = true;
}
@LauncherAPI
@ -52,6 +55,7 @@ public AuthRequest(LauncherConfig config, String login, byte[] password, HWID hw
this.hwid = hwid;
this.auth_id = auth_id;
this.customText = customText;
getSession = true;
}
@LauncherAPI

View file

@ -0,0 +1,40 @@
package ru.gravit.launcher.request.auth;
import ru.gravit.launcher.LauncherNetworkAPI;
import ru.gravit.launcher.events.request.AuthRequestEvent;
import ru.gravit.launcher.events.request.RestoreSessionRequestEvent;
import ru.gravit.launcher.request.Request;
import ru.gravit.launcher.request.websockets.LegacyRequestBridge;
import ru.gravit.launcher.request.websockets.RequestInterface;
import ru.gravit.launcher.serialize.HInput;
import ru.gravit.launcher.serialize.HOutput;
import java.io.IOException;
public class RestoreSessionRequest extends Request<RestoreSessionRequestEvent> implements RequestInterface {
@LauncherNetworkAPI
public long session;
public RestoreSessionRequest(long session) {
this.session = session;
}
@Override
public RestoreSessionRequestEvent requestWebSockets() throws IOException, InterruptedException {
return (RestoreSessionRequestEvent) LegacyRequestBridge.sendRequest(this);
}
@Override
public Integer getLegacyType() {
return 0;
}
@Override
protected RestoreSessionRequestEvent requestDo(HInput input, HOutput output) throws Exception {
return new RestoreSessionRequestEvent();
}
@Override
public String getType() {
return "restoreSession";
}
}

View file

@ -91,6 +91,7 @@ public void registerResults() {
registerResult("updateList", UpdateListRequestEvent.class);
registerResult("error", ErrorRequestEvent.class);
registerResult("update", UpdateRequestEvent.class);
registerResult("restoreSession", RestoreSessionRequestEvent.class);
}
public void registerHandler(EventHandler eventHandler) {

View file

@ -24,6 +24,8 @@ public AuthRequestEvent() {
public String accessToken;
@LauncherNetworkAPI
public String protectToken;
@LauncherNetworkAPI
public long session;
public AuthRequestEvent(PlayerProfile pp, String accessToken, ClientPermissions permissions) {
this.playerProfile = pp;
@ -38,6 +40,15 @@ public AuthRequestEvent(ClientPermissions permissions, PlayerProfile playerProfi
this.protectToken = protectToken;
}
public AuthRequestEvent(String error, ClientPermissions permissions, PlayerProfile playerProfile, String accessToken, String protectToken, long session) {
this.error = error;
this.permissions = permissions;
this.playerProfile = playerProfile;
this.accessToken = accessToken;
this.protectToken = protectToken;
this.session = session;
}
@Override
public UUID getUUID() {
return uuid;

View file

@ -0,0 +1,10 @@
package ru.gravit.launcher.events.request;
import ru.gravit.launcher.request.ResultInterface;
public class RestoreSessionRequestEvent implements ResultInterface {
@Override
public String getType() {
return "restoreSession";
}
}