bumblebee-status/modules/contrib/vpn.py

117 lines
3.8 KiB
Python
Raw Normal View History

2020-04-26 10:38:40 +02:00
# 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:
* tk python library (usually python-tk or python3-tk, depending on your distribution)
* nmcli needs to be installed and configured properly.
2020-04-26 10:42:19 +02:00
To quickly test, whether nmcli is working correctly, type 'nmcli -g NAME,TYPE,DEVICE con' which
2020-04-26 10:38:40 +02:00
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:
2020-05-06 12:57:38 +02:00
`sudo nmcli connection import type openvpn file </path/to/your/openvpn/profile.ovpn>`
contributed by `bbernhard <https://github.com/bbernhard>`_ - many thanks!
2020-04-26 10:38:40 +02:00
"""
import logging
import functools
2020-04-26 10:42:19 +02:00
import core.module
import core.widget
import core.input
2020-04-26 10:38:40 +02:00
2020-04-26 10:42:19 +02:00
import util.cli
import util.popup
2020-04-26 10:38:40 +02:00
2020-04-26 10:42:19 +02:00
class Module(core.module.Module):
def __init__(self, config, theme):
super().__init__(config, theme, core.widget.Widget(self.vpn_status))
2020-04-26 10:42:19 +02:00
self.__connected_vpn_profile = None
self.__selected_vpn_profile = None
res = util.cli.execute("nmcli -g NAME,TYPE c")
2020-04-26 10:38:40 +02:00
lines = res.splitlines()
2020-04-26 10:42:19 +02:00
self.__vpn_profiles = []
2020-04-26 10:38:40 +02:00
for line in lines:
info = line.split(":")
2020-04-26 10:38:40 +02:00
try:
2020-04-26 10:42:19 +02:00
if self.__isvpn(info[1]):
self.__vpn_profiles.append(info[0])
2020-04-26 10:38:40 +02:00
except:
pass
core.input.register(self, button=core.input.LEFT_MOUSE, cmd=self.popup)
2020-04-26 10:38:40 +02:00
2020-04-26 10:42:19 +02:00
def __isvpn(self, connection_type):
return connection_type in ["vpn", "wireguard"]
2020-04-26 10:38:40 +02:00
2020-04-26 10:42:19 +02:00
def update(self):
2020-04-26 10:38:40 +02:00
try:
res = util.cli.execute("nmcli -g NAME,TYPE,DEVICE con")
2020-04-26 10:38:40 +02:00
lines = res.splitlines()
2020-04-26 10:42:19 +02:00
self.__connected_vpn_profile = None
2020-04-26 10:38:40 +02:00
for line in lines:
info = line.split(":")
if self.__isvpn(info[1]) and info[2] != "":
2020-04-26 10:42:19 +02:00
self.__connected_vpn_profile = info[0]
2020-04-26 10:38:40 +02:00
except Exception as e:
logging.exception("Could not get VPN status")
2020-04-26 10:42:19 +02:00
self.__connected_vpn_profile = None
2020-04-26 10:38:40 +02:00
def vpn_status(self, widget):
2020-04-26 10:42:19 +02:00
if self.__connected_vpn_profile is None:
return "off"
2020-04-26 10:42:19 +02:00
return self.__connected_vpn_profile
2020-04-26 10:38:40 +02:00
2020-04-26 10:42:19 +02:00
def __on_vpndisconnect(self):
2020-04-26 10:38:40 +02:00
try:
util.cli.execute(
"nmcli c down '{vpn}'".format(vpn=self.__connected_vpn_profile)
)
2020-04-26 10:42:19 +02:00
self.__connected_vpn_profile = None
2020-04-26 10:38:40 +02:00
except Exception as e:
logging.exception("Could not disconnect VPN connection")
2020-04-26 10:38:40 +02:00
2020-04-26 10:42:19 +02:00
def __on_vpnconnect(self, name):
self.__selected_vpn_profile = name
2020-04-26 10:38:40 +02:00
try:
util.cli.execute(
"nmcli c up '{vpn}'".format(vpn=self.__selected_vpn_profile)
)
2020-04-26 10:42:19 +02:00
self.__connected_vpn_profile = name
2020-04-26 10:38:40 +02:00
except Exception as e:
logging.exception("Could not establish VPN connection")
2020-04-26 10:42:19 +02:00
self.__connected_vpn_profile = None
2020-04-26 10:38:40 +02:00
def popup(self, widget):
2020-04-26 10:42:19 +02:00
menu = util.popup.menu()
2020-04-26 10:38:40 +02:00
2020-04-26 10:42:19 +02:00
if self.__connected_vpn_profile is not None:
menu.add_menuitem("Disconnect", callback=self.__on_vpndisconnect)
2020-04-26 10:42:19 +02:00
for vpn_profile in self.__vpn_profiles:
if (
self.__connected_vpn_profile is not None
and self.__connected_vpn_profile == vpn_profile
):
2020-04-26 10:38:40 +02:00
continue
menu.add_menuitem(
vpn_profile,
callback=functools.partial(self.__on_vpnconnect, vpn_profile),
)
2020-04-26 10:38:40 +02:00
menu.show(widget)
def state(self, widget):
return []
2020-04-26 10:42:19 +02:00
2020-04-26 10:42:19 +02:00
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4