Signed-off-by: pengzhile <pengzhile@gmail.com>
pull/1/head
pengzhile 3 years ago
parent 642f49a2c3
commit 8a0cc23737

@ -20,6 +20,7 @@ public class Launcher {
}
try {
jarURL = new URL("file:/Users/neo/Downloads/ja-netfilter/target/ja-netfilter-jar-with-dependencies.jar");
inst.appendToBootstrapClassLoaderSearch(new JarFile(jarURL.getPath()));
} catch (Throwable e) {
throw new RuntimeException("Can not access ja-netfilter jar file.", e);

@ -1,9 +1,21 @@
package io.zhile.research.ja.netfilter.enums;
import io.zhile.research.ja.netfilter.rulers.*;
public enum RuleType {
PREFIX,
SUFFIX,
KEYWORD,
REGEXP,
EQUAL
PREFIX(new PrefixRuler()),
SUFFIX(new SuffixRuler()),
KEYWORD(new KeywordRuler()),
REGEXP(new RegExpRuler()),
EQUAL(new EqualRuler());
private final Ruler ruler;
RuleType(Ruler ruler) { // Lazy here. No lazy loading
this.ruler = ruler;
}
public Ruler getRuler() {
return ruler;
}
}

@ -13,7 +13,7 @@ public class DNSFilter {
static {
RULES = new ArrayList<>(); // TODO read from config file
RULES.add(new FilterRule(RuleType.EQUAL, "zhile.io"));
RULES.add(new FilterRule(RuleType.REGEXP, ".*?zhile.io"));
}
public static String testQuery(String host) throws IOException {
@ -22,15 +22,12 @@ public class DNSFilter {
}
for (FilterRule rule : RULES) {
switch (rule.getType()) { // TODO rewrite
case EQUAL:
if (host.equals(rule.getContent())) {
System.out.println("=== reject dns query: " + host);
throw new java.net.UnknownHostException();
}
default: // TODO support more rule types
return host;
if (!rule.test(host)) {
continue;
}
System.out.println("=== reject dns query: " + host + ", rule: " + rule);
throw new java.net.UnknownHostException();
}
return host;
@ -42,15 +39,12 @@ public class DNSFilter {
}
for (FilterRule rule : RULES) {
switch (rule.getType()) { // TODO rewrite
case EQUAL:
if (n.getHostName().equals(rule.getContent())) {
System.out.println("=== reject dns reachable test: " + n.getHostName());
return false;
}
default: // TODO support more rule types
return null;
if (!rule.test(n.getHostName())) {
continue;
}
System.out.println("=== reject dns reachable test: " + n.getHostName() + ", rule: " + rule);
return false;
}
return null;

@ -23,15 +23,12 @@ public class URLFilter {
}
for (FilterRule rule : RULES) {
switch (rule.getType()) { // TODO rewrite
case PREFIX:
if (url.toString().startsWith(rule.getContent())) {
System.out.println("=== reject url: " + url.toString());
throw new SocketTimeoutException("connect timed out");
}
default: // TODO support more rule types
return url;
if (!rule.test(url.toString())) {
continue;
}
System.out.println("=== reject url: " + url + ", rule: " + rule);
throw new SocketTimeoutException("connect timed out");
}
return url;

@ -5,11 +5,11 @@ import io.zhile.research.ja.netfilter.enums.RuleType;
public class FilterRule {
private RuleType type;
private String content;
private String rule;
public FilterRule(RuleType type, String content) {
public FilterRule(RuleType type, String rule) {
this.type = type;
this.content = content;
this.rule = rule;
}
public RuleType getType() {
@ -20,11 +20,23 @@ public class FilterRule {
this.type = type;
}
public String getContent() {
return content;
public String getRule() {
return rule;
}
public void setContent(String content) {
this.content = content;
public void setRule(String rule) {
this.rule = rule;
}
public boolean test(String content) {
return type.getRuler().test(this.rule, content);
}
@Override
public String toString() {
return "{" +
"type=" + type +
", rule='" + rule + '\'' +
'}';
}
}

@ -0,0 +1,8 @@
package io.zhile.research.ja.netfilter.rulers;
public class EqualRuler implements Ruler {
@Override
public boolean test(String rule, String content) {
return content.equals(rule);
}
}

@ -0,0 +1,8 @@
package io.zhile.research.ja.netfilter.rulers;
public class KeywordRuler implements Ruler {
@Override
public boolean test(String rule, String content) {
return content.contains(rule);
}
}

@ -0,0 +1,8 @@
package io.zhile.research.ja.netfilter.rulers;
public class PrefixRuler implements Ruler {
@Override
public boolean test(String rule, String content) {
return content.startsWith(rule);
}
}

@ -0,0 +1,10 @@
package io.zhile.research.ja.netfilter.rulers;
import java.util.regex.Pattern;
public class RegExpRuler implements Ruler {
@Override
public boolean test(String rule, String content) {
return Pattern.matches(rule, content);
}
}

@ -0,0 +1,7 @@
package io.zhile.research.ja.netfilter.rulers;
public interface Ruler {
default boolean test(String rule, String content) {
return false;
}
}

@ -0,0 +1,8 @@
package io.zhile.research.ja.netfilter.rulers;
public class SuffixRuler implements Ruler {
@Override
public boolean test(String rule, String content) {
return content.endsWith(rule);
}
}
Loading…
Cancel
Save