Signed-off-by: pengzhile <pengzhile@gmail.com>pull/1/head
parent
7057cd3394
commit
e622c8fed4
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
@ -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;
|
@ -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"));
|
@ -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()),
|
@ -1,4 +1,4 @@
|
||||
package io.zhile.research.ja.netfilter.models;
|
||||
package com.janetfilter.core.models;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
@ -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;
|
@ -1,4 +1,4 @@
|
||||
package io.zhile.research.ja.netfilter.plugin;
|
||||
package com.janetfilter.core.plugin;
|
||||
|
||||
public interface MyTransformer {
|
||||
String getHookClassName();
|
@ -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<FilterRule> filterRules) {
|
||||
default void init(Environment environment, List<FilterRule> filterRules) {
|
||||
// get plugin config
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
package io.zhile.research.ja.netfilter.rulers;
|
||||
package com.janetfilter.core.rulers;
|
||||
|
||||
public class EqualICRuler implements Ruler {
|
||||
@Override
|
@ -1,4 +1,4 @@
|
||||
package io.zhile.research.ja.netfilter.rulers;
|
||||
package com.janetfilter.core.rulers;
|
||||
|
||||
public class EqualRuler implements Ruler {
|
||||
@Override
|
@ -1,4 +1,4 @@
|
||||
package io.zhile.research.ja.netfilter.rulers;
|
||||
package com.janetfilter.core.rulers;
|
||||
|
||||
public class KeywordICRuler implements Ruler {
|
||||
@Override
|
@ -1,4 +1,4 @@
|
||||
package io.zhile.research.ja.netfilter.rulers;
|
||||
package com.janetfilter.core.rulers;
|
||||
|
||||
public class KeywordRuler implements Ruler {
|
||||
@Override
|
@ -1,4 +1,4 @@
|
||||
package io.zhile.research.ja.netfilter.rulers;
|
||||
package com.janetfilter.core.rulers;
|
||||
|
||||
public class PrefixICRuler implements Ruler {
|
||||
@Override
|
@ -1,4 +1,4 @@
|
||||
package io.zhile.research.ja.netfilter.rulers;
|
||||
package com.janetfilter.core.rulers;
|
||||
|
||||
public class PrefixRuler implements Ruler {
|
||||
@Override
|
@ -1,4 +1,4 @@
|
||||
package io.zhile.research.ja.netfilter.rulers;
|
||||
package com.janetfilter.core.rulers;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
@ -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) {
|
@ -1,4 +1,4 @@
|
||||
package io.zhile.research.ja.netfilter.rulers;
|
||||
package com.janetfilter.core.rulers;
|
||||
|
||||
public class SuffixICRuler implements Ruler {
|
||||
@Override
|
@ -1,4 +1,4 @@
|
||||
package io.zhile.research.ja.netfilter.rulers;
|
||||
package com.janetfilter.core.rulers;
|
||||
|
||||
public class SuffixRuler implements Ruler {
|
||||
@Override
|
@ -1,4 +1,4 @@
|
||||
package io.zhile.research.ja.netfilter.utils;
|
||||
package com.janetfilter.core.utils;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
@ -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) {
|
@ -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<FilterRule> ruleList;
|
||||
|
||||
public static void setRules(List<FilterRule> 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;
|
||||
}
|
||||
}
|
@ -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<MyTransformer> transformers = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public void init(List<FilterRule> 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<MyTransformer> getTransformers() {
|
||||
return transformers;
|
||||
}
|
||||
}
|
@ -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<FilterRule> rules;
|
||||
|
||||
public InetAddressTransformer(List<FilterRule> 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();
|
||||
}
|
||||
}
|
@ -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<FilterRule> rules;
|
||||
|
||||
public HttpClientTransformer(List<FilterRule> 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();
|
||||
}
|
||||
}
|
@ -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<FilterRule> ruleList;
|
||||
|
||||
public static void setRules(List<FilterRule> 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;
|
||||
}
|
||||
}
|
@ -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<MyTransformer> transformers = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public void init(List<FilterRule> 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<MyTransformer> getTransformers() {
|
||||
return transformers;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue