2017-06-21 23:28:01 +02:00
|
|
|
package eu.siacs.conversations.utils;
|
|
|
|
|
2018-01-21 20:41:30 +01:00
|
|
|
import android.content.ContentValues;
|
|
|
|
import android.database.Cursor;
|
2017-06-21 23:28:01 +02:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2018-09-08 11:30:15 +02:00
|
|
|
import java.lang.reflect.Field;
|
2019-11-19 14:41:47 +01:00
|
|
|
import java.net.InetAddress;
|
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
import java.net.Socket;
|
|
|
|
import java.net.UnknownHostException;
|
2017-06-21 23:28:01 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
2019-11-19 14:41:47 +01:00
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2017-06-21 23:28:01 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
2018-09-08 11:30:15 +02:00
|
|
|
import de.measite.minidns.AbstractDNSClient;
|
2017-06-21 23:28:01 +02:00
|
|
|
import de.measite.minidns.DNSClient;
|
|
|
|
import de.measite.minidns.DNSName;
|
2017-09-21 21:46:11 +02:00
|
|
|
import de.measite.minidns.Question;
|
|
|
|
import de.measite.minidns.Record;
|
2017-06-25 22:46:56 +02:00
|
|
|
import de.measite.minidns.dnssec.DNSSECResultNotAuthenticException;
|
2017-06-21 23:28:01 +02:00
|
|
|
import de.measite.minidns.hla.DnssecResolverApi;
|
2017-06-25 22:46:56 +02:00
|
|
|
import de.measite.minidns.hla.ResolverApi;
|
2017-06-21 23:28:01 +02:00
|
|
|
import de.measite.minidns.hla.ResolverResult;
|
2018-09-08 11:30:15 +02:00
|
|
|
import de.measite.minidns.iterative.ReliableDNSClient;
|
2019-11-19 14:41:47 +01:00
|
|
|
import de.measite.minidns.record.A;
|
|
|
|
import de.measite.minidns.record.AAAA;
|
2017-07-10 08:49:22 +02:00
|
|
|
import de.measite.minidns.record.CNAME;
|
2017-06-25 22:46:56 +02:00
|
|
|
import de.measite.minidns.record.Data;
|
2019-11-19 14:41:47 +01:00
|
|
|
import de.measite.minidns.record.InternetAddressRR;
|
2017-06-21 23:28:01 +02:00
|
|
|
import de.measite.minidns.record.SRV;
|
|
|
|
import eu.siacs.conversations.Config;
|
2017-07-10 09:59:25 +02:00
|
|
|
import eu.siacs.conversations.R;
|
2019-11-19 14:41:47 +01:00
|
|
|
import eu.siacs.conversations.persistance.FileBackend;
|
2017-07-10 09:59:25 +02:00
|
|
|
import eu.siacs.conversations.services.XmppConnectionService;
|
2017-06-21 23:28:01 +02:00
|
|
|
|
|
|
|
public class Resolver {
|
|
|
|
|
2018-11-10 10:19:29 +01:00
|
|
|
public static final int DEFAULT_PORT_XMPP = 5222;
|
|
|
|
|
2018-09-08 11:30:15 +02:00
|
|
|
private static final String DIRECT_TLS_SERVICE = "_xmpps-client";
|
2018-11-10 23:56:09 +01:00
|
|
|
private static final String STARTTLS_SERVICE = "_xmpp-client";
|
2018-09-08 11:30:15 +02:00
|
|
|
|
|
|
|
private static XmppConnectionService SERVICE = null;
|
|
|
|
|
|
|
|
|
|
|
|
public static void init(XmppConnectionService service) {
|
|
|
|
Resolver.SERVICE = service;
|
|
|
|
final AbstractDNSClient client = ResolverApi.INSTANCE.getClient();
|
|
|
|
if (client instanceof ReliableDNSClient) {
|
|
|
|
disableHardcodedDnsServers((ReliableDNSClient) client);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void disableHardcodedDnsServers(ReliableDNSClient reliableDNSClient) {
|
|
|
|
try {
|
|
|
|
final Field dnsClientField = ReliableDNSClient.class.getDeclaredField("dnsClient");
|
|
|
|
dnsClientField.setAccessible(true);
|
|
|
|
final DNSClient dnsClient = (DNSClient) dnsClientField.get(reliableDNSClient);
|
|
|
|
dnsClient.getDataSource().setTimeout(3000);
|
|
|
|
final Field useHardcodedDnsServers = DNSClient.class.getDeclaredField("useHardcodedDnsServers");
|
|
|
|
useHardcodedDnsServers.setAccessible(true);
|
|
|
|
useHardcodedDnsServers.setBoolean(dnsClient, false);
|
2018-12-09 22:32:42 +01:00
|
|
|
} catch (NoSuchFieldException e) {
|
|
|
|
Log.e(Config.LOGTAG, "Unable to disable hardcoded DNS servers", e);
|
|
|
|
} catch (IllegalAccessException e) {
|
2018-09-08 11:30:15 +02:00
|
|
|
Log.e(Config.LOGTAG, "Unable to disable hardcoded DNS servers", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 14:41:47 +01:00
|
|
|
public static Result fromHardCoded(String hostname, int port) {
|
|
|
|
final Result ipResult = fromIpAddress(hostname, port);
|
|
|
|
if (ipResult != null) {
|
|
|
|
ipResult.connect();
|
|
|
|
return ipResult;
|
|
|
|
}
|
|
|
|
return happyEyeball(resolveNoSrvRecords(DNSName.from(hostname), port, true));
|
2018-10-01 17:07:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-05 12:08:58 +02:00
|
|
|
public static boolean useDirectTls(final int port) {
|
|
|
|
return port == 443 || port == 5223;
|
|
|
|
}
|
|
|
|
|
2019-11-19 14:41:47 +01:00
|
|
|
public static Result resolve(String domain) {
|
|
|
|
final Result ipResult = fromIpAddress(domain, DEFAULT_PORT_XMPP);
|
|
|
|
if (ipResult != null) {
|
|
|
|
ipResult.connect();
|
|
|
|
return ipResult;
|
2018-10-01 17:07:37 +02:00
|
|
|
}
|
2018-09-08 11:30:15 +02:00
|
|
|
final List<Result> results = new ArrayList<>();
|
|
|
|
final List<Result> fallbackResults = new ArrayList<>();
|
|
|
|
Thread[] threads = new Thread[3];
|
|
|
|
threads[0] = new Thread(() -> {
|
|
|
|
try {
|
|
|
|
final List<Result> list = resolveSrv(domain, true);
|
|
|
|
synchronized (results) {
|
|
|
|
results.addAll(list);
|
|
|
|
}
|
|
|
|
} catch (Throwable throwable) {
|
|
|
|
Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": error resolving SRV record (direct TLS)", throwable);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
threads[1] = new Thread(() -> {
|
|
|
|
try {
|
|
|
|
final List<Result> list = resolveSrv(domain, false);
|
|
|
|
synchronized (results) {
|
|
|
|
results.addAll(list);
|
|
|
|
}
|
|
|
|
} catch (Throwable throwable) {
|
|
|
|
Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": error resolving SRV record (STARTTLS)", throwable);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
threads[2] = new Thread(() -> {
|
2019-11-19 14:41:47 +01:00
|
|
|
List<Result> list = resolveNoSrvRecords(DNSName.from(domain), DEFAULT_PORT_XMPP, true);
|
2018-09-08 11:30:15 +02:00
|
|
|
synchronized (fallbackResults) {
|
|
|
|
fallbackResults.addAll(list);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
for (Thread thread : threads) {
|
|
|
|
thread.start();
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
threads[0].join();
|
|
|
|
threads[1].join();
|
|
|
|
if (results.size() > 0) {
|
|
|
|
threads[2].interrupt();
|
2018-09-20 20:29:21 +02:00
|
|
|
synchronized (results) {
|
|
|
|
Collections.sort(results);
|
|
|
|
Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": " + results.toString());
|
2019-11-19 14:41:47 +01:00
|
|
|
return happyEyeball(results);
|
2018-09-20 20:29:21 +02:00
|
|
|
}
|
2018-09-08 11:30:15 +02:00
|
|
|
} else {
|
|
|
|
threads[2].join();
|
2018-09-20 20:29:21 +02:00
|
|
|
synchronized (fallbackResults) {
|
|
|
|
Collections.sort(fallbackResults);
|
|
|
|
Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": " + fallbackResults.toString());
|
2019-11-19 14:41:47 +01:00
|
|
|
return happyEyeball(fallbackResults);
|
2018-09-20 20:29:21 +02:00
|
|
|
}
|
2018-09-08 11:30:15 +02:00
|
|
|
}
|
|
|
|
} catch (InterruptedException e) {
|
2018-10-01 17:07:37 +02:00
|
|
|
for (Thread thread : threads) {
|
2018-09-26 10:18:56 +02:00
|
|
|
thread.interrupt();
|
|
|
|
}
|
2019-11-19 14:41:47 +01:00
|
|
|
return null;
|
2018-09-08 11:30:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 14:41:47 +01:00
|
|
|
private static Result fromIpAddress(String domain, int port) {
|
2018-10-01 17:07:37 +02:00
|
|
|
if (!IP.matches(domain)) {
|
2019-11-19 14:41:47 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
Result result = new Result();
|
|
|
|
result.ip = InetAddress.getByName(domain);
|
|
|
|
result.port = port;
|
|
|
|
return result;
|
|
|
|
} catch (UnknownHostException e) {
|
|
|
|
return null;
|
2018-10-01 17:07:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-08 11:30:15 +02:00
|
|
|
private static List<Result> resolveSrv(String domain, final boolean directTls) throws IOException {
|
2018-11-10 23:56:09 +01:00
|
|
|
DNSName dnsName = DNSName.from((directTls ? DIRECT_TLS_SERVICE : STARTTLS_SERVICE) + "._tcp." + domain);
|
2018-09-08 11:30:15 +02:00
|
|
|
ResolverResult<SRV> result = resolveWithFallback(dnsName, SRV.class);
|
|
|
|
final List<Result> results = new ArrayList<>();
|
2019-11-19 14:41:47 +01:00
|
|
|
final List<Thread> threads = new ArrayList<>();
|
2018-09-08 11:30:15 +02:00
|
|
|
for (SRV record : result.getAnswersOrEmptySet()) {
|
|
|
|
if (record.name.length() == 0 && record.priority == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-11-19 14:41:47 +01:00
|
|
|
threads.add(new Thread(() -> {
|
|
|
|
final List<Result> ipv6s = resolveIp(record, AAAA.class, result.isAuthenticData(), directTls);
|
|
|
|
synchronized (results) {
|
|
|
|
results.addAll(ipv6s);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
threads.add(new Thread(() -> {
|
|
|
|
final List<Result> ipv4s = resolveIp(record, A.class, result.isAuthenticData(), directTls);
|
|
|
|
synchronized (results) {
|
|
|
|
results.addAll(ipv4s);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
for (Thread thread : threads) {
|
|
|
|
thread.start();
|
|
|
|
}
|
|
|
|
for (Thread thread : threads) {
|
|
|
|
try {
|
|
|
|
thread.join();
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
return Collections.emptyList();
|
|
|
|
}
|
2018-09-08 11:30:15 +02:00
|
|
|
}
|
2018-11-10 23:56:09 +01:00
|
|
|
return results;
|
2018-09-08 11:30:15 +02:00
|
|
|
}
|
|
|
|
|
2019-11-19 14:41:47 +01:00
|
|
|
private static <D extends InternetAddressRR> List<Result> resolveIp(SRV srv, Class<D> type, boolean authenticated, boolean directTls) {
|
|
|
|
List<Result> list = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
ResolverResult<D> results = resolveWithFallback(srv.name, type, authenticated);
|
|
|
|
for (D record : results.getAnswersOrEmptySet()) {
|
|
|
|
Result resolverResult = Result.fromRecord(srv, directTls);
|
|
|
|
resolverResult.authenticated = results.isAuthenticData() && authenticated;
|
|
|
|
resolverResult.ip = record.getInetAddress();
|
|
|
|
list.add(resolverResult);
|
|
|
|
}
|
|
|
|
} catch (Throwable t) {
|
|
|
|
Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": error resolving " + type.getSimpleName() + " " + t.getMessage());
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static List<Result> resolveNoSrvRecords(DNSName dnsName, int port, boolean withCnames) {
|
2018-09-08 11:30:15 +02:00
|
|
|
List<Result> results = new ArrayList<>();
|
|
|
|
try {
|
2019-11-19 14:41:47 +01:00
|
|
|
for (AAAA aaaa : resolveWithFallback(dnsName, AAAA.class, false).getAnswersOrEmptySet()) {
|
|
|
|
results.add(Result.createDefault(dnsName, aaaa.getInetAddress(), port));
|
|
|
|
}
|
|
|
|
for (A a : resolveWithFallback(dnsName, A.class, false).getAnswersOrEmptySet()) {
|
|
|
|
results.add(Result.createDefault(dnsName, a.getInetAddress(), port));
|
|
|
|
}
|
|
|
|
if (results.size() == 0 && withCnames) {
|
2018-09-08 11:30:15 +02:00
|
|
|
for (CNAME cname : resolveWithFallback(dnsName, CNAME.class, false).getAnswersOrEmptySet()) {
|
2019-11-19 14:41:47 +01:00
|
|
|
results.addAll(resolveNoSrvRecords(cname.name, port, false));
|
2018-09-08 11:30:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Throwable throwable) {
|
|
|
|
Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + "error resolving fallback records", throwable);
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static <D extends Data> ResolverResult<D> resolveWithFallback(DNSName dnsName, Class<D> type) throws IOException {
|
|
|
|
return resolveWithFallback(dnsName, type, validateHostname());
|
|
|
|
}
|
|
|
|
|
|
|
|
private static <D extends Data> ResolverResult<D> resolveWithFallback(DNSName dnsName, Class<D> type, boolean validateHostname) throws IOException {
|
|
|
|
final Question question = new Question(dnsName, Record.TYPE.getType(type));
|
|
|
|
if (!validateHostname) {
|
|
|
|
return ResolverApi.INSTANCE.resolve(question);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
return DnssecResolverApi.INSTANCE.resolveDnssecReliable(question);
|
|
|
|
} catch (DNSSECResultNotAuthenticException e) {
|
|
|
|
Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": error resolving " + type.getSimpleName() + " with DNSSEC. Trying DNS instead.", e);
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw e;
|
|
|
|
} catch (Throwable throwable) {
|
|
|
|
Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": error resolving " + type.getSimpleName() + " with DNSSEC. Trying DNS instead.", throwable);
|
|
|
|
}
|
|
|
|
return ResolverApi.INSTANCE.resolve(question);
|
|
|
|
}
|
|
|
|
|
2019-11-19 14:41:47 +01:00
|
|
|
private static Result happyEyeball(List<Result> r) {
|
|
|
|
if (r.size() == 0) return null;
|
|
|
|
|
|
|
|
Result result;
|
|
|
|
if (r.size() == 1) {
|
|
|
|
result = r.get(0);
|
|
|
|
result.connect();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
ExecutorService executor = (ExecutorService) Executors.newFixedThreadPool(4);
|
|
|
|
|
|
|
|
try {
|
|
|
|
result = executor.invokeAny(r);
|
|
|
|
executor.shutdown();
|
2020-02-08 08:54:19 +01:00
|
|
|
if (result == null) {
|
|
|
|
Log.i(Config.LOGTAG, Resolver.class.getSimpleName() + ": happy eyeball unable to connect to one address");
|
|
|
|
return null;
|
|
|
|
}
|
2019-11-19 14:41:47 +01:00
|
|
|
Thread disconnector = new Thread(() -> {
|
|
|
|
while (true) {
|
|
|
|
try {
|
|
|
|
if (executor.awaitTermination(5, TimeUnit.SECONDS)) break;
|
|
|
|
Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": happy eyeball wait for cleanup ...");
|
|
|
|
} catch (InterruptedException e) {}
|
|
|
|
}
|
|
|
|
Log.i(Config.LOGTAG, Resolver.class.getSimpleName() + ": happy eyeball cleanup");
|
|
|
|
for (Result re : r) {
|
|
|
|
if(!re.equals(result)) re.disconnect();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
disconnector.start();
|
|
|
|
Log.i(Config.LOGTAG, Resolver.class.getSimpleName() + ": happy eyeball used: " + result.toString());
|
|
|
|
return result;
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
Log.e(Config.LOGTAG, Resolver.class.getSimpleName() + ": happy eyeball failed: ", e);
|
|
|
|
return null;
|
|
|
|
} catch (ExecutionException e) {
|
|
|
|
Log.e(Config.LOGTAG, Resolver.class.getSimpleName() + ": happy eyeball failed: ", e);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-08 11:30:15 +02:00
|
|
|
private static boolean validateHostname() {
|
|
|
|
return SERVICE != null && SERVICE.getBooleanPreference("validate_hostname", R.bool.validate_hostname);
|
|
|
|
}
|
|
|
|
|
2019-11-19 14:41:47 +01:00
|
|
|
public static class Result implements Comparable<Result>, Callable<Result> {
|
|
|
|
public static final String IP = "ip";
|
2018-09-08 11:30:15 +02:00
|
|
|
public static final String HOSTNAME = "hostname";
|
|
|
|
public static final String PORT = "port";
|
|
|
|
public static final String PRIORITY = "priority";
|
|
|
|
public static final String DIRECT_TLS = "directTls";
|
|
|
|
public static final String AUTHENTICATED = "authenticated";
|
2019-11-19 14:41:47 +01:00
|
|
|
|
|
|
|
private InetAddress ip;
|
2018-09-08 11:30:15 +02:00
|
|
|
private DNSName hostname;
|
2018-11-10 10:19:29 +01:00
|
|
|
private int port = DEFAULT_PORT_XMPP;
|
2018-09-08 11:30:15 +02:00
|
|
|
private boolean directTls = false;
|
|
|
|
private boolean authenticated = false;
|
|
|
|
private int priority;
|
2019-11-19 14:41:47 +01:00
|
|
|
private Socket socket;
|
2018-09-08 11:30:15 +02:00
|
|
|
|
|
|
|
static Result fromRecord(SRV srv, boolean directTls) {
|
|
|
|
Result result = new Result();
|
|
|
|
result.port = srv.port;
|
|
|
|
result.hostname = srv.name;
|
|
|
|
result.directTls = directTls;
|
|
|
|
result.priority = srv.priority;
|
|
|
|
return result;
|
|
|
|
}
|
2019-11-19 14:41:47 +01:00
|
|
|
|
|
|
|
static Result createDefault(DNSName hostname, InetAddress ip, int port) {
|
2018-09-08 11:30:15 +02:00
|
|
|
Result result = new Result();
|
2019-11-19 14:41:47 +01:00
|
|
|
result.port = port;
|
2018-09-08 11:30:15 +02:00
|
|
|
result.hostname = hostname;
|
2019-11-19 14:41:47 +01:00
|
|
|
result.ip = ip;
|
2018-09-08 11:30:15 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
if (this == o) return true;
|
|
|
|
if (o == null || getClass() != o.getClass()) return false;
|
|
|
|
|
|
|
|
Result result = (Result) o;
|
|
|
|
|
|
|
|
if (port != result.port) return false;
|
|
|
|
if (directTls != result.directTls) return false;
|
|
|
|
if (authenticated != result.authenticated) return false;
|
|
|
|
if (priority != result.priority) return false;
|
2019-11-19 14:41:47 +01:00
|
|
|
if (ip != null ? !ip.equals(result.ip) : result.ip != null) return false;
|
2018-09-08 11:30:15 +02:00
|
|
|
return hostname != null ? hostname.equals(result.hostname) : result.hostname == null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
2019-11-19 14:41:47 +01:00
|
|
|
int result = ip != null ? ip.hashCode() : 0;
|
|
|
|
result = 31 * result + (hostname != null ? hostname.hashCode() : 0);
|
2018-09-08 11:30:15 +02:00
|
|
|
result = 31 * result + port;
|
|
|
|
result = 31 * result + (directTls ? 1 : 0);
|
|
|
|
result = 31 * result + (authenticated ? 1 : 0);
|
|
|
|
result = 31 * result + priority;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public DNSName getHostname() {
|
|
|
|
return hostname;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isDirectTls() {
|
|
|
|
return directTls;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isAuthenticated() {
|
|
|
|
return authenticated;
|
|
|
|
}
|
|
|
|
|
2019-11-19 14:41:47 +01:00
|
|
|
public Socket getSocket() {
|
|
|
|
return socket;
|
|
|
|
}
|
|
|
|
|
2018-09-08 11:30:15 +02:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return "Result{" +
|
2019-11-19 14:41:47 +01:00
|
|
|
"ip='" + (ip == null ? null : ip.getHostAddress()) + '\'' +
|
2020-02-07 08:10:58 +01:00
|
|
|
", hostame='" + (hostname == null ? null : hostname.toString()) + '\'' +
|
2018-09-08 11:30:15 +02:00
|
|
|
", port=" + port +
|
|
|
|
", directTls=" + directTls +
|
|
|
|
", authenticated=" + authenticated +
|
|
|
|
", priority=" + priority +
|
|
|
|
'}';
|
|
|
|
}
|
|
|
|
|
2019-11-19 14:41:47 +01:00
|
|
|
public void connect() {
|
|
|
|
if (this.socket != null) {
|
|
|
|
this.disconnect();
|
|
|
|
}
|
|
|
|
final InetSocketAddress addr = new InetSocketAddress(this.ip, this.port);
|
|
|
|
this.socket = new Socket();
|
|
|
|
try {
|
|
|
|
long time = System.currentTimeMillis();
|
|
|
|
this.socket.connect(addr, Config.SOCKET_TIMEOUT * 1000);
|
|
|
|
time = System.currentTimeMillis() - time;
|
|
|
|
Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": Result connect: " + toString() + " after: " + time + " ms");
|
|
|
|
} catch (IOException e) {
|
|
|
|
this.disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void disconnect() {
|
|
|
|
if (this.socket != null ) {
|
|
|
|
FileBackend.close(this.socket);
|
|
|
|
this.socket = null;
|
|
|
|
Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": Result disconnect: " + toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-08 11:30:15 +02:00
|
|
|
@Override
|
|
|
|
public int compareTo(@NonNull Result result) {
|
|
|
|
if (result.priority == priority) {
|
|
|
|
if (directTls == result.directTls) {
|
2019-11-19 14:41:47 +01:00
|
|
|
return 0;
|
2018-09-08 11:30:15 +02:00
|
|
|
} else {
|
|
|
|
return directTls ? -1 : 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return priority - result.priority;
|
|
|
|
}
|
|
|
|
}
|
2019-11-19 14:41:47 +01:00
|
|
|
@Override
|
|
|
|
public Result call() throws Exception {
|
|
|
|
this.connect();
|
2020-02-08 08:54:19 +01:00
|
|
|
if (this.socket != null && this.socket.isConnected()) {
|
|
|
|
return this ;
|
|
|
|
}
|
|
|
|
return null;
|
2019-11-19 14:41:47 +01:00
|
|
|
}
|
2018-09-08 11:30:15 +02:00
|
|
|
|
|
|
|
public ContentValues toContentValues() {
|
|
|
|
final ContentValues contentValues = new ContentValues();
|
2019-11-19 14:41:47 +01:00
|
|
|
contentValues.put(IP, ip == null ? null : ip.getAddress());
|
2018-10-13 16:13:33 +02:00
|
|
|
contentValues.put(HOSTNAME, hostname == null ? null : hostname.toString());
|
2018-09-08 11:30:15 +02:00
|
|
|
contentValues.put(PORT, port);
|
|
|
|
contentValues.put(PRIORITY, priority);
|
|
|
|
contentValues.put(DIRECT_TLS, directTls ? 1 : 0);
|
|
|
|
contentValues.put(AUTHENTICATED, authenticated ? 1 : 0);
|
|
|
|
return contentValues;
|
|
|
|
}
|
|
|
|
}
|
2017-06-21 23:28:01 +02:00
|
|
|
|
|
|
|
}
|