mirror of https://github.com/M66B/FairEmail.git
Extended ipinfo
This commit is contained in:
parent
16fc4360c6
commit
9dee5e793b
|
@ -324,9 +324,8 @@ public class ActivityDmarc extends ActivityBase {
|
|||
|
||||
try {
|
||||
InetAddress addr = InetAddress.getByName(text);
|
||||
IPInfo.Organization info =
|
||||
IPInfo.getOrganization(addr, context);
|
||||
ssb.append('(').append(info.name).append(") ");
|
||||
IPInfo info = IPInfo.getOrganization(addr, context);
|
||||
ssb.append('(').append(info.org).append(") ");
|
||||
} catch (Throwable ex) {
|
||||
Log.w(ex);
|
||||
ssb.append(ex.toString()).append('\n');
|
||||
|
|
|
@ -369,7 +369,7 @@ public class FragmentDialogOpenLink extends FragmentDialogBase {
|
|||
Bundle args = new Bundle();
|
||||
args.putParcelable("uri", Uri.parse(etLink.getText().toString()));
|
||||
|
||||
new SimpleTask<Pair<InetAddress, IPInfo.Organization>>() {
|
||||
new SimpleTask<Pair<InetAddress, IPInfo>>() {
|
||||
@Override
|
||||
protected void onPreExecute(Bundle args) {
|
||||
ibMore.setEnabled(false);
|
||||
|
@ -389,15 +389,23 @@ public class FragmentDialogOpenLink extends FragmentDialogBase {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected Pair<InetAddress, IPInfo.Organization> onExecute(Context context, Bundle args) throws Throwable {
|
||||
protected Pair<InetAddress, IPInfo> onExecute(Context context, Bundle args) throws Throwable {
|
||||
Uri uri = args.getParcelable("uri");
|
||||
return IPInfo.getOrganization(uri, context);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onExecuted(Bundle args, Pair<InetAddress, IPInfo.Organization> data) {
|
||||
protected void onExecuted(Bundle args, Pair<InetAddress, IPInfo> data) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String value : new String[]{data.second.org, data.second.city, data.second.country})
|
||||
if (!TextUtils.isEmpty(value)) {
|
||||
if (sb.length() != 0)
|
||||
sb.append("; ");
|
||||
sb.append(value.replaceAll("\\r?\\n", " "));
|
||||
}
|
||||
|
||||
tvHost.setText(data.first.toString());
|
||||
tvOwner.setText(data.second.name == null ? "?" : data.second.name);
|
||||
tvOwner.setText(sb.length() == 0 ? "?" : sb.toString());
|
||||
|
||||
ApplicationEx.getMainHandler().post(new Runnable() {
|
||||
@Override
|
||||
|
|
|
@ -26,6 +26,9 @@ import android.util.Pair;
|
|||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.net.IDN;
|
||||
|
@ -38,11 +41,15 @@ import java.util.Map;
|
|||
import javax.net.ssl.HttpsURLConnection;
|
||||
|
||||
public class IPInfo {
|
||||
private static Map<InetAddress, Organization> addressOrganization = new HashMap<>();
|
||||
public String org;
|
||||
public String city;
|
||||
public String country;
|
||||
|
||||
private static final Map<InetAddress, IPInfo> addressOrganization = new HashMap<>();
|
||||
|
||||
private final static int FETCH_TIMEOUT = 15 * 1000; // milliseconds
|
||||
|
||||
static Pair<InetAddress, Organization> getOrganization(@NonNull Uri uri, Context context) throws IOException, ParseException {
|
||||
static Pair<InetAddress, IPInfo> getOrganization(@NonNull Uri uri, Context context) throws IOException, ParseException, JSONException {
|
||||
String host = UriHelper.getHost(uri);
|
||||
if (host == null)
|
||||
throw new UnknownHostException();
|
||||
|
@ -57,14 +64,29 @@ public class IPInfo {
|
|||
return new Pair<>(address, getOrganization(address, context));
|
||||
}
|
||||
|
||||
static Organization getOrganization(InetAddress address, Context context) throws IOException {
|
||||
static IPInfo getOrganization(InetAddress address, Context context) throws IOException, JSONException {
|
||||
synchronized (addressOrganization) {
|
||||
if (addressOrganization.containsKey(address))
|
||||
return addressOrganization.get(address);
|
||||
}
|
||||
|
||||
// https://ipinfo.io/developers
|
||||
URL url = new URL("https://ipinfo.io/" + address.getHostAddress() + "/org");
|
||||
|
||||
//{
|
||||
// "ip": "8.8.8.8",
|
||||
// "hostname": "dns.google",
|
||||
// "anycast": true,
|
||||
// "city": "Mountain View",
|
||||
// "region": "California",
|
||||
// "country": "US",
|
||||
// "loc": "37.4056,-122.0775",
|
||||
// "org": "AS15169 Google LLC",
|
||||
// "postal": "94043",
|
||||
// "timezone": "America/Los_Angeles",
|
||||
// "readme": "https://ipinfo.io/missingauth"
|
||||
//}
|
||||
|
||||
URL url = new URL("https://ipinfo.io/" + address.getHostAddress() + "/json");
|
||||
Log.i("GET " + url);
|
||||
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
|
||||
connection.setRequestMethod("GET");
|
||||
|
@ -73,28 +95,25 @@ public class IPInfo {
|
|||
ConnectionHelper.setUserAgent(context, connection);
|
||||
connection.connect();
|
||||
|
||||
Organization organization = new Organization();
|
||||
IPInfo info = new IPInfo();
|
||||
try {
|
||||
int status = connection.getResponseCode();
|
||||
if (status != HttpsURLConnection.HTTP_OK)
|
||||
throw new FileNotFoundException("Error " + status + ": " + connection.getResponseMessage());
|
||||
|
||||
String response = Helper.readStream(connection.getInputStream());
|
||||
organization.name = response.trim();
|
||||
if ("".equals(organization.name) || "undefined".equals(organization.name))
|
||||
organization.name = null;
|
||||
JSONObject jroot = new JSONObject(response);
|
||||
info.org = jroot.optString("org");
|
||||
info.city = jroot.optString("city");
|
||||
info.country = jroot.optString("country");
|
||||
} finally {
|
||||
connection.disconnect();
|
||||
}
|
||||
|
||||
synchronized (addressOrganization) {
|
||||
addressOrganization.put(address, organization);
|
||||
addressOrganization.put(address, info);
|
||||
}
|
||||
|
||||
return organization;
|
||||
}
|
||||
|
||||
static class Organization {
|
||||
String name;
|
||||
return info;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue