2022-07-05 20:05:25 +02:00
|
|
|
|
"""
|
2022-07-07 13:08:20 +02:00
|
|
|
|
Displays information about the public IP address associated with the default route:
|
2022-07-06 12:51:19 +02:00
|
|
|
|
* Public IP address
|
|
|
|
|
* Country Name
|
|
|
|
|
* Country Code
|
|
|
|
|
* City Name
|
2022-07-07 13:08:20 +02:00
|
|
|
|
* Geographic Coordinates
|
2022-07-05 20:05:25 +02:00
|
|
|
|
|
2022-08-29 21:01:25 +02:00
|
|
|
|
Left mouse click on the widget forces immediate update.
|
|
|
|
|
Any change to the default route will cause the widget to update.
|
2022-07-05 20:05:25 +02:00
|
|
|
|
|
2022-07-07 13:08:20 +02:00
|
|
|
|
Requirements:
|
|
|
|
|
* netifaces
|
2022-07-05 20:05:25 +02:00
|
|
|
|
|
2022-07-07 13:08:20 +02:00
|
|
|
|
Parameters:
|
|
|
|
|
* publicip.format: Format string (defaults to ‘{ip} ({country_code})’)
|
|
|
|
|
* Available format strings - ip, country_name, country_code, city_name, coordinates
|
2022-07-05 20:05:25 +02:00
|
|
|
|
|
2022-07-06 12:51:19 +02:00
|
|
|
|
Examples:
|
2022-07-07 13:08:20 +02:00
|
|
|
|
* bumblebee-status -m publicip -p publicip.format="{ip} ({country_code})"
|
|
|
|
|
* bumblebee-status -m publicip -p publicip.format="{ip} which is in {city_name}"
|
|
|
|
|
* bumblebee-status -m publicip -p publicip.format="Your packets are right here: {coordinates}"
|
|
|
|
|
|
2022-08-29 21:01:25 +02:00
|
|
|
|
contributed by `tfwiii <https://github.com/tfwiii>` - many thanks!
|
2020-04-11 09:15:29 +02:00
|
|
|
|
"""
|
|
|
|
|
|
2022-07-07 13:08:20 +02:00
|
|
|
|
import re
|
|
|
|
|
import threading
|
|
|
|
|
import netifaces
|
2022-07-07 13:38:43 +02:00
|
|
|
|
import time
|
2022-07-07 13:08:20 +02:00
|
|
|
|
|
2020-04-11 09:20:19 +02:00
|
|
|
|
import core.module
|
|
|
|
|
import core.widget
|
2022-07-05 20:05:25 +02:00
|
|
|
|
import core.input
|
2022-07-07 13:08:20 +02:00
|
|
|
|
import core.decorators
|
|
|
|
|
|
2022-07-05 20:05:25 +02:00
|
|
|
|
import util.format
|
2020-04-15 13:26:08 +02:00
|
|
|
|
import util.location
|
2022-08-31 19:09:04 +02:00
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
2020-04-15 13:26:08 +02:00
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
|
2020-04-11 09:20:19 +02:00
|
|
|
|
class Module(core.module.Module):
|
2022-07-06 14:37:29 +02:00
|
|
|
|
@core.decorators.every(minutes=60)
|
2020-04-26 16:39:24 +02:00
|
|
|
|
def __init__(self, config, theme):
|
2022-07-07 13:08:20 +02:00
|
|
|
|
super().__init__(config, theme, core.widget.Widget(self.publicip))
|
|
|
|
|
|
2022-07-29 14:00:28 +02:00
|
|
|
|
self.__previous_default_route = None
|
|
|
|
|
self.__current_default_route = None
|
|
|
|
|
self.background = True
|
2020-04-11 09:20:19 +02:00
|
|
|
|
|
2022-07-06 12:51:19 +02:00
|
|
|
|
# Immediate update (override default) when left click on widget
|
2022-07-05 20:05:25 +02:00
|
|
|
|
core.input.register(self, button=core.input.LEFT_MOUSE, cmd=self.__click_update)
|
|
|
|
|
|
2022-07-06 12:51:19 +02:00
|
|
|
|
# By default show: <ip> (<2 letter country code>)
|
|
|
|
|
self._format = self.parameter("format", "{ip} ({country_code})")
|
2022-07-05 20:05:25 +02:00
|
|
|
|
|
2022-08-05 14:30:50 +02:00
|
|
|
|
self.__monitor = threading.Thread(target=self.monitor, args=())
|
|
|
|
|
self.__monitor.start()
|
|
|
|
|
|
|
|
|
|
def monitor(self):
|
2022-10-08 05:42:12 +02:00
|
|
|
|
__previous_ips = set()
|
|
|
|
|
__current_ips = set()
|
2022-08-29 21:01:25 +02:00
|
|
|
|
# Initially set to True to force an info update on first pass
|
2022-10-08 05:42:12 +02:00
|
|
|
|
__information_changed = True
|
2022-08-29 21:01:25 +02:00
|
|
|
|
|
|
|
|
|
self.update()
|
|
|
|
|
|
2022-08-05 14:30:50 +02:00
|
|
|
|
while threading.main_thread().is_alive():
|
2022-10-08 05:42:12 +02:00
|
|
|
|
__current_ips.clear()
|
|
|
|
|
# Look for any changes to IP addresses
|
2022-08-06 09:17:49 +02:00
|
|
|
|
try:
|
2022-10-08 05:42:12 +02:00
|
|
|
|
for interface in netifaces.interfaces():
|
|
|
|
|
__current_ips.add(netifaces.ifaddresses(interface)[2][0]['addr'])
|
|
|
|
|
|
2022-08-06 09:17:49 +02:00
|
|
|
|
except:
|
2022-10-08 05:42:12 +02:00
|
|
|
|
# If not ip address information found clear __current_ips
|
|
|
|
|
__current_ips.clear()
|
|
|
|
|
|
|
|
|
|
# If a change of any interfaces' IP then flag change
|
|
|
|
|
if __current_ips.symmetric_difference(__previous_ips):
|
|
|
|
|
__previous_ips = __current_ips.copy()
|
|
|
|
|
__information_changed = True
|
|
|
|
|
|
|
|
|
|
# Update if change is flagged
|
|
|
|
|
if __information_changed:
|
|
|
|
|
__information_changed = False
|
2022-08-05 14:30:50 +02:00
|
|
|
|
self.update()
|
2022-10-08 05:42:12 +02:00
|
|
|
|
|
2022-08-29 21:01:25 +02:00
|
|
|
|
# Throttle the calls to netifaces
|
2022-08-05 14:30:50 +02:00
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
2022-07-07 13:08:20 +02:00
|
|
|
|
def publicip(self, widget):
|
2022-08-29 21:01:25 +02:00
|
|
|
|
if widget.get("public_ip") is None:
|
2022-07-29 14:00:28 +02:00
|
|
|
|
return "n/a"
|
2022-07-07 13:08:20 +02:00
|
|
|
|
return self._format.format(
|
2022-10-08 05:42:12 +02:00
|
|
|
|
ip = widget.get("public_ip", "-"),
|
|
|
|
|
country_name = widget.get("country_name", "-"),
|
|
|
|
|
country_code = widget.get("country_code", "-"),
|
|
|
|
|
city_name = widget.get("city_name", "-"),
|
|
|
|
|
coordinates = widget.get("coordinates", "-"),
|
2022-07-07 13:38:43 +02:00
|
|
|
|
)
|
2022-07-07 13:08:20 +02:00
|
|
|
|
|
2022-07-05 20:05:25 +02:00
|
|
|
|
def __click_update(self, event):
|
|
|
|
|
util.location.reset()
|
2020-04-11 09:15:29 +02:00
|
|
|
|
|
2020-04-11 09:20:19 +02:00
|
|
|
|
def update(self):
|
2022-07-29 14:00:28 +02:00
|
|
|
|
widget = self.widget()
|
|
|
|
|
|
|
|
|
|
try:
|
2022-08-05 14:30:50 +02:00
|
|
|
|
util.location.reset()
|
2022-10-08 05:42:12 +02:00
|
|
|
|
time.sleep(5) # wait for reset to complete before querying results
|
2022-08-29 21:01:25 +02:00
|
|
|
|
|
2022-08-05 14:30:50 +02:00
|
|
|
|
# Fetch fresh location information
|
|
|
|
|
__info = util.location.location_info()
|
2022-10-08 05:42:12 +02:00
|
|
|
|
__raw_lat = __info["latitude"]
|
|
|
|
|
__raw_lon = __info["longitude"]
|
2022-08-05 14:30:50 +02:00
|
|
|
|
|
2022-10-06 08:49:37 +02:00
|
|
|
|
# Contstruct coordinates string if util.location has provided required info
|
2022-10-08 05:42:12 +02:00
|
|
|
|
if isinstance(__raw_lat, float) and isinstance(__raw_lon, float):
|
|
|
|
|
__lat = float("{:.2f}".format(__raw_lat))
|
|
|
|
|
__lon = float("{:.2f}".format(__raw_lon))
|
2022-10-06 08:49:37 +02:00
|
|
|
|
if __lat < 0:
|
2022-10-08 05:42:12 +02:00
|
|
|
|
__coords = str(__lat) + "°S"
|
2022-10-06 08:49:37 +02:00
|
|
|
|
else:
|
2022-10-08 05:42:12 +02:00
|
|
|
|
__coords = str(__lat) + "°N"
|
2022-10-06 08:49:37 +02:00
|
|
|
|
__coords += ","
|
|
|
|
|
if __lon < 0:
|
2022-10-08 05:42:12 +02:00
|
|
|
|
__coords += str(__lon) + "°W"
|
2022-10-06 08:49:37 +02:00
|
|
|
|
else:
|
2022-10-08 05:42:12 +02:00
|
|
|
|
__coords += str(__lon) + "°E"
|
2022-10-06 08:49:37 +02:00
|
|
|
|
else:
|
|
|
|
|
__coords = "Unknown"
|
2022-08-05 14:30:50 +02:00
|
|
|
|
|
|
|
|
|
# Set widget values
|
|
|
|
|
widget.set("public_ip", __info["public_ip"])
|
|
|
|
|
widget.set("country_name", __info["country"])
|
|
|
|
|
widget.set("country_code", __info["country_code"])
|
|
|
|
|
widget.set("city_name", __info["city_name"])
|
|
|
|
|
widget.set("coordinates", __coords)
|
|
|
|
|
|
|
|
|
|
# Update widget values
|
|
|
|
|
core.event.trigger("update", [widget.module.id], redraw_only=True)
|
2022-08-29 21:01:25 +02:00
|
|
|
|
except Exception as ex:
|
2022-07-29 14:00:28 +02:00
|
|
|
|
widget.set("public_ip", None)
|
2022-08-31 19:09:04 +02:00
|
|
|
|
logging.error(str(ex))
|
2022-07-07 13:08:20 +02:00
|
|
|
|
|
|
|
|
|
def state(self, widget):
|
|
|
|
|
return widget.get("state", None)
|
|
|
|
|
|
|
|
|
|
|
2022-08-31 19:09:04 +02:00
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|