From 03ac997fa4dfbd6b651e996ead6e9bea58f3c84f Mon Sep 17 00:00:00 2001 From: Franco Fichtner Date: Wed, 15 Mar 2023 12:28:05 +0100 Subject: [PATCH] system: migrate services page to MVC #6376 Widget is next. While here set configd to 'locked'. --- plist | 4 +- src/etc/inc/plugins.inc.d/core.inc | 3 +- .../OPNsense/Core/Api/ServiceController.php | 129 ++++++++++++++++++ .../OPNsense/Core/ServiceController.php | 44 ++++++ .../mvc/app/models/OPNsense/Core/ACL/ACL.xml | 3 +- .../app/models/OPNsense/Core/Menu/Menu.xml | 4 +- .../mvc/app/views/OPNsense/Core/service.volt | 105 ++++++++++++++ .../service/conf/actions_service.conf | 18 +++ src/www/foot.inc | 10 +- src/www/status_ntpd.php | 2 +- src/www/status_services.php | 103 -------------- src/www/widgets/include/services_status.inc | 2 +- 12 files changed, 311 insertions(+), 116 deletions(-) create mode 100644 src/opnsense/mvc/app/controllers/OPNsense/Core/Api/ServiceController.php create mode 100644 src/opnsense/mvc/app/controllers/OPNsense/Core/ServiceController.php create mode 100644 src/opnsense/mvc/app/views/OPNsense/Core/service.volt delete mode 100644 src/www/status_services.php diff --git a/plist b/plist index 49404c4eb..680a54b69 100644 --- a/plist +++ b/plist @@ -269,12 +269,14 @@ /usr/local/opnsense/mvc/app/controllers/OPNsense/CaptivePortal/forms/dialogZone.xml /usr/local/opnsense/mvc/app/controllers/OPNsense/Core/Api/FirmwareController.php /usr/local/opnsense/mvc/app/controllers/OPNsense/Core/Api/MenuController.php +/usr/local/opnsense/mvc/app/controllers/OPNsense/Core/Api/ServiceController.php /usr/local/opnsense/mvc/app/controllers/OPNsense/Core/Api/SystemController.php /usr/local/opnsense/mvc/app/controllers/OPNsense/Core/FirmwareController.php /usr/local/opnsense/mvc/app/controllers/OPNsense/Core/HaltController.php /usr/local/opnsense/mvc/app/controllers/OPNsense/Core/IndexController.php /usr/local/opnsense/mvc/app/controllers/OPNsense/Core/LicenseController.php /usr/local/opnsense/mvc/app/controllers/OPNsense/Core/RebootController.php +/usr/local/opnsense/mvc/app/controllers/OPNsense/Core/ServiceController.php /usr/local/opnsense/mvc/app/controllers/OPNsense/Cron/Api/ServiceController.php /usr/local/opnsense/mvc/app/controllers/OPNsense/Cron/Api/SettingsController.php /usr/local/opnsense/mvc/app/controllers/OPNsense/Cron/IndexController.php @@ -668,6 +670,7 @@ /usr/local/opnsense/mvc/app/views/OPNsense/Core/license.volt /usr/local/opnsense/mvc/app/views/OPNsense/Core/not_found.volt /usr/local/opnsense/mvc/app/views/OPNsense/Core/reboot.volt +/usr/local/opnsense/mvc/app/views/OPNsense/Core/service.volt /usr/local/opnsense/mvc/app/views/OPNsense/Cron/index.volt /usr/local/opnsense/mvc/app/views/OPNsense/Diagnostics/arp.volt /usr/local/opnsense/mvc/app/views/OPNsense/Diagnostics/dns_diagnostics.volt @@ -1957,7 +1960,6 @@ /usr/local/www/status_interfaces.php /usr/local/www/status_ntpd.php /usr/local/www/status_openvpn.php -/usr/local/www/status_services.php /usr/local/www/status_wireless.php /usr/local/www/system_advanced_admin.php /usr/local/www/system_advanced_firewall.php diff --git a/src/etc/inc/plugins.inc.d/core.inc b/src/etc/inc/plugins.inc.d/core.inc index 809a9b6e1..f44ab3a14 100644 --- a/src/etc/inc/plugins.inc.d/core.inc +++ b/src/etc/inc/plugins.inc.d/core.inc @@ -68,6 +68,7 @@ function core_services() 'stop' => array('/usr/local/etc/rc.d/configd stop'), ), 'name' => 'configd', + 'locked' => true, ); $services[] = array( @@ -124,8 +125,8 @@ function core_services() 'php' => array( 'restart' => array('webgui_configure_delayed') ), - 'locked' => true, 'name' => 'webgui', + 'locked' => true, ); return $services; diff --git a/src/opnsense/mvc/app/controllers/OPNsense/Core/Api/ServiceController.php b/src/opnsense/mvc/app/controllers/OPNsense/Core/Api/ServiceController.php new file mode 100644 index 000000000..6da4710e0 --- /dev/null +++ b/src/opnsense/mvc/app/controllers/OPNsense/Core/Api/ServiceController.php @@ -0,0 +1,129 @@ +configdRun('service list'), true); + } + + /** + * Search phase 1 session entries + * @return array + */ + public function searchAction() + { + $this->sessionClose(); + + $data = $this->list_status(); + $records = []; + $phase1s = []; + + if (!empty($data)) { + foreach ($data as $service) { + $record = [ + 'id' => $service['name'] . (array_key_exists('id', $service) ? '/' . $service['id'] : ''), + 'locked' => !empty($service['locked']) || !empty($service['nocheck']) ? 1 : 0, + 'running' => strpos($service['status'], 'is running') !== false ? 1 : 0, + 'description' => $service['description'], + 'name' => $service['name'], + ]; + $records[] = $record; + } + } + return $this->searchRecordsetBase($records); + } + + /** + * start a service + * @param string $name to identify the service + * @param string $id to identify the service instance + * @return array + */ + public function startAction($name, $id = '') + { + if (!$this->request->isPost()) { + return ['result' => 'failed']; + } + + $this->sessionClose(); + + (new Backend())-> configdpRun('service start', [$name, $id]); + + return ['result' => 'ok']; + } + + /** + * restart a service + * @param string $name to identify the service + * @param string $id to identify the service instance + * @return array + */ + public function restartAction($name, $id = '') + { + if (!$this->request->isPost()) { + return ['result' => 'failed']; + } + + $this->sessionClose(); + + (new Backend())-> configdpRun('service restart', [$name, $id]); + + return ['result' => 'ok']; + } + + /** + * stop a service + * @param string $name to identify the service + * @param string $id to identify the service instance + * @return array + */ + public function stopAction($name, $id = '') + { + if (!$this->request->isPost()) { + return ['result' => 'failed']; + } + + $this->sessionClose(); + + (new Backend())-> configdpRun('service stop', [$name, $id]); + + return ['result' => 'ok']; + } +} diff --git a/src/opnsense/mvc/app/controllers/OPNsense/Core/ServiceController.php b/src/opnsense/mvc/app/controllers/OPNsense/Core/ServiceController.php new file mode 100644 index 000000000..332f88bc6 --- /dev/null +++ b/src/opnsense/mvc/app/controllers/OPNsense/Core/ServiceController.php @@ -0,0 +1,44 @@ +view->pick('OPNsense/Core/service'); + } +} diff --git a/src/opnsense/mvc/app/models/OPNsense/Core/ACL/ACL.xml b/src/opnsense/mvc/app/models/OPNsense/Core/ACL/ACL.xml index 6343452aa..8e2e19fcc 100644 --- a/src/opnsense/mvc/app/models/OPNsense/Core/ACL/ACL.xml +++ b/src/opnsense/mvc/app/models/OPNsense/Core/ACL/ACL.xml @@ -520,7 +520,8 @@ Status: Services - status_services.php* + ui/core/service/* + api/core/service/* diff --git a/src/opnsense/mvc/app/models/OPNsense/Core/Menu/Menu.xml b/src/opnsense/mvc/app/models/OPNsense/Core/Menu/Menu.xml index 22252defb..758ea4dcc 100644 --- a/src/opnsense/mvc/app/models/OPNsense/Core/Menu/Menu.xml +++ b/src/opnsense/mvc/app/models/OPNsense/Core/Menu/Menu.xml @@ -98,9 +98,7 @@ - - - + diff --git a/src/opnsense/mvc/app/views/OPNsense/Core/service.volt b/src/opnsense/mvc/app/views/OPNsense/Core/service.volt new file mode 100644 index 000000000..34ac61bf3 --- /dev/null +++ b/src/opnsense/mvc/app/views/OPNsense/Core/service.volt @@ -0,0 +1,105 @@ +{# + # Copyright (c) 2023 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. + #} + + + +
+ + + + + + + + + + + + + +
{{ lang._('ID') }}{{ lang._('Name') }}{{ lang._('Description') }}
+
diff --git a/src/opnsense/service/conf/actions_service.conf b/src/opnsense/service/conf/actions_service.conf index ba985cf69..4996582e5 100644 --- a/src/opnsense/service/conf/actions_service.conf +++ b/src/opnsense/service/conf/actions_service.conf @@ -15,3 +15,21 @@ command:/usr/local/sbin/pluginctl -S parameters:%s %s type:script_output message:Fetching service list (%s %s) + +[start] +command:/usr/local/sbin/pluginctl -s +parameters:%s start %s +type:script +message:Starting service (%s %s) + +[restart] +command:/usr/local/sbin/pluginctl -s +parameters:%s restart %s +type:script +message:Restarting service (%s %s) + +[stop] +command:/usr/local/sbin/pluginctl -s +parameters:%s stop %s +type:script +message:Stopping service (%s %s) diff --git a/src/www/foot.inc b/src/www/foot.inc index 9d1ed22e0..e5ab34ee3 100644 --- a/src/www/foot.inc +++ b/src/www/foot.inc @@ -86,12 +86,12 @@ $product = product::getInstance(); $( document ).ready(function() { $('.srv_status_act').click(function(event){ event.preventDefault(); - let params = {}; - params['action'] = $(this).data('service_action'); - params['service'] = $(this).data('service'); - params['id'] = $(this).data('service_id'); + let url = '/api/core/service/' + $(this).data('service_action') + '/' + $(this).data('service'); + if ($(this).data('service_id') != '') { + url += '/' + $(this).data('service_id'); + } $("#OPNsenseStdWaitDialog").modal('show'); - $.post('/status_services.php',params, function(data) { + $.post(url, {}, function (data) { // refresh page after service action via server location.reload(true); }); diff --git a/src/www/status_ntpd.php b/src/www/status_ntpd.php index 2a18dfee3..5d588b270 100644 --- a/src/www/status_ntpd.php +++ b/src/www/status_ntpd.php @@ -219,7 +219,7 @@ include("head.inc"); elseif (count($ntpq_servers) == 0): ?> - ','') ?> + - * Copyright (C) 2004-2005 Scott Ullrich - * 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. - */ - -require_once("guiconfig.inc"); -require_once("system.inc"); -require_once("filter.inc"); -require_once("interfaces.inc"); - -if (!empty($_POST['service'])) { - $service_name = $_POST['service']; - switch ($_POST['action']) { - case 'restart': - echo service_control_restart($service_name, $_POST); - break; - case 'start': - echo service_control_start($service_name, $_POST); - break; - case 'stop': - echo service_control_stop($service_name, $_POST); - break; - } - exit; -} - -$services = plugins_services(); - -include("head.inc"); - -?> - - - -
-
-
- -
-
-
- - - - - - - - - - 0): - foreach($services as $service):?> - - - - - - - - - - - -
- - -
-
-
-
-
-
-
- diff --git a/src/www/widgets/include/services_status.inc b/src/www/widgets/include/services_status.inc index fa35c966d..d9207f504 100644 --- a/src/www/widgets/include/services_status.inc +++ b/src/www/widgets/include/services_status.inc @@ -1,4 +1,4 @@