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/CloudAdminHostNetworkServic...

986 lines
39 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Services\Cloud;
use App\Models\CloudHost;
use App\Models\CloudHostNetworkAttachment;
use App\Models\CloudHostResourcePoolMembership;
use App\Models\CloudNetworkLine;
use App\Models\CloudPublicIp;
use App\Models\CloudPublicIpPool;
use App\Models\CloudResourcePool;
/**
* Administrator-owned desired state for one host's public IPv4 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
* host Agent; the Agent validates it but never stores a second pool catalog.
*/
final class CloudAdminHostNetworkService
{
private const MAX_POOLS = 32;
private const STATES = ['active', 'draining', 'disabled'];
private const OCCUPIED_IP_STATES = [
'reserved', 'assigned', 'changing', 'uncertain',
];
public function read(int $hostId, array $actor): array
{
$host = $this->host($hostId);
$catalog = $this->databaseCatalog($hostId);
return $this->response($host, $catalog);
}
public function create(int $hostId, array $actor, array $body): array
{
$requestKey = $body['request_key'] ?? null;
if (!is_string($requestKey)
|| preg_match('/^[0-9a-f]{32}$/D', $requestKey) !== 1) {
throw new CloudError('请提供有效的地址池创建请求标识', 422);
}
return $this->save(
$hostId,
$actor,
self::poolKeyForRequest($hostId, $requestKey),
$body,
true,
null
);
}
public function update(int $hostId, string $poolKey, array $actor,
array $body): array
{
if (preg_match('/^[0-9a-f]{32}$/D', $poolKey) !== 1) {
throw new CloudError('公网地址池标识无效', 404);
}
$expectedRevision = $body['expected_revision'] ?? null;
if (is_bool($expectedRevision) || !is_int($expectedRevision)
|| $expectedRevision < 1) {
throw new CloudError('公网地址池版本无效,请刷新后重试', 422);
}
return $this->save(
$hostId,
$actor,
$poolKey,
$body,
false,
$expectedRevision
);
}
private function save(int $hostId, array $actor, string $poolKey,
array $body, bool $creating, ?int $expectedRevision): array
{
$host = $this->host($hostId);
$catalog = $this->databaseCatalog($hostId);
$desired = self::normalizeInput($body);
$prepared = $this->prepareDesired(
$host,
$poolKey,
$desired,
$catalog['items'],
$creating,
$expectedRevision
);
if ($prepared['replay']) {
return $this->response($host, $catalog, $poolKey);
}
$applied = [
'pool_key' => $poolKey,
...$prepared['payload'],
];
$replaced = false;
foreach ($catalog['items'] as $index => $item) {
if (hash_equals((string) $item['pool_key'], $poolKey)) {
$catalog['items'][$index] = $applied;
$replaced = true;
break;
}
}
if (!$replaced) {
$catalog['items'][] = $applied;
}
return $this->response(
$this->host($hostId),
$catalog,
$poolKey
);
}
/** Record one desired revision under the host-scoped lock order. */
private function prepareDesired(CloudHost $host, string $poolKey,
array $desired, array $catalogItems, bool $creating,
?int $expectedRevision): array
{
return $host->getConnection()->transaction(function () use (
$host, $poolKey, $desired, $catalogItems, $creating,
$expectedRevision
): array {
$lockedHost = CloudHost::where('id', (int) $host->id)
->lockForUpdate()->first();
if ($lockedHost === null) {
throw new CloudError('宿主机不存在', 404);
}
$memberships = CloudHostPoolMembershipService::lockedRows($lockedHost);
$membership = CloudHostPoolMembershipService::selected(
$memberships
);
if ($membership === null) {
throw new CloudError(
'请先将宿主机加入资源池,再配置该线路的公网地址池',
409
);
}
$resourcePool = CloudResourcePool::where(
'id', (int) $membership->resource_pool_id
)->lockForUpdate()->first();
if ($resourcePool === null || (int) $resourcePool->line_id < 1) {
throw new CloudError('宿主机资源池或线路配置已失效', 409);
}
$selector = (string) $lockedHost->default_network;
if (!CloudNetworkSelector::isValid($selector)) {
throw new CloudError(
'请先在面板配置 bridge:网桥名 或 network:libvirt网络名;旧选择器须核对实际映射后迁移',
409
);
}
$attachmentKey = substr(hash('sha256', $selector), 0, 32);
$attachments = CloudHostNetworkAttachment::where(
'host_id', (int) $lockedHost->id
)->orderBy('id')->lockForUpdate()->get();
$attachment = $attachments->first(static fn ($row): bool =>
hash_equals((string) $row->attachment_key, $attachmentKey)
);
$poolRows = CloudPublicIpPool::where(
'host_id', (int) $lockedHost->id
)->orderBy('id')->lockForUpdate()->get();
$pool = $poolRows->first(static fn ($row): bool =>
hash_equals((string) $row->pool_key, $poolKey)
);
$originalAttachment = $pool === null ? null
: $attachments->first(static fn ($row): bool =>
(int) $row->id === (int) $pool->network_attachment_id
);
$live = null;
$liveKeys = [];
foreach ($catalogItems as $item) {
$liveKeys[(string) $item['pool_key']] = true;
if (hash_equals((string) $item['pool_key'], $poolKey)) {
$live = $item;
}
}
$liveRevision = $live === null ? 0 : (int) $live['revision'];
$revisionBase = max(
$liveRevision,
(int) ($pool?->desired_revision ?? 0),
(int) ($pool?->observed_revision ?? 0)
);
if ($creating) {
if ($pool !== null || $live !== null) {
if ($pool !== null && self::sameCreateRequest(
$pool,
$originalAttachment,
$desired,
$attachmentKey,
$selector,
(int) $resourcePool->line_id
)) {
if ((string) $pool->sync_state === 'applied') {
return [
'replay' => true,
'revision' => $revisionBase,
'payload' => [],
];
}
}
throw new CloudError(
'同一创建请求标识不能用于不同的公网地址池配置',
409
);
}
foreach ($poolRows as $row) {
if (!isset($liveKeys[(string) $row->pool_key])) {
throw new CloudError(
'存在尚未核对的地址池创建记录,请刷新后编辑该记录,不能继续创建新地址池',
409
);
}
}
} else {
if ($pool === null && $live === null) {
throw new CloudError('公网地址池不存在', 404);
}
if ($expectedRevision !== $revisionBase) {
throw new CloudError(
'公网地址池版本已变化,请刷新后重试',
409
);
}
}
if ($pool === null && self::catalogCount($poolRows) >= self::MAX_POOLS) {
throw new CloudError(
'一台宿主机最多配置 32 个公网地址池',
409
);
}
self::assertNoOverlap(
$poolKey,
$desired['cidr'],
$poolRows
);
self::assertNoGlobalOverlap(
(int) $lockedHost->id,
$desired['cidr']
);
$occupiedByPool = [];
$occupiedAddressesByPool = [];
if (!$poolRows->isEmpty()) {
$occupiedRows = CloudPublicIp::where(
'host_id', (int) $lockedHost->id
)->whereIn('public_ip_pool_id', $poolRows->pluck('id')->all())
->where(function ($query): void {
$query->whereIn('state', self::OCCUPIED_IP_STATES)
->orWhereNotNull('reservation_id')
->orWhereNotNull('instance_id')
->orWhereNotNull('operation_id');
})->orderBy('id')->lockForUpdate()
->get(['id', 'public_ip_pool_id', 'address']);
foreach ($occupiedRows as $occupiedRow) {
$id = (int) $occupiedRow->public_ip_pool_id;
$occupiedByPool[$id] = ($occupiedByPool[$id] ?? 0) + 1;
$occupiedAddressesByPool[$id][
(string) $occupiedRow->address
] = true;
}
}
$occupied = $pool === null ? 0
: ($occupiedByPool[(int) $pool->id] ?? 0);
$attachmentLineChanged = $attachment !== null
&& (int) $attachment->line_id
!== (int) $resourcePool->line_id;
if ($attachmentLineChanged) {
foreach ($poolRows as $row) {
if ((int) $row->network_attachment_id
=== (int) $attachment->id
&& ($occupiedByPool[(int) $row->id] ?? 0) > 0) {
throw new CloudError(
'公网接入仍有关联地址被占用,不能切换所属线路',
409
);
}
}
}
$current = $live ?? ($pool === null ? null : [
...self::poolConfig($pool),
'network_selector' => (string) (
$originalAttachment?->network_selector ?? ''
),
]);
$attachmentChanged = $pool !== null && (
$originalAttachment === null
|| (string) $originalAttachment->attachment_key
!== $attachmentKey
|| (string) $originalAttachment->network_selector
!== $selector
|| (int) $originalAttachment->line_id
!== (int) $resourcePool->line_id
);
$occupiedAddresses = $pool === null ? []
: ($occupiedAddressesByPool[(int) $pool->id] ?? []);
$requestedOccupiedAddresses = array_values(array_filter(
$desired['reserved_ips'],
static fn (string $address): bool =>
isset($occupiedAddresses[$address])
));
if ($requestedOccupiedAddresses !== []) {
sort($requestedOccupiedAddresses, SORT_STRING);
throw new CloudError(
'已分配或任务占用的公网 IP 不能加入保留 / 禁用列表:'
. implode(', ', $requestedOccupiedAddresses),
409
);
}
if ($occupied > 0 && ($attachmentChanged
|| ($current !== null
&& self::occupiedUnsafeRuntimeChanged(
$current, $desired, $selector
)))) {
throw new CloudError(
'该地址池仍有实例或任务占用,只能修改名称、运行状态和未占用的保留 / 禁用 IP',
409
);
}
if ($revisionBase >= PHP_INT_MAX) {
throw new CloudError('公网地址池版本已达到上限', 409);
}
$revision = $revisionBase + 1;
$now = time();
if ($attachment === null) {
$attachment = new CloudHostNetworkAttachment();
$attachment->attachment_key = $attachmentKey;
$attachment->host_id = (int) $lockedHost->id;
$attachment->vlan_id = null;
$attachment->routing_table = null;
$attachment->state = 'disabled';
$attachment->priority = 0;
$attachment->desired_revision = 0;
$attachment->observed_revision = 0;
$attachment->sync_state = 'never';
$attachment->last_error_code = '';
$attachment->created_at = $now;
}
$attachment->line_id = (int) $resourcePool->line_id;
$attachment->name = mb_substr($selector, 0, 80);
$attachment->network_mode = 'selector';
$attachment->network_selector = $selector;
if ($pool === null) {
$pool = new CloudPublicIpPool();
$pool->pool_key = $poolKey;
$pool->host_id = (int) $lockedHost->id;
$pool->network_attachment_id = 0;
$pool->priority = 0;
$pool->observed_revision = 0;
$pool->created_at = $now;
}
$pool->name = $desired['name'];
$pool->cidr = $desired['cidr'];
$pool->gateway = $desired['gateway'];
$pool->guest_prefix_length = $desired['guest_prefix_length'];
$pool->dns_servers_json = json_encode(
$desired['dns_servers'],
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
);
$pool->reserved_ips_json = json_encode(
$desired['reserved_ips'],
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
);
$pool->state = $desired['state'];
$pool->desired_revision = $revision;
// Public-pool configuration is PHP-owned. Desired state, the
// concrete IP ledger and its observed revision commit atomically;
// there is no Agent task or follow-up repair phase.
$pool->observed_revision = $revision;
$pool->sync_state = 'applied';
$pool->last_error_code = '';
$pool->updated_at = $now;
// The attachment row must exist before its identifier can be
// referenced by the pool row. Both remain inside this transaction.
// Pool revisions are independent. The parent revision represents
// evidence for the shared selector, so another child mutation
// must not invalidate an already confirmed egress attachment.
$attachment->desired_revision = max(
1, (int) $attachment->desired_revision
);
$attachment->updated_at = $now;
$attachment->save();
$pool->network_attachment_id = (int) $attachment->id;
$pool->save();
self::materializeIpLedger($pool);
self::refreshAttachmentAggregate($attachment);
return [
'replay' => false,
'revision' => $revision,
'payload' => [
'revision' => $revision,
'state' => $desired['state'],
'network_selector' => $selector,
'cidr' => $desired['cidr'],
'gateway' => $desired['gateway'],
'dns_servers' => $desired['dns_servers'],
'reserved_ips' => $desired['reserved_ips'],
'guest_prefix_length' => $desired['guest_prefix_length'],
],
];
}, 3);
}
private function response(CloudHost $host, array $catalog,
?string $savedPoolKey = null): array
{
$dbRows = CloudPublicIpPool::where('host_id', (int) $host->id)
->orderBy('priority')->orderBy('id')->get();
$byKey = [];
foreach ($dbRows as $row) {
$byKey[(string) $row->pool_key] = $row;
}
$occupied = [];
if (!$dbRows->isEmpty()) {
foreach (CloudPublicIp::whereIn(
'public_ip_pool_id', $dbRows->pluck('id')->all()
)->selectRaw('public_ip_pool_id, COUNT(*) AS aggregate')
->where(function ($query): void {
$query->whereIn('state', self::OCCUPIED_IP_STATES)
->orWhereNotNull('reservation_id')
->orWhereNotNull('instance_id')
->orWhereNotNull('operation_id');
})->groupBy('public_ip_pool_id')->get() as $count) {
$occupied[(int) $count->public_ip_pool_id] = (int) $count->aggregate;
}
}
$items = [];
$seen = [];
foreach ($catalog['items'] as $live) {
$key = (string) $live['pool_key'];
$row = $byKey[$key] ?? null;
$items[] = self::poolDto(
$row,
$live,
$row === null ? 0 : ($occupied[(int) $row->id] ?? 0)
);
$seen[$key] = true;
}
foreach ($dbRows as $row) {
$key = (string) $row->pool_key;
if (isset($seen[$key])) {
continue;
}
$items[] = self::poolDto(
$row,
null,
$occupied[(int) $row->id] ?? 0
);
}
$context = $this->lineContext((int) $host->id);
return [
'host' => [
'id' => (int) $host->id,
'name' => self::safeText((string) $host->name, 80),
'default_network' => self::safeText(
(string) $host->default_network, 128
),
],
'line' => $context,
'items' => $items,
'count' => count($items),
'maximum' => self::MAX_POOLS,
'saved_pool_key' => $savedPoolKey,
];
}
private function lineContext(int $hostId): ?array
{
$host = CloudHost::find($hostId);
$rows = CloudHostResourcePoolMembership::where('host_id', $hostId)
->orderBy('resource_pool_id')->orderBy('id')->get();
$membership = $host === null ? null
: CloudHostPoolMembershipService::selected($rows);
$resourcePool = $membership === null ? null
: CloudResourcePool::find((int) $membership->resource_pool_id);
$lineId = $resourcePool === null ? null : (int) $resourcePool->line_id;
$line = $lineId === null ? null : CloudNetworkLine::find($lineId);
return $line === null ? null : [
'id' => (int) $line->id,
'name' => self::safeText((string) $line->name, 80),
'status' => self::safeText((string) $line->status, 16),
];
}
/** The panel database is the sole catalog for scheduling and IP capacity. */
private function databaseCatalog(int $hostId): array
{
$rows = CloudPublicIpPool::where('host_id', $hostId)
->orderBy('priority')->orderBy('id')->get();
$attachmentIds = $rows->pluck('network_attachment_id')->filter()
->map(static fn ($id): int => (int) $id)->unique()->values()->all();
$attachments = $attachmentIds === [] ? collect()
: CloudHostNetworkAttachment::whereIn('id', $attachmentIds)
->get(['id', 'network_selector'])->keyBy('id');
$items = [];
foreach ($rows as $row) {
$items[] = [
'pool_key' => (string) $row->pool_key,
'revision' => max(0, (int) $row->desired_revision),
'network_selector' => (string) (
$attachments[(int) $row->network_attachment_id]->network_selector
?? ''
),
...self::poolConfig($row),
'state' => (string) $row->state,
];
}
return ['items' => $items];
}
private static function normalizeInput(array $body, int $status = 422): array
{
$name = $body['name'] ?? '';
$state = $body['state'] ?? null;
$prefix = $body['guest_prefix_length'] ?? null;
$dns = $body['dns_servers'] ?? null;
$reserved = $body['reserved_ips'] ?? null;
if (!is_string($name) || trim($name) === ''
|| mb_strlen(trim($name)) > 80
|| preg_match('//u', $name) !== 1
|| preg_match('/[\x00-\x1f\x7f]/u', $name) === 1) {
throw new CloudError('地址池名称需为 1 至 80 个字符', $status);
}
if (!is_string($state) || !in_array($state, self::STATES, true)) {
throw new CloudError('公网地址池状态无效', $status);
}
if (is_bool($prefix) || !is_int($prefix)) {
throw new CloudError('客户机网络前缀无效', $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
);
}
$gateway = self::normalizeIpv4(
$body['gateway'] ?? null, '公网网关', false, $status
);
if (!self::containsUsable($cidr, $gateway)) {
throw new CloudError('公网网关必须位于地址池的可用范围内', $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);
}
$dns = self::normalizeIpList(
$dns, '公网 DNS', false, null, $status
);
$reserved = self::normalizeIpList(
$reserved, '保留 IP', true, $cidr, $status
);
return [
'name' => trim($name),
'cidr' => $cidr,
'gateway' => $gateway,
'dns_servers' => $dns,
'reserved_ips' => $reserved,
'guest_prefix_length' => $prefix,
'state' => $state,
];
}
private static function normalizeCidr($value, int $status): string
{
if (!is_string($value)
|| preg_match('/^([^\/]+)\/([0-9]{1,2})$/D', trim($value), $match) !== 1) {
throw new CloudError('公网地址池必须填写有效的 IPv4 CIDR', $status);
}
$address = self::normalizeIpv4(
$match[1], '公网地址池', false, $status
);
$prefix = (int) $match[2];
if ($prefix < 24 || $prefix > 30) {
throw new CloudError('公网地址池前缀必须为 /24 至 /30', $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);
}
return $networkAddress . '/' . $prefix;
}
private static function normalizeIpList(array $values, string $label,
bool $allowReservedRange, ?string $cidr, int $status): array
{
$result = [];
foreach ($values as $value) {
$address = self::normalizeIpv4(
$value, $label, $allowReservedRange, $status
);
if ($cidr !== null && !self::containsUsable($cidr, $address)) {
throw new CloudError($label . '必须位于地址池的可用范围内', $status);
}
$result[$address] = true;
}
return array_keys($result);
}
private static function normalizeIpv4($value, string $label,
bool $allowReservedRange, int $status): string
{
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);
}
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;
}
private static function containsUsable(string $cidr, string $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;
}
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)];
}
private static function uint(string $address): int
{
$packed = inet_pton($address);
if ($packed === false || strlen($packed) !== 4) {
throw new CloudError('IPv4 地址格式无效', 422);
}
return (int) unpack('Naddress', $packed)['address'];
}
private static function mask(int $prefix): int
{
return (0xffffffff << (32 - $prefix)) & 0xffffffff;
}
private static function assertNoOverlap(string $poolKey, string $cidr,
$poolRows): void
{
$checked = [];
foreach ($poolRows as $row) {
if ((string) $row->pool_key === $poolKey) {
continue;
}
$checked[(string) $row->pool_key] = (string) $row->cidr;
}
foreach ($checked as $other) {
if (self::overlaps($cidr, $other)) {
throw new CloudError('同一宿主机的公网地址池不能重叠', 409);
}
}
}
/** Public addresses are globally unique in cloud_public_ips. */
private static function assertNoGlobalOverlap(int $hostId, string $cidr): void
{
foreach (CloudPublicIpPool::where('host_id', '<>', $hostId)
->get(['pool_key', 'cidr']) as $row) {
if (self::overlaps($cidr, (string) $row->cidr)) {
throw new CloudError(
'公网地址池地址段已被其他宿主机占用,不能重复配置',
409
);
}
}
}
private static function catalogCount($poolRows): int
{
$keys = [];
foreach ($poolRows as $row) {
$keys[(string) $row->pool_key] = true;
}
return count($keys);
}
private static function poolKeyForRequest(int $hostId,
string $requestKey): string
{
return substr(hash_hmac(
'sha256',
'host-public-pool:' . $hostId . ':' . $requestKey,
CloudSecrets::key('public-pool-key')
), 0, 32);
}
private static function sameCreateRequest(CloudPublicIpPool $pool,
?CloudHostNetworkAttachment $attachment, array $desired,
string $attachmentKey, string $selector, int $lineId): bool
{
return (string) $pool->name === $desired['name']
&& (string) $pool->state === $desired['state']
&& !self::runtimeChanged([
...self::poolConfig($pool),
'network_selector' => (string) (
$attachment?->network_selector ?? ''
),
], $desired, $selector)
&& $attachment !== null
&& (string) $attachment->attachment_key === $attachmentKey
&& (int) $attachment->line_id === $lineId;
}
private static function poolConfig(CloudPublicIpPool $pool): array
{
return [
'cidr' => (string) $pool->cidr,
'gateway' => (string) $pool->gateway,
'dns_servers' => self::decodeIpList($pool->dns_servers_json),
'reserved_ips' => self::decodeIpList($pool->reserved_ips_json),
'guest_prefix_length' => (int) $pool->guest_prefix_length,
];
}
private static function runtimeChanged(array $current, array $desired,
string $selector): bool
{
return self::occupiedUnsafeRuntimeChanged(
$current, $desired, $selector
) || array_values($current['reserved_ips'] ?? [])
!== $desired['reserved_ips'];
}
/** Changes here can invalidate the network identity of an occupied IP. */
private static function occupiedUnsafeRuntimeChanged(array $current,
array $desired, string $selector): bool
{
return (string) ($current['network_selector'] ?? $selector) !== $selector
|| (string) ($current['cidr'] ?? '') !== $desired['cidr']
|| (string) ($current['gateway'] ?? '') !== $desired['gateway']
|| array_values($current['dns_servers'] ?? []) !== $desired['dns_servers']
|| (int) ($current['guest_prefix_length'] ?? 0)
!== $desired['guest_prefix_length'];
}
/** Recompute selector availability without coupling independent pools. */
private static function refreshAttachmentAggregate(
CloudHostNetworkAttachment $attachment): void
{
$pools = CloudPublicIpPool::where(
'network_attachment_id', (int) $attachment->id
)->orderBy('id')->get();
$states = $pools->pluck('state')->all();
$attachment->state = in_array('active', $states, true)
? 'active'
: (in_array('draining', $states, true) ? 'draining' : 'disabled');
$rank = [
'applied' => 0,
'never' => 1,
'pending' => 2,
'failed' => 3,
'uncertain' => 4,
];
$aggregate = $pools->isEmpty() ? 'never' : 'applied';
$aggregateRank = $rank[$aggregate];
$errorCode = '';
$confirmedRevision = 0;
$desiredRevision = max(1, (int) $attachment->desired_revision);
$observedRevision = max(0, (int) $attachment->observed_revision);
foreach ($pools as $pool) {
$desiredRevision = max(
$desiredRevision, (int) $pool->desired_revision
);
$observedRevision = max(
$observedRevision, (int) $pool->observed_revision
);
$childState = (string) $pool->sync_state;
if ($childState === 'applied'
&& (int) $pool->observed_revision
< (int) $pool->desired_revision) {
$childState = 'pending';
}
if (!isset($rank[$childState])) {
$childState = 'uncertain';
}
if ($childState === 'applied') {
$confirmedRevision = max(
$confirmedRevision,
(int) $pool->observed_revision
);
}
if ($rank[$childState] > $aggregateRank) {
$aggregate = $childState;
$aggregateRank = $rank[$childState];
$candidate = (string) $pool->last_error_code;
$errorCode = preg_match('/^[a-z0-9_]{1,64}$/D', $candidate) === 1
? $candidate : 'host_public_pool_child_' . $childState;
}
}
if ($confirmedRevision > 0) {
// One applied child is sufficient to keep the shared selector
// usable; a failed sibling does not rewrite another pool's intent.
$evidenceRevision = max(
$desiredRevision, $observedRevision, $confirmedRevision
);
$attachment->desired_revision = $evidenceRevision;
$attachment->observed_revision = $evidenceRevision;
$attachment->sync_state = 'applied';
$attachment->last_error_code = '';
} else {
$attachment->desired_revision = $desiredRevision;
$attachment->observed_revision = min(
$observedRevision, max(0, $desiredRevision - 1)
);
$attachment->sync_state = $aggregate;
$attachment->last_error_code = $errorCode;
}
$attachment->save();
}
/** Materialize the configured CIDR into the panel-owned address ledger. */
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);
$blocked = array_fill_keys(array_merge(
[$config['gateway']], $config['dns_servers'], $config['reserved_ips']
), true);
$managementAddress = CloudPanelUsageService::managementAddress(
CloudHost::find((int) $pool->host_id)
);
if ($managementAddress !== null) {
$blocked[$managementAddress] = true;
}
$wanted = [];
for ($value = $networkValue + 1; $value < $broadcast; $value++) {
$address = long2ip($value);
if ($address !== false) {
$wanted[$address] = isset($blocked[$address]) ? 'disabled' : 'available';
}
}
$rows = CloudPublicIp::where('public_ip_pool_id', (int) $pool->id)
->orderBy('id')->lockForUpdate()->get();
$byAddress = [];
foreach ($rows as $row) {
$byAddress[(string) $row->address] = $row;
}
$now = time();
foreach ($wanted as $address => $state) {
$row = $byAddress[$address] ?? null;
if ($row === null) {
$row = new CloudPublicIp();
$row->host_id = (int) $pool->host_id;
$row->public_ip_pool_id = (int) $pool->id;
$row->address = $address;
$row->created_at = $now;
}
$row->prefix_length = (int) $config['guest_prefix_length'];
if ($row->reservation_id === null && $row->instance_id === null
&& $row->operation_id === null) {
$row->state = $state;
}
$row->updated_at = $now;
$row->save();
unset($byAddress[$address]);
}
foreach ($byAddress as $row) {
if ($row->reservation_id === null && $row->instance_id === null
&& $row->operation_id === null) {
$row->state = 'disabled';
$row->updated_at = $now;
$row->save();
}
}
}
private static function poolDto(?CloudPublicIpPool $row, ?array $live,
int $occupied): array
{
$desired = $row === null ? null : self::poolConfig($row);
return [
'pool_key' => (string) ($row?->pool_key ?? $live['pool_key']),
'name' => self::safeText(
(string) ($row?->name ?? $live['cidr']), 80
),
'cidr' => (string) ($desired['cidr'] ?? $live['cidr']),
'gateway' => (string) ($desired['gateway'] ?? $live['gateway']),
'dns_servers' => $desired['dns_servers'] ?? $live['dns_servers'],
'reserved_ips' => $desired['reserved_ips'] ?? $live['reserved_ips'],
'guest_prefix_length' => (int) (
$desired['guest_prefix_length'] ?? $live['guest_prefix_length']
),
'state' => (string) ($row?->state ?? $live['state']),
'revision' => (int) ($row?->desired_revision ?? $live['revision']),
'expected_revision' => max(
(int) ($row?->desired_revision ?? 0),
(int) ($row?->observed_revision ?? 0),
(int) ($live['revision'] ?? 0)
),
'observed_revision' => (int) (
$row?->observed_revision ?? $live['revision']
),
'live_revision' => $row === null ? null : (int) $row->observed_revision,
'network_selector' => (string) ($live['network_selector'] ?? ''),
'sync_state' => (string) ($row?->sync_state ?? 'untracked'),
'sync_message' => self::syncMessage(
(string) ($row?->sync_state ?? 'untracked')
),
'occupied_count' => max(0, $occupied),
'read_only' => false,
];
}
private static function decodeIpList($value): array
{
try {
$decoded = json_decode((string) $value, true, 32, JSON_THROW_ON_ERROR);
} catch (\Throwable $error) {
return [];
}
return is_array($decoded) && array_is_list($decoded)
? array_values(array_filter($decoded, 'is_string')) : [];
}
private static function syncMessage(string $state): string
{
return [
'applied' => '已应用',
'pending' => '正在保存',
'uncertain' => '账本已锁定(任务失败)',
'failed' => '账本保存失败',
'never' => '尚未保存',
'untracked' => '面板配置',
][$state] ?? '应用状态未知';
}
private function host(int $hostId): CloudHost
{
if ($hostId < 1 || ($host = CloudHost::find($hostId)) === null) {
throw new CloudError('宿主机不存在', 404);
}
return $host;
}
private static function safeText(string $value, int $maximum): string
{
$value = preg_replace('/[\x00-\x1f\x7f]/u', '', $value) ?? '';
return mb_substr(trim($value), 0, $maximum);
}
}