80 lines
2.0 KiB
PHP
80 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Wireguard;
|
|
|
|
class FakeWireguardHub implements WireguardHub
|
|
{
|
|
/** @var array<string, string> pubkey => ip */
|
|
private array $peers = [];
|
|
|
|
private int $next = 2;
|
|
|
|
public string $endpointValue = 'vpn.clupilot.test:51820';
|
|
|
|
public string $publicKeyValue = 'HUBPUBLICKEY0000000000000000000000000000000=';
|
|
|
|
/** Snapshots the tests can shape; defaults to "configured, never handshaked". */
|
|
public array $snapshots = [];
|
|
|
|
/** When set, peers() throws — simulates the hub being unreachable (down, or the `vpn` profile not running). */
|
|
public bool $failPeers = false;
|
|
|
|
/** How many times peers() was called — tests use this to prove a caller fetches once, not once per host. */
|
|
public int $peersCalls = 0;
|
|
|
|
public function allocateIp(): string
|
|
{
|
|
return '10.66.0.'.$this->next++;
|
|
}
|
|
|
|
public function addPeer(string $publicKey, string $ip): void
|
|
{
|
|
$this->peers[$publicKey] = $ip;
|
|
}
|
|
|
|
public function removePeer(string $publicKey): void
|
|
{
|
|
unset($this->peers[$publicKey]);
|
|
}
|
|
|
|
public function peers(): array
|
|
{
|
|
$this->peersCalls++;
|
|
|
|
if ($this->failPeers) {
|
|
throw new \RuntimeException('wg show failed: interface not found');
|
|
}
|
|
|
|
$peers = [];
|
|
foreach ($this->peers as $publicKey => $ip) {
|
|
$peers[$publicKey] = $this->snapshots[$publicKey] ?? new PeerSnapshot(
|
|
publicKey: $publicKey,
|
|
endpoint: null,
|
|
allowedIps: $ip.'/32',
|
|
latestHandshake: null,
|
|
rxBytes: 0,
|
|
txBytes: 0,
|
|
);
|
|
}
|
|
|
|
return $peers;
|
|
}
|
|
|
|
public function endpoint(): string
|
|
{
|
|
return $this->endpointValue;
|
|
}
|
|
|
|
public function publicKey(): string
|
|
{
|
|
return $this->publicKeyValue;
|
|
}
|
|
|
|
/** Raw pubkey => ip map — what the tests assert on. Not the interface's
|
|
* peers(), which returns live snapshots. */
|
|
public function peerIps(): array
|
|
{
|
|
return $this->peers;
|
|
}
|
|
}
|