|
|
|
|
@ -9,12 +9,13 @@ use App\Models\CloudHostResourcePoolMembership;
|
|
|
|
|
use App\Models\CloudNetworkLine;
|
|
|
|
|
use App\Models\CloudPublicIp;
|
|
|
|
|
use App\Models\CloudPublicIpPool;
|
|
|
|
|
use App\Models\CloudPublicIpPoolCatalogMutex;
|
|
|
|
|
use App\Models\CloudPublicIpPoolReplacementBatch;
|
|
|
|
|
use App\Models\CloudResourcePool;
|
|
|
|
|
use App\Models\CloudResourceReservation;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Administrator-owned desired state for one host's public IPv4 pools.
|
|
|
|
|
* Administrator-owned desired state for one host's public IPv4/IPv6 pools.
|
|
|
|
|
*
|
|
|
|
|
* PHP is the sole authority for public-pool configuration and IP allocation.
|
|
|
|
|
* VM create/change-network jobs carry a signed immutable pool snapshot to the
|
|
|
|
|
@ -23,6 +24,7 @@ use App\Models\CloudResourceReservation;
|
|
|
|
|
final class CloudAdminHostNetworkService
|
|
|
|
|
{
|
|
|
|
|
private const MAX_POOLS = 32;
|
|
|
|
|
private const MAX_IPV6_ALLOCATIONS = 65535;
|
|
|
|
|
private const STATES = ['active', 'draining', 'disabled'];
|
|
|
|
|
private const OCCUPIED_IP_STATES = [
|
|
|
|
|
'reserved', 'assigned', 'changing', 'uncertain',
|
|
|
|
|
@ -122,6 +124,14 @@ final class CloudAdminHostNetworkService
|
|
|
|
|
$host, $poolKey, $desired, $catalogItems, $creating,
|
|
|
|
|
$expectedRevision
|
|
|
|
|
): array {
|
|
|
|
|
$catalogMutex = CloudPublicIpPoolCatalogMutex::where('id', 1)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
if ($catalogMutex === null) {
|
|
|
|
|
throw new CloudError(
|
|
|
|
|
'公网地址池全局目录锁未初始化,请先完成数据库结构更新',
|
|
|
|
|
503
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$lockedHost = CloudHost::where('id', (int) $host->id)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
if ($lockedHost === null) {
|
|
|
|
|
@ -241,14 +251,24 @@ final class CloudAdminHostNetworkService
|
|
|
|
|
409
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if ($pool !== null
|
|
|
|
|
&& (int) $pool->address_family
|
|
|
|
|
!== $desired['address_family']) {
|
|
|
|
|
throw new CloudError(
|
|
|
|
|
'公网地址池地址族创建后不能修改,请新建目标地址族的地址池',
|
|
|
|
|
409
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self::assertNoOverlap(
|
|
|
|
|
$poolKey,
|
|
|
|
|
$desired['address_family'],
|
|
|
|
|
$desired['cidr'],
|
|
|
|
|
$poolRows
|
|
|
|
|
);
|
|
|
|
|
self::assertNoGlobalOverlap(
|
|
|
|
|
(int) $lockedHost->id,
|
|
|
|
|
$desired['address_family'],
|
|
|
|
|
$desired['cidr']
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
@ -323,8 +343,10 @@ final class CloudAdminHostNetworkService
|
|
|
|
|
$pool->created_at = $now;
|
|
|
|
|
}
|
|
|
|
|
$pool->name = $desired['name'];
|
|
|
|
|
$pool->address_family = $desired['address_family'];
|
|
|
|
|
$pool->cidr = $desired['cidr'];
|
|
|
|
|
$pool->gateway = $desired['gateway'];
|
|
|
|
|
$pool->gateway_on_link = $desired['gateway_on_link'];
|
|
|
|
|
$pool->guest_prefix_length = $desired['guest_prefix_length'];
|
|
|
|
|
$pool->dns_servers_json = json_encode(
|
|
|
|
|
$desired['dns_servers'],
|
|
|
|
|
@ -334,6 +356,12 @@ final class CloudAdminHostNetworkService
|
|
|
|
|
$desired['reserved_ips'],
|
|
|
|
|
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
|
|
|
|
|
);
|
|
|
|
|
$pool->allocation_start = $desired['allocation_start'];
|
|
|
|
|
$pool->allocation_end = $desired['allocation_end'];
|
|
|
|
|
$pool->allocation_cursor = $pool->address_family === 6
|
|
|
|
|
? ($pool->allocation_cursor ?? $desired['allocation_start'])
|
|
|
|
|
: null;
|
|
|
|
|
$pool->max_allocations = $desired['max_allocations'];
|
|
|
|
|
$pool->state = $desired['state'];
|
|
|
|
|
$pool->desired_revision = $revision;
|
|
|
|
|
// Public-pool configuration is PHP-owned. Desired state, the
|
|
|
|
|
@ -356,10 +384,15 @@ final class CloudAdminHostNetworkService
|
|
|
|
|
'state' => $desired['state'],
|
|
|
|
|
'network_attachment_id' => (int) $attachment->id,
|
|
|
|
|
'network_selector' => (string) $attachment->network_selector,
|
|
|
|
|
'address_family' => $desired['address_family'],
|
|
|
|
|
'cidr' => $desired['cidr'],
|
|
|
|
|
'gateway' => $desired['gateway'],
|
|
|
|
|
'gateway_on_link' => $desired['gateway_on_link'],
|
|
|
|
|
'dns_servers' => $desired['dns_servers'],
|
|
|
|
|
'reserved_ips' => $desired['reserved_ips'],
|
|
|
|
|
'allocation_start' => $desired['allocation_start'],
|
|
|
|
|
'allocation_end' => $desired['allocation_end'],
|
|
|
|
|
'max_allocations' => $desired['max_allocations'],
|
|
|
|
|
'guest_prefix_length' => $desired['guest_prefix_length'],
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
@ -380,8 +413,6 @@ final class CloudAdminHostNetworkService
|
|
|
|
|
$lines = $lineIds === [] ? collect()
|
|
|
|
|
: CloudNetworkLine::whereIn('id', $lineIds)->get()->keyBy('id');
|
|
|
|
|
$agent = CloudHostAgent::find((int) $host->id);
|
|
|
|
|
$testedSelectors = $agent === null ? []
|
|
|
|
|
: CloudAgentSettingsService::testedNetworkSelectors($agent);
|
|
|
|
|
$activePoolIds = CloudHostResourcePoolMembership::where(
|
|
|
|
|
'host_id', (int) $host->id
|
|
|
|
|
)->where('scheduler_state', 'active')
|
|
|
|
|
@ -400,7 +431,6 @@ final class CloudAdminHostNetworkService
|
|
|
|
|
$attachmentRow,
|
|
|
|
|
$lines->get((int) $attachmentRow->line_id),
|
|
|
|
|
$agent,
|
|
|
|
|
$testedSelectors,
|
|
|
|
|
$activeLineIds
|
|
|
|
|
);
|
|
|
|
|
$attachments[] = $dto;
|
|
|
|
|
@ -412,6 +442,7 @@ final class CloudAdminHostNetworkService
|
|
|
|
|
}
|
|
|
|
|
$occupied = [];
|
|
|
|
|
$assigned = [];
|
|
|
|
|
$available = [];
|
|
|
|
|
if (!$dbRows->isEmpty()) {
|
|
|
|
|
foreach (CloudPublicIp::whereIn(
|
|
|
|
|
'public_ip_pool_id', $dbRows->pluck('id')->all()
|
|
|
|
|
@ -424,13 +455,21 @@ final class CloudAdminHostNetworkService
|
|
|
|
|
})->groupBy('public_ip_pool_id')->get() as $count) {
|
|
|
|
|
$occupied[(int) $count->public_ip_pool_id] = (int) $count->aggregate;
|
|
|
|
|
}
|
|
|
|
|
foreach (CloudResourceReservation::whereIn(
|
|
|
|
|
foreach (CloudPublicIp::whereIn(
|
|
|
|
|
'public_ip_pool_id', $dbRows->pluck('id')->all()
|
|
|
|
|
)->where('state', 'assigned')
|
|
|
|
|
->selectRaw('public_ip_pool_id, COUNT(*) AS aggregate')
|
|
|
|
|
->groupBy('public_ip_pool_id')->get() as $count) {
|
|
|
|
|
$assigned[(int) $count->public_ip_pool_id] = (int) $count->aggregate;
|
|
|
|
|
}
|
|
|
|
|
foreach (CloudPublicIp::whereIn(
|
|
|
|
|
'public_ip_pool_id', $dbRows->pluck('id')->all()
|
|
|
|
|
)->where('state', 'committed')->whereNotNull('instance_id')
|
|
|
|
|
)->where('state', 'available')
|
|
|
|
|
->whereNull('reservation_id')->whereNull('instance_id')
|
|
|
|
|
->whereNull('operation_id')
|
|
|
|
|
->selectRaw('public_ip_pool_id, COUNT(*) AS aggregate')
|
|
|
|
|
->groupBy('public_ip_pool_id')->get() as $count) {
|
|
|
|
|
$assigned[(int) $count->public_ip_pool_id] =
|
|
|
|
|
(int) $count->aggregate;
|
|
|
|
|
$available[(int) $count->public_ip_pool_id] = (int) $count->aggregate;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -484,6 +523,11 @@ final class CloudAdminHostNetworkService
|
|
|
|
|
$attachmentsById[$attachmentId]
|
|
|
|
|
?? self::missingAttachmentDto($attachmentId),
|
|
|
|
|
$row === null ? 0 : ($assigned[(int) $row->id] ?? 0),
|
|
|
|
|
$row === null ? 0 : self::availableCount(
|
|
|
|
|
$row,
|
|
|
|
|
$occupied[(int) $row->id] ?? 0,
|
|
|
|
|
$available[(int) $row->id] ?? 0
|
|
|
|
|
),
|
|
|
|
|
$row === null ? null
|
|
|
|
|
: ($replacementByPool[(int) $row->id] ?? null),
|
|
|
|
|
$row === null ? 0
|
|
|
|
|
@ -505,11 +549,59 @@ final class CloudAdminHostNetworkService
|
|
|
|
|
(int) $row->network_attachment_id
|
|
|
|
|
),
|
|
|
|
|
$assigned[(int) $row->id] ?? 0,
|
|
|
|
|
self::availableCount(
|
|
|
|
|
$row,
|
|
|
|
|
$occupied[(int) $row->id] ?? 0,
|
|
|
|
|
$available[(int) $row->id] ?? 0
|
|
|
|
|
),
|
|
|
|
|
$replacementByPool[(int) $row->id] ?? null,
|
|
|
|
|
$replacementHistoryByPool[(int) $row->id] ?? 0
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$familyCapacityByAttachment = [];
|
|
|
|
|
foreach ($items as $item) {
|
|
|
|
|
if ((string) ($item['state'] ?? '') !== 'active'
|
|
|
|
|
|| (string) ($item['sync_state'] ?? '') !== 'applied') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$attachmentId = (int) ($item['network_attachment_id'] ?? 0);
|
|
|
|
|
$family = (int) ($item['address_family'] ?? 0);
|
|
|
|
|
if ($attachmentId > 0 && in_array($family, [4, 6], true)) {
|
|
|
|
|
$familyCapacityByAttachment[$attachmentId][$family] =
|
|
|
|
|
($familyCapacityByAttachment[$attachmentId][$family] ?? 0)
|
|
|
|
|
+ max(0, (int) ($item['available_count'] ?? 0));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
foreach ($attachments as &$attachment) {
|
|
|
|
|
$capacity = $familyCapacityByAttachment[(int) $attachment['id']] ?? [];
|
|
|
|
|
$attachment['public_network_capacity'] = [
|
|
|
|
|
'ipv4' => max(0, (int) ($capacity[4] ?? 0)),
|
|
|
|
|
'ipv6' => max(0, (int) ($capacity[6] ?? 0)),
|
|
|
|
|
'bundles' => min(
|
|
|
|
|
max(0, (int) ($capacity[4] ?? 0)),
|
|
|
|
|
max(0, (int) ($capacity[6] ?? 0))
|
|
|
|
|
),
|
|
|
|
|
];
|
|
|
|
|
$attachment['dual_stack_ready'] = $attachment['ready'] === true
|
|
|
|
|
&& $attachment['public_network_capacity']['bundles'] > 0;
|
|
|
|
|
$attachmentsById[(int) $attachment['id']] = $attachment;
|
|
|
|
|
}
|
|
|
|
|
unset($attachment);
|
|
|
|
|
foreach ($items as &$item) {
|
|
|
|
|
$attachment = $attachmentsById[
|
|
|
|
|
(int) ($item['network_attachment_id'] ?? 0)
|
|
|
|
|
] ?? null;
|
|
|
|
|
if ($attachment !== null) {
|
|
|
|
|
$item['attachment'] = $attachment;
|
|
|
|
|
$item['dual_stack_ready'] =
|
|
|
|
|
(bool) ($attachment['dual_stack_ready'] ?? false);
|
|
|
|
|
} else {
|
|
|
|
|
$item['dual_stack_ready'] = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
unset($item);
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'host' => [
|
|
|
|
|
'id' => (int) $host->id,
|
|
|
|
|
@ -589,17 +681,13 @@ final class CloudAdminHostNetworkService
|
|
|
|
|
if ((string) $attachment->state !== 'active') {
|
|
|
|
|
throw new CloudError('所选网络接入未启用', 409);
|
|
|
|
|
}
|
|
|
|
|
$testedSelectors = $agent === null ? []
|
|
|
|
|
: CloudAgentSettingsService::testedNetworkSelectors($agent);
|
|
|
|
|
if ($agent === null || !$agent->isReady()
|
|
|
|
|
if ($agent === null
|
|
|
|
|
|| (string) $attachment->verification_state !== 'verified'
|
|
|
|
|
|| (int) $attachment->verified_agent_revision
|
|
|
|
|
!== (int) $agent->revision
|
|
|
|
|
|| (int) $attachment->last_verified_at < 1
|
|
|
|
|
|| !in_array(
|
|
|
|
|
|| !CloudAgentSettingsService::publicNetworkReady(
|
|
|
|
|
$agent,
|
|
|
|
|
(string) $attachment->network_selector,
|
|
|
|
|
$testedSelectors,
|
|
|
|
|
true
|
|
|
|
|
(int) $attachment->verified_agent_revision
|
|
|
|
|
)) {
|
|
|
|
|
throw new CloudError(
|
|
|
|
|
'所选网络接入没有当前 Agent 版本的有效验证证据,请重新测试',
|
|
|
|
|
@ -611,15 +699,20 @@ final class CloudAdminHostNetworkService
|
|
|
|
|
private static function normalizeInput(array $body, int $status = 422): array
|
|
|
|
|
{
|
|
|
|
|
$attachmentId = $body['network_attachment_id'] ?? null;
|
|
|
|
|
$family = $body['address_family'] ?? null;
|
|
|
|
|
$name = $body['name'] ?? '';
|
|
|
|
|
$state = $body['state'] ?? null;
|
|
|
|
|
$prefix = $body['guest_prefix_length'] ?? null;
|
|
|
|
|
$gatewayOnLink = $body['gateway_on_link'] ?? null;
|
|
|
|
|
$dns = $body['dns_servers'] ?? null;
|
|
|
|
|
$reserved = $body['reserved_ips'] ?? null;
|
|
|
|
|
if (is_bool($attachmentId) || !is_int($attachmentId)
|
|
|
|
|
|| $attachmentId < 1) {
|
|
|
|
|
throw new CloudError('请选择有效的宿主机网络接入', $status);
|
|
|
|
|
}
|
|
|
|
|
if (!is_int($family) || !in_array($family, [4, 6], true)) {
|
|
|
|
|
throw new CloudError('请选择有效的公网地址族', $status);
|
|
|
|
|
}
|
|
|
|
|
if (!is_string($name) || trim($name) === ''
|
|
|
|
|
|| mb_strlen(trim($name)) > 80
|
|
|
|
|
|| preg_match('//u', $name) !== 1
|
|
|
|
|
@ -632,130 +725,205 @@ final class CloudAdminHostNetworkService
|
|
|
|
|
if (is_bool($prefix) || !is_int($prefix)) {
|
|
|
|
|
throw new CloudError('客户机网络前缀无效', $status);
|
|
|
|
|
}
|
|
|
|
|
if (!is_bool($gatewayOnLink)) {
|
|
|
|
|
throw new CloudError('网关 on-link 标记无效', $status);
|
|
|
|
|
}
|
|
|
|
|
if (!is_array($dns) || !array_is_list($dns)
|
|
|
|
|
|| !is_array($reserved) || !array_is_list($reserved)) {
|
|
|
|
|
throw new CloudError('公网地址列表格式无效', $status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$cidr = self::normalizeCidr($body['cidr'] ?? null, $status);
|
|
|
|
|
[, $networkPrefix] = explode('/', $cidr, 2);
|
|
|
|
|
$networkPrefix = (int) $networkPrefix;
|
|
|
|
|
if ($prefix < 24 || $prefix > $networkPrefix) {
|
|
|
|
|
throw new CloudError(
|
|
|
|
|
'客户机网络前缀必须介于 /24 和地址池前缀之间',
|
|
|
|
|
$status
|
|
|
|
|
);
|
|
|
|
|
$network = self::normalizeNetworkPrefix(
|
|
|
|
|
$body['cidr'] ?? null, $family, $status
|
|
|
|
|
);
|
|
|
|
|
$cidr = $network->networkCidr();
|
|
|
|
|
if ($prefix !== $network->prefixLength()) {
|
|
|
|
|
throw new CloudError('客户机网络前缀必须与地址池前缀一致', $status);
|
|
|
|
|
}
|
|
|
|
|
$gateway = self::normalizeIpv4(
|
|
|
|
|
$body['gateway'] ?? null, '公网网关', false, $status
|
|
|
|
|
$gateway = self::normalizePublicAddress(
|
|
|
|
|
$body['gateway'] ?? null, $family, '公网网关', $status
|
|
|
|
|
);
|
|
|
|
|
if (!self::containsUsable($cidr, $gateway)) {
|
|
|
|
|
throw new CloudError('公网网关必须位于地址池的可用范围内', $status);
|
|
|
|
|
$gatewayInPool = self::addressUsableInPrefix($network, $gateway);
|
|
|
|
|
if ($family === 4 && ($gatewayOnLink || !$gatewayInPool)) {
|
|
|
|
|
throw new CloudError('IPv4 网关必须位于地址池可用范围内且不能启用 on-link', $status);
|
|
|
|
|
}
|
|
|
|
|
if ($family === 6 && !$gatewayInPool && !$gatewayOnLink) {
|
|
|
|
|
throw new CloudError('地址池外的 IPv6 网关必须启用 on-link', $status);
|
|
|
|
|
}
|
|
|
|
|
if (count($dns) < 1 || count($dns) > 4) {
|
|
|
|
|
throw new CloudError('公网 DNS 必须填写 1 至 4 个地址', $status);
|
|
|
|
|
}
|
|
|
|
|
if (count($reserved) > 254) {
|
|
|
|
|
throw new CloudError('保留 IP 最多可填写 254 个地址', $status);
|
|
|
|
|
if (count($reserved) > 256) {
|
|
|
|
|
throw new CloudError('保留 IP 最多可填写 256 个地址', $status);
|
|
|
|
|
}
|
|
|
|
|
$dns = self::normalizeIpList(
|
|
|
|
|
$dns, '公网 DNS', false, null, $status
|
|
|
|
|
$dns = self::normalizePublicAddressList(
|
|
|
|
|
$dns, null, '公网 DNS', null, $status
|
|
|
|
|
);
|
|
|
|
|
$reserved = self::normalizeIpList(
|
|
|
|
|
$reserved, '保留 IP', true, $cidr, $status
|
|
|
|
|
$reserved = self::normalizePublicAddressList(
|
|
|
|
|
$reserved, $family, '保留 IP', $network, $status
|
|
|
|
|
);
|
|
|
|
|
$allocationStart = null;
|
|
|
|
|
$allocationEnd = null;
|
|
|
|
|
$maxAllocations = $body['max_allocations'] ?? null;
|
|
|
|
|
if ($family === 4) {
|
|
|
|
|
if (($body['allocation_start'] ?? null) !== null
|
|
|
|
|
|| ($body['allocation_end'] ?? null) !== null
|
|
|
|
|
|| $maxAllocations !== 0) {
|
|
|
|
|
throw new CloudError('IPv4 地址池不使用稀疏分配范围', $status);
|
|
|
|
|
}
|
|
|
|
|
$maxAllocations = 0;
|
|
|
|
|
} else {
|
|
|
|
|
if (is_bool($maxAllocations) || !is_int($maxAllocations)
|
|
|
|
|
|| $maxAllocations < 1
|
|
|
|
|
|| $maxAllocations > self::MAX_IPV6_ALLOCATIONS) {
|
|
|
|
|
throw new CloudError(
|
|
|
|
|
'IPv6 最大分配数量必须为 1 至 '
|
|
|
|
|
. self::MAX_IPV6_ALLOCATIONS,
|
|
|
|
|
$status
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$start = self::normalizePublicAddress(
|
|
|
|
|
$body['allocation_start'] ?? null,
|
|
|
|
|
6,
|
|
|
|
|
'IPv6 分配起始地址',
|
|
|
|
|
$status
|
|
|
|
|
);
|
|
|
|
|
$end = self::normalizePublicAddress(
|
|
|
|
|
$body['allocation_end'] ?? null,
|
|
|
|
|
6,
|
|
|
|
|
'IPv6 分配结束地址',
|
|
|
|
|
$status
|
|
|
|
|
);
|
|
|
|
|
if (!self::addressUsableInPrefix($network, $start)
|
|
|
|
|
|| !self::addressUsableInPrefix($network, $end)
|
|
|
|
|
|| $start->compare($end) > 0) {
|
|
|
|
|
throw new CloudError('IPv6 稀疏分配范围必须位于地址池内且顺序有效', $status);
|
|
|
|
|
}
|
|
|
|
|
$blocked = [];
|
|
|
|
|
foreach (array_merge(
|
|
|
|
|
[$gateway->text()], $dns, $reserved
|
|
|
|
|
) as $blockedText) {
|
|
|
|
|
$blockedAddress = CloudIpAddress::parse($blockedText);
|
|
|
|
|
if ($blockedAddress->family() === 6
|
|
|
|
|
&& $blockedAddress->compare($start) >= 0
|
|
|
|
|
&& $blockedAddress->compare($end) <= 0) {
|
|
|
|
|
$blocked[$blockedAddress->text()] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$candidate = $start;
|
|
|
|
|
$usable = 0;
|
|
|
|
|
while (true) {
|
|
|
|
|
if (!isset($blocked[$candidate->text()])) {
|
|
|
|
|
$usable++;
|
|
|
|
|
if ($usable >= $maxAllocations) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ($candidate->equals($end)) {
|
|
|
|
|
throw new CloudError(
|
|
|
|
|
'IPv6 稀疏分配范围扣除网关、DNS 和保留地址后小于最大分配数量',
|
|
|
|
|
$status
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$candidate = $candidate->next();
|
|
|
|
|
}
|
|
|
|
|
$allocationStart = $start->text();
|
|
|
|
|
$allocationEnd = $end->text();
|
|
|
|
|
}
|
|
|
|
|
return [
|
|
|
|
|
'network_attachment_id' => $attachmentId,
|
|
|
|
|
'address_family' => $family,
|
|
|
|
|
'name' => trim($name),
|
|
|
|
|
'cidr' => $cidr,
|
|
|
|
|
'gateway' => $gateway,
|
|
|
|
|
'gateway' => $gateway->text(),
|
|
|
|
|
'gateway_on_link' => $gatewayOnLink,
|
|
|
|
|
'dns_servers' => $dns,
|
|
|
|
|
'reserved_ips' => $reserved,
|
|
|
|
|
'allocation_start' => $allocationStart,
|
|
|
|
|
'allocation_end' => $allocationEnd,
|
|
|
|
|
'max_allocations' => $maxAllocations,
|
|
|
|
|
'guest_prefix_length' => $prefix,
|
|
|
|
|
'state' => $state,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function normalizeCidr($value, int $status): string
|
|
|
|
|
private static function normalizeNetworkPrefix($value, int $family,
|
|
|
|
|
int $status): CloudIpPrefix
|
|
|
|
|
{
|
|
|
|
|
if (!is_string($value)
|
|
|
|
|
|| preg_match('/^([^\/]+)\/([0-9]{1,2})$/D', trim($value), $match) !== 1) {
|
|
|
|
|
throw new CloudError('公网地址池必须填写有效的 IPv4 CIDR', $status);
|
|
|
|
|
try {
|
|
|
|
|
$prefix = CloudIpPrefix::network($value);
|
|
|
|
|
} catch (\Throwable) {
|
|
|
|
|
throw new CloudError('公网地址池 CIDR 格式无效', $status);
|
|
|
|
|
}
|
|
|
|
|
$address = self::normalizeIpv4(
|
|
|
|
|
$match[1], '公网地址池', false, $status
|
|
|
|
|
);
|
|
|
|
|
$prefix = (int) $match[2];
|
|
|
|
|
if ($prefix < 24 || $prefix > 30) {
|
|
|
|
|
throw new CloudError('公网地址池前缀必须为 /24 至 /30', $status);
|
|
|
|
|
if ($prefix->family() !== $family
|
|
|
|
|
|| !$prefix->networkAddress()->isPublicUnicast()) {
|
|
|
|
|
throw new CloudError('公网地址池必须使用所选地址族的可路由网段', $status);
|
|
|
|
|
}
|
|
|
|
|
$network = self::uint($address) & self::mask($prefix);
|
|
|
|
|
$networkAddress = long2ip($network);
|
|
|
|
|
$broadcastAddress = long2ip($network | (~self::mask($prefix) & 0xffffffff));
|
|
|
|
|
if (!self::isPublicIpv4($networkAddress)
|
|
|
|
|
|| !self::isPublicIpv4($broadcastAddress)) {
|
|
|
|
|
throw new CloudError('公网地址池必须是可路由的 IPv4 网段', $status);
|
|
|
|
|
$length = $prefix->prefixLength();
|
|
|
|
|
if (($family === 4 && ($length < 24 || $length > 30))
|
|
|
|
|
|| ($family === 6 && ($length < 48 || $length > 120))) {
|
|
|
|
|
throw new CloudError(
|
|
|
|
|
$family === 4
|
|
|
|
|
? 'IPv4 地址池前缀必须为 /24 至 /30'
|
|
|
|
|
: 'IPv6 地址池前缀必须为 /48 至 /120',
|
|
|
|
|
$status
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return $networkAddress . '/' . $prefix;
|
|
|
|
|
return $prefix;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function normalizeIpList(array $values, string $label,
|
|
|
|
|
bool $allowReservedRange, ?string $cidr, int $status): array
|
|
|
|
|
private static function normalizePublicAddressList(array $values,
|
|
|
|
|
?int $family, string $label, ?CloudIpPrefix $prefix,
|
|
|
|
|
int $status): array
|
|
|
|
|
{
|
|
|
|
|
$result = [];
|
|
|
|
|
foreach ($values as $value) {
|
|
|
|
|
$address = self::normalizeIpv4(
|
|
|
|
|
$value, $label, $allowReservedRange, $status
|
|
|
|
|
$address = self::normalizePublicAddress(
|
|
|
|
|
$value, $family, $label, $status
|
|
|
|
|
);
|
|
|
|
|
if ($cidr !== null && !self::containsUsable($cidr, $address)) {
|
|
|
|
|
if ($prefix !== null
|
|
|
|
|
&& !self::addressUsableInPrefix($prefix, $address)) {
|
|
|
|
|
throw new CloudError($label . '必须位于地址池的可用范围内', $status);
|
|
|
|
|
}
|
|
|
|
|
$result[$address] = true;
|
|
|
|
|
$result[$address->text()] = true;
|
|
|
|
|
}
|
|
|
|
|
return array_keys($result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function normalizeIpv4($value, string $label,
|
|
|
|
|
bool $allowReservedRange, int $status): string
|
|
|
|
|
private static function normalizePublicAddress($value, ?int $family,
|
|
|
|
|
string $label, int $status): CloudIpAddress
|
|
|
|
|
{
|
|
|
|
|
if (!is_string($value) || trim($value) !== $value
|
|
|
|
|
|| filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false
|
|
|
|
|
|| (!$allowReservedRange && !self::isPublicIpv4($value))) {
|
|
|
|
|
throw new CloudError($label . '必须是有效的公网 IPv4 地址', $status);
|
|
|
|
|
try {
|
|
|
|
|
$address = CloudIpAddress::parse($value);
|
|
|
|
|
} catch (\Throwable) {
|
|
|
|
|
throw new CloudError($label . '必须是有效的公网 IP 地址', $status);
|
|
|
|
|
}
|
|
|
|
|
return long2ip(self::uint($value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function isPublicIpv4(string $address): bool
|
|
|
|
|
{
|
|
|
|
|
return filter_var(
|
|
|
|
|
$address,
|
|
|
|
|
FILTER_VALIDATE_IP,
|
|
|
|
|
FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE
|
|
|
|
|
) !== false;
|
|
|
|
|
if (($family !== null && $address->family() !== $family)
|
|
|
|
|
|| !$address->isPublicUnicast()) {
|
|
|
|
|
throw new CloudError($label . '必须是有效的公网 IP 地址', $status);
|
|
|
|
|
}
|
|
|
|
|
return $address;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function containsUsable(string $cidr, string $address): bool
|
|
|
|
|
private static function addressUsableInPrefix(CloudIpPrefix $prefix,
|
|
|
|
|
CloudIpAddress $address): bool
|
|
|
|
|
{
|
|
|
|
|
[$networkAddress, $prefix] = explode('/', $cidr, 2);
|
|
|
|
|
$network = self::uint($networkAddress);
|
|
|
|
|
$broadcast = $network | (~self::mask((int) $prefix) & 0xffffffff);
|
|
|
|
|
$candidate = self::uint($address);
|
|
|
|
|
return $candidate > $network && $candidate < $broadcast;
|
|
|
|
|
return $address->isPublicUnicast()
|
|
|
|
|
&& $prefix->contains($address)
|
|
|
|
|
&& !$address->equals($prefix->networkAddress())
|
|
|
|
|
&& ($address->family() !== 4
|
|
|
|
|
|| !$address->equals($prefix->lastAddress()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function overlaps(string $left, string $right): bool
|
|
|
|
|
{
|
|
|
|
|
[$leftStart, $leftEnd] = self::bounds($left);
|
|
|
|
|
[$rightStart, $rightEnd] = self::bounds($right);
|
|
|
|
|
return $leftStart <= $rightEnd && $rightStart <= $leftEnd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function bounds(string $cidr): array
|
|
|
|
|
{
|
|
|
|
|
[$address, $prefix] = explode('/', $cidr, 2);
|
|
|
|
|
$start = self::uint($address) & self::mask((int) $prefix);
|
|
|
|
|
return [$start, $start | (~self::mask((int) $prefix) & 0xffffffff)];
|
|
|
|
|
try {
|
|
|
|
|
return CloudIpPrefix::network($left)->overlaps(
|
|
|
|
|
CloudIpPrefix::network($right)
|
|
|
|
|
);
|
|
|
|
|
} catch (\Throwable) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function uint(string $address): int
|
|
|
|
|
@ -772,14 +940,17 @@ final class CloudAdminHostNetworkService
|
|
|
|
|
return (0xffffffff << (32 - $prefix)) & 0xffffffff;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function assertNoOverlap(string $poolKey, string $cidr,
|
|
|
|
|
$poolRows): void
|
|
|
|
|
private static function assertNoOverlap(string $poolKey, int $family,
|
|
|
|
|
string $cidr, $poolRows): void
|
|
|
|
|
{
|
|
|
|
|
$checked = [];
|
|
|
|
|
foreach ($poolRows as $row) {
|
|
|
|
|
if ((string) $row->pool_key === $poolKey) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if ((int) $row->address_family !== $family) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$checked[(string) $row->pool_key] = (string) $row->cidr;
|
|
|
|
|
}
|
|
|
|
|
foreach ($checked as $other) {
|
|
|
|
|
@ -790,9 +961,11 @@ final class CloudAdminHostNetworkService
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Public addresses are globally unique in cloud_public_ips. */
|
|
|
|
|
private static function assertNoGlobalOverlap(int $hostId, string $cidr): void
|
|
|
|
|
private static function assertNoGlobalOverlap(int $hostId, int $family,
|
|
|
|
|
string $cidr): void
|
|
|
|
|
{
|
|
|
|
|
foreach (CloudPublicIpPool::where('host_id', '<>', $hostId)
|
|
|
|
|
->where('address_family', $family)
|
|
|
|
|
->get(['pool_key', 'cidr']) as $row) {
|
|
|
|
|
if (self::overlaps($cidr, (string) $row->cidr)) {
|
|
|
|
|
throw new CloudError(
|
|
|
|
|
@ -827,6 +1000,7 @@ final class CloudAdminHostNetworkService
|
|
|
|
|
{
|
|
|
|
|
return (string) $pool->name === $desired['name']
|
|
|
|
|
&& (string) $pool->state === $desired['state']
|
|
|
|
|
&& (int) $pool->address_family === $desired['address_family']
|
|
|
|
|
&& (int) $pool->network_attachment_id
|
|
|
|
|
=== $desired['network_attachment_id']
|
|
|
|
|
&& !self::runtimeChanged(self::poolConfig($pool), $desired);
|
|
|
|
|
@ -835,10 +1009,17 @@ final class CloudAdminHostNetworkService
|
|
|
|
|
private static function poolConfig(CloudPublicIpPool $pool): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'address_family' => (int) $pool->address_family,
|
|
|
|
|
'cidr' => (string) $pool->cidr,
|
|
|
|
|
'gateway' => (string) $pool->gateway,
|
|
|
|
|
'gateway_on_link' => (bool) $pool->gateway_on_link,
|
|
|
|
|
'dns_servers' => self::decodeIpList($pool->dns_servers_json),
|
|
|
|
|
'reserved_ips' => self::decodeIpList($pool->reserved_ips_json),
|
|
|
|
|
'allocation_start' => $pool->allocation_start === null
|
|
|
|
|
? null : (string) $pool->allocation_start,
|
|
|
|
|
'allocation_end' => $pool->allocation_end === null
|
|
|
|
|
? null : (string) $pool->allocation_end,
|
|
|
|
|
'max_allocations' => (int) $pool->max_allocations,
|
|
|
|
|
'guest_prefix_length' => (int) $pool->guest_prefix_length,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
@ -856,8 +1037,18 @@ final class CloudAdminHostNetworkService
|
|
|
|
|
array $desired): bool
|
|
|
|
|
{
|
|
|
|
|
return (string) ($current['cidr'] ?? '') !== $desired['cidr']
|
|
|
|
|
|| (int) ($current['address_family'] ?? 0)
|
|
|
|
|
!== $desired['address_family']
|
|
|
|
|
|| (string) ($current['gateway'] ?? '') !== $desired['gateway']
|
|
|
|
|
|| (bool) ($current['gateway_on_link'] ?? false)
|
|
|
|
|
!== $desired['gateway_on_link']
|
|
|
|
|
|| array_values($current['dns_servers'] ?? []) !== $desired['dns_servers']
|
|
|
|
|
|| ($current['allocation_start'] ?? null)
|
|
|
|
|
!== $desired['allocation_start']
|
|
|
|
|
|| ($current['allocation_end'] ?? null)
|
|
|
|
|
!== $desired['allocation_end']
|
|
|
|
|
|| (int) ($current['max_allocations'] ?? 0)
|
|
|
|
|
!== $desired['max_allocations']
|
|
|
|
|
|| (int) ($current['guest_prefix_length'] ?? 0)
|
|
|
|
|
!== $desired['guest_prefix_length'];
|
|
|
|
|
}
|
|
|
|
|
@ -866,9 +1057,7 @@ final class CloudAdminHostNetworkService
|
|
|
|
|
private static function materializeIpLedger(CloudPublicIpPool $pool): void
|
|
|
|
|
{
|
|
|
|
|
$config = self::poolConfig($pool);
|
|
|
|
|
[$network, $prefix] = explode('/', $config['cidr'], 2);
|
|
|
|
|
$networkValue = self::uint($network);
|
|
|
|
|
$broadcast = $networkValue | (~self::mask((int) $prefix) & 0xffffffff);
|
|
|
|
|
$family = (int) $config['address_family'];
|
|
|
|
|
$blocked = array_fill_keys(array_merge(
|
|
|
|
|
[$config['gateway']], $config['dns_servers'], $config['reserved_ips']
|
|
|
|
|
), true);
|
|
|
|
|
@ -876,8 +1065,22 @@ final class CloudAdminHostNetworkService
|
|
|
|
|
CloudHost::find((int) $pool->host_id)
|
|
|
|
|
);
|
|
|
|
|
if ($managementAddress !== null) {
|
|
|
|
|
$blocked[$managementAddress] = true;
|
|
|
|
|
try {
|
|
|
|
|
$management = CloudIpAddress::parse($managementAddress);
|
|
|
|
|
if ($management->family() === $family) {
|
|
|
|
|
$blocked[$management->text()] = true;
|
|
|
|
|
}
|
|
|
|
|
} catch (\Throwable) {
|
|
|
|
|
// Host usage validation owns malformed management metadata.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ($family === 6) {
|
|
|
|
|
self::refreshSparseIpv6Ledger($pool, $config, $blocked);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
[$network, $prefix] = explode('/', $config['cidr'], 2);
|
|
|
|
|
$networkValue = self::uint($network);
|
|
|
|
|
$broadcast = $networkValue | (~self::mask((int) $prefix) & 0xffffffff);
|
|
|
|
|
$wanted = [];
|
|
|
|
|
for ($value = $networkValue + 1; $value < $broadcast; $value++) {
|
|
|
|
|
$address = long2ip($value);
|
|
|
|
|
@ -898,7 +1101,9 @@ final class CloudAdminHostNetworkService
|
|
|
|
|
$row = new CloudPublicIp();
|
|
|
|
|
$row->host_id = (int) $pool->host_id;
|
|
|
|
|
$row->public_ip_pool_id = (int) $pool->id;
|
|
|
|
|
$row->address_family = 4;
|
|
|
|
|
$row->address = $address;
|
|
|
|
|
$row->address_binary = inet_pton($address);
|
|
|
|
|
$row->created_at = $now;
|
|
|
|
|
}
|
|
|
|
|
$row->prefix_length = (int) $config['guest_prefix_length'];
|
|
|
|
|
@ -920,22 +1125,55 @@ final class CloudAdminHostNetworkService
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** IPv6 pools materialize only addresses that were actually claimed. */
|
|
|
|
|
private static function refreshSparseIpv6Ledger(CloudPublicIpPool $pool,
|
|
|
|
|
array $config, array $blocked): void
|
|
|
|
|
{
|
|
|
|
|
$start = CloudIpAddress::parse((string) $config['allocation_start']);
|
|
|
|
|
$end = CloudIpAddress::parse((string) $config['allocation_end']);
|
|
|
|
|
$now = time();
|
|
|
|
|
foreach (CloudPublicIp::where(
|
|
|
|
|
'public_ip_pool_id', (int) $pool->id
|
|
|
|
|
)->orderBy('id')->lockForUpdate()->get() as $row) {
|
|
|
|
|
try {
|
|
|
|
|
$address = CloudIpAddress::parse((string) $row->address);
|
|
|
|
|
$inRange = $address->family() === 6
|
|
|
|
|
&& $address->compare($start) >= 0
|
|
|
|
|
&& $address->compare($end) <= 0;
|
|
|
|
|
} catch (\Throwable) {
|
|
|
|
|
$inRange = false;
|
|
|
|
|
$address = null;
|
|
|
|
|
}
|
|
|
|
|
$row->address_family = 6;
|
|
|
|
|
if ($address !== null) {
|
|
|
|
|
$row->address = $address->text();
|
|
|
|
|
$row->address_binary = $address->binary();
|
|
|
|
|
}
|
|
|
|
|
$row->prefix_length = (int) $config['guest_prefix_length'];
|
|
|
|
|
if ($row->reservation_id === null && $row->instance_id === null
|
|
|
|
|
&& $row->operation_id === null) {
|
|
|
|
|
$row->state = $inRange && $address !== null
|
|
|
|
|
&& !isset($blocked[$address->text()])
|
|
|
|
|
? 'available' : 'disabled';
|
|
|
|
|
}
|
|
|
|
|
$row->updated_at = $now;
|
|
|
|
|
$row->save();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function attachmentDto(
|
|
|
|
|
CloudHostNetworkAttachment $attachment,
|
|
|
|
|
?CloudNetworkLine $line,
|
|
|
|
|
?CloudHostAgent $agent,
|
|
|
|
|
array $testedSelectors,
|
|
|
|
|
array $activeLineIds
|
|
|
|
|
): array {
|
|
|
|
|
$storedVerification = (string) $attachment->verification_state;
|
|
|
|
|
$currentEvidence = $agent !== null && $agent->isReady()
|
|
|
|
|
&& (int) $attachment->verified_agent_revision
|
|
|
|
|
=== (int) $agent->revision
|
|
|
|
|
$currentEvidence = $agent !== null
|
|
|
|
|
&& (int) $attachment->last_verified_at > 0
|
|
|
|
|
&& in_array(
|
|
|
|
|
&& CloudAgentSettingsService::publicNetworkReady(
|
|
|
|
|
$agent,
|
|
|
|
|
(string) $attachment->network_selector,
|
|
|
|
|
$testedSelectors,
|
|
|
|
|
true
|
|
|
|
|
(int) $attachment->verified_agent_revision
|
|
|
|
|
);
|
|
|
|
|
$verification = $storedVerification === 'verified'
|
|
|
|
|
&& !$currentEvidence ? 'stale' : $storedVerification;
|
|
|
|
|
@ -990,6 +1228,7 @@ final class CloudAdminHostNetworkService
|
|
|
|
|
|
|
|
|
|
private static function poolDto(?CloudPublicIpPool $row, ?array $live,
|
|
|
|
|
int $occupied, ?array $attachment, int $assigned = 0,
|
|
|
|
|
int $available = 0,
|
|
|
|
|
?array $activeReplacement = null,
|
|
|
|
|
int $replacementHistoryCount = 0): array
|
|
|
|
|
{
|
|
|
|
|
@ -1006,8 +1245,21 @@ final class CloudAdminHostNetworkService
|
|
|
|
|
),
|
|
|
|
|
'cidr' => (string) ($desired['cidr'] ?? $live['cidr']),
|
|
|
|
|
'gateway' => (string) ($desired['gateway'] ?? $live['gateway']),
|
|
|
|
|
'address_family' => (int) (
|
|
|
|
|
$desired['address_family'] ?? $live['address_family'] ?? 0
|
|
|
|
|
),
|
|
|
|
|
'gateway_on_link' => (bool) (
|
|
|
|
|
$desired['gateway_on_link'] ?? $live['gateway_on_link'] ?? false
|
|
|
|
|
),
|
|
|
|
|
'dns_servers' => $desired['dns_servers'] ?? $live['dns_servers'],
|
|
|
|
|
'reserved_ips' => $desired['reserved_ips'] ?? $live['reserved_ips'],
|
|
|
|
|
'allocation_start' => $desired['allocation_start']
|
|
|
|
|
?? $live['allocation_start'] ?? null,
|
|
|
|
|
'allocation_end' => $desired['allocation_end']
|
|
|
|
|
?? $live['allocation_end'] ?? null,
|
|
|
|
|
'max_allocations' => (int) (
|
|
|
|
|
$desired['max_allocations'] ?? $live['max_allocations'] ?? 0
|
|
|
|
|
),
|
|
|
|
|
'guest_prefix_length' => (int) (
|
|
|
|
|
$desired['guest_prefix_length'] ?? $live['guest_prefix_length']
|
|
|
|
|
),
|
|
|
|
|
@ -1031,6 +1283,7 @@ final class CloudAdminHostNetworkService
|
|
|
|
|
(string) ($row?->sync_state ?? 'untracked')
|
|
|
|
|
),
|
|
|
|
|
'occupied_count' => max(0, $occupied),
|
|
|
|
|
'available_count' => max(0, $available),
|
|
|
|
|
'assigned_instance_count' => max(0, $assigned),
|
|
|
|
|
'replacement_locked' => $activeReplacement !== null,
|
|
|
|
|
'active_replacement' => $activeReplacement,
|
|
|
|
|
@ -1043,6 +1296,15 @@ final class CloudAdminHostNetworkService
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function availableCount(CloudPublicIpPool $pool,
|
|
|
|
|
int $occupied, int $concreteAvailable): int
|
|
|
|
|
{
|
|
|
|
|
if ((int) $pool->address_family === 6) {
|
|
|
|
|
return max(0, (int) $pool->max_allocations - $occupied);
|
|
|
|
|
}
|
|
|
|
|
return max(0, $concreteAvailable);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function decodeIpList($value): array
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
|