From 16c01a34a370eeb993a6b3f9e16ac164098b0145 Mon Sep 17 00:00:00 2001 From: pengzhile Date: Thu, 2 Dec 2021 10:58:59 +0800 Subject: [PATCH] optimize JANF startup performance & fix the encode of config file Signed-off-by: pengzhile --- README.md | 2 +- pom.xml | 2 +- .../java/io/zhile/research/ja/netfilter/Dispatcher.java | 4 ++++ .../java/io/zhile/research/ja/netfilter/Initializer.java | 4 +++- src/main/java/io/zhile/research/ja/netfilter/Launcher.java | 2 +- .../zhile/research/ja/netfilter/commons/ConfigParser.java | 6 ++++-- 6 files changed, 14 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index f3ddb5e..f374e20 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# ja-netfilter v1.1.3 +# ja-netfilter v1.1.4 ### A javaagent lib for network filter diff --git a/pom.xml b/pom.xml index 9312230..515f12d 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ io.zhile.research ja-netfilter - 1.1.3 + 1.1.4 UTF-8 diff --git a/src/main/java/io/zhile/research/ja/netfilter/Dispatcher.java b/src/main/java/io/zhile/research/ja/netfilter/Dispatcher.java index 1d83da6..33a4846 100644 --- a/src/main/java/io/zhile/research/ja/netfilter/Dispatcher.java +++ b/src/main/java/io/zhile/research/ja/netfilter/Dispatcher.java @@ -45,6 +45,10 @@ public class Dispatcher implements ClassFileTransformer { addTransformers(Arrays.asList(transformers)); } + public Set getHookClassNames() { + return transformerMap.keySet(); + } + public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws IllegalClassFormatException { do { if (null == className) { diff --git a/src/main/java/io/zhile/research/ja/netfilter/Initializer.java b/src/main/java/io/zhile/research/ja/netfilter/Initializer.java index b5c46c2..39d8ba5 100644 --- a/src/main/java/io/zhile/research/ja/netfilter/Initializer.java +++ b/src/main/java/io/zhile/research/ja/netfilter/Initializer.java @@ -8,6 +8,7 @@ import io.zhile.research.ja.netfilter.plugin.PluginManager; import java.io.File; import java.lang.instrument.Instrumentation; +import java.util.Set; public class Initializer { public static void init(String args, Instrumentation inst, File currentDirectory) { @@ -28,9 +29,10 @@ public class Initializer { inst.addTransformer(Dispatcher.getInstance(), true); + Set classSet = Dispatcher.getInstance().getHookClassNames(); for (Class c : inst.getAllLoadedClasses()) { String name = c.getName(); - if (name.startsWith("java.lang.invoke.LambdaForm$") || '[' == name.charAt(0)) { + if (!classSet.contains(name.replace('/', '.'))) { continue; } diff --git a/src/main/java/io/zhile/research/ja/netfilter/Launcher.java b/src/main/java/io/zhile/research/ja/netfilter/Launcher.java index 3a64f73..151d55d 100644 --- a/src/main/java/io/zhile/research/ja/netfilter/Launcher.java +++ b/src/main/java/io/zhile/research/ja/netfilter/Launcher.java @@ -9,7 +9,7 @@ import java.net.URL; import java.util.jar.JarFile; public class Launcher { - private static final String VERSION = "v1.1.3"; + private static final String VERSION = "v1.1.4"; public static void main(String[] args) { printUsage(); diff --git a/src/main/java/io/zhile/research/ja/netfilter/commons/ConfigParser.java b/src/main/java/io/zhile/research/ja/netfilter/commons/ConfigParser.java index eb2542b..9ea4b3d 100644 --- a/src/main/java/io/zhile/research/ja/netfilter/commons/ConfigParser.java +++ b/src/main/java/io/zhile/research/ja/netfilter/commons/ConfigParser.java @@ -5,7 +5,9 @@ import io.zhile.research.ja.netfilter.utils.StringUtils; import java.io.BufferedReader; import java.io.File; -import java.io.FileReader; +import java.io.FileInputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -19,7 +21,7 @@ public class ConfigParser { return map; } - try (BufferedReader reader = new BufferedReader(new FileReader(file))) { + try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8))) { int lineNumber = 0; String line, lastSection = null;