Merge pull request #933 from tfwiii/main
publicip module - Fixed bug and minor improvements in output
This commit is contained in:
commit
1682a47554
2 changed files with 53 additions and 48 deletions
|
@ -60,43 +60,34 @@ class Module(core.module.Module):
|
||||||
self.__monitor.start()
|
self.__monitor.start()
|
||||||
|
|
||||||
def monitor(self):
|
def monitor(self):
|
||||||
default_route = None
|
__previous_ips = set()
|
||||||
interfaces = None
|
__current_ips = set()
|
||||||
# Initially set to True to force an info update on first pass
|
# Initially set to True to force an info update on first pass
|
||||||
information_changed = True
|
__information_changed = True
|
||||||
|
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
while threading.main_thread().is_alive():
|
while threading.main_thread().is_alive():
|
||||||
# Look for any changes in the netifaces default route information
|
__current_ips.clear()
|
||||||
|
# Look for any changes to IP addresses
|
||||||
try:
|
try:
|
||||||
current_default_route = netifaces.gateways()["default"][2]
|
for interface in netifaces.interfaces():
|
||||||
|
__current_ips.add(netifaces.ifaddresses(interface)[2][0]['addr'])
|
||||||
|
|
||||||
except:
|
except:
|
||||||
# error reading out default gw -> assume none exists
|
# If not ip address information found clear __current_ips
|
||||||
current_default_route = None
|
__current_ips.clear()
|
||||||
if current_default_route != default_route:
|
|
||||||
default_route = current_default_route
|
# If a change of any interfaces' IP then flag change
|
||||||
information_changed = True
|
if __current_ips.symmetric_difference(__previous_ips):
|
||||||
|
__previous_ips = __current_ips.copy()
|
||||||
|
__information_changed = True
|
||||||
|
|
||||||
# netifaces does not check ALL routing tables which might lead to false negatives
|
# Update if change is flagged
|
||||||
# (ref: http://linux-ip.net/html/routing-tables.html) so additionally... look for
|
if __information_changed:
|
||||||
# any changes in the netifaces interfaces information which might also be an inticator
|
__information_changed = False
|
||||||
# of a change of route/external IP
|
|
||||||
if not information_changed: # Only check if no routing table change found
|
|
||||||
try:
|
|
||||||
current_interfaces = netifaces.interfaces()
|
|
||||||
except:
|
|
||||||
# error reading interfaces information -> assume none exists
|
|
||||||
current_interfaces = None
|
|
||||||
if current_interfaces != interfaces:
|
|
||||||
interfaces = current_interfaces
|
|
||||||
information_changed = True
|
|
||||||
|
|
||||||
# Update either routing or interface information has changed
|
|
||||||
if information_changed:
|
|
||||||
information_changed = False
|
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
# Throttle the calls to netifaces
|
# Throttle the calls to netifaces
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
|
@ -104,11 +95,11 @@ class Module(core.module.Module):
|
||||||
if widget.get("public_ip") is None:
|
if widget.get("public_ip") is None:
|
||||||
return "n/a"
|
return "n/a"
|
||||||
return self._format.format(
|
return self._format.format(
|
||||||
ip=widget.get("public_ip", "-"),
|
ip = widget.get("public_ip", "-"),
|
||||||
country_name=widget.get("country_name", "-"),
|
country_name = widget.get("country_name", "-"),
|
||||||
country_code=widget.get("country_code", "-"),
|
country_code = widget.get("country_code", "-"),
|
||||||
city_name=widget.get("city_name", "-"),
|
city_name = widget.get("city_name", "-"),
|
||||||
coordinates=widget.get("coordinates", "-"),
|
coordinates = widget.get("coordinates", "-"),
|
||||||
)
|
)
|
||||||
|
|
||||||
def __click_update(self, event):
|
def __click_update(self, event):
|
||||||
|
@ -119,14 +110,28 @@ class Module(core.module.Module):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
util.location.reset()
|
util.location.reset()
|
||||||
|
time.sleep(5) # wait for reset to complete before querying results
|
||||||
|
|
||||||
# Fetch fresh location information
|
# Fetch fresh location information
|
||||||
__info = util.location.location_info()
|
__info = util.location.location_info()
|
||||||
|
__raw_lat = __info["latitude"]
|
||||||
|
__raw_lon = __info["longitude"]
|
||||||
|
|
||||||
# Contstruct coordinates string
|
# Contstruct coordinates string if util.location has provided required info
|
||||||
__lat = "{:.2f}".format(__info["latitude"])
|
if isinstance(__raw_lat, float) and isinstance(__raw_lon, float):
|
||||||
__lon = "{:.2f}".format(__info["longitude"])
|
__lat = float("{:.2f}".format(__raw_lat))
|
||||||
__coords = __lat + "°N" + "," + " " + __lon + "°E"
|
__lon = float("{:.2f}".format(__raw_lon))
|
||||||
|
if __lat < 0:
|
||||||
|
__coords = str(__lat) + "°S"
|
||||||
|
else:
|
||||||
|
__coords = str(__lat) + "°N"
|
||||||
|
__coords += ","
|
||||||
|
if __lon < 0:
|
||||||
|
__coords += str(__lon) + "°W"
|
||||||
|
else:
|
||||||
|
__coords += str(__lon) + "°E"
|
||||||
|
else:
|
||||||
|
__coords = "Unknown"
|
||||||
|
|
||||||
# Set widget values
|
# Set widget values
|
||||||
widget.set("public_ip", __info["public_ip"])
|
widget.set("public_ip", __info["public_ip"])
|
||||||
|
|
|
@ -18,17 +18,6 @@ __document = None
|
||||||
__data = {}
|
__data = {}
|
||||||
__next = 0
|
__next = 0
|
||||||
__sources = [
|
__sources = [
|
||||||
{
|
|
||||||
"url": "http://ipapi.co/json",
|
|
||||||
"mapping": {
|
|
||||||
"latitude": "latitude",
|
|
||||||
"longitude": "longitude",
|
|
||||||
"country_name": "country_name",
|
|
||||||
"country_code": "country_code",
|
|
||||||
"city": "city_name",
|
|
||||||
"ip": "public_ip",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"url": "http://free.ipwhois.io/json/",
|
"url": "http://free.ipwhois.io/json/",
|
||||||
"mapping": {
|
"mapping": {
|
||||||
|
@ -51,6 +40,17 @@ __sources = [
|
||||||
"query": "public_ip",
|
"query": "public_ip",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"url": "http://ipapi.co/json",
|
||||||
|
"mapping": {
|
||||||
|
"latitude": "latitude",
|
||||||
|
"longitude": "longitude",
|
||||||
|
"country_name": "country_name",
|
||||||
|
"country_code": "country_code",
|
||||||
|
"city": "city_name",
|
||||||
|
"ip": "public_ip",
|
||||||
|
},
|
||||||
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue