diff --git a/actions/wireguard b/plinth/modules/wireguard/privileged.py old mode 100755 new mode 100644 similarity index 56% rename from actions/wireguard rename to plinth/modules/wireguard/privileged.py index 48ab2627f..34a61d0f0 --- a/actions/wireguard +++ b/plinth/modules/wireguard/privileged.py @@ -1,40 +1,27 @@ -#!/usr/bin/python3 # SPDX-License-Identifier: AGPL-3.0-or-later -""" -Configuration helper for WireGuard. -""" +"""Configuration helper for WireGuard.""" -import argparse -import json import subprocess +from plinth.actions import privileged + SERVER_INTERFACE = 'wg0' -def parse_arguments(): - """Return parsed command line arguments as dictionary.""" - parser = argparse.ArgumentParser() - subparsers = parser.add_subparsers(dest='subcommand', help='Sub command') - - subparsers.add_parser('get-info', - help='Get info for each configured interface') - - subparsers.required = True - return parser.parse_args() - - -def _get_info(): +@privileged +def get_info() -> dict[str, dict]: """Return info for each configured interface.""" output = subprocess.check_output(['wg', 'show', 'all', 'dump']).decode().strip() lines = output.split('\n') - interfaces = {} + interfaces: dict[str, dict] = {} for line in lines: if not line: continue - fields = line.split() - fields = [field if field != '(none)' else None for field in fields] + fields = [ + field if field != '(none)' else None for field in line.split() + ] interface_name = fields[0] if interface_name in interfaces: latest_handshake = int(fields[5]) if int(fields[5]) else None @@ -61,21 +48,3 @@ def _get_info(): } return interfaces - - -def subcommand_get_info(_): - """Print info for each configured interface.""" - print(json.dumps(_get_info())) - - -def main(): - """Parse arguments and perform all duties.""" - arguments = parse_arguments() - - subcommand = arguments.subcommand.replace('-', '_') - subcommand_method = globals()['subcommand_' + subcommand] - subcommand_method(arguments) - - -if __name__ == '__main__': - main() diff --git a/plinth/modules/wireguard/utils.py b/plinth/modules/wireguard/utils.py index 2db3d19de..2d7b56c79 100644 --- a/plinth/modules/wireguard/utils.py +++ b/plinth/modules/wireguard/utils.py @@ -1,19 +1,17 @@ # SPDX-License-Identifier: AGPL-3.0-or-later -""" -Utilities for managing WireGuard. -""" +"""Utilities for managing WireGuard.""" import datetime -import json import logging import subprocess import time -from plinth import actions from plinth import app as app_module from plinth import network from plinth.utils import import_from_gi +from . import privileged + nm = import_from_gi('NM', '1.0') IP_TEMPLATE = '10.84.0.{}' @@ -70,8 +68,7 @@ def get_nm_info(): def get_info(): """Return server and clients info.""" - output = actions.superuser_run('wireguard', ['get-info']) - status = json.loads(output) + status = privileged.get_info() nm_info = get_nm_info()