2021-07-05 14:55:47 +02:00
|
|
|
"""
|
2021-07-09 06:00:57 +02:00
|
|
|
A module to show the currently active network connection (ethernet or wifi) and connection strength if the connection is wireless.
|
|
|
|
|
|
|
|
Requires the Python netifaces package and iw installed on Linux.
|
|
|
|
|
|
|
|
A simpler take on nic and network_traffic. No extra config necessary!
|
2021-07-05 20:54:28 +02:00
|
|
|
|
2021-07-05 14:55:47 +02:00
|
|
|
"""
|
|
|
|
|
2021-07-05 17:29:37 +02:00
|
|
|
|
|
|
|
import util.cli
|
|
|
|
import util.format
|
2021-07-05 14:55:47 +02:00
|
|
|
|
|
|
|
import core.module
|
|
|
|
import core.widget
|
2021-07-05 19:34:42 +02:00
|
|
|
import core.input
|
2021-07-05 14:55:47 +02:00
|
|
|
|
2021-07-08 16:04:40 +02:00
|
|
|
import netifaces
|
|
|
|
import socket
|
|
|
|
|
2021-07-05 14:55:47 +02:00
|
|
|
|
|
|
|
class Module(core.module.Module):
|
2021-07-09 05:55:23 +02:00
|
|
|
@core.decorators.every(seconds=5)
|
2021-07-05 14:55:47 +02:00
|
|
|
def __init__(self, config, theme):
|
|
|
|
super().__init__(config, theme, core.widget.Widget(self.network))
|
2021-07-05 20:54:28 +02:00
|
|
|
self.__is_wireless = False
|
2021-07-08 16:04:40 +02:00
|
|
|
self.__is_connected = False
|
2021-07-05 20:09:17 +02:00
|
|
|
self.__interface = None
|
|
|
|
self.__message = None
|
2021-07-05 18:26:46 +02:00
|
|
|
self.__signal = -110
|
2021-07-05 14:55:47 +02:00
|
|
|
|
2021-07-05 20:54:28 +02:00
|
|
|
# Get network information to display to the user
|
2021-07-05 14:55:47 +02:00
|
|
|
def network(self, widgets):
|
2021-07-08 16:04:40 +02:00
|
|
|
# Determine whether there is an internet connection
|
|
|
|
try:
|
|
|
|
socket.create_connection(("1.1.1.1", 53))
|
|
|
|
self.__is_connected = True
|
2021-07-09 05:55:23 +02:00
|
|
|
except Exception:
|
2021-07-08 16:04:40 +02:00
|
|
|
self.__is_connected = False
|
2021-07-05 14:55:47 +02:00
|
|
|
|
2021-07-05 17:29:37 +02:00
|
|
|
# Attempt to extract a valid network interface device
|
2021-07-09 05:55:23 +02:00
|
|
|
try:
|
|
|
|
self.__interface = netifaces.gateways()["default"][netifaces.AF_INET][1]
|
|
|
|
except Exception:
|
|
|
|
self.__interface = None
|
2021-07-05 14:55:47 +02:00
|
|
|
|
2021-07-08 16:04:40 +02:00
|
|
|
# Check to see if the interface (if connected to the internet) is wireless
|
2021-07-09 05:55:23 +02:00
|
|
|
if self.__is_connected and self.__interface:
|
|
|
|
try:
|
|
|
|
with open("/proc/net/wireless", "r") as f:
|
|
|
|
self.__is_wireless = self.__interface in f.read()
|
|
|
|
f.close()
|
|
|
|
except Exception:
|
|
|
|
self.__is_wireless = False
|
2021-07-05 14:55:47 +02:00
|
|
|
|
2021-07-05 17:29:37 +02:00
|
|
|
# setup message to send to the user
|
2021-07-09 05:55:23 +02:00
|
|
|
if not self.__is_connected or not self.__interface:
|
2021-07-08 16:04:40 +02:00
|
|
|
self.__message = "No connection"
|
|
|
|
elif not self.__is_wireless:
|
|
|
|
# Assuming that if user is connected via non-wireless means that it will be ethernet
|
|
|
|
self.__signal = -30
|
|
|
|
self.__message = "Ethernet"
|
|
|
|
else:
|
|
|
|
# We have a wireless connection
|
2021-07-05 20:54:28 +02:00
|
|
|
iw_dat = util.cli.execute("iwgetid")
|
2021-07-05 17:29:37 +02:00
|
|
|
has_ssid = "ESSID" in iw_dat
|
2021-07-08 16:04:40 +02:00
|
|
|
signal = self.__compute_signal(self.__interface)
|
2021-07-09 05:55:23 +02:00
|
|
|
|
|
|
|
# If signal is None, that means that we can't compute the default interface's signal strength
|
|
|
|
self.__signal = (
|
|
|
|
util.format.asint(signal, minimum=-110, maximum=-30) if signal else None
|
|
|
|
)
|
2021-07-08 16:04:40 +02:00
|
|
|
|
2021-07-05 20:54:28 +02:00
|
|
|
ssid = (
|
|
|
|
iw_dat[iw_dat.index(":") + 1 :].replace('"', "").strip()
|
|
|
|
if has_ssid
|
|
|
|
else "Unknown"
|
|
|
|
)
|
2021-07-05 20:09:17 +02:00
|
|
|
self.__message = self.__generate_wireles_message(ssid, self.__signal)
|
2021-07-05 14:55:47 +02:00
|
|
|
|
2021-07-05 20:09:17 +02:00
|
|
|
return self.__message
|
2021-07-05 14:55:47 +02:00
|
|
|
|
2021-07-08 16:04:40 +02:00
|
|
|
# State determined by signal strength
|
2021-07-05 18:26:46 +02:00
|
|
|
def state(self, widget):
|
2021-07-08 16:04:40 +02:00
|
|
|
if self.__compute_strength(self.__signal) < 50:
|
2021-07-05 18:26:46 +02:00
|
|
|
return "critical"
|
2021-07-08 16:04:40 +02:00
|
|
|
if self.__compute_strength(self.__signal) < 75:
|
2021-07-05 20:09:17 +02:00
|
|
|
return "warning"
|
|
|
|
|
2021-07-05 18:26:46 +02:00
|
|
|
return None
|
|
|
|
|
2021-07-05 20:54:28 +02:00
|
|
|
# manually done for better granularity / ease of parsing strength data
|
2021-07-08 16:04:40 +02:00
|
|
|
def __generate_wireles_message(self, ssid, signal):
|
|
|
|
computed_strength = self.__compute_strength(signal)
|
2021-07-09 05:55:23 +02:00
|
|
|
strength_str = str(computed_strength) if computed_strength else "?"
|
|
|
|
|
|
|
|
return "{} {}%".format(ssid, strength_str)
|
2021-07-08 16:04:40 +02:00
|
|
|
|
|
|
|
def __compute_strength(self, signal):
|
2021-07-09 05:55:23 +02:00
|
|
|
return int(100 * ((signal + 100) / 70.0)) if signal else None
|
2021-07-08 16:04:40 +02:00
|
|
|
|
|
|
|
# get signal strength in decibels/milliwat
|
|
|
|
def __compute_signal(self, interface):
|
|
|
|
# Get connection strength
|
|
|
|
cmd = "iwconfig {}".format(interface)
|
|
|
|
config_dat = " ".join(util.cli.execute(cmd).split())
|
|
|
|
config_tokens = config_dat.replace("=", " ").split()
|
2021-07-09 05:55:23 +02:00
|
|
|
|
|
|
|
# handle weird output
|
|
|
|
try:
|
|
|
|
signal = config_tokens[config_tokens.index("level") + 1]
|
|
|
|
except Exception:
|
|
|
|
signal = None
|
2021-07-08 16:04:40 +02:00
|
|
|
|
|
|
|
return signal
|