diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java index 9e458841..d9cb4933 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java @@ -263,6 +263,7 @@ private SignConf(BlockConfigEntry block, Path coredir) { } public static void main(String... args) throws Throwable { + JVMHelper.checkStackTrace(LaunchServer.class); JVMHelper.verifySystemProperties(LaunchServer.class, true); LogHelper.addOutput(IOHelper.WORKING_DIR.resolve("LaunchServer.log")); LogHelper.printVersion("LaunchServer"); diff --git a/Launcher/build.gradle b/Launcher/build.gradle index 79da931f..f1ab3304 100644 --- a/Launcher/build.gradle +++ b/Launcher/build.gradle @@ -1,4 +1,5 @@ -String realMainClassName = "ru.gravit.launcher.LauncherEngine" + +String realMainClassName = "ru.gravit.launcher.ClientLauncherWrapper" String mainAgentName = "ru.gravit.launcher.LauncherAgent" String mainClassName = "ru.gravit.launcher.relauncher.VerRelauncher" diff --git a/Launcher/runtime/init.js b/Launcher/runtime/init.js index f27431e2..4a0561c6 100644 --- a/Launcher/runtime/init.js +++ b/Launcher/runtime/init.js @@ -10,6 +10,9 @@ var LauncherApp = Java.extend(JSApplication, { }, start: function(primaryStage) { stage = primaryStage; stage.setTitle(config.title); + + // Disable resizable button + stage.setResizable(false); // Set icons for each (var icon in config.icons) { diff --git a/Launcher/src/main/java/ru/gravit/launcher/ClientLauncherWrapper.java b/Launcher/src/main/java/ru/gravit/launcher/ClientLauncherWrapper.java new file mode 100644 index 00000000..053e334f --- /dev/null +++ b/Launcher/src/main/java/ru/gravit/launcher/ClientLauncherWrapper.java @@ -0,0 +1,49 @@ +package ru.gravit.launcher; + +import ru.gravit.launcher.client.ClientLauncher; +import ru.gravit.utils.helper.EnvHelper; +import ru.gravit.utils.helper.IOHelper; +import ru.gravit.utils.helper.JVMHelper; +import ru.gravit.utils.helper.LogHelper; + +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +public class ClientLauncherWrapper { + @LauncherAPI + public static void main(String[] arguments) throws IOException, InterruptedException { + LogHelper.printVersion("Launcher"); + JVMHelper.checkStackTrace(ClientLauncherWrapper.class); + JVMHelper.verifySystemProperties(Launcher.class, true); + EnvHelper.checkDangerousParametrs(); + LogHelper.debug("Restart Launcher"); + ProcessBuilder processBuilder = new ProcessBuilder(); + processBuilder.inheritIO(); + Path javaBin = IOHelper.resolveJavaBin(Paths.get(System.getProperty("java.home"))); + List args = new LinkedList<>(); + args.add(javaBin.toString()); + String pathLauncher = IOHelper.getCodeSource(ClientLauncher.class).toString(); + Collections.addAll(args, "-javaagent:".concat(pathLauncher)); + Collections.addAll(args, LauncherEngine.class.getName()); + args.add(JVMHelper.jvmProperty(LogHelper.DEBUG_PROPERTY, Boolean.toString(LogHelper.isDebugEnabled()))); + EnvHelper.addEnv(processBuilder); + processBuilder.command(args); + Process process = processBuilder.start(); + if(!LogHelper.isDebugEnabled()) { + Thread.sleep(3000); + if (!process.isAlive()) { + LogHelper.error("Process error code: %d", process.exitValue()); + } else { + LogHelper.debug("Process started success"); + } + } + else + { + process.waitFor(); + } + } +} diff --git a/Launcher/src/main/java/ru/gravit/launcher/LauncherEngine.java b/Launcher/src/main/java/ru/gravit/launcher/LauncherEngine.java index 40a033bd..b35f5e40 100644 --- a/Launcher/src/main/java/ru/gravit/launcher/LauncherEngine.java +++ b/Launcher/src/main/java/ru/gravit/launcher/LauncherEngine.java @@ -4,10 +4,10 @@ import java.io.IOException; import java.net.URL; import java.nio.file.Path; +import java.nio.file.Paths; import java.time.Duration; import java.time.Instant; -import java.util.Map; -import java.util.Objects; +import java.util.*; import java.util.concurrent.atomic.AtomicBoolean; import javax.script.Bindings; @@ -148,8 +148,10 @@ public static void addLauncherClassBindings(Map bindings) { } public static void main(String... args) throws Throwable { + JVMHelper.checkStackTrace(LauncherEngine.class); JVMHelper.verifySystemProperties(Launcher.class, true); EnvHelper.checkDangerousParametrs(); + if(!LauncherAgent.isStarted()) throw new SecurityException("JavaAgent not set"); LogHelper.printVersion("Launcher"); // Start Launcher Instant start = Instant.now(); diff --git a/Launcher/src/main/java/ru/gravit/launcher/client/ClientLauncher.java b/Launcher/src/main/java/ru/gravit/launcher/client/ClientLauncher.java index d00431ac..1cc236e2 100644 --- a/Launcher/src/main/java/ru/gravit/launcher/client/ClientLauncher.java +++ b/Launcher/src/main/java/ru/gravit/launcher/client/ClientLauncher.java @@ -416,6 +416,7 @@ public static void main(String... args) throws Throwable { checkJVMBitsAndVersion(); JVMHelper.verifySystemProperties(ClientLauncher.class, true); EnvHelper.checkDangerousParametrs(); + JVMHelper.checkStackTrace(ClientLauncher.class); LogHelper.printVersion("Client Launcher"); // Read and delete params file LogHelper.debug("Reading ClientLauncher params"); diff --git a/libLauncher/src/main/java/ru/gravit/launcher/LauncherAgent.java b/libLauncher/src/main/java/ru/gravit/launcher/LauncherAgent.java index 6a80b2d3..9786c9c8 100644 --- a/libLauncher/src/main/java/ru/gravit/launcher/LauncherAgent.java +++ b/libLauncher/src/main/java/ru/gravit/launcher/LauncherAgent.java @@ -50,4 +50,8 @@ public static void premain(String agentArgument, Instrumentation instrumentation } } } + public static boolean isStarted() + { + return isAgentStarted; + } } diff --git a/libLauncher/src/main/java/ru/gravit/utils/helper/EnvHelper.java b/libLauncher/src/main/java/ru/gravit/utils/helper/EnvHelper.java index 9e6d20a9..a64b466a 100644 --- a/libLauncher/src/main/java/ru/gravit/utils/helper/EnvHelper.java +++ b/libLauncher/src/main/java/ru/gravit/utils/helper/EnvHelper.java @@ -7,68 +7,27 @@ import java.util.regex.Pattern; public class EnvHelper { - private static final boolean TST; - private static final boolean HASXW; public static final String[] toTest; - public static final Pattern[] test; static { toTest = new String[] { "_JAVA_OPTIONS", "_JAVA_OPTS", "JAVA_OPTS", "JAVA_OPTIONS" }; - test = new Pattern[] { Pattern.compile("-xm.*\\d+[KMG]") }; - TST = check0(); - HASXW = check1(); } public static void addEnv(ProcessBuilder builder) { - if (hasOptsEnv()) { - Map repl = new HashMap<>(); - for (String str : toTest) { - repl.put(str, ""); - repl.put(str.toLowerCase(Locale.US), ""); - } - JVMHelper.appendVars(builder, repl); + Map repl = builder.environment(); + for (String str : toTest) { + repl.put(str, ""); + repl.put(str.toLowerCase(Locale.US), ""); } } - private static boolean check0() { - for (String test : toTest) - if (System.getProperty(test) != null) - return true; - return false; - } - - /** - * Вынужденное решение ибо тест на наличие -Xm* этакой нужен. - */ - private static boolean check1() { - if (hasOptsEnv()) - for (String testStr : toTest) - if (System.getenv(testStr) != null) { - String str = System.getenv(testStr).toLowerCase(Locale.US); - StringTokenizer st = new StringTokenizer(str); - while (st.hasMoreTokens()) - if (CommonHelper.multiMatches(test, st.nextToken())) - return true; - } - return false; - } - public static void checkDangerousParametrs() { - if (hasOptsEnv()) - for (String t : toTest) - if (System.getenv(t) != null) { - String env = System.getenv(t).toLowerCase(Locale.US); - if (env.contains("-cp") || env.contains("-classpath") || env.contains("-javaagent") - || env.contains("-agentpath") || env.contains("-agentlib")) - throw new SecurityException("JavaAgent in global optings not allow"); - } - } - - public static boolean hasOptsEnv() { - return TST; - } - - public static boolean hasWarnPreDef() { - return HASXW; + for (String t : toTest) + if (System.getenv(t) != null) { + String env = System.getenv(t).toLowerCase(Locale.US); + if (env.contains("-cp") || env.contains("-classpath") || env.contains("-javaagent") + || env.contains("-agentpath") || env.contains("-agentlib")) + throw new SecurityException("JavaAgent in global optings not allow"); + } } } diff --git a/libLauncher/src/main/java/ru/gravit/utils/helper/JVMHelper.java b/libLauncher/src/main/java/ru/gravit/utils/helper/JVMHelper.java index 446183ae..2165d916 100644 --- a/libLauncher/src/main/java/ru/gravit/utils/helper/JVMHelper.java +++ b/libLauncher/src/main/java/ru/gravit/utils/helper/JVMHelper.java @@ -71,18 +71,6 @@ public static OS byName(String name) { } } - @Deprecated - - public static void addClassPath(URL url) { - throw new IllegalArgumentException("Method Deprecated"); - } - - @Deprecated - - public static void addNativePath(Path path) { - throw new IllegalArgumentException("Method Deprecated"); - } - public static void appendVars(ProcessBuilder builder, Map vars) { builder.environment().putAll(vars); @@ -106,12 +94,6 @@ public static void fullGC() { LogHelper.debug("Used heap: %d MiB", RUNTIME.totalMemory() - RUNTIME.freeMemory() >> 20); } - @Deprecated - - public static Certificate[] getCertificates(String resource) { - throw new IllegalArgumentException("Method Deprecated"); - } - public static String[] getClassPath() { return System.getProperty("java.class.path").split(File.pathSeparator); @@ -134,6 +116,15 @@ public static URL[] getClassPathURL() { } return list; } + public static void checkStackTrace(Class mainClass) + { + Exception e = new Exception("Testing stacktrace"); + StackTraceElement[] list = e.getStackTrace(); + if(!list[list.length - 1].getClassName().equals(mainClass.getName())) + { + throw new SecurityException(String.format("Invalid StackTraceElement: %s",list[list.length - 1].getClassName())); + } + } @SuppressWarnings("CallToSystemGetenv") private static int getCorrectOSArch() {