mirror of https://github.com/M66B/FairEmail.git
Get network DNS server
This commit is contained in:
parent
c142c44055
commit
7c437d2a2d
|
@ -3,6 +3,7 @@ package eu.faircode.email;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.net.ConnectivityManager;
|
import android.net.ConnectivityManager;
|
||||||
|
import android.net.LinkProperties;
|
||||||
import android.net.Network;
|
import android.net.Network;
|
||||||
import android.net.NetworkCapabilities;
|
import android.net.NetworkCapabilities;
|
||||||
import android.net.NetworkInfo;
|
import android.net.NetworkInfo;
|
||||||
|
@ -16,6 +17,7 @@ import com.bugsnag.android.BreadcrumbType;
|
||||||
import com.bugsnag.android.Bugsnag;
|
import com.bugsnag.android.Bugsnag;
|
||||||
import com.sun.mail.imap.IMAPStore;
|
import com.sun.mail.imap.IMAPStore;
|
||||||
|
|
||||||
|
import java.net.InetAddress;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
@ -26,6 +28,11 @@ import java.util.Map;
|
||||||
import javax.mail.MessagingException;
|
import javax.mail.MessagingException;
|
||||||
|
|
||||||
public class ConnectionHelper {
|
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
|
// Roam like at home
|
||||||
// https://en.wikipedia.org/wiki/European_Union_roaming_regulations
|
// https://en.wikipedia.org/wiki/European_Union_roaming_regulations
|
||||||
private static final List<String> RLAH_COUNTRY_CODES = Collections.unmodifiableList(Arrays.asList(
|
private static final List<String> RLAH_COUNTRY_CODES = Collections.unmodifiableList(Arrays.asList(
|
||||||
|
@ -62,9 +69,6 @@ public class ConnectionHelper {
|
||||||
"GB" // United Kingdom
|
"GB" // United Kingdom
|
||||||
));
|
));
|
||||||
|
|
||||||
static final int AUTH_TYPE_PASSWORD = 1;
|
|
||||||
static final int AUTH_TYPE_GMAIL = 2;
|
|
||||||
|
|
||||||
static class NetworkState {
|
static class NetworkState {
|
||||||
private Boolean connected = null;
|
private Boolean connected = null;
|
||||||
private Boolean suitable = null;
|
private Boolean suitable = null;
|
||||||
|
@ -269,4 +273,23 @@ public class ConnectionHelper {
|
||||||
return Settings.System.getInt(context.getContentResolver(),
|
return Settings.System.getInt(context.getContentResolver(),
|
||||||
Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
|
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<InetAddress> dns = props.getDnsServers();
|
||||||
|
if (dns.size() == 0)
|
||||||
|
return DEFAULT_DNS;
|
||||||
|
else
|
||||||
|
return dns.get(0).getHostAddress();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -167,17 +167,17 @@ public class EmailProvider {
|
||||||
private static EmailProvider fromDomainInternal(Context context, String domain) throws IOException {
|
private static EmailProvider fromDomainInternal(Context context, String domain) throws IOException {
|
||||||
try {
|
try {
|
||||||
Log.i("Provider from DNS domain=" + domain);
|
Log.i("Provider from DNS domain=" + domain);
|
||||||
return addSpecials(context, fromDNS(domain));
|
return addSpecials(context, fromDNS(context, domain));
|
||||||
} catch (Throwable ex) {
|
} catch (Throwable ex) {
|
||||||
Log.w(ex);
|
Log.w(ex);
|
||||||
try {
|
try {
|
||||||
Log.i("Provider from ISPDB domain=" + domain);
|
Log.i("Provider from ISPDB domain=" + domain);
|
||||||
return addSpecials(context, fromISPDB(domain));
|
return addSpecials(context, fromISPDB(context, domain));
|
||||||
} catch (Throwable ex1) {
|
} catch (Throwable ex1) {
|
||||||
Log.w(ex1);
|
Log.w(ex1);
|
||||||
try {
|
try {
|
||||||
Log.i("Provider from template domain=" + domain);
|
Log.i("Provider from template domain=" + domain);
|
||||||
return addSpecials(context, fromTemplate(domain));
|
return addSpecials(context, fromTemplate(context, domain));
|
||||||
} catch (Throwable ex2) {
|
} catch (Throwable ex2) {
|
||||||
Log.w(ex2);
|
Log.w(ex2);
|
||||||
throw new UnknownHostException(context.getString(R.string.title_setup_no_settings, domain));
|
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);
|
EmailProvider provider = new EmailProvider(domain);
|
||||||
|
|
||||||
// https://wiki.mozilla.org/Thunderbird:Autoconfiguration:ConfigFileFormat
|
// https://wiki.mozilla.org/Thunderbird:Autoconfiguration:ConfigFileFormat
|
||||||
|
@ -366,10 +366,10 @@ public class EmailProvider {
|
||||||
return provider;
|
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
|
// https://tools.ietf.org/html/rfc6186
|
||||||
SRVRecord imap = lookup("_imaps._tcp." + domain);
|
SRVRecord imap = lookup(context, "_imaps._tcp." + domain);
|
||||||
SRVRecord smtp = lookup("_submission._tcp." + domain);
|
SRVRecord smtp = lookup(context, "_submission._tcp." + domain);
|
||||||
|
|
||||||
EmailProvider provider = new EmailProvider(domain);
|
EmailProvider provider = new EmailProvider(domain);
|
||||||
provider.imap_host = imap.getTarget().toString(true);
|
provider.imap_host = imap.getTarget().toString(true);
|
||||||
|
@ -383,7 +383,7 @@ public class EmailProvider {
|
||||||
return provider;
|
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))
|
if (checkTemplate(domain, null, 993, null, 587))
|
||||||
return new EmailProvider(domain, domain, null, null);
|
return new EmailProvider(domain, domain, null, null);
|
||||||
|
|
||||||
|
@ -403,7 +403,7 @@ public class EmailProvider {
|
||||||
isHostReachable((smtp_prefix == null ? "" : smtp_prefix + ".") + domain, smtp_port, 5000);
|
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);
|
Log.i("Checking " + host + ":" + port);
|
||||||
try (Socket socket = new Socket()) {
|
try (Socket socket = new Socket()) {
|
||||||
InetAddress iaddr = InetAddress.getByName(host);
|
InetAddress iaddr = InetAddress.getByName(host);
|
||||||
|
@ -439,24 +439,26 @@ public class EmailProvider {
|
||||||
return provider;
|
return provider;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static SRVRecord lookup(String dns) throws TextParseException, UnknownHostException {
|
private static SRVRecord lookup(Context context, String record) throws TextParseException, UnknownHostException {
|
||||||
Lookup lookup = new Lookup(dns, Type.SRV);
|
Lookup lookup = new Lookup(record, Type.SRV);
|
||||||
|
|
||||||
// https://dns.watch/ 84.200.69.80
|
// https://dns.watch/ 84.200.69.80
|
||||||
SimpleResolver resolver = new SimpleResolver(Helper.DEFAULT_DNS);
|
SimpleResolver resolver = new SimpleResolver(ConnectionHelper.getDnsServer(context));
|
||||||
lookup.setResolver(resolver);
|
lookup.setResolver(resolver);
|
||||||
Log.i("Lookup dns=" + dns + " @" + resolver.getAddress());
|
Log.i("Lookup record=" + record + " @" + resolver.getAddress());
|
||||||
Record[] records = lookup.run();
|
Record[] records = lookup.run();
|
||||||
|
|
||||||
if (lookup.getResult() != Lookup.SUCCESSFUL)
|
if (lookup.getResult() != Lookup.SUCCESSFUL)
|
||||||
if (lookup.getResult() == Lookup.HOST_NOT_FOUND ||
|
if (lookup.getResult() == Lookup.HOST_NOT_FOUND ||
|
||||||
lookup.getResult() == Lookup.TYPE_NOT_FOUND)
|
lookup.getResult() == Lookup.TYPE_NOT_FOUND)
|
||||||
throw new UnknownHostException(dns);
|
throw new UnknownHostException(record);
|
||||||
else
|
else
|
||||||
throw new IllegalArgumentException(lookup.getErrorString());
|
throw new IllegalArgumentException(lookup.getErrorString());
|
||||||
|
|
||||||
Log.i("Found dns=" + (records == null ? -1 : records.length));
|
SRVRecord result = (records == null || records.length == 0 ? null : (SRVRecord) records[0]);
|
||||||
return (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
|
@Override
|
||||||
|
|
|
@ -2769,14 +2769,16 @@ public class FragmentCompose extends FragmentBase {
|
||||||
|
|
||||||
String domain = email.split("@")[1];
|
String domain = email.split("@")[1];
|
||||||
Lookup lookup = new Lookup(domain, Type.MX);
|
Lookup lookup = new Lookup(domain, Type.MX);
|
||||||
SimpleResolver resolver = new SimpleResolver(Helper.DEFAULT_DNS);
|
SimpleResolver resolver = new SimpleResolver(ConnectionHelper.getDnsServer(context));
|
||||||
lookup.setResolver(resolver);
|
lookup.setResolver(resolver);
|
||||||
Log.i("Lookup dns=" + domain + " @" + resolver.getAddress());
|
Log.i("Lookup MX=" + domain + " @" + resolver.getAddress());
|
||||||
|
|
||||||
lookup.run();
|
lookup.run();
|
||||||
if (lookup.getResult() == Lookup.HOST_NOT_FOUND ||
|
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));
|
throw new IllegalArgumentException(context.getString(R.string.title_no_server, domain));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getActionName(int id) {
|
private String getActionName(int id) {
|
||||||
|
|
|
@ -114,8 +114,6 @@ public class Helper {
|
||||||
|
|
||||||
static final float LOW_LIGHT = 0.6f;
|
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 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";
|
static final String XDA_URI = "https://forum.xda-developers.com/android/apps-games/source-email-t3824168";
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue