VPN/IPsec - add enable toggle to connection page

This commit is contained in:
Ad Schellevis 2022-12-22 17:38:19 +01:00
parent ec06c3cc49
commit fe65dded8d
2 changed files with 62 additions and 1 deletions

View File

@ -29,6 +29,7 @@
namespace OPNsense\IPsec\Api;
use OPNsense\Base\ApiMutableModelControllerBase;
use OPNsense\Core\Config;
/**
* Class ConnectionsController
@ -274,4 +275,41 @@ class ConnectionsController extends ApiMutableModelControllerBase
{
return $this->delBase('children.child', $uuid);
}
/**
* is IPsec enabled
*/
public function isEnabledAction()
{
$this->sessionClose();
return [
'enabled' => isset(Config::getInstance()->object()->ipsec->enable)
];
}
/**
* toggle if IPsec is enabled
*/
public function toggleAction($enabled = null)
{
if ($this->request->isPost()) {
$this->sessionClose();
Config::getInstance()->lock();
$config = Config::getInstance()->object();
if ($enabled == "0" || $enabled == "1") {
$new_status = $enabled == "1";
} else {
$new_status = !isset($config->ipsec->enable);
}
if ($new_status) {
$config->ipsec->enable = true;
} elseif (isset($config->ipsec->enable)) {
unset($config->ipsec->enable);
}
Config::getInstance()->save();
return ['status' => 'ok'];
}
return ['status' => 'failed'];
}
}

View File

@ -93,6 +93,22 @@
$("#frm_ConnectionDialog").append($("#frm_DialogConnection").detach());
updateServiceControlUI('ipsec');
$("#enable").click(function(){
if (!$(this).hasClass("pending")) {
$(this).addClass("pending");
let enabled = $("#enable").prop('checked') ? '1' : '0';
ajaxCall('/api/ipsec/connections/toggle/' + enabled, {}, function (data, status) {
$("#enable").removeClass("pending");
});
}
});
ajaxGet('/api/ipsec/connections/is_enabled', {}, function (data, status) {
if (data.enabled === true) {
$("#enable").prop('checked', data.enabled);
}
$("#enable").removeClass("pending");
});
/**
* reconfigure
*/
@ -150,7 +166,7 @@
</div>
<hr/>
</div>
<div class="col-md-12">
<div class="col-md-10">
<button class="btn btn-primary" id="reconfigureAct"
data-endpoint="/api/ipsec/service/reconfigure"
data-label="{{ lang._('Apply') }}"
@ -159,6 +175,13 @@
></button>
<br/><br/>
</div>
<div class="col-md-2">
<div class="pull-right">
<input name="enable" class="pending" type="checkbox" id="enable"/>
<strong>{{ lang._('Enable IPsec') }}</strong>
</div>
</div>
</div>
<div id="edit_connection" class="tab-pane fade in">
<div class="section_header">