You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
132 lines
4.9 KiB
PHP
132 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
/** Immutable, canonical IPv4+IPv6 identity frozen into every new VM intent. */
|
|
final class CloudPublicNetworkSnapshot
|
|
{
|
|
private function __construct(private readonly array $snapshot)
|
|
{
|
|
}
|
|
|
|
public static function fromArray(mixed $value): self
|
|
{
|
|
if (!is_array($value) || array_is_list($value)) {
|
|
throw new \InvalidArgumentException('Public network must be an object.');
|
|
}
|
|
self::exactKeys($value, [
|
|
'contract', 'network_selector', 'ipv4', 'ipv6', 'dns_servers',
|
|
]);
|
|
if (($value['contract'] ?? null) !== 1
|
|
|| !CloudNetworkSelector::isValid($value['network_selector'] ?? null)) {
|
|
throw new \InvalidArgumentException('Public network contract is invalid.');
|
|
}
|
|
$ipv4 = self::leg($value['ipv4'] ?? null, 4);
|
|
$ipv6 = self::leg($value['ipv6'] ?? null, 6);
|
|
$dns = self::dns($value['dns_servers'] ?? null);
|
|
return new self([
|
|
'contract' => 1,
|
|
'network_selector' => $value['network_selector'],
|
|
'ipv4' => $ipv4,
|
|
'ipv6' => $ipv6,
|
|
'dns_servers' => $dns,
|
|
]);
|
|
}
|
|
|
|
public function toArray(): array
|
|
{
|
|
return $this->snapshot;
|
|
}
|
|
|
|
public function fingerprint(): string
|
|
{
|
|
return hash('sha256', json_encode(
|
|
$this->snapshot,
|
|
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
|
|
));
|
|
}
|
|
|
|
public function address(int $family): string
|
|
{
|
|
$key = $family === 4 ? 'ipv4' : ($family === 6 ? 'ipv6' : null);
|
|
if ($key === null) {
|
|
throw new \InvalidArgumentException('Address family is invalid.');
|
|
}
|
|
return $this->snapshot[$key]['address_cidr'];
|
|
}
|
|
|
|
private static function leg(mixed $value, int $family): array
|
|
{
|
|
if (!is_array($value) || array_is_list($value)) {
|
|
throw new \InvalidArgumentException('Public network leg is invalid.');
|
|
}
|
|
self::exactKeys($value, [
|
|
'pool_key', 'pool_cidr', 'address_cidr', 'gateway',
|
|
'gateway_on_link',
|
|
]);
|
|
$poolKey = $value['pool_key'] ?? null;
|
|
if (!is_string($poolKey)
|
|
|| preg_match('/^[a-f0-9]{32}$/D', $poolKey) !== 1
|
|
|| !is_bool($value['gateway_on_link'] ?? null)) {
|
|
throw new \InvalidArgumentException('Public network leg is invalid.');
|
|
}
|
|
$pool = CloudIpPrefix::network($value['pool_cidr'] ?? null);
|
|
$address = CloudIpPrefix::address($value['address_cidr'] ?? null);
|
|
$gateway = CloudIpAddress::parse($value['gateway'] ?? null);
|
|
if ($pool->family() !== $family || $address->family() !== $family
|
|
|| $gateway->family() !== $family
|
|
|| ($family === 4 && $pool->prefixLength() > 30)
|
|
|| ($family === 6 && $pool->prefixLength() > 120)
|
|
|| $address->prefixLength() !== $pool->prefixLength()
|
|
|| !$pool->contains($address->addressValue())
|
|
|| !$address->addressValue()->isPublicUnicast()
|
|
|| !$gateway->isPublicUnicast()
|
|
|| $address->addressValue()->equals($pool->networkAddress())
|
|
|| ($family === 4
|
|
&& $address->addressValue()->equals($pool->lastAddress()))
|
|
|| ($family === 4
|
|
&& ($gateway->equals($pool->networkAddress())
|
|
|| $gateway->equals($pool->lastAddress())))
|
|
|| $address->addressValue()->equals($gateway)
|
|
|| (!$pool->contains($gateway)
|
|
&& $value['gateway_on_link'] !== true)) {
|
|
throw new \InvalidArgumentException('Public network leg is inconsistent.');
|
|
}
|
|
return [
|
|
'pool_key' => $poolKey,
|
|
'pool_cidr' => $pool->networkCidr(),
|
|
'address_cidr' => $address->canonical(),
|
|
'gateway' => $gateway->text(),
|
|
'gateway_on_link' => $value['gateway_on_link'],
|
|
];
|
|
}
|
|
|
|
private static function dns(mixed $value): array
|
|
{
|
|
if (!is_array($value) || !array_is_list($value)
|
|
|| $value === [] || count($value) > 4) {
|
|
throw new \InvalidArgumentException('DNS server list is invalid.');
|
|
}
|
|
$result = [];
|
|
foreach ($value as $raw) {
|
|
$address = CloudIpAddress::parse($raw);
|
|
if (!$address->isPublicUnicast()
|
|
|| isset($result[$address->text()])) {
|
|
throw new \InvalidArgumentException('DNS server list is invalid.');
|
|
}
|
|
$result[$address->text()] = $address->text();
|
|
}
|
|
return array_values($result);
|
|
}
|
|
|
|
private static function exactKeys(array $value, array $expected): void
|
|
{
|
|
$actual = array_keys($value);
|
|
sort($actual, SORT_STRING);
|
|
sort($expected, SORT_STRING);
|
|
if ($actual !== $expected) {
|
|
throw new \InvalidArgumentException('Public network fields are invalid.');
|
|
}
|
|
}
|
|
}
|