diff --git a/web/app/Command/CloudSetup.php b/web/app/Command/CloudSetup.php
index 730c1fa1..779b63c7 100644
--- a/web/app/Command/CloudSetup.php
+++ b/web/app/Command/CloudSetup.php
@@ -8,7 +8,7 @@ use App\Services\Config;
/** Initializes only the clean-break PHP business and Agent log schemas. */
final class CloudSetup
{
- private const SCHEMA_VERSION = 'go-agent-wss-v13-vpc-l3-v1';
+ private const SCHEMA_VERSION = 'go-agent-wss-v13-vpc-l3-v2';
private const MAIN_TABLES = [
'cloud_regions', 'cloud_network_lines', 'cloud_resource_pools',
'cloud_hosts', 'cloud_host_agents',
@@ -75,11 +75,11 @@ final class CloudSetup
'cloud_vpc_cidr_pools' => [
'id', 'pool_key', 'region_id', 'name', 'cidr',
'prefix_length', 'allocation_prefix', 'state',
- 'created_at', 'updated_at',
+ 'revision', 'created_at', 'updated_at',
],
'cloud_vpcs' => [
'id', 'vpc_key', 'user_id', 'region_id', 'cidr_pool_id',
- 'name', 'cidr', 'prefix_length', 'gateway', 'state',
+ 'name', 'cidr', 'prefix_length', 'gateway', 'active_claim', 'state',
'desired_revision', 'deleted_at', 'created_at', 'updated_at',
],
'cloud_vpc_ports' => [
@@ -216,7 +216,8 @@ final class CloudSetup
'chk_cloud_vpc_cidr_pool_state',
],
'cloud_vpcs' => [
- 'chk_cloud_vpc_prefix', 'chk_cloud_vpc_state',
+ 'chk_cloud_vpc_prefix', 'chk_cloud_vpc_active_claim',
+ 'chk_cloud_vpc_state',
'chk_cloud_vpc_deleted_at',
],
'cloud_vpc_ports' => [
diff --git a/web/app/Controllers/Admin/CloudPageController.php b/web/app/Controllers/Admin/CloudPageController.php
index b6951487..1250774f 100644
--- a/web/app/Controllers/Admin/CloudPageController.php
+++ b/web/app/Controllers/Admin/CloudPageController.php
@@ -59,6 +59,11 @@ final class CloudPageController extends AdminController
->display('admin/hosts/index.tpl');
}
+ public function vpc($request, $response, $args)
+ {
+ return $this->view()->display('admin/cloud/vpc.tpl');
+ }
+
public function agents($request, $response, $args)
{
return $this->view()->display('admin/cloud/agents.tpl');
diff --git a/web/app/Controllers/CloudAdminVpcController.php b/web/app/Controllers/CloudAdminVpcController.php
new file mode 100644
index 00000000..6cf36d51
--- /dev/null
+++ b/web/app/Controllers/CloudAdminVpcController.php
@@ -0,0 +1,49 @@
+getAttribute('cloud_actor') ?? CloudAccess::actor(false);
+ if (!($actor['is_admin'] ?? false)) {
+ throw new CloudError('需要管理员权限', 403);
+ }
+ if ($write) {
+ CloudAccess::manage($actor);
+ }
+ }
+
+ public function pools($request, $response, $args = [])
+ {
+ $method = strtoupper((string) $request->getMethod());
+ $write = $method !== 'GET';
+ $this->actor($request, $write);
+ $id = isset($args['id']) ? (int) $args['id'] : null;
+ if (!$write) {
+ $result = $id === null
+ ? CloudAdminVpcCidrPoolService::listing()
+ : CloudAdminVpcCidrPoolService::read($id);
+ $status = 200;
+ } else {
+ $body = CloudInput::object($request);
+ CloudInput::only($body, [
+ 'region_id', 'name', 'cidr', 'allocation_prefix', 'state',
+ 'request_key', 'expected_revision',
+ ]);
+ $result = $id === null
+ ? CloudAdminVpcCidrPoolService::create($body)
+ : CloudAdminVpcCidrPoolService::update($id, $body);
+ $status = $id === null ? 201 : 200;
+ }
+ return $response->withJson($result, $status)
+ ->withHeader('Cache-Control', 'private, no-store');
+ }
+}
diff --git a/web/app/Middleware/CloudApi.php b/web/app/Middleware/CloudApi.php
index 81ce63d0..61494669 100644
--- a/web/app/Middleware/CloudApi.php
+++ b/web/app/Middleware/CloudApi.php
@@ -22,6 +22,7 @@ final class CloudApi
|| preg_match('#^/api/v1/admin/system-logs(?:/|$)#D', $path) === 1
|| preg_match('#^/api/v1/admin/platform-test-policy(?:/|$)#D', $path) === 1
|| preg_match('#^/api/v1/admin/(?:resources|regions|network-lines|resource-pools|resource-reservations)(?:/|$)#D', $path) === 1
+ || preg_match('#^/api/v1/admin/vpc-cidr-pools(?:/[1-9][0-9]*)?$#D', $path) === 1
|| preg_match('#^/api/v1/admin/hosts/[0-9]+/scheduling$#D', $path) === 1
|| preg_match('#^/api/v1/admin/hosts/[1-9][0-9]*/vpc-transport(?:/(?:discover|test))?$#D', $path) === 1
|| preg_match('#^/api/v1/admin/agents(?:/[1-9][0-9]*(?:/test)?)?$#D', $path) === 1
diff --git a/web/app/Models/CloudVpc.php b/web/app/Models/CloudVpc.php
index 90659446..31ffae9f 100644
--- a/web/app/Models/CloudVpc.php
+++ b/web/app/Models/CloudVpc.php
@@ -13,6 +13,7 @@ final class CloudVpc extends Model
'region_id' => 'int',
'cidr_pool_id' => 'int',
'prefix_length' => 'int',
+ 'active_claim' => 'int',
'desired_revision' => 'int',
'deleted_at' => 'int',
'created_at' => 'int',
diff --git a/web/app/Models/CloudVpcCidrPool.php b/web/app/Models/CloudVpcCidrPool.php
index 7a90b544..a5f97d12 100644
--- a/web/app/Models/CloudVpcCidrPool.php
+++ b/web/app/Models/CloudVpcCidrPool.php
@@ -12,6 +12,7 @@ final class CloudVpcCidrPool extends Model
'region_id' => 'int',
'prefix_length' => 'int',
'allocation_prefix' => 'int',
+ 'revision' => 'int',
'created_at' => 'int',
'updated_at' => 'int',
];
diff --git a/web/app/Services/Cloud/CloudAdminVpcCidrPoolService.php b/web/app/Services/Cloud/CloudAdminVpcCidrPoolService.php
new file mode 100644
index 00000000..c4e8982b
--- /dev/null
+++ b/web/app/Services/Cloud/CloudAdminVpcCidrPoolService.php
@@ -0,0 +1,323 @@
+orderBy('sort')->orderBy('id')
+ ->get(['id', 'code', 'name', 'country_code', 'status'])
+ ->map(static fn ($region): array => [
+ 'id' => (int) $region->id,
+ 'code' => (string) $region->code,
+ 'name' => (string) $region->name,
+ 'country_code' => (string) $region->country_code,
+ 'status' => (string) $region->status,
+ ])->all();
+ $regionNames = [];
+ foreach ($regions as $region) {
+ $regionNames[$region['id']] = $region['name'];
+ }
+ $usage = CloudVpc::query()->where('active_claim', 1)
+ ->selectRaw('cidr_pool_id, COUNT(*) AS row_count')
+ ->groupBy('cidr_pool_id')->pluck('row_count', 'cidr_pool_id');
+ $items = CloudVpcCidrPool::query()->orderBy('region_id')->orderBy('id')
+ ->get()->map(static fn ($pool): array => self::dto(
+ $pool,
+ (string) ($regionNames[(int) $pool->region_id]
+ ?? '地区已缺失'),
+ (int) ($usage[(int) $pool->id] ?? 0)
+ ))->all();
+ return [
+ 'regions' => $regions,
+ 'items' => $items,
+ 'feature_enabled' => CloudVpcFeature::enabled(),
+ ];
+ }
+
+ public static function read(int $id): array
+ {
+ $pool = self::pool($id);
+ $region = CloudRegion::find((int) $pool->region_id);
+ return ['item' => self::dto(
+ $pool,
+ (string) ($region?->name ?? '地区已缺失'),
+ self::activeUsage((int) $pool->id)
+ )];
+ }
+
+ public static function create(array $input): array
+ {
+ $requestKey = $input['request_key'] ?? null;
+ if (!is_string($requestKey)
+ || preg_match('/^[0-9a-f]{32}$/D', $requestKey) !== 1) {
+ throw new CloudError('请提供有效的 VPC 地址池创建请求标识');
+ }
+ $desired = self::normalize($input);
+ $poolKey = substr(hash('sha256', "vpc-cidr-pool\0" . $requestKey),
+ 0, 32);
+ return self::persist(null, $poolKey, 0, $desired);
+ }
+
+ public static function update(int $id, array $input): array
+ {
+ $expected = $input['expected_revision'] ?? null;
+ if (is_bool($expected) || !is_int($expected) || $expected < 1) {
+ throw new CloudError('VPC 地址池版本无效,请刷新后重试', 409);
+ }
+ return self::persist($id, '', $expected, self::normalize($input));
+ }
+
+ private static function persist(?int $id, string $poolKey,
+ int $expectedRevision, array $desired): array
+ {
+ $connection = (new CloudVpcCidrPool())->getConnection();
+ $pool = $connection->transaction(static function () use (
+ $id, $poolKey, $expectedRevision, $desired
+ ): CloudVpcCidrPool {
+ $region = CloudRegion::query()->where('id', $desired['region_id'])
+ ->lockForUpdate()->first();
+ if ($region === null) {
+ throw new CloudError('VPC 地址池所属地区不存在', 404);
+ }
+ $rows = CloudVpcCidrPool::query()
+ ->where('region_id', $desired['region_id'])
+ ->orderBy('id')->lockForUpdate()->get();
+ $pool = $id === null
+ ? $rows->first(static fn ($row): bool =>
+ hash_equals((string) $row->pool_key, $poolKey))
+ : $rows->first(static fn ($row): bool =>
+ (int) $row->id === $id);
+
+ if ($id === null && $pool !== null) {
+ if (self::same($pool, $desired)) {
+ return $pool;
+ }
+ throw new CloudError(
+ '同一创建请求标识不能用于不同的 VPC 地址池配置', 409
+ );
+ }
+ if ($id !== null && $pool === null) {
+ if (CloudVpcCidrPool::find($id) !== null) {
+ throw new CloudError('VPC 地址池不允许更换所属地区', 409);
+ }
+ throw new CloudError('VPC 地址池不存在', 404);
+ }
+ if ($pool !== null
+ && (int) $pool->revision !== $expectedRevision) {
+ throw new CloudError(
+ 'VPC 地址池配置已被修改,请刷新后重试', 409
+ );
+ }
+
+ $usage = $pool === null ? 0 : CloudVpc::query()
+ ->where('cidr_pool_id', (int) $pool->id)
+ ->where('active_claim', 1)->orderBy('id')
+ ->lockForUpdate()->get(['id'])->count();
+ if ($pool !== null && $usage > 0
+ && ((string) $pool->cidr !== $desired['cidr']
+ || (int) $pool->allocation_prefix
+ !== $desired['allocation_prefix'])) {
+ throw new CloudError(
+ '该地址池仍有 VPC 占用,不能修改 CIDR 或分配前缀', 409
+ );
+ }
+ foreach ($rows as $other) {
+ if ($pool !== null && (int) $other->id === (int) $pool->id) {
+ continue;
+ }
+ if (self::overlaps($desired['cidr'], (string) $other->cidr)) {
+ throw new CloudError(
+ '同一地区的 VPC 地址池不能重叠', 409
+ );
+ }
+ }
+ if ($pool !== null && self::same($pool, $desired)) {
+ return $pool;
+ }
+ $now = time();
+ if ($pool === null) {
+ $pool = new CloudVpcCidrPool();
+ $pool->pool_key = $poolKey;
+ $pool->created_at = $now;
+ $pool->revision = 1;
+ } else {
+ $pool->revision = (int) $pool->revision + 1;
+ }
+ foreach ($desired as $field => $value) {
+ $pool->{$field} = $value;
+ }
+ $pool->updated_at = $now;
+ $pool->save();
+ return $pool;
+ }, 3);
+ $region = CloudRegion::find((int) $pool->region_id);
+ return ['item' => self::dto(
+ $pool,
+ (string) ($region?->name ?? '地区已缺失'),
+ self::activeUsage((int) $pool->id)
+ )];
+ }
+
+ private static function normalize(array $input): array
+ {
+ $regionId = $input['region_id'] ?? null;
+ $name = $input['name'] ?? null;
+ $cidr = $input['cidr'] ?? null;
+ $allocationPrefix = $input['allocation_prefix'] ?? null;
+ $state = $input['state'] ?? null;
+ if (is_bool($regionId) || !is_int($regionId) || $regionId < 1) {
+ throw new CloudError('请选择有效的 VPC 地址池所属地区');
+ }
+ if (!is_string($name) || ($name = trim($name)) === ''
+ || mb_strlen($name) > 80
+ || preg_match('/[\x00-\x1f\x7f]/u', $name) === 1) {
+ throw new CloudError('VPC 地址池名称需为 1 至 80 个字符');
+ }
+ [$normalizedCidr, $prefix] = self::normalizeCidr($cidr);
+ if (is_bool($allocationPrefix) || !is_int($allocationPrefix)
+ || $allocationPrefix < $prefix || $allocationPrefix > 28) {
+ throw new CloudError(
+ 'VPC 分配前缀必须不小于地址池前缀,且不能大于 28'
+ );
+ }
+ if (!is_string($state) || !in_array($state, self::STATES, true)) {
+ throw new CloudError('VPC 地址池状态无效');
+ }
+ return [
+ 'region_id' => $regionId,
+ 'name' => $name,
+ 'cidr' => $normalizedCidr,
+ 'prefix_length' => $prefix,
+ 'allocation_prefix' => $allocationPrefix,
+ 'state' => $state,
+ ];
+ }
+
+ private static function normalizeCidr(mixed $value): array
+ {
+ if (!is_string($value)
+ || preg_match('/^([^\/]+)\/([0-9]{1,2})$/D', trim($value),
+ $match) !== 1
+ || filter_var($match[1], FILTER_VALIDATE_IP,
+ FILTER_FLAG_IPV4) === false) {
+ throw new CloudError('VPC 地址池必须填写有效的 RFC1918 IPv4 CIDR');
+ }
+ $prefix = (int) $match[2];
+ if ($prefix < 8 || $prefix > 28) {
+ throw new CloudError('VPC 地址池前缀必须为 /8 至 /28');
+ }
+ $address = self::number($match[1]);
+ $mask = self::mask($prefix);
+ $network = $address & $mask;
+ if ($network !== $address) {
+ throw new CloudError('VPC 地址池 CIDR 必须填写规范网络地址');
+ }
+ $end = $network | (~$mask & 0xffffffff);
+ if (!self::insidePrivateRange($network, $end)) {
+ throw new CloudError('VPC 地址池必须完整位于 RFC1918 私网范围');
+ }
+ return [long2ip($network) . '/' . $prefix, $prefix];
+ }
+
+ private static function insidePrivateRange(int $start, int $end): bool
+ {
+ foreach ([['10.0.0.0', 8], ['172.16.0.0', 12],
+ ['192.168.0.0', 16]] as [$address, $prefix]) {
+ $base = self::number($address);
+ $last = $base | (~self::mask($prefix) & 0xffffffff);
+ if ($start >= $base && $end <= $last) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ 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::number($address) & self::mask((int) $prefix);
+ return [$start,
+ $start | (~self::mask((int) $prefix) & 0xffffffff)];
+ }
+
+ private static function number(string $address): int
+ {
+ $packed = inet_pton($address);
+ if ($packed === false || strlen($packed) !== 4) {
+ throw new CloudError('VPC IPv4 地址格式无效');
+ }
+ return (int) unpack('Naddress', $packed)['address'];
+ }
+
+ private static function mask(int $prefix): int
+ {
+ return (0xffffffff << (32 - $prefix)) & 0xffffffff;
+ }
+
+ private static function same(CloudVpcCidrPool $pool,
+ array $desired): bool
+ {
+ foreach ($desired as $field => $value) {
+ if ((string) $pool->{$field} !== (string) $value) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ private static function activeUsage(int $poolId): int
+ {
+ return (int) CloudVpc::query()->where('cidr_pool_id', $poolId)
+ ->where('active_claim', 1)->count();
+ }
+
+ private static function pool(int $id): CloudVpcCidrPool
+ {
+ $pool = $id > 0 ? CloudVpcCidrPool::find($id) : null;
+ if ($pool === null) {
+ throw new CloudError('VPC 地址池不存在', 404);
+ }
+ return $pool;
+ }
+
+ private static function dto(CloudVpcCidrPool $pool, string $regionName,
+ int $used): array
+ {
+ $capacity = 1 << ((int) $pool->allocation_prefix
+ - (int) $pool->prefix_length);
+ return [
+ 'id' => (int) $pool->id,
+ 'pool_key' => (string) $pool->pool_key,
+ 'region_id' => (int) $pool->region_id,
+ 'region_name' => $regionName,
+ 'name' => (string) $pool->name,
+ 'cidr' => (string) $pool->cidr,
+ 'prefix_length' => (int) $pool->prefix_length,
+ 'allocation_prefix' => (int) $pool->allocation_prefix,
+ 'state' => (string) $pool->state,
+ 'revision' => (int) $pool->revision,
+ 'used' => $used,
+ 'capacity' => $capacity,
+ 'available' => max(0, $capacity - $used),
+ 'address_locked' => $used > 0,
+ 'created_at' => (int) $pool->created_at,
+ 'updated_at' => (int) $pool->updated_at,
+ ];
+ }
+}
diff --git a/web/config/routes.php b/web/config/routes.php
index bb616094..2bbb6063 100644
--- a/web/config/routes.php
+++ b/web/config/routes.php
@@ -171,6 +171,7 @@ $app->group('/admin', function () {
$this->get('', 'App\Controllers\AdminController:index');
$this->get('/', 'App\Controllers\AdminController:index');
$this->get('/resources', 'App\Controllers\Admin\CloudPageController:resources');
+ $this->get('/vpc', 'App\Controllers\Admin\CloudPageController:vpc');
// Host network preparation (administrator only).
$this->get('/hosts', 'App\Controllers\Admin\CloudPageController:hosts');
$this->get('/agents', 'App\Controllers\Admin\CloudPageController:agents');
diff --git a/web/config/routes_cloud.php b/web/config/routes_cloud.php
index 85aac669..4015dc2f 100644
--- a/web/config/routes_cloud.php
+++ b/web/config/routes_cloud.php
@@ -139,6 +139,10 @@ $app->group('/api/v1', function () {
$this->get('/admin/billing/payments',
'App\Controllers\CloudAdminBillingController:payments');
$this->get('/admin/resources', 'App\Controllers\CloudAdminResourceController:resources');
+ $this->map(['GET', 'POST'], '/admin/vpc-cidr-pools',
+ 'App\Controllers\CloudAdminVpcController:pools');
+ $this->map(['GET', 'PATCH'], '/admin/vpc-cidr-pools/{id:[1-9][0-9]*}',
+ 'App\Controllers\CloudAdminVpcController:pools');
$this->get('/admin/platform-test-policy',
'App\Controllers\CloudAdminPlatformPolicyController:index');
$this->post('/admin/platform-test-policy/rules',
diff --git a/web/public/cloud/admin-vpc.js b/web/public/cloud/admin-vpc.js
new file mode 100644
index 00000000..5971c2a9
--- /dev/null
+++ b/web/public/cloud/admin-vpc.js
@@ -0,0 +1,66 @@
+const root = document.querySelector('[data-admin-vpc]');
+const byId = id => document.getElementById(id);
+const canManage = root?.dataset.canManage === '1';
+const state = { csrf: window.ADMIN_CSRF_TOKEN || '', regions: [], items: [], busy: false, requestKey: '' };
+
+function notice(message, error = false) {
+ const box = byId('admin-vpc-status'); box.textContent = message; box.className = `alert ${error ? 'alert-danger' : 'alert-info'} cloud-admin-notice`;
+}
+function errorMessage(payload, status) { return String(payload?.detail || payload?.error || `请求失败 (${status})`); }
+async function api(path, options = {}) {
+ const method = (options.method || 'GET').toUpperCase(); const headers = new Headers({ Accept: 'application/json' });
+ if (!['GET', 'HEAD'].includes(method)) { headers.set('Content-Type', 'application/json'); headers.set('X-CSRF-Token', state.csrf); }
+ const response = await fetch(path, { method, headers, credentials: 'same-origin', cache: 'no-store', body: options.body === undefined ? undefined : JSON.stringify(options.body) });
+ let payload; try { payload = await response.json(); } catch { throw new Error('服务返回非 JSON 响应。'); }
+ if (response.status === 401) { location.assign('/auth/login'); throw new Error('登录已过期。'); }
+ if (!response.ok) throw new Error(errorMessage(payload, response.status)); return payload;
+}
+function newRequestKey() {
+ if (!globalThis.crypto?.getRandomValues) throw new Error('浏览器无法生成安全创建标识,请升级浏览器。');
+ const bytes = new Uint8Array(16); globalThis.crypto.getRandomValues(bytes); return Array.from(bytes, value => value.toString(16).padStart(2, '0')).join('');
+}
+function renderRegions(selected = '') {
+ const select = byId('admin-vpc-pool-region'); select.replaceChildren();
+ const empty = document.createElement('option'); empty.value = ''; empty.textContent = state.regions.length ? '请选择地区' : '请先创建地区'; select.append(empty);
+ for (const region of state.regions) { const option = document.createElement('option'); option.value = String(region.id); option.textContent = `${region.name} · ${region.country_code}${region.status === 'active' ? '' : '(停用)'}`; option.selected = String(region.id) === String(selected); select.append(option); }
+}
+function lockAddressFields(editing) {
+ byId('admin-vpc-pool-region').disabled = !canManage || editing;
+ byId('admin-vpc-pool-cidr').disabled = !canManage || (editing && byId('admin-vpc-pool-cidr').dataset.locked === '1');
+ byId('admin-vpc-pool-prefix').disabled = !canManage || (editing && byId('admin-vpc-pool-prefix').dataset.locked === '1');
+}
+function resetForm() {
+ byId('admin-vpc-pool-form').reset(); byId('admin-vpc-pool-id').value = ''; byId('admin-vpc-pool-revision').value = '0'; byId('admin-vpc-pool-prefix').value = '24'; byId('admin-vpc-pool-state').value = 'active'; byId('admin-vpc-pool-title').textContent = '添加 VPC 地址池'; byId('admin-vpc-pool-capacity').textContent = '保存后显示可分配子网数量。'; delete byId('admin-vpc-pool-cidr').dataset.locked; delete byId('admin-vpc-pool-prefix').dataset.locked; state.requestKey = newRequestKey(); renderRegions(); lockAddressFields(false);
+}
+function editPool(pool) {
+ byId('admin-vpc-pool-id').value = pool.id; byId('admin-vpc-pool-revision').value = pool.revision; byId('admin-vpc-pool-title').textContent = `编辑地址池 · ${pool.name}`; renderRegions(pool.region_id); byId('admin-vpc-pool-name').value = pool.name; byId('admin-vpc-pool-cidr').value = pool.cidr; byId('admin-vpc-pool-prefix').value = pool.allocation_prefix; byId('admin-vpc-pool-state').value = pool.state; byId('admin-vpc-pool-cidr').dataset.locked = pool.address_locked ? '1' : '0'; byId('admin-vpc-pool-prefix').dataset.locked = pool.address_locked ? '1' : '0'; byId('admin-vpc-pool-capacity').textContent = `${pool.used} 个已占用 · ${pool.available} 个可用 · 共 ${pool.capacity} 个 /${pool.allocation_prefix} 子网${pool.address_locked ? ';地址边界已锁定' : ''}`; lockAddressFields(true);
+}
+function renderList() {
+ const list = byId('admin-vpc-pool-list'); list.replaceChildren();
+ if (!state.items.length) { const empty = document.createElement('p'); empty.className = 'text-muted'; empty.textContent = '暂无 VPC 地址池。'; list.append(empty); return; }
+ for (const pool of state.items) { const button = document.createElement('button'); button.type = 'button'; button.className = 'btn btn-light cloud-admin-host'; const strong = document.createElement('strong'); strong.textContent = pool.name; const detail = document.createElement('div'); detail.className = 'text-muted'; detail.textContent = `${pool.region_name} · ${pool.cidr} → /${pool.allocation_prefix} · ${pool.used}/${pool.capacity} · ${pool.state === 'active' ? '启用' : '停用'}`; button.append(strong, detail); button.addEventListener('click', () => editPool(pool)); list.append(button); }
+}
+async function load() {
+ const payload = await api('/api/v1/admin/vpc-cidr-pools'); state.regions = Array.isArray(payload.regions) ? payload.regions : []; state.items = Array.isArray(payload.items) ? payload.items : []; renderList();
+}
+function requestBody() {
+ const regionId = Number(byId('admin-vpc-pool-region').value); const prefix = Number(byId('admin-vpc-pool-prefix').value); const id = byId('admin-vpc-pool-id').value;
+ if (!Number.isInteger(regionId) || regionId < 1) throw new Error('请选择所属地区。');
+ if (!Number.isInteger(prefix) || prefix < 8 || prefix > 28) throw new Error('VPC 分配前缀必须是 8 至 28 的整数。');
+ const result = { region_id: regionId, name: byId('admin-vpc-pool-name').value.trim(), cidr: byId('admin-vpc-pool-cidr').value.trim(), allocation_prefix: prefix, state: byId('admin-vpc-pool-state').value };
+ if (id) result.expected_revision = Number(byId('admin-vpc-pool-revision').value); else result.request_key = state.requestKey;
+ return result;
+}
+async function save(event) {
+ event.preventDefault(); if (!canManage || state.busy) return; let payload; try { payload = requestBody(); } catch (error) { notice(error.message, true); return; }
+ const id = byId('admin-vpc-pool-id').value; state.busy = true; byId('admin-vpc-pool-save').disabled = true; notice('正在保存 VPC 地址池…');
+ try { const result = await api(`/api/v1/admin/vpc-cidr-pools${id ? '/' + encodeURIComponent(id) : ''}`, { method: id ? 'PATCH' : 'POST', body: payload }); await load(); editPool(result.item); notice(id ? 'VPC 地址池已更新。' : 'VPC 地址池已创建。'); }
+ catch (error) { try { await load(); } catch {} notice(error.message, true); }
+ finally { state.busy = false; byId('admin-vpc-pool-save').disabled = !canManage; }
+}
+async function initialize() {
+ if (!root) return;
+ try { const session = await api('/api/v1/session'); state.csrf = session.csrf_token || state.csrf; if (!session.admin || !state.csrf) throw new Error('管理员会话无效,请重新登录。'); await load(); resetForm(); byId('admin-vpc-pool-new').addEventListener('click', resetForm); byId('admin-vpc-pool-form').addEventListener('submit', save); notice('VPC 地址池目录已读取。功能开关仍保持关闭。'); }
+ catch (error) { notice(error.message, true); }
+}
+void initialize();
diff --git a/web/resources/views/venix/admin/cloud/vpc.tpl b/web/resources/views/venix/admin/cloud/vpc.tpl
new file mode 100644
index 00000000..8bd3b9b1
--- /dev/null
+++ b/web/resources/views/venix/admin/cloud/vpc.tpl
@@ -0,0 +1,59 @@
+{assign var=admin_title value='VPC 私网'}
+{assign var=admin_active value='vpc'}
+{include file='admin/static_header.tpl'}
+
+
+ Private Networking 按地区维护互不重叠的 RFC1918 地址池;每个用户 VPC 从所属地区取得一个独立子网。 正在加载…VPC 私网
+