From 6fdeb04ed93b66948a2c471e8811633c19b8cf15 Mon Sep 17 00:00:00 2001 From: pengzhile Date: Mon, 28 Feb 2022 15:31:14 +0800 Subject: [PATCH] 1. more comprehensive transform methods 2. expose several DebugInfo configs 3. new version number Signed-off-by: pengzhile --- README.md | 2 +- pom.xml | 2 +- .../java/com/janetfilter/core/Dispatcher.java | 10 ++--- .../com/janetfilter/core/Environment.java | 7 ++++ .../java/com/janetfilter/core/Launcher.java | 5 ++- .../janetfilter/core/commons/DebugInfo.java | 16 ++++++-- .../core/plugin/MyTransformer.java | 37 +++++++++++++++++++ 7 files changed, 66 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 7780487..4ecbf28 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# ja-netfilter v2.3.2 +# ja-netfilter 2022.1.0 ### A javaagent framework diff --git a/pom.xml b/pom.xml index e50b503..872a82f 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.ja-netfilter ja-netfilter - 2.3.2 + 2022.1.0 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 30eaa03..93baee0 100644 --- a/src/main/java/com/janetfilter/core/Dispatcher.java +++ b/src/main/java/com/janetfilter/core/Dispatcher.java @@ -87,25 +87,25 @@ public final class Dispatcher implements ClassFileTransformer { try { for (MyTransformer transformer : globalTransformers) { - transformer.before(className, classFileBuffer); + transformer.before(loader, classBeingRedefined, protectionDomain, className, classFileBuffer); } for (MyTransformer transformer : globalTransformers) { - classFileBuffer = transformer.preTransform(className, classFileBuffer, order++); + classFileBuffer = transformer.preTransform(loader, classBeingRedefined, protectionDomain, className, classFileBuffer, order++); } if (null != transformers) { for (MyTransformer transformer : transformers) { - classFileBuffer = transformer.transform(className, classFileBuffer, order++); + classFileBuffer = transformer.transform(loader, classBeingRedefined, protectionDomain, className, classFileBuffer, order++); } } for (MyTransformer transformer : globalTransformers) { - classFileBuffer = transformer.postTransform(className, classFileBuffer, order++); + classFileBuffer = transformer.postTransform(loader, classBeingRedefined, protectionDomain, className, classFileBuffer, order++); } for (MyTransformer transformer : globalTransformers) { - transformer.after(className, classFileBuffer); + transformer.after(loader, classBeingRedefined, protectionDomain, className, classFileBuffer); } } catch (Throwable e) { DebugInfo.error("Transform class failed: " + className, e); diff --git a/src/main/java/com/janetfilter/core/Environment.java b/src/main/java/com/janetfilter/core/Environment.java index 2deb81f..2c380b2 100644 --- a/src/main/java/com/janetfilter/core/Environment.java +++ b/src/main/java/com/janetfilter/core/Environment.java @@ -9,6 +9,7 @@ import java.lang.instrument.Instrumentation; public final class Environment { private final String pid; private final String version; + private final int versionNumber; private final String appName; private final File baseDir; private final File agentFile; @@ -44,6 +45,7 @@ public final class Environment { pid = ProcessUtils.currentId(); version = Launcher.VERSION; + versionNumber = Launcher.VERSION_NUMBER; nativePrefix = StringUtils.randomMethodName(15) + "_"; disabledPluginSuffix = ".disabled.jar"; this.attachMode = attachMode; @@ -57,6 +59,10 @@ public final class Environment { return version; } + public int getVersionNumber() { + return versionNumber; + } + public String getAppName() { return appName; } @@ -106,6 +112,7 @@ public final class Environment { return "Environment: {" + "\n\tpid = " + pid + ", \n\tversion = " + version + + ", \n\tversionNumber = " + versionNumber + ", \n\tappName = " + appName + ", \n\tbaseDir = " + baseDir + ", \n\tagentFile = " + agentFile + diff --git a/src/main/java/com/janetfilter/core/Launcher.java b/src/main/java/com/janetfilter/core/Launcher.java index 3db4bc5..0d6e25d 100644 --- a/src/main/java/com/janetfilter/core/Launcher.java +++ b/src/main/java/com/janetfilter/core/Launcher.java @@ -12,7 +12,8 @@ import java.util.jar.JarFile; public class Launcher { public static final String ATTACH_ARG = "--attach"; - public static final String VERSION = "v2.3.2"; + public static final String VERSION = "2022.1.0"; + public static final int VERSION_NUMBER = 202201000; private static boolean loaded = false; @@ -88,7 +89,7 @@ public class Launcher { private static void printUsage() { String content = "\n ============================================================================ \n" + "\n" + - " ja-netfilter-" + VERSION + + " ja-netfilter " + VERSION + "\n\n" + " A javaagent framework :)\n" + "\n" + diff --git a/src/main/java/com/janetfilter/core/commons/DebugInfo.java b/src/main/java/com/janetfilter/core/commons/DebugInfo.java index 3d161c2..a393083 100644 --- a/src/main/java/com/janetfilter/core/commons/DebugInfo.java +++ b/src/main/java/com/janetfilter/core/commons/DebugInfo.java @@ -13,9 +13,9 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class DebugInfo { - private static final long OUTPUT_CONSOLE = 0x1L; - private static final long OUTPUT_FILE = 0x2L; - private static final long OUTPUT_WITH_PID = 0x4L; + public static final long OUTPUT_CONSOLE = 0x1L; + public static final long OUTPUT_FILE = 0x2L; + public static final long OUTPUT_WITH_PID = 0x4L; private static final ExecutorService CONSOLE_EXECUTOR = Executors.newSingleThreadExecutor(); private static final ExecutorService FILE_EXECUTOR = Executors.newSingleThreadExecutor(); @@ -61,6 +61,14 @@ public class DebugInfo { logDir = dir; } + public static Level getLogLevel() { + return LOG_LEVEL; + } + + public static long getLogOutput() { + return LOG_OUTPUT; + } + public static void debug(String content, Throwable e) { output(Level.DEBUG, content, e); } @@ -115,7 +123,7 @@ public class DebugInfo { } } - private enum Level { + public enum Level { NONE, DEBUG, INFO, WARN, ERROR; public static Level of(String valueStr) { diff --git a/src/main/java/com/janetfilter/core/plugin/MyTransformer.java b/src/main/java/com/janetfilter/core/plugin/MyTransformer.java index 66b2aad..171215a 100644 --- a/src/main/java/com/janetfilter/core/plugin/MyTransformer.java +++ b/src/main/java/com/janetfilter/core/plugin/MyTransformer.java @@ -1,5 +1,7 @@ package com.janetfilter.core.plugin; +import java.security.ProtectionDomain; + public interface MyTransformer { /** * @return class name like this: package/to/className, null means it's a global transformer @@ -30,6 +32,13 @@ public interface MyTransformer { return false; } + /** + * for global transformers only + */ + default void before(ClassLoader loader, Class classBeingRedefined, ProtectionDomain protectionDomain, String className, byte[] classBytes) throws Exception { + before(className, classBytes); + } + /** * for global transformers only */ @@ -37,6 +46,13 @@ public interface MyTransformer { } + /** + * for global transformers only + */ + default byte[] preTransform(ClassLoader loader, Class classBeingRedefined, ProtectionDomain protectionDomain, String className, byte[] classBytes, int order) throws Exception { + return preTransform(className, classBytes, order); + } + /** * for global transformers only */ @@ -44,6 +60,13 @@ public interface MyTransformer { return transform(className, classBytes, order); // for old version } + /** + * for normal transformers only + */ + default byte[] transform(ClassLoader loader, Class classBeingRedefined, ProtectionDomain protectionDomain, String className, byte[] classBytes, int order) throws Exception { + return transform(className, classBytes, order); + } + /** * for normal transformers only */ @@ -51,6 +74,13 @@ public interface MyTransformer { return classBytes; } + /** + * for global transformers only + */ + default byte[] postTransform(ClassLoader loader, Class classBeingRedefined, ProtectionDomain protectionDomain, String className, byte[] classBytes, int order) throws Exception { + return postTransform(className, classBytes, order); + } + /** * for global transformers only */ @@ -58,6 +88,13 @@ public interface MyTransformer { return classBytes; } + /** + * for global transformers only + */ + default void after(ClassLoader loader, Class classBeingRedefined, ProtectionDomain protectionDomain, String className, byte[] classBytes) throws Exception { + after(className, classBytes); + } + /** * for global transformers only */