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

106 lines
3.9 KiB
Java
Raw Normal View History

2019-05-12 16:46:26 +00:00
package eu.faircode.email;
2019-05-18 07:35:01 +00: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 07:56:36 +00:00
Copyright 2018-2021 by Marcel Bokhorst (M66B)
2019-05-18 07:35:01 +00:00
*/
2019-09-08 11:22:44 +00:00
import android.content.Context;
import android.net.ParseException;
import android.net.Uri;
2020-04-07 14:06:12 +00:00
import android.util.Pair;
2020-11-14 13:27:53 +00:00
import androidx.annotation.NonNull;
import androidx.core.net.MailTo;
2021-05-19 15:20:42 +00:00
import java.io.FileNotFoundException;
2019-05-12 16:46:26 +00:00
import java.io.IOException;
import java.net.InetAddress;
import java.net.URL;
2019-06-07 05:51:08 +00:00
import java.net.UnknownHostException;
2019-05-12 16:46:26 +00:00
import java.util.HashMap;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
public class IPInfo {
2020-04-07 14:06:12 +00:00
private static Map<InetAddress, Organization> addressOrganization = new HashMap<>();
2019-05-12 16:46:26 +00:00
2020-01-17 15:59:29 +00:00
private final static int FETCH_TIMEOUT = 15 * 1000; // milliseconds
2021-07-03 18:36:38 +00:00
static Pair<InetAddress, Organization> getOrganization(@NonNull Uri uri, Context context) throws IOException, ParseException {
2020-11-14 13:27:53 +00:00
if ("mailto".equalsIgnoreCase(uri.getScheme())) {
MailTo email = MailTo.parse(uri.toString());
String domain = UriHelper.getEmailDomain(email.getTo());
if (domain == null)
2019-06-07 05:51:08 +00:00
throw new UnknownHostException();
2021-07-03 18:36:38 +00:00
//InetAddress address = DnsHelper.lookupMx(context, domain);
//if (address == null)
// throw new UnknownHostException();
InetAddress address = InetAddress.getByName(domain);
return new Pair<>(address, getOrganization(address, context));
} else {
String host = uri.getHost();
if (host == null)
2019-06-07 05:51:08 +00:00
throw new UnknownHostException();
2019-09-08 11:22:44 +00:00
InetAddress address = InetAddress.getByName(host);
2021-07-03 18:36:38 +00:00
return new Pair<>(address, getOrganization(address, context));
}
}
2021-05-14 09:47:14 +00:00
private static Organization getOrganization(InetAddress address, Context context) throws IOException {
2020-04-07 14:06:12 +00:00
synchronized (addressOrganization) {
if (addressOrganization.containsKey(address))
return addressOrganization.get(address);
2019-05-12 16:46:26 +00:00
}
2020-04-07 14:06:12 +00:00
// https://ipinfo.io/developers
URL url = new URL("https://ipinfo.io/" + address.getHostAddress() + "/org");
2019-05-12 16:46:26 +00:00
Log.i("GET " + url);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET");
2020-01-17 15:59:29 +00:00
connection.setReadTimeout(FETCH_TIMEOUT);
2020-06-30 09:50:28 +00:00
connection.setConnectTimeout(FETCH_TIMEOUT);
2021-05-14 09:47:14 +00:00
connection.setRequestProperty("User-Agent", WebViewEx.getUserAgent(context));
2019-05-12 16:46:26 +00:00
connection.connect();
2020-04-07 14:06:12 +00:00
Organization organization = new Organization();
try {
2021-05-19 15:20:42 +00:00
int status = connection.getResponseCode();
if (status != HttpsURLConnection.HTTP_OK)
2021-07-15 06:36:55 +00:00
throw new FileNotFoundException("Error " + status + ": " + connection.getResponseMessage());
2021-05-19 15:20:42 +00:00
2021-01-20 19:00:44 +00: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 14:06:12 +00:00
} finally {
connection.disconnect();
}
synchronized (addressOrganization) {
addressOrganization.put(address, organization);
2019-05-12 16:46:26 +00:00
}
2020-04-07 14:06:12 +00:00
return organization;
}
static class Organization {
String name;
2019-05-12 16:46:26 +00:00
}
}