CluPilotCloud/app/Services/Wireguard/PeerSnapshot.php

42 lines
1.3 KiB
PHP

<?php
namespace App\Services\Wireguard;
use Carbon\CarbonImmutable;
/**
* One line of `wg show <if> dump` — what the hub currently reports about a peer.
* A value object rather than an array so callers cannot silently mistype a key.
*/
final readonly class PeerSnapshot
{
public function __construct(
public string $publicKey,
public ?string $endpoint,
public string $allowedIps,
public ?CarbonImmutable $latestHandshake,
public int $rxBytes,
public int $txBytes,
) {}
/** @param list<string> $columns one dump line, already split on tabs */
public static function fromDumpLine(array $columns): ?self
{
// publickey, presharedkey, endpoint, allowed-ips, latest-handshake, rx, tx, keepalive
if (count($columns) < 8) {
return null;
}
$handshake = (int) $columns[4];
return new self(
publicKey: $columns[0],
endpoint: $columns[2] === '(none)' ? null : $columns[2],
allowedIps: $columns[3],
// 0 means "never handshaked" — not the unix epoch.
latestHandshake: $handshake > 0 ? CarbonImmutable::createFromTimestamp($handshake) : null,
rxBytes: (int) $columns[5],
txBytes: (int) $columns[6],
);
}
}