Filter SPF records

pull/214/head
M66B 1 year ago
parent a15293a73a
commit 4033d07417

@ -600,17 +600,16 @@ public class ActivityDMARC extends ActivityBase {
List<Pair<String, DnsHelper.DnsRecord>> result = new ArrayList<>(); List<Pair<String, DnsHelper.DnsRecord>> result = new ArrayList<>();
try { try {
DnsHelper.DnsRecord[] records = DnsHelper.lookup(context, domain, "txt"); DnsHelper.DnsRecord[] records = DnsHelper.lookup(context, domain, "txt:v=spf1 ");
ssb.append(domain).append('=').append(Integer.toString(records.length)).append('\n'); ssb.append(domain).append('=').append(Integer.toString(records.length)).append('\n');
for (DnsHelper.DnsRecord r : records) for (DnsHelper.DnsRecord r : records) {
if (r.response.contains("spf")) { result.add(new Pair<>(domain, r));
result.add(new Pair<>(domain, r)); for (String part : r.response.split("\\s+"))
for (String part : r.response.split("\\s+")) if (part.toLowerCase(Locale.ROOT).startsWith("include:")) {
if (part.toLowerCase(Locale.ROOT).startsWith("include:")) { String sub = part.substring("include:".length());
String sub = part.substring("include:".length()); result.addAll(lookupSpf(context, sub, ssb));
result.addAll(lookupSpf(context, sub, ssb)); }
} }
}
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(ex); Log.w(ex);
ssb.append(ex.toString()).append('\n'); ssb.append(ex.toString()).append('\n');

@ -48,6 +48,7 @@ import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.Semaphore; import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -81,6 +82,13 @@ public class DnsHelper {
@NonNull @NonNull
static DnsRecord[] lookup(Context context, String name, String type, int timeout) throws UnknownHostException { static DnsRecord[] lookup(Context context, String name, String type, int timeout) throws UnknownHostException {
String filter = null;
int colon = type.indexOf(':');
if (colon > 0) {
filter = type.substring(colon + 1);
type = type.substring(0, colon);
}
int rtype; int rtype;
switch (type) { switch (type) {
case "ns": case "ns":
@ -214,6 +222,9 @@ public class DnsHelper {
TXTRecord txt = (TXTRecord) record; TXTRecord txt = (TXTRecord) record;
for (Object content : txt.getStrings()) { for (Object content : txt.getStrings()) {
String text = content.toString(); String text = content.toString();
if (filter != null &&
(TextUtils.isEmpty(text) || !text.toLowerCase(Locale.ROOT).startsWith(filter)))
continue;
int i = 0; int i = 0;
int slash = text.indexOf('\\', i); int slash = text.indexOf('\\', i);
while (slash >= 0 && slash + 4 < text.length()) { while (slash >= 0 && slash + 4 < text.length()) {

Loading…
Cancel
Save