@ -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 || public DnsServers. length || publicReservedIps . length ) ) {
throw new Error ( "填写公网网关、 DNS 或保留地址时,必须先填写允许分配的 /24 网段;如需关闭地址池,请将这四 项全部清空。") ;
if ( ! publicPoolCidr && ( publicGateway || public VmPrefix !== null || public DnsServers. 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 ) {