Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VPN: OpenVPN: Connection Status - refactor to MVC
PR: #6382 (cherry picked from commit b9a1633) (cherry picked from commit fa30a8c) (cherry picked from commit 16492ce) (cherry picked from commit b50e529) (cherry picked from commit c897054)
- Loading branch information
1 parent
4ac3e1d
commit 30e38b0
Showing
12 changed files
with
530 additions
and
314 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
221 changes: 221 additions & 0 deletions
221
src/opnsense/mvc/app/controllers/OPNsense/OpenVPN/Api/ServiceController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
<?php | ||
|
||
/* | ||
* 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. | ||
*/ | ||
|
||
namespace OPNsense\OpenVPN\Api; | ||
|
||
use OPNsense\Base\ApiControllerBase; | ||
use OPNsense\Core\Config; | ||
use OPNsense\Core\Backend; | ||
|
||
/** | ||
* Class ServiceController | ||
* @package OPNsense\OpenVPN | ||
*/ | ||
class ServiceController extends ApiControllerBase | ||
{ | ||
private function getConfigs($role) | ||
{ | ||
$config = Config::getInstance()->object(); | ||
$config_payload = []; | ||
$cnf_section = 'openvpn-' . $role; | ||
if (!empty($config->openvpn->$cnf_section)) { | ||
foreach ($config->openvpn->$cnf_section as $cnf) { | ||
if (!empty((string)$cnf->vpnid)) { | ||
$config_payload[(string)$cnf->vpnid] = $cnf; | ||
} | ||
} | ||
} | ||
return $config_payload; | ||
} | ||
|
||
/** | ||
* Search sessions | ||
* @return array | ||
*/ | ||
public function searchSessionsAction() | ||
{ | ||
$this->sessionClose(); | ||
$data = json_decode((new Backend())->configdRun('openvpn connections client,server') ?? '', true) ?? []; | ||
$records = []; | ||
$roles = ['client', 'server']; | ||
if ($this->request->has('type') && is_array($this->request->get('type'))) { | ||
$roles = array_intersect($this->request->get('type'), $roles); | ||
} | ||
foreach ($roles as $role) { | ||
$config_payload = $this->getConfigs($role); | ||
$vpnids = []; | ||
if (!empty($data[$role])) { | ||
foreach ($data[$role] as $idx => $stats) { | ||
$vpnids[] = $idx; | ||
$stats['type'] = $role; | ||
$stats['id'] = $idx; | ||
$stats['description'] = ''; | ||
$stats['connected_since'] = null; | ||
if (!empty($stats['timestamp'])) { | ||
$stats['connected_since'] = date('Y-m-d H:i:s', $stats['timestamp']); | ||
} | ||
if (!empty($config_payload[$idx])) { | ||
$stats['description'] = (string)$config_payload[$idx]->description ?? ''; | ||
} | ||
if (!empty($stats['client_list'])) { | ||
foreach ($stats['client_list'] as $client) { | ||
$tmp = array_merge($stats, $client); | ||
$tmp['id'] .= '_' . $client['real_address']; | ||
$tmp['is_client'] = true; | ||
$records[] = $tmp; | ||
} | ||
} else { | ||
$records[] = $stats; | ||
} | ||
} | ||
} | ||
// add non running enabled servers | ||
foreach ($config_payload as $idx => $cnf) { | ||
if (!in_array($idx, $vpnids) && empty((string)$cnf->disable)) { | ||
$records[] = [ | ||
'id' => $idx, | ||
'service_id' => "openvpn/" . $idx, | ||
'type' => $role, | ||
'description' => (string)$cnf->description ?? '', | ||
'connected_since' => null, | ||
'status' => null | ||
]; | ||
} | ||
} | ||
} | ||
return $this->searchRecordsetBase($records); | ||
} | ||
|
||
/** | ||
* Search routes | ||
* @return array | ||
*/ | ||
public function searchRoutesAction() | ||
{ | ||
$records = []; | ||
$data = json_decode((new Backend())->configdRun('openvpn connections client,server') ?? '', true) ?? []; | ||
$records = []; | ||
$roles = ['client', 'server']; | ||
if ($this->request->has('type') && is_array($this->request->get('type'))) { | ||
$roles = array_intersect($this->request->get('type'), $roles); | ||
} | ||
foreach ($roles as $role) { | ||
if (!empty($data[$role])) { | ||
$config_payload = $this->getConfigs($role); | ||
foreach ($data[$role] as $idx => $payload) { | ||
if (!empty($payload['routing_table'])) { | ||
foreach ($payload['routing_table'] as $route_entry) { | ||
$route_entry['type'] = $role; | ||
$route_entry['id'] = $idx; | ||
$route_entry['description'] = ''; | ||
if (!empty($config_payload[$idx])) { | ||
$route_entry['description'] = (string)$config_payload[$idx]->description ?? ''; | ||
} | ||
$records[] = $route_entry; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return $this->searchRecordsetBase($records); | ||
} | ||
|
||
/** | ||
* kill session by source ip:port or common name | ||
* @return array | ||
*/ | ||
public function killSessionAction() | ||
{ | ||
if (!$this->request->isPost()) { | ||
return ['result' => 'failed']; | ||
} | ||
$this->sessionClose(); | ||
$server_id = $this->request->get('server_id', null); | ||
$session_id = $this->request->get('session_id', null); | ||
if ($server_id != null && $session_id != null) { | ||
$data = json_decode((new Backend())->configdpRun('openvpn kill', [$server_id, $session_id]) ?? '', true); | ||
if (!empty($data)) { | ||
return $data; | ||
} | ||
return ['result' => 'failed']; | ||
} else { | ||
return ['status' => 'invalid']; | ||
} | ||
} | ||
|
||
/** | ||
* @param int $id server/client id to start | ||
* @return array | ||
*/ | ||
public function startServiceAction($id = null) | ||
{ | ||
if (!$this->request->isPost() || $id == null) { | ||
return ['result' => 'failed']; | ||
} | ||
|
||
$this->sessionClose(); | ||
|
||
(new Backend())-> configdpRun('service start', ['openvpn', $id]); | ||
|
||
return ['result' => 'ok']; | ||
} | ||
|
||
/** | ||
* @param int $id server/client id to stop | ||
* @return array | ||
*/ | ||
public function stopServiceAction($id = null) | ||
{ | ||
if (!$this->request->isPost() || $id == null) { | ||
return ['result' => 'failed']; | ||
} | ||
|
||
$this->sessionClose(); | ||
|
||
(new Backend())-> configdpRun('service stop', ['openvpn', $id]); | ||
|
||
return ['result' => 'ok']; | ||
} | ||
|
||
/** | ||
* @param int $id server/client id to restart | ||
* @return array | ||
*/ | ||
public function restartServiceAction($id = null) | ||
{ | ||
if (!$this->request->isPost() || $id == null) { | ||
return ['result' => 'failed']; | ||
} | ||
|
||
$this->sessionClose(); | ||
|
||
(new Backend())-> configdpRun('service restart', ['openvpn', $id]); | ||
|
||
return ['result' => 'ok']; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/opnsense/mvc/app/controllers/OPNsense/OpenVPN/StatusController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
/* | ||
* 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. | ||
*/ | ||
|
||
namespace OPNsense\OpenVPN; | ||
|
||
use OPNsense\Base\IndexController as BaseIndexController; | ||
|
||
/** | ||
* Class StatusController | ||
* @package OPNsense\OpenVPN | ||
*/ | ||
class StatusController extends BaseIndexController | ||
{ | ||
/** | ||
* default index page | ||
* @throws \Exception | ||
*/ | ||
public function indexAction() | ||
{ | ||
$this->view->pick('OPNsense/OpenVPN/status'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.