Extended ipinfo

pull/209/head
M66B 2 years ago
parent 16fc4360c6
commit 9dee5e793b

@ -324,9 +324,8 @@ public class ActivityDmarc extends ActivityBase {
try { try {
InetAddress addr = InetAddress.getByName(text); InetAddress addr = InetAddress.getByName(text);
IPInfo.Organization info = IPInfo info = IPInfo.getOrganization(addr, context);
IPInfo.getOrganization(addr, context); ssb.append('(').append(info.org).append(") ");
ssb.append('(').append(info.name).append(") ");
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(ex); Log.w(ex);
ssb.append(ex.toString()).append('\n'); ssb.append(ex.toString()).append('\n');

@ -369,7 +369,7 @@ public class FragmentDialogOpenLink extends FragmentDialogBase {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putParcelable("uri", Uri.parse(etLink.getText().toString())); args.putParcelable("uri", Uri.parse(etLink.getText().toString()));
new SimpleTask<Pair<InetAddress, IPInfo.Organization>>() { new SimpleTask<Pair<InetAddress, IPInfo>>() {
@Override @Override
protected void onPreExecute(Bundle args) { protected void onPreExecute(Bundle args) {
ibMore.setEnabled(false); ibMore.setEnabled(false);
@ -389,15 +389,23 @@ public class FragmentDialogOpenLink extends FragmentDialogBase {
} }
@Override @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"); Uri uri = args.getParcelable("uri");
return IPInfo.getOrganization(uri, context); return IPInfo.getOrganization(uri, context);
} }
@Override @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()); 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() { ApplicationEx.getMainHandler().post(new Runnable() {
@Override @Override

@ -26,6 +26,9 @@ import android.util.Pair;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.net.IDN; import java.net.IDN;
@ -38,11 +41,15 @@ import java.util.Map;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
public class IPInfo { 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 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); String host = UriHelper.getHost(uri);
if (host == null) if (host == null)
throw new UnknownHostException(); throw new UnknownHostException();
@ -57,14 +64,29 @@ public class IPInfo {
return new Pair<>(address, getOrganization(address, context)); 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) { synchronized (addressOrganization) {
if (addressOrganization.containsKey(address)) if (addressOrganization.containsKey(address))
return addressOrganization.get(address); return addressOrganization.get(address);
} }
// https://ipinfo.io/developers // 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); Log.i("GET " + url);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET"); connection.setRequestMethod("GET");
@ -73,28 +95,25 @@ public class IPInfo {
ConnectionHelper.setUserAgent(context, connection); ConnectionHelper.setUserAgent(context, connection);
connection.connect(); connection.connect();
Organization organization = new Organization(); IPInfo info = new IPInfo();
try { try {
int status = connection.getResponseCode(); int status = connection.getResponseCode();
if (status != HttpsURLConnection.HTTP_OK) if (status != HttpsURLConnection.HTTP_OK)
throw new FileNotFoundException("Error " + status + ": " + connection.getResponseMessage()); throw new FileNotFoundException("Error " + status + ": " + connection.getResponseMessage());
String response = Helper.readStream(connection.getInputStream()); String response = Helper.readStream(connection.getInputStream());
organization.name = response.trim(); JSONObject jroot = new JSONObject(response);
if ("".equals(organization.name) || "undefined".equals(organization.name)) info.org = jroot.optString("org");
organization.name = null; info.city = jroot.optString("city");
info.country = jroot.optString("country");
} finally { } finally {
connection.disconnect(); connection.disconnect();
} }
synchronized (addressOrganization) { synchronized (addressOrganization) {
addressOrganization.put(address, organization); addressOrganization.put(address, info);
}
return organization;
} }
static class Organization { return info;
String name;
} }
} }

Loading…
Cancel
Save