Debug: prefer trusted hosts

pull/199/head
M66B 4 years ago
parent c0953d25d5
commit 1c71f3caf6

@ -40,6 +40,8 @@ import java.net.InetSocketAddress;
import java.net.Socket; import java.net.Socket;
import java.net.URL; import java.net.URL;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.text.Collator; import java.text.Collator;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -52,6 +54,9 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
public class EmailProvider implements Parcelable { public class EmailProvider implements Parcelable {
public String id; public String id;
public String name; public String name;
@ -610,11 +615,20 @@ public class EmailProvider implements Parcelable {
imaps.add(new Server(context, domain, "mx", 143)); imaps.add(new Server(context, domain, "mx", 143));
imaps.add(new Server(context, domain, null, 143)); imaps.add(new Server(context, domain, null, 143));
for (Server server : imaps) Server untrusted = null;
if (server.reachable.get()) { for (Server server : imaps) {
Boolean result = server.reachable.get();
if (result == null) {
if (untrusted == null)
untrusted = server;
} else if (result) {
imap = server; imap = server;
break; break;
} }
}
if (imap == null)
imap = untrusted;
if (imap == null) if (imap == null)
throw new UnknownHostException(domain + " template"); throw new UnknownHostException(domain + " template");
@ -634,11 +648,20 @@ public class EmailProvider implements Parcelable {
smtps.add(new Server(context, domain, "mx", 465)); smtps.add(new Server(context, domain, "mx", 465));
smtps.add(new Server(context, domain, null, 465)); smtps.add(new Server(context, domain, null, 465));
for (Server server : smtps) Server untrusted = null;
if (server.reachable.get()) { for (Server server : smtps) {
Boolean result = server.reachable.get();
if (result == null) {
if (untrusted == null)
untrusted = server;
} else if (result) {
smtp = server; smtp = server;
break; break;
} }
}
if (smtp == null)
smtp = untrusted;
if (smtp == null) if (smtp == null)
throw new UnknownHostException(domain + " template"); throw new UnknownHostException(domain + " template");
@ -768,6 +791,36 @@ public class EmailProvider implements Parcelable {
try { try {
for (InetAddress iaddr : InetAddress.getAllByName(host)) { for (InetAddress iaddr : InetAddress.getAllByName(host)) {
InetSocketAddress address = new InetSocketAddress(iaddr, Server.this.port); InetSocketAddress address = new InetSocketAddress(iaddr, Server.this.port);
if (BuildConfig.DEBUG) {
SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
try (SSLSocket socket = (SSLSocket) factory.createSocket()) {
Log.i("SSL connecting to " + address);
socket.setSoTimeout(SCAN_TIMEOUT);
socket.connect(address);
try {
socket.startHandshake();
Log.i("SSL connected " + address);
Certificate[] certs = socket.getSession().getPeerCertificates();
for (Certificate cert : certs)
if (cert instanceof X509Certificate) {
List<String> names = ConnectionHelper.getDnsNames((X509Certificate) cert);
if (ConnectionHelper.matches(host, names))
return true;
}
Log.i("SSL untrusted " + address);
return null;
} catch (Throwable ex) {
// Port 143
// javax.net.ssl.SSLException: Unable to parse TLS packet header
Log.i("SSL handshake " + address + ": " + Log.formatThrowable(ex));
return true;
}
} catch (Throwable ex) {
Log.i("SSL unreachable " + address + ": " + Log.formatThrowable(ex));
}
}
try (Socket socket = new Socket()) { try (Socket socket = new Socket()) {
Log.i("Connecting to " + address); Log.i("Connecting to " + address);
socket.connect(address, SCAN_TIMEOUT); socket.connect(address, SCAN_TIMEOUT);

Loading…
Cancel
Save