From e553f5691ffe569db53284cfc0c97749f272d532 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Brzezi=C5=84ski?= Date: Mon, 26 Aug 2024 11:48:10 +0200 Subject: [PATCH] WOL widget rewrite for new dashboard (#4192) --- net/wol/Makefile | 3 +- .../www/js/widgets/Metadata/WakeOnLan.xml | 17 ++++ .../src/opnsense/www/js/widgets/WakeOnLan.js | 99 +++++++++++++++++++ .../src/www/widgets/include/wake_on_lan.inc | 4 - .../widgets/widgets/wake_on_lan.widget.php | 86 ---------------- 5 files changed, 117 insertions(+), 92 deletions(-) create mode 100644 net/wol/src/opnsense/www/js/widgets/Metadata/WakeOnLan.xml create mode 100644 net/wol/src/opnsense/www/js/widgets/WakeOnLan.js delete mode 100644 net/wol/src/www/widgets/include/wake_on_lan.inc delete mode 100644 net/wol/src/www/widgets/widgets/wake_on_lan.widget.php diff --git a/net/wol/Makefile b/net/wol/Makefile index 08a4a492c..7ce734882 100644 --- a/net/wol/Makefile +++ b/net/wol/Makefile @@ -1,6 +1,5 @@ PLUGIN_NAME= wol -PLUGIN_VERSION= 2.4 -PLUGIN_REVISION= 2 +PLUGIN_VERSION= 2.5 PLUGIN_DEPENDS= wol PLUGIN_COMMENT= Wake on LAN Service PLUGIN_MAINTAINER= franco@opnsense.org diff --git a/net/wol/src/opnsense/www/js/widgets/Metadata/WakeOnLan.xml b/net/wol/src/opnsense/www/js/widgets/Metadata/WakeOnLan.xml new file mode 100644 index 000000000..7cb8e5f50 --- /dev/null +++ b/net/wol/src/opnsense/www/js/widgets/Metadata/WakeOnLan.xml @@ -0,0 +1,17 @@ + + + WakeOnLan.js + + /api/wol/wol/searchHost + /api/wol/wol/set + /diagnostics/interface/getArp + + + WakeOnLan + No saved WOL addresses + Device + Interface + Status + + + diff --git a/net/wol/src/opnsense/www/js/widgets/WakeOnLan.js b/net/wol/src/opnsense/www/js/widgets/WakeOnLan.js new file mode 100644 index 000000000..23d577f77 --- /dev/null +++ b/net/wol/src/opnsense/www/js/widgets/WakeOnLan.js @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2024 Michał Brzeziński + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +export default class WakeOnLan extends BaseTableWidget { + constructor() { + super(); + } + + getGridOptions() { + return { + // trigger overflow-y:scroll after 650px height + sizeToContent: 650 + } + } + + getMarkup() { + let $container = $('
'); + let $wol_table = this.createTable('wol-table', { + headerPosition: 'none' + }); + + $container.append($wol_table); + return $container; + } + + async onWidgetTick() { + const data = await this.ajaxCall('/api/wol/wol/searchHost'); + + let rows = []; + if (data.total == 0) { + const empty_list = [`${this.translations.msg_empty_wol}`]; + rows.push(empty_list); + } else { + const header = [`${this.translations.h_device}`, + `${this.translations.h_interface}`, + `${this.translations.h_status}`, + '']; + rows.push(header); + + //NOTE: this ARP list call is the most expensive one and can grow substantialy in big networks + //With previous widget it had been done on the backend side with direct exec of 'arp -an | grep ...' + const arp = await this.ajaxCall('/api/diagnostics/interface/getArp'); + + for(let it = 0; it < data.rows.length; it++){ + const item = data.rows[it]; + let is_active = this.checkActive(arp, item.mac, item.interface); + let row = [ + `${item.descr.length !== 0 ? item.descr + '
': ''} ${item.mac}`, + `${item.interface}`, + ` + ${ is_active == 1 ? "Online" : "Offline"}`, + `` + ]; + rows.push(row); + } + } + super.updateTable('wol-table', rows); + + $('.wakeupbtn').on('click', async (event) => { + event.preventDefault(); + const data = {uuid: $(event.currentTarget).data('uuid')}; + const result = await this.ajaxCall('/api/wol/wol/set', JSON.stringify(data), 'POST'); + }); + } + + checkActive(arp_list, mac, intf) { + const arp = arp_list.find((obj) => obj.mac === mac); + if (arp === undefined) { + return 0; + } else { + return (arp.expired === false && arp.intf_description === intf); + } + } +} diff --git a/net/wol/src/www/widgets/include/wake_on_lan.inc b/net/wol/src/www/widgets/include/wake_on_lan.inc deleted file mode 100644 index db09f7f1d..000000000 --- a/net/wol/src/www/widgets/include/wake_on_lan.inc +++ /dev/null @@ -1,4 +0,0 @@ - - - - - - - - - - - -wolentry->iterateItems() as $wolent): - $is_active = exec("/usr/sbin/arp -an |/usr/bin/grep -i {$wolent->mac}| /usr/bin/wc -l|/usr/bin/awk '{print $1;}'");?> - - - - - - -wolentry->iterateItems())) == 0):?> - - - - - - - - - - -
descr) ? $wolent->descr : gettext('Unnamed entry') ?>
mac ?>
interface));?> - fa-fw text-" > - - - -
-