Files
FairEmail/app/src/main/java/eu/faircode/email/IPInfo.java

100 lines
3.5 KiB
Java
Raw Normal View History

2019-05-12 18:46:26 +02:00
package eu.faircode.email;
2019-05-18 09:35:01 +02:00
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2021-01-01 08:56:36 +01:00
Copyright 2018-2021 by Marcel Bokhorst (M66B)
2019-05-18 09:35:01 +02:00
*/
2019-09-08 13:22:44 +02:00
import android.content.Context;
import android.net.ParseException;
import android.net.Uri;
2020-04-07 16:06:12 +02:00
import android.util.Pair;
2020-11-14 14:27:53 +01:00
import androidx.annotation.NonNull;
import androidx.core.net.MailTo;
2019-05-12 18:46:26 +02:00
import java.io.IOException;
import java.net.InetAddress;
import java.net.URL;
2019-06-07 07:51:08 +02:00
import java.net.UnknownHostException;
2019-05-12 18:46:26 +02:00
import java.util.HashMap;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
public class IPInfo {
2020-04-07 16:06:12 +02:00
private static Map<InetAddress, Organization> addressOrganization = new HashMap<>();
2019-05-12 18:46:26 +02:00
2020-01-17 16:59:29 +01:00
private final static int FETCH_TIMEOUT = 15 * 1000; // milliseconds
2020-11-14 14:27:53 +01:00
static Pair<String, Organization> getOrganization(@NonNull Uri uri, Context context) throws IOException, ParseException {
if ("mailto".equalsIgnoreCase(uri.getScheme())) {
MailTo email = MailTo.parse(uri.toString());
String to = email.getTo();
if (to == null || !to.contains("@"))
2019-06-07 07:51:08 +02:00
throw new UnknownHostException();
2019-09-08 13:22:44 +02:00
String domain = to.substring(to.indexOf('@') + 1);
2020-04-16 17:39:29 +02:00
InetAddress address = DnsHelper.lookupMx(context, domain);
2019-09-08 13:22:44 +02:00
if (address == null)
throw new UnknownHostException();
2020-04-07 16:06:12 +02:00
return new Pair<>(domain, getOrganization(address));
} else {
String host = uri.getHost();
if (host == null)
2019-06-07 07:51:08 +02:00
throw new UnknownHostException();
2019-09-08 13:22:44 +02:00
InetAddress address = InetAddress.getByName(host);
2020-04-07 16:06:12 +02:00
return new Pair<>(host, getOrganization(address));
}
}
2020-04-07 16:06:12 +02:00
private static Organization getOrganization(InetAddress address) throws IOException {
synchronized (addressOrganization) {
if (addressOrganization.containsKey(address))
return addressOrganization.get(address);
2019-05-12 18:46:26 +02:00
}
2020-04-07 16:06:12 +02:00
// https://ipinfo.io/developers
URL url = new URL("https://ipinfo.io/" + address.getHostAddress() + "/org");
2019-05-12 18:46:26 +02:00
Log.i("GET " + url);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET");
2020-01-17 16:59:29 +01:00
connection.setReadTimeout(FETCH_TIMEOUT);
2020-06-30 11:50:28 +02:00
connection.setConnectTimeout(FETCH_TIMEOUT);
2019-05-12 18:46:26 +02:00
connection.connect();
2020-04-07 16:06:12 +02:00
Organization organization = new Organization();
try {
2021-01-20 20:00:44 +01:00
String response = Helper.readStream(connection.getInputStream());
organization.name = response.trim();
if ("".equals(organization.name) || "undefined".equals(organization.name))
organization.name = null;
2020-04-07 16:06:12 +02:00
} finally {
connection.disconnect();
}
synchronized (addressOrganization) {
addressOrganization.put(address, organization);
2019-05-12 18:46:26 +02:00
}
2020-04-07 16:06:12 +02:00
return organization;
}
static class Organization {
String name;
2019-05-12 18:46:26 +02:00
}
}