From 318df8723a5ff87084a19d1707d3f844cccfa652 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Wed, 15 Jan 2020 14:49:12 -0800 Subject: [PATCH] wireguard: Enable/disable connections along with the app Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- plinth/modules/wireguard/__init__.py | 3 +++ plinth/modules/wireguard/utils.py | 29 +++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/plinth/modules/wireguard/__init__.py b/plinth/modules/wireguard/__init__.py index 341cfa402..b359865a5 100644 --- a/plinth/modules/wireguard/__init__.py +++ b/plinth/modules/wireguard/__init__.py @@ -26,6 +26,7 @@ from plinth import cfg, frontpage, menu from plinth.modules.firewall.components import Firewall from plinth.utils import format_lazy, import_from_gi +from . import utils from .manifest import clients # noqa, pylint: disable=unused-import nm = import_from_gi('NM', '1.0') @@ -89,12 +90,14 @@ class WireguardApp(app_module.App): from plinth import kvstore super().enable() kvstore.set('wireguard-enabled', True) + utils.enable_connections(True) def disable(self): """Disable the app by simply storing a flag in key/value store.""" from plinth import kvstore super().disable() kvstore.set('wireguard-enabled', False) + utils.enable_connections(False) def is_enabled(self): """Return whether all leader components are enabled and flag is set.""" diff --git a/plinth/modules/wireguard/utils.py b/plinth/modules/wireguard/utils.py index 09ed58142..d6a8ec8e3 100644 --- a/plinth/modules/wireguard/utils.py +++ b/plinth/modules/wireguard/utils.py @@ -114,6 +114,27 @@ def get_info(): } +def enable_connections(enable): + """Activate all connections and set them to auto-connect.""" + setting_name = nm.SETTING_WIREGUARD_SETTING_NAME + client = network.get_nm_client() + for connection in client.get_connections(): + if connection.get_connection_type() != setting_name: + continue + + network.edit_connection(connection, + {'common': { + 'autoconnect': enable + }}) + if enable: + network.activate_connection(connection.get_uuid()) + else: + try: + network.deactivate_connection(connection.get_uuid()) + except network.ConnectionNotFound: + pass # Connection is already inactive + + def _generate_private_key(): """Return a private key generated by 'wg' command.""" process = subprocess.run(['wg', 'genkey'], check=True, capture_output=True) @@ -136,9 +157,12 @@ def _find_next_interface(): def add_server(settings): """Add information for connecting to a server.""" + from plinth.modules.wireguard import app + interface_name = _find_next_interface() settings['common']['name'] = 'WireGuard-Client-' + interface_name settings['common']['interface'] = interface_name + settings['common']['autoconnect'] = app.is_enabled() if not settings['wireguard']['private_key']: settings['wireguard']['private_key'] = _generate_private_key() @@ -158,6 +182,8 @@ def edit_server(interface, settings): def setup_server(): """Setup a server connection that clients can connect to.""" + from plinth.modules.wireguard import app + setting_name = nm.SETTING_WIREGUARD_SETTING_NAME private_key = _generate_private_key() settings = { @@ -165,7 +191,8 @@ def setup_server(): 'name': 'WireGuard-Server-wg0', 'type': setting_name, 'zone': 'internal', - 'interface': 'wg0' + 'interface': 'wg0', + 'autoconnect': app.is_enabled(), }, 'ipv4': { 'method': 'manual',