diff --git a/app/src/main/java/eu/faircode/email/ConnectionHelper.java b/app/src/main/java/eu/faircode/email/ConnectionHelper.java index a874a0f868..ea98077822 100644 --- a/app/src/main/java/eu/faircode/email/ConnectionHelper.java +++ b/app/src/main/java/eu/faircode/email/ConnectionHelper.java @@ -3,6 +3,7 @@ package eu.faircode.email; import android.content.Context; import android.content.SharedPreferences; import android.net.ConnectivityManager; +import android.net.LinkProperties; import android.net.Network; import android.net.NetworkCapabilities; import android.net.NetworkInfo; @@ -16,6 +17,7 @@ import com.bugsnag.android.BreadcrumbType; import com.bugsnag.android.Bugsnag; import com.sun.mail.imap.IMAPStore; +import java.net.InetAddress; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; @@ -26,6 +28,11 @@ import java.util.Map; import javax.mail.MessagingException; public class ConnectionHelper { + private static final String DEFAULT_DNS = "8.8.8.8"; + + static final int AUTH_TYPE_PASSWORD = 1; + static final int AUTH_TYPE_GMAIL = 2; + // Roam like at home // https://en.wikipedia.org/wiki/European_Union_roaming_regulations private static final List RLAH_COUNTRY_CODES = Collections.unmodifiableList(Arrays.asList( @@ -62,9 +69,6 @@ public class ConnectionHelper { "GB" // United Kingdom )); - static final int AUTH_TYPE_PASSWORD = 1; - static final int AUTH_TYPE_GMAIL = 2; - static class NetworkState { private Boolean connected = null; private Boolean suitable = null; @@ -269,4 +273,23 @@ public class ConnectionHelper { return Settings.System.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0) != 0; } + + static String getDnsServer(Context context) { + if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M) + return DEFAULT_DNS; + ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + if (cm == null) + return DEFAULT_DNS; + Network active = cm.getActiveNetwork(); + if (active == null) + return DEFAULT_DNS; + LinkProperties props = cm.getLinkProperties(active); + if (props == null) + return DEFAULT_DNS; + List dns = props.getDnsServers(); + if (dns.size() == 0) + return DEFAULT_DNS; + else + return dns.get(0).getHostAddress(); + } } diff --git a/app/src/main/java/eu/faircode/email/EmailProvider.java b/app/src/main/java/eu/faircode/email/EmailProvider.java index b911c2503a..f1914538ec 100644 --- a/app/src/main/java/eu/faircode/email/EmailProvider.java +++ b/app/src/main/java/eu/faircode/email/EmailProvider.java @@ -167,17 +167,17 @@ public class EmailProvider { private static EmailProvider fromDomainInternal(Context context, String domain) throws IOException { try { Log.i("Provider from DNS domain=" + domain); - return addSpecials(context, fromDNS(domain)); + return addSpecials(context, fromDNS(context, domain)); } catch (Throwable ex) { Log.w(ex); try { Log.i("Provider from ISPDB domain=" + domain); - return addSpecials(context, fromISPDB(domain)); + return addSpecials(context, fromISPDB(context, domain)); } catch (Throwable ex1) { Log.w(ex1); try { Log.i("Provider from template domain=" + domain); - return addSpecials(context, fromTemplate(domain)); + return addSpecials(context, fromTemplate(context, domain)); } catch (Throwable ex2) { Log.w(ex2); throw new UnknownHostException(context.getString(R.string.title_setup_no_settings, domain)); @@ -186,7 +186,7 @@ public class EmailProvider { } } - private static EmailProvider fromISPDB(String domain) throws IOException, XmlPullParserException { + private static EmailProvider fromISPDB(Context context, String domain) throws IOException, XmlPullParserException { EmailProvider provider = new EmailProvider(domain); // https://wiki.mozilla.org/Thunderbird:Autoconfiguration:ConfigFileFormat @@ -366,10 +366,10 @@ public class EmailProvider { return provider; } - private static EmailProvider fromDNS(String domain) throws TextParseException, UnknownHostException { + private static EmailProvider fromDNS(Context context, String domain) throws TextParseException, UnknownHostException { // https://tools.ietf.org/html/rfc6186 - SRVRecord imap = lookup("_imaps._tcp." + domain); - SRVRecord smtp = lookup("_submission._tcp." + domain); + SRVRecord imap = lookup(context, "_imaps._tcp." + domain); + SRVRecord smtp = lookup(context, "_submission._tcp." + domain); EmailProvider provider = new EmailProvider(domain); provider.imap_host = imap.getTarget().toString(true); @@ -383,7 +383,7 @@ public class EmailProvider { return provider; } - private static EmailProvider fromTemplate(String domain) throws UnknownHostException { + private static EmailProvider fromTemplate(Context context, String domain) throws UnknownHostException { if (checkTemplate(domain, null, 993, null, 587)) return new EmailProvider(domain, domain, null, null); @@ -403,7 +403,7 @@ public class EmailProvider { isHostReachable((smtp_prefix == null ? "" : smtp_prefix + ".") + domain, smtp_port, 5000); } - static boolean isHostReachable(String host, int port, int timeoutms) { + private static boolean isHostReachable(String host, int port, int timeoutms) { Log.i("Checking " + host + ":" + port); try (Socket socket = new Socket()) { InetAddress iaddr = InetAddress.getByName(host); @@ -439,24 +439,26 @@ public class EmailProvider { return provider; } - private static SRVRecord lookup(String dns) throws TextParseException, UnknownHostException { - Lookup lookup = new Lookup(dns, Type.SRV); + private static SRVRecord lookup(Context context, String record) throws TextParseException, UnknownHostException { + Lookup lookup = new Lookup(record, Type.SRV); // https://dns.watch/ 84.200.69.80 - SimpleResolver resolver = new SimpleResolver(Helper.DEFAULT_DNS); + SimpleResolver resolver = new SimpleResolver(ConnectionHelper.getDnsServer(context)); lookup.setResolver(resolver); - Log.i("Lookup dns=" + dns + " @" + resolver.getAddress()); + Log.i("Lookup record=" + record + " @" + resolver.getAddress()); Record[] records = lookup.run(); if (lookup.getResult() != Lookup.SUCCESSFUL) if (lookup.getResult() == Lookup.HOST_NOT_FOUND || lookup.getResult() == Lookup.TYPE_NOT_FOUND) - throw new UnknownHostException(dns); + throw new UnknownHostException(record); else throw new IllegalArgumentException(lookup.getErrorString()); - Log.i("Found dns=" + (records == null ? -1 : records.length)); - return (records == null || records.length == 0 ? null : (SRVRecord) records[0]); + SRVRecord result = (records == null || records.length == 0 ? null : (SRVRecord) records[0]); + Log.i("Found record=" + (records == null ? -1 : records.length) + + " result=" + (result == null ? "" : result.toString())); + return result; } @Override diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index 7a40f8ff64..01854bb1e6 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -2769,14 +2769,16 @@ public class FragmentCompose extends FragmentBase { String domain = email.split("@")[1]; Lookup lookup = new Lookup(domain, Type.MX); - SimpleResolver resolver = new SimpleResolver(Helper.DEFAULT_DNS); + SimpleResolver resolver = new SimpleResolver(ConnectionHelper.getDnsServer(context)); lookup.setResolver(resolver); - Log.i("Lookup dns=" + domain + " @" + resolver.getAddress()); + Log.i("Lookup MX=" + domain + " @" + resolver.getAddress()); lookup.run(); if (lookup.getResult() == Lookup.HOST_NOT_FOUND || - lookup.getResult() == Lookup.TYPE_NOT_FOUND) + lookup.getResult() == Lookup.TYPE_NOT_FOUND) { + Log.i("Lookup MX=" + domain + " result=" + lookup.getErrorString()); throw new IllegalArgumentException(context.getString(R.string.title_no_server, domain)); + } } private String getActionName(int id) { diff --git a/app/src/main/java/eu/faircode/email/Helper.java b/app/src/main/java/eu/faircode/email/Helper.java index 7d6192032b..62ea07570f 100644 --- a/app/src/main/java/eu/faircode/email/Helper.java +++ b/app/src/main/java/eu/faircode/email/Helper.java @@ -114,8 +114,6 @@ public class Helper { static final float LOW_LIGHT = 0.6f; - static final String DEFAULT_DNS = "8.8.8.8"; - static final String FAQ_URI = "https://github.com/M66B/FairEmail/blob/master/FAQ.md"; static final String XDA_URI = "https://forum.xda-developers.com/android/apps-games/source-email-t3824168";