toArray(); } catch (\InvalidArgumentException) { return self::invalid(); } $ipv4 = self::leg($snapshot['ipv4']); $ipv6 = self::leg($snapshot['ipv6']); $network = [ 'contract' => 1, 'network_selector' => $snapshot['network_selector'], 'ipv4' => $ipv4, 'ipv6' => $ipv6, 'dns_servers' => $snapshot['dns_servers'], ]; return [ 'public_network_valid' => true, 'public_network' => $network, 'public_ipv4' => $ipv4['address'], 'public_ipv4_cidr' => $ipv4['address_cidr'], 'public_ipv6' => $ipv6['address'], 'public_ipv6_cidr' => $ipv6['address_cidr'], // Compatibility aliases remain derived from the immutable // snapshot while IPv4-only mutation paths are retired in C10. 'public_ip' => $ipv4['address'], 'public_ip_cidr' => $ipv4['address_cidr'], 'gateway' => $ipv4['gateway'], 'dns_servers' => $network['dns_servers'], ]; } /** * Re-validate a browser-safe projection before a cached snapshot is * decorated with live Agent metadata. Aliases are always re-derived from * the nested network object, never copied from the caller. */ public static function fromProjection(array $projection): array { if (!array_key_exists('public_network_valid', $projection)) { return []; } if ($projection['public_network_valid'] !== true) { return self::invalid(); } $network = $projection['public_network'] ?? null; if (!is_array($network) || array_is_list($network) || !self::exactKeys($network, [ 'contract', 'network_selector', 'ipv4', 'ipv6', 'dns_servers', ])) { return self::invalid(); } $raw = [ 'contract' => $network['contract'] ?? null, 'network_selector' => $network['network_selector'] ?? null, 'ipv4' => self::rawLeg($network['ipv4'] ?? null, str_repeat('1', 32)), 'ipv6' => self::rawLeg($network['ipv6'] ?? null, str_repeat('2', 32)), 'dns_servers' => $network['dns_servers'] ?? null, ]; if ($raw['ipv4'] === null || $raw['ipv6'] === null) { return self::invalid(); } $result = self::fromSpec(['public_network' => $raw]); return ($result['public_network_valid'] ?? false) === true && ($result['public_network'] ?? null) === $network ? $result : self::invalid(); } private static function leg(array $leg): array { return [ 'address_cidr' => $leg['address_cidr'], 'address' => explode('/', $leg['address_cidr'], 2)[0], 'pool_cidr' => $leg['pool_cidr'], 'gateway' => $leg['gateway'], 'gateway_on_link' => $leg['gateway_on_link'], ]; } private static function rawLeg(mixed $leg, string $poolKey): ?array { if (!is_array($leg) || array_is_list($leg) || !self::exactKeys($leg, [ 'address_cidr', 'address', 'pool_cidr', 'gateway', 'gateway_on_link', ])) { return null; } return [ 'pool_key' => $poolKey, 'pool_cidr' => $leg['pool_cidr'] ?? null, 'address_cidr' => $leg['address_cidr'] ?? null, 'gateway' => $leg['gateway'] ?? null, 'gateway_on_link' => $leg['gateway_on_link'] ?? null, ]; } private static function exactKeys(array $value, array $expected): bool { $actual = array_keys($value); sort($actual, SORT_STRING); sort($expected, SORT_STRING); return $actual === $expected; } private static function invalid(): array { return [ 'public_network_valid' => false, 'public_network' => null, ]; } }