[FEATURE] Портирование AuthRequest/LauncherRequest

This commit is contained in:
Gravit 2019-02-10 16:48:44 +07:00
parent 40fe55a46d
commit 98354345a0
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
7 changed files with 110 additions and 53 deletions

View file

@ -75,7 +75,7 @@ public void registerResponses() {
registerResponse("checkServer", CheckServerResponse.class); registerResponse("checkServer", CheckServerResponse.class);
registerResponse("joinServer", JoinServerResponse.class); registerResponse("joinServer", JoinServerResponse.class);
registerResponse("profiles", ProfilesResponse.class); registerResponse("profiles", ProfilesResponse.class);
registerResponse("launcherUpdate", LauncherResponse.class); registerResponse("launcher", LauncherResponse.class);
registerResponse("updateList", UpdateListResponse.class); registerResponse("updateList", UpdateListResponse.class);
registerResponse("cmdExec", UpdateListResponse.class); registerResponse("cmdExec", UpdateListResponse.class);
} }
@ -99,14 +99,17 @@ public void sendEvent(EventResult obj) {
channels.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj))); channels.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj)));
} }
public static class ErrorResult { public static class ErrorResult implements ResultInterface {
public ErrorResult(String error) { public ErrorResult(String error) {
this.error = error; this.error = error;
this.type = "requestError";
} }
public final String error; public final String error;
public final String type;
@Override
public String getType() {
return "error";
}
} }
public static class SuccessResult { public static class SuccessResult {
@ -119,15 +122,18 @@ public SuccessResult(String requesttype) {
public final String type; public final String type;
} }
public static class EventResult { public static class EventResult implements ResultInterface {
public EventResult() { public EventResult() {
this.type = "event";
} }
public final String type; @Override
public String getType() {
return "event";
}
} }
public static class ExceptionResult { public static class ExceptionResult implements ResultInterface {
public ExceptionResult(Exception e) { public ExceptionResult(Exception e) {
this.message = e.getMessage(); this.message = e.getMessage();
this.clazz = e.getClass().getName(); this.clazz = e.getClass().getName();
@ -137,5 +143,10 @@ public ExceptionResult(Exception e) {
public final String message; public final String message;
public final String clazz; public final String clazz;
public final String type; public final String type;
@Override
public String getType() {
return "exception";
}
} }
} }

View file

@ -14,8 +14,11 @@
import ru.gravit.launchserver.socket.websocket.WebSocketService; import ru.gravit.launchserver.socket.websocket.WebSocketService;
import ru.gravit.launchserver.socket.websocket.json.JsonResponseInterface; import ru.gravit.launchserver.socket.websocket.json.JsonResponseInterface;
import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.SecurityHelper;
import ru.gravit.utils.helper.VerifyHelper; import ru.gravit.utils.helper.VerifyHelper;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import java.util.Collection; import java.util.Collection;
import java.util.UUID; import java.util.UUID;
@ -25,6 +28,7 @@ public class AuthResponse implements JsonResponseInterface {
public String customText; public String customText;
public String password; public String password;
public byte[] encryptedPassword;
public AuthResponse(String login, String password, int authid, OshiHWID hwid) { public AuthResponse(String login, String password, int authid, OshiHWID hwid) {
this.login = login; this.login = login;
@ -59,6 +63,15 @@ public void execute(WebSocketService service, ChannelHandlerContext ctx, Client
AuthProvider.authError("Don't skip Launcher Update"); AuthProvider.authError("Don't skip Launcher Update");
return; return;
} }
if(password == null)
{
try {
password = IOHelper.decode(SecurityHelper.newRSADecryptCipher(LaunchServer.server.privateKey).
doFinal(encryptedPassword));
} catch (IllegalBlockSizeException | BadPaddingException ignored) {
throw new AuthException("Password decryption error");
}
}
clientData.permissions = LaunchServer.server.config.permissionsHandler.getPermissions(login); clientData.permissions = LaunchServer.server.config.permissionsHandler.getPermissions(login);
if(authType == ConnectTypes.BOT && !clientData.permissions.canBot) if(authType == ConnectTypes.BOT && !clientData.permissions.canBot)
{ {

View file

@ -14,6 +14,7 @@
public class LauncherResponse implements JsonResponseInterface { public class LauncherResponse implements JsonResponseInterface {
public Version version; public Version version;
public String hash; public String hash;
public byte[] digest;
public int launcher_type; public int launcher_type;
//REPLACED TO REAL URL //REPLACED TO REAL URL
public static final String JAR_URL = "http://localhost:9752/Launcher.jar"; public static final String JAR_URL = "http://localhost:9752/Launcher.jar";
@ -21,12 +22,16 @@ public class LauncherResponse implements JsonResponseInterface {
@Override @Override
public String getType() { public String getType() {
return "launcherUpdate"; return "launcher";
} }
@Override @Override
public void execute(WebSocketService service, ChannelHandlerContext ctx, Client client) { public void execute(WebSocketService service, ChannelHandlerContext ctx, Client client) {
byte[] bytes = Base64.getDecoder().decode(hash); byte[] bytes;
if(hash != null)
bytes = Base64.getDecoder().decode(hash);
else
bytes = digest;
if (launcher_type == 1) // JAR if (launcher_type == 1) // JAR
{ {
byte[] hash = LaunchServer.server.launcherBinary.getBytes().getDigest(); byte[] hash = LaunchServer.server.launcherBinary.getBytes().getDigest();

View file

@ -29,9 +29,9 @@ public static void requestError(String message) throws RequestException {
} }
@LauncherAPI @LauncherAPI
protected final LauncherConfig config; protected transient final LauncherConfig config;
private final AtomicBoolean started = new AtomicBoolean(false); private transient final AtomicBoolean started = new AtomicBoolean(false);
@LauncherAPI @LauncherAPI
protected Request() { protected Request() {

View file

@ -5,6 +5,7 @@
import ru.gravit.launcher.profiles.PlayerProfile; import ru.gravit.launcher.profiles.PlayerProfile;
import ru.gravit.launcher.request.Request; import ru.gravit.launcher.request.Request;
import ru.gravit.launcher.request.RequestType; import ru.gravit.launcher.request.RequestType;
import ru.gravit.launcher.request.websockets.LegacyRequestBridge;
import ru.gravit.launcher.request.websockets.RequestInterface; import ru.gravit.launcher.request.websockets.RequestInterface;
import ru.gravit.launcher.serialize.HInput; import ru.gravit.launcher.serialize.HInput;
import ru.gravit.launcher.serialize.HOutput; import ru.gravit.launcher.serialize.HOutput;
@ -14,7 +15,7 @@
import java.io.IOException; import java.io.IOException;
public final class AuthRequest extends Request<AuthRequestEvent> { public final class AuthRequest extends Request<AuthRequestEvent> implements RequestInterface {
private final String login; private final String login;
@ -24,51 +25,55 @@ public final class AuthRequest extends Request<AuthRequestEvent> {
private final String customText; private final String customText;
@LauncherAPI @LauncherAPI
public AuthRequest(LauncherConfig config, String login, byte[] encryptedPassword, HWID hwid) { public AuthRequest(LauncherConfig config, String login, byte[] password, HWID hwid) {
super(config); super(config);
this.login = VerifyHelper.verify(login, VerifyHelper.NOT_EMPTY, "Login can't be empty"); this.login = VerifyHelper.verify(login, VerifyHelper.NOT_EMPTY, "Login can't be empty");
this.encryptedPassword = encryptedPassword.clone(); this.encryptedPassword = password.clone();
this.hwid = hwid; this.hwid = hwid;
customText = ""; customText = "";
auth_id = 0; auth_id = 0;
} }
@LauncherAPI @LauncherAPI
public AuthRequest(LauncherConfig config, String login, byte[] encryptedPassword, HWID hwid, String customText) { public AuthRequest(LauncherConfig config, String login, byte[] password, HWID hwid, String customText) {
super(config); super(config);
this.login = VerifyHelper.verify(login, VerifyHelper.NOT_EMPTY, "Login can't be empty"); this.login = VerifyHelper.verify(login, VerifyHelper.NOT_EMPTY, "Login can't be empty");
this.encryptedPassword = encryptedPassword.clone(); this.encryptedPassword = password.clone();
this.hwid = hwid; this.hwid = hwid;
this.customText = customText; this.customText = customText;
auth_id = 0; auth_id = 0;
} }
@LauncherAPI @LauncherAPI
public AuthRequest(LauncherConfig config, String login, byte[] encryptedPassword, HWID hwid, int auth_id) { public AuthRequest(LauncherConfig config, String login, byte[] password, HWID hwid, int auth_id) {
super(config); super(config);
this.login = VerifyHelper.verify(login, VerifyHelper.NOT_EMPTY, "Login can't be empty"); this.login = VerifyHelper.verify(login, VerifyHelper.NOT_EMPTY, "Login can't be empty");
this.encryptedPassword = encryptedPassword.clone(); this.encryptedPassword = password.clone();
this.hwid = hwid; this.hwid = hwid;
this.auth_id = auth_id; this.auth_id = auth_id;
customText = ""; customText = "";
} }
@LauncherAPI @LauncherAPI
public AuthRequest(LauncherConfig config, String login, byte[] encryptedPassword, HWID hwid, String customText, int auth_id) { public AuthRequest(LauncherConfig config, String login, byte[] password, HWID hwid, String customText, int auth_id) {
super(config); super(config);
this.login = VerifyHelper.verify(login, VerifyHelper.NOT_EMPTY, "Login can't be empty"); this.login = VerifyHelper.verify(login, VerifyHelper.NOT_EMPTY, "Login can't be empty");
this.encryptedPassword = encryptedPassword.clone(); this.encryptedPassword = password.clone();
this.hwid = hwid; this.hwid = hwid;
this.auth_id = auth_id; this.auth_id = auth_id;
this.customText = customText; this.customText = customText;
} }
@LauncherAPI @LauncherAPI
public AuthRequest(String login, byte[] encryptedPassword, HWID hwid) { public AuthRequest(String login, byte[] password, HWID hwid) {
this(null, login, encryptedPassword, hwid); this(null, login, password, hwid);
}
@Override
public AuthRequestEvent requestWebSockets() throws Exception
{
return (AuthRequestEvent) LegacyRequestBridge.sendRequest(this);
} }
@LauncherAPI @LauncherAPI
public AuthRequest(String login, byte[] encryptedPassword, HWID hwid, int auth_id) { public AuthRequest(String login, byte[] password, HWID hwid, int auth_id) {
this(null, login, encryptedPassword, hwid, auth_id); this(null, login, password, hwid, auth_id);
} }
@Override @Override
@ -115,4 +120,9 @@ protected AuthRequestEvent requestDo(HInput input, HOutput output) throws IOExce
ClientPermissions permissions = new ClientPermissions(input); ClientPermissions permissions = new ClientPermissions(input);
return new AuthRequestEvent(pp, accessToken, permissions); return new AuthRequestEvent(pp, accessToken, permissions);
} }
@Override
public String getType() {
return "auth";
}
} }

View file

@ -3,8 +3,12 @@
import ru.gravit.launcher.Launcher; import ru.gravit.launcher.Launcher;
import ru.gravit.launcher.LauncherAPI; import ru.gravit.launcher.LauncherAPI;
import ru.gravit.launcher.LauncherConfig; import ru.gravit.launcher.LauncherConfig;
import ru.gravit.launcher.LauncherNetworkAPI;
import ru.gravit.launcher.events.request.LauncherRequestEvent;
import ru.gravit.launcher.request.Request; import ru.gravit.launcher.request.Request;
import ru.gravit.launcher.request.RequestType; import ru.gravit.launcher.request.RequestType;
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.HInput;
import ru.gravit.launcher.serialize.HOutput; import ru.gravit.launcher.serialize.HOutput;
import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.IOHelper;
@ -17,27 +21,11 @@
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public final class LauncherRequest extends Request<LauncherRequest.Result> { public final class LauncherRequest extends Request<LauncherRequestEvent> implements RequestInterface {
public static final class Result { @LauncherNetworkAPI
private final byte[] binary; public byte[] digest;
private final byte[] digest; @LauncherNetworkAPI
public int launcher_type = EXE_BINARY ? 2 : 1;
public Result(byte[] binary, byte[] sign) {
this.binary = binary == null ? null : binary.clone();
this.digest = sign.clone();
}
@LauncherAPI
public byte[] getBinary() {
return binary == null ? null : binary.clone();
}
@LauncherAPI
public byte[] getDigest() {
return digest.clone();
}
}
@LauncherAPI @LauncherAPI
public static final Path BINARY_PATH = IOHelper.getCodeSource(Launcher.class); public static final Path BINARY_PATH = IOHelper.getCodeSource(Launcher.class);
@ -45,7 +33,7 @@ public byte[] getDigest() {
public static final boolean EXE_BINARY = IOHelper.hasExtension(BINARY_PATH, "exe"); public static final boolean EXE_BINARY = IOHelper.hasExtension(BINARY_PATH, "exe");
@LauncherAPI @LauncherAPI
public static void update(LauncherConfig config, Result result) throws IOException { public static void update(LauncherConfig config, LauncherRequestEvent result) throws IOException {
List<String> args = new ArrayList<>(8); List<String> args = new ArrayList<>(8);
args.add(IOHelper.resolveJavaBin(null).toString()); args.add(IOHelper.resolveJavaBin(null).toString());
if (LogHelper.isDebugEnabled()) if (LogHelper.isDebugEnabled())
@ -71,9 +59,21 @@ public LauncherRequest() {
this(null); this(null);
} }
@Override
public LauncherRequestEvent requestWebSockets() throws Exception
{
return (LauncherRequestEvent) LegacyRequestBridge.sendRequest(this);
}
@LauncherAPI @LauncherAPI
public LauncherRequest(LauncherConfig config) { public LauncherRequest(LauncherConfig config) {
super(config); super(config);
Path launcherPath = IOHelper.getCodeSource(LauncherRequest.class);
try {
digest = SecurityHelper.digest(SecurityHelper.DigestAlgorithm.SHA512, launcherPath);
} catch (IOException e) {
LogHelper.error(e);
}
} }
@Override @Override
@ -82,9 +82,7 @@ public Integer getLegacyType() {
} }
@Override @Override
protected Result requestDo(HInput input, HOutput output) throws Exception { protected LauncherRequestEvent requestDo(HInput input, HOutput output) throws Exception {
Path launcherPath = IOHelper.getCodeSource(LauncherRequest.class);
byte[] digest = SecurityHelper.digest(SecurityHelper.DigestAlgorithm.SHA512, launcherPath);
output.writeBoolean(EXE_BINARY); output.writeBoolean(EXE_BINARY);
output.writeByteArray(digest, 0); output.writeByteArray(digest, 0);
output.flush(); output.flush();
@ -94,11 +92,16 @@ protected Result requestDo(HInput input, HOutput output) throws Exception {
boolean shouldUpdate = input.readBoolean(); boolean shouldUpdate = input.readBoolean();
if (shouldUpdate) { if (shouldUpdate) {
byte[] binary = input.readByteArray(0); byte[] binary = input.readByteArray(0);
Result result = new Result(binary, digest); LauncherRequestEvent result = new LauncherRequestEvent(binary, digest);
update(Launcher.getConfig(), result); update(Launcher.getConfig(), result);
} }
// Return request result // Return request result
return new Result(null, digest); return new LauncherRequestEvent(null, digest);
}
@Override
public String getType() {
return "launcher";
} }
} }

View file

@ -10,6 +10,10 @@ public class LauncherRequestEvent implements EventInterface, ResultInterface {
private static final UUID uuid = UUID.fromString("d54cc12a-4f59-4f23-9b10-f527fdd2e38f"); private static final UUID uuid = UUID.fromString("d54cc12a-4f59-4f23-9b10-f527fdd2e38f");
@LauncherNetworkAPI @LauncherNetworkAPI
public String url; public String url;
@LauncherNetworkAPI
public byte[] digest;
@LauncherNetworkAPI
public byte[] binary;
public LauncherRequestEvent(boolean needUpdate, String url) { public LauncherRequestEvent(boolean needUpdate, String url) {
this.needUpdate = needUpdate; this.needUpdate = needUpdate;
@ -17,6 +21,17 @@ public LauncherRequestEvent(boolean needUpdate, String url) {
} }
public boolean needUpdate; public boolean needUpdate;
public LauncherRequestEvent(boolean b, byte[] digest) {
this.needUpdate = b;
this.digest = digest;
}
public LauncherRequestEvent(byte[] binary, byte[] digest) { //Legacy support constructor
this.binary = binary;
this.digest = digest;
}
@Override @Override
public UUID getUUID() { public UUID getUUID() {
return uuid; return uuid;