Added support for Spamhaus/DBL

pull/199/head
M66B 4 years ago
parent 692c1ed647
commit 2fb20056f0

@ -35,22 +35,39 @@ import java.util.Map;
public class DnsBlockList { public class DnsBlockList {
private static final List<BlockList> BLOCKLISTS = Collections.unmodifiableList(Arrays.asList( private static final List<BlockList> BLOCKLISTS = Collections.unmodifiableList(Arrays.asList(
new BlockList("Spamhaus/zen", "zen.spamhaus.org", new String[]{ // https://www.spamhaus.org/zen/
//https://www.spamhaus.org/faq/section/DNSBL%20Usage#200 new BlockList(true, "Spamhaus/zen", "zen.spamhaus.org", true, new String[]{
// https://www.spamhaus.org/faq/section/DNSBL%20Usage#200
"127.0.0.2", // SBL Spamhaus SBL Data "127.0.0.2", // SBL Spamhaus SBL Data
"127.0.0.3", // SBL Spamhaus SBL CSS Data "127.0.0.3", // SBL Spamhaus SBL CSS Data
"127.0.0.4", // XBL CBL Data "127.0.0.4", // XBL CBL Data
"127.0.0.9" // SBL Spamhaus DROP/EDROP Data "127.0.0.9", // SBL Spamhaus DROP/EDROP Data
//127.0.0.10 PBL ISP Maintained //127.0.0.10 PBL ISP Maintained
//127.0.0.11 PBL Spamhaus Maintained //127.0.0.11 PBL Spamhaus Maintained
}), }),
new BlockList("Spamcop", "bl.spamcop.net", new String[]{
// https://www.spamhaus.org/dbl/
new BlockList(true, "Spamhaus/DBL", "dbl.spamhaus.org", false, new String[]{
// https://www.spamhaus.org/faq/section/Spamhaus%20DBL#291
"127.0.1.2", // spam domain
"127.0.1.4", // phish domain
"127.0.1.5", // malware domain
"127.0.1.6", // botnet C&C domain
"127.0.1.102", // abused legit spam
"127.0.1.103", // abused spammed redirector domain
"127.0.1.104", // abused legit phish
"127.0.1.105", // abused legit malware
"127.0.1.106", // abused legit botnet C&C
}),
new BlockList(true, "Spamcop", "bl.spamcop.net", true, new String[]{
// https://www.spamcop.net/fom-serve/cache/291.html // https://www.spamcop.net/fom-serve/cache/291.html
"127.0.0.2" "127.0.0.2",
}),
new BlockList(false, "Barracuda", "b.barracudacentral.org", true, new String[]{
// https://www.barracudacentral.org/rbl/how-to-use
}) })
//new BlockList("b.barracudacentral.org", new String[]{
// // https://www.barracudacentral.org/rbl/how-to-use
//})
)); ));
private static final long CACHE_EXPIRY_AFTER = 3600 * 1000L; // milliseconds private static final long CACHE_EXPIRY_AFTER = 3600 * 1000L; // milliseconds
@ -59,7 +76,8 @@ public class DnsBlockList {
static List<String> getNames() { static List<String> getNames() {
List<String> names = new ArrayList<>(); List<String> names = new ArrayList<>();
for (BlockList blocklist : BLOCKLISTS) for (BlockList blocklist : BLOCKLISTS)
names.add(blocklist.name); if (blocklist.enabled)
names.add(blocklist.name);
return names; return names;
} }
@ -83,7 +101,7 @@ public class DnsBlockList {
boolean blocked = false; boolean blocked = false;
for (BlockList blocklist : blocklists) for (BlockList blocklist : blocklists)
if (isJunk(domain, blocklist)) { if (blocklist.enabled && isJunk(domain, blocklist)) {
blocked = true; blocked = true;
break; break;
} }
@ -97,65 +115,42 @@ public class DnsBlockList {
private static boolean isJunk(String domain, BlockList blocklist) { private static boolean isJunk(String domain, BlockList blocklist) {
try { try {
long start = new Date().getTime(); if (blocklist.numeric) {
InetAddress[] addresses = InetAddress.getAllByName(domain); long start = new Date().getTime();
long elapsed = new Date().getTime() - start; InetAddress[] addresses = InetAddress.getAllByName(domain);
Log.i("isJunk resolved=" + domain + " elapse=" + elapsed + " ms"); long elapsed = new Date().getTime() - start;
for (InetAddress addr : addresses) Log.i("isJunk resolved=" + domain + " elapse=" + elapsed + " ms");
try { for (InetAddress addr : addresses)
StringBuilder lookup = new StringBuilder();
if (addr instanceof Inet4Address) {
byte[] a = addr.getAddress();
for (int i = 3; i >= 0; i--)
lookup.append(a[i] & 0xff).append('.');
} else if (addr instanceof Inet6Address) {
byte[] a = addr.getAddress();
for (int i = 15; i >= 0; i--) {
int b = a[i] & 0xff;
lookup.append(String.format("%01x", b & 0xf)).append('.');
lookup.append(String.format("%01x", b >> 4)).append('.');
}
}
lookup.append(blocklist.address);
start = new Date().getTime();
InetAddress result;
try { try {
// Possibly blocked StringBuilder lookup = new StringBuilder();
result = InetAddress.getByName(lookup.toString()); if (addr instanceof Inet4Address) {
} catch (UnknownHostException ignored) { byte[] a = addr.getAddress();
// Not blocked for (int i = 3; i >= 0; i--)
result = null; lookup.append(a[i] & 0xff).append('.');
} } else if (addr instanceof Inet6Address) {
elapsed = new Date().getTime() - start; byte[] a = addr.getAddress();
for (int i = 15; i >= 0; i--) {
if (result != null && blocklist.responses.length > 0) { int b = a[i] & 0xff;
boolean blocked = false; lookup.append(String.format("%01x", b & 0xf)).append('.');
for (InetAddress response : blocklist.responses) lookup.append(String.format("%01x", b >> 4)).append('.');
if (response.equals(result)) {
blocked = true;
break;
} }
if (!blocked) {
Log.w("isJunk" +
" addr=" + addr +
" lookup=" + lookup +
" result=" + result +
" elapsed=" + elapsed);
result = null;
} }
}
Log.i("isJunk " + addr + lookup.append(blocklist.address);
" " + lookup + "=" + (result == null ? "false" : result) +
" elapsed=" + elapsed);
if (result != null) if (isJunk(lookup.toString(), blocklist.responses))
return true; return true;
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(ex); Log.w(ex);
} }
} else {
long start = new Date().getTime();
String lookup = domain + "." + blocklist.address;
boolean junk = isJunk(lookup, blocklist.responses);
long elapsed = new Date().getTime() - start;
Log.i("isJunk" + " " + lookup + "=" + junk + " elapsed=" + elapsed);
return junk;
}
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(ex); Log.w(ex);
} }
@ -163,6 +158,41 @@ public class DnsBlockList {
return false; return false;
} }
private static boolean isJunk(String lookup, InetAddress[] responses) {
long start = new Date().getTime();
InetAddress result;
try {
// Possibly blocked
result = InetAddress.getByName(lookup);
} catch (UnknownHostException ignored) {
// Not blocked
result = null;
}
long elapsed = new Date().getTime() - start;
if (result != null && responses.length > 0) {
boolean blocked = false;
for (InetAddress response : responses)
if (response.equals(result)) {
blocked = true;
break;
}
if (!blocked) {
Log.w("isJunk" +
" lookup=" + lookup +
" result=" + result +
" elapsed=" + elapsed);
result = null;
}
}
Log.i("isJunk" +
" " + lookup + "=" + (result == null ? "false" : result) +
" elapsed=" + elapsed);
return (result != null);
}
private static class CacheEntry { private static class CacheEntry {
private final long time; private final long time;
private final boolean blocked; private final boolean blocked;
@ -182,13 +212,17 @@ public class DnsBlockList {
} }
static class BlockList { static class BlockList {
boolean enabled;
String name; String name;
String address; String address;
boolean numeric;
InetAddress[] responses; InetAddress[] responses;
BlockList(String name, String address, String[] responses) { BlockList(boolean enabled, String name, String address, boolean numeric, String[] responses) {
this.enabled = enabled;
this.name = name; this.name = name;
this.address = address; this.address = address;
this.numeric = numeric;
List<InetAddress> r = new ArrayList<>(); List<InetAddress> r = new ArrayList<>();
for (String response : responses) for (String response : responses)
try { try {

Loading…
Cancel
Save