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.
64 lines
3.6 KiB
PHP
64 lines
3.6 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 operations diagnostics need no engine
|
|
// session. Health may call the signed, session-independent engine
|
|
// probe; authenticating these routes must not create cloud_sessions.
|
|
$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/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/fabric(?:/|$)#D', $path) === 1
|
|
|| preg_match('#^/api/v1/admin/hosts/[0-9]+/scheduling$#D', $path) === 1
|
|
|| preg_match('#^/api/v1/ssh-keys(?:/|$)#D', $path) === 1
|
|
|| preg_match('#^/api/v1/(?:jobs|reinstall-jobs|rescue-jobs)/[0-9]+$#D', $path) === 1
|
|
|| preg_match('#^/api/v1/vms/[0-9]+/operations$#D', $path) === 1
|
|
|| preg_match('#^/api/v1/vms/[0-9]+/firewall$#D', $path) === 1
|
|
|| preg_match('#^/api/v1/vpcs(?:/[0-9a-f]{32}(?:/.*)?)?$#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]+/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');
|
|
}
|
|
}
|
|
}
|