remove unused 'service' from resolver

This commit is contained in:
Daniel Gultsch 2023-03-02 19:45:45 +01:00
parent 4addeaa356
commit 807078b24f
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2
3 changed files with 34 additions and 43 deletions

View file

@ -1,8 +1,7 @@
package im.conversations.android.dns;
import android.app.Application;
import android.content.Context;
import androidx.annotation.NonNull;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Ordering;
import com.google.common.util.concurrent.Futures;
@ -53,10 +52,7 @@ public class Resolver {
private static final Executor EXECUTOR = Executors.newFixedThreadPool(4);
private static Context SERVICE;
public static void init(final Application application) {
SERVICE = application.getApplicationContext();
DNSClient.removeDNSServerLookupMechanism(AndroidUsingExec.INSTANCE);
DNSClient.addDnsServerLookupMechanism(AndroidUsingExecLowPriority.INSTANCE);
DNSClient.addDnsServerLookupMechanism(new AndroidUsingLinkProperties(application));
@ -116,15 +112,15 @@ public class Resolver {
}
}
public static List<ServiceRecord> resolve(final String domain) {
public static List<ServiceRecord> resolve(final String domain, final boolean validateHostname) {
final List<ServiceRecord> ipResults = fromIpAddress(domain);
if (ipResults.size() > 0) {
return ipResults;
}
final ListenableFuture<List<ServiceRecord>> directTlsSrvRecords =
Futures.submitAsync(() -> resolveSrv(domain, true), EXECUTOR);
Futures.submitAsync(() -> resolveSrv(domain, true, validateHostname), EXECUTOR);
final ListenableFuture<List<ServiceRecord>> startTlsSrvRecords =
Futures.submitAsync(() -> resolveSrv(domain, false), EXECUTOR);
Futures.submitAsync(() -> resolveSrv(domain, false, validateHostname), EXECUTOR);
final ListenableFuture<List<ServiceRecord>> srvRecords =
Futures.transform(
Futures.allAsList(directTlsSrvRecords, startTlsSrvRecords),
@ -164,39 +160,24 @@ public class Resolver {
} catch (final UnknownHostException e) {
return Collections.emptyList();
}
return Collections.singletonList(new ServiceRecord(ip, null, DEFAULT_PORT_XMPP, false, 0, false));
return Collections.singletonList(
new ServiceRecord(ip, null, DEFAULT_PORT_XMPP, false, 0, false));
}
private static ListenableFuture<List<ServiceRecord>> resolveSrv(
final String domain, final boolean directTls) throws IOException {
DNSName dnsName =
final String domain, final boolean directTls, final boolean validateHostname)
throws IOException {
final var dnsName =
DNSName.from(
(directTls ? DIRECT_TLS_SERVICE : STARTTLS_SERVICE) + "._tcp." + domain);
final ResolverResult<SRV> result = resolveWithFallback(dnsName, SRV.class);
final ResolverResult<SRV> result = resolveWithFallback(dnsName, validateHostname);
final List<ListenableFuture<List<ServiceRecord>>> results = new ArrayList<>();
for (final SRV record : result.getAnswersOrEmptySet()) {
if (record.name.length() == 0 && record.priority == 0) {
continue;
}
results.add(
Futures.submit(
() -> {
final List<ServiceRecord> ipv4s =
resolveIp(
record,
A.class,
result.isAuthenticData(),
directTls);
if (ipv4s.isEmpty()) {
return Collections.singletonList(
ServiceRecord.fromRecord(
record, directTls, result.isAuthenticData()));
} else {
return ipv4s;
}
},
EXECUTOR));
results.add(
final var ipv4 = Futures.submit(() -> resolveIPv4(directTls, result, record), EXECUTOR);
final var ipv6 =
Futures.submit(
() ->
resolveIp(
@ -204,7 +185,9 @@ public class Resolver {
AAAA.class,
result.isAuthenticData(),
directTls),
EXECUTOR));
EXECUTOR);
results.add(ipv4);
results.add(ipv6);
}
return Futures.transform(
Futures.allAsList(results),
@ -212,6 +195,19 @@ public class Resolver {
MoreExecutors.directExecutor());
}
@NonNull
private static List<ServiceRecord> resolveIPv4(
boolean directTls, ResolverResult<SRV> result, SRV record) {
final List<ServiceRecord> ipv4s =
resolveIp(record, A.class, result.isAuthenticData(), directTls);
if (ipv4s.isEmpty()) {
return Collections.singletonList(
ServiceRecord.fromRecord(record, directTls, result.isAuthenticData()));
} else {
return ipv4s;
}
}
private static <D extends InternetAddressRR> List<ServiceRecord> resolveIp(
SRV srv, Class<D> type, boolean authenticated, boolean directTls) {
final ImmutableList.Builder<ServiceRecord> builder = new ImmutableList.Builder<>();
@ -254,9 +250,9 @@ public class Resolver {
return results;
}
private static <D extends Data> ResolverResult<D> resolveWithFallback(
DNSName dnsName, Class<D> type) throws IOException {
return resolveWithFallback(dnsName, type, validateHostname());
private static ResolverResult<SRV> resolveWithFallback(
final DNSName dnsName, boolean validateHostname) throws IOException {
return resolveWithFallback(dnsName, SRV.class, validateHostname);
}
private static <D extends Data> ResolverResult<D> resolveWithFallback(
@ -285,5 +281,4 @@ public class Resolver {
// TODO bring back in one form or another
return false;
}
}

View file

@ -1,14 +1,11 @@
package im.conversations.android.dns;
import androidx.annotation.NonNull;
import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
import java.net.InetAddress;
import de.measite.minidns.DNSName;
import de.measite.minidns.record.SRV;
import java.net.InetAddress;
public class ServiceRecord implements Comparable<ServiceRecord> {
private final InetAddress ip;

View file

@ -327,7 +327,7 @@ public class XmppConnection implements Runnable {
if (connection != null) {
results = Resolver.fromHardCoded(connection);
} else {
results = Resolver.resolve(domain);
results = Resolver.resolve(domain, false);
}
LOGGER.info("{}", results);
if (Thread.currentThread().isInterrupted()) {
@ -353,8 +353,7 @@ public class XmppConnection implements Runnable {
+ storedBackupResult);
}
}
for (Iterator<ServiceRecord> iterator = results.iterator();
iterator.hasNext(); ) {
for (Iterator<ServiceRecord> iterator = results.iterator(); iterator.hasNext(); ) {
final ServiceRecord result = iterator.next();
if (Thread.currentThread().isInterrupted()) {
LOGGER.debug(account.address + ": Thread was interrupted");