[modules/vpn] Update to latest API
This commit is contained in:
parent
f44ff3fd34
commit
354d723b7c
1 changed files with 50 additions and 47 deletions
|
@ -8,7 +8,7 @@
|
||||||
Prerequisites:
|
Prerequisites:
|
||||||
* tk python library (usually python-tk or python3-tk, depending on your distribution)
|
* tk python library (usually python-tk or python3-tk, depending on your distribution)
|
||||||
* nmcli needs to be installed and configured properly.
|
* nmcli needs to be installed and configured properly.
|
||||||
To quickly test, whether nmcli is working correctly, type "nmcli -g NAME,TYPE,DEVICE con" which
|
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!
|
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:
|
e.g: to import a openvpn profile via nmcli:
|
||||||
|
@ -16,87 +16,90 @@
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import bumblebee.input
|
|
||||||
import bumblebee.output
|
|
||||||
import bumblebee.engine
|
|
||||||
import bumblebee.popup_v2
|
|
||||||
import functools
|
import functools
|
||||||
|
|
||||||
class Module(bumblebee.engine.Module):
|
import core.module
|
||||||
def __init__(self, engine, config):
|
import core.widget
|
||||||
super(Module, self).__init__(engine, config,
|
import core.input
|
||||||
bumblebee.output.Widget(full_text=self.vpn_status)
|
|
||||||
)
|
|
||||||
|
|
||||||
self._connected_vpn_profile = None
|
import util.cli
|
||||||
self._selected_vpn_profile = None
|
import util.popup
|
||||||
|
|
||||||
res = bumblebee.util.execute("nmcli -g NAME,TYPE c")
|
class Module(core.module.Module):
|
||||||
|
def __init__(self, config):
|
||||||
|
super().__init__(config, core.widget.Widget(self.vpn_status))
|
||||||
|
|
||||||
|
self.__connected_vpn_profile = None
|
||||||
|
self.__selected_vpn_profile = None
|
||||||
|
|
||||||
|
res = util.cli.execute('nmcli -g NAME,TYPE c')
|
||||||
lines = res.splitlines()
|
lines = res.splitlines()
|
||||||
|
|
||||||
self._vpn_profiles = []
|
self.__vpn_profiles = []
|
||||||
for line in lines:
|
for line in lines:
|
||||||
info = line.split(':')
|
info = line.split(':')
|
||||||
try:
|
try:
|
||||||
if self._isvpn(info[1]):
|
if self.__isvpn(info[1]):
|
||||||
self._vpn_profiles.append(info[0])
|
self.__vpn_profiles.append(info[0])
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE,
|
core.input.register(self, button=core.input.LEFT_MOUSE,
|
||||||
cmd=self.popup)
|
cmd=self.popup)
|
||||||
|
|
||||||
def _isvpn(self, connection_type):
|
def __isvpn(self, connection_type):
|
||||||
return connection_type in ["vpn", "wireguard"]
|
return connection_type in ['vpn', 'wireguard']
|
||||||
|
|
||||||
def update(self, widgets):
|
def update(self):
|
||||||
try:
|
try:
|
||||||
res = bumblebee.util.execute("nmcli -g NAME,TYPE,DEVICE con")
|
res = util.cli.execute('nmcli -g NAME,TYPE,DEVICE con')
|
||||||
lines = res.splitlines()
|
lines = res.splitlines()
|
||||||
self._connected_vpn_profile = None
|
self.__connected_vpn_profile = None
|
||||||
for line in lines:
|
for line in lines:
|
||||||
info = line.split(':')
|
info = line.split(':')
|
||||||
if self._isvpn(info[1]) and info[2] != "":
|
if self.__isvpn(info[1]) and info[2] != '':
|
||||||
self._connected_vpn_profile = info[0]
|
self.__connected_vpn_profile = info[0]
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.exception("Couldn't get VPN status")
|
logging.exception('Could not get VPN status')
|
||||||
self._connected_vpn_profile = None
|
self.__connected_vpn_profile = None
|
||||||
|
|
||||||
def vpn_status(self, widget):
|
def vpn_status(self, widget):
|
||||||
if self._connected_vpn_profile is None:
|
if self.__connected_vpn_profile is None:
|
||||||
return "off"
|
return 'off'
|
||||||
return self._connected_vpn_profile
|
return self.__connected_vpn_profile
|
||||||
|
|
||||||
def _on_vpn_disconnect(self):
|
def __on_vpndisconnect(self):
|
||||||
try:
|
try:
|
||||||
bumblebee.util.execute("nmcli c down \"{vpn}\""
|
util.lci.execute('nmcli c down \'{vpn}\''
|
||||||
.format(vpn=self._connected_vpn_profile))
|
.format(vpn=self.__connected_vpn_profile))
|
||||||
self._connected_vpn_profile = None
|
self.__connected_vpn_profile = None
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.exception("Couldn't disconnect VPN connection")
|
logging.exception('Could not disconnect VPN connection')
|
||||||
|
|
||||||
def _on_vpn_connect(self, name):
|
def __on_vpnconnect(self, name):
|
||||||
self._selected_vpn_profile = name
|
self.__selected_vpn_profile = name
|
||||||
|
|
||||||
try:
|
try:
|
||||||
bumblebee.util.execute("nmcli c up \"{vpn}\""
|
util.cli.execute('nmcli c up \'{vpn}\''
|
||||||
.format(vpn=self._selected_vpn_profile))
|
.format(vpn=self.__selected_vpn_profile))
|
||||||
self._connected_vpn_profile = name
|
self.__connected_vpn_profile = name
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.exception("Couldn't establish VPN connection")
|
logging.exception('Could not establish VPN connection')
|
||||||
self._connected_vpn_profile = None
|
self.__connected_vpn_profile = None
|
||||||
|
|
||||||
def popup(self, widget):
|
def popup(self, widget):
|
||||||
menu = bumblebee.popup_v2.PopupMenu()
|
menu = util.popup.menu()
|
||||||
|
|
||||||
if self._connected_vpn_profile is not None:
|
if self.__connected_vpn_profile is not None:
|
||||||
menu.add_menuitem("Disconnect", callback=self._on_vpn_disconnect)
|
menu.add_menuitem('Disconnect', callback=self.__on_vpndisconnect)
|
||||||
for vpn_profile in self._vpn_profiles:
|
for vpn_profile in self.__vpn_profiles:
|
||||||
if self._connected_vpn_profile is not None and self._connected_vpn_profile == vpn_profile:
|
if self.__connected_vpn_profile is not None and self.__connected_vpn_profile == vpn_profile:
|
||||||
continue
|
continue
|
||||||
menu.add_menuitem(vpn_profile, callback=functools.partial(self._on_vpn_connect, vpn_profile))
|
menu.add_menuitem(vpn_profile, callback=functools.partial(self.__on_vpnconnect, vpn_profile))
|
||||||
menu.show(widget)
|
menu.show(widget)
|
||||||
|
|
||||||
def state(self, widget):
|
def state(self, widget):
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||||
|
|
Loading…
Reference in a new issue