improve the attach mode

Signed-off-by: pengzhile <pengzhile@gmail.com>
This commit is contained in:
pengzhile 2022-01-25 15:40:39 +08:00
parent 7d8b08d5b3
commit 0469dfd2b5
7 changed files with 60 additions and 12 deletions

View File

@ -1,4 +1,4 @@
# ja-netfilter v2.2.2
# ja-netfilter v2.2.3
### A javaagent framework

View File

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

View File

@ -9,11 +9,26 @@ import java.security.ProtectionDomain;
import java.util.*;
public final class Dispatcher implements ClassFileTransformer {
private final Environment environment;
private final Set<String> classSet = new TreeSet<>();
private final Map<String, List<MyTransformer>> transformerMap = new HashMap<>();
private final List<MyTransformer> 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);

View File

@ -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}";
}
}

View File

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

View File

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

View File

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