diff --git a/README.md b/README.md index efd4cf7..41c4f23 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# ja-netfilter v2.1.0 +# ja-netfilter v2.1.1 ### A javaagent framework @@ -10,8 +10,8 @@ * some apps support the `JVM Options file`, you can add as a line of the `JVM Options file`. * **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**. +* edit your plugin config files: `${lower plugin name}.conf` file in the `config` dir where `ja-netfilter.jar` is located. +* the `config` 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 `config-appname` and `plugins-appname`. * if no javaagent args, they default to `config` and `plugins`. * this mechanism will avoid extraneous and bloated `config` and `plugins`. diff --git a/pom.xml b/pom.xml index 2ef86a3..956d137 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.ja-netfilter ja-netfilter - 2.1.0 + 2.1.1 ja-netfilter A javaagent framework @@ -85,6 +85,7 @@ com.janetfilter.core.Launcher true true + true diff --git a/src/main/java/com/janetfilter/core/Dispatcher.java b/src/main/java/com/janetfilter/core/Dispatcher.java index 167f49d..2d44b23 100644 --- a/src/main/java/com/janetfilter/core/Dispatcher.java +++ b/src/main/java/com/janetfilter/core/Dispatcher.java @@ -11,9 +11,15 @@ import java.util.*; public final class Dispatcher implements ClassFileTransformer { private final Set classSet = new TreeSet<>(); private final Map> transformerMap = new HashMap<>(); + private final List globalTransformers = new ArrayList<>(); public synchronized void addTransformer(MyTransformer transformer) { String className = transformer.getHookClassName(); + if (null == className) { + globalTransformers.add(transformer); + return; + } + classSet.add(className.replace('/', '.')); List transformers = transformerMap.computeIfAbsent(className, k -> new ArrayList<>()); @@ -56,6 +62,10 @@ public final class Dispatcher implements ClassFileTransformer { int order = 0; try { + for (MyTransformer transformer : globalTransformers) { + classFileBuffer = transformer.transform(className, classFileBuffer, order++); + } + for (MyTransformer transformer : transformers) { classFileBuffer = transformer.transform(className, classFileBuffer, order++); } diff --git a/src/main/java/com/janetfilter/core/Environment.java b/src/main/java/com/janetfilter/core/Environment.java index aa4ae89..0d873f0 100644 --- a/src/main/java/com/janetfilter/core/Environment.java +++ b/src/main/java/com/janetfilter/core/Environment.java @@ -9,6 +9,7 @@ public final class Environment { private final File agentFile; private final File configDir; private final File pluginsDir; + private final String nativePrefix; public Environment(File agentFile) { this(agentFile, null); @@ -26,6 +27,8 @@ public final class Environment { configDir = new File(baseDir, "config-" + app); pluginsDir = new File(baseDir, "plugins-" + app); } + + nativePrefix = StringUtils.randomMethodName(15) + "_"; } public File getBaseDir() { @@ -44,6 +47,10 @@ public final class Environment { return pluginsDir; } + public String getNativePrefix() { + return nativePrefix; + } + @Override public String toString() { return "Environment: {" + @@ -51,6 +58,7 @@ public final class Environment { ", \n\tagentFile=" + agentFile + ", \n\tconfigDir=" + configDir + ", \n\tpluginsDir=" + pluginsDir + + ", \n\tnativePrefix=" + nativePrefix + "\n}"; } } diff --git a/src/main/java/com/janetfilter/core/Initializer.java b/src/main/java/com/janetfilter/core/Initializer.java index f6d100b..9d609cc 100644 --- a/src/main/java/com/janetfilter/core/Initializer.java +++ b/src/main/java/com/janetfilter/core/Initializer.java @@ -14,6 +14,7 @@ public class Initializer { new PluginManager(inst, dispatcher, environment).loadPlugins(); inst.addTransformer(dispatcher, true); + inst.setNativeMethodPrefix(dispatcher, environment.getNativePrefix()); Set classSet = dispatcher.getHookClassNames(); for (Class c : inst.getAllLoadedClasses()) { diff --git a/src/main/java/com/janetfilter/core/Launcher.java b/src/main/java/com/janetfilter/core/Launcher.java index 66ed0e9..e0cd5a1 100644 --- a/src/main/java/com/janetfilter/core/Launcher.java +++ b/src/main/java/com/janetfilter/core/Launcher.java @@ -9,17 +9,25 @@ import java.net.URL; import java.util.jar.JarFile; public class Launcher { - private static final String VERSION = "v2.1.0"; + private static final String VERSION = "v2.1.1"; + + private static boolean loaded = false; public static void main(String[] args) { printUsage(); } public static void premain(String args, Instrumentation inst) { + if (loaded) { + DebugInfo.output("WARN: You have multiple `ja-netfilter` as -javaagent."); + return; + } + printUsage(); URI jarURI; try { + loaded = true; jarURI = getJarURI(); } catch (Throwable e) { DebugInfo.output("ERROR: Can not locate ja-netfilter jar file.", e); diff --git a/src/main/java/com/janetfilter/core/utils/StringUtils.java b/src/main/java/com/janetfilter/core/utils/StringUtils.java index 09b72f3..feab05a 100644 --- a/src/main/java/com/janetfilter/core/utils/StringUtils.java +++ b/src/main/java/com/janetfilter/core/utils/StringUtils.java @@ -1,7 +1,28 @@ package com.janetfilter.core.utils; +import java.util.Random; + public class StringUtils { + private static final String METHOD_NAME_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz$_0123456789"; + public static boolean isEmpty(String str) { return null == str || str.isEmpty(); } + + public static String randomMethodName(int length) { + int i = 0; + if (i == length) { + return ""; + } + + char[] buffer = new char[length]; + Random rnd = new Random(); + + buffer[i++] = METHOD_NAME_CHARS.charAt(rnd.nextInt(54)); + while (i < length) { + buffer[i++] = METHOD_NAME_CHARS.charAt(rnd.nextInt(64)); + } + + return new String(buffer); + } }