From 0837fd71c6e68a3ac2b8f03a440ce5f1f5d7dc8e Mon Sep 17 00:00:00 2001 From: codex Date: Wed, 4 Nov 2026 01:47:42 +0800 Subject: [PATCH] fix: define tenant IP changes as IPv4 only --- web/app/Controllers/CloudApiController.php | 18 +++++++---- .../Cloud/CloudBillingPresentationCatalog.php | 2 +- .../Cloud/CloudInvoicePresentation.php | 15 +++++---- .../Cloud/CloudIpChangeBillingService.php | 20 ++++++------ .../CloudNetworkChangeOptionsService.php | 15 +++++---- .../CloudPublicNetworkTransitionWorkflow.php | 2 +- web/public/cloud/detail.js | 32 +++++++++---------- .../views/venix/cloud/pages/detail.tpl | 12 +++---- .../views/venix/cloud/pages/invoices.tpl | 2 +- .../CloudBillingPresentationCatalogTest.php | 2 ++ .../CloudInvoicePresentationModuleTest.php | 7 ++-- web/tests/CloudInvoicePresentationTest.php | 5 +-- .../CloudNetworkChangeOptionsServiceTest.php | 5 +-- web/tests/CloudPaidIpChangeContractTest.php | 14 +++++--- 14 files changed, 86 insertions(+), 65 deletions(-) diff --git a/web/app/Controllers/CloudApiController.php b/web/app/Controllers/CloudApiController.php index 9e633383..5b2d9569 100644 --- a/web/app/Controllers/CloudApiController.php +++ b/web/app/Controllers/CloudApiController.php @@ -1466,7 +1466,7 @@ final class CloudApiController $actor = $this->actor($request); $instance = CloudAccess::instance($args['id'], $actor); if ((int) $instance->user_id !== (int) $actor['user_id']) { - throw new CloudError('只能为自己持有的实例更换 IP', 403); + throw new CloudError('只能为自己持有的实例更换公网 IPv4', 403); } $this->requireService($instance, $actor); $options = CloudNetworkChangeOptionsService::tenantQuote($instance); @@ -1485,13 +1485,14 @@ final class CloudApiController && $enabled && $balanceSufficient; $message = (string) ($options['message'] ?? ''); if (!$enabled) { - $message = '管理员尚未启用付费更换 IP。'; + $message = '管理员尚未启用付费更换公网 IPv4。'; } elseif (!$balanceSufficient) { $message = '账户余额不足,请先充值。'; } return $this->json($response, [ 'instance_id' => (string) $instance->id, + 'address_family' => (int) ($options['address_family'] ?? 0), 'active_job' => $options['active_job'] ?? null, 'configuration_state' => (string) ($options['configuration_state'] ?? 'invalid'), @@ -1564,16 +1565,21 @@ final class CloudApiController { $actor = $this->actor($request); if (!isset($actor['request_idempotency_key'])) { - throw new CloudError('付费更换 IP 必须提供请求幂等标识', 400); + throw new CloudError( + '付费更换公网 IPv4 必须提供请求幂等标识', + 400 + ); } $instance = CloudAccess::instance($args['id'], $actor, true); if ((int) $instance->user_id !== (int) $actor['user_id']) { - throw new CloudError('只能为自己持有的实例更换 IP', 403); + throw new CloudError('只能为自己持有的实例更换公网 IPv4', 403); } $body = CloudInput::object($request); CloudInput::only($body, ['expected_amount', 'confirm_restart']); if (($body['confirm_restart'] ?? false) !== true) { - throw new CloudError('更换公网 IP 前必须确认虚拟机会短暂重启'); + throw new CloudError( + '更换公网 IPv4 前必须确认虚拟机会短暂重启' + ); } try { $expectedAmount = CloudIpChangeBillingService::normalizePrice( @@ -1591,7 +1597,7 @@ final class CloudApiController $expectedAmount ); } catch (\InvalidArgumentException) { - throw new CloudError('付费更换 IP 请求无效', 422); + throw new CloudError('付费更换公网 IPv4 请求无效', 422); } $dispatch = CloudPublicNetworkTransitionWorkflow::begin($command); return $this->json($response, [ diff --git a/web/app/Services/Cloud/CloudBillingPresentationCatalog.php b/web/app/Services/Cloud/CloudBillingPresentationCatalog.php index 1cd98fe0..ce427474 100644 --- a/web/app/Services/Cloud/CloudBillingPresentationCatalog.php +++ b/web/app/Services/Cloud/CloudBillingPresentationCatalog.php @@ -7,7 +7,7 @@ final class CloudBillingPresentationCatalog { private const PURPOSES = [ 'provision' => '实例开通', 'renewal' => '实例续费', - 'upgrade' => '实例升级', 'ip_change' => '更换公网 IP', + 'upgrade' => '实例升级', 'ip_change' => '更换公网 IPv4', 'backup_addon' => '自动备份开通', 'recharge' => '余额充值', ]; private const PROVIDERS = [ diff --git a/web/app/Services/Cloud/CloudInvoicePresentation.php b/web/app/Services/Cloud/CloudInvoicePresentation.php index 8791e0a8..bd7f53a4 100644 --- a/web/app/Services/Cloud/CloudInvoicePresentation.php +++ b/web/app/Services/Cloud/CloudInvoicePresentation.php @@ -482,7 +482,7 @@ final class CloudInvoicePresentation if ($message === '') { $message = $label !== '' ? $label : match ($purpose) { 'upgrade' => '等待升级', - 'ip_change' => '等待更换公网 IP', + 'ip_change' => '等待更换公网 IPv4', default => '等待创建', }; } @@ -491,12 +491,12 @@ final class CloudInvoicePresentation 'show_fulfillment' => $show, 'fulfillment_title' => match ($purpose) { 'upgrade' => '实例升级进度', - 'ip_change' => '公网 IP 更换进度', + 'ip_change' => '公网 IPv4 更换进度', default => '实例创建进度', }, 'fulfillment_description' => match ($purpose) { 'upgrade' => '账单扣款后,系统会在后台升级实例;关闭页面不会取消任务。', - 'ip_change' => '账单扣款后,系统会在后台更换公网 IP;关闭页面不会取消任务。', + 'ip_change' => '账单扣款后,系统会在后台更换公网 IPv4;公网 IPv6 保持不变,关闭页面不会取消任务。', default => '账单付款后,系统会在后台创建实例;关闭页面不会取消任务。', }, 'fulfillment_message' => $message, @@ -567,14 +567,15 @@ final class CloudInvoicePresentation if ($purpose === 'ip_change') { return match ($taskStatus) { 'succeeded', 'completed' => [ - 'message' => '公网 IP 已更换。', 'tone' => 'success', + 'message' => '公网 IPv4 已更换;公网 IPv6 保持不变。', + 'tone' => 'success', ], 'review' => [ - 'message' => '公网 IP 更换结果待人工核对,请勿重复提交。', + 'message' => '公网 IPv4 更换结果待人工核对,请勿重复提交。', 'tone' => 'error', ], 'failed', 'cancelled', 'canceled' => [ - 'message' => '公网 IP 更换未完成,请在账单详情查看任务状态。', + 'message' => '公网 IPv4 更换未完成,请在账单详情查看任务状态。', 'tone' => 'error', ], default => null, @@ -701,7 +702,7 @@ final class CloudInvoicePresentation $specification[] = [ 'label' => '服务内容', 'value' => $purpose === 'ip_change' - ? '更换公网 IP' : '自动备份开通', + ? '更换公网 IPv4' : '自动备份开通', ]; } return $specification; diff --git a/web/app/Services/Cloud/CloudIpChangeBillingService.php b/web/app/Services/Cloud/CloudIpChangeBillingService.php index d17a7146..77da0839 100644 --- a/web/app/Services/Cloud/CloudIpChangeBillingService.php +++ b/web/app/Services/Cloud/CloudIpChangeBillingService.php @@ -9,7 +9,7 @@ use App\Models\Configurations; use App\Models\CloudPublicNetworkTransition; use App\Models\User; -/** Balance pricing and exactly-once settlement for tenant IP changes. */ +/** Balance pricing and exactly-once settlement for tenant IPv4 changes. */ final class CloudIpChangeBillingService { public const PRICE_CONFIG_KEY = 'cloud_ip_change_price'; @@ -28,7 +28,7 @@ final class CloudIpChangeBillingService ); } catch (\InvalidArgumentException $error) { throw new CloudError( - '付费更换 IP 的后台价格配置无效,请联系管理员', + '付费更换公网 IPv4 的后台价格配置无效,请联系管理员', 503 ); } @@ -43,7 +43,7 @@ final class CloudIpChangeBillingService { if (is_array($value) || is_object($value)) { throw new \InvalidArgumentException( - '更换 IP 价格必须是十进制金额' + '更换公网 IPv4 价格必须是十进制金额' ); } $raw = trim((string) $value); @@ -51,14 +51,14 @@ final class CloudIpChangeBillingService $cents = CloudMoney::cents($raw); } catch (CloudError $error) { throw new \InvalidArgumentException( - '更换 IP 价格格式无效,仅支持最多两位小数', + '更换公网 IPv4 价格格式无效,仅支持最多两位小数', 0, $error ); } if ($cents > 99999999) { throw new \InvalidArgumentException( - '更换 IP 价格不能超过 999999.99' + '更换公网 IPv4 价格不能超过 999999.99' ); } return CloudMoney::decimal($cents); @@ -131,7 +131,7 @@ final class CloudIpChangeBillingService || (int) $job->instance_id !== (int) $instance->id || (int) $instance->user_id !== (int) $user->id || $priceCents < 1) { - throw new CloudError('付费更换 IP 的账单关联无效', 409); + throw new CloudError('付费更换公网 IPv4 的账单关联无效', 409); } $balance = CloudMoney::cents((string) $user->money); if ($balance < $priceCents) { @@ -177,7 +177,7 @@ final class CloudIpChangeBillingService ): void { if ($effectResolution !== 'not_applied') { throw new CloudError( - '更换 IP 只有明确确认未生效时才允许退款', + '更换公网 IPv4 只有明确确认未生效时才允许退款', 409 ); } @@ -186,7 +186,7 @@ final class CloudIpChangeBillingService } if ($user === null || (int) $user->id !== (int) $job->user_id || (string) $job->kind !== 'network_change') { - throw new CloudError('更换 IP 退款账户关联无效', 409); + throw new CloudError('更换公网 IPv4 退款账户关联无效', 409); } $invoice = CloudInvoice::where('id', (int) $job->invoice_id) ->lockForUpdate()->first(); @@ -199,7 +199,7 @@ final class CloudIpChangeBillingService 'network-change-' . (int) $job->id, (string) $invoice->payment_reference )) { - throw new CloudError('更换 IP 退款账单关联不一致', 409); + throw new CloudError('更换公网 IPv4 退款账单关联不一致', 409); } if ((string) $invoice->status === 'refunded') { return; @@ -207,7 +207,7 @@ final class CloudIpChangeBillingService if ((string) $invoice->status !== 'paid' || (string) $invoice->paid_via !== 'balance' || $invoice->paid_at === null) { - throw new CloudError('更换 IP 退款账单状态无效', 409); + throw new CloudError('更换公网 IPv4 退款账单状态无效', 409); } $amount = CloudMoney::cents((string) $invoice->amount); $balance = CloudMoney::cents((string) $user->money); diff --git a/web/app/Services/Cloud/CloudNetworkChangeOptionsService.php b/web/app/Services/Cloud/CloudNetworkChangeOptionsService.php index accc8169..42713101 100644 --- a/web/app/Services/Cloud/CloudNetworkChangeOptionsService.php +++ b/web/app/Services/Cloud/CloudNetworkChangeOptionsService.php @@ -29,10 +29,11 @@ final class CloudNetworkChangeOptionsService $state = $catalog['malformed'] ? 'invalid' : ($hasAutomaticIpv4 ? 'ready' : 'not_configured'); - $availability = self::availability($instance, $state); + $availability = self::availability($instance, $state, true); return [ 'instance_id' => (string) $instance->id, + 'address_family' => 4, 'configuration_state' => $state, 'active_job' => $availability['active_job'], 'can_change' => $availability['can_change'], @@ -379,17 +380,19 @@ final class CloudNetworkChangeOptionsService } private static function availability(CloudInstance $instance, - string $configurationState): array + string $configurationState, bool $tenant = false): array { $decision = CloudPublicNetworkMutationAdmission::inspect($instance); $canChange = $decision->allowed() && $configurationState === 'ready'; $message = match (true) { - $configurationState === 'invalid' => - '面板公网地址池配置异常,请先修复双栈地址账本。', + $configurationState === 'invalid' => $tenant + ? '面板公网 IPv4 地址池配置异常,请联系管理员。' + : '面板公网地址池配置异常,请先修复双栈地址账本。', !$decision->allowed() => $decision->message, - $configurationState !== 'ready' => - '当前公网地址池没有可用目标地址。', + $configurationState !== 'ready' => $tenant + ? '当前公网 IPv4 地址池没有可用目标地址。' + : '当前公网地址池没有可用目标地址。', default => '', }; return [ diff --git a/web/app/Services/Cloud/CloudPublicNetworkTransitionWorkflow.php b/web/app/Services/Cloud/CloudPublicNetworkTransitionWorkflow.php index 31c08e55..770f09d0 100644 --- a/web/app/Services/Cloud/CloudPublicNetworkTransitionWorkflow.php +++ b/web/app/Services/Cloud/CloudPublicNetworkTransitionWorkflow.php @@ -396,7 +396,7 @@ final class CloudPublicNetworkTransitionWorkflow && $command->priceCents() !== CloudIpChangeBillingService::priceCents()) { throw new CloudError( - '付费更换 IP 的价格已发生变化,请刷新后重试', + '付费更换公网 IPv4 的价格已发生变化,请刷新后重试', 409 ); } diff --git a/web/public/cloud/detail.js b/web/public/cloud/detail.js index cf2f9385..b132333e 100644 --- a/web/public/cloud/detail.js +++ b/web/public/cloud/detail.js @@ -406,7 +406,7 @@ function bindDetailTabs(vmId) { const INSTANCE_SETTING_LABELS = Object.freeze({ hostname: '主机名', password: 'root 密码', ssh_key: 'SSH 公钥', - ip_change: '公网 IP', upgrade: '实例升级', automatic_backup: '自动备份', firewall: '防火墙', + ip_change: '公网 IPv4', upgrade: '实例升级', automatic_backup: '自动备份', firewall: '防火墙', snapshots: '在线快照', rescue: '紧急救援', }); const INSTANCE_SETTING_TABS = Object.freeze([ @@ -1925,8 +1925,8 @@ async function loadIpChangeQuote(vmId, force = false) { try { const payload = await api(`/api/v1/vms/${encodeURIComponent(vmId)}/network-options`); if (!payload || typeof payload !== 'object' || Array.isArray(payload) - || typeof payload.price !== 'string') { - throw new Error('更换 IP 配置响应无效。'); + || payload.address_family !== 4 || typeof payload.price !== 'string') { + throw new Error('更换公网 IPv4 配置响应无效。'); } ipChangePayload = payload; ipChangeLoadedVmId = String(vmId); @@ -1937,14 +1937,14 @@ async function loadIpChangeQuote(vmId, force = false) { } else { ipChangeStatus(message || (payload.active_job ? '实例已有任务正在执行,请等待完成后刷新。' - : '当前暂不能更换公网 IP。'), 'warning'); + : '当前暂不能更换公网 IPv4。'), 'warning'); } return payload; } catch (error) { ipChangePayload = null; ipChangeLoadedVmId = null; renderIpChangeQuote({ price: '0.00' }); - ipChangeStatus(error.message || '更换 IP 配置加载失败,请稍后重试。', 'error'); + ipChangeStatus(error.message || '更换公网 IPv4 配置加载失败,请稍后重试。', 'error'); throw error; } finally { ipChangeRequest = null; @@ -1972,22 +1972,22 @@ function clearPendingIpChangeKey(vmId) { } function pollIpChangeJob(vmId, receipt) { - return watchTask(receipt, '更换公网 IP', { - onUpdate: () => ipChangeStatus('更换公网 IP 任务正在后台执行;费用已扣除,请勿重复提交。'), + return watchTask(receipt, '更换公网 IPv4', { + onUpdate: () => ipChangeStatus('更换公网 IPv4 任务正在后台执行;费用已扣除,请勿重复提交。'), onTerminal: task => { void (async () => { await refreshMetadataAndStatus(vmId, false, true, true); await loadIpChangeQuote(vmId, true).catch(() => null); if (taskSucceededWithoutRecovery(task)) { - ipChangeStatus('公网 IP 已更换,实例详情已刷新。', 'success'); - toast('公网 IP 更换完成。', 'success'); + ipChangeStatus('公网 IPv4 已更换,公网 IPv6 保持不变;实例详情已刷新。', 'success'); + toast('公网 IPv4 更换完成。', 'success'); return; } - const base = taskFailureMessage(task, '更换公网 IP 任务失败。'); + const base = taskFailureMessage(task, '更换公网 IPv4 任务失败。'); if (task.effect_resolution === 'not_applied') { ipChangeStatus(`${base} 系统已确认未生效,本次费用已自动退回余额。`, 'error'); } else if (task.effect_resolution === 'applied') { - ipChangeStatus(`${base} 系统已确认新 IP 实际生效,本次费用不会退回。`, 'warning'); + ipChangeStatus(`${base} 系统已确认新公网 IPv4 实际生效,本次费用不会退回。`, 'warning'); } else { ipChangeStatus(`${base} 操作结果仍在核对,本次费用暂不退回,请勿再次提交。`, 'warning'); } @@ -2021,8 +2021,8 @@ function bindIpChange(vmId) { return; } const confirmed = await requestUserConfirmation({ - title: '确认付费更换公网 IP', - message: `系统将自动分配一个可用公网 IP,并从余额扣除 ${ipChangeMoney(expectedAmount, ipChangePayload.currency)}。实例可能短暂重启。`, + title: '确认付费更换公网 IPv4', + message: `系统将自动分配一个可用公网 IPv4,并从余额扣除 ${ipChangeMoney(expectedAmount, ipChangePayload.currency)}。公网 IPv6 保持不变,实例可能短暂重启。`, confirmText: '确认扣费并更换', tone: 'danger', }); @@ -2052,13 +2052,13 @@ function bindIpChange(vmId) { }; renderIpChangeQuote(ipChangePayload); ipChangeStatus(`任务已接收,已扣除 ${ipChangeMoney(expectedAmount, ipChangePayload.currency)};正在等待宿主机确认结果。`); - toast('付费更换 IP 任务已接收。', 'success'); + toast('付费更换公网 IPv4 任务已接收。', 'success'); if (!pollIpChangeJob(vmId, receipt)) { ipChangeStatus('任务已接收,但自动观察协议无效。请勿重复提交,并在任务中心核查结果。', 'error'); } } catch (error) { const uncertain = firewallTransportUncertain(error); - const message = error.message || '更换 IP 提交失败,请稍后重试。'; + const message = error.message || '更换公网 IPv4 提交失败,请稍后重试。'; if (uncertain) { ipChangeError(message); ipChangeStatus('请求结果暂时无法确认;页面已保留同一请求标识。请勿发起其他更换,重新点击提交只会核对原请求。', 'warning'); @@ -2066,7 +2066,7 @@ function bindIpChange(vmId) { clearPendingIpChangeKey(vmId); await loadIpChangeQuote(vmId, true).catch(() => null); ipChangeError(message); - ipChangeStatus('更换 IP 未提交,请按提示修正后重试。', 'error'); + ipChangeStatus('更换公网 IPv4 未提交,请按提示修正后重试。', 'error'); } } finally { ipChangeSubmitting = false; diff --git a/web/resources/views/venix/cloud/pages/detail.tpl b/web/resources/views/venix/cloud/pages/detail.tpl index 43f68a1c..9358e13b 100644 --- a/web/resources/views/venix/cloud/pages/detail.tpl +++ b/web/resources/views/venix/cloud/pages/detail.tpl @@ -54,7 +54,7 @@ - + @@ -95,14 +95,14 @@