Files
portal-api/app/Services/PortalMonitorClient.php
T
baodev0andClaude Sonnet 5.5 6315a392c0 feat(service): custom product, lịch sử giám sát VM và mã hoá response theo phiên
- Dịch vụ custom: bảng custom_products + services.custom_product_id; createService nhận
  custom{} thay product_id, giá = đơn giá tháng x số tháng chu kỳ (1/3/6/12/24/36),
  gia hạn tính theo cùng đơn giá, ServiceClientDetailResource trả custom_product và specs.
- Giám sát VM: GET v1/system/vm-metrics/history gom lịch sử về tối đa 240 điểm;
  lỗi lấy dung lượng đĩa không còn làm hỏng cả thẻ metrics.
- SealResponse: AES-256-GCM theo khoá phiên do proxy Next cấp qua header x-seal-key
  (HMAC của session id). Client opt-in bằng x-seal; chi tiết dịch vụ, mật khẩu VM và
  console ticket luôn được mã hoá. Body gửi lên dạng {enc} được giải mã trước validate;
  khoá lệch trả 428 để client lấy khoá mới. Đây là che mắt DevTools, không phải bảo mật
  chống chính chủ tài khoản.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-27 13:58:50 +07:00

65 lines
2.0 KiB
PHP

<?php
namespace App\Services;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Facades\Http;
use RuntimeException;
class PortalMonitorClient
{
public function vmMetrics(string $vmwareId): array
{
return $this->get($vmwareId, 'metrics', 'VM metrics are unavailable');
}
public function vmStorage(string $vmwareId): array
{
return $this->get($vmwareId, 'storage', 'VM storage is unavailable');
}
public function vmHistory(string $vmwareId, \DateTimeInterface $from, \DateTimeInterface $to): array
{
return $this->get($vmwareId, 'history', 'VM metrics are unavailable', [
'from' => $from->format(DATE_RFC3339),
'to' => $to->format(DATE_RFC3339),
'limit' => 10000,
]);
}
private function get(string $vmwareId, string $resource, string $notFoundMessage, array $query = []): array
{
$url = rtrim((string) config('portal-monitor.url'), '/');
$apiKey = (string) config('portal-monitor.api_key');
if ($url === '' || $apiKey === '') {
throw new RuntimeException('Portal Monitor is not configured');
}
try {
$response = Http::acceptJson()
->withHeaders(['X-API-Key' => $apiKey])
->timeout((int) config('portal-monitor.timeout', 5))
->get($url . '/api/v1/vsphere/vms/' . rawurlencode($vmwareId) . '/' . $resource, $query);
} catch (ConnectionException) {
throw new RuntimeException('Portal Monitor is unavailable');
}
if ($response->status() === 404) {
throw new RuntimeException($notFoundMessage, 404);
}
if (!$response->successful()) {
throw new RuntimeException('Portal Monitor request failed');
}
$data = $response->json('data');
if (!is_array($data)) {
throw new RuntimeException('Portal Monitor returned an invalid response');
}
return $data;
}
}