diff --git a/API.md b/API.md
index 2d2cbad3..9e02e5cf 100644
--- a/API.md
+++ b/API.md
@@ -342,18 +342,7 @@ Returns:
"timezone": "America/Los_Angeles",
"latitude": 37.386,
"longitude": -122.0838,
- "accuracy": 1000,
- "net": [{"description": "Google Inc.",
- "address": "1600 Amphitheatre Parkway",
- "city": "Mountain View",
- "state": "CA",
- "postal_code": "94043",
- "country": "United States",
- ...
- },
- {...}
- ]
-
+ "accuracy": 1000
}
json:
{"error": "The address 127.0.0.1 is not in the database."
@@ -1716,6 +1705,34 @@ Returns:
```
+### get_whois_lookup
+Get the ISP info for an IP address.
+
+```
+Required parameters:
+ ip_address
+
+Optional parameters:
+ None
+
+Returns:
+ json:
+ [{"description": "Google Inc.",
+ "address": "1600 Amphitheatre Parkway",
+ "city": "Mountain View",
+ "state": "CA",
+ "postal_code": "94043",
+ "country": "United States",
+ ...
+ },
+ {...}
+ ]
+ json:
+ {"error": "The address 127.0.0.1 is not in the database."
+ }
+```
+
+
### import_database
Import a PlexWatch or Plexivity database into PlexPy.
diff --git a/data/interfaces/default/css/plexpy.css b/data/interfaces/default/css/plexpy.css
index c77c4270..60d33cb6 100644
--- a/data/interfaces/default/css/plexpy.css
+++ b/data/interfaces/default/css/plexpy.css
@@ -3009,8 +3009,10 @@ a:hover .overlay-refresh-image {
a:hover .overlay-refresh-image:hover {
opacity: .9;
}
-#ip_error {
+#ip_error, #isp_error {
color: #aaa;
display: none;
text-align: center;
+ padding-top: 10px;
+ padding-bottom: 10px;
}
\ No newline at end of file
diff --git a/data/interfaces/default/ip_address_modal.html b/data/interfaces/default/ip_address_modal.html
index 39c5f310..2f7e263a 100644
--- a/data/interfaces/default/ip_address_modal.html
+++ b/data/interfaces/default/ip_address_modal.html
@@ -13,10 +13,10 @@
-
-
Location Details
+ Location Details
+
- Continent:
@@ -35,8 +35,9 @@
-
Connection Details
+ Connection Details
+
- ISP:
@@ -59,13 +60,16 @@
type: 'GET',
data: { ip_address: ip_address },
cache: true,
- async: true,
+ async: true,
+ complete: function () {
+ $('#ip_loading').remove();
+ },
error: function () {
- $('#ip_error').html(' Request failed.
').show();
+ $('#ip_error').html(' Internal request failed.').show();
},
success: function (data) {
if ('error' in data) {
- $('#ip_error').html(' ' + data.error + '
').show();
+ $('#ip_error').html(' ' + data.error).show();
} else {
$('#continent').html(data.continent);
$('#country').html(data.country);
@@ -76,30 +80,54 @@
$('#latitude').html(data.latitude);
$('#longitude').html(data.longitude);
$('#accuracy').html(data.accuracy + ' km');
-
- var nets = data.nets;
- if (!(nets.length)) {
- $('#isp').html('Not Found')
- $('#isp_address').html('Not Found')
- } else {
- $('#isp_instance').remove();
- $.each(nets, function (index, net) {
- $('#modal-text').append(' \
-
\
- - ISP: ' + net.description + '
\
- - Address: \
- ' + net.address + '
\
- ' + net.city + ", " + net.state + " " + net.postal_code + '
\
- ' + net.country + ' \
- \
-
\
-
')
- });
- }
}
}
});
}
+
+ function getUserConnection(ip_address) {
+ $.ajax({
+ url: 'get_whois_lookup',
+ type: 'GET',
+ data: { ip_address: ip_address },
+ cache: true,
+ async: true,
+ complete: function () {
+ $('#isp_loading').remove();
+ },
+ error: function () {
+ $('#isp_error').html(' Internal request failed.').show();
+ },
+ success: function (data) {
+ if ('error' in data) {
+ $('#isp_error').html(' ' + data.error).show();
+ } else if (!(data.length)) {
+ $('#isp_error').html(' Connection details not found.').show();
+ } else {
+ $('#isp_instance').remove();
+ $.each(data, function (index, net) {
+ var s = '';
+ if (net.city || net.state || net.postal_code) {
+ s = (net.city && net.state) ? net.city + ', ' + net.state : net.city || net.state || '';
+ s = (s && net.postal_code) ? s + ' ' + net.postal_code : s || net.postal_code || '';
+ }
+ s = (s) ? '' + s + '
' : s;
+ $('#modal-text').append(' \
+
\
+ - ISP: ' + net.description + '
\
+ - Address: \
+ ' + net.address + '
' + s +
+ '' + net.country + ' \
+ \
+
\
+
')
+ });
+ }
+ }
+ });
+ }
+
getUserLocation('${data}');
+ getUserConnection('${data}');
% endif
\ No newline at end of file
diff --git a/plexpy/helpers.py b/plexpy/helpers.py
index 461e1270..31b1393b 100644
--- a/plexpy/helpers.py
+++ b/plexpy/helpers.py
@@ -20,8 +20,7 @@ import geoip2.database, geoip2.errors
import gzip
import hashlib
import imghdr
-from ipwhois import IPWhois
-from ipwhois.utils import get_countries
+import ipwhois, ipwhois.exceptions, ipwhois.utils
from IPy import IP
import json
import math
@@ -604,12 +603,11 @@ def geoip_lookup(ip_address):
reader = geoip2.database.Reader(plexpy.CONFIG.GEOIP_DB)
geo = reader.city(ip_address)
reader.close()
+ except ValueError as e:
+ return 'Invalid IP address provided: %s.' % ip_address
except IOError as e:
return 'Missing GeoLite2 database. Please reinstall from the ' \
'Settings page.'
- except ValueError as e:
- return 'Unable to read GeoLite2 database. Please reinstall from the ' \
- 'Settings page.'
except maxminddb.InvalidDatabaseError as e:
return 'Invalid GeoLite2 database. Please reinstall from the ' \
'Settings page.'
@@ -618,17 +616,6 @@ def geoip_lookup(ip_address):
except Exception as e:
return 'Error: %s' % e
- nets = []
- try:
- whois = IPWhois(ip_address).lookup_whois()
- countries = get_countries()
- nets = whois['nets']
- for net in nets:
- net['country'] = countries[net['country']]
- net['postal_code'] = net['postal_code'].replace('-', ' ')
- except Exception as e:
- logger.warn(u"PlexPy Helpers :: %s." % e)
-
geo_info = {'continent': geo.continent.name,
'country': geo.country.name,
'region': geo.subdivisions.most_specific.name,
@@ -637,12 +624,33 @@ def geoip_lookup(ip_address):
'timezone': geo.location.time_zone,
'latitude': geo.location.latitude,
'longitude': geo.location.longitude,
- 'accuracy': geo.location.accuracy_radius,
- 'nets': nets
+ 'accuracy': geo.location.accuracy_radius
}
return geo_info
+def whois_lookup(ip_address):
+
+ nets = []
+ try:
+ whois = ipwhois.IPWhois(ip_address).lookup_whois(retry_count=0)
+ countries = ipwhois.utils.get_countries()
+ nets = whois['nets']
+ for net in nets:
+ net['country'] = countries[net['country']]
+ if net['postal_code']:
+ net['postal_code'] = net['postal_code'].replace('-', ' ')
+ except ValueError as e:
+ return 'Invalid IP address provided: %s.' % ip_address
+ except ipwhois.exceptions.IPDefinedError as e:
+ return '%s' % e
+ except ipwhois.exceptions.ASNRegistryError as e:
+ return '%s' % e
+ except Exception as e:
+ return 'Error: %s' % e
+
+ return nets
+
# Taken from SickRage
def anon_url(*url):
"""
diff --git a/plexpy/webserve.py b/plexpy/webserve.py
index e2cc5852..0dfe6d5c 100644
--- a/plexpy/webserve.py
+++ b/plexpy/webserve.py
@@ -4310,18 +4310,7 @@ class WebInterface(object):
"timezone": "America/Los_Angeles",
"latitude": 37.386,
"longitude": -122.0838,
- "accuracy": 1000,
- "net": [{"description": "Google Inc.",
- "address": "1600 Amphitheatre Parkway",
- "city": "Mountain View",
- "state": "CA",
- "postal_code": "94043",
- "country": "United States",
- ...
- },
- {...}
- ]
-
+ "accuracy": 1000
}
json:
{"error": "The address 127.0.0.1 is not in the database."
@@ -4332,3 +4321,39 @@ class WebInterface(object):
if isinstance(geo_info, basestring):
return {'error': geo_info}
return geo_info
+
+ @cherrypy.expose
+ @cherrypy.tools.json_out()
+ @requireAuth()
+ @addtoapi()
+ def get_whois_lookup(self, ip_address='', **kwargs):
+ """ Get the ISP info for an IP address.
+
+ ```
+ Required parameters:
+ ip_address
+
+ Optional parameters:
+ None
+
+ Returns:
+ json:
+ [{"description": "Google Inc.",
+ "address": "1600 Amphitheatre Parkway",
+ "city": "Mountain View",
+ "state": "CA",
+ "postal_code": "94043",
+ "country": "United States",
+ ...
+ },
+ {...}
+ ]
+ json:
+ {"error": "The address 127.0.0.1 is not in the database."
+ }
+ ```
+ """
+ whois_info = helpers.whois_lookup(ip_address)
+ if isinstance(whois_info, basestring):
+ return {'error': whois_info}
+ return whois_info