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.
115 lines
4.1 KiB
PHP
115 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Services\Config;
|
|
use GuzzleHttp\Client;
|
|
|
|
/** Authenticated PHP client for the root-owned local update helper. */
|
|
final class CloudUpdateHelperClient
|
|
{
|
|
private const SOCKET_PATH = '/run/virt-cloud/update.sock';
|
|
private const BASE_URI = 'http://localhost';
|
|
|
|
private Client $client;
|
|
private string $secret;
|
|
|
|
public function __construct(?Client $client = null, ?string $secret = null)
|
|
{
|
|
$this->secret = $secret ?? (string) Config::get(
|
|
'cloud_update_helper_secret',
|
|
''
|
|
);
|
|
if (strlen($this->secret) < 32
|
|
|| str_contains($this->secret, "\0")
|
|
|| str_contains($this->secret, "\r")
|
|
|| str_contains($this->secret, "\n")) {
|
|
throw new CloudError('本机更新助手密钥尚未正确配置', 503);
|
|
}
|
|
if ($client !== null) {
|
|
$this->client = $client;
|
|
return;
|
|
}
|
|
if (!defined('CURLOPT_UNIX_SOCKET_PATH')) {
|
|
throw new CloudError('PHP cURL 不支持本机 Unix Socket 更新助手', 503);
|
|
}
|
|
$this->client = new Client([
|
|
'base_uri' => self::BASE_URI,
|
|
'connect_timeout' => 3,
|
|
'timeout' => 95,
|
|
'http_errors' => false,
|
|
'allow_redirects' => false,
|
|
]);
|
|
}
|
|
|
|
public function request(
|
|
string $method,
|
|
string $target,
|
|
array $actor,
|
|
?array $payload = null
|
|
): array {
|
|
if (!preg_match('#^/v1(?:/[A-Za-z0-9_-]+)*$#D', $target)) {
|
|
throw new \InvalidArgumentException('Invalid update-helper target');
|
|
}
|
|
$body = $payload === null ? '' : json_encode(
|
|
$payload === [] ? (object) [] : $payload,
|
|
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
|
|
| JSON_UNESCAPED_UNICODE
|
|
);
|
|
$options = [
|
|
'headers' => CloudAgentClient::headers(
|
|
$method,
|
|
$target,
|
|
$body,
|
|
$actor,
|
|
$this->secret
|
|
),
|
|
'body' => $body,
|
|
'allow_redirects' => false,
|
|
'http_errors' => false,
|
|
'proxy' => '',
|
|
'timeout' => 95,
|
|
];
|
|
if (defined('CURLOPT_UNIX_SOCKET_PATH')) {
|
|
$options['curl'] = [constant('CURLOPT_UNIX_SOCKET_PATH') => self::SOCKET_PATH];
|
|
}
|
|
try {
|
|
$response = $this->client->request(
|
|
strtoupper($method),
|
|
self::BASE_URI . $target,
|
|
$options
|
|
);
|
|
} catch (\Throwable $error) {
|
|
throw new CloudError('本机更新助手不可用,请通过 SSH 检查服务状态', 503);
|
|
}
|
|
$status = $response->getStatusCode();
|
|
$raw = (string) $response->getBody();
|
|
if (strlen($raw) > 262144) {
|
|
throw new CloudError('本机更新助手响应过大', 502);
|
|
}
|
|
$result = json_decode($raw, true);
|
|
if (!is_array($result) || ($result !== [] && array_is_list($result))) {
|
|
throw new CloudError('本机更新助手返回了无效响应', 502);
|
|
}
|
|
if ($status < 200 || $status >= 300) {
|
|
$code = is_string($result['error']['code'] ?? null)
|
|
? $result['error']['code'] : '';
|
|
$message = match ($status) {
|
|
401 => '本机更新助手身份验证失败',
|
|
403 => '只有超级管理员可以执行在线更新',
|
|
404 => '更新任务不存在',
|
|
409 => '更新前置条件不满足或状态需要人工核对',
|
|
422 => '更新请求参数无效',
|
|
503 => '本机更新助手状态不可用',
|
|
default => '在线更新请求未成功',
|
|
};
|
|
if ($code === 'update_precondition_failed') {
|
|
$message = '代码、服务或依赖状态不满足安全更新条件';
|
|
}
|
|
throw new CloudError($message, in_array($status,
|
|
[403, 404, 409, 422, 503], true) ? $status : 502);
|
|
}
|
|
return $result;
|
|
}
|
|
}
|