From 0469dfd2b5a7145f46e67bc49637ad95e9b4fa8d Mon Sep 17 00:00:00 2001 From: pengzhile Date: Tue, 25 Jan 2022 15:40:39 +0800 Subject: [PATCH] improve the attach mode Signed-off-by: pengzhile --- README.md | 2 +- pom.xml | 2 +- .../java/com/janetfilter/core/Dispatcher.java | 15 ++++++++++++++ .../com/janetfilter/core/Environment.java | 17 +++++++++++++--- .../com/janetfilter/core/Initializer.java | 2 +- .../java/com/janetfilter/core/Launcher.java | 20 +++++++++++++------ .../core/plugin/MyTransformer.java | 14 +++++++++++++ 7 files changed, 60 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index abf8812..693e2f3 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# ja-netfilter v2.2.2 +# ja-netfilter v2.2.3 ### A javaagent framework diff --git a/pom.xml b/pom.xml index 6fdba67..73f3697 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.ja-netfilter ja-netfilter - 2.2.2 + 2.2.3 ja-netfilter A javaagent framework diff --git a/src/main/java/com/janetfilter/core/Dispatcher.java b/src/main/java/com/janetfilter/core/Dispatcher.java index d89cd01..0c4cbbc 100644 --- a/src/main/java/com/janetfilter/core/Dispatcher.java +++ b/src/main/java/com/janetfilter/core/Dispatcher.java @@ -9,11 +9,26 @@ import java.security.ProtectionDomain; import java.util.*; public final class Dispatcher implements ClassFileTransformer { + private final Environment environment; private final Set classSet = new TreeSet<>(); private final Map> transformerMap = new HashMap<>(); private final List globalTransformers = new ArrayList<>(); + public Dispatcher(Environment environment) { + this.environment = environment; + } + public synchronized void addTransformer(MyTransformer transformer) { + if (environment.isAttachMode() && !transformer.attachMode()) { + DebugInfo.debug("Transformer: " + transformer.getClass().getName() + " is set to not load in attach mode, ignored."); + return; + } + + if (environment.isJavaagentMode() && !transformer.javaagentMode()) { + DebugInfo.debug("Transformer: " + transformer.getClass().getName() + " is set to not load in -javaagent mode, ignored."); + return; + } + String className = transformer.getHookClassName(); if (null == className) { globalTransformers.add(transformer); diff --git a/src/main/java/com/janetfilter/core/Environment.java b/src/main/java/com/janetfilter/core/Environment.java index 39fce0c..a1a4ad6 100644 --- a/src/main/java/com/janetfilter/core/Environment.java +++ b/src/main/java/com/janetfilter/core/Environment.java @@ -16,12 +16,13 @@ public final class Environment { private final File logsDir; private final String nativePrefix; private final String disabledPluginSuffix; + private final boolean attachMode; - public Environment(File agentFile) { - this(agentFile, null); + public Environment(File agentFile, boolean attachMode) { + this(agentFile, null, attachMode); } - public Environment(File agentFile, String app) { + public Environment(File agentFile, String app, boolean attachMode) { this.agentFile = agentFile; baseDir = agentFile.getParentFile(); @@ -41,6 +42,7 @@ public final class Environment { version = Launcher.VERSION; nativePrefix = StringUtils.randomMethodName(15) + "_"; disabledPluginSuffix = ".disabled.jar"; + this.attachMode = attachMode; } public String getPid() { @@ -83,6 +85,14 @@ public final class Environment { return disabledPluginSuffix; } + public boolean isAttachMode() { + return attachMode; + } + + public boolean isJavaagentMode() { + return !attachMode; + } + @Override public String toString() { return "Environment: {" + @@ -96,6 +106,7 @@ public final class Environment { ", \n\tlogsDir = " + logsDir + ", \n\tnativePrefix = " + nativePrefix + ", \n\tdisabledPluginSuffix = " + disabledPluginSuffix + + ", \n\tattachMode = " + attachMode + "\n}"; } } diff --git a/src/main/java/com/janetfilter/core/Initializer.java b/src/main/java/com/janetfilter/core/Initializer.java index 7634f5c..963703f 100644 --- a/src/main/java/com/janetfilter/core/Initializer.java +++ b/src/main/java/com/janetfilter/core/Initializer.java @@ -11,7 +11,7 @@ public class Initializer { DebugInfo.useFile(environment.getLogsDir()); DebugInfo.info(environment.toString()); - Dispatcher dispatcher = new Dispatcher(); + Dispatcher dispatcher = new Dispatcher(environment); new PluginManager(inst, dispatcher, environment).loadPlugins(); inst.addTransformer(dispatcher, true); diff --git a/src/main/java/com/janetfilter/core/Launcher.java b/src/main/java/com/janetfilter/core/Launcher.java index 33fb8ad..966523d 100644 --- a/src/main/java/com/janetfilter/core/Launcher.java +++ b/src/main/java/com/janetfilter/core/Launcher.java @@ -12,7 +12,7 @@ import java.util.jar.JarFile; public class Launcher { public static final String ATTACH_ARG = "--attach"; - public static final String VERSION = "v2.2.2"; + public static final String VERSION = "v2.2.3"; private static boolean loaded = false; @@ -42,6 +42,18 @@ public class Launcher { } public static void premain(String args, Instrumentation inst) { + 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) { if (loaded) { DebugInfo.warn("You have multiple `ja-netfilter` as javaagent."); return; @@ -66,11 +78,7 @@ public class Launcher { return; } - Initializer.init(inst, new Environment(agentFile, args)); // for some custom UrlLoaders - } - - public static void agentmain(String args, Instrumentation inst) { - premain(args, inst); + Initializer.init(inst, new Environment(agentFile, args, attachMode)); // for some custom UrlLoaders } private static void printUsage() { diff --git a/src/main/java/com/janetfilter/core/plugin/MyTransformer.java b/src/main/java/com/janetfilter/core/plugin/MyTransformer.java index 2d3570e..13964d8 100644 --- a/src/main/java/com/janetfilter/core/plugin/MyTransformer.java +++ b/src/main/java/com/janetfilter/core/plugin/MyTransformer.java @@ -6,6 +6,20 @@ public interface MyTransformer { */ String getHookClassName(); + /** + * whether to load in attach mode + */ + default boolean attachMode() { + return true; + } + + /** + * whether to load in -javaagent mode + */ + default boolean javaagentMode() { + return true; + } + /** * for global transformers only */