From a7a1da6b5daa2bd6af073cc90e78a5086ed045b2 Mon Sep 17 00:00:00 2001 From: Bernhard B Date: Mon, 10 Jun 2019 20:05:32 +0200 Subject: [PATCH 1/2] added vpn module * shows current VPN connection * allows to establish a VPN connection --- bumblebee/modules/vpn.py | 90 +++++++++++++++++++++++++++++++++ themes/icons/awesome-fonts.json | 3 ++ 2 files changed, 93 insertions(+) create mode 100644 bumblebee/modules/vpn.py diff --git a/bumblebee/modules/vpn.py b/bumblebee/modules/vpn.py new file mode 100644 index 0000000..acb2e05 --- /dev/null +++ b/bumblebee/modules/vpn.py @@ -0,0 +1,90 @@ +# pylint: disable=C0111,R0903 + +""" Displays the VPN profile that is currently in use. + + Left click opens a popup menu that lists all available VPN profiles and allows to establish + a VPN connection using that profile. + + Prerequisites: + * nmcli needs to be installed and configured properly. + To quickly test, whether nmcli is working correctly, type "nmcli -g NAME,TYPE,DEVICE con" which + lists all the connection profiles that are configured. Make sure that your VPN profile is in that list! + + e.g: to import a openvpn profile via nmcli: + sudo nmcli connection import type openvpn file +""" + +import logging +import bumblebee.input +import bumblebee.output +import bumblebee.engine +import functools + +class Module(bumblebee.engine.Module): + def __init__(self, engine, config): + super(Module, self).__init__(engine, config, + bumblebee.output.Widget(full_text=self.vpn_status) + ) + + self._connected_vpn_profile = None + self._selected_vpn_profile = None + + res = bumblebee.util.execute("nmcli -g NAME,TYPE c") + lines = res.splitlines() + + self._vpn_profiles = [] + for line in lines: + info = line.split(':') + if info[1] == "vpn": + self._vpn_profiles.append(info[0]) + + engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, + cmd=self.popup) + + def update(self, widgets): + try: + res = bumblebee.util.execute("nmcli -g NAME,TYPE,DEVICE con") + lines = res.splitlines() + self._connected_vpn_profile = None + for line in lines: + info = line.split(':') + if info[1] == "vpn" and info[2] != "": + self._connected_vpn_profile = info[0] + + except Exception as e: + logging.exception("Couldn't get VPN status") + self._connected_vpn_profile = None + + def vpn_status(self, widget): + if self._connected_vpn_profile is None: + return "off" + return self._connected_vpn_profile + + def _on_vpn_disconnect(self): + try: + bumblebee.util.execute("nmcli c down " + self._connected_vpn_profile) + self._connected_vpn_profile = None + except Exception as e: + logging.exception("Couldn't disconnect VPN connection") + + def _on_vpn_connect(self, name): + self._selected_vpn_profile = name + + try: + bumblebee.util.execute("nmcli c up " + self._selected_vpn_profile) + self._connected_vpn_profile = name + except Exception as e: + logging.exception("Couldn't establish VPN connection") + self._connected_vpn_profile = None + + def popup(self, widget): + menu = bumblebee.popup_v2.PopupMenu() + + if self._connected_vpn_profile is not None: + menu.add_menuitem("Off", callback=self._on_vpn_disconnect) + for vpn_profile in self._vpn_profiles: + menu.add_menuitem(vpn_profile, callback=functools.partial(self._on_vpn_connect, vpn_profile)) + menu.show(widget) + + def state(self, widget): + return [] diff --git a/themes/icons/awesome-fonts.json b/themes/icons/awesome-fonts.json index daa370c..cb904e5 100644 --- a/themes/icons/awesome-fonts.json +++ b/themes/icons/awesome-fonts.json @@ -196,5 +196,8 @@ "pihole": { "enabled": { "prefix": "" }, "disabled": { "prefix": "" } + }, + "vpn": { + "prefix": "" } } From c7a0caeda5ee167e8f00cfac4a08307a59ac8853 Mon Sep 17 00:00:00 2001 From: Bernhard B Date: Mon, 10 Jun 2019 20:12:54 +0200 Subject: [PATCH 2/2] small improvements in vpn module * renamed "Off" to "Disconnect" * do not show already established connection in popup menu --- bumblebee/modules/vpn.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bumblebee/modules/vpn.py b/bumblebee/modules/vpn.py index acb2e05..149e547 100644 --- a/bumblebee/modules/vpn.py +++ b/bumblebee/modules/vpn.py @@ -81,8 +81,10 @@ class Module(bumblebee.engine.Module): menu = bumblebee.popup_v2.PopupMenu() if self._connected_vpn_profile is not None: - menu.add_menuitem("Off", callback=self._on_vpn_disconnect) + menu.add_menuitem("Disconnect", callback=self._on_vpn_disconnect) for vpn_profile in self._vpn_profiles: + if self._connected_vpn_profile is not None and self._connected_vpn_profile == vpn_profile: + continue menu.add_menuitem(vpn_profile, callback=functools.partial(self._on_vpn_connect, vpn_profile)) menu.show(widget)