From 2cc36105daca9d047be31af936fcae154a6e5461 Mon Sep 17 00:00:00 2001 From: Monviech <79600909+Monviech@users.noreply.github.com> Date: Wed, 19 Feb 2025 08:58:54 +0100 Subject: [PATCH] vpn/wireguard: Change tracking of wg peer status, improve widget and diagnostics (#8337) * vpn/wireguard: Introduce latest-handshake-age to calculate if tunnel is online in backend. Implement it in wireguard.js widget and diagnostics.volt * vpn/wireguard: expose peer-connected via API to approximate state of wireguard peers online/offline status, change status formatter to show statos of interfaces and peers, improve diagnostic grid * vpn/wireguard: Move epoch calculation from frontend to controller * vpn/wireguard: Track 3 different status instead of a boolean offline/online. Online means a handshake happened recently, Stale means a handshake happened in the past above a threshold of 300s, Offline means there was never a handshake yet. The same icons are implemented in the widget and the wireguard diagnostics page. * vpn/wireguard: Remote peer disconnected translation since this is tracked by the icon now. Add stale translation. * vpn/wireguard: Compact widget information for better readability --- .../Wireguard/Api/ServiceController.php | 20 +++++ .../views/OPNsense/Wireguard/diagnostics.volt | 44 ++++++++-- src/opnsense/www/js/widgets/Metadata/Core.xml | 3 +- src/opnsense/www/js/widgets/Wireguard.js | 86 +++++++++++++------ 4 files changed, 116 insertions(+), 37 deletions(-) diff --git a/src/opnsense/mvc/app/controllers/OPNsense/Wireguard/Api/ServiceController.php b/src/opnsense/mvc/app/controllers/OPNsense/Wireguard/Api/ServiceController.php index 0d11d5722..7a97615be 100644 --- a/src/opnsense/mvc/app/controllers/OPNsense/Wireguard/Api/ServiceController.php +++ b/src/opnsense/mvc/app/controllers/OPNsense/Wireguard/Api/ServiceController.php @@ -95,6 +95,26 @@ class ServiceController extends ApiMutableServiceControllerBase $record['name'] = $key_descriptions[$key]; } } + + if (!empty($record['latest-handshake'])) { + $record['latest-handshake-age'] = time() - (int)$record['latest-handshake']; + $record['latest-handshake-epoch'] = date('Y-m-d H:i:s', (int)$record['latest-handshake']); + } else { + $record['latest-handshake-age'] = null; + $record['latest-handshake-epoch'] = null; + } + + // Peer is considered online if handshake was within 300s, wg handshakes approx every 120s. + if ($record['type'] === 'peer' && !is_null($record['latest-handshake-age'])) { + if ($record['latest-handshake-age'] <= 300) { + $record['peer-status'] = 'online'; + } elseif ($record['latest-handshake-age'] > 300) { + $record['peer-status'] = 'stale'; + } + } else { + $record['peer-status'] = 'offline'; + } + $record['ifname'] = $ifnames[$record['if']]; } $filter_funct = null; diff --git a/src/opnsense/mvc/app/views/OPNsense/Wireguard/diagnostics.volt b/src/opnsense/mvc/app/views/OPNsense/Wireguard/diagnostics.volt index af3cf7631..35e885e93 100644 --- a/src/opnsense/mvc/app/views/OPNsense/Wireguard/diagnostics.volt +++ b/src/opnsense/mvc/app/views/OPNsense/Wireguard/diagnostics.volt @@ -41,13 +41,36 @@ return row[column.id]; }, epoch: function(column, row) { - if (row[column.id]) { - return moment.unix(row[column.id]).local().format('YYYY-MM-DD HH:mm:ss'); + if (row[column.id] !== null) { + return row[column.id] } else { return ''; } + }, + seconds: function(column, row) { + if (row[column.id] !== null) { + return row[column.id] + "s"; + } else { + return ''; + } + }, + status: function(column, row) { + if (row.type === 'peer' && row['peer-status'] === 'stale') { + return ''; + } + + if ( + (row.type === 'interface' && row.status === 'up') || + (row.type === 'peer' && row['peer-status'] === 'online') + ) { + return ''; + } + + return ''; + }, + + - } }, requestHandler: function(request){ if ( $('#type_filter').val().length > 0) { @@ -62,6 +85,10 @@ $('#grid-sessions').bootgrid('reload'); }); + $("#grid-sessions").on('loaded.rs.jquery.bootgrid', function() { + $('[data-toggle="tooltip"]').tooltip(); + }); + $("#type_filter_container").detach().prependTo('#grid-sessions-header > .row > .actionBar > .actions'); }); @@ -80,13 +107,14 @@
| {{ lang._('Device') }} | -{{ lang._('Type') }} | -{{ lang._('Status') }} | -{{ lang._('Public key') }} | +{{ lang._('Status') }} | +{{ lang._('Device') }} | +{{ lang._('Type') }} | +{{ lang._('Public key') }} | {{ lang._('Name') }} | {{ lang._('Port / Endpoint') }} | -{{ lang._('Handshake') }} | +{{ lang._('Handshake') }} | +{{ lang._('Handshake Age') }} | {{ lang._('Sent') }} | {{ lang._('Received') }} |
|---|