Support provider subnets and separate guest IPv4 prefix

main
admin 2 months ago
parent 70f2349129
commit c878696e6a

@ -89,9 +89,13 @@ SELECT id, username, email FROM users WHERE id = 1;
面板的“创建虚拟机”现在有独立的“公网云镜像”方式。先在宿主机设置中选择已工作的 Linux 网桥,例如 `bridge:br0`;打开创建弹窗的“导入新的云镜像模板”,填写 [AlmaLinux 10 官方 Generic Cloud qcow2 镜像](https://wiki.almalinux.org/cloud/Generic-cloud)在 `https://repo.almalinux.org/almalinux/10/cloud/x86_64/images/` 下的下载地址、发布方 SHA-256 和模板显示名。地址必须是官方 HTTPS 直链,面板不会跟随重定向。面板服务器 A 需要能访问该地址。导入在后台运行,一次只允许一个任务,面板下载、校验 SHA-256 后通过当前 libvirt SSH 连接把模板镜像上传到 B 的磁盘池;无需登录 B 手工复制。模板目录记录加密保存在 MariaDB,绑定当前宿主机身份和磁盘池。不要在导入期间关闭面板进程;若进程异常退出,运行中的导入任务记录不会恢复,可能需要检查项目 `data/` 中的 `.cloud-image-*` 临时文件和 B 磁盘池中 `panel-template-*` 卷,不要未经核对就删除。
选择模板后填写 VM 资源和 root 登录方式。如果宿主机已配置公网 `/24` 地址池,创建时会自动选择一个尚未使用的地址,并沿用宿主机设置的网关和 DNS;用户不能在创建请求中另填 IP、网关或 DNS 来绕过地址池。面板会跳过网络地址、广播地址、网关、保留地址、宿主机自身的字面 IPv4,以及 libvirt 可见虚拟机元数据中的已用 IP,并防止面板并发创建分到相同地址。地址池用尽时创建会报错,不会复用仍占用的地址;面板删除 VM 后会释放其地址占用记录。旧宿主机若未配置地址池,仍可按原方式手填已分配给你的**具体公网 IPv4/CIDR**(如 `/24` 或 `/28`)、同网段网关和 1–4 个 DNS 地址。填写 OpenSSH **公钥**时,只能用对应私钥登录 `root@<公网IP>`,密码输入会隐藏;不填公钥时,可以设置 12–128 字符的初始 root 密码,可用它从 VNC 和 SSH 登录;公钥和密码都不填时,面板会自动生成强初始 root 密码。客体登录密钥与连接宿主机的 root 私钥是两套独立凭据。面板无法探测面板及 libvirt 元数据之外的设备是否占用地址,也无法替服务商分配或路由公网 IP;**不要把未由服务商分配或路由给你的整个 `/24` 填入地址池**。当前版本只支持网关与 VM IP 同一子网的桥接 IPv4;服务商给出 `/32` 或异网段网关时不要强填,需另做路由网络模式。上游网络还必须允许 VM 使用额外 MAC,`br0` 本身不能保证公网可达。
选择模板后填写 VM 资源和 root 登录方式。宿主机公网地址池支持实际获配的 `/24` 至 `/30` 前缀。例如服务商分配 `123.123.123.112/28`、宿主机网关为 `123.123.123.113` 时,在宿主机设置填写该 `/28` 网段(填写 `123.123.123.113/28` 也会规范化为 `123.123.123.112/28`),网关填 `123.123.123.113`,DNS 按服务商要求填写。自动分配只考虑此 `/28` 的有效主机地址;扣除网关后是 `123.123.123.114` 至 `123.123.123.126`,还会跳过保留、已用及池内 DNS 地址。**不要填写 `123.123.123.114/24` 或 `123.123.123.0/24`,除非服务商确实分配了整个 `/24`。**设置页会预览输入前缀换算出的实际网段和主机地址范围。
若 DNS 本身位于该 `/24` 中,它的地址也会自动跳过。面板创建成功后会保留 IP 占用记录,只有通过面板删除 VM 才自动释放;如果直接在宿主机或其他工具中删除 VM,旧占用记录会保守保留,避免误将该 IP 分给新 VM,需核查真实占用情况后再处理。
“虚拟机网卡前缀”与可分配地址池是独立设置:留空时客体使用地址池前缀;如果现场确认服务商要求 VM 内配置 `/24`,而实际只获配上述 `/28`,这里填 `24`,新建 VM 将获得例如 `123.123.123.114/24`,但面板仍只会从 `123.123.123.112/28` 挑选地址。可填写的客体前缀范围为 `/24` 到地址池前缀,以确保整个地址池中的 IP 与网关处于同一客体子网。修改此项不会自动改写已创建 VM;云镜像重装会保留该 VM 首次创建时的网络前缀。若使用 `/28` 不通,其具体原因仍需结合宿主机路由、网桥及服务商网络确认;仅改变面板掩码无法替代上游放行或路由配置。
配置地址池后,创建时会自动选择一个尚未使用的地址,并沿用宿主机设置的网关和 DNS;用户不能在创建请求中另填 IP、网关或 DNS 来绕过地址池。面板会跳过网络地址、广播地址、网关、保留地址、宿主机自身的字面 IPv4,以及 libvirt 可见虚拟机元数据中的已用 IP,并防止面板并发创建分到相同地址。地址池用尽时创建会报错,不会复用仍占用的地址;面板删除 VM 后会释放其地址占用记录。旧宿主机若未配置地址池,仍可按原方式手填已分配给你的**具体公网 IPv4/CIDR**(如 `/24` 或 `/28`)、同网段网关和 1–4 个 DNS 地址。填写 OpenSSH **公钥**时,只能用对应私钥登录 `root@<公网IP>`,密码输入会隐藏;不填公钥时,可以设置 12–128 字符的初始 root 密码,可用它从 VNC 和 SSH 登录;公钥和密码都不填时,面板会自动生成强初始 root 密码。客体登录密钥与连接宿主机的 root 私钥是两套独立凭据。面板无法探测面板及 libvirt 元数据之外的设备是否占用地址,也无法替服务商分配或路由公网 IP。当前版本只支持网关与 VM IP 同一子网的桥接 IPv4;服务商给出 `/32` 或异网段网关时不要强填,需另做路由网络模式。上游网络还必须允许 VM 使用额外 MAC,`br0` 本身不能保证公网可达。
若 DNS 本身位于该地址池中,它的地址也会自动跳过。面板创建成功后会保留 IP 占用记录,只有通过面板删除 VM 才自动释放;如果直接在宿主机或其他工具中删除 VM,旧占用记录会保守保留,避免误将该 IP 分给新 VM,需核查真实占用情况后再处理。
每个已登录用户都可导入并命名自己的**单行 OpenSSH 公钥**:已审核账号从侧栏“公钥管理”进入,等待审核的账号可用“管理我的公钥”入口。可以粘贴 `.pub` 文件内容,或在浏览器选择本地 `.pub` 文件填入表单;面板只接受公钥,不要选择或粘贴私钥。列表只显示当前账户导入的公钥;管理员账户也不能把另一个用户的公钥当作自己的预设公钥使用。创建公网云镜像 VM 时可以选择已导入的公钥,不必重复粘贴;也可以继续临时手动填写公钥,或选择初始 root 密码。预设公钥、手动公钥与密码不能同时使用。未通过管理员审核的用户可以管理自己的公钥,但仍不能创建虚拟机。删除公钥只删除面板中的预设记录,**不会撤销已经写入现有虚拟机的 SSH 授权密钥**;要撤销现有 VM 的访问权限,需在客体系统中删除对应的 `authorized_keys` 条目或重装并更换登录密钥。

@ -190,7 +190,8 @@ function stopPolling() {
}
function clearAdminContent() {
for (const id of ["smtp-host", "smtp-username", "smtp-password", "smtp-from-address", "host-address", "host-region", "host-storage-pool", "host-iso-pool", "host-network", "host-public-pool-cidr", "host-public-gateway", "host-public-dns", "host-public-reserved"]) byId(id).value = "";
for (const id of ["smtp-host", "smtp-username", "smtp-password", "smtp-from-address", "host-address", "host-region", "host-storage-pool", "host-iso-pool", "host-network", "host-public-pool-cidr", "host-public-gateway", "host-public-vm-prefix", "host-public-dns", "host-public-reserved"]) byId(id).value = "";
refreshPublicPoolPreview();
byId("smtp-port").value = 465;
byId("smtp-tls-mode").value = "ssl";
byId("smtp-status").hidden = true;
@ -391,7 +392,9 @@ function showHostView(hostConfig = { configured: false }) {
byId("host-iso-pool").value = configured ? (hostConfig.iso_pool || "iso") : "iso";
byId("host-network").value = configured ? (hostConfig.default_network || "default") : "default";
byId("host-public-pool-cidr").value = configured ? (hostConfig.public_pool_cidr || "") : "";
refreshPublicPoolPreview();
byId("host-public-gateway").value = configured ? (hostConfig.public_gateway || "") : "";
byId("host-public-vm-prefix").value = configured && Number.isInteger(hostConfig.public_vm_prefix) ? String(hostConfig.public_vm_prefix) : "";
byId("host-public-dns").value = configured && Array.isArray(hostConfig.public_dns_servers) ? hostConfig.public_dns_servers.join(", ") : "";
byId("host-public-reserved").value = configured && Array.isArray(hostConfig.public_reserved_ips) ? hostConfig.public_reserved_ips.join("\n") : "";
text("host-key-required-mark", configured ? "" : "*");
@ -951,23 +954,29 @@ function hostPayload() {
}
const publicPoolCidr = byId("host-public-pool-cidr").value.trim();
const publicGateway = byId("host-public-gateway").value.trim();
const publicVmPrefixText = byId("host-public-vm-prefix").value.trim();
const publicVmPrefix = publicVmPrefixText ? Number(publicVmPrefixText) : null;
const publicDnsServers = byId("host-public-dns").value.split(/[\s,;]+/).filter(Boolean);
const publicReservedIps = [...new Set(byId("host-public-reserved").value.split(/[\s,;]+/).filter(Boolean))];
if (!publicPoolCidr && (publicGateway || publicDnsServers.length || publicReservedIps.length)) {
throw new Error("填写公网网关、DNS 或保留地址时,必须先填写允许分配的 /24 网段;如需关闭地址池,请将这四项全部清空。");
if (!publicPoolCidr && (publicGateway || publicVmPrefix !== null || publicDnsServers.length || publicReservedIps.length)) {
throw new Error("填写公网网关、虚拟机网卡前缀、DNS 或保留地址时,必须先填写服务商分配的公网网段;如需关闭地址池,请将这些项全部清空。");
}
if (publicPoolCidr) {
if (!validPublicPoolCidr(publicPoolCidr)) {
throw new Error("公网地址池必须是服务商实际分配给该宿主机的可路由 IPv4 /24 网段地址,不可填写单台主机 IP 或示例保留网段。");
const pool = parsePublicPool(publicPoolCidr);
if (!pool) {
throw new Error("公网地址池请填写服务商实际分配的 IPv4 网段(/24 至 /30),例如 123.123.123.112/28;/28 可填段内网关/前缀并自动换算,/24 必须填写网段起始地址。");
}
if (publicVmPrefix !== null && (!Number.isInteger(publicVmPrefix) || publicVmPrefix < 24 || publicVmPrefix > pool.prefix)) {
throw new Error(`虚拟机网卡前缀须介于 /24 和地址池前缀 /${pool.prefix} 之间;留空则使用 /${pool.prefix}。`);
}
if (!validPoolMember(publicGateway, publicPoolCidr)) {
throw new Error("公网地址池网关必须是该 /24 网段内有效的 IPv4 地址,且不能是网段或广播地址。");
throw new Error("公网地址池网关必须是该网段内有效的 IPv4 地址,且不能是网段或广播地址。");
}
if (publicDnsServers.length < 1 || publicDnsServers.length > 4 || !publicDnsServers.every((address) => ipv4Parts(address))) {
throw new Error("公网地址池需配置 1 至 4 个有效的 DNS IPv4 地址。");
}
if (!publicReservedIps.every((address) => validPoolMember(address, publicPoolCidr))) {
throw new Error("额外保留的 IP 必须是该 /24 网段内的有效主机地址,不能填写网段或广播地址。");
throw new Error("额外保留的 IP 必须是该网段内的有效主机地址,不能填写网段或广播地址。");
}
}
return {
@ -983,6 +992,7 @@ function hostPayload() {
default_network: byId("host-network").value.trim(),
public_pool_cidr: publicPoolCidr,
public_gateway: publicGateway,
public_vm_prefix: publicVmPrefix,
public_dns_servers: publicDnsServers,
public_reserved_ips: publicReservedIps,
};
@ -1856,6 +1866,8 @@ function updateCreateAuthMode() {
function updateCreateSource() {
const cloud = selectedCreateSource() === "cloud";
const publicPool = configuredPublicPool();
const guestPrefix = state.resources?.public_vm_prefix
?? state.hostConfig?.public_vm_prefix ?? parsePublicPool(publicPool)?.prefix;
const automaticPublicIp = cloud && Boolean(publicPool);
byId("vm-iso-field").hidden = cloud;
byId("vm-template-field").hidden = !cloud;
@ -1864,7 +1876,7 @@ function updateCreateSource() {
byId("create-cloud-tip").hidden = !cloud;
byId("vm-auto-public-pool").hidden = !automaticPublicIp;
text("vm-auto-public-pool-description", publicPool
? `创建时将从 ${publicPool} 中选择未使用的公网 IP,并自动配置宿主机保存的网关和 DNS。`
? `创建时仅从 ${publicPool} 中选择未使用的公网 IP${guestPrefix ? `,虚拟机网卡使用 /${guestPrefix}` : ""},并自动配置宿主机保存的网关和 DNS。`
: "创建时将从宿主机地址池中选择未使用的 IP,并自动写入网关和 DNS。");
byId("vm-manual-public-network").hidden = automaticPublicIp;
@ -2449,9 +2461,10 @@ byId("host-back-button").addEventListener("click", () => {
});
byId("host-address").addEventListener("input", resetFingerprint);
byId("host-port").addEventListener("input", resetFingerprint);
for (const id of ["host-region", "host-storage-pool", "host-iso-pool", "host-network", "host-public-pool-cidr", "host-public-gateway", "host-public-dns", "host-public-reserved"]) {
for (const id of ["host-region", "host-storage-pool", "host-iso-pool", "host-network", "host-public-pool-cidr", "host-public-gateway", "host-public-vm-prefix", "host-public-dns", "host-public-reserved"]) {
byId(id).addEventListener("input", markHostTestStale);
}
byId("host-public-pool-cidr").addEventListener("input", refreshPublicPoolPreview);
byId("host-private-key").addEventListener("input", () => {
if (state.uploadedPrivateKey) {
state.uploadedPrivateKey = "";
@ -2801,6 +2814,10 @@ function ipv4Number(parts) {
return parts.reduce((accumulator, part) => ((accumulator << 8) | part) >>> 0, 0);
}
function ipv4Text(number) {
return [number >>> 24, (number >>> 16) & 255, (number >>> 8) & 255, number & 255].join(".");
}
function validPublicIpCidr(value) {
const match = /^([^/]+)\/(\d{1,2})$/.exec(value);
if (!match) return false;
@ -2822,21 +2839,42 @@ function validPublicIpCidr(value) {
return true;
}
function validPublicPoolCidr(value) {
const match = /^([^/]+)\/24$/.exec(value);
function parsePublicPool(value) {
const match = /^([^/]+)\/(\d{1,2})$/.exec(value);
const parts = match && ipv4Parts(match[1]);
if (!parts || parts[3] !== 0) return false;
const prefix = match && Number(match[2]);
if (!parts || prefix < 24 || prefix > 30) return null;
if ((parts[0] === 192 && parts[1] === 0 && parts[2] === 2)
|| (parts[0] === 198 && parts[1] === 51 && parts[2] === 100)
|| (parts[0] === 203 && parts[1] === 0 && parts[2] === 113)) return false;
return validPublicIpCidr(`${parts[0]}.${parts[1]}.${parts[2]}.1/24`);
|| (parts[0] === 203 && parts[1] === 0 && parts[2] === 113)) return null;
const mask = (0xffffffff << (32 - prefix)) >>> 0;
const inputAddress = ipv4Number(parts);
const network = (inputAddress & mask) >>> 0;
if (prefix === 24 && inputAddress !== network) return null;
const broadcast = (network | (~mask >>> 0)) >>> 0;
if (!validPublicIpCidr(`${ipv4Text(network + 1)}/${prefix}`)
|| !validPublicIpCidr(`${ipv4Text(broadcast - 1)}/${prefix}`)) return null;
return { network, broadcast, prefix, cidr: `${ipv4Text(network)}/${prefix}` };
}
function refreshPublicPoolPreview() {
const value = byId("host-public-pool-cidr").value.trim();
const pool = parsePublicPool(value);
const preview = byId("host-public-pool-preview");
preview.hidden = !pool;
if (pool) {
preview.textContent = `实际网段:${pool.cidr};主机地址范围:${ipv4Text(pool.network + 1)}–${ipv4Text(pool.broadcast - 1)}。网关、保留和已用地址不会分配。`;
} else {
preview.textContent = "";
}
}
function validPoolMember(value, publicPoolCidr) {
const parts = ipv4Parts(value);
const network = ipv4Parts(publicPoolCidr.split("/")[0]);
return Boolean(parts && network && parts[0] === network[0] && parts[1] === network[1]
&& parts[2] === network[2] && parts[3] >= 1 && parts[3] <= 254);
const pool = parsePublicPool(publicPoolCidr);
if (!parts || !pool) return false;
const address = ipv4Number(parts);
return address > pool.network && address < pool.broadcast;
}
function validGatewayForCidr(gateway, publicIpCidr) {

@ -5,8 +5,8 @@
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#101d2c" />
<title>VirtPilot · 虚拟机管理</title>
<link rel="stylesheet" href="/static/styles.css?v=public-ip-pool-1" />
<script type="module" src="/static/app.js?v=public-ip-pool-1"></script>
<link rel="stylesheet" href="/static/styles.css?v=public-ip-pool-2" />
<script type="module" src="/static/app.js?v=public-ip-pool-2"></script>
</head>
<body>
<div id="boot-screen" class="boot-screen" role="status" aria-live="polite">
@ -165,10 +165,11 @@
<div class="field"><label for="host-iso-pool">ISO 目录或存储池名</label><input id="host-iso-pool" name="iso_pool" type="text" maxlength="4096" value="iso" required autocomplete="off" placeholder="例如 /home/iso 或 iso" /><small>可直接填写宿主机上的绝对路径,如 /home/iso;不是面板服务器的路径。</small></div>
<div class="field"><label for="host-network">默认虚拟网络或网桥</label><input id="host-network" name="default_network" type="text" maxlength="128" value="default" required autocomplete="off" placeholder="例如 default 或 bridge:br0" /><small>libvirt 虚拟网络填名称;宿主机现有 Linux 网桥 br0 填 bridge:br0。</small></div>
<div class="host-public-pool full" role="group" aria-labelledby="host-public-pool-title">
<div class="host-public-pool-heading"><strong id="host-public-pool-title">公网 IPv4 地址池</strong><small>仅用于云镜像虚拟机;配置后创建时由面板从该 /24 网段自动分配未占用的地址。请先确认这些公网 IP 已分配给该宿主机,并由上游网络允许虚拟机使用。</small></div>
<div class="host-public-pool-heading"><strong id="host-public-pool-title">公网 IPv4 地址池</strong><small>仅用于云镜像虚拟机;配置后面板只从填写的服务商获配网段自动分配地址。请按真实前缀填写,不要把仅获配的 /28 误填成 /24。</small></div>
<div class="form-grid">
<div class="field"><label for="host-public-pool-cidr">允许分配的 /24 网段</label><input id="host-public-pool-cidr" name="public_pool_cidr" type="text" maxlength="18" inputmode="decimal" autocomplete="off" placeholder="填写服务商分配的公网网段/24" /><small>填写真实可路由的网段地址,不是某台虚拟机的 IP;留空则继续手动指定公网 IP。</small></div>
<div class="field"><label for="host-public-gateway">网关 IPv4</label><input id="host-public-gateway" name="public_gateway" type="text" maxlength="15" inputmode="decimal" autocomplete="off" placeholder="填写该 /24 网段内的网关" /><small>网关不会被分配给虚拟机。</small></div>
<div class="field"><label for="host-public-pool-cidr">服务商分配的公网网段(/24 至 /30)</label><input id="host-public-pool-cidr" name="public_pool_cidr" type="text" maxlength="18" inputmode="decimal" autocomplete="off" placeholder="例如 123.123.123.112/28" /><small>可填网段或段内地址/前缀,例如 123.123.123.113/28 会保存为 123.123.123.112/28;留空则手动指定公网 IP。</small><small id="host-public-pool-preview" role="status" hidden></small></div>
<div class="field"><label for="host-public-gateway">网关 IPv4</label><input id="host-public-gateway" name="public_gateway" type="text" maxlength="15" inputmode="decimal" autocomplete="off" placeholder="例如 123.123.123.113" /><small>网关不会被分配给虚拟机。</small></div>
<div class="field"><label for="host-public-vm-prefix">虚拟机网卡前缀(可选)</label><input id="host-public-vm-prefix" name="public_vm_prefix" type="number" min="24" max="30" step="1" inputmode="numeric" autocomplete="off" placeholder="默认与地址池前缀相同" /><small>只影响虚拟机内的网络掩码,不扩大可分配 IP 范围。如果你的服务商要求客体使用 /24,而获配地址池是 /28,这里填 24。</small></div>
<div class="field full"><label for="host-public-dns">DNS IPv4</label><input id="host-public-dns" name="public_dns_servers" type="text" maxlength="128" autocomplete="off" placeholder="例如 1.1.1.1, 8.8.8.8" /><small>填写 1 至 4 个 DNS 地址,用逗号或空格分隔。</small></div>
<div class="field full"><label for="host-public-reserved">额外保留的公网 IP(可选)</label><textarea id="host-public-reserved" name="public_reserved_ips" rows="3" maxlength="4096" spellcheck="false" autocomplete="off" placeholder="已有其他用途的地址,每行一个;也可用逗号或空格分隔"></textarea><small>这些地址不会自动分配。请填写面板之外已占用的地址;若宿主机地址填的是域名,也请把宿主机自身的公网 IP 填在这里。</small></div>
</div>

@ -174,6 +174,7 @@ class FakeRemoteManager:
validate_public_pool(
profile.public_pool_cidr, profile.public_gateway,
profile.public_dns_servers, profile.public_reserved_ips, profile.host,
profile.public_vm_prefix,
)
self.calls.append(("stage", profile.host, profile.port, confirmed_fingerprint))
self.staged_profiles.append(profile)
@ -206,6 +207,7 @@ class FakeRemoteManager:
"public_gateway": getattr(staged.profile, "public_gateway", ""),
"public_dns_servers": list(getattr(staged.profile, "public_dns_servers", [])),
"public_reserved_ips": list(getattr(staged.profile, "public_reserved_ips", [])),
"public_vm_prefix": getattr(staged.profile, "public_vm_prefix", None),
"libvirt_uri": "qemu+ssh://root@vp-managed/system?no_tty=1&command=/fake/ssh-wrapper",
"ssh_target": "vp-managed", "ssh_config_file": "/fake/config",
"ssh_known_hosts_file": "/fake/known_hosts",
@ -469,7 +471,10 @@ def test_host_public_pool_round_trips_and_rejects_invalid_networks(web):
assert manager.current()[key] == body[key]
for changes in (
{"public_pool_cidr": "8.8.8.0/25"},
{"public_pool_cidr": "8.8.8.18/24"},
{"public_pool_cidr": "8.8.8.0/23"},
{"public_vm_prefix": 25},
{"public_vm_prefix": 23},
{"public_pool_cidr": "10.0.0.0/24", "public_gateway": "10.0.0.1"},
{"public_gateway": "9.9.9.1"},
{"public_dns_servers": ["invalid-dns"]},
@ -480,6 +485,26 @@ def test_host_public_pool_round_trips_and_rejects_invalid_networks(web):
assert manager.current()["public_pool_cidr"] == body["public_pool_cidr"]
def test_host_accepts_provider_28_and_gateway_address_form(web):
client, _, token, _service, manager, _ = web
csrf = setup_admin(client, token)
body = {
**HOST_BODY,
"host": "123.123.123.113",
"default_network": "bridge:br0",
"public_pool_cidr": "123.123.123.113/28",
"public_gateway": "123.123.123.113",
"public_dns_servers": ["1.1.1.1"],
"public_reserved_ips": ["123.123.123.114"],
"public_vm_prefix": 24,
}
headers = {"X-CSRF-Token": csrf}
assert client.post("/api/v1/host/test", json=body, headers=headers).status_code == 200
assert client.put("/api/v1/host", json=body, headers=headers).status_code == 200
assert manager.current()["public_pool_cidr"] == body["public_pool_cidr"]
assert manager.current()["public_vm_prefix"] == 24
def test_private_key_passphrase_reaches_host_profile_but_is_never_echoed(web):
client, _, token, _, manager, _ = web
csrf = setup_admin(client, token)

@ -33,6 +33,7 @@ def _user_data(seed: bytes) -> dict:
[
("8.8.8.18/24", "8.8.8.1"),
("8.8.8.18/28", "8.8.8.17"),
("123.123.123.114/24", "123.123.123.113"),
],
)
def test_public_network_accepts_24_and_28(address, gateway):

@ -199,9 +199,9 @@ def test_remote_host_is_rebuilt_from_database_without_durable_private_key(tmp_pa
host="host.example", port=22, private_key=PRIVATE_KEY,
host_key=scan["known_hosts_line"], storage_pool="/home/kvm",
iso_pool="/home/iso", default_network="bridge:br0", region="东京",
public_pool_cidr="1.2.3.0/24", public_gateway="1.2.3.1",
public_pool_cidr="1.2.3.1/28", public_gateway="1.2.3.1",
public_dns_servers=["1.1.1.1", "8.8.8.8"],
public_reserved_ips=["1.2.3.10"],
public_reserved_ips=["1.2.3.10"], public_vm_prefix=24,
)
staged = first.stage(profile, scan["fingerprint"])
assert first.test(staged)["libvirt"] == "available"
@ -219,7 +219,8 @@ def test_remote_host_is_rebuilt_from_database_without_durable_private_key(tmp_pa
assert second.current()["storage_pool"] == "/home/kvm"
assert second.current()["default_network"] == "bridge:br0"
assert second.current()["region"] == "东京"
assert second.current()["public_pool_cidr"] == "1.2.3.0/24"
assert second.current()["public_pool_cidr"] == "1.2.3.0/28"
assert second.current()["public_vm_prefix"] == 24
assert second.current()["public_gateway"] == "1.2.3.1"
assert second.current()["public_dns_servers"] == ["1.1.1.1", "8.8.8.8"]
assert second.current()["public_reserved_ips"] == ["1.2.3.10"]
@ -230,7 +231,7 @@ def test_remote_host_is_rebuilt_from_database_without_durable_private_key(tmp_pa
legacy = json.loads(store.records[("host", "current")])
legacy["metadata"].pop("region")
for key in ("public_pool_cidr", "public_gateway", "public_dns_servers", "public_reserved_ips"):
for key in ("public_pool_cidr", "public_gateway", "public_dns_servers", "public_reserved_ips", "public_vm_prefix"):
legacy["metadata"].pop(key)
store.records[("host", "current")] = json.dumps(legacy)
third = DatabaseRemoteHostManager(
@ -240,6 +241,7 @@ def test_remote_host_is_rebuilt_from_database_without_durable_private_key(tmp_pa
assert third.current()["public_pool_cidr"] == ""
assert third.current()["public_gateway"] == ""
assert third.current()["public_dns_servers"] == []
assert third.current()["public_vm_prefix"] is None
third.close()

@ -10,13 +10,15 @@ from virsh_panel.remote_host import RemoteHostError, validate_public_pool
_DEFAULT = object()
def validate(cidr="1.2.3.0/24", gateway="1.2.3.1", dns=_DEFAULT, reserved=_DEFAULT):
def validate(cidr="1.2.3.0/24", gateway="1.2.3.1", dns=_DEFAULT, reserved=_DEFAULT,
vm_prefix=None):
return validate_public_pool(
cidr,
gateway,
["1.1.1.1", "8.8.8.8"] if dns is _DEFAULT else dns,
[] if reserved is _DEFAULT else reserved,
"1.2.3.2",
vm_prefix,
)
@ -26,7 +28,10 @@ def test_unconfigured_public_pool_has_empty_defaults():
"public_gateway": "",
"public_dns_servers": [],
"public_reserved_ips": [],
"public_vm_prefix": None,
}
with pytest.raises(RemoteHostError):
validate_public_pool("", "", [], [], "host.example", 24)
def test_public_pool_normalizes_and_deduplicates_addresses():
@ -38,9 +43,37 @@ def test_public_pool_normalizes_and_deduplicates_addresses():
"public_gateway": "1.2.3.1",
"public_dns_servers": ["1.1.1.1", "8.8.8.8"],
"public_reserved_ips": ["1.2.3.10", "1.2.3.20"],
"public_vm_prefix": None,
}
@pytest.mark.parametrize("cidr", ["123.123.123.112/28", "123.123.123.113/28"])
def test_provider_28_is_normalized_without_widening_to_24(cidr):
configured = validate(
cidr, "123.123.123.113", ["1.1.1.1"],
["123.123.123.114", "123.123.123.126"],
)
assert configured["public_pool_cidr"] == "123.123.123.112/28"
assert configured["public_gateway"] == "123.123.123.113"
assert configured["public_reserved_ips"] == [
"123.123.123.114", "123.123.123.126",
]
def test_guest_mask_can_be_24_while_allocation_remains_28():
configured = validate("123.123.123.112/28", "123.123.123.113",
["1.1.1.1"], [], vm_prefix=24)
assert configured["public_pool_cidr"] == "123.123.123.112/28"
assert configured["public_vm_prefix"] == 24
@pytest.mark.parametrize("prefix", [23, 29, 31, True, "24"])
def test_guest_mask_rejects_values_outside_pool_bounds(prefix):
with pytest.raises(RemoteHostError):
validate("123.123.123.112/28", "123.123.123.113",
["1.1.1.1"], [], vm_prefix=prefix)
@pytest.mark.parametrize(
("cidr", "gateway", "dns", "reserved"),
[
@ -49,7 +82,11 @@ def test_public_pool_normalizes_and_deduplicates_addresses():
("1.2.3.0/24", "1.2.3.1", [], []),
("", "", [], ["1.2.3.10"]),
("1.2.3.1/24", "1.2.3.1", ["1.1.1.1"], []),
("1.2.3.0/25", "1.2.3.1", ["1.1.1.1"], []),
("1.2.2.0/23", "1.2.3.1", ["1.1.1.1"], []),
("1.2.3.0/31", "1.2.3.1", ["1.1.1.1"], []),
("123.123.123.112/28", "123.123.123.111", ["1.1.1.1"], []),
("123.123.123.112/28", "123.123.123.127", ["1.1.1.1"], []),
("123.123.123.112/28", "123.123.123.113", ["1.1.1.1"], ["123.123.123.128"]),
("10.0.0.0/24", "10.0.0.1", ["1.1.1.1"], []),
("203.0.113.0/24", "203.0.113.1", ["1.1.1.1"], []),
("2001:db8::/24", "2001:db8::1", ["1.1.1.1"], []),

@ -739,3 +739,72 @@ def test_host_public_pool_allocates_globally_and_preserves_tenant_isolation(
**automatic_body, "name": "uncertain-ip-protected",
}, headers=first_headers)
assert still_exhausted.status_code == 409
@pytest.mark.parametrize(("vm_prefix", "first_ip"), [
(None, "123.123.123.114/28"),
(24, "123.123.123.114/24"),
])
def test_provider_28_pool_allocates_only_inside_assigned_block(
monkeypatch, tmp_path, vm_prefix, first_ip,
):
store = MemoryStore()
service = _Service()
manager = _Manager()
manager.profile = {
**_Manager.profile,
"host": "123.123.123.113",
"public_pool_cidr": "123.123.123.112/28",
"public_gateway": "123.123.123.113",
"public_dns_servers": ["1.1.1.1"],
"public_reserved_ips": ["123.123.123.115"],
"public_vm_prefix": vm_prefix,
}
monkeypatch.setattr(app_module, "make_components", lambda directory, *_: (
DatabasePanelState(directory, store, store.users), manager,
DatabaseTemplateStore(store), store,
))
monkeypatch.setattr(app_module, "MetricsCollector", _NoMetrics)
settings = Settings(
data_dir=tmp_path / "data",
static_dir=Path(__file__).resolve().parents[1] / "static",
)
app = app_module.create_app(settings, service_factory=lambda _profile: service)
token = (settings.data_dir / "setup-token").read_text(encoding="ascii").strip()
automatic_body = {
key: value for key, value in CLOUD_VM.items()
if key not in {"public_ip_cidr", "gateway", "dns_servers"}
}
automatic_body["guest_ssh_public_key"] = GUEST_PUBLIC_KEY
with TestClient(app, base_url=ORIGIN) as admin:
assert admin.post("/api/v1/database/setup", json={
"host": "127.0.0.1", "port": 3306, "database": "virsh_panel",
"username": "panel", "password": "database-password", "setup_token": token,
}).status_code == 200
initial = admin.post("/api/v1/setup/admin", json={
"token": token, "username": "operator", "password": PASSWORD,
})
assert initial.status_code == 200
headers = {"X-CSRF-Token": initial.json()["csrf_token"]}
store.users.promote("operator")
DatabaseTemplateStore(store).add(template_scope(manager.profile), {
"id": "alma10-template", "name": "AlmaLinux 10",
"volume_name": "panel-template-alma10.qcow2",
})
created = admin.post("/api/v1/vms", json=automatic_body, headers=headers)
assert created.status_code == 201
assert service.cloud_specs[0][0]["public_ip_cidr"] == first_ip
assert service.cloud_specs[0][0]["gateway"] == "123.123.123.113"
# Exhaust the small provider allocation; never spill into the /24.
for address in ipaddress.ip_network("123.123.123.112/28").hosts():
ip = str(address)
if ip not in {"123.123.123.113", "123.123.123.114", "123.123.123.115"}:
service.vms[f"used-{ip}"] = {"uuid": f"used-{ip}",
"public_ip_cidr": f"{ip}/{vm_prefix or 28}"}
exhausted = admin.post("/api/v1/vms", json={
**automatic_body, "name": "no-provider-ip-left",
}, headers=headers)
assert exhausted.status_code == 409
assert len(service.cloud_specs) == 1

@ -130,6 +130,25 @@ def test_public_pool_survives_host_save_and_legacy_metadata_defaults_empty(tmp_p
assert manager.current()["public_reserved_ips"] == []
def test_provider_28_gateway_cidr_is_saved_as_canonical_network(tmp_path):
runner = Runner()
manager = make_manager(tmp_path, runner)
scanned = manager.scan("host.example", 2222)
profile = replace(
make_profile(scanned),
public_pool_cidr="123.123.123.113/28",
public_gateway="123.123.123.113",
public_dns_servers=["1.1.1.1"],
public_vm_prefix=24,
)
staged = manager.stage(profile, scanned["fingerprint"])
assert staged.metadata["public_pool_cidr"] == "123.123.123.112/28"
assert staged.metadata["public_vm_prefix"] == 24
manager.test(staged)
assert manager.commit(staged)["public_pool_cidr"] == "123.123.123.112/28"
assert manager.current()["public_vm_prefix"] == 24
class FakeAgent:
sequence = itertools.count()

@ -143,6 +143,7 @@ class HostBody(HostScanBody):
public_gateway: str = Field(default="", max_length=15)
public_dns_servers: list[str] = Field(default_factory=list, max_length=4)
public_reserved_ips: list[str] = Field(default_factory=list, max_length=254)
public_vm_prefix: int | None = Field(default=None, ge=24, le=30)
def profile(self) -> HostProfile:
return HostProfile(
@ -159,6 +160,7 @@ class HostBody(HostScanBody):
public_gateway=self.public_gateway,
public_dns_servers=self.public_dns_servers,
public_reserved_ips=self.public_reserved_ips,
public_vm_prefix=self.public_vm_prefix,
)
@ -238,6 +240,7 @@ def _public_host(profile: dict | None) -> dict:
"public_gateway": profile.get("public_gateway", ""),
"public_dns_servers": profile.get("public_dns_servers", []),
"public_reserved_ips": profile.get("public_reserved_ips", []),
"public_vm_prefix": profile.get("public_vm_prefix"),
}
@ -366,10 +369,17 @@ def create_app(
try:
network = ipaddress.IPv4Network(profile["public_pool_cidr"], strict=True)
gateway = ipaddress.IPv4Address(profile["public_gateway"])
if (network.prefixlen != 24 or not network.network_address.is_global
guest_prefix = profile.get("public_vm_prefix")
if guest_prefix is None:
guest_prefix = network.prefixlen
if (not 24 <= network.prefixlen <= 30
or not network.network_address.is_global
or not network.broadcast_address.is_global
or gateway not in network
or gateway in (network.network_address, network.broadcast_address)):
or gateway in (network.network_address, network.broadcast_address)
or isinstance(guest_prefix, bool)
or not isinstance(guest_prefix, int)
or not 24 <= guest_prefix <= network.prefixlen):
raise ValueError("Invalid host public IPv4 pool")
blocked = {network.network_address, network.broadcast_address, gateway}
for value in (*profile.get("public_reserved_ips", []),
@ -414,8 +424,8 @@ def create_app(
continue
if not insert_public_ip_claim(profile, str(address), vm_id):
continue
return f"{address}/{network.prefixlen}"
raise VMError("公网 /24 地址池中没有可分配的 IP,请检查已用和保留地址", 409)
return f"{address}/{guest_prefix}"
raise VMError("公网地址池中没有可分配的 IP,请检查已用和保留地址", 409)
def claim_explicit_public_ip(profile: dict, public_ip_cidr: str,
vm_id: str) -> None:
@ -1264,6 +1274,7 @@ def create_app(
**new_service(profile).resources(),
"public_pool_cidr": profile.get("public_pool_cidr", ""),
"public_pool_enabled": bool(profile.get("public_pool_cidr")),
"public_vm_prefix": profile.get("public_vm_prefix"),
}
@app.get("/api/v1/vm-templates")

@ -827,6 +827,7 @@ class DatabaseRemoteHostManager(RemoteHostManager):
metadata.get("public_dns_servers", []),
metadata.get("public_reserved_ips", []),
host,
metadata.get("public_vm_prefix"),
)
private_key = _validate_private_key(record["private_key"])
known_hosts = record["known_hosts"].strip()

@ -69,6 +69,7 @@ class HostProfile:
public_gateway: str = ""
public_dns_servers: list[str] = field(default_factory=list)
public_reserved_ips: list[str] = field(default_factory=list)
public_vm_prefix: int | None = None
@dataclass
@ -158,12 +159,15 @@ def validate_public_pool(
public_dns_servers: list[str],
public_reserved_ips: list[str],
host: str,
public_vm_prefix: int | None = None,
) -> dict[str, Any]:
"""Validate one host's public IPv4 pool without probing live network use.
The SSH host address is deliberately not persisted in the reserved list:
the allocator excludes a literal host IPv4 address while allocating, so
the operator can still distinguish explicit reservations from that rule.
A VM prefix override changes guest network configuration, never the set
of addresses this pool may allocate.
"""
if not isinstance(public_pool_cidr, str) or not isinstance(public_gateway, str):
raise RemoteHostError("公网地址池和网关格式无效")
@ -175,23 +179,37 @@ def validate_public_pool(
dns_input = list(public_dns_servers)
reserved_input = list(public_reserved_ips)
if not any((cidr, gateway, dns_input, reserved_input)):
if public_vm_prefix is not None:
raise RemoteHostError("启用虚拟机网卡前缀前必须配置公网地址池")
return {
"public_pool_cidr": "",
"public_gateway": "",
"public_dns_servers": [],
"public_reserved_ips": [],
"public_vm_prefix": None,
}
if not cidr or not gateway or not dns_input:
raise RemoteHostError("启用公网地址池时必须同时填写 /24 网段、网关和 DNS")
raise RemoteHostError("启用公网地址池时必须同时填写公网网段、网关和 DNS")
if len(reserved_input) > 254:
raise RemoteHostError("保留 IP 最多可填写 254 个地址")
try:
network = ipaddress.IPv4Network(cidr, strict=True)
# Providers sometimes describe a routed block using its gateway (for
# example 123.123.123.113/28). Normalize that host/CIDR to its actual
# network before checking membership or allocating any addresses.
network = ipaddress.IPv4Network(cidr, strict=False)
except (ValueError, ipaddress.AddressValueError, ipaddress.NetmaskValueError) as exc:
raise RemoteHostError("公网地址池必须填写规范的 IPv4 /24 网段地址") from exc
if (network.prefixlen != 24 or not network.network_address.is_global
raise RemoteHostError("公网地址池必须填写有效的 IPv4 地址/前缀") from exc
if (not 24 <= network.prefixlen <= 30
or not network.network_address.is_global
or not network.broadcast_address.is_global):
raise RemoteHostError("公网地址池必须是可路由的 IPv4 /24 网段")
raise RemoteHostError("公网地址池必须是可路由的 IPv4 网段,前缀长度为 /24 至 /30")
if network.prefixlen == 24 and cidr.split("/", 1)[0] != str(network.network_address):
raise RemoteHostError("/24 地址池必须填写网段起始地址;如果实际只获配 /28,请填写 /28 而不是 /24")
if (public_vm_prefix is not None
and (isinstance(public_vm_prefix, bool)
or not isinstance(public_vm_prefix, int)
or not 24 <= public_vm_prefix <= network.prefixlen)):
raise RemoteHostError("虚拟机网卡前缀须介于 /24 和地址池前缀之间")
def parse_ipv4(value: str, label: str) -> ipaddress.IPv4Address:
if not isinstance(value, str) or not value or value != value.strip():
@ -204,7 +222,7 @@ def validate_public_pool(
gateway_ip = parse_ipv4(gateway, "公网网关")
if gateway_ip not in network or gateway_ip in (
network.network_address, network.broadcast_address):
raise RemoteHostError("公网网关必须位于所配置 /24 网段内")
raise RemoteHostError("公网网关必须位于所配置的网段内")
def unique_ips(values: list[str], label: str, *, in_pool: bool) -> list[str]:
normalized: list[str] = []
@ -214,7 +232,7 @@ def validate_public_pool(
if in_pool:
if address not in network or address in (
network.network_address, network.broadcast_address):
raise RemoteHostError(f"{label}必须位于所配置 /24 网段内")
raise RemoteHostError(f"{label}必须位于所配置的网段内")
elif not address.is_global:
raise RemoteHostError(f"{label}必须是可路由的公网 IPv4 地址")
address_text = str(address)
@ -234,6 +252,7 @@ def validate_public_pool(
"public_gateway": str(gateway_ip),
"public_dns_servers": dns,
"public_reserved_ips": reserved,
"public_vm_prefix": public_vm_prefix,
}
@ -420,6 +439,7 @@ class RemoteHostManager:
profile.public_dns_servers,
profile.public_reserved_ips,
host,
profile.public_vm_prefix,
)
if not isinstance(confirmed_fingerprint, str) or not _FINGERPRINT.fullmatch(confirmed_fingerprint):
raise RemoteHostError("请确认扫描得到的 SHA256 主机指纹")
@ -768,6 +788,7 @@ class RemoteHostManager:
metadata.get("public_dns_servers", []),
metadata.get("public_reserved_ips", []),
metadata["host"],
metadata.get("public_vm_prefix"),
)
except RemoteHostError as exc:
raise RemoteHostError("已保存的宿主机配置损坏", status=500) from exc

Loading…
Cancel
Save