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.
virt/web/app/Services/Cloud/CloudVpcTransportCapability...

198 lines
7.7 KiB
PHP

<?php
namespace App\Services\Cloud;
/**
* Validate and reduce the Agent's VPC capability envelope.
*
* Host discovery is evidence only: the panel remains the desired-state
* authority and a host is ready only when the saved transport exactly matches
* one bounded Agent candidate and every routing prerequisite is available.
*/
final class CloudVpcTransportCapability
{
public const VERSION = 3;
private const MAX_CANDIDATES = 64;
public static function inspect(array $payload): array
{
$features = $payload['features'] ?? null;
$vpc = $payload['vpc_l3'] ?? null;
if (($payload['backend'] ?? null) !== 'libvirt'
|| !is_array($features) || array_is_list($features)
|| !is_bool($features['vpc_l3'] ?? null)
|| !is_array($vpc) || array_is_list($vpc)
|| ($vpc['version'] ?? null) !== self::VERSION
|| !is_bool($vpc['apply_supported'] ?? null)
|| !is_bool($vpc['vm_port_supported'] ?? null)
|| !is_bool($vpc['ipv4_forwarding'] ?? null)
|| !is_array($vpc['transport_candidates'] ?? null)
|| !array_is_list($vpc['transport_candidates'])
|| count($vpc['transport_candidates']) > self::MAX_CANDIDATES) {
throw self::invalid();
}
$candidates = [];
$previous = null;
foreach ($vpc['transport_candidates'] as $candidate) {
$normalized = self::candidate($candidate);
$orderKey = $normalized['interface'] . "\0"
. $normalized['address_cidr'];
if ($previous !== null && strcmp($previous, $orderKey) >= 0) {
throw self::invalid();
}
$previous = $orderKey;
$candidates[] = $normalized;
}
$checks = self::checks($payload['checks'] ?? null);
return [
'version' => self::VERSION,
'feature_enabled' => $features['vpc_l3'],
'apply_supported' => $vpc['apply_supported'],
'vm_port_supported' => $vpc['vm_port_supported'],
'ipv4_forwarding' => $vpc['ipv4_forwarding'],
'transport_candidates' => $candidates,
'checks' => $checks,
];
}
public static function assertReady(array $capability, array $transport): array
{
if (($capability['feature_enabled'] ?? null) !== true) {
throw new CloudAgentError(
'宿主机 Agent 尚未启用 VPC L3 能力', 409,
'agent_vpc_feature_unavailable', false, 200
);
}
if (($capability['apply_supported'] ?? null) !== true
|| (($capability['checks']['vpc_l3_apply']['available'] ?? null)
!== true)) {
throw new CloudAgentError(
'宿主机 Agent 尚未提供 VPC L3 路由应用能力', 409,
'agent_vpc_apply_unavailable', false, 200
);
}
if (($capability['vm_port_supported'] ?? null) !== true
|| (($capability['checks']['vpc_vm_port']['available'] ?? null)
!== true)) {
throw new CloudAgentError(
'宿主机 Agent 尚未提供 VPC 虚拟机网卡能力', 409,
'agent_vpc_port_unavailable', false, 200
);
}
if (($capability['ipv4_forwarding'] ?? null) !== true
|| (($capability['checks']['vpc_ipv4_forwarding']['available'] ?? null)
!== true)) {
throw new CloudAgentError(
'宿主机未启用 IPv4 转发,不能承载 VPC', 409,
'agent_vpc_forwarding_disabled', false, 200
);
}
if (($capability['checks']['vpc_transport_candidates']['available'] ?? null)
!== true) {
throw new CloudAgentError(
'宿主机没有可用于 VPC 的私网传输地址', 409,
'agent_vpc_transport_unavailable', false, 200
);
}
foreach ($capability['transport_candidates'] ?? [] as $candidate) {
if (hash_equals((string) $candidate['address'],
(string) ($transport['transport_address'] ?? ''))
&& (int) $candidate['prefix_length']
=== (int) ($transport['transport_prefix_length'] ?? 0)
&& hash_equals((string) $candidate['interface'],
(string) ($transport['transport_interface'] ?? ''))
&& (int) $candidate['mtu'] === (int) ($transport['mtu'] ?? 0)) {
return $candidate;
}
}
throw new CloudAgentError(
'面板保存的 VPC 私网地址、接口、前缀或 MTU 与宿主机不一致',
409, 'agent_vpc_transport_mismatch', false, 200
);
}
private static function candidate(mixed $candidate): array
{
if (!is_array($candidate) || array_is_list($candidate)
|| !is_string($candidate['address'] ?? null)
|| !is_string($candidate['address_cidr'] ?? null)
|| !is_int($candidate['prefix_length'] ?? null)
|| !is_string($candidate['interface'] ?? null)
|| !is_int($candidate['mtu'] ?? null)
|| !is_bool($candidate['is_bridge'] ?? null)) {
throw self::invalid();
}
$address = $candidate['address'];
$prefix = $candidate['prefix_length'];
$interface = $candidate['interface'];
$mtu = $candidate['mtu'];
if (!self::isPrivateIpv4($address)
|| $prefix < 1 || $prefix > 32
|| $candidate['address_cidr'] !== $address . '/' . $prefix
|| preg_match('/^[A-Za-z0-9][A-Za-z0-9_.-]{0,14}$/D',
$interface) !== 1
|| $mtu < 576 || $mtu > 65535) {
throw self::invalid();
}
return [
'address_cidr' => $candidate['address_cidr'],
'address' => $address,
'prefix_length' => $prefix,
'interface' => $interface,
'mtu' => $mtu,
'is_bridge' => $candidate['is_bridge'],
'selectable' => $mtu <= 9216,
];
}
private static function checks(mixed $checks): array
{
if (!is_array($checks) || array_is_list($checks)) {
throw self::invalid();
}
$result = [];
foreach ([
'vpc_transport_candidates', 'vpc_ipv4_forwarding', 'vpc_l3_apply',
'vpc_vm_port',
] as $name) {
$check = $checks[$name] ?? null;
if (!is_array($check) || array_is_list($check)
|| !is_bool($check['available'] ?? null)
|| (array_key_exists('reason', $check)
&& (!is_string($check['reason'])
|| preg_match('/^[a-z][a-z0-9_]{0,63}$/D',
$check['reason']) !== 1))) {
throw self::invalid();
}
$result[$name] = [
'available' => $check['available'],
'reason' => (string) ($check['reason'] ?? ''),
];
}
return $result;
}
private static function isPrivateIpv4(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 invalid(): CloudAgentError
{
return new CloudAgentError(
'宿主机 Agent VPC 能力响应无效', 502,
'agent_vpc_capability_invalid', false, 200
);
}
}