2021-12-06 10:49:40 +08:00
|
|
|
package com.janetfilter.core;
|
2021-11-29 13:14:51 +08:00
|
|
|
|
2022-01-20 14:55:03 +08:00
|
|
|
import com.janetfilter.core.attach.VMLauncher;
|
|
|
|
import com.janetfilter.core.attach.VMSelector;
|
2021-12-06 10:49:40 +08:00
|
|
|
import com.janetfilter.core.commons.DebugInfo;
|
2022-01-20 14:55:03 +08:00
|
|
|
import com.janetfilter.core.utils.WhereIsUtils;
|
2021-11-29 16:21:12 +08:00
|
|
|
|
|
|
|
import java.io.File;
|
2021-11-29 13:14:51 +08:00
|
|
|
import java.lang.instrument.Instrumentation;
|
2021-11-30 11:06:20 +08:00
|
|
|
import java.net.URI;
|
2021-11-29 13:14:51 +08:00
|
|
|
import java.util.jar.JarFile;
|
|
|
|
|
|
|
|
public class Launcher {
|
2022-01-20 14:55:03 +08:00
|
|
|
public static final String ATTACH_ARG = "--attach";
|
2022-02-15 17:34:24 +08:00
|
|
|
public static final String VERSION = "v2.3.0";
|
2022-01-14 19:53:31 +08:00
|
|
|
|
|
|
|
private static boolean loaded = false;
|
2021-12-01 12:33:37 +08:00
|
|
|
|
2021-11-29 13:14:51 +08:00
|
|
|
public static void main(String[] args) {
|
2022-01-20 14:55:03 +08:00
|
|
|
URI jarURI;
|
|
|
|
try {
|
|
|
|
jarURI = WhereIsUtils.getJarURI();
|
|
|
|
} catch (Throwable e) {
|
|
|
|
DebugInfo.error("Can not locate `ja-netfilter` jar file.", e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
String jarPath = jarURI.getPath();
|
|
|
|
if (args.length > 1 && args[0].equals(ATTACH_ARG)) {
|
|
|
|
VMLauncher.attachVM(jarPath, args[1], args.length > 2 ? args[2] : null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-29 13:14:51 +08:00
|
|
|
printUsage();
|
2022-01-20 14:55:03 +08:00
|
|
|
|
|
|
|
try {
|
|
|
|
new VMSelector(new File(jarPath)).select();
|
|
|
|
} catch (Throwable e) {
|
|
|
|
System.err.println(" ERROR: Select virtual machine failed.");
|
|
|
|
e.printStackTrace(System.err);
|
|
|
|
}
|
2021-11-29 13:14:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void premain(String args, Instrumentation inst) {
|
2022-01-25 15:40:39 +08:00
|
|
|
premain(args, inst, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void agentmain(String args, Instrumentation inst) {
|
|
|
|
if (null == System.getProperty("janf.debug")) {
|
|
|
|
System.setProperty("janf.debug", "1");
|
|
|
|
}
|
|
|
|
|
|
|
|
premain(args, inst, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void premain(String args, Instrumentation inst, boolean attachMode) {
|
2022-01-14 19:53:31 +08:00
|
|
|
if (loaded) {
|
2022-01-20 14:55:03 +08:00
|
|
|
DebugInfo.warn("You have multiple `ja-netfilter` as javaagent.");
|
2022-01-14 19:53:31 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-29 13:14:51 +08:00
|
|
|
printUsage();
|
|
|
|
|
2021-11-30 11:06:20 +08:00
|
|
|
URI jarURI;
|
|
|
|
try {
|
2022-01-14 19:53:31 +08:00
|
|
|
loaded = true;
|
2022-01-20 14:55:03 +08:00
|
|
|
jarURI = WhereIsUtils.getJarURI();
|
2021-11-30 19:24:10 +08:00
|
|
|
} catch (Throwable e) {
|
2022-01-20 14:55:03 +08:00
|
|
|
DebugInfo.error("Can not locate `ja-netfilter` jar file.", e);
|
2021-11-30 11:06:20 +08:00
|
|
|
return;
|
2021-11-29 13:14:51 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 10:49:40 +08:00
|
|
|
File agentFile = new File(jarURI.getPath());
|
2021-11-29 13:14:51 +08:00
|
|
|
try {
|
2021-12-06 10:49:40 +08:00
|
|
|
inst.appendToBootstrapClassLoaderSearch(new JarFile(agentFile));
|
2021-11-29 13:14:51 +08:00
|
|
|
} catch (Throwable e) {
|
2022-01-20 14:55:03 +08:00
|
|
|
DebugInfo.error("Can not access `ja-netfilter` jar file.", e);
|
2021-11-30 11:06:20 +08:00
|
|
|
return;
|
2021-11-29 13:14:51 +08:00
|
|
|
}
|
|
|
|
|
2022-02-15 17:34:24 +08:00
|
|
|
Initializer.init(new Environment(inst, agentFile, args, attachMode)); // for some custom UrlLoaders
|
2022-01-20 14:55:03 +08:00
|
|
|
}
|
|
|
|
|
2021-11-29 13:14:51 +08:00
|
|
|
private static void printUsage() {
|
|
|
|
String content = "\n ============================================================================ \n" +
|
|
|
|
"\n" +
|
2021-12-01 12:33:37 +08:00
|
|
|
" ja-netfilter-" + VERSION +
|
|
|
|
"\n\n" +
|
2021-12-06 11:00:51 +08:00
|
|
|
" A javaagent framework :)\n" +
|
2021-11-29 13:14:51 +08:00
|
|
|
"\n" +
|
2021-12-06 10:49:40 +08:00
|
|
|
" https://github.com/ja-netfilter/ja-netfilter\n" +
|
2021-11-29 13:14:51 +08:00
|
|
|
"\n" +
|
|
|
|
" ============================================================================ \n\n";
|
|
|
|
|
|
|
|
System.out.print(content);
|
|
|
|
}
|
|
|
|
}
|