mirror of https://github.com/M66B/FairEmail.git
Lookup MX record for email domains
This commit is contained in:
parent
d9d601ef4a
commit
ab34587e3f
|
@ -3742,7 +3742,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
|
|||
@Override
|
||||
protected String[] onExecute(Context context, Bundle args) throws Throwable {
|
||||
Uri uri = args.getParcelable("uri");
|
||||
return IPInfo.getOrganization(uri);
|
||||
return IPInfo.getOrganization(uri, context);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -14,6 +14,8 @@ import android.telephony.TelephonyManager;
|
|||
import androidx.preference.PreferenceManager;
|
||||
|
||||
import org.xbill.DNS.Lookup;
|
||||
import org.xbill.DNS.MXRecord;
|
||||
import org.xbill.DNS.Record;
|
||||
import org.xbill.DNS.SimpleResolver;
|
||||
import org.xbill.DNS.Type;
|
||||
|
||||
|
@ -318,6 +320,28 @@ public class ConnectionHelper {
|
|||
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) {
|
||||
String domain1 = getDomain(host1);
|
||||
String domain2 = getDomain(host2);
|
||||
|
|
|
@ -19,6 +19,7 @@ package eu.faircode.email;
|
|||
Copyright 2018-2019 by Marcel Bokhorst (M66B)
|
||||
*/
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.MailTo;
|
||||
import android.net.ParseException;
|
||||
import android.net.Uri;
|
||||
|
@ -35,30 +36,33 @@ import java.util.Map;
|
|||
import javax.net.ssl.HttpsURLConnection;
|
||||
|
||||
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())) {
|
||||
MailTo email = MailTo.parse(uri.toString());
|
||||
String to = email.getTo();
|
||||
if (to == null || !to.contains("@"))
|
||||
throw new UnknownHostException();
|
||||
String host = to.substring(to.indexOf('@') + 1);
|
||||
return getOrganization(host);
|
||||
String domain = to.substring(to.indexOf('@') + 1);
|
||||
InetAddress address = ConnectionHelper.lookupMx(domain, context);
|
||||
if (address == null)
|
||||
throw new UnknownHostException();
|
||||
return new String[]{domain, getOrganization(address)};
|
||||
} else {
|
||||
String host = uri.getHost();
|
||||
if (host == null)
|
||||
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) {
|
||||
if (hostOrganization.containsKey(host))
|
||||
return new String[]{host, hostOrganization.get(host)};
|
||||
if (hostOrganization.containsKey(address))
|
||||
return hostOrganization.get(address);
|
||||
}
|
||||
InetAddress address = InetAddress.getByName(host);
|
||||
URL url = new URL("https://ipinfo.io/" + address.getHostAddress() + "/org");
|
||||
Log.i("GET " + url);
|
||||
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
|
||||
|
@ -70,9 +74,9 @@ public class IPInfo {
|
|||
if ("undefined".equals(organization))
|
||||
organization = null;
|
||||
synchronized (hostOrganization) {
|
||||
hostOrganization.put(host, organization);
|
||||
hostOrganization.put(address, organization);
|
||||
}
|
||||
return new String[]{host, organization};
|
||||
return organization;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue