diff options
author | Christian Schneppe <christian@pix-art.de> | 2017-08-01 11:44:23 +0200 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2017-08-01 11:44:23 +0200 |
commit | 6bb9c9ce3b714b18a99dbecbb10505f4adbd7118 (patch) | |
tree | 09a7c2555f878ceb45f6cc533ad60a8f2b8afab8 | |
parent | 541b7c3f7fe86d4974cc4d6befe219b22141b16d (diff) |
resolve non-srv domains in resolver class as well
-rw-r--r-- | src/main/java/de/pixart/messenger/utils/Resolver.java | 50 |
1 files changed, 43 insertions, 7 deletions
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<Result> 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 <D extends InternetAddressRR> List<Result> resolveIp(SRV srv, Class<D> type, boolean authenticated, boolean directTls) { - if (Thread.interrupted()) { + if (Thread.currentThread().isInterrupted()) { return Collections.emptyList(); } List<Result> list = new ArrayList<>(); try { - ResolverResult<D> results = resolveWithFallback(srv.name, type); + ResolverResult<D> 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<Result> resolveFallback(DNSName dnsName) { + List<Result> 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 <D extends Data> ResolverResult<D> resolveWithFallback(DNSName dnsName, Class<D> type) throws IOException { + return resolveWithFallback(dnsName, type, false); + } + + private static <D extends Data> ResolverResult<D> resolveWithFallback(DNSName dnsName, Class<D> type, boolean skipDnssec) throws IOException { + if (skipDnssec) { + return ResolverApi.INSTANCE.resolve(dnsName, type); + } try { final ResolverResult<D> 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 |