From bc2ca23c9384e301fd2605661d0c3adca30a7a93 Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Sun, 17 Mar 2024 13:34:02 +0100 Subject: [PATCH] VPN: WireGuard: Peers - add instances column and move backreference logic to model using the volatile switch recently introduced for the Trust section. closes https://github.com/opnsense/core/issues/7221 --- .../Wireguard/Api/ClientController.php | 36 +++++++---- .../app/models/OPNsense/Wireguard/Client.xml | 12 +++- .../Wireguard/FieldTypes/ClientField.php | 59 +++++++++++++++++ .../mvc/app/views/OPNsense/Trust/cert.volt | 4 +- .../app/views/OPNsense/Wireguard/general.volt | 64 +++++++++++++++---- 5 files changed, 147 insertions(+), 28 deletions(-) create mode 100644 src/opnsense/mvc/app/models/OPNsense/Wireguard/FieldTypes/ClientField.php diff --git a/src/opnsense/mvc/app/controllers/OPNsense/Wireguard/Api/ClientController.php b/src/opnsense/mvc/app/controllers/OPNsense/Wireguard/Api/ClientController.php index 26e98667f13..1b7cec16435 100644 --- a/src/opnsense/mvc/app/controllers/OPNsense/Wireguard/Api/ClientController.php +++ b/src/opnsense/mvc/app/controllers/OPNsense/Wireguard/Api/ClientController.php @@ -44,27 +44,39 @@ public function pskAction() return ['psk' => trim((new Backend())->configdRun('wireguard gen_psk')), 'status' => 'ok' ]; } + public function listServersAction() + { + if ($this->request->isGet()) { + $results = ['rows' => [], 'status' => 'ok']; + foreach ((new Server())->servers->server->iterateItems() as $key => $node) { + $results['rows'][] = [ + 'uuid' => $key, + 'name' => (string)$node->name + ]; + } + return $results; + } + return ['status' => 'failed']; + } + public function searchClientAction() { + $servers = $this->request->get('servers'); + $filter_funct = function ($record) use ($servers) { + return empty($servers) || array_intersect(explode(',', $record->servers), $servers); + }; + return $this->searchBase( 'clients.client', - ["enabled", "name", "pubkey", "tunneladdress", "serveraddress", "serverport"] + ["enabled", "name", "pubkey", "tunneladdress", "serveraddress", "serverport", "servers"], + null, + $filter_funct ); } public function getClientAction($uuid = null) { - $result = $this->getBase('client', 'clients.client', $uuid); - if (!empty($result['client'])) { - $result['client']['servers'] = []; - foreach ((new Server())->servers->server->iterateItems() as $key => $node) { - $result['client']['servers'][$key] = [ - 'value' => (string)$node->name, - 'selected' => in_array($uuid, explode(',', (string)$node->peers)) ? '1' : '0' - ]; - } - } - return $result; + return $this->getBase('client', 'clients.client', $uuid); } public function addClientAction() diff --git a/src/opnsense/mvc/app/models/OPNsense/Wireguard/Client.xml b/src/opnsense/mvc/app/models/OPNsense/Wireguard/Client.xml index cb498d80826..f317ced7210 100644 --- a/src/opnsense/mvc/app/models/OPNsense/Wireguard/Client.xml +++ b/src/opnsense/mvc/app/models/OPNsense/Wireguard/Client.xml @@ -4,7 +4,7 @@ 1.0.0 - + 1 Y @@ -53,6 +53,16 @@ 86400 Please specify a value between 1 and 86400. + + + + + Y + diff --git a/src/opnsense/mvc/app/models/OPNsense/Wireguard/FieldTypes/ClientField.php b/src/opnsense/mvc/app/models/OPNsense/Wireguard/FieldTypes/ClientField.php new file mode 100644 index 00000000000..4d2d25788ed --- /dev/null +++ b/src/opnsense/mvc/app/models/OPNsense/Wireguard/FieldTypes/ClientField.php @@ -0,0 +1,59 @@ +servers->server->iterateItems() as $key => $node) { + if (!empty((string)$node->peers)) { + foreach (explode(',', (string)$node->peers) as $peer) { + if (!isset($peers[$peer])) { + $peers[$peer] = []; + } + $peers[$peer][] = $key; + } + } + } + foreach ($this->internalChildnodes as $key => $node) { + if (isset($peers[$key])) { + $node->servers->setValue(implode(',', $peers[$key])); + } + } + return parent::actionPostLoadingEvent(); + } +} diff --git a/src/opnsense/mvc/app/views/OPNsense/Trust/cert.volt b/src/opnsense/mvc/app/views/OPNsense/Trust/cert.volt index 5db6ad2753f..343fa1228c4 100644 --- a/src/opnsense/mvc/app/views/OPNsense/Trust/cert.volt +++ b/src/opnsense/mvc/app/views/OPNsense/Trust/cert.volt @@ -35,8 +35,8 @@ del:'/api/trust/cert/del/', options:{ requestHandler: function(request){ - if ( $('#ca_filter').val().length > 0) { - request['carefs'] = $('#ca_filter').val(); + if ( $('#server_filter').val().length > 0) { + request['servers'] = $('#server_filter').val(); } return request; } diff --git a/src/opnsense/mvc/app/views/OPNsense/Wireguard/general.volt b/src/opnsense/mvc/app/views/OPNsense/Wireguard/general.volt index 55f0e46d984..4b58d55a49b 100644 --- a/src/opnsense/mvc/app/views/OPNsense/Wireguard/general.volt +++ b/src/opnsense/mvc/app/views/OPNsense/Wireguard/general.volt @@ -33,25 +33,49 @@ $('.selectpicker').selectpicker('refresh'); }); - $("#grid-peers").UIBootgrid( + let grid_peers = $("#grid-peers").UIBootgrid( { - 'search':'/api/wireguard/client/searchClient', - 'get':'/api/wireguard/client/getClient/', - 'set':'/api/wireguard/client/setClient/', - 'add':'/api/wireguard/client/addClient/', - 'del':'/api/wireguard/client/delClient/', - 'toggle':'/api/wireguard/client/toggleClient/' + search: '/api/wireguard/client/searchClient', + get: '/api/wireguard/client/getClient/', + set: '/api/wireguard/client/setClient/', + add: '/api/wireguard/client/addClient/', + del: '/api/wireguard/client/delClient/', + toggle: '/api/wireguard/client/toggleClient/', + options:{ + requestHandler: function(request){ + if ( $('#server_filter').val().length > 0) { + request['servers'] = $('#server_filter').val(); + } + return request; + } + }, + + } ); + grid_peers.on("loaded.rs.jquery.bootgrid", function (e){ + // reload servers before grid load + if ($("#server_filter > option").length == 0) { + ajaxGet('/api/wireguard/client/list_servers', {}, function(data, status){ + if (data.rows !== undefined) { + for (let i=0; i < data.rows.length ; ++i) { + let row = data.rows[i]; + $("#server_filter").append($("