diff --git a/app/src/main/java/eu/faircode/email/ActivityDmarc.java b/app/src/main/java/eu/faircode/email/ActivityDmarc.java index 967858a67d..d04571b91f 100644 --- a/app/src/main/java/eu/faircode/email/ActivityDmarc.java +++ b/app/src/main/java/eu/faircode/email/ActivityDmarc.java @@ -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'); diff --git a/app/src/main/java/eu/faircode/email/FragmentDialogOpenLink.java b/app/src/main/java/eu/faircode/email/FragmentDialogOpenLink.java index 98bc28fdb8..ac1636d9fb 100644 --- a/app/src/main/java/eu/faircode/email/FragmentDialogOpenLink.java +++ b/app/src/main/java/eu/faircode/email/FragmentDialogOpenLink.java @@ -369,7 +369,7 @@ public class FragmentDialogOpenLink extends FragmentDialogBase { Bundle args = new Bundle(); args.putParcelable("uri", Uri.parse(etLink.getText().toString())); - new SimpleTask>() { + new SimpleTask>() { @Override protected void onPreExecute(Bundle args) { ibMore.setEnabled(false); @@ -389,15 +389,23 @@ public class FragmentDialogOpenLink extends FragmentDialogBase { } @Override - protected Pair onExecute(Context context, Bundle args) throws Throwable { + protected Pair onExecute(Context context, Bundle args) throws Throwable { Uri uri = args.getParcelable("uri"); return IPInfo.getOrganization(uri, context); } @Override - protected void onExecuted(Bundle args, Pair data) { + protected void onExecuted(Bundle args, Pair 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 diff --git a/app/src/main/java/eu/faircode/email/IPInfo.java b/app/src/main/java/eu/faircode/email/IPInfo.java index e6be5d3686..c2d099d737 100644 --- a/app/src/main/java/eu/faircode/email/IPInfo.java +++ b/app/src/main/java/eu/faircode/email/IPInfo.java @@ -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 addressOrganization = new HashMap<>(); + public String org; + public String city; + public String country; + + private static final Map addressOrganization = new HashMap<>(); private final static int FETCH_TIMEOUT = 15 * 1000; // milliseconds - static Pair getOrganization(@NonNull Uri uri, Context context) throws IOException, ParseException { + static Pair 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; } }