first try of happy eyeball with invokeAny

This commit is contained in:
genofire 2019-11-19 14:41:47 +01:00
parent 1c0c6a6fba
commit 611effc409
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
2 changed files with 161 additions and 112 deletions

View File

@ -7,8 +7,15 @@ import android.util.Log;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.List; import java.util.List;
import de.measite.minidns.AbstractDNSClient; import de.measite.minidns.AbstractDNSClient;
@ -21,8 +28,11 @@ import de.measite.minidns.hla.DnssecResolverApi;
import de.measite.minidns.hla.ResolverApi; import de.measite.minidns.hla.ResolverApi;
import de.measite.minidns.hla.ResolverResult; import de.measite.minidns.hla.ResolverResult;
import de.measite.minidns.iterative.ReliableDNSClient; import de.measite.minidns.iterative.ReliableDNSClient;
import de.measite.minidns.record.A;
import de.measite.minidns.record.AAAA;
import de.measite.minidns.record.CNAME; import de.measite.minidns.record.CNAME;
import de.measite.minidns.record.Data; import de.measite.minidns.record.Data;
import de.measite.minidns.record.InternetAddressRR;
import de.measite.minidns.record.SRV; import de.measite.minidns.record.SRV;
import eu.siacs.conversations.Config; import eu.siacs.conversations.Config;
import eu.siacs.conversations.R; import eu.siacs.conversations.R;
@ -62,13 +72,13 @@ public class Resolver {
} }
} }
public static List<Result> fromHardCoded(String hostname, int port) { public static Result fromHardCoded(String hostname, int port) {
Result result = new Result(); final Result ipResult = fromIpAddress(domain, port);
result.hostname = DNSName.from(hostname); if (ipResult != null) {
result.port = port; ipResult.call();
result.directTls = useDirectTls(port); return ipResult;
result.authenticated = true; }
return Collections.singletonList(result); return happyEyeball(resolveNoSrvRecords(DNSName.from(hostname), true));
} }
@ -76,10 +86,11 @@ public class Resolver {
return port == 443 || port == 5223; return port == 443 || port == 5223;
} }
public static List<Result> resolve(String domain) { public static Result resolve(String domain) {
final List<Result> ipResults = fromIpAddress(domain); final Result ipResult = fromIpAddress(domain);
if (ipResults.size() > 0) { if (ipResult != null) {
return ipResults; ipResult.call();
return ipResult;
} }
final List<Result> results = new ArrayList<>(); final List<Result> results = new ArrayList<>();
final List<Result> fallbackResults = new ArrayList<>(); final List<Result> fallbackResults = new ArrayList<>();
@ -121,63 +132,110 @@ public class Resolver {
synchronized (results) { synchronized (results) {
Collections.sort(results); Collections.sort(results);
Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": " + results.toString()); Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": " + results.toString());
return new ArrayList<>(results); return happyEyeball(results);
} }
} else { } else {
threads[2].join(); threads[2].join();
synchronized (fallbackResults) { synchronized (fallbackResults) {
Collections.sort(fallbackResults); Collections.sort(fallbackResults);
Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": " + fallbackResults.toString()); Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": " + fallbackResults.toString());
return new ArrayList<>(fallbackResults); return happyEyeball(fallbackResults);
} }
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
for (Thread thread : threads) { for (Thread thread : threads) {
thread.interrupt(); thread.interrupt();
} }
return Collections.emptyList(); return null;
} }
} }
private static List<Result> fromIpAddress(String domain) { private static Result fromIpAddress(String domain) {
if (!IP.matches(domain)) { return fromIpAddress(domain, DEFAULT_PORT_XMPP);
return Collections.emptyList();
} }
return Collections.singletonList(Result.createDefault(DNSName.from(domain))); private static Result fromIpAddress(String domain, int port) {
if (IP.matches(domain)) {
Result result = new Result(InetAddress.getByName(domain), port);
result.authenticated = true;
return result;
}
return null;
} }
private static List<Result> resolveSrv(String domain, final boolean directTls) throws IOException { private static List<Result> resolveSrv(String domain, final boolean directTls) throws IOException {
DNSName dnsName = DNSName.from((directTls ? DIRECT_TLS_SERVICE : STARTTLS_SERVICE) + "._tcp." + domain); DNSName dnsName = DNSName.from((directTls ? DIRECT_TLS_SERVICE : STARTTLS_SERICE) + "._tcp." + domain);
ResolverResult<SRV> result = resolveWithFallback(dnsName, SRV.class); ResolverResult<SRV> result = resolveWithFallback(dnsName, SRV.class);
final List<Result> results = new ArrayList<>(); final List<Result> results = new ArrayList<>();
final List<Thread> threads = new ArrayList<>();
for (SRV record : result.getAnswersOrEmptySet()) { for (SRV record : result.getAnswersOrEmptySet()) {
if (record.name.length() == 0 && record.priority == 0) { if (record.name.length() == 0 && record.priority == 0) {
continue; continue;
} }
Result resolverResult = Result.fromRecord(record, directTls); threads.add(new Thread(() -> {
resolverResult.authenticated = result.isAuthenticData(); final List<Result> ipv4s = resolveIp(record, A.class, result.isAuthenticData(), directTls);
results.add(resolverResult); synchronized (results) {
results.addAll(ipv4s);
} }
}));
threads.add(new Thread(() -> {
final List<Result> ipv6s = resolveIp(record, AAAA.class, result.isAuthenticData(), directTls);
synchronized (results) {
results.addAll(ipv6s);
}
}));
}
for (Thread thread : threads) {
thread.start();
}
for (Thread thread : threads) {
try {
thread.join();
} catch (InterruptedException e) {
return Collections.emptyList();
}
}
return results; return results;
} }
private static List<Result> resolveNoSrvRecords(DNSName dnsName, boolean withCnames) { private static <D extends InternetAddressRR> List<Result> resolveIp(SRV srv, Class<D> type, boolean authenticated, boolean directTls) {
List<Result> results = new ArrayList<>(); List<Result> list = new ArrayList<>();
Boolean resolveCNAME = false;
try { try {
if (withCnames) { ResolverResult<D> results = resolveWithFallback(srv.name, type, authenticated);
for (D record : results.getAnswersOrEmptySet()) {
Result resolverResult = new Result(record.getInetAddress(), srv.port, srv.priority);
resolverResult.authenticated = results.isAuthenticData() && authenticated;
resolverResult.directTls = directTls;
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, boolean withCnames) {
return resolveNoSrvRecords(dnsName, DEFAULT_PORT_XMPP, withCnames);
}
private static List<Result> resolveNoSrvRecords(DNSName dnsName, int port, boolean withCnames) {
List<Result> results = new ArrayList<>();
try {
for (A a : resolveWithFallback(dnsName, A.class, false).getAnswersOrEmptySet()) {
results.add(new Result(a.getInetAddress(), port));
}
for (AAAA aaaa : resolveWithFallback(dnsName, AAAA.class, false).getAnswersOrEmptySet()) {
results.add(new Result(aaaa.getInetAddress(), port));
}
if (results.size() == 0 && withCnames) {
for (CNAME cname : resolveWithFallback(dnsName, CNAME.class, false).getAnswersOrEmptySet()) { for (CNAME cname : resolveWithFallback(dnsName, CNAME.class, false).getAnswersOrEmptySet()) {
results.addAll(resolveNoSrvRecords(cname.name, false)); results.addAll(resolveNoSrvRecords(cname.name, port, false));
resolveCNAME = true; resolveCNAME = true;
} }
} }
} catch (Throwable throwable) { } catch (Throwable throwable) {
Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + "error resolving fallback records", throwable); Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + "error resolving fallback records", throwable);
} }
if(!resolveCNAME) {
results.add(Result.createDefault(dnsName));
}
return results; return results;
} }
@ -202,47 +260,54 @@ public class Resolver {
return ResolverApi.INSTANCE.resolve(question); return ResolverApi.INSTANCE.resolve(question);
} }
private static Result happyEyeball(List<Result> r) {
Result result;
ExecutorService executor = (ExecutorService) Executors.newCachedThreadPool();
try {
result = executor.invokeAny(r);
executor.shutdown();
return result;
}
catch (InterruptedException e) {
return null;
}
catch (ExecutionException e) {
return null;
}
}
private static boolean validateHostname() { private static boolean validateHostname() {
return SERVICE != null && SERVICE.getBooleanPreference("validate_hostname", R.bool.validate_hostname); return SERVICE != null && SERVICE.getBooleanPreference("validate_hostname", R.bool.validate_hostname);
} }
public static class Result implements Comparable<Result> { public static class Result implements Comparable<Result>, Callable<Result> {
public static final String DOMAIN = "domain"; public static final String IP = "ip";
public static final String HOSTNAME = "hostname";
public static final String PORT = "port"; public static final String PORT = "port";
public static final String PRIORITY = "priority"; public static final String PRIORITY = "priority";
public static final String DIRECT_TLS = "directTls"; public static final String DIRECT_TLS = "directTls";
public static final String AUTHENTICATED = "authenticated"; public static final String AUTHENTICATED = "authenticated";
private DNSName hostname; private InetAddress ip;
private int port = DEFAULT_PORT_XMPP; private int port = DEFAULT_PORT_XMPP;
private boolean directTls = false; private boolean directTls = false;
private boolean authenticated = false; private boolean authenticated = false;
private int priority; private int priority;
private Socket socket;
static Result fromRecord(SRV srv, boolean directTls) { public Result(InetAddress ip) {
Result result = new Result(); return Result(ip, DEFAULT_PORT_XMPP);
result.port = srv.port;
result.hostname = srv.name;
result.directTls = directTls;
result.priority = srv.priority;
return result;
} }
static Result createDefault(DNSName hostname) { public Result(InetAddress ip, int port) {
Result result = new Result(); return Result(ip, port, 0);
result.port = DEFAULT_PORT_XMPP;
result.hostname = hostname;
return result;
} }
public static Result fromCursor(Cursor cursor) { public Result(InetAddress ip, int port, int priority) {
final Result result = new Result(); this.ip = ip;
final String hostname = cursor.getString(cursor.getColumnIndex(HOSTNAME)); this.port = port;
result.hostname = hostname == null ? null : DNSName.from(hostname); this.directTls = useDirectTls(port);
result.port = cursor.getInt(cursor.getColumnIndex(PORT)); this.priority = priority;
result.priority = cursor.getInt(cursor.getColumnIndex(PRIORITY));
result.authenticated = cursor.getInt(cursor.getColumnIndex(AUTHENTICATED)) > 0;
result.directTls = cursor.getInt(cursor.getColumnIndex(DIRECT_TLS)) > 0;
return result; return result;
} }
@ -257,12 +322,12 @@ public class Resolver {
if (directTls != result.directTls) return false; if (directTls != result.directTls) return false;
if (authenticated != result.authenticated) return false; if (authenticated != result.authenticated) return false;
if (priority != result.priority) return false; if (priority != result.priority) return false;
return hostname != null ? hostname.equals(result.hostname) : result.hostname == null; return ip != null ? ip.equals(result.ip) : result.ip == null;
} }
@Override @Override
public int hashCode() { public int hashCode() {
int result = hostname != null ? hostname.hashCode() : 0; int result = ip != null ? ip.hashCode() : 0;
result = 31 * result + port; result = 31 * result + port;
result = 31 * result + (directTls ? 1 : 0); result = 31 * result + (directTls ? 1 : 0);
result = 31 * result + (authenticated ? 1 : 0); result = 31 * result + (authenticated ? 1 : 0);
@ -274,10 +339,6 @@ public class Resolver {
return port; return port;
} }
public DNSName getHostname() {
return hostname;
}
public boolean isDirectTls() { public boolean isDirectTls() {
return directTls; return directTls;
} }
@ -286,10 +347,14 @@ public class Resolver {
return authenticated; return authenticated;
} }
public Socket getSocket() {
return socket;
}
@Override @Override
public String toString() { public String toString() {
return "Result{" + return "Result{" +
", hostame='" + hostname.toString() + '\'' + "ip='" + (ip == null ? null : ip.getHostAddress()) + '\'' +
", port=" + port + ", port=" + port +
", directTls=" + directTls + ", directTls=" + directTls +
", authenticated=" + authenticated + ", authenticated=" + authenticated +
@ -309,10 +374,21 @@ public class Resolver {
return priority - result.priority; return priority - result.priority;
} }
} }
@Override
public Result call() throws Exception {
final InetSocketAddress addr = new InetSocketAddress(ip, port);
final Socket localSocket = new Socket();
localSocket.connect(result.getSocketAddress(), Config.SOCKET_TIMEOUT * 1000);
if(localSocket.isConnected()) {
this.socket = localSocket;
return this;
}
return null;
}
public ContentValues toContentValues() { public ContentValues toContentValues() {
final ContentValues contentValues = new ContentValues(); final ContentValues contentValues = new ContentValues();
contentValues.put(HOSTNAME, hostname == null ? null : hostname.toString()); contentValues.put(IP, ip == null ? null : ip.getAddress());
contentValues.put(PORT, port); contentValues.put(PORT, port);
contentValues.put(PRIORITY, priority); contentValues.put(PRIORITY, priority);
contentValues.put(DIRECT_TLS, directTls ? 1 : 0); contentValues.put(DIRECT_TLS, directTls ? 1 : 0);

View File

@ -290,39 +290,26 @@ public class XmppConnection implements Runnable {
} }
} else { } else {
final String domain = account.getJid().getDomain(); final String domain = account.getJid().getDomain();
final List<Resolver.Result> results; final Resolver.Result result;
final boolean hardcoded = extended && !account.getHostname().isEmpty(); final boolean hardcoded = extended && !account.getHostname().isEmpty();
if (hardcoded) { if (hardcoded) {
results = Resolver.fromHardCoded(account.getHostname(), account.getPort()); result = Resolver.fromHardCoded(account.getHostname(), account.getPort());
} else { } else {
results = Resolver.resolve(domain); result = Resolver.resolve(domain);
} }
if (Thread.currentThread().isInterrupted()) { if (Thread.currentThread().isInterrupted()) {
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": Thread was interrupted"); Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": Thread was interrupted");
return; return;
} }
if (results.size() == 0) {
Log.e(Config.LOGTAG,account.getJid().asBareJid()+": Resolver results were empty");
return;
}
for (Iterator<Resolver.Result> iterator = results.iterator(); iterator.hasNext(); ) {
final Resolver.Result result = iterator.next();
if (Thread.currentThread().isInterrupted()) {
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": Thread was interrupted");
return;
}
try {
// if tls is true, encryption is implied and must not be started // if tls is true, encryption is implied and must not be started
features.encryptionEnabled = result.isDirectTls(); features.encryptionEnabled = result.isDirectTls();
verifiedHostname = result.isAuthenticated() ? result.getHostname().toString() : null; verifiedHostname = result.isAuthenticated() ? result.getHostname().toString() : null;
Log.d(Config.LOGTAG,"verified hostname "+verifiedHostname); Log.d(Config.LOGTAG,"verified hostname "+verifiedHostname);
final InetSocketAddress addr = new InetSocketAddress(IDN.toASCII(result.getHostname().toString()), result.getPort());
Log.d(Config.LOGTAG, account.getJid().asBareJid().toString() Log.d(Config.LOGTAG, account.getJid().asBareJid().toString()
+ ": using values from resolver " + ": using values from resolver "
+ result.getHostname().toString() + ":" + result.getPort() + " tls: " + features.encryptionEnabled); + result.getHostname().toString() + ":" + result.getPort() + " tls: " + features.encryptionEnabled);
localSocket = new Socket(); localSocket = result.getSocket();
localSocket.connect(addr, Config.SOCKET_TIMEOUT * 1000);
if (features.encryptionEnabled) { if (features.encryptionEnabled) {
localSocket = upgradeSocketToTls(localSocket); localSocket = upgradeSocketToTls(localSocket);
@ -336,20 +323,6 @@ public class XmppConnection implements Runnable {
FileBackend.close(localSocket); FileBackend.close(localSocket);
throw new StateChangingException(Account.State.STREAM_OPENING_ERROR); throw new StateChangingException(Account.State.STREAM_OPENING_ERROR);
} }
} catch (final StateChangingException e) {
if (!iterator.hasNext()) {
throw e;
}
} catch (InterruptedException e) {
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": thread was interrupted before beginning stream");
return;
} catch (final Throwable e) {
Log.d(Config.LOGTAG, account.getJid().asBareJid().toString() + ": " + e.getMessage() + "(" + e.getClass().getName() + ")");
if (!iterator.hasNext()) {
throw new UnknownHostException();
}
}
}
} }
processStream(); processStream();
} catch (final SecurityException e) { } catch (final SecurityException e) {