From bce044f538db7b67893eafce6f25e6545afce6eb Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Mon, 6 Jun 2022 13:45:31 +0200 Subject: [PATCH] dns / ddclient - Add current ip address and updated timestamp to search api and grid --- dns/ddclient/Makefile | 2 +- dns/ddclient/pkg-descr | 4 + .../DynDNS/Api/AccountsController.php | 5 +- .../mvc/app/models/OPNsense/DynDNS/DynDNS.xml | 2 +- .../DynDNS/FieldTypes/AccountField.php | 81 +++++++++++++++++++ .../mvc/app/views/OPNsense/DynDNS/index.volt | 2 + .../src/opnsense/scripts/ddclient/stats | 53 ++++++++++++ .../conf/actions.d/actions_ddclient.conf | 5 ++ 8 files changed, 151 insertions(+), 3 deletions(-) create mode 100644 dns/ddclient/src/opnsense/mvc/app/models/OPNsense/DynDNS/FieldTypes/AccountField.php create mode 100755 dns/ddclient/src/opnsense/scripts/ddclient/stats diff --git a/dns/ddclient/Makefile b/dns/ddclient/Makefile index f1cbd72cc..1c97be904 100644 --- a/dns/ddclient/Makefile +++ b/dns/ddclient/Makefile @@ -1,5 +1,5 @@ PLUGIN_NAME= ddclient -PLUGIN_VERSION= 1.6 +PLUGIN_VERSION= 1.7 PLUGIN_DEPENDS= ddclient PLUGIN_COMMENT= Dynamic DNS client PLUGIN_MAINTAINER= ad@opnsense.org diff --git a/dns/ddclient/pkg-descr b/dns/ddclient/pkg-descr index 91dba346b..0464eccab 100644 --- a/dns/ddclient/pkg-descr +++ b/dns/ddclient/pkg-descr @@ -6,6 +6,10 @@ WWW: https://github.com/ddclient/ddclient Plugin Changelog ================ +1.7 + +* Add current ip address and updated timestamp to search api and grid + 1.6 * Add Gandi support (contributed by Neozlag) diff --git a/dns/ddclient/src/opnsense/mvc/app/controllers/OPNsense/DynDNS/Api/AccountsController.php b/dns/ddclient/src/opnsense/mvc/app/controllers/OPNsense/DynDNS/Api/AccountsController.php index 08c21b694..97c7f4060 100644 --- a/dns/ddclient/src/opnsense/mvc/app/controllers/OPNsense/DynDNS/Api/AccountsController.php +++ b/dns/ddclient/src/opnsense/mvc/app/controllers/OPNsense/DynDNS/Api/AccountsController.php @@ -41,7 +41,10 @@ class AccountsController extends ApiMutableModelControllerBase { $result = $this->searchBase( "accounts.account", - ['enabled', 'service', 'description', 'username', 'hostnames', 'use_interface', 'interface', 'protocol'], + [ + 'enabled', 'service', 'description', 'username', 'hostnames', 'use_interface', + 'interface', 'protocol', 'current_ip', 'current_mtime' + ], "description" ); foreach ($result['rows'] as &$row) { diff --git a/dns/ddclient/src/opnsense/mvc/app/models/OPNsense/DynDNS/DynDNS.xml b/dns/ddclient/src/opnsense/mvc/app/models/OPNsense/DynDNS/DynDNS.xml index 003426fc8..c28271bbe 100644 --- a/dns/ddclient/src/opnsense/mvc/app/models/OPNsense/DynDNS/DynDNS.xml +++ b/dns/ddclient/src/opnsense/mvc/app/models/OPNsense/DynDNS/DynDNS.xml @@ -26,7 +26,7 @@ - + 1 Y diff --git a/dns/ddclient/src/opnsense/mvc/app/models/OPNsense/DynDNS/FieldTypes/AccountField.php b/dns/ddclient/src/opnsense/mvc/app/models/OPNsense/DynDNS/FieldTypes/AccountField.php new file mode 100644 index 000000000..24d856e99 --- /dev/null +++ b/dns/ddclient/src/opnsense/mvc/app/models/OPNsense/DynDNS/FieldTypes/AccountField.php @@ -0,0 +1,81 @@ +setInternalIsVirtual(); + $current_mtime = new TextField(); + $current_mtime->setInternalIsVirtual(); + + if (!empty((string)$node->hostnames)) { + foreach (explode(",", (string)$node->hostnames) as $hostname) { + if (!empty((self::$current_stats[$hostname]))) { + $stats = self::$current_stats[$hostname]; + $current_ip->setValue($stats['ip']); + $current_mtime->setValue(date('c', $stats['mtime'])); + break; + } + } + } + $node->addChildNode('current_ip', $current_ip); + $node->addChildNode('current_mtime', $current_mtime); + } + + protected function actionPostLoadingEvent() + { + if (self::$current_stats === null) { + self::$current_stats = []; + $stats = json_decode((new Backend())->configdRun('ddclient statistics'), true); + if (!empty($stats) && !empty($stats['hosts'])) { + self::$current_stats = $stats['hosts']; + } + } + foreach ($this->internalChildnodes as $node) { + if (!$node->getInternalIsVirtual()) { + $this->addStatsFields($node); + } + } + return parent::actionPostLoadingEvent(); + } +} diff --git a/dns/ddclient/src/opnsense/mvc/app/views/OPNsense/DynDNS/index.volt b/dns/ddclient/src/opnsense/mvc/app/views/OPNsense/DynDNS/index.volt index d8f64ad27..baaefe65d 100644 --- a/dns/ddclient/src/opnsense/mvc/app/views/OPNsense/DynDNS/index.volt +++ b/dns/ddclient/src/opnsense/mvc/app/views/OPNsense/DynDNS/index.volt @@ -105,6 +105,8 @@ POSSIBILITY OF SUCH DAMAGE. {{ lang._('Hostnames') }} {{ lang._('Username') }} {{ lang._('Interface') }} + {{ lang._('Current IP') }} + {{ lang._('Updated') }} {{ lang._('Description') }} {{ lang._('Commands') }} diff --git a/dns/ddclient/src/opnsense/scripts/ddclient/stats b/dns/ddclient/src/opnsense/scripts/ddclient/stats new file mode 100755 index 000000000..891e3ea87 --- /dev/null +++ b/dns/ddclient/src/opnsense/scripts/ddclient/stats @@ -0,0 +1,53 @@ +#!/usr/local/bin/python3 + +""" + Copyright (c) 2022 Ad Schellevis + 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 os +import json + +filename = "/var/tmp/ddclient.cache" + +result = {"hosts": {}} +if os.path.isfile(filename): + with open(filename, "r") as fhandle: + for idx, row in enumerate(fhandle): + if idx == 0: + result['version'] = row.strip('#\n ') + elif idx == 1: + tmp = row.split('(')[-1].split(')')[0] + if tmp.isdigit(): + result['updated'] = int(tmp) + elif tmp.startswith('#') is False: + record = {} + for pair in row.split(','): + parts = pair.split('=') + if len(parts) == 2: + record[parts[0]] = parts[1] + if 'host' in record: + result['hosts'][record['host']] = record + +print(json.dumps(result)) diff --git a/dns/ddclient/src/opnsense/service/conf/actions.d/actions_ddclient.conf b/dns/ddclient/src/opnsense/service/conf/actions.d/actions_ddclient.conf index 5ceb8a787..d59aaf852 100644 --- a/dns/ddclient/src/opnsense/service/conf/actions.d/actions_ddclient.conf +++ b/dns/ddclient/src/opnsense/service/conf/actions.d/actions_ddclient.conf @@ -23,3 +23,8 @@ description:Restart ddclient service command:/usr/local/etc/rc.d/ddclient reload type:script message:reload ddclient configuration + +[statistics] +command:/usr/local/opnsense/scripts/ddclient/stats +type:script_output +message:get ddclient statistics