2023-12-23 08:05:23 +03:00
|
|
|
package pro.gravit.launcher.client;
|
2019-04-07 10:53:39 +03:00
|
|
|
|
2023-12-23 08:05:23 +03:00
|
|
|
import pro.gravit.launcher.client.runtime.utils.NativeJVMHalt;
|
2019-10-19 19:46:04 +03:00
|
|
|
import pro.gravit.utils.helper.LogHelper;
|
|
|
|
|
2019-08-23 13:35:36 +03:00
|
|
|
import java.io.File;
|
2019-04-07 10:53:39 +03:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.lang.instrument.Instrumentation;
|
2019-08-23 13:35:36 +03:00
|
|
|
import java.nio.file.Path;
|
2019-04-07 10:53:39 +03:00
|
|
|
import java.util.jar.JarFile;
|
|
|
|
|
2019-12-08 22:21:05 +03:00
|
|
|
|
2019-04-07 10:53:39 +03:00
|
|
|
public final class LauncherAgent {
|
|
|
|
public static Instrumentation inst;
|
2020-04-05 10:27:04 +03:00
|
|
|
private static boolean isAgentStarted = false;
|
2019-04-07 10:53:39 +03:00
|
|
|
|
|
|
|
public static void addJVMClassPath(String path) throws IOException {
|
|
|
|
LogHelper.debug("Launcher Agent addJVMClassPath");
|
2019-08-23 13:35:36 +03:00
|
|
|
inst.appendToSystemClassLoaderSearch(new JarFile(new File(path)));
|
|
|
|
}
|
2019-10-19 19:46:04 +03:00
|
|
|
|
2019-08-23 13:35:36 +03:00
|
|
|
public static void addJVMClassPath(Path path) throws IOException {
|
|
|
|
LogHelper.debug("Launcher Agent addJVMClassPath");
|
|
|
|
inst.appendToSystemClassLoaderSearch(new JarFile(path.toFile()));
|
2019-04-07 10:53:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void premain(String agentArgument, Instrumentation instrumentation) {
|
2019-05-15 14:11:22 +03:00
|
|
|
System.out.println("Launcher Agent");
|
2019-09-30 04:40:32 +03:00
|
|
|
checkAgentStacktrace();
|
2019-04-07 10:53:39 +03:00
|
|
|
inst = instrumentation;
|
2019-10-17 17:37:05 +03:00
|
|
|
NativeJVMHalt.initFunc();
|
2019-09-30 04:40:32 +03:00
|
|
|
isAgentStarted = true;
|
|
|
|
}
|
2019-10-19 19:46:04 +03:00
|
|
|
|
|
|
|
public static void checkAgentStacktrace() {
|
2019-09-30 04:40:32 +03:00
|
|
|
RuntimeException ex = new SecurityException("Error check agent stacktrace");
|
|
|
|
boolean isFoundNative = false;
|
|
|
|
boolean foundPreMain = false;
|
2019-10-19 19:46:04 +03:00
|
|
|
for (StackTraceElement e : ex.getStackTrace()) {
|
|
|
|
if (e.isNativeMethod()) {
|
|
|
|
if (!isFoundNative) isFoundNative = true;
|
2019-09-30 04:40:32 +03:00
|
|
|
else throw ex;
|
|
|
|
}
|
2019-10-19 19:46:04 +03:00
|
|
|
if (e.getMethodName().equals("premain")) {
|
|
|
|
if (!foundPreMain) foundPreMain = true;
|
2019-09-30 04:40:32 +03:00
|
|
|
else throw ex;
|
|
|
|
}
|
|
|
|
}
|
2019-10-19 19:46:04 +03:00
|
|
|
if (!isFoundNative || !foundPreMain) throw ex;
|
2019-04-07 10:53:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isStarted() {
|
|
|
|
return isAgentStarted;
|
|
|
|
}
|
2020-04-05 10:27:04 +03:00
|
|
|
|
|
|
|
public boolean isAgentStarted() {
|
|
|
|
return isAgentStarted;
|
|
|
|
}
|
2019-04-07 10:53:39 +03:00
|
|
|
}
|