|
|
@ -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;
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static class Organization {
|
|
|
|
|
|
|
|
String name;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|