You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
5.2 KiB
PHP
81 lines
5.2 KiB
PHP
<?php
|
|
namespace App\Middleware;
|
|
|
|
use App\Services\AdminCsrf;
|
|
use App\Services\Auth;
|
|
use App\Services\Cloud\CloudAccess;
|
|
use App\Services\Cloud\CloudError;
|
|
|
|
final class CloudApi
|
|
{
|
|
public function __invoke($request, $response, $next)
|
|
{
|
|
try {
|
|
$path = (string) $request->getUri()->getPath();
|
|
// PHP-owned snapshots and operation diagnostics need no Agent
|
|
// session. Authenticated database-only routes must not create
|
|
// cloud_sessions merely to render panel state.
|
|
$databaseOnly = preg_match('#^/api/v1/invoices(?:/|$)#D', $path) === 1
|
|
|| preg_match('#^/api/v1/admin/billing(?:/|$)#D', $path) === 1
|
|
|| preg_match('#^/api/v1/admin/instances(?:/|$)#D', $path) === 1
|
|
|| preg_match('#^/api/v1/admin/operations(?:/|$)#D', $path) === 1
|
|
|| preg_match('#^/api/v1/admin/maintenance-notices(?:/|$)#D', $path) === 1
|
|
|| preg_match('#^/api/v1/admin/service-compensations(?:/|$)#D', $path) === 1
|
|
|| 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/traffic-pool-groups(?:/[1-9][0-9]*(?:/(?:lines|readiness))?)?$#D', $path) === 1
|
|
|| preg_match('#^/api/v1/admin/vpc-control(?:/vpcs(?:/[1-9][0-9]*)?)?$#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(?:/memberships/[1-9][0-9]*)?$#D', $path) === 1
|
|
|| preg_match('#^/api/v1/admin/hosts/[1-9][0-9]*/network-attachments(?:/discover|/[0-9a-f]{32}(?:/test)?)?$#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
|
|
|| preg_match('#^/api/v1/admin/cloud-templates/[A-Za-z0-9_.-]+/startup-script$#D', $path) === 1
|
|
|| ($request->getMethod() === 'GET'
|
|
&& $path === '/api/v1/admin/cloud-templates')
|
|
|| (in_array($request->getMethod(), ['PATCH', 'DELETE'], true)
|
|
&& preg_match('#^/api/v1/admin/cloud-templates/[A-Za-z0-9_.-]+$#D', $path) === 1)
|
|
|| preg_match('#^/api/v1/ssh-keys(?:/|$)#D', $path) === 1
|
|
|| $path === '/api/v1/traffic-pools'
|
|
|| preg_match('#^/api/v1/tasks(?:/[0-9]+|/read)?$#D', $path) === 1
|
|
|| preg_match('#^/api/v1/vpcs(?:/[0-9a-f]{32})?$#D', $path) === 1
|
|
|| preg_match('#^/api/v1/vms/[0-9]+/operations$#D', $path) === 1
|
|
|| preg_match('#^/api/v1/vms/[0-9]+/(?:upgrade-options|upgrade)$#D', $path) === 1
|
|
|| preg_match('#^/api/v1/vms/[0-9]+/firewall$#D', $path) === 1
|
|
|| preg_match('#^/api/v1/vms/[0-9]+/snapshots(?:/[0-9a-f]{32}(?:/restore)?)?$#D', $path) === 1
|
|
|| preg_match('#^/api/v1/vms/[0-9]+/automatic-backup(?:/(?:enable|disable))?$#D', $path) === 1
|
|
|| preg_match('#^/api/v1/vms/[0-9]+/metrics$#D', $path) === 1
|
|
|| preg_match('#^/api/v1/vms/[0-9]+/traffic/daily$#D', $path) === 1
|
|
|| preg_match('#^/api/v1/vms/[0-9]+/display-name$#D', $path) === 1
|
|
|| $path === '/api/v1/announcements'
|
|
|| $path === '/api/v1/vms/page'
|
|
// The normal list status poll reads only cloud_instance_runtime.
|
|
// Keep the explicit live=1 diagnostic path session-backed.
|
|
|| ($path === '/api/v1/vms/statuses'
|
|
&& (($request->getQueryParams()['live'] ?? '') !== '1'))
|
|
|| (preg_match('#^/api/v1/vms/[0-9]+/status$#D', $path) === 1
|
|
&& (($request->getQueryParams()['live'] ?? '') !== '1'));
|
|
$actor = CloudAccess::actor(!$databaseOnly);
|
|
if (AdminCsrf::requiresProtection($request->getMethod())) {
|
|
if ($request->getHeaderLine('X-CSRF-Token') !== '') {
|
|
$request = $request->withHeader(AdminCsrf::HEADER_NAME, $request->getHeaderLine('X-CSRF-Token'));
|
|
}
|
|
if (!AdminCsrf::verify($request)) {
|
|
throw new CloudError('CSRF 校验失败,请刷新页面', 403);
|
|
}
|
|
}
|
|
return $next(
|
|
$request->withAttribute('cloud_actor', $actor)->withAttribute('auth_user', Auth::getUser()),
|
|
$response
|
|
);
|
|
} catch (CloudError $exception) {
|
|
return $response->withJson(['error' => $exception->getMessage(), 'detail' => $exception->getMessage()], $exception->httpStatus)
|
|
->withHeader('Cache-Control', 'no-store');
|
|
} catch (\Throwable $exception) {
|
|
error_log('Cloud API unavailable: ' . get_class($exception));
|
|
return $response->withJson(['error' => '虚拟化业务暂不可用'], 503)->withHeader('Cache-Control', 'no-store');
|
|
}
|
|
}
|
|
}
|