From e622c8fed4f45a441003d1dc98438e8ae80ecf5e Mon Sep 17 00:00:00 2001 From: pengzhile Date: Mon, 6 Dec 2021 10:49:40 +0800 Subject: [PATCH] rename package && separate builtin plugins Signed-off-by: pengzhile --- README.md | 2 +- pom.xml | 8 +-- .../janetfilter/core}/Dispatcher.java | 25 +++---- .../com/janetfilter/core/Environment.java | 27 ++++++++ .../janetfilter/core}/Initializer.java | 25 +++---- .../janetfilter/core}/Launcher.java | 17 +++-- .../core}/commons/ConfigDetector.java | 4 +- .../core}/commons/ConfigParser.java | 6 +- .../janetfilter/core}/commons/DebugInfo.java | 4 +- .../janetfilter/core}/enums/RuleType.java | 4 +- .../core}/models/FilterConfig.java | 2 +- .../janetfilter/core}/models/FilterRule.java | 4 +- .../core}/plugin/MyTransformer.java | 2 +- .../core}/plugin/PluginClassLoader.java | 4 +- .../janetfilter/core}/plugin/PluginEntry.java | 7 +- .../core}/plugin/PluginManager.java | 42 +++++------- .../core}/rulers/EqualICRuler.java | 2 +- .../janetfilter/core}/rulers/EqualRuler.java | 2 +- .../core}/rulers/KeywordICRuler.java | 2 +- .../core}/rulers/KeywordRuler.java | 2 +- .../core}/rulers/PrefixICRuler.java | 2 +- .../janetfilter/core}/rulers/PrefixRuler.java | 2 +- .../janetfilter/core}/rulers/RegExpRuler.java | 2 +- .../janetfilter/core}/rulers/Ruler.java | 2 +- .../core}/rulers/SuffixICRuler.java | 2 +- .../janetfilter/core}/rulers/SuffixRuler.java | 2 +- .../janetfilter/core}/utils/DateUtils.java | 2 +- .../janetfilter/core}/utils/StringUtils.java | 2 +- .../ja/netfilter/plugins/dns/DNSFilter.java | 50 -------------- .../plugins/dns/DNSFilterPlugin.java | 42 ------------ .../plugins/dns/InetAddressTransformer.java | 67 ------------------- .../plugins/url/HttpClientTransformer.java | 50 -------------- .../ja/netfilter/plugins/url/URLFilter.java | 34 ---------- .../plugins/url/URLFilterPlugin.java | 42 ------------ ...909d6ba41ae03fb85af5b8ba702709f5798cf.txt} | 0 35 files changed, 111 insertions(+), 381 deletions(-) rename src/main/java/{io/zhile/research/ja/netfilter => com/janetfilter/core}/Dispatcher.java (76%) create mode 100644 src/main/java/com/janetfilter/core/Environment.java rename src/main/java/{io/zhile/research/ja/netfilter => com/janetfilter/core}/Initializer.java (52%) rename src/main/java/{io/zhile/research/ja/netfilter => com/janetfilter/core}/Launcher.java (79%) rename src/main/java/{io/zhile/research/ja/netfilter => com/janetfilter/core}/commons/ConfigDetector.java (96%) rename src/main/java/{io/zhile/research/ja/netfilter => com/janetfilter/core}/commons/ConfigParser.java (95%) rename src/main/java/{io/zhile/research/ja/netfilter => com/janetfilter/core}/commons/DebugInfo.java (84%) rename src/main/java/{io/zhile/research/ja/netfilter => com/janetfilter/core}/enums/RuleType.java (84%) rename src/main/java/{io/zhile/research/ja/netfilter => com/janetfilter/core}/models/FilterConfig.java (95%) rename src/main/java/{io/zhile/research/ja/netfilter => com/janetfilter/core}/models/FilterRule.java (92%) rename src/main/java/{io/zhile/research/ja/netfilter => com/janetfilter/core}/plugin/MyTransformer.java (80%) rename src/main/java/{io/zhile/research/ja/netfilter => com/janetfilter/core}/plugin/PluginClassLoader.java (92%) rename src/main/java/{io/zhile/research/ja/netfilter => com/janetfilter/core}/plugin/PluginEntry.java (62%) rename src/main/java/{io/zhile/research/ja/netfilter => com/janetfilter/core}/plugin/PluginManager.java (66%) rename src/main/java/{io/zhile/research/ja/netfilter => com/janetfilter/core}/rulers/EqualICRuler.java (78%) rename src/main/java/{io/zhile/research/ja/netfilter => com/janetfilter/core}/rulers/EqualRuler.java (77%) rename src/main/java/{io/zhile/research/ja/netfilter => com/janetfilter/core}/rulers/KeywordICRuler.java (80%) rename src/main/java/{io/zhile/research/ja/netfilter => com/janetfilter/core}/rulers/KeywordRuler.java (77%) rename src/main/java/{io/zhile/research/ja/netfilter => com/janetfilter/core}/rulers/PrefixICRuler.java (80%) rename src/main/java/{io/zhile/research/ja/netfilter => com/janetfilter/core}/rulers/PrefixRuler.java (77%) rename src/main/java/{io/zhile/research/ja/netfilter => com/janetfilter/core}/rulers/RegExpRuler.java (81%) rename src/main/java/{io/zhile/research/ja/netfilter => com/janetfilter/core}/rulers/Ruler.java (70%) rename src/main/java/{io/zhile/research/ja/netfilter => com/janetfilter/core}/rulers/SuffixICRuler.java (80%) rename src/main/java/{io/zhile/research/ja/netfilter => com/janetfilter/core}/rulers/SuffixRuler.java (77%) rename src/main/java/{io/zhile/research/ja/netfilter => com/janetfilter/core}/utils/DateUtils.java (96%) rename src/main/java/{io/zhile/research/ja/netfilter => com/janetfilter/core}/utils/StringUtils.java (73%) delete mode 100644 src/main/java/io/zhile/research/ja/netfilter/plugins/dns/DNSFilter.java delete mode 100644 src/main/java/io/zhile/research/ja/netfilter/plugins/dns/DNSFilterPlugin.java delete mode 100644 src/main/java/io/zhile/research/ja/netfilter/plugins/dns/InetAddressTransformer.java delete mode 100644 src/main/java/io/zhile/research/ja/netfilter/plugins/url/HttpClientTransformer.java delete mode 100644 src/main/java/io/zhile/research/ja/netfilter/plugins/url/URLFilter.java delete mode 100644 src/main/java/io/zhile/research/ja/netfilter/plugins/url/URLFilterPlugin.java rename src/main/resources/{442fcf28466515a81d5434931496ffa64611cc8e.txt => b7e909d6ba41ae03fb85af5b8ba702709f5798cf.txt} (100%) diff --git a/README.md b/README.md index 9b1346e..a9ff042 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# ja-netfilter v1.1.6 +# ja-netfilter v1.2.0 ### A javaagent lib for network filter diff --git a/pom.xml b/pom.xml index 2f76aa0..bb0505d 100644 --- a/pom.xml +++ b/pom.xml @@ -4,9 +4,9 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - io.zhile.research + com.ja-netfilter ja-netfilter - 1.1.6 + 1.2.0 UTF-8 @@ -48,8 +48,8 @@ neo - io.zhile.research.ja.netfilter.Launcher - io.zhile.research.ja.netfilter.Launcher + com.janetfilter.core.Launcher + com.janetfilter.core.Launcher true true diff --git a/src/main/java/io/zhile/research/ja/netfilter/Dispatcher.java b/src/main/java/com/janetfilter/core/Dispatcher.java similarity index 76% rename from src/main/java/io/zhile/research/ja/netfilter/Dispatcher.java rename to src/main/java/com/janetfilter/core/Dispatcher.java index 33a4846..848cf56 100644 --- a/src/main/java/io/zhile/research/ja/netfilter/Dispatcher.java +++ b/src/main/java/com/janetfilter/core/Dispatcher.java @@ -1,28 +1,21 @@ -package io.zhile.research.ja.netfilter; +package com.janetfilter.core; -import io.zhile.research.ja.netfilter.commons.DebugInfo; -import io.zhile.research.ja.netfilter.plugin.MyTransformer; +import com.janetfilter.core.commons.DebugInfo; +import com.janetfilter.core.plugin.MyTransformer; import java.lang.instrument.ClassFileTransformer; import java.lang.instrument.IllegalClassFormatException; import java.security.ProtectionDomain; import java.util.*; -public class Dispatcher implements ClassFileTransformer { - private static Dispatcher INSTANCE; - +public final class Dispatcher implements ClassFileTransformer { + private final Set classSet = new TreeSet<>(); private final Map> transformerMap = new HashMap<>(); - public static synchronized Dispatcher getInstance() { - if (null == INSTANCE) { - INSTANCE = new Dispatcher(); - } - - return INSTANCE; - } - public void addTransformer(MyTransformer transformer) { - List transformers = transformerMap.computeIfAbsent(transformer.getHookClassName(), k -> new ArrayList<>()); + String className = transformer.getHookClassName(); + classSet.add(className.replace('/', '.')); + List transformers = transformerMap.computeIfAbsent(className, k -> new ArrayList<>()); transformers.add(transformer); } @@ -46,7 +39,7 @@ public class Dispatcher implements ClassFileTransformer { } public Set getHookClassNames() { - return transformerMap.keySet(); + return classSet; } public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws IllegalClassFormatException { diff --git a/src/main/java/com/janetfilter/core/Environment.java b/src/main/java/com/janetfilter/core/Environment.java new file mode 100644 index 0000000..0fbe28f --- /dev/null +++ b/src/main/java/com/janetfilter/core/Environment.java @@ -0,0 +1,27 @@ +package com.janetfilter.core; + +import java.io.File; + +public final class Environment { + private final File baseDir; + private final File agentFile; + private final File pluginsDir; + + public Environment(File agentFile) { + this.agentFile = agentFile; + baseDir = agentFile.getParentFile(); + pluginsDir = new File(baseDir, "plugins"); + } + + public File getBaseDir() { + return baseDir; + } + + public File getAgentFile() { + return agentFile; + } + + public File getPluginsDir() { + return pluginsDir; + } +} diff --git a/src/main/java/io/zhile/research/ja/netfilter/Initializer.java b/src/main/java/com/janetfilter/core/Initializer.java similarity index 52% rename from src/main/java/io/zhile/research/ja/netfilter/Initializer.java rename to src/main/java/com/janetfilter/core/Initializer.java index a9637c8..ceadb51 100644 --- a/src/main/java/io/zhile/research/ja/netfilter/Initializer.java +++ b/src/main/java/com/janetfilter/core/Initializer.java @@ -1,18 +1,18 @@ -package io.zhile.research.ja.netfilter; +package com.janetfilter.core; -import io.zhile.research.ja.netfilter.commons.ConfigDetector; -import io.zhile.research.ja.netfilter.commons.ConfigParser; -import io.zhile.research.ja.netfilter.commons.DebugInfo; -import io.zhile.research.ja.netfilter.models.FilterConfig; -import io.zhile.research.ja.netfilter.plugin.PluginManager; +import com.janetfilter.core.commons.ConfigDetector; +import com.janetfilter.core.commons.ConfigParser; +import com.janetfilter.core.commons.DebugInfo; +import com.janetfilter.core.models.FilterConfig; +import com.janetfilter.core.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) { - File configFile = ConfigDetector.detect(currentDirectory, args); + public static void init(String args, Instrumentation inst, Environment environment) { + File configFile = ConfigDetector.detect(environment.getBaseDir(), args); if (null == configFile) { DebugInfo.output("Could not find any configuration files."); } else { @@ -25,14 +25,15 @@ public class Initializer { DebugInfo.output(e.getMessage()); } - PluginManager.getInstance().loadPlugins(inst, currentDirectory); + Dispatcher dispatcher = new Dispatcher(); + new PluginManager(dispatcher, environment).loadPlugins(inst); - inst.addTransformer(Dispatcher.getInstance(), true); + inst.addTransformer(dispatcher, true); - Set classSet = Dispatcher.getInstance().getHookClassNames(); + Set classSet = dispatcher.getHookClassNames(); for (Class c : inst.getAllLoadedClasses()) { String name = c.getName(); - if (!classSet.contains(name.replace('.', '/'))) { + if (!classSet.contains(name)) { continue; } diff --git a/src/main/java/io/zhile/research/ja/netfilter/Launcher.java b/src/main/java/com/janetfilter/core/Launcher.java similarity index 79% rename from src/main/java/io/zhile/research/ja/netfilter/Launcher.java rename to src/main/java/com/janetfilter/core/Launcher.java index f2f1edd..6eabc0a 100644 --- a/src/main/java/io/zhile/research/ja/netfilter/Launcher.java +++ b/src/main/java/com/janetfilter/core/Launcher.java @@ -1,6 +1,6 @@ -package io.zhile.research.ja.netfilter; +package com.janetfilter.core; -import io.zhile.research.ja.netfilter.commons.DebugInfo; +import com.janetfilter.core.commons.DebugInfo; import java.io.File; import java.lang.instrument.Instrumentation; @@ -9,7 +9,7 @@ import java.net.URL; import java.util.jar.JarFile; public class Launcher { - private static final String VERSION = "v1.1.6"; + private static final String VERSION = "v1.2.0"; public static void main(String[] args) { printUsage(); @@ -26,16 +26,15 @@ public class Launcher { return; } - File currentFile = new File(jarURI.getPath()); - File currentDirectory = currentFile.getParentFile(); + File agentFile = new File(jarURI.getPath()); try { - inst.appendToBootstrapClassLoaderSearch(new JarFile(currentFile)); + inst.appendToBootstrapClassLoaderSearch(new JarFile(agentFile)); } catch (Throwable e) { DebugInfo.output("ERROR: Can not access ja-netfilter jar file."); return; } - Initializer.init(args, inst, currentDirectory); // for some custom UrlLoaders + Initializer.init(args, inst, new Environment(agentFile)); // for some custom UrlLoaders } private static void printUsage() { @@ -45,7 +44,7 @@ public class Launcher { "\n\n" + " A javaagent lib for network filter :)\n" + "\n" + - " https://github.com/pengzhile/ja-netfilter\n" + + " https://github.com/ja-netfilter/ja-netfilter\n" + "\n" + " ============================================================================ \n\n"; @@ -59,7 +58,7 @@ public class Launcher { return url.toURI(); } - String resourcePath = "/442fcf28466515a81d5434931496ffa64611cc8e.txt"; + String resourcePath = "/b7e909d6ba41ae03fb85af5b8ba702709f5798cf.txt"; url = Launcher.class.getResource(resourcePath); if (null == url) { throw new Exception("Can not locate resource file."); diff --git a/src/main/java/io/zhile/research/ja/netfilter/commons/ConfigDetector.java b/src/main/java/com/janetfilter/core/commons/ConfigDetector.java similarity index 96% rename from src/main/java/io/zhile/research/ja/netfilter/commons/ConfigDetector.java rename to src/main/java/com/janetfilter/core/commons/ConfigDetector.java index ca03d1c..2c1c7be 100644 --- a/src/main/java/io/zhile/research/ja/netfilter/commons/ConfigDetector.java +++ b/src/main/java/com/janetfilter/core/commons/ConfigDetector.java @@ -1,6 +1,6 @@ -package io.zhile.research.ja.netfilter.commons; +package com.janetfilter.core.commons; -import io.zhile.research.ja.netfilter.utils.StringUtils; +import com.janetfilter.core.utils.StringUtils; import java.io.File; diff --git a/src/main/java/io/zhile/research/ja/netfilter/commons/ConfigParser.java b/src/main/java/com/janetfilter/core/commons/ConfigParser.java similarity index 95% rename from src/main/java/io/zhile/research/ja/netfilter/commons/ConfigParser.java rename to src/main/java/com/janetfilter/core/commons/ConfigParser.java index 13a1b32..b913df6 100644 --- a/src/main/java/io/zhile/research/ja/netfilter/commons/ConfigParser.java +++ b/src/main/java/com/janetfilter/core/commons/ConfigParser.java @@ -1,7 +1,7 @@ -package io.zhile.research.ja.netfilter.commons; +package com.janetfilter.core.commons; -import io.zhile.research.ja.netfilter.models.FilterRule; -import io.zhile.research.ja.netfilter.utils.StringUtils; +import com.janetfilter.core.models.FilterRule; +import com.janetfilter.core.utils.StringUtils; import java.io.BufferedReader; import java.io.File; diff --git a/src/main/java/io/zhile/research/ja/netfilter/commons/DebugInfo.java b/src/main/java/com/janetfilter/core/commons/DebugInfo.java similarity index 84% rename from src/main/java/io/zhile/research/ja/netfilter/commons/DebugInfo.java rename to src/main/java/com/janetfilter/core/commons/DebugInfo.java index 803a89c..f950177 100644 --- a/src/main/java/io/zhile/research/ja/netfilter/commons/DebugInfo.java +++ b/src/main/java/com/janetfilter/core/commons/DebugInfo.java @@ -1,6 +1,6 @@ -package io.zhile.research.ja.netfilter.commons; +package com.janetfilter.core.commons; -import io.zhile.research.ja.netfilter.utils.DateUtils; +import com.janetfilter.core.utils.DateUtils; public class DebugInfo { private static final boolean DEBUG = "1".equals(System.getenv("JANF_DEBUG")) || "1".equals(System.getProperty("janf.debug")); diff --git a/src/main/java/io/zhile/research/ja/netfilter/enums/RuleType.java b/src/main/java/com/janetfilter/core/enums/RuleType.java similarity index 84% rename from src/main/java/io/zhile/research/ja/netfilter/enums/RuleType.java rename to src/main/java/com/janetfilter/core/enums/RuleType.java index a46b54b..be9ddad 100644 --- a/src/main/java/io/zhile/research/ja/netfilter/enums/RuleType.java +++ b/src/main/java/com/janetfilter/core/enums/RuleType.java @@ -1,6 +1,6 @@ -package io.zhile.research.ja.netfilter.enums; +package com.janetfilter.core.enums; -import io.zhile.research.ja.netfilter.rulers.*; +import com.janetfilter.core.rulers.*; public enum RuleType { PREFIX(new PrefixRuler()), diff --git a/src/main/java/io/zhile/research/ja/netfilter/models/FilterConfig.java b/src/main/java/com/janetfilter/core/models/FilterConfig.java similarity index 95% rename from src/main/java/io/zhile/research/ja/netfilter/models/FilterConfig.java rename to src/main/java/com/janetfilter/core/models/FilterConfig.java index 6ec4507..85a3ede 100644 --- a/src/main/java/io/zhile/research/ja/netfilter/models/FilterConfig.java +++ b/src/main/java/com/janetfilter/core/models/FilterConfig.java @@ -1,4 +1,4 @@ -package io.zhile.research.ja.netfilter.models; +package com.janetfilter.core.models; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/io/zhile/research/ja/netfilter/models/FilterRule.java b/src/main/java/com/janetfilter/core/models/FilterRule.java similarity index 92% rename from src/main/java/io/zhile/research/ja/netfilter/models/FilterRule.java rename to src/main/java/com/janetfilter/core/models/FilterRule.java index 8f099c9..bbacb48 100644 --- a/src/main/java/io/zhile/research/ja/netfilter/models/FilterRule.java +++ b/src/main/java/com/janetfilter/core/models/FilterRule.java @@ -1,6 +1,6 @@ -package io.zhile.research.ja.netfilter.models; +package com.janetfilter.core.models; -import io.zhile.research.ja.netfilter.enums.RuleType; +import com.janetfilter.core.enums.RuleType; import java.util.HashMap; import java.util.Map; diff --git a/src/main/java/io/zhile/research/ja/netfilter/plugin/MyTransformer.java b/src/main/java/com/janetfilter/core/plugin/MyTransformer.java similarity index 80% rename from src/main/java/io/zhile/research/ja/netfilter/plugin/MyTransformer.java rename to src/main/java/com/janetfilter/core/plugin/MyTransformer.java index 6053739..758744b 100644 --- a/src/main/java/io/zhile/research/ja/netfilter/plugin/MyTransformer.java +++ b/src/main/java/com/janetfilter/core/plugin/MyTransformer.java @@ -1,4 +1,4 @@ -package io.zhile.research.ja.netfilter.plugin; +package com.janetfilter.core.plugin; public interface MyTransformer { String getHookClassName(); diff --git a/src/main/java/io/zhile/research/ja/netfilter/plugin/PluginClassLoader.java b/src/main/java/com/janetfilter/core/plugin/PluginClassLoader.java similarity index 92% rename from src/main/java/io/zhile/research/ja/netfilter/plugin/PluginClassLoader.java rename to src/main/java/com/janetfilter/core/plugin/PluginClassLoader.java index 6e0e967..a1f28ab 100644 --- a/src/main/java/io/zhile/research/ja/netfilter/plugin/PluginClassLoader.java +++ b/src/main/java/com/janetfilter/core/plugin/PluginClassLoader.java @@ -1,4 +1,4 @@ -package io.zhile.research.ja.netfilter.plugin; +package com.janetfilter.core.plugin; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -6,7 +6,7 @@ import java.io.InputStream; import java.util.jar.JarFile; import java.util.zip.ZipEntry; -public class PluginClassLoader extends ClassLoader { +public final class PluginClassLoader extends ClassLoader { private final JarFile jarFile; public PluginClassLoader(JarFile jarFile) { diff --git a/src/main/java/io/zhile/research/ja/netfilter/plugin/PluginEntry.java b/src/main/java/com/janetfilter/core/plugin/PluginEntry.java similarity index 62% rename from src/main/java/io/zhile/research/ja/netfilter/plugin/PluginEntry.java rename to src/main/java/com/janetfilter/core/plugin/PluginEntry.java index b5bfd62..5f838e2 100644 --- a/src/main/java/io/zhile/research/ja/netfilter/plugin/PluginEntry.java +++ b/src/main/java/com/janetfilter/core/plugin/PluginEntry.java @@ -1,11 +1,12 @@ -package io.zhile.research.ja.netfilter.plugin; +package com.janetfilter.core.plugin; -import io.zhile.research.ja.netfilter.models.FilterRule; +import com.janetfilter.core.Environment; +import com.janetfilter.core.models.FilterRule; import java.util.List; public interface PluginEntry { - default void init(List filterRules) { + default void init(Environment environment, List filterRules) { // get plugin config } diff --git a/src/main/java/io/zhile/research/ja/netfilter/plugin/PluginManager.java b/src/main/java/com/janetfilter/core/plugin/PluginManager.java similarity index 66% rename from src/main/java/io/zhile/research/ja/netfilter/plugin/PluginManager.java rename to src/main/java/com/janetfilter/core/plugin/PluginManager.java index dcbe405..2fd8dc5 100644 --- a/src/main/java/io/zhile/research/ja/netfilter/plugin/PluginManager.java +++ b/src/main/java/com/janetfilter/core/plugin/PluginManager.java @@ -1,11 +1,10 @@ -package io.zhile.research.ja.netfilter.plugin; +package com.janetfilter.core.plugin; -import io.zhile.research.ja.netfilter.Dispatcher; -import io.zhile.research.ja.netfilter.commons.DebugInfo; -import io.zhile.research.ja.netfilter.models.FilterConfig; -import io.zhile.research.ja.netfilter.plugins.dns.DNSFilterPlugin; -import io.zhile.research.ja.netfilter.plugins.url.URLFilterPlugin; -import io.zhile.research.ja.netfilter.utils.StringUtils; +import com.janetfilter.core.Dispatcher; +import com.janetfilter.core.Environment; +import com.janetfilter.core.commons.DebugInfo; +import com.janetfilter.core.models.FilterConfig; +import com.janetfilter.core.utils.StringUtils; import java.io.File; import java.lang.instrument.Instrumentation; @@ -15,22 +14,19 @@ import java.util.List; import java.util.jar.JarFile; import java.util.jar.Manifest; -public class PluginManager { - private static final String PLUGINS_DIR = "plugins"; +public final class PluginManager { private static final String ENTRY_NAME = "JANF-Plugin-Entry"; - private static PluginManager INSTANCE; + private final Dispatcher dispatcher; + private final Environment environment; - public static synchronized PluginManager getInstance() { - if (null == INSTANCE) { - INSTANCE = new PluginManager(); - } - - return INSTANCE; + public PluginManager(Dispatcher dispatcher, Environment environment) { + this.dispatcher = dispatcher; + this.environment = environment; } - public void loadPlugins(Instrumentation inst, File currentDirectory) { - for (Class klass : getAllPluginClasses(inst, currentDirectory)) { + public void loadPlugins(Instrumentation inst) { + for (Class klass : getAllPluginClasses(inst)) { try { addPluginEntry(klass); } catch (Throwable e) { @@ -39,13 +35,11 @@ public class PluginManager { } } - private List> getAllPluginClasses(Instrumentation inst, File currentDirectory) { + private List> getAllPluginClasses(Instrumentation inst) { List> classes = new ArrayList<>(); - classes.add(DNSFilterPlugin.class); - classes.add(URLFilterPlugin.class); do { - File pluginsDirectory = new File(currentDirectory, PLUGINS_DIR); + File pluginsDirectory = environment.getPluginsDir(); if (!pluginsDirectory.exists() || !pluginsDirectory.isDirectory()) { break; } @@ -84,8 +78,8 @@ public class PluginManager { private void addPluginEntry(Class entryClass) throws Exception { PluginEntry pluginEntry = entryClass.newInstance(); - pluginEntry.init(FilterConfig.getBySection(pluginEntry.getName())); - Dispatcher.getInstance().addTransformers(pluginEntry.getTransformers()); + pluginEntry.init(environment, FilterConfig.getBySection(pluginEntry.getName())); + dispatcher.addTransformers(pluginEntry.getTransformers()); DebugInfo.output("Plugin loaded: {name=" + pluginEntry.getName() + ", version=" + pluginEntry.getVersion() + ", author=" + pluginEntry.getAuthor() + "}"); } diff --git a/src/main/java/io/zhile/research/ja/netfilter/rulers/EqualICRuler.java b/src/main/java/com/janetfilter/core/rulers/EqualICRuler.java similarity index 78% rename from src/main/java/io/zhile/research/ja/netfilter/rulers/EqualICRuler.java rename to src/main/java/com/janetfilter/core/rulers/EqualICRuler.java index ef12785..02961d6 100644 --- a/src/main/java/io/zhile/research/ja/netfilter/rulers/EqualICRuler.java +++ b/src/main/java/com/janetfilter/core/rulers/EqualICRuler.java @@ -1,4 +1,4 @@ -package io.zhile.research.ja.netfilter.rulers; +package com.janetfilter.core.rulers; public class EqualICRuler implements Ruler { @Override diff --git a/src/main/java/io/zhile/research/ja/netfilter/rulers/EqualRuler.java b/src/main/java/com/janetfilter/core/rulers/EqualRuler.java similarity index 77% rename from src/main/java/io/zhile/research/ja/netfilter/rulers/EqualRuler.java rename to src/main/java/com/janetfilter/core/rulers/EqualRuler.java index 24c9660..ba6aeb8 100644 --- a/src/main/java/io/zhile/research/ja/netfilter/rulers/EqualRuler.java +++ b/src/main/java/com/janetfilter/core/rulers/EqualRuler.java @@ -1,4 +1,4 @@ -package io.zhile.research.ja.netfilter.rulers; +package com.janetfilter.core.rulers; public class EqualRuler implements Ruler { @Override diff --git a/src/main/java/io/zhile/research/ja/netfilter/rulers/KeywordICRuler.java b/src/main/java/com/janetfilter/core/rulers/KeywordICRuler.java similarity index 80% rename from src/main/java/io/zhile/research/ja/netfilter/rulers/KeywordICRuler.java rename to src/main/java/com/janetfilter/core/rulers/KeywordICRuler.java index 1529b4c..1b9c978 100644 --- a/src/main/java/io/zhile/research/ja/netfilter/rulers/KeywordICRuler.java +++ b/src/main/java/com/janetfilter/core/rulers/KeywordICRuler.java @@ -1,4 +1,4 @@ -package io.zhile.research.ja.netfilter.rulers; +package com.janetfilter.core.rulers; public class KeywordICRuler implements Ruler { @Override diff --git a/src/main/java/io/zhile/research/ja/netfilter/rulers/KeywordRuler.java b/src/main/java/com/janetfilter/core/rulers/KeywordRuler.java similarity index 77% rename from src/main/java/io/zhile/research/ja/netfilter/rulers/KeywordRuler.java rename to src/main/java/com/janetfilter/core/rulers/KeywordRuler.java index 2268fad..0f5b5a1 100644 --- a/src/main/java/io/zhile/research/ja/netfilter/rulers/KeywordRuler.java +++ b/src/main/java/com/janetfilter/core/rulers/KeywordRuler.java @@ -1,4 +1,4 @@ -package io.zhile.research.ja.netfilter.rulers; +package com.janetfilter.core.rulers; public class KeywordRuler implements Ruler { @Override diff --git a/src/main/java/io/zhile/research/ja/netfilter/rulers/PrefixICRuler.java b/src/main/java/com/janetfilter/core/rulers/PrefixICRuler.java similarity index 80% rename from src/main/java/io/zhile/research/ja/netfilter/rulers/PrefixICRuler.java rename to src/main/java/com/janetfilter/core/rulers/PrefixICRuler.java index fb28235..8b0b117 100644 --- a/src/main/java/io/zhile/research/ja/netfilter/rulers/PrefixICRuler.java +++ b/src/main/java/com/janetfilter/core/rulers/PrefixICRuler.java @@ -1,4 +1,4 @@ -package io.zhile.research.ja.netfilter.rulers; +package com.janetfilter.core.rulers; public class PrefixICRuler implements Ruler { @Override diff --git a/src/main/java/io/zhile/research/ja/netfilter/rulers/PrefixRuler.java b/src/main/java/com/janetfilter/core/rulers/PrefixRuler.java similarity index 77% rename from src/main/java/io/zhile/research/ja/netfilter/rulers/PrefixRuler.java rename to src/main/java/com/janetfilter/core/rulers/PrefixRuler.java index 5bd5a01..9bdac1b 100644 --- a/src/main/java/io/zhile/research/ja/netfilter/rulers/PrefixRuler.java +++ b/src/main/java/com/janetfilter/core/rulers/PrefixRuler.java @@ -1,4 +1,4 @@ -package io.zhile.research.ja.netfilter.rulers; +package com.janetfilter.core.rulers; public class PrefixRuler implements Ruler { @Override diff --git a/src/main/java/io/zhile/research/ja/netfilter/rulers/RegExpRuler.java b/src/main/java/com/janetfilter/core/rulers/RegExpRuler.java similarity index 81% rename from src/main/java/io/zhile/research/ja/netfilter/rulers/RegExpRuler.java rename to src/main/java/com/janetfilter/core/rulers/RegExpRuler.java index 097d104..c8c4931 100644 --- a/src/main/java/io/zhile/research/ja/netfilter/rulers/RegExpRuler.java +++ b/src/main/java/com/janetfilter/core/rulers/RegExpRuler.java @@ -1,4 +1,4 @@ -package io.zhile.research.ja.netfilter.rulers; +package com.janetfilter.core.rulers; import java.util.regex.Pattern; diff --git a/src/main/java/io/zhile/research/ja/netfilter/rulers/Ruler.java b/src/main/java/com/janetfilter/core/rulers/Ruler.java similarity index 70% rename from src/main/java/io/zhile/research/ja/netfilter/rulers/Ruler.java rename to src/main/java/com/janetfilter/core/rulers/Ruler.java index ad05d2c..16eca07 100644 --- a/src/main/java/io/zhile/research/ja/netfilter/rulers/Ruler.java +++ b/src/main/java/com/janetfilter/core/rulers/Ruler.java @@ -1,4 +1,4 @@ -package io.zhile.research.ja.netfilter.rulers; +package com.janetfilter.core.rulers; public interface Ruler { default boolean test(String rule, String content) { diff --git a/src/main/java/io/zhile/research/ja/netfilter/rulers/SuffixICRuler.java b/src/main/java/com/janetfilter/core/rulers/SuffixICRuler.java similarity index 80% rename from src/main/java/io/zhile/research/ja/netfilter/rulers/SuffixICRuler.java rename to src/main/java/com/janetfilter/core/rulers/SuffixICRuler.java index ce3deb0..4234a09 100644 --- a/src/main/java/io/zhile/research/ja/netfilter/rulers/SuffixICRuler.java +++ b/src/main/java/com/janetfilter/core/rulers/SuffixICRuler.java @@ -1,4 +1,4 @@ -package io.zhile.research.ja.netfilter.rulers; +package com.janetfilter.core.rulers; public class SuffixICRuler implements Ruler { @Override diff --git a/src/main/java/io/zhile/research/ja/netfilter/rulers/SuffixRuler.java b/src/main/java/com/janetfilter/core/rulers/SuffixRuler.java similarity index 77% rename from src/main/java/io/zhile/research/ja/netfilter/rulers/SuffixRuler.java rename to src/main/java/com/janetfilter/core/rulers/SuffixRuler.java index 7508a9e..f9d0808 100644 --- a/src/main/java/io/zhile/research/ja/netfilter/rulers/SuffixRuler.java +++ b/src/main/java/com/janetfilter/core/rulers/SuffixRuler.java @@ -1,4 +1,4 @@ -package io.zhile.research.ja.netfilter.rulers; +package com.janetfilter.core.rulers; public class SuffixRuler implements Ruler { @Override diff --git a/src/main/java/io/zhile/research/ja/netfilter/utils/DateUtils.java b/src/main/java/com/janetfilter/core/utils/DateUtils.java similarity index 96% rename from src/main/java/io/zhile/research/ja/netfilter/utils/DateUtils.java rename to src/main/java/com/janetfilter/core/utils/DateUtils.java index 38b71e2..0ce40a0 100644 --- a/src/main/java/io/zhile/research/ja/netfilter/utils/DateUtils.java +++ b/src/main/java/com/janetfilter/core/utils/DateUtils.java @@ -1,4 +1,4 @@ -package io.zhile.research.ja.netfilter.utils; +package com.janetfilter.core.utils; import java.text.DateFormat; import java.text.ParseException; diff --git a/src/main/java/io/zhile/research/ja/netfilter/utils/StringUtils.java b/src/main/java/com/janetfilter/core/utils/StringUtils.java similarity index 73% rename from src/main/java/io/zhile/research/ja/netfilter/utils/StringUtils.java rename to src/main/java/com/janetfilter/core/utils/StringUtils.java index c78a8e3..09b72f3 100644 --- a/src/main/java/io/zhile/research/ja/netfilter/utils/StringUtils.java +++ b/src/main/java/com/janetfilter/core/utils/StringUtils.java @@ -1,4 +1,4 @@ -package io.zhile.research.ja.netfilter.utils; +package com.janetfilter.core.utils; public class StringUtils { public static boolean isEmpty(String str) { diff --git a/src/main/java/io/zhile/research/ja/netfilter/plugins/dns/DNSFilter.java b/src/main/java/io/zhile/research/ja/netfilter/plugins/dns/DNSFilter.java deleted file mode 100644 index 8596524..0000000 --- a/src/main/java/io/zhile/research/ja/netfilter/plugins/dns/DNSFilter.java +++ /dev/null @@ -1,50 +0,0 @@ -package io.zhile.research.ja.netfilter.plugins.dns; - -import io.zhile.research.ja.netfilter.commons.DebugInfo; -import io.zhile.research.ja.netfilter.models.FilterRule; - -import java.io.IOException; -import java.net.InetAddress; -import java.util.List; - -public class DNSFilter { - private static List ruleList; - - public static void setRules(List rules) { - ruleList = rules; - } - - public static String testQuery(String host) throws IOException { - if (null == host || null == ruleList) { - return null; - } - - for (FilterRule rule : ruleList) { - if (!rule.test(host)) { - continue; - } - - DebugInfo.output("Reject dns query: " + host + ", rule: " + rule); - throw new java.net.UnknownHostException(); - } - - return host; - } - - public static Object testReachable(InetAddress n) throws IOException { - if (null == n || null == ruleList) { - return null; - } - - for (FilterRule rule : ruleList) { - if (!rule.test(n.getHostName())) { - continue; - } - - DebugInfo.output("Reject dns reachable test: " + n.getHostName() + ", rule: " + rule); - return false; - } - - return null; - } -} diff --git a/src/main/java/io/zhile/research/ja/netfilter/plugins/dns/DNSFilterPlugin.java b/src/main/java/io/zhile/research/ja/netfilter/plugins/dns/DNSFilterPlugin.java deleted file mode 100644 index 6cf8113..0000000 --- a/src/main/java/io/zhile/research/ja/netfilter/plugins/dns/DNSFilterPlugin.java +++ /dev/null @@ -1,42 +0,0 @@ -package io.zhile.research.ja.netfilter.plugins.dns; - -import io.zhile.research.ja.netfilter.models.FilterRule; -import io.zhile.research.ja.netfilter.plugin.MyTransformer; -import io.zhile.research.ja.netfilter.plugin.PluginEntry; - -import java.util.ArrayList; -import java.util.List; - -public class DNSFilterPlugin implements PluginEntry { - private final List transformers = new ArrayList<>(); - - @Override - public void init(List filterRules) { - transformers.add(new InetAddressTransformer(filterRules)); - } - - @Override - public String getName() { - return "DNS"; - } - - @Override - public String getAuthor() { - return "neo"; - } - - @Override - public String getVersion() { - return "v1.0.0"; - } - - @Override - public String getDescription() { - return "ja-netfilter core: dns plugin"; - } - - @Override - public List getTransformers() { - return transformers; - } -} diff --git a/src/main/java/io/zhile/research/ja/netfilter/plugins/dns/InetAddressTransformer.java b/src/main/java/io/zhile/research/ja/netfilter/plugins/dns/InetAddressTransformer.java deleted file mode 100644 index 920dfe7..0000000 --- a/src/main/java/io/zhile/research/ja/netfilter/plugins/dns/InetAddressTransformer.java +++ /dev/null @@ -1,67 +0,0 @@ -package io.zhile.research.ja.netfilter.plugins.dns; - -import io.zhile.research.ja.netfilter.models.FilterRule; -import io.zhile.research.ja.netfilter.plugin.MyTransformer; -import jdk.internal.org.objectweb.asm.ClassReader; -import jdk.internal.org.objectweb.asm.ClassWriter; -import jdk.internal.org.objectweb.asm.tree.*; - -import java.util.List; - -import static jdk.internal.org.objectweb.asm.Opcodes.*; - -public class InetAddressTransformer implements MyTransformer { - private final List rules; - - public InetAddressTransformer(List rules) { - this.rules = rules; - } - - @Override - public String getHookClassName() { - return "java/net/InetAddress"; - } - - @Override - public byte[] transform(String className, byte[] classBytes, int order) throws Exception { - DNSFilter.setRules(rules); - - ClassReader reader = new ClassReader(classBytes); - ClassNode node = new ClassNode(ASM5); - reader.accept(node, 0); - - for (MethodNode m : node.methods) { - if ("getAllByName".equals(m.name) && "(Ljava/lang/String;Ljava/net/InetAddress;)[Ljava/net/InetAddress;".equals(m.desc)) { - InsnList list = new InsnList(); - list.add(new VarInsnNode(ALOAD, 0)); - list.add(new MethodInsnNode(INVOKESTATIC, "io/zhile/research/ja/netfilter/plugins/dns/DNSFilter", "testQuery", "(Ljava/lang/String;)Ljava/lang/String;", false)); - list.add(new InsnNode(POP)); - - m.instructions.insert(list); - continue; - } - - if ("isReachable".equals(m.name) && "(Ljava/net/NetworkInterface;II)Z".equals(m.desc)) { - InsnList list = new InsnList(); - list.add(new VarInsnNode(ALOAD, 0)); - list.add(new MethodInsnNode(INVOKESTATIC, "io/zhile/research/ja/netfilter/plugins/dns/DNSFilter", "testReachable", "(Ljava/net/InetAddress;)Ljava/lang/Object;", false)); - list.add(new VarInsnNode(ASTORE, 4)); - list.add(new InsnNode(ACONST_NULL)); - list.add(new VarInsnNode(ALOAD, 4)); - - LabelNode label1 = new LabelNode(); - list.add(new JumpInsnNode(IF_ACMPEQ, label1)); - list.add(new InsnNode(ICONST_0)); - list.add(new InsnNode(IRETURN)); - list.add(label1); - - m.instructions.insert(list); - } - } - - ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); - node.accept(writer); - - return writer.toByteArray(); - } -} diff --git a/src/main/java/io/zhile/research/ja/netfilter/plugins/url/HttpClientTransformer.java b/src/main/java/io/zhile/research/ja/netfilter/plugins/url/HttpClientTransformer.java deleted file mode 100644 index 4be940d..0000000 --- a/src/main/java/io/zhile/research/ja/netfilter/plugins/url/HttpClientTransformer.java +++ /dev/null @@ -1,50 +0,0 @@ -package io.zhile.research.ja.netfilter.plugins.url; - -import io.zhile.research.ja.netfilter.models.FilterRule; -import io.zhile.research.ja.netfilter.plugin.MyTransformer; -import jdk.internal.org.objectweb.asm.ClassReader; -import jdk.internal.org.objectweb.asm.ClassWriter; -import jdk.internal.org.objectweb.asm.tree.*; - -import java.util.List; - -import static jdk.internal.org.objectweb.asm.Opcodes.*; - -public class HttpClientTransformer implements MyTransformer { - private final List rules; - - public HttpClientTransformer(List rules) { - this.rules = rules; - } - - @Override - public String getHookClassName() { - return "sun/net/www/http/HttpClient"; - } - - @Override - public byte[] transform(String className, byte[] classBytes, int order) throws Exception { - URLFilter.setRules(rules); - - ClassReader reader = new ClassReader(classBytes); - ClassNode node = new ClassNode(ASM5); - reader.accept(node, 0); - - for (MethodNode mn : node.methods) { - if ("openServer".equals(mn.name) && "()V".equals(mn.desc)) { - InsnList list = new InsnList(); - list.add(new VarInsnNode(ALOAD, 0)); - list.add(new FieldInsnNode(GETFIELD, "sun/net/www/http/HttpClient", "url", "Ljava/net/URL;")); - list.add(new MethodInsnNode(INVOKESTATIC, "io/zhile/research/ja/netfilter/plugins/url/URLFilter", "testURL", "(Ljava/net/URL;)Ljava/net/URL;", false)); - list.add(new InsnNode(POP)); - - mn.instructions.insert(list); - } - } - - ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); - node.accept(writer); - - return writer.toByteArray(); - } -} diff --git a/src/main/java/io/zhile/research/ja/netfilter/plugins/url/URLFilter.java b/src/main/java/io/zhile/research/ja/netfilter/plugins/url/URLFilter.java deleted file mode 100644 index 37d53cc..0000000 --- a/src/main/java/io/zhile/research/ja/netfilter/plugins/url/URLFilter.java +++ /dev/null @@ -1,34 +0,0 @@ -package io.zhile.research.ja.netfilter.plugins.url; - -import io.zhile.research.ja.netfilter.commons.DebugInfo; -import io.zhile.research.ja.netfilter.models.FilterRule; - -import java.io.IOException; -import java.net.SocketTimeoutException; -import java.net.URL; -import java.util.List; - -public class URLFilter { - private static List ruleList; - - public static void setRules(List rules) { - ruleList = rules; - } - - public static URL testURL(URL url) throws IOException { - if (null == url || null == ruleList) { - return null; - } - - for (FilterRule rule : ruleList) { - if (!rule.test(url.toString())) { - continue; - } - - DebugInfo.output("Reject url: " + url + ", rule: " + rule); - throw new SocketTimeoutException("connect timed out"); - } - - return url; - } -} diff --git a/src/main/java/io/zhile/research/ja/netfilter/plugins/url/URLFilterPlugin.java b/src/main/java/io/zhile/research/ja/netfilter/plugins/url/URLFilterPlugin.java deleted file mode 100644 index faa143f..0000000 --- a/src/main/java/io/zhile/research/ja/netfilter/plugins/url/URLFilterPlugin.java +++ /dev/null @@ -1,42 +0,0 @@ -package io.zhile.research.ja.netfilter.plugins.url; - -import io.zhile.research.ja.netfilter.models.FilterRule; -import io.zhile.research.ja.netfilter.plugin.MyTransformer; -import io.zhile.research.ja.netfilter.plugin.PluginEntry; - -import java.util.ArrayList; -import java.util.List; - -public class URLFilterPlugin implements PluginEntry { - private final List transformers = new ArrayList<>(); - - @Override - public void init(List filterRules) { - transformers.add(new HttpClientTransformer(filterRules)); - } - - @Override - public String getName() { - return "URL"; - } - - @Override - public String getAuthor() { - return "neo"; - } - - @Override - public String getVersion() { - return "v1.0.0"; - } - - @Override - public String getDescription() { - return "ja-netfilter core: url plugin"; - } - - @Override - public List getTransformers() { - return transformers; - } -} diff --git a/src/main/resources/442fcf28466515a81d5434931496ffa64611cc8e.txt b/src/main/resources/b7e909d6ba41ae03fb85af5b8ba702709f5798cf.txt similarity index 100% rename from src/main/resources/442fcf28466515a81d5434931496ffa64611cc8e.txt rename to src/main/resources/b7e909d6ba41ae03fb85af5b8ba702709f5798cf.txt