Lookup MX record for email domains

pull/162/head
M66B 6 years ago
parent d9d601ef4a
commit ab34587e3f

@ -3742,7 +3742,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
@Override @Override
protected String[] onExecute(Context context, Bundle args) throws Throwable { protected String[] onExecute(Context context, Bundle args) throws Throwable {
Uri uri = args.getParcelable("uri"); Uri uri = args.getParcelable("uri");
return IPInfo.getOrganization(uri); return IPInfo.getOrganization(uri, context);
} }
@Override @Override

@ -14,6 +14,8 @@ import android.telephony.TelephonyManager;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import org.xbill.DNS.Lookup; import org.xbill.DNS.Lookup;
import org.xbill.DNS.MXRecord;
import org.xbill.DNS.Record;
import org.xbill.DNS.SimpleResolver; import org.xbill.DNS.SimpleResolver;
import org.xbill.DNS.Type; import org.xbill.DNS.Type;
@ -318,6 +320,28 @@ public class ConnectionHelper {
return ok; return ok;
} }
static InetAddress lookupMx(String domain, Context context) {
try {
Lookup lookup = new Lookup(domain, Type.MX);
SimpleResolver resolver = new SimpleResolver(getDnsServer(context));
lookup.setResolver(resolver);
Log.i("Lookup MX=" + domain + " @" + resolver.getAddress());
lookup.run();
if (lookup.getResult() == Lookup.SUCCESSFUL) {
Record[] answers = lookup.getAnswers();
if (answers != null && answers.length > 0 && answers[0] instanceof MXRecord) {
MXRecord mx = (MXRecord) answers[0];
return InetAddress.getByName(mx.getTarget().toString(true));
}
}
} catch (Throwable ex) {
Log.w(ex);
}
return null;
}
static boolean isSameDomain(String host1, String host2) { static boolean isSameDomain(String host1, String host2) {
String domain1 = getDomain(host1); String domain1 = getDomain(host1);
String domain2 = getDomain(host2); String domain2 = getDomain(host2);

@ -19,6 +19,7 @@ package eu.faircode.email;
Copyright 2018-2019 by Marcel Bokhorst (M66B) Copyright 2018-2019 by Marcel Bokhorst (M66B)
*/ */
import android.content.Context;
import android.net.MailTo; import android.net.MailTo;
import android.net.ParseException; import android.net.ParseException;
import android.net.Uri; import android.net.Uri;
@ -35,30 +36,33 @@ import java.util.Map;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
public class IPInfo { public class IPInfo {
private static Map<String, String> hostOrganization = new HashMap<>(); private static Map<InetAddress, String> hostOrganization = new HashMap<>();
static String[] getOrganization(Uri uri) throws IOException, ParseException { static String[] getOrganization(Uri uri, Context context) throws IOException, ParseException {
if ("mailto".equals(uri.getScheme())) { if ("mailto".equals(uri.getScheme())) {
MailTo email = MailTo.parse(uri.toString()); MailTo email = MailTo.parse(uri.toString());
String to = email.getTo(); String to = email.getTo();
if (to == null || !to.contains("@")) if (to == null || !to.contains("@"))
throw new UnknownHostException(); throw new UnknownHostException();
String host = to.substring(to.indexOf('@') + 1); String domain = to.substring(to.indexOf('@') + 1);
return getOrganization(host); InetAddress address = ConnectionHelper.lookupMx(domain, context);
if (address == null)
throw new UnknownHostException();
return new String[]{domain, getOrganization(address)};
} else { } else {
String host = uri.getHost(); String host = uri.getHost();
if (host == null) if (host == null)
throw new UnknownHostException(); throw new UnknownHostException();
return getOrganization(host); InetAddress address = InetAddress.getByName(host);
return new String[]{host, getOrganization(address)};
} }
} }
private static String[] getOrganization(String host) throws IOException { private static String getOrganization(InetAddress address) throws IOException {
synchronized (hostOrganization) { synchronized (hostOrganization) {
if (hostOrganization.containsKey(host)) if (hostOrganization.containsKey(address))
return new String[]{host, hostOrganization.get(host)}; return hostOrganization.get(address);
} }
InetAddress address = InetAddress.getByName(host);
URL url = new URL("https://ipinfo.io/" + address.getHostAddress() + "/org"); URL url = new URL("https://ipinfo.io/" + address.getHostAddress() + "/org");
Log.i("GET " + url); Log.i("GET " + url);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
@ -70,9 +74,9 @@ public class IPInfo {
if ("undefined".equals(organization)) if ("undefined".equals(organization))
organization = null; organization = null;
synchronized (hostOrganization) { synchronized (hostOrganization) {
hostOrganization.put(host, organization); hostOrganization.put(address, organization);
} }
return new String[]{host, organization}; return organization;
} }
} }
} }

Loading…
Cancel
Save