2021-11-29 13:14:51 +08:00
|
|
|
package io.zhile.research.ja.netfilter;
|
|
|
|
|
2021-11-29 16:21:12 +08:00
|
|
|
import io.zhile.research.ja.netfilter.commons.DebugInfo;
|
|
|
|
|
|
|
|
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.net.URL;
|
|
|
|
import java.util.jar.JarFile;
|
|
|
|
|
|
|
|
public class Launcher {
|
2021-12-03 18:04:26 +08:00
|
|
|
private static final String VERSION = "v1.1.6";
|
2021-12-01 12:33:37 +08:00
|
|
|
|
2021-11-29 13:14:51 +08:00
|
|
|
public static void main(String[] args) {
|
|
|
|
printUsage();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void premain(String args, Instrumentation inst) {
|
|
|
|
printUsage();
|
|
|
|
|
2021-11-30 11:06:20 +08:00
|
|
|
URI jarURI;
|
|
|
|
try {
|
|
|
|
jarURI = getJarURI();
|
2021-11-30 19:24:10 +08:00
|
|
|
} catch (Throwable e) {
|
2021-11-30 11:06:20 +08:00
|
|
|
DebugInfo.output("ERROR: Can not locate ja-netfilter jar file.");
|
|
|
|
return;
|
2021-11-29 13:14:51 +08:00
|
|
|
}
|
|
|
|
|
2021-11-30 17:06:48 +08:00
|
|
|
File currentFile = new File(jarURI.getPath());
|
|
|
|
File currentDirectory = currentFile.getParentFile();
|
2021-11-29 13:14:51 +08:00
|
|
|
try {
|
2021-11-30 17:06:48 +08:00
|
|
|
inst.appendToBootstrapClassLoaderSearch(new JarFile(currentFile));
|
2021-11-29 13:14:51 +08:00
|
|
|
} catch (Throwable e) {
|
2021-11-30 11:06:20 +08:00
|
|
|
DebugInfo.output("ERROR: Can not access ja-netfilter jar file.");
|
|
|
|
return;
|
2021-11-29 13:14:51 +08:00
|
|
|
}
|
|
|
|
|
2021-11-30 17:06:48 +08:00
|
|
|
Initializer.init(args, inst, currentDirectory); // for some custom UrlLoaders
|
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-11-29 13:14:51 +08:00
|
|
|
" A javaagent lib for network filter :)\n" +
|
|
|
|
"\n" +
|
|
|
|
" https://github.com/pengzhile/ja-netfilter\n" +
|
|
|
|
"\n" +
|
|
|
|
" ============================================================================ \n\n";
|
|
|
|
|
|
|
|
System.out.print(content);
|
|
|
|
System.out.flush();
|
|
|
|
}
|
|
|
|
|
2021-11-30 11:06:20 +08:00
|
|
|
private static URI getJarURI() throws Exception {
|
2021-11-29 13:14:51 +08:00
|
|
|
URL url = Launcher.class.getProtectionDomain().getCodeSource().getLocation();
|
|
|
|
if (null != url) {
|
2021-11-30 11:06:20 +08:00
|
|
|
return url.toURI();
|
2021-11-29 13:14:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
String resourcePath = "/442fcf28466515a81d5434931496ffa64611cc8e.txt";
|
|
|
|
url = Launcher.class.getResource(resourcePath);
|
|
|
|
if (null == url) {
|
2021-11-30 11:06:20 +08:00
|
|
|
throw new Exception("Can not locate resource file.");
|
2021-11-29 13:14:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
String path = url.getPath();
|
|
|
|
if (!path.endsWith("!" + resourcePath)) {
|
2021-11-30 11:06:20 +08:00
|
|
|
throw new Exception("Invalid resource path.");
|
2021-11-29 13:14:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
path = path.substring(0, path.length() - resourcePath.length() - 1);
|
|
|
|
|
2021-11-30 11:06:20 +08:00
|
|
|
return new URI(path);
|
2021-11-29 13:14:51 +08:00
|
|
|
}
|
|
|
|
}
|