From 6bb9c9ce3b714b18a99dbecbb10505f4adbd7118 Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Tue, 1 Aug 2017 11:44:23 +0200 Subject: resolve non-srv domains in resolver class as well --- .../java/de/pixart/messenger/utils/Resolver.java | 50 +++++++++++++++++++--- 1 file changed, 43 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/main/java/de/pixart/messenger/utils/Resolver.java b/src/main/java/de/pixart/messenger/utils/Resolver.java index 03ad00145..528b4882c 100644 --- a/src/main/java/de/pixart/messenger/utils/Resolver.java +++ b/src/main/java/de/pixart/messenger/utils/Resolver.java @@ -19,6 +19,7 @@ import de.measite.minidns.hla.ResolverApi; import de.measite.minidns.hla.ResolverResult; import de.measite.minidns.record.A; import de.measite.minidns.record.AAAA; +import de.measite.minidns.record.CNAME; import de.measite.minidns.record.Data; import de.measite.minidns.record.InternetAddressRR; import de.measite.minidns.record.SRV; @@ -46,7 +47,7 @@ public class Resolver { Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": " + e.getMessage()); } if (results.size() == 0) { - results.add(Result.createDefault(domain)); + results.addAll(resolveFallback(DNSName.from(domain))); } Collections.sort(results); Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": " + results.toString()); @@ -54,7 +55,7 @@ public class Resolver { } private static List resolveSrv(String domain, final boolean directTls) throws IOException { - if (Thread.interrupted()) { + if (Thread.currentThread().isInterrupted()) { return Collections.emptyList(); } DNSName dnsName = DNSName.from((directTls ? DIRECT_TLS_SERVICE : STARTTLS_SERICE) + "._tcp." + domain); @@ -63,7 +64,7 @@ public class Resolver { for (SRV record : result.getAnswersOrEmptySet()) { final boolean addedIPv4 = results.addAll(resolveIp(record, A.class, result.isAuthenticData(), directTls)); results.addAll(resolveIp(record, AAAA.class, result.isAuthenticData(), directTls)); - if (!addedIPv4 && !Thread.interrupted()) { + if (!addedIPv4 && !Thread.currentThread().isInterrupted()) { Result resolverResult = Result.fromRecord(record, directTls); resolverResult.authenticated = resolverResult.isAuthenticated(); results.add(resolverResult); @@ -73,12 +74,12 @@ public class Resolver { } private static List resolveIp(SRV srv, Class type, boolean authenticated, boolean directTls) { - if (Thread.interrupted()) { + if (Thread.currentThread().isInterrupted()) { return Collections.emptyList(); } List list = new ArrayList<>(); try { - ResolverResult results = resolveWithFallback(srv.name, type); + ResolverResult results = resolveWithFallback(srv.name, type, !authenticated); for (D record : results.getAnswersOrEmptySet()) { Result resolverResult = Result.fromRecord(srv, directTls); resolverResult.authenticated = results.isAuthenticData() && authenticated; @@ -91,7 +92,37 @@ public class Resolver { return list; } + private static List resolveFallback(DNSName dnsName) { + List results = new ArrayList<>(); + try { + for (A a : resolveWithFallback(dnsName, A.class, true).getAnswersOrEmptySet()) { + results.add(Result.createDefault(dnsName, a.getInetAddress())); + } + for (AAAA aaaa : resolveWithFallback(dnsName, AAAA.class, true).getAnswersOrEmptySet()) { + results.add(Result.createDefault(dnsName, aaaa.getInetAddress())); + } + if (results.size() == 0) { + for (CNAME cname : resolveWithFallback(dnsName, CNAME.class, true).getAnswersOrEmptySet()) { + results.addAll(resolveFallback(cname.name)); + } + } + } catch (IOException e) { + Log.d(Config.LOGTAG, "error resolving fallback records " + e); + } + if (results.size() == 0) { + results.add(Result.createDefault(dnsName)); + } + return results; + } + private static ResolverResult resolveWithFallback(DNSName dnsName, Class type) throws IOException { + return resolveWithFallback(dnsName, type, false); + } + + private static ResolverResult resolveWithFallback(DNSName dnsName, Class type, boolean skipDnssec) throws IOException { + if (skipDnssec) { + return ResolverApi.INSTANCE.resolve(dnsName, type); + } try { final ResolverResult r = DnssecResolverApi.INSTANCE.resolveDnssecReliable(dnsName, type); if (r.wasSuccessful()) { @@ -183,11 +214,16 @@ public class Resolver { return result; } - public static Result createDefault(String domain) { + public static Result createDefault(DNSName hostname, InetAddress ip) { Result result = new Result(); result.port = 5222; - result.hostname = DNSName.from(domain); + result.hostname = hostname; + result.ip = ip; return result; } + + public static Result createDefault(DNSName hostname) { + return createDefault(hostname, null); + } } } \ No newline at end of file -- cgit v1.2.3