From dd68ab68d88c5afd355a1662cfc4bcb44117a4ed Mon Sep 17 00:00:00 2001 From: Monviech <79600909+Monviech@users.noreply.github.com> Date: Fri, 2 Aug 2024 13:46:07 +0200 Subject: [PATCH] dns/ddclient: Add Dyndns widget for new dashboard (#4145) * dns/ddclient: Add Dyndns widget for new dashboard. --- .../src/opnsense/www/js/widgets/Dyndns.js | 136 ++++++++++++++++++ .../www/js/widgets/Metadata/Dyndns.xml | 20 +++ 2 files changed, 156 insertions(+) create mode 100644 dns/ddclient/src/opnsense/www/js/widgets/Dyndns.js create mode 100644 dns/ddclient/src/opnsense/www/js/widgets/Metadata/Dyndns.xml diff --git a/dns/ddclient/src/opnsense/www/js/widgets/Dyndns.js b/dns/ddclient/src/opnsense/www/js/widgets/Dyndns.js new file mode 100644 index 000000000..c0da3944b --- /dev/null +++ b/dns/ddclient/src/opnsense/www/js/widgets/Dyndns.js @@ -0,0 +1,136 @@ +/* + * Copyright (C) 2024 Deciso B.V. + * 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. + */ + +import BaseTableWidget from "./BaseTableWidget.js"; + +export default class Dyndns extends BaseTableWidget { + constructor() { + super(); + } + + getGridOptions() { + return { + // Trigger overflow-y:scroll after 650px height + sizeToContent: 650 + }; + } + + getMarkup() { + let $container = $('
'); + let $dyndnsTable = this.createTable('dyndnsTable', { + headerPosition: 'top', + headers: [ + this.translations.service, + // this.translations.domains + ] + }); + + $container.append($dyndnsTable); + return $container; + } + + async onWidgetTick() { + // Check if DynDNS is enabled + const statusData = await this.ajaxCall('/api/dyndns/service/status'); + if (!statusData || statusData.status !== "running") { + this.displayError(this.translations.servicedisabled); + return; + } + + // Fetch DynDNS account information + const accountData = await this.ajaxCall('/api/dyndns/accounts/search_item'); + if (!accountData || !accountData.rows || accountData.rows.length === 0) { + this.displayError(this.translations.noaccount); + return; + } + + this.processAccounts(accountData.rows); + } + + // Utility function to display errors within the widget + displayError(message) { + const $error = $(` +
+ ${message} +
+ `); + $('#dyndnsTable').empty().append($error); + } + + processAccounts(accounts) { + if (!this.dataChanged('accounts', accounts)) { + return; + } + + $('.dyndns-tooltip').tooltip('hide'); + + let rows = []; + accounts.forEach(account => { + let colorClass = account.enabled === "1" ? 'text-success' : 'text-danger'; + let tooltipText = account.enabled === "1" ? this.translations.enabled : this.translations.disabled; + + let domainNames = account.hostnames.split(',') + .map(domain => `
${domain}
`) + .join(''); + + // Convert time to a localized format + let localizedTime = account.current_mtime ? new Date(account.current_mtime).toLocaleString() : this.translations.undefined; + let currentIp = account.current_ip || this.translations.undefined; + + let row = [ + ` +
+ +  ${account.service} +
+
${this.translations.currentip}: ${currentIp}
+
${this.translations.currentmtime}: ${localizedTime}
+ `, + ` +
${this.translations.domains}
+
${domainNames}
+ ` + ]; + + rows.push(row); + }); + + // Update table with rows + super.updateTable('dyndnsTable', rows); + + // Initialize tooltips + $('.dyndns-tooltip').tooltip({container: 'body'}); + } + + onWidgetResize(elem, width, height) { + if (width < 320) { + $('.domain-names').parent().hide(); + } else { + $('.domain-names').parent().show(); + } + return true; // Return true to force the grid to update its layout + } +} diff --git a/dns/ddclient/src/opnsense/www/js/widgets/Metadata/Dyndns.xml b/dns/ddclient/src/opnsense/www/js/widgets/Metadata/Dyndns.xml new file mode 100644 index 000000000..43a822cb3 --- /dev/null +++ b/dns/ddclient/src/opnsense/www/js/widgets/Metadata/Dyndns.xml @@ -0,0 +1,20 @@ + + + Dyndns.js + + /api/dyndns/* + + + Dynamic DNS + DynDNS service is not running + No DynDNS account information available + Enabled + Disabled + Current IP + Updated + Hostnames + Accounts + N/A + + +