Removed костыльный command handler

This commit is contained in:
dima_dencep 2023-07-02 12:02:42 +07:00
parent 75410477bf
commit 5e63726b1f
No known key found for this signature in database
GPG key ID: ED87FE0D1750A12D
9 changed files with 196 additions and 194 deletions

View file

@ -1,5 +1,6 @@
package pro.gravit.launcher.server.authlib;
import pro.gravit.launcher.server.commands.InstallAuthLib;
import pro.gravit.utils.helper.LogHelper;
import pro.gravit.utils.helper.SecurityHelper;
@ -10,7 +11,7 @@
public class DownloadContextModifier implements LibrariesHashFileModifier {
@Override
public byte[] apply(byte[] data, InstallAuthlib.InstallAuthlibContext context) throws IOException {
public byte[] apply(byte[] data, InstallAuthLib.InstallAuthlibContext context) throws IOException {
String[] lines = new String(data).split("\n");
for(int i=0;i<lines.length;++i) {
if(lines[i].contains("mojang_")) {

View file

@ -1,8 +1,10 @@
package pro.gravit.launcher.server.authlib;
import pro.gravit.launcher.server.commands.InstallAuthLib;
import java.io.IOException;
@FunctionalInterface
public interface LibrariesHashFileModifier {
byte[] apply(byte[] data, InstallAuthlib.InstallAuthlibContext context) throws IOException;
byte[] apply(byte[] data, InstallAuthLib.InstallAuthlibContext context) throws IOException;
}

View file

@ -1,5 +1,6 @@
package pro.gravit.launcher.server.authlib;
import pro.gravit.launcher.server.commands.InstallAuthLib;
import pro.gravit.utils.helper.SecurityHelper;
import java.nio.charset.StandardCharsets;
@ -7,7 +8,7 @@
public class LibrariesLstModifier implements LibrariesHashFileModifier {
@Override
public byte[] apply(byte[] data, InstallAuthlib.InstallAuthlibContext context) {
public byte[] apply(byte[] data, InstallAuthLib.InstallAuthlibContext context) {
String[] lines = new String(data).split("\n");
for(int i=0;i<lines.length;++i) {
if(lines[i].contains("com.mojang:authlib")) {

View file

@ -1,5 +1,6 @@
package pro.gravit.launcher.server.authlib;
import pro.gravit.launcher.server.commands.InstallAuthLib;
import pro.gravit.utils.helper.LogHelper;
import pro.gravit.utils.helper.SecurityHelper;
@ -10,7 +11,7 @@
public class PatchPropertiesModifier implements LibrariesHashFileModifier {
@Override
public byte[] apply(byte[] data, InstallAuthlib.InstallAuthlibContext context) throws IOException {
public byte[] apply(byte[] data, InstallAuthLib.InstallAuthlibContext context) throws IOException {
String[] lines = new String(data).split("\n");
String version = null;
int linePatchedHashIndex = -1;

View file

@ -1,5 +1,6 @@
package pro.gravit.launcher.server.authlib;
import pro.gravit.launcher.server.commands.InstallAuthLib;
import pro.gravit.utils.helper.LogHelper;
import pro.gravit.utils.helper.SecurityHelper;
@ -10,7 +11,7 @@
public class PatchesLstModifier implements LibrariesHashFileModifier {
@Override
public byte[] apply(byte[] data, InstallAuthlib.InstallAuthlibContext context) throws IOException {
public byte[] apply(byte[] data, InstallAuthLib.InstallAuthlibContext context) throws IOException {
String[] lines = new String(data).split("\n");
for(int i=0;i<lines.length;++i) {
if(lines[i].contains("paper-")) {

View file

@ -1,5 +1,7 @@
package pro.gravit.launcher.server.authlib;
package pro.gravit.launcher.server.commands;
import pro.gravit.launcher.server.authlib.*;
import pro.gravit.utils.command.Command;
import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.LogHelper;
@ -13,16 +15,30 @@
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class InstallAuthlib {
private static final Map<String, LibrariesHashFileModifier> modifierMap;
static {
modifierMap = new HashMap<>();
public class InstallAuthLib extends Command {
private final Map<String, LibrariesHashFileModifier> modifierMap = new HashMap<>();
public InstallAuthLib() {
modifierMap.put("META-INF/libraries.list", new LibrariesLstModifier());
modifierMap.put("patch.properties", new PatchPropertiesModifier());
modifierMap.put("META-INF/download-context", new DownloadContextModifier());
modifierMap.put("META-INF/patches.list", new PatchesLstModifier());
}
public void run(String... args) throws Exception {
@Override
public String getArgsDescription() {
return "[Local/Url path to AuthLib]";
}
@Override
public String getUsageDescription() {
return "Patch authlib";
}
@Override
public void invoke(String... args) throws Exception {
verifyArgs(args, 1);
boolean deleteAuthlibAfterInstall = false;
InstallAuthlibContext context = new InstallAuthlibContext();
if(args[0].startsWith("http://") || args[0].startsWith("https://")) {

View file

@ -0,0 +1,163 @@
package pro.gravit.launcher.server.commands;
import pro.gravit.launcher.events.request.GetPublicKeyRequestEvent;
import pro.gravit.launcher.profiles.ClientProfile;
import pro.gravit.launcher.request.Request;
import pro.gravit.launcher.request.auth.GetPublicKeyRequest;
import pro.gravit.launcher.request.websockets.StdWebSocketService;
import pro.gravit.launcher.server.ServerWrapper;
import pro.gravit.utils.PublicURLClassLoader;
import pro.gravit.utils.command.Command;
import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.JVMHelper;
import pro.gravit.utils.helper.LogHelper;
import java.io.File;
import java.io.Writer;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.jar.JarFile;
public class SetupCommand extends Command {
private final ServerWrapper wrapper;
public SetupCommand(ServerWrapper wrapper) {
this.wrapper = wrapper;
}
@Override
public String getArgsDescription() {
return null;
}
@Override
public String getUsageDescription() {
return "Setup ServerWrapper";
}
@Override
public void invoke(String... args) throws Exception {
LogHelper.info("Print server jar filename:");
String jarName = this.wrapper.commandHandler.readLine();
Path jarPath = Paths.get(jarName);
String mainClassName;
String agentClassName;
try (JarFile file = new JarFile(jarPath.toFile())) {
LogHelper.info("Check server jar MainClass");
mainClassName = file.getManifest().getMainAttributes().getValue("Main-Class");
agentClassName = file.getManifest().getMainAttributes().getValue("Premain-Class");
if (mainClassName == null) {
LogHelper.error("Main-Class not found in MANIFEST");
return;
}
try {
Class.forName(mainClassName, false, new PublicURLClassLoader(new URL[] { jarPath.toUri().toURL() }));
} catch (ClassNotFoundException e) {
LogHelper.error(e);
return;
}
}
LogHelper.info("Found MainClass %s", mainClassName);
if (agentClassName != null)
LogHelper.info("Found PremainClass %s", agentClassName);
this.wrapper.config.mainclass = mainClassName;
for (int i = 0; i < 10; ++i) {
if(!Request.isAvailable() || Request.getRequestService().isClosed()) {
LogHelper.info("Print websocket address (ws://host:port/api):");
wrapper.config.address = this.wrapper.commandHandler.readLine();
try {
Request.setRequestService(StdWebSocketService.initWebSockets(this.wrapper.config.address).get());
} catch (Throwable e) {
LogHelper.error(e);
continue;
}
}
LogHelper.info("Print server token:");
this.wrapper.config.extendedTokens.put("checkServer", this.wrapper.commandHandler.readLine());
try {
this.wrapper.restore();
GetPublicKeyRequestEvent publicKeyRequestEvent = new GetPublicKeyRequest().request();
this.wrapper.config.encodedServerRsaPublicKey = publicKeyRequestEvent.rsaPublicKey;
this.wrapper.config.encodedServerEcPublicKey = publicKeyRequestEvent.ecdsaPublicKey;
break;
} catch (Throwable e) {
LogHelper.error(e);
if (Request.isAvailable() && Request.getRequestService() instanceof AutoCloseable) {
((AutoCloseable) Request.getRequestService()).close();
}
}
}
LogHelper.info("(ONLY FOR 1.18+) Use alternative start mode? (Y/N)");
boolean altMode = this.wrapper.commandHandler.readLine().equals("Y");
if (altMode) {
LogHelper.debug("Switch to alternative start mode (1.18+)");
this.wrapper.config.classpath.add(jarName);
this.wrapper.config.classLoaderConfig = ClientProfile.ClassLoaderConfig.LAUNCHER;
}
this.wrapper.saveConfig();
LogHelper.info("Generate start script");
Path startScript = Paths.get(JVMHelper.OS_TYPE == JVMHelper.OS.MUSTDIE ? "start.bat" : "start.sh");
if (Files.exists(startScript)) {
LogHelper.warning("Start script found. Move to start.bak");
IOHelper.move(startScript, Paths.get("start.bak"));
}
try (Writer writer = IOHelper.newWriter(startScript)) {
if (JVMHelper.OS_TYPE == JVMHelper.OS.LINUX) {
writer.append("#!/bin/bash\n\n");
}
writer.append("\"");
writer.append(IOHelper.resolveJavaBin(Paths.get(System.getProperty("java.home")), true).toAbsolutePath().toString());
writer.append("\" ");
if (agentClassName != null) {
writer.append("-javaagent:ServerWrapper.jar ");
writer.append("-Dserverwrapper.agentproxy=");
writer.append(agentClassName);
writer.append(" ");
}
writer.append("-Dserverwrapper.disableSetup=true ");
writer.append("-cp ");
writer.append(IOHelper.getCodeSource(ServerWrapper.class).getFileName().toString());
if(!altMode) {
writer.append(File.pathSeparator);
writer.append(jarName);
}
writer.append(" ");
writer.append(ServerWrapper.class.getName());
writer.append("\n");
}
if (JVMHelper.OS_TYPE != JVMHelper.OS.MUSTDIE) {
if (!startScript.toFile().setExecutable(true)) {
LogHelper.error("Failed to set executable %s", startScript);
}
}
}
}

View file

@ -1,36 +0,0 @@
package pro.gravit.launcher.server.setup;
import pro.gravit.utils.command.CommandHandler;
import pro.gravit.utils.command.JLineCommandHandler;
import pro.gravit.utils.command.StdCommandHandler;
import pro.gravit.utils.helper.LogHelper;
import java.io.IOException;
public class ServerWrapperCommands {
public final CommandHandler commandHandler;
public ServerWrapperCommands(CommandHandler commandHandler) {
this.commandHandler = commandHandler;
}
public ServerWrapperCommands() throws IOException {
// Set command handler
CommandHandler localCommandHandler;
try {
Class.forName("org.jline.terminal.Terminal");
// JLine2 available
localCommandHandler = new JLineCommandHandler();
LogHelper.info("JLine2 terminal enabled");
} catch (ClassNotFoundException ignored) {
localCommandHandler = new StdCommandHandler(true);
LogHelper.warning("JLine2 isn't in classpath, using std");
}
commandHandler = localCommandHandler;
}
public void registerCommands() {
//FUTURE
}
}

View file

@ -1,147 +0,0 @@
package pro.gravit.launcher.server.setup;
import pro.gravit.launcher.events.request.GetPublicKeyRequestEvent;
import pro.gravit.launcher.profiles.ClientProfile;
import pro.gravit.launcher.profiles.ClientProfileVersions;
import pro.gravit.launcher.request.Request;
import pro.gravit.launcher.request.auth.GetPublicKeyRequest;
import pro.gravit.launcher.request.websockets.StdWebSocketService;
import pro.gravit.launcher.server.ServerWrapper;
import pro.gravit.utils.PublicURLClassLoader;
import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.JVMHelper;
import pro.gravit.utils.helper.LogHelper;
import java.io.IOException;
import java.io.Writer;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.jar.JarFile;
public class ServerWrapperSetup {
public ServerWrapperCommands commands;
public PublicURLClassLoader urlClassLoader;
public ServerWrapperSetup() throws IOException {
commands = new ServerWrapperCommands();
}
public void run() throws Exception {
ServerWrapper wrapper = ServerWrapper.wrapper;
System.out.println("Print server jar filename:");
String jarName = commands.commandHandler.readLine();
Path jarPath = Paths.get(jarName);
String mainClassName;
String agentClassName;
try (JarFile file = new JarFile(jarPath.toFile())) {
URL jarURL = jarPath.toUri().toURL();
urlClassLoader = new PublicURLClassLoader(new URL[]{jarURL});
LogHelper.info("Check server jar MainClass");
mainClassName = file.getManifest().getMainAttributes().getValue("Main-Class");
agentClassName = file.getManifest().getMainAttributes().getValue("Premain-Class");
if (mainClassName == null) {
LogHelper.error("Main-Class not found in MANIFEST");
return;
}
try {
Class.forName(mainClassName, false, urlClassLoader);
} catch (ClassNotFoundException e) {
LogHelper.error(e);
return;
}
}
LogHelper.info("Found MainClass %s", mainClassName);
if (agentClassName != null) {
LogHelper.info("Found PremainClass %s", agentClassName);
}
System.out.println("Print your server name:");
wrapper.config.serverName = commands.commandHandler.readLine();
wrapper.config.mainclass = mainClassName;
boolean altMode = false;
for (int i = 0; i < 10; ++i) {
if(!Request.isAvailable() || Request.getRequestService().isClosed()) {
System.out.println("Print launchserver websocket host( ws://host:port/api ):");
wrapper.config.address = commands.commandHandler.readLine();
StdWebSocketService service;
try {
service = StdWebSocketService.initWebSockets(wrapper.config.address).get();
} catch (Throwable e) {
LogHelper.error(e);
continue;
}
Request.setRequestService(service);
}
System.out.println("Print server token:");
String checkServerToken = commands.commandHandler.readLine();
wrapper.config.extendedTokens.put("checkServer", checkServerToken);
wrapper.updateLauncherConfig();
try {
wrapper.restore();
wrapper.getProfiles();
GetPublicKeyRequestEvent publicKeyRequestEvent = new GetPublicKeyRequest().request();
wrapper.config.encodedServerRsaPublicKey = publicKeyRequestEvent.rsaPublicKey;
wrapper.config.encodedServerEcPublicKey = publicKeyRequestEvent.ecdsaPublicKey;
break;
} catch (Throwable e) {
LogHelper.error(e);
if(Request.isAvailable() && Request.getRequestService() instanceof AutoCloseable) {
((AutoCloseable) Request.getRequestService()).close();
}
}
}
if(wrapper.profile != null && wrapper.profile.getVersion().compareTo(ClientProfileVersions.MINECRAFT_1_18) >= 0) {
LogHelper.info("Switch to alternative start mode (1.18)");
wrapper.config.classpath.add(jarName);
wrapper.config.classLoaderConfig = ClientProfile.ClassLoaderConfig.LAUNCHER;
altMode = true;
}
wrapper.saveConfig();
LogHelper.info("Generate start script");
Path startScript;
if (JVMHelper.OS_TYPE == JVMHelper.OS.MUSTDIE) startScript = Paths.get("start.bat");
else startScript = Paths.get("start.sh");
if (Files.exists(startScript)) {
LogHelper.warning("start script found. Move to start.bak");
Path startScriptBak = Paths.get("start.bak");
IOHelper.move(startScript, startScriptBak);
}
try (Writer writer = IOHelper.newWriter(startScript)) {
if (JVMHelper.OS_TYPE == JVMHelper.OS.LINUX) {
writer.append("#!/bin/bash\n\n");
}
writer.append("\"");
writer.append(IOHelper.resolveJavaBin(Paths.get(System.getProperty("java.home")), true).toAbsolutePath().toString());
writer.append("\" ");
if (mainClassName.contains("bungee")) {
LogHelper.info("Found BungeeCord mainclass. Modules dir change to modules_srv");
writer.append(JVMHelper.jvmProperty("serverwrapper.modulesDir", "modules_srv"));
writer.append(" ");
}
if (agentClassName != null) {
writer.append("-javaagent:ServerWrapper.jar ");
writer.append("-Dserverwrapper.agentproxy=".concat(agentClassName));
writer.append(" ");
}
//More args
writer.append("-cp ");
String pathServerWrapper = IOHelper.getCodeSource(ServerWrapper.class).getFileName().toString();
writer.append(pathServerWrapper);
if(!altMode) {
if (JVMHelper.OS_TYPE == JVMHelper.OS.MUSTDIE) {
writer.append(";");
} else writer.append(":");
writer.append(jarName);
}
writer.append(" ");
writer.append(ServerWrapper.class.getName());
writer.append("\n");
}
if(JVMHelper.OS_TYPE != JVMHelper.OS.MUSTDIE) {
if(!startScript.toFile().setExecutable(true)) {
LogHelper.error("Failed to set executable %s", startScript);
}
}
}
}