net/wireguard: allow instance selection from peer

This commit is contained in:
Franco Fichtner
2023-11-01 08:21:18 +01:00
parent 9af41b126b
commit 06d0969eb2
3 changed files with 46 additions and 3 deletions
@@ -30,6 +30,8 @@
namespace OPNsense\Wireguard\Api;
use OPNsense\Base\ApiMutableModelControllerBase;
use OPNsense\Core\Config;
use OPNsense\Wireguard\Server;
class ClientController extends ApiMutableModelControllerBase
{
@@ -46,12 +48,22 @@ class ClientController extends ApiMutableModelControllerBase
public function getClientAction($uuid = null)
{
return $this->getBase('client', 'clients.client', $uuid);
$result = $this->getBase('client', 'clients.client', $uuid);
if (!empty($result['client'])) {
$result['client']['servers'] = [];
foreach ((new Server())->servers->server->iterateItems() as $key => $node) {
$result['client']['servers'][$key] = [
'value' => (string)$node->name,
'selected' => in_array($uuid, explode(',', (string)$node->peers)) ? '1' : '0'
];
}
}
return $result;
}
public function addClientAction()
{
return $this->addBase('client', 'clients.client');
return $this->setClientAction(null);
}
public function delClientAction($uuid)
@@ -61,6 +73,32 @@ class ClientController extends ApiMutableModelControllerBase
public function setClientAction($uuid)
{
if (!empty($this->request->getPost(static::$internalModelName)) && $this->request->isPost()) {
$servers = [];
if (!empty($this->request->getPost(static::$internalModelName)['servers'])) {
$servers = explode(',', $this->request->getPost(static::$internalModelName)['servers']);
}
Config::getInstance()->lock();
$mdl = new Server();
if (empty($uuid)) {
// add new client, generate uuid
$uuid = $mdl->servers->generateUUID();
}
foreach ($mdl->servers->server->iterateItems() as $key => $node) {
$peers = array_filter(explode(',', (string)$node->peers));
if (in_array($uuid, $peers) && !in_array($key, $servers)) {
$node->peers = implode(',', array_diff($peers, [$uuid]));
} elseif (!in_array($uuid, $peers) && in_array($key, $servers)) {
$node->peers = implode(',', array_merge($peers, [$uuid]));
}
}
/**
* Save to in memory model.
* Ignore validations as $uuid might be new or trigger an existing validation issue.
* Persisting the data is handled by setBase()
*/
$mdl->serializeToConfig(false, true);
}
return $this->setBase('client', 'clients.client', $uuid);
}
@@ -43,6 +43,12 @@
<type>text</type>
<help>Set port the endpoint listens to.</help>
</field>
<field>
<id>client.servers</id>
<label>Instances</label>
<type>select_multiple</type>
<help>List of instances this peer belongs to.</help>
</field>
<field>
<id>client.keepalive</id>
<label>Keepalive interval</label>
@@ -69,7 +69,6 @@
<id>server.peers</id>
<label>Peers</label>
<type>select_multiple</type>
<allownew>true</allownew>
<help>List of peers for this instance.</help>
</field>
<field>