$ports * @param array $transports * @return array */ public static function build(array $vpc, array $ports, array $transports, int $revision): array { self::assertVpc($vpc, $revision); if ($ports === []) { return []; } $byHost = []; $usedIps = []; $usedMacs = []; foreach ($ports as $port) { $hostId = filter_var($port['host_id'] ?? null, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]); $ip = $port['private_ip'] ?? null; $mac = $port['mac_address'] ?? null; if ($hostId === false || !is_string($ip) || !is_string($mac) || !self::usableAddress($vpc['cidr'], $vpc['gateway'], $ip) || preg_match('/^[0-9a-f]{2}(?::[0-9a-f]{2}){5}$/D', $mac) !== 1 || (hexdec(substr($mac, 0, 2)) & 1) !== 0 || isset($usedIps[$ip]) || isset($usedMacs[$mac])) { throw new CloudError('VPC 端口账本存在无效或重复地址', 503); } $usedIps[$ip] = true; $usedMacs[$mac] = true; $byHost[(int) $hostId][] = ['ip' => $ip, 'mac' => $mac]; } ksort($byHost, SORT_NUMERIC); foreach (array_keys($byHost) as $hostId) { if (!isset($transports[$hostId])) { throw new CloudError('VPC 宿主机缺少已验证的私网传输配置', 409); } self::assertTransport($transports[$hostId]); } self::assertUnderlayReachability($byHost, $transports); $result = []; foreach ($byHost as $hostId => $localPorts) { usort($localPorts, static fn (array $left, array $right): int => self::compareIpv4($left['ip'], $right['ip'])); $peerRoutes = []; foreach ($byHost as $peerHostId => $peerPorts) { if ($peerHostId === $hostId) { continue; } $via = (string) $transports[$peerHostId]['transport_address']; foreach ($peerPorts as $peerPort) { $peerRoutes[] = [ 'ip' => (string) $peerPort['ip'], 'via' => $via, ]; } } usort($peerRoutes, static function (array $left, array $right): int { $order = self::compareIpv4($left['ip'], $right['ip']); return $order !== 0 ? $order : self::compareIpv4($left['via'], $right['via']); }); $transport = $transports[$hostId]; $network = [ 'vpc_key' => $vpc['vpc_key'], 'desired_revision' => $revision, 'cidr' => $vpc['cidr'], 'gateway' => $vpc['gateway'], 'transport' => [ 'address_cidr' => $transport['transport_address'] . '/' . (int) $transport['transport_prefix_length'], 'interface' => $transport['transport_interface'], 'mtu' => (int) $transport['mtu'], ], 'local_ports' => array_values($localPorts), 'peer_routes' => $peerRoutes, ]; $result[$hostId] = [ 'network' => $network, 'digest' => self::digest($network), ]; } return $result; } public static function digest(array $network): string { return hash('sha256', json_encode($network, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); } public static function bridgeName(string $vpcKey): string { if (preg_match('/^[a-f0-9]{32}$/D', $vpcKey) !== 1) { throw new CloudError('VPC 标识无效', 503); } return 'vc' . substr(hash('sha256', 'virt-cloud-vpc-bridge:' . $vpcKey), 0, 12); } private static function assertVpc(array $vpc, int $revision): void { if ($revision < 1 || preg_match('/^[a-f0-9]{32}$/D', (string) ($vpc['vpc_key'] ?? '')) !== 1 || !self::canonicalPrivateCidr((string) ($vpc['cidr'] ?? '')) || !self::gatewayMatches((string) $vpc['cidr'], (string) ($vpc['gateway'] ?? ''))) { throw new CloudError('VPC 路由投影基础账本无效', 503); } } private static function assertTransport(array $transport): void { $address = $transport['transport_address'] ?? null; $prefix = $transport['transport_prefix_length'] ?? null; $interface = $transport['transport_interface'] ?? null; $mtu = $transport['mtu'] ?? null; if (!is_string($address) || !self::privateIpv4($address) || is_bool($prefix) || !is_int($prefix) || $prefix < 1 || $prefix > 32 || !is_string($interface) || preg_match('/^[A-Za-z0-9][A-Za-z0-9_.-]{0,14}$/D', $interface) !== 1 || is_bool($mtu) || !is_int($mtu) || $mtu < 576 || $mtu > 9216) { throw new CloudError('VPC 宿主机私网传输账本无效', 503); } } private static function assertUnderlayReachability(array $byHost, array $transports): void { foreach (array_keys($byHost) as $hostId) { $transport = $transports[$hostId]; $network = self::ipv4Number($transport['transport_address']) & self::mask((int) $transport['transport_prefix_length']); foreach (array_keys($byHost) as $peerHostId) { if ($peerHostId === $hostId) { continue; } $peer = self::ipv4Number( $transports[$peerHostId]['transport_address'] ); if (($peer & self::mask( (int) $transport['transport_prefix_length'])) !== $network) { throw new CloudError( '同一 VPC 的宿主机私网地址当前不能直接互通', 409 ); } } } } private static function canonicalPrivateCidr(string $cidr): bool { $parts = explode('/', $cidr, 2); if (count($parts) !== 2 || !self::privateIpv4($parts[0]) || preg_match('/^(?:1[6-9]|2[0-8])$/D', $parts[1]) !== 1) { return false; } $prefix = (int) $parts[1]; $number = self::ipv4Number($parts[0]); return ($number & self::mask($prefix)) === $number; } private static function gatewayMatches(string $cidr, string $gateway): bool { [$network] = explode('/', $cidr, 2); return filter_var($gateway, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false && self::ipv4Number($gateway) === self::ipv4Number($network) + 1; } private static function usableAddress(string $cidr, string $gateway, string $address): bool { if (filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) { return false; } [$network, $prefixText] = explode('/', $cidr, 2); $prefix = (int) $prefixText; $base = self::ipv4Number($network); $number = self::ipv4Number($address); $broadcast = $base + (1 << (32 - $prefix)) - 1; return $number > $base + 1 && $number < $broadcast && $address !== $gateway; } private static function privateIpv4(string $address): bool { if (filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) { return false; } $parts = array_map('intval', explode('.', $address)); return $parts[0] === 10 || ($parts[0] === 172 && $parts[1] >= 16 && $parts[1] <= 31) || ($parts[0] === 192 && $parts[1] === 168); } private static function compareIpv4(string $left, string $right): int { return self::ipv4Number($left) <=> self::ipv4Number($right); } private static function ipv4Number(string $address): int { $packed = inet_pton($address); if ($packed === false || strlen($packed) !== 4) { throw new CloudError('VPC IPv4 地址账本无效', 503); } return (int) unpack('Naddress', $packed)['address']; } private static function mask(int $prefix): int { return $prefix === 0 ? 0 : ((0xffffffff << (32 - $prefix)) & 0xffffffff); } }