1. fix debug info (thank RayGicEFL)

2. split conf and plugins directories by javaagent args

Signed-off-by: pengzhile <pengzhile@gmail.com>
This commit is contained in:
pengzhile 2022-01-13 19:31:41 +08:00
parent 9f0d195b93
commit f625cfc738
7 changed files with 47 additions and 11 deletions

View File

@ -1,4 +1,4 @@
# ja-netfilter v2.0.1
# ja-netfilter v2.1.0
### A javaagent framework
@ -11,6 +11,10 @@
* **WARNING: DO NOT put some unnecessary whitespace characters!**
* edit your plugin config files: `${lower plugin name}.conf` file in the `conf` dir where `ja-netfilter.jar` is located.
* the `conf` and `plugins` directory can be specified through **the javaagent args**.
* eg: `-javaagent:/path/to/ja-netfilter.jar=appName`, your config and plugins directories will be `conf-appname` and `plugins-appname`.
* if no javaagent args, they default to `conf` and `plugins`.
* this mechanism will avoid extraneous and bloated `conf` and `plugins`.
* run your java application and enjoy~

View File

@ -6,7 +6,7 @@
<groupId>com.ja-netfilter</groupId>
<artifactId>ja-netfilter</artifactId>
<version>2.0.1</version>
<version>2.1.0</version>
<name>ja-netfilter</name>
<description>A javaagent framework</description>

View File

@ -1,5 +1,7 @@
package com.janetfilter.core;
import com.janetfilter.core.utils.StringUtils;
import java.io.File;
public final class Environment {
@ -9,10 +11,21 @@ public final class Environment {
private final File pluginsDir;
public Environment(File agentFile) {
this(agentFile, null);
}
public Environment(File agentFile, String app) {
this.agentFile = agentFile;
baseDir = agentFile.getParentFile();
configDir = new File(baseDir, "config");
pluginsDir = new File(baseDir, "plugins");
if (StringUtils.isEmpty(app)) {
configDir = new File(baseDir, "config");
pluginsDir = new File(baseDir, "plugins");
} else {
app = app.toLowerCase();
configDir = new File(baseDir, "config-" + app);
pluginsDir = new File(baseDir, "plugins-" + app);
}
}
public File getBaseDir() {
@ -30,4 +43,14 @@ public final class Environment {
public File getPluginsDir() {
return pluginsDir;
}
@Override
public String toString() {
return "Environment: {" +
"\n\tbaseDir=" + baseDir +
", \n\tagentFile=" + agentFile +
", \n\tconfigDir=" + configDir +
", \n\tpluginsDir=" + pluginsDir +
"\n}";
}
}

View File

@ -8,6 +8,8 @@ import java.util.Set;
public class Initializer {
public static void init(Instrumentation inst, Environment environment) {
DebugInfo.output(environment.toString());
Dispatcher dispatcher = new Dispatcher();
new PluginManager(inst, dispatcher, environment).loadPlugins();

View File

@ -9,7 +9,7 @@ import java.net.URL;
import java.util.jar.JarFile;
public class Launcher {
private static final String VERSION = "v2.0.1";
private static final String VERSION = "v2.1.0";
public static void main(String[] args) {
printUsage();
@ -34,7 +34,7 @@ public class Launcher {
return;
}
Initializer.init(inst, new Environment(agentFile)); // for some custom UrlLoaders
Initializer.init(inst, new Environment(agentFile, args)); // for some custom UrlLoaders
}
private static void printUsage() {
@ -58,7 +58,7 @@ public class Launcher {
return url.toURI();
}
String resourcePath = "/5a1666cf298cd1d4fa64d62d123af55f5f39024f.txt";
String resourcePath = "/4cc9c353c626d6510ca855ab6907ed7f64400257.txt";
url = Launcher.class.getResource(resourcePath);
if (null == url) {
throw new Exception("Can not locate resource file.");

View File

@ -4,6 +4,8 @@ import com.janetfilter.core.utils.DateUtils;
public class DebugInfo {
private static final boolean DEBUG = "1".equals(System.getenv("JANF_DEBUG")) || "1".equals(System.getProperty("janf.debug"));
private static final String CLASS_NAME = DebugInfo.class.getName();
private static final String LOG_TEMPLATE = "[%s] %s DEBUG : %s%n";
public static void output(String content) {
output(content, null);
@ -14,12 +16,17 @@ public class DebugInfo {
return;
}
String template = "[%s] %s DEBUG : %s%n";
String caller = "UNKNOWN";
StackTraceElement[] traces = new Throwable().getStackTrace();
String caller = traces.length < 2 ? "UNKNOWN" : traces[1].toString();
for (int i = 1, l = traces.length; i < l; i++) { // thank RayGicEFL
StackTraceElement element = traces[i];
if (!CLASS_NAME.equals(element.getClassName())) {
caller = element.toString();
break;
}
}
String outContent = String.format(template, DateUtils.formatNow(), caller, content);
String outContent = String.format(LOG_TEMPLATE, DateUtils.formatNow(), caller, content);
if (null == e) {
System.out.print(outContent);
return;