1. more comprehensive transform methods

2. expose several DebugInfo configs
3. new version number

Signed-off-by: pengzhile <pengzhile@gmail.com>
This commit is contained in:
pengzhile 2022-02-28 15:31:14 +08:00
parent a9df6b6634
commit 6fdeb04ed9
7 changed files with 66 additions and 13 deletions

View File

@ -1,4 +1,4 @@
# ja-netfilter v2.3.2
# ja-netfilter 2022.1.0
### A javaagent framework

View File

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

View File

@ -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);

View File

@ -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 +

View File

@ -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" +

View File

@ -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) {

View File

@ -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
*/