diff --git a/ServerWrapper/src/main/java/ru/gravit/launcher/server/ServerWrapper.java b/ServerWrapper/src/main/java/ru/gravit/launcher/server/ServerWrapper.java index 999b8593..c4c8b3a4 100644 --- a/ServerWrapper/src/main/java/ru/gravit/launcher/server/ServerWrapper.java +++ b/ServerWrapper/src/main/java/ru/gravit/launcher/server/ServerWrapper.java @@ -9,6 +9,7 @@ import ru.gravit.launcher.profiles.ClientProfile; import ru.gravit.launcher.request.auth.AuthServerRequest; import ru.gravit.launcher.request.update.ProfilesRequest; +import ru.gravit.launcher.server.setup.ServerWrapperSetup; import ru.gravit.utils.PublicURLClassLoader; import ru.gravit.utils.helper.CommonHelper; import ru.gravit.utils.helper.IOHelper; @@ -33,12 +34,14 @@ public class ServerWrapper { public static PublicURLClassLoader ucp; public static ClassLoader loader; public static ClientPermissions permissions; + public static ServerWrapper wrapper; private static Gson gson; private static GsonBuilder gsonBuiler; public static Path modulesDir = Paths.get(System.getProperty("serverwrapper.modulesDir", "modules")); public static Path configFile = Paths.get(System.getProperty("serverwrapper.configFile", "ServerWrapperConfig.json")); public static Path publicKeyFile = Paths.get(System.getProperty("serverwrapper.publicKeyFile", "public.key")); + public static boolean disableSetup = Boolean.valueOf(System.getProperty("serverwrapper.disableSetup", "false")); public static boolean auth(ServerWrapper wrapper) { try { @@ -93,18 +96,29 @@ public static void initGson() { } public static void main(String... args) throws Throwable { - ServerWrapper wrapper = new ServerWrapper(); LogHelper.printVersion("ServerWrapper"); LogHelper.printLicense("ServerWrapper"); + wrapper = new ServerWrapper(); + gsonBuiler = new GsonBuilder(); + gsonBuiler.setPrettyPrinting(); + gson = gsonBuiler.create(); + initGson(); + if(args.length > 0 && args[0].equals("setup")) + { + generateConfigIfNotExists(); + LogHelper.debug("Read ServerWrapperConfig.json"); + try (Reader reader = IOHelper.newReader(configFile)) { + config = gson.fromJson(reader, Config.class); + } + ServerWrapperSetup setup = new ServerWrapperSetup(); + setup.run(); + System.exit(0); + } modulesManager = new ModulesManager(wrapper); modulesManager.autoload(modulesDir); Launcher.modulesManager = modulesManager; modulesManager.preInitModules(); LogHelper.debug("Read ServerWrapperConfig.json"); - gsonBuiler = new GsonBuilder(); - gsonBuiler.setPrettyPrinting(); - gson = gsonBuiler.create(); - initGson(); generateConfigIfNotExists(); try (Reader reader = IOHelper.newReader(configFile)) { config = gson.fromJson(reader, Config.class); @@ -160,11 +174,15 @@ public static void main(String... args) throws Throwable { LogHelper.debug("Invoke main method %s", mainClass.getName()); if(config.args == null) { - String[] real_args = new String[args.length - 1]; - System.arraycopy(args, 1, real_args, 0, args.length - 1); + String[] real_args; + if(args.length > 0) + { + real_args = new String[args.length - 1]; + System.arraycopy(args, 1, real_args, 0, args.length - 1); + } else real_args = args; + mainMethod.invoke(real_args); } - else { mainMethod.invoke(config.args); @@ -194,10 +212,7 @@ private static void generateConfigIfNotExists() throws IOException { LogHelper.warning("Title is not set. Please show ServerWrapper.cfg"); // Write LaunchServer config - LogHelper.info("Writing ServerWrapper config file"); - try (Writer writer = IOHelper.newWriter(configFile)) { - gson.toJson(newConfig, writer); - } + newConfig.save(); } public static final class Config { @@ -220,6 +235,13 @@ public static final class Config { public String password; public String auth_id = ""; public LauncherConfig.LauncherEnvironment env; + public void save() throws IOException + { + LogHelper.info("Writing ServerWrapper config file"); + try (Writer writer = IOHelper.newWriter(configFile)) { + gson.toJson(this, writer); + } + } } public ClientProfile profile; diff --git a/ServerWrapper/src/main/java/ru/gravit/launcher/server/setup/ServerWrapperCommands.java b/ServerWrapper/src/main/java/ru/gravit/launcher/server/setup/ServerWrapperCommands.java new file mode 100644 index 00000000..7ab8e07d --- /dev/null +++ b/ServerWrapper/src/main/java/ru/gravit/launcher/server/setup/ServerWrapperCommands.java @@ -0,0 +1,36 @@ +package ru.gravit.launcher.server.setup; + +import ru.gravit.utils.command.CommandHandler; +import ru.gravit.utils.command.JLineCommandHandler; +import ru.gravit.utils.command.StdCommandHandler; +import ru.gravit.utils.helper.LogHelper; + +import java.io.IOException; + +public class ServerWrapperCommands { + public final CommandHandler commandHandler; + public void registerCommands() + { + //FUTURE + } + + public ServerWrapperCommands(CommandHandler commandHandler) { + this.commandHandler = commandHandler; + } + + public ServerWrapperCommands() throws IOException { + // Set command handler + CommandHandler localCommandHandler; + try { + Class.forName("jline.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; + } +} diff --git a/ServerWrapper/src/main/java/ru/gravit/launcher/server/setup/ServerWrapperSetup.java b/ServerWrapper/src/main/java/ru/gravit/launcher/server/setup/ServerWrapperSetup.java new file mode 100644 index 00000000..95af4fc6 --- /dev/null +++ b/ServerWrapper/src/main/java/ru/gravit/launcher/server/setup/ServerWrapperSetup.java @@ -0,0 +1,141 @@ +package ru.gravit.launcher.server.setup; + +import ru.gravit.launcher.Launcher; +import ru.gravit.launcher.LauncherConfig; +import ru.gravit.launcher.server.ServerWrapper; +import ru.gravit.utils.PublicURLClassLoader; +import ru.gravit.utils.helper.IOHelper; +import ru.gravit.utils.helper.JVMHelper; +import ru.gravit.utils.helper.LogHelper; +import ru.gravit.utils.helper.SecurityHelper; + +import java.io.BufferedWriter; +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.security.spec.InvalidKeySpecException; +import java.util.HashMap; +import java.util.jar.JarFile; + +public class ServerWrapperSetup { + public ServerWrapperCommands commands; + public PublicURLClassLoader urlClassLoader; + public void run() throws IOException + { + System.out.println("Print jar filename:"); + String jarName = commands.commandHandler.readLine(); + Path jarPath = Paths.get(jarName); + JarFile file = new JarFile(jarPath.toFile()); + URL jarURL = jarPath.toUri().toURL(); + urlClassLoader = new PublicURLClassLoader(new URL[]{jarURL}); + LogHelper.info("Check jar MainClass"); + String mainClassName = file.getManifest().getMainAttributes().getValue("Main-Class"); + if(mainClassName == null) + { + LogHelper.error("Main-Class not found in MANIFEST"); + return; + } + try { + Class mainClass = Class.forName(mainClassName, false, urlClassLoader); + } catch (ClassNotFoundException e) + { + LogHelper.error(e); + return; + } + LogHelper.info("Found MainClass %s", mainClassName); + System.out.println("Print launchserver host:"); + String address = commands.commandHandler.readLine(); + System.out.println("Print launchserver port:"); + int port = Integer.valueOf(commands.commandHandler.readLine()); + ServerWrapper.config.mainclass = mainClassName; + ServerWrapper.config.address = address; + ServerWrapper.config.port = port; + if(!Files.exists(ServerWrapper.publicKeyFile)) + { + LogHelper.error("public.key not found"); + for(int i=0;i<10;++i) + { + System.out.println("Print F to continue:"); + String printF = commands.commandHandler.readLine(); + if(printF.equals("stop")) return; + if(Files.exists(ServerWrapper.publicKeyFile)) break; + else LogHelper.error("public.key not found"); + } + } + boolean stopOnError = ServerWrapper.config.stopOnError; + for(int i=0;i<10;++i) + { + System.out.println("Print server account login:"); + String login = commands.commandHandler.readLine(); + System.out.println("Print server account password:"); + String password = commands.commandHandler.readLine(); + System.out.println("Print profile title:"); + String title = commands.commandHandler.readLine(); + ServerWrapper.config.login = login; + ServerWrapper.config.password = password; + ServerWrapper.config.title = title; + ServerWrapper.config.stopOnError = false; + LauncherConfig cfg = null; + try { + cfg = new LauncherConfig(ServerWrapper.config.address, ServerWrapper.config.port, SecurityHelper.toPublicRSAKey(IOHelper.read(ServerWrapper.publicKeyFile)), new HashMap<>(), ServerWrapper.config.projectname); + } catch (InvalidKeySpecException e) { + LogHelper.error(e); + } + Launcher.setConfig(cfg); + if(ServerWrapper.auth(ServerWrapper.wrapper)) + { + break; + } + else + { + LogHelper.error("Auth error. Recheck account params"); + } + } + ServerWrapper.config.stopOnError = stopOnError; + ServerWrapper.config.save(); + 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/sh\n\n"); + } + writer.append("java "); + 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(" "); + } + //More args + writer.append("-cp "); + String pathServerWrapper = IOHelper.getCodeSource(ServerWrapper.class).getFileName().toString(); + writer.append(pathServerWrapper); + 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"); + } + + } + + public ServerWrapperSetup() throws IOException { + commands = new ServerWrapperCommands(); + } +}