|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Controllers;
|
|
|
|
|
|
|
|
|
|
use App\Models\CloudHost;
|
|
|
|
|
use App\Models\CloudJob;
|
|
|
|
|
use App\Services\Cloud\CloudAccess;
|
|
|
|
|
use App\Services\Cloud\CloudAdminInstanceService;
|
|
|
|
|
use App\Services\Cloud\CloudError;
|
|
|
|
|
use App\Services\Cloud\CloudEngineClient;
|
|
|
|
|
use App\Services\Cloud\CloudInput;
|
|
|
|
|
use App\Services\Cloud\CloudJobService;
|
|
|
|
|
|
|
|
|
|
/** Administrator inventory snapshots plus explicitly queued VM mutations. */
|
|
|
|
|
final class CloudAdminInstanceController
|
|
|
|
|
{
|
|
|
|
|
public function index($request, $response)
|
|
|
|
|
{
|
|
|
|
|
return $response->withJson(CloudAdminInstanceService::page(
|
|
|
|
|
$request->getAttribute('cloud_actor') ?? CloudAccess::actor(false),
|
|
|
|
|
$request->getQueryParams()
|
|
|
|
|
))->withHeader('Cache-Control', 'private, no-store');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function detail($request, $response, $args)
|
|
|
|
|
{
|
|
|
|
|
return $response->withJson(CloudAdminInstanceService::detail(
|
|
|
|
|
$request->getAttribute('cloud_actor') ?? CloudAccess::actor(false),
|
|
|
|
|
(string) ($args['id'] ?? '')
|
|
|
|
|
))->withHeader('Cache-Control', 'private, no-store');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return only the current host network choices needed by the IP selector.
|
|
|
|
|
* The normal instance detail stays database-only; this endpoint explicitly
|
|
|
|
|
* asks the private engine for a fresh, non-secret address-pool snapshot.
|
|
|
|
|
*/
|
|
|
|
|
public function networkOptions($request, $response, $args)
|
|
|
|
|
{
|
|
|
|
|
$actor = $request->getAttribute('cloud_actor') ?? CloudAccess::actor(false);
|
|
|
|
|
CloudAccess::manage($actor);
|
|
|
|
|
$instance = CloudAccess::instance((string) ($args['id'] ?? ''), $actor);
|
|
|
|
|
$spec = json_decode((string) $instance->spec_json, true);
|
|
|
|
|
if (!is_array($spec) || array_is_list($spec)
|
|
|
|
|
|| ($spec['managed'] ?? false) !== true
|
|
|
|
|
|| ($spec['os_source'] ?? null) !== 'cloud_template'
|
|
|
|
|
|| !is_string($spec['public_ip_cidr'] ?? null)
|
|
|
|
|
|| trim($spec['public_ip_cidr']) === '') {
|
|
|
|
|
throw new CloudError('当前虚拟机不是支持网络配置的面板云镜像实例', 409);
|
|
|
|
|
}
|
|
|
|
|
$host = CloudHost::find($instance->host_id);
|
|
|
|
|
if ($host === null) {
|
|
|
|
|
throw new CloudError('虚拟机所属宿主机不存在,无法读取地址池', 409);
|
|
|
|
|
}
|
|
|
|
|
$profile = (new CloudEngineClient())->host(
|
|
|
|
|
(int) $host->id, 'GET', 'host/network-options', $actor
|
|
|
|
|
);
|
|
|
|
|
$gateway = self::safeIpv4($profile['gateway'] ?? null);
|
|
|
|
|
$dnsServers = self::safeIpv4List($profile['dns_servers'] ?? null, 4);
|
|
|
|
|
$guestPrefix = self::safePrefix($profile['guest_prefix'] ?? null);
|
|
|
|
|
$blocked = array_values(array_filter([$gateway, ...$dnsServers]));
|
|
|
|
|
$rawAvailable = $profile['available_ips'] ?? null;
|
|
|
|
|
$availableOverflow = is_array($rawAvailable) && count($rawAvailable) > 254;
|
|
|
|
|
$available = [];
|
|
|
|
|
foreach (is_array($rawAvailable) ? array_slice($rawAvailable, 0, 254) : [] as $value) {
|
|
|
|
|
$cidr = self::safePublicIpCidr($value);
|
|
|
|
|
$address = $cidr === null ? null : explode('/', $cidr, 2)[0];
|
|
|
|
|
$parts = $cidr === null ? [] : explode('/', $cidr, 2);
|
|
|
|
|
if ($cidr !== null && $guestPrefix !== null
|
|
|
|
|
&& (int) ($parts[1] ?? 0) === $guestPrefix
|
|
|
|
|
&& self::cidrInPool($cidr, $profile['pool_cidr'] ?? null)
|
|
|
|
|
&& !in_array($address, $blocked, true)) {
|
|
|
|
|
$available[] = $cidr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$available = array_values(array_unique($available));
|
|
|
|
|
$configurationState = in_array($profile['configuration_state'] ?? null,
|
|
|
|
|
['ready', 'not_configured', 'invalid', 'drift'], true)
|
|
|
|
|
? $profile['configuration_state'] : 'unavailable';
|
|
|
|
|
if (!is_array($rawAvailable) || $availableOverflow) {
|
|
|
|
|
$configurationState = 'unavailable';
|
|
|
|
|
$available = [];
|
|
|
|
|
}
|
|
|
|
|
$currentCidr = self::safePublicIpCidr($spec['public_ip_cidr']);
|
|
|
|
|
$message = !is_array($rawAvailable)
|
|
|
|
|
? '宿主机返回的可用公网 IP 列表格式异常,请先检查引擎状态。'
|
|
|
|
|
: ($availableOverflow
|
|
|
|
|
? '宿主机返回的可用公网 IP 列表超出安全上限,请先检查地址池配置。'
|
|
|
|
|
: (is_string($profile['message'] ?? null)
|
|
|
|
|
? mb_substr($profile['message'], 0, 240) : ''));
|
|
|
|
|
$activeJob = CloudJob::where('instance_id', $instance->id)
|
|
|
|
|
->whereIn('status', ['queued', 'running', 'uncertain'])
|
|
|
|
|
->orderByDesc('id')->first(['id', 'kind', 'status', 'updated_at']);
|
|
|
|
|
$active = (string) $instance->status === 'active';
|
|
|
|
|
return $response->withJson([
|
|
|
|
|
'instance_id' => (string) $instance->id,
|
|
|
|
|
'host_id' => (string) $host->id,
|
|
|
|
|
'host_name' => (string) $host->name,
|
|
|
|
|
'region' => (string) $host->region,
|
|
|
|
|
'current_ip_cidr' => $currentCidr,
|
|
|
|
|
'pool_cidr' => self::safeText($profile['pool_cidr'] ?? null, 32),
|
|
|
|
|
'guest_prefix' => $guestPrefix,
|
|
|
|
|
'gateway' => $gateway,
|
|
|
|
|
'dns_servers' => $dnsServers,
|
|
|
|
|
'available_ips' => $available,
|
|
|
|
|
'available_count' => count($available),
|
|
|
|
|
'configuration_state' => $configurationState,
|
|
|
|
|
'active_job' => $activeJob === null ? null : [
|
|
|
|
|
'id' => (string) $activeJob->id,
|
|
|
|
|
'kind' => (string) $activeJob->kind,
|
|
|
|
|
'status' => (string) $activeJob->status,
|
|
|
|
|
'updated_at' => max(0, (int) $activeJob->updated_at),
|
|
|
|
|
],
|
|
|
|
|
'can_change' => $active && $activeJob === null
|
|
|
|
|
&& $configurationState === 'ready'
|
|
|
|
|
&& $available !== [],
|
|
|
|
|
'message' => $message,
|
|
|
|
|
])->withHeader('Cache-Control', 'private, no-store');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Queue a controlled same-host public IPv4 replacement.
|
|
|
|
|
*
|
|
|
|
|
* The engine owns the actual address pool and performs the shutdown,
|
|
|
|
|
* cloud-init/network update, clean-traffic update and claim swap. PHP
|
|
|
|
|
* only validates the administrative boundary and records the durable job;
|
|
|
|
|
* it deliberately does not update spec_json until the worker receives a
|
|
|
|
|
* successful engine result.
|
|
|
|
|
*/
|
|
|
|
|
public function network($request, $response, $args)
|
|
|
|
|
{
|
|
|
|
|
$actor = $request->getAttribute('cloud_actor') ?? CloudAccess::actor(false);
|
|
|
|
|
CloudAccess::manage($actor);
|
|
|
|
|
$instance = CloudAccess::instance((string) ($args['id'] ?? ''), $actor, true);
|
|
|
|
|
if ((string) $instance->status !== 'active') {
|
|
|
|
|
throw new CloudError('只有处于活动状态的虚拟机可以更换公网 IP', 409);
|
|
|
|
|
}
|
|
|
|
|
$body = CloudInput::object($request);
|
|
|
|
|
CloudInput::only($body, ['public_ip_cidr', 'confirm_restart']);
|
|
|
|
|
if (($body['confirm_restart'] ?? false) !== true) {
|
|
|
|
|
throw new CloudError('更换公网 IP 前必须确认虚拟机会短暂重启');
|
|
|
|
|
}
|
|
|
|
|
$cidr = CloudInput::publicIpCidr($body['public_ip_cidr'] ?? null);
|
|
|
|
|
$spec = json_decode((string) $instance->spec_json, true);
|
|
|
|
|
if (!is_array($spec) || array_is_list($spec)
|
|
|
|
|
|| ($spec['managed'] ?? false) !== true
|
|
|
|
|
|| ($spec['os_source'] ?? null) !== 'cloud_template') {
|
|
|
|
|
throw new CloudError('当前虚拟机不是支持网络配置的面板云镜像实例', 409);
|
|
|
|
|
}
|
|
|
|
|
$current = is_string($spec['public_ip_cidr'] ?? null)
|
|
|
|
|
? trim($spec['public_ip_cidr']) : '';
|
|
|
|
|
if ($current !== '' && $current === $cidr) {
|
|
|
|
|
throw new CloudError('新公网 IP 与当前配置相同', 409);
|
|
|
|
|
}
|
|
|
|
|
$job = CloudJobService::enqueue($instance, $actor, 'network_change', 'POST',
|
|
|
|
|
'/network', ['public_ip_cidr' => $cidr, 'confirm_restart' => true]);
|
|
|
|
|
return $response->withJson([
|
|
|
|
|
'job_id' => (string) $job->id,
|
|
|
|
|
'status_url' => '/api/v1/jobs/' . $job->id,
|
|
|
|
|
'public_ip_cidr' => $cidr,
|
|
|
|
|
], 202)->withHeader('Cache-Control', 'no-store');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function safeText($value, int $length): ?string
|
|
|
|
|
{
|
|
|
|
|
if (!is_string($value) || trim($value) !== $value || $value === ''
|
|
|
|
|
|| strlen($value) > $length || preg_match('/[\x00-\x1f\x7f]/', $value) === 1) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return $value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function safePrefix($value): ?int
|
|
|
|
|
{
|
|
|
|
|
return is_int($value) && $value >= 24 && $value <= 30 ? $value : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function safeIpv4($value): ?string
|
|
|
|
|
{
|
|
|
|
|
return is_string($value) && trim($value) === $value
|
|
|
|
|
&& filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false
|
|
|
|
|
? $value : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function safeIpv4List($value, int $limit): array
|
|
|
|
|
{
|
|
|
|
|
if (!is_array($value)) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
$result = [];
|
|
|
|
|
foreach (array_slice($value, 0, $limit) as $item) {
|
|
|
|
|
$ip = self::safeIpv4($item);
|
|
|
|
|
if ($ip !== null) {
|
|
|
|
|
$result[] = $ip;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return array_values(array_unique($result));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function safePublicIpCidr($value): ?string
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$cidr = CloudInput::publicIpCidr($value);
|
|
|
|
|
} catch (\Throwable $error) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
[$address, $prefix] = explode('/', $cidr, 2);
|
|
|
|
|
$prefix = (int) $prefix;
|
|
|
|
|
return $prefix >= 24 && $prefix <= 30 ? $address . '/' . $prefix : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function cidrInPool(string $cidr, $pool): bool
|
|
|
|
|
{
|
|
|
|
|
$target = self::ipv4Number(explode('/', $cidr, 2)[0]);
|
|
|
|
|
$parts = is_string($pool) ? explode('/', trim($pool), 2) : [];
|
|
|
|
|
$network = isset($parts[0], $parts[1]) ? self::ipv4Number($parts[0]) : null;
|
|
|
|
|
$prefix = isset($parts[1]) && ctype_digit($parts[1]) ? (int) $parts[1] : -1;
|
|
|
|
|
if ($target === null || $network === null || $prefix < 24 || $prefix > 30) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$block = 2 ** (32 - $prefix);
|
|
|
|
|
if (floor($target / $block) !== floor($network / $block)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$networkBase = floor($network / $block) * $block;
|
|
|
|
|
return $target !== $networkBase && $target !== $networkBase + $block - 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function ipv4Number($value): ?int
|
|
|
|
|
{
|
|
|
|
|
if (!is_string($value) || filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$packed = inet_pton($value);
|
|
|
|
|
if ($packed === false || strlen($packed) !== 4) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$parts = unpack('Nnumber', $packed);
|
|
|
|
|
return is_array($parts) && isset($parts['number'])
|
|
|
|
|
? (int) sprintf('%u', $parts['number']) : null;
|
|
|
|
|
}
|
|
|
|
|
}
|