Skip to content

Commit

Permalink
Merge pull request #50 from xterm-inator/peer-names
Browse files Browse the repository at this point in the history
Add peer name or comment of name to router depending on MikroTik version
  • Loading branch information
xterm-inator authored Oct 13, 2024
2 parents cd2d60f + 5169c9b commit b3e7c90
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 4 deletions.
1 change: 1 addition & 0 deletions api/app/Listeners/CreateWireGuardPeer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function __construct()
public function handle(ConfigCreated $event): void
{
$peer = new Peer(
$event->config->peer_name,
$event->config->address,
$event->config->peer_public_key,
config('services.wireguard.interface'),
Expand Down
7 changes: 6 additions & 1 deletion api/app/RouterOS/Data/Peer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@

readonly class Peer
{
public string $name;

public function __construct(
$name,
public string $allowedAddress,
public string $publicKey,
public string $interface,
public string $presharedKey,
)
{}
{
$this->name = preg_replace('/[^A-Za-z0-9 .\-_@+]/', '', $name);
}
}
13 changes: 13 additions & 0 deletions api/app/RouterOS/Data/Resource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\RouterOS\Data;

readonly class Resource
{
public string $version;

public function __construct(string $version)
{
$this->version = explode(' ', $version)[0];
}
}
4 changes: 2 additions & 2 deletions api/app/RouterOS/Data/WireGuardInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace App\RouterOS\Data;

class WireGuardInterface
readonly class WireGuardInterface
{
public function __construct(public readonly string $publicKey)
public function __construct(public string $publicKey)
{}
}
21 changes: 21 additions & 0 deletions api/app/RouterOS/Resource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\RouterOS;

use App\RouterOS\Data\Resource as ResourceData;

class Resource extends RouterOS
{

public static function getRouterResource(): ResourceData
{
$routerOS = new self();

$response = $routerOS->client->query('/system/resource/print')->read();
if ($response) {
return new ResourceData($response[0]['version']);
}

throw new \Exception('Could not find WireGuard interface');
}
}
5 changes: 4 additions & 1 deletion api/app/RouterOS/WireGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,15 @@ public static function getPeers(): array
public static function createPeer(Peer $peer): void
{
$routerOS = new self();
$resource = Resource::getRouterResource();
$useName = version_compare($resource->version, '7.15') >= 0;

$query = new Query('/interface/wireguard/peers/add');
$query->equal('allowed-address', $peer->allowedAddress)
->equal('interface', $peer->interface)
->equal('public-key', $peer->publicKey)
->equal('preshared-key', $peer->presharedKey);
->equal('preshared-key', $peer->presharedKey)
->equal($useName ? 'name' : 'comment', $peer->name);

$routerOS->client->query($query)->read();
}
Expand Down

0 comments on commit b3e7c90

Please sign in to comment.