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.
virt/web/app/Services/Cloud/CloudAdminAbuseMonitoringSe...

1247 lines
46 KiB
PHP

<?php
namespace App\Services\Cloud;
use App\Models\User;
/**
* Read-only administrator projection of bounded network-abuse observations.
*
* This service deliberately exposes only aggregate counters already accepted
* by the observation contract. It never returns packet bodies, remote address
* lists or remote port lists, and it owns no enforcement action.
*/
final class CloudAdminAbuseMonitoringService
{
private const STALE_SECONDS = 180;
private const DETAIL_SAMPLE_LIMIT = 60;
private const DETAIL_FINDING_LIMIT = 50;
private const JSON_LIMIT = 131072;
private const RISKS = [
'normal', 'observing', 'suspicious', 'high_risk',
'collection_anomaly',
];
private const DIRECTIONS = ['none', 'inbound', 'outbound', 'both'];
private const IP_VERSIONS = ['ipv4', 'ipv6'];
private const RATE_FIELDS = ['rx_bps', 'tx_bps', 'rx_pps', 'tx_pps'];
private const INTERFACE_FIELDS = [
'rx_bytes', 'tx_bytes', 'rx_packets', 'tx_packets',
'rx_errors', 'tx_errors', 'rx_drops', 'tx_drops',
];
private const FLOW_FIELDS = [
'tcp_active_flows', 'tcp_new_flows', 'tcp_syn_flows',
'tcp_unreplied_flows', 'udp_active_flows', 'udp_new_flows',
'other_active_flows', 'unique_remote_ips',
'unique_remote_ports', 'unique_local_ports',
'max_remote_ports_per_remote_ip',
'max_remote_ips_per_remote_port',
'max_local_ports_per_remote_ip',
'max_remote_ips_per_local_port',
];
private const RULE_LABELS = [
'outbound_tcp_syn_sweep' => '对外 TCP SYN 扫描',
'outbound_tcp_port_scan' => '对外 TCP 端口扫描',
'outbound_udp_fanout' => '对外 UDP 扇出',
'inbound_tcp_syn_attack' => '入站 TCP SYN 攻击',
'inbound_tcp_port_scan' => '入站 TCP 端口扫描',
'inbound_udp_flood' => '入站 UDP Flood',
];
private const EVIDENCE_LABELS = [
'tcp_active_flows' => 'TCP 活跃 Flow',
'tcp_new_flows' => 'TCP 新建 Flow',
'tcp_syn_flows' => 'TCP SYN Flow',
'tcp_unreplied_flows' => 'TCP 未应答 Flow',
'udp_active_flows' => 'UDP 活跃 Flow',
'udp_new_flows' => 'UDP 新建 Flow',
'unique_remote_ips' => '唯一远端 IP 数',
'unique_remote_ports' => '唯一远端端口数',
'unique_local_ports' => '唯一本地端口数',
'max_remote_ports_per_remote_ip' => '单远端 IP 最大端口数',
'max_remote_ips_per_remote_port' => '单远端端口最大 IP 数',
'max_local_ports_per_remote_ip' => '单远端 IP 最大本地端口数',
'max_remote_ips_per_local_port' => '单本地端口最大远端 IP 数',
];
/** @var \Closure():int */
private readonly \Closure $clock;
/** @var \Closure():array<int,array<string,mixed>> */
private readonly \Closure $heartbeatProbe;
public function __construct(
?callable $clock = null,
?callable $heartbeatProbe = null
) {
$this->clock = \Closure::fromCallable(
$clock ?? static fn (): int => time()
);
$this->heartbeatProbe = \Closure::fromCallable(
$heartbeatProbe
?? [CloudRuntimeHeartbeat::class, 'snapshot']
);
}
/** @param array<string,mixed> $actor @param array<string,mixed> $input */
public function page(array $actor, array $input = []): array
{
self::authorize($actor);
$now = $this->now();
$filters = self::filters($input);
$base = $this->baseQuery();
$this->applySharedFilters($base, $filters);
$summary = $this->summary(clone $base, $now);
$query = clone $base;
$this->applyRiskFilter($query, $filters['risk'], $now);
$total = max(0, (int) $query->count());
$pages = max(1, (int) ceil($total / $filters['per_page']));
$filters['page'] = min($filters['page'], $pages);
$driver = $query->getConnection()->getDriverName();
$records = $query->select(self::columns())
->addSelect($query->getConnection()->raw(
self::jsonProjectionSql(
$driver,
'state.latest_metrics_json',
'summary_json',
self::validSummarySql($driver)
)
))
->orderByDesc('state.updated_at')
->orderBy('state.instance_id')
->offset(($filters['page'] - 1) * $filters['per_page'])
->limit($filters['per_page'])->get();
$items = [];
foreach ($records as $record) {
$items[] = $this->row($record, $now);
}
$from = $total === 0 ? 0
: (($filters['page'] - 1) * $filters['per_page']) + 1;
return [
'summary' => $summary + [
'total' => $summary['normal'] + $summary['observing']
+ $summary['suspicious'] + $summary['high_risk']
+ $summary['collection_anomaly'],
],
'filters' => $filters,
'options' => $this->options(),
'health' => $this->health($now),
'items' => $items,
'pagination' => [
'page' => $filters['page'],
'per_page' => $filters['per_page'],
'total' => $total,
'total_pages' => $pages,
'from' => $from,
'to' => $from === 0 ? 0 : $from + count($items) - 1,
'previous_url' => self::pageUrl($filters,
max(1, $filters['page'] - 1)),
'next_url' => self::pageUrl($filters,
min($pages, $filters['page'] + 1)),
],
'generated_at' => self::timestamp($now),
];
}
/** @param array<string,mixed> $actor */
public function detail(array $actor, mixed $instanceId): array
{
self::authorize($actor);
$id = self::positiveInteger($instanceId, '实例编号');
$now = $this->now();
$query = $this->baseQuery()->where('state.instance_id', $id);
$driver = $query->getConnection()->getDriverName();
$record = $query->select(self::columns())
->addSelect($query->getConnection()->raw(
self::jsonProjectionSql(
$driver,
'state.latest_metrics_json',
'summary_json',
self::validSummarySql($driver)
)
))->first();
if ($record === null) {
throw new CloudError('监控记录不存在', 404);
}
$main = User::getConnectionResolver()->connection('default');
$findings = $main->table('cloud_network_findings')
->where('instance_id', $id)
->orderByDesc('last_seen_at')->orderByDesc('id')
->limit(self::DETAIL_FINDING_LIMIT)
->select([
'id', 'rule_code', 'direction', 'ip_family', 'severity',
'state', 'consecutive_windows', 'first_seen_at',
'last_seen_at', 'resolved_at',
])->addSelect($main->raw(self::jsonProjectionSql(
$main->getDriverName(),
'evidence_json',
'evidence_json'
)))->get();
return [
'item' => $this->row($record, $now),
'series' => $this->series($id),
'findings' => array_map(
[self::class, 'findingRow'],
$findings->all()
),
];
}
private function now(): int
{
$now = ($this->clock)();
if (!is_int($now) || $now < 1) {
throw new \LogicException('Abuse monitoring clock is invalid');
}
return $now;
}
/** @param array<string,mixed> $actor */
private static function authorize(array $actor): void
{
if (!($actor['is_admin'] ?? false)
|| (int) ($actor['user_id'] ?? 0) < 1) {
throw new CloudError('需要管理员权限', 403);
}
}
/** @param array<string,mixed> $input @return array<string,mixed> */
public static function filters(array $input): array
{
$allowed = [
'risk', 'direction', 'host_id', 'line_id', 'user_id',
'query', 'ip_version', 'page', 'per_page',
];
foreach ($input as $key => $value) {
if (!in_array($key, $allowed, true)
|| (!is_string($value) && !is_int($value))) {
throw new CloudError('筛选参数无效', 400);
}
}
$filters = [];
foreach ([
'risk' => self::RISKS,
'direction' => self::DIRECTIONS,
'ip_version' => self::IP_VERSIONS,
] as $key => $values) {
$value = trim((string) ($input[$key] ?? ''));
if ($value !== '' && !in_array($value, $values, true)) {
throw new CloudError('筛选参数无效', 400);
}
$filters[$key] = $value;
}
foreach (['host_id', 'line_id', 'user_id'] as $key) {
$value = trim((string) ($input[$key] ?? ''));
$filters[$key] = $value === ''
? null : self::positiveInteger($value, $key);
}
$query = trim((string) ($input['query'] ?? ''));
if (mb_strlen($query, 'UTF-8') > 120
|| preg_match('/[\x00-\x1f\x7f]/u', $query) !== 0) {
throw new CloudError('搜索关键词无效', 400);
}
$filters['query'] = $query;
$filters['page'] = self::boundedInteger(
$input['page'] ?? 1,
1,
100000,
'页码'
);
$filters['per_page'] = self::boundedInteger(
$input['per_page'] ?? 20,
1,
100,
'每页数量'
);
return $filters;
}
private function baseQuery()
{
return User::getConnectionResolver()->connection('default')
->table('cloud_network_instance_ledger as state')
->join('cloud_instances as instance',
'instance.id', '=', 'state.instance_id')
->leftJoin('user as owner', 'owner.id', '=', 'instance.user_id')
->leftJoin('cloud_hosts as host', 'host.id', '=', 'instance.host_id')
->leftJoin('cloud_resource_pools as pool',
'pool.id', '=', 'instance.resource_pool_id')
->leftJoin('cloud_network_lines as line',
'line.id', '=', 'pool.line_id')
->where('instance.status', '<>', 'deleted');
}
/** @return list<string> */
private static function columns(): array
{
return [
'state.instance_id',
'instance.user_id as user_id',
'instance.host_id as host_id',
'state.risk_state', 'state.collection_state',
'state.latest_direction', 'state.latest_rule_code',
'state.latest_severity',
'state.last_bucket_start', 'state.last_sample_at',
'state.last_evaluated_at', 'state.updated_at',
'state.current_day_start', 'state.current_day_json',
'instance.name as instance_name',
'instance.uuid as instance_uuid',
'instance.resource_pool_id',
'owner.user_name as user_name',
'host.name as host_name',
'host.region as host_region',
'line.id as line_id', 'line.name as line_name',
];
}
/** @param array<string,mixed> $filters */
private function applySharedFilters(object $query, array $filters): void
{
foreach ([
'host_id' => 'instance.host_id',
'user_id' => 'instance.user_id',
'line_id' => 'line.id',
] as $filter => $column) {
if ($filters[$filter] !== null) {
$query->where($column, $filters[$filter]);
}
}
if ($filters['direction'] !== '') {
$query->where('state.latest_direction', $filters['direction']);
}
if ($filters['ip_version'] !== '') {
$family = $filters['ip_version'];
$query->whereExists(static function ($finding) use ($family): void {
$finding->selectRaw('1')->from(
'cloud_network_findings as family_finding'
)->whereColumn(
'family_finding.instance_id',
'state.instance_id'
)->where('family_finding.state', 'open')
->whereIn('family_finding.ip_family', [$family, 'mixed']);
});
}
if ($filters['query'] === '') {
return;
}
$escaped = str_replace(
['!', '%', '_'],
['!!', '!%', '!_'],
$filters['query']
);
$like = '%' . $escaped . '%';
$numeric = ctype_digit($filters['query'])
? (int) $filters['query'] : null;
$query->where(static function ($search) use ($like, $numeric): void {
$search->whereRaw("instance.name LIKE ? ESCAPE '!'", [$like])
->orWhereRaw("instance.uuid LIKE ? ESCAPE '!'", [$like])
->orWhereRaw("owner.user_name LIKE ? ESCAPE '!'", [$like]);
if ($numeric !== null && $numeric > 0) {
$search->orWhere('state.instance_id', $numeric)
->orWhere('instance.user_id', $numeric);
}
});
}
private function applyRiskFilter(object $query, string $risk, int $now): void
{
if ($risk === '') {
return;
}
$cutoff = $now - self::STALE_SECONDS;
$validSummary = self::validSummarySql(
$query->getConnection()->getDriverName()
);
if ($risk === 'collection_anomaly') {
$query->where(static function ($anomaly) use (
$cutoff,
$validSummary
): void {
$anomaly->where('state.collection_state', '<>', 'healthy')
->orWhereNull('state.last_sample_at')
->orWhere('state.last_sample_at', '<', $cutoff)
->orWhereRaw('NOT (' . $validSummary . ')');
});
return;
}
$query->where('state.collection_state', 'healthy')
->whereNotNull('state.last_sample_at')
->where('state.last_sample_at', '>=', $cutoff)
->whereRaw($validSummary)
->where('state.risk_state', $risk);
}
/** @return array<string,int|string> */
private function summary(object $query, int $now): array
{
$cutoff = $now - self::STALE_SECONDS;
$valid = self::validSummarySql(
$query->getConnection()->getDriverName()
);
$effective = "CASE WHEN state.collection_state = 'healthy' "
. 'AND state.last_sample_at >= ? AND ' . $valid . ' '
. "THEN state.risk_state ELSE 'collection_anomaly' END";
$rows = $query->selectRaw(
$effective . ' AS effective_risk, COUNT(*) AS aggregate_count, '
. 'MAX(state.last_sample_at) AS last_sample_at',
[$cutoff]
)->groupBy('effective_risk')->get();
$result = array_fill_keys(self::RISKS, 0);
$lastSample = null;
foreach ($rows as $row) {
$risk = (string) ($row->effective_risk ?? '');
if (array_key_exists($risk, $result)) {
$result[$risk] = max(0, (int) $row->aggregate_count);
}
if ($row->last_sample_at !== null) {
$lastSample = $lastSample === null
? (int) $row->last_sample_at
: max($lastSample, (int) $row->last_sample_at);
}
}
return $result + [
'last_sample_at' => self::timestampOrNull($lastSample) ?? '',
];
}
/** @return array<string,mixed> */
private function row(object $record, int $now): array
{
$summary = self::normalizeSummary(
self::decodeObject((string) $record->summary_json)
);
$valid = $summary !== null;
$rates = $valid ? $summary['rates'] : self::nullRates();
$metrics = $valid ? $summary['metrics'] : null;
$flow = $metrics === null
? self::nullFlowSummary() : self::flowSummary($metrics);
$stale = $record->last_sample_at === null
|| (int) $record->last_sample_at < $now - self::STALE_SECONDS;
$healthy = $valid && !$stale
&& (string) $record->collection_state === 'healthy';
$qualityFlags = $valid ? $summary['quality_flags'] : null;
$sampleQuality = $healthy && $qualityFlags !== null
&& !in_array('incomplete', $qualityFlags, true)
? 'complete' : ($stale ? 'stale' : 'incomplete');
$effectiveRisk = $healthy
? (string) $record->risk_state : 'collection_anomaly';
return [
'instance_id' => (string) $record->instance_id,
'instance_name' => (string) ($record->instance_name ?? ''),
'instance_uuid' => (string) ($record->instance_uuid ?? ''),
'user_id' => (string) $record->user_id,
'user_name' => (string) ($record->user_name ?? ''),
'host_id' => (string) $record->host_id,
'host_name' => (string) ($record->host_name ?? ''),
'host_region' => (string) ($record->host_region ?? ''),
'line_id' => $record->line_id === null
? '' : (string) $record->line_id,
'line_name' => (string) ($record->line_name ?? ''),
'risk_state' => $effectiveRisk,
'collection_state' => $valid
? ($stale ? 'stale' : (string) $record->collection_state)
: 'degraded',
'direction' => in_array(
(string) $record->latest_direction,
self::DIRECTIONS,
true
) ? (string) $record->latest_direction : 'none',
'latest_rule_code' => $healthy
? (string) $record->latest_rule_code : '',
'latest_severity' => $healthy
? (string) $record->latest_severity : 'none',
'metrics' => $rates + $flow + [
'rx_bps_display' => self::rateDisplay($rates['rx_bps'], false),
'tx_bps_display' => self::rateDisplay($rates['tx_bps'], false),
'rx_pps_display' => self::rateDisplay($rates['rx_pps'], true),
'tx_pps_display' => self::rateDisplay($rates['tx_pps'], true),
],
'sample' => [
'bucket_start' => self::timestampOrNull(
$record->last_bucket_start
),
'sampled_at' => self::timestampOrNull(
$record->last_sample_at
),
'quality' => $sampleQuality,
'quality_label' => $sampleQuality === 'complete'
? '样本完整' : ($sampleQuality === 'stale'
? '采集已过期' : '样本不完整'),
],
'sample_count' => $valid ? $summary['sample_count'] : null,
'warmup_remaining' => $valid
? $summary['warmup_remaining'] : null,
'last_evaluated_at' => self::timestampOrNull(
$record->last_evaluated_at
),
];
}
/** @return list<array<string,mixed>> */
private function series(int $instanceId): array
{
$main = User::getConnectionResolver()->connection('default');
$records = $main->table('cloud_network_daily_summaries')
->where('instance_id', $instanceId)
->orderByDesc('day_start')
->limit(self::DETAIL_SAMPLE_LIMIT)
->get()->all();
$records = array_reverse($records);
$series = [];
foreach ($records as $record) {
$series[] = self::dailySeriesRow($record);
}
$ledger = $main->table('cloud_network_instance_ledger')
->where('instance_id', $instanceId)
->first(['current_day_start', 'current_day_json']);
if ($ledger !== null) {
$current = self::decodeObject(
(string) $ledger->current_day_json
);
if ($current !== null
&& self::validDailySummary($current)) {
$current['day_start'] = (int) $ledger->current_day_start;
$series[] = self::dailySeriesRow((object) $current);
}
}
return $series;
}
/** @return array<string,mixed> */
private static function dailySeriesRow(object $record): array
{
$flow = self::nullFlowSummary();
foreach ([
'tcp_active_peak' => 'tcp_active_flows',
'tcp_new_peak' => 'tcp_new_flows',
'tcp_syn_peak' => 'tcp_syn_flows',
'tcp_unreplied_peak' => 'tcp_unreplied_flows',
'udp_active_peak' => 'udp_active_flows',
'udp_new_peak' => 'udp_new_flows',
'peer_ip_peak' => 'unique_remote_ips',
'peer_port_peak' => 'unique_remote_ports',
] as $source => $target) {
$flow[$target] = self::nonNegativeInteger(
$record->{$source} ?? null
);
}
$samples = max(0, (int) ($record->sample_count ?? 0));
$complete = max(0, (int) ($record->complete_count ?? 0));
return [
'bucket_start' => self::timestamp((int) $record->day_start),
'quality' => $samples > 0 && $samples === $complete
? 'complete' : 'incomplete',
'rx_bps' => self::nonNegativeInteger(
$record->rx_bps_peak ?? null
),
'tx_bps' => self::nonNegativeInteger(
$record->tx_bps_peak ?? null
),
'rx_pps' => self::nonNegativeInteger(
$record->rx_pps_peak ?? null
),
'tx_pps' => self::nonNegativeInteger(
$record->tx_pps_peak ?? null
),
] + $flow;
}
/** @param array<string,mixed> $value */
private static function validDailySummary(array $value): bool
{
foreach ([
'sample_count', 'complete_count', 'quality_mask',
'rx_bps_peak', 'tx_bps_peak', 'rx_pps_peak', 'tx_pps_peak',
'tcp_active_peak', 'tcp_new_peak', 'tcp_syn_peak',
'tcp_unreplied_peak', 'udp_active_peak', 'udp_new_peak',
'peer_ip_peak', 'peer_port_peak', 'first_sample_at',
'last_sample_at',
] as $field) {
if (self::nonNegativeInteger($value[$field] ?? null) === null) {
return false;
}
}
return (int) $value['sample_count'] > 0
&& (int) $value['complete_count']
<= (int) $value['sample_count'];
}
/** @return array<string,mixed> */
private static function findingRow(object $finding): array
{
$evidence = self::decodeObject((string) $finding->evidence_json);
$parts = [];
if ($evidence !== null) {
foreach (self::EVIDENCE_LABELS as $key => $label) {
$value = self::nonNegativeInteger($evidence[$key] ?? null);
if ($value !== null) {
$parts[] = $label . ' ' . number_format($value, 0, '.', ',');
}
if (count($parts) >= 4) {
break;
}
}
}
return [
'finding_id' => (string) $finding->id,
'rule_code' => (string) $finding->rule_code,
'label' => self::RULE_LABELS[(string) $finding->rule_code]
?? '网络行为规则',
'direction' => (string) $finding->direction,
'ip_family' => (string) $finding->ip_family,
'severity' => (string) $finding->severity,
'state' => (string) $finding->state,
'consecutive_windows' => max(1,
(int) $finding->consecutive_windows),
'first_seen_at' => self::timestamp((int) $finding->first_seen_at),
'last_seen_at' => self::timestamp((int) $finding->last_seen_at),
'resolved_at' => self::timestampOrNull($finding->resolved_at),
'evidence_summary' => $parts === []
? '聚合证据不可用;未保存远端地址或端口明细。'
: implode(' · ', $parts),
];
}
/** @return array{hosts:list<array<string,string>>,lines:list<array<string,string>>} */
private function options(): array
{
$main = User::getConnectionResolver()->connection('default');
$hosts = [];
foreach ($main->table('cloud_hosts')->orderBy('name')->orderBy('id')
->get(['id', 'name']) as $host) {
$hosts[] = ['id' => (string) $host->id,
'name' => (string) $host->name];
}
$lines = [];
foreach ($main->table('cloud_network_lines')
->orderBy('name')->orderBy('id')->get(['id', 'name']) as $line) {
$lines[] = ['id' => (string) $line->id,
'name' => (string) $line->name];
}
return ['hosts' => $hosts, 'lines' => $lines];
}
/** @return array<string,mixed> */
private function health(int $now): array
{
$worker = null;
try {
foreach (($this->heartbeatProbe)() as $row) {
if (is_array($row) && ($row['key'] ?? null) === 'abuse_monitor') {
$worker = $row;
break;
}
}
} catch (\Throwable) {
$worker = null;
}
try {
$main = User::getConnectionResolver()->connection('default');
$hostIds = array_map('intval', $main->table('cloud_hosts')
->where('enabled', 1)->orderBy('id')->pluck('id')->all());
$cursors = $hostIds === [] ? [] : $main->table(
'cloud_network_monitor_hosts'
)->whereIn('host_id', $hostIds)->get()->keyBy('host_id');
} catch (\Throwable) {
return self::healthRow(
'unavailable', null, 'monitor_database_unavailable',
'监控账本暂不可读取;不能确认实例网络状态。'
);
}
if ($hostIds === []) {
return self::healthRow(
'unavailable', null, 'no_enabled_hosts',
'没有已启用计算节点,当前无可验证的监控覆盖。'
);
}
$healthy = 0;
$stale = 0;
$unavailable = 0;
$degraded = 0;
$lastSuccess = null;
foreach ($hostIds as $hostId) {
$cursor = $cursors->get($hostId);
if ($cursor === null) {
++$unavailable;
continue;
}
$success = $cursor->last_success_at === null
? null : (int) $cursor->last_success_at;
if ($success !== null) {
$lastSuccess = $lastSuccess === null
? $success : max($lastSuccess, $success);
}
if ((string) $cursor->collection_state === 'unavailable'
|| $success === null) {
++$unavailable;
} elseif ($success < $now - self::STALE_SECONDS) {
++$stale;
} elseif ((string) $cursor->collection_state !== 'healthy') {
++$degraded;
} else {
++$healthy;
}
}
$workerStatus = is_array($worker)
? (string) ($worker['status'] ?? 'unknown') : 'unknown';
$workerReady = in_array($workerStatus, ['ok', 'busy'], true);
if (!$workerReady) {
return self::healthRow(
$workerStatus === 'warning' ? 'degraded' : 'unavailable',
$lastSuccess,
'monitor_worker_unavailable',
'监控 Worker 没有可验证的常驻心跳;历史数据不会被视为当前健康。'
);
}
if ($healthy === count($hostIds)) {
return self::healthRow(
'healthy', $lastSuccess, '', '所有已启用节点最近采集正常。'
);
}
if ($unavailable === count($hostIds)) {
return self::healthRow(
'unavailable', $lastSuccess, 'all_hosts_unavailable',
'所有已启用节点均缺少可验证的最新采集。'
);
}
if ($stale > 0 && $healthy === 0 && $degraded === 0
&& $unavailable === 0) {
return self::healthRow(
'stale', $lastSuccess, 'host_collection_stale',
'节点采集记录均已过期;不会据此判断实例正常。'
);
}
return self::healthRow(
'degraded', $lastSuccess, 'partial_collection_failure',
'部分节点采集缺失、过期或异常,请核查节点状态。'
);
}
private static function healthRow(
string $state,
?int $lastSuccess,
string $error,
string $message
): array {
return [
'worker_state' => $state,
'last_success_at' => self::timestampOrNull($lastSuccess) ?? '',
'error_code' => $error,
'message' => $message,
];
}
/** @return null|array<string,int|null> */
private static function rates(mixed $value): ?array
{
if (!is_array($value) || array_is_list($value)) {
return null;
}
$result = [];
foreach (self::RATE_FIELDS as $field) {
if (!array_key_exists($field, $value)) {
return null;
}
$parsed = $value[$field] === null
? null : self::nonNegativeInteger($value[$field]);
if ($value[$field] !== null && $parsed === null) {
return null;
}
$result[$field] = $parsed;
}
return $result;
}
/** @return array<string,null> */
private static function nullRates(): array
{
return array_fill_keys(self::RATE_FIELDS, null);
}
/** @return array<string,int|null> */
private static function flowSummary(array $metrics): array
{
$result = [];
foreach (self::FLOW_FIELDS as $field) {
$sum = 0;
$valid = true;
$families = 0;
foreach (['ipv4', 'ipv6'] as $family) {
if (!is_array($metrics[$family] ?? null)) {
continue;
}
++$families;
foreach (['inbound', 'outbound'] as $direction) {
$value = self::nonNegativeInteger(
$metrics[$family][$direction][$field] ?? null
);
if ($value === null || $sum > PHP_INT_MAX - $value) {
$valid = false;
break 2;
}
$sum += $value;
}
}
$result[$field] = $valid && $families > 0 ? $sum : null;
}
return $result;
}
/** @return array<string,null> */
private static function nullFlowSummary(): array
{
return array_fill_keys(self::FLOW_FIELDS, null);
}
/** @return null|array<string,int> */
private static function interfaceCounters(mixed $value): ?array
{
if (!is_array($value) || array_is_list($value)) {
return null;
}
$result = [];
foreach (self::INTERFACE_FIELDS as $field) {
$parsed = self::nonNegativeInteger($value[$field] ?? null);
if ($parsed === null) {
return null;
}
$result[$field] = $parsed;
}
return $result;
}
/** @param array<string,int> $previous @param array<string,int> $current */
private static function interfaceRates(array $previous, array $current): array
{
$rates = self::nullRates();
foreach ([
'rx_bytes' => 'rx_bps', 'tx_bytes' => 'tx_bps',
'rx_packets' => 'rx_pps', 'tx_packets' => 'tx_pps',
] as $counter => $rate) {
if ($current[$counter] < $previous[$counter]) {
continue;
}
$delta = $current[$counter] - $previous[$counter];
if (str_ends_with($counter, '_bytes')) {
if ($delta > intdiv(PHP_INT_MAX, 8)) {
continue;
}
$rates[$rate] = intdiv($delta * 8, 60);
} else {
$rates[$rate] = intdiv($delta, 60);
}
}
return $rates;
}
/** @return null|list<string> */
private static function qualityFlags(mixed $value): ?array
{
if (!is_array($value) || !array_is_list($value)
|| count($value) > 16) {
return null;
}
$result = [];
foreach ($value as $flag) {
if (!is_string($flag)
|| preg_match('/^[a-z0-9_]{3,64}$/D', $flag) !== 1) {
return null;
}
$result[] = $flag;
}
return $result;
}
private static function decodeObject(string $json): ?array
{
if ($json === '' || strlen($json) > self::JSON_LIMIT) {
return null;
}
try {
// MySQL rejects JSON documents deeper than 100 levels. Keep the
// reader above that storage limit so harmless unknown fields do
// not make PHP disagree with the SQL validity projection.
$value = json_decode($json, true, 128, JSON_THROW_ON_ERROR);
} catch (\JsonException) {
return null;
}
return is_array($value) && !array_is_list($value) ? $value : null;
}
private static function normalizeSummary(?array $value): ?array
{
if ($value === null) {
return null;
}
$rates = self::rates($value['rates'] ?? null);
$metrics = self::normalizeMetrics($value['metrics'] ?? null);
$flags = self::summaryQualityFlags(
$value['quality_flags'] ?? null
);
$sampleCount = self::nonNegativeInteger(
$value['sample_count'] ?? null
);
$warmup = self::nonNegativeInteger(
$value['warmup_remaining'] ?? null
);
if ($rates === null || $metrics === null || $flags === null
|| $sampleCount === null || $warmup === null) {
return null;
}
return [
'rates' => $rates,
'sample_count' => $sampleCount,
'warmup_remaining' => $warmup,
'quality_flags' => $flags,
'metrics' => $metrics,
];
}
private static function validSummarySql(string $driver): string
{
$column = 'state.latest_metrics_json';
$rootType = $driver === 'sqlite'
? "json_type({$column})" : "JSON_TYPE({$column})";
$objectType = $driver === 'sqlite' ? 'object' : 'OBJECT';
$arrayType = $driver === 'sqlite' ? 'array' : 'ARRAY';
$stringType = $driver === 'sqlite' ? 'text' : 'STRING';
$checks = [
$rootType . " = '{$objectType}'",
self::jsonTypeSql($driver, $column, '$.rates')
. " = '{$objectType}'",
self::jsonTypeSql($driver, $column, '$.metrics')
. " = '{$objectType}'",
self::jsonTypeSql($driver, $column, '$.metrics.interface')
. " = '{$objectType}'",
self::jsonTypeSql($driver, $column, '$.quality_flags')
. " = '{$arrayType}'",
self::jsonArrayLengthSql(
$driver,
$column,
'$.quality_flags'
) . ' <= 16',
self::jsonIntegerSql(
$driver,
$column,
'$.sample_count'
),
self::jsonIntegerSql(
$driver,
$column,
'$.warmup_remaining'
),
];
foreach (self::RATE_FIELDS as $field) {
$checks[] = self::jsonIntegerSql(
$driver,
$column,
'$.rates.' . $field,
true
);
}
foreach (self::INTERFACE_FIELDS as $field) {
$checks[] = self::jsonIntegerSql(
$driver,
$column,
'$.metrics.interface.' . $field
);
}
for ($index = 0; $index < 16; ++$index) {
$type = self::jsonTypeSql(
$driver,
$column,
'$.quality_flags[' . $index . ']'
);
$checks[] = '(' . $type . ' IS NULL OR '
. $type . " = '{$stringType}')";
}
$familyValid = [];
$familyAbsent = [];
$jsonNull = $driver === 'sqlite' ? 'null' : 'NULL';
foreach (['ipv4', 'ipv6'] as $family) {
$familyValid[$family] = self::validFamilySql(
$driver,
$column,
$family
);
$familyType = self::jsonTypeSql(
$driver,
$column,
'$.metrics.' . $family
);
$familyAbsent[$family] = '(' . $familyType . ' IS NULL OR '
. $familyType . " = '{$jsonNull}')";
}
$checks[] = '((' . $familyValid['ipv4'] . ') OR ('
. $familyValid['ipv6'] . '))';
foreach (['ipv4', 'ipv6'] as $family) {
$checks[] = '((' . $familyAbsent[$family] . ') OR ('
. $familyValid[$family] . '))';
}
$jsonValid = $driver === 'sqlite'
? "json_valid({$column}) = 1"
: "JSON_VALID({$column}) = 1";
$withinLimit = self::jsonByteLengthSql($driver, $column)
. ' <= ' . self::JSON_LIMIT;
return '(CASE WHEN ' . $jsonValid . ' AND ' . $withinLimit
. ' THEN CASE WHEN ' . implode(' AND ', $checks)
. ' THEN 1 ELSE 0 END ELSE 0 END) = 1';
}
/** @return null|array<string,mixed> */
private static function normalizeMetrics(mixed $value): ?array
{
if (!is_array($value) || array_is_list($value)) {
return null;
}
$interface = self::interfaceCounters($value['interface'] ?? null);
if ($interface === null) {
return null;
}
$result = ['interface' => $interface];
$families = 0;
foreach (['ipv4', 'ipv6'] as $family) {
if (!array_key_exists($family, $value)
|| $value[$family] === null) {
continue;
}
if (!is_array($value[$family])
|| array_is_list($value[$family])) {
return null;
}
$normalized = [];
foreach (['inbound', 'outbound'] as $direction) {
$flows = $value[$family][$direction] ?? null;
if (!is_array($flows) || array_is_list($flows)) {
return null;
}
$directionValues = [];
foreach (self::FLOW_FIELDS as $field) {
$parsed = self::nonNegativeInteger(
$flows[$field] ?? null
);
if ($parsed === null) {
return null;
}
$directionValues[$field] = $parsed;
}
$normalized[$direction] = $directionValues;
}
$result[$family] = $normalized;
++$families;
}
return $families > 0 ? $result : null;
}
/** @return null|list<string> */
private static function summaryQualityFlags(mixed $value): ?array
{
if (!is_array($value) || !array_is_list($value)
|| count($value) > 16) {
return null;
}
foreach ($value as $flag) {
if (!is_string($flag)) {
return null;
}
}
return $value;
}
private static function validFamilySql(
string $driver,
string $column,
string $family
): string {
$objectType = $driver === 'sqlite' ? 'object' : 'OBJECT';
$checks = [self::jsonTypeSql(
$driver,
$column,
'$.metrics.' . $family
) . " = '{$objectType}'"];
foreach (['inbound', 'outbound'] as $direction) {
$base = '$.metrics.' . $family . '.' . $direction;
$checks[] = self::jsonTypeSql($driver, $column, $base)
. " = '{$objectType}'";
foreach (self::FLOW_FIELDS as $field) {
$checks[] = self::jsonIntegerSql(
$driver,
$column,
$base . '.' . $field
);
}
}
return '(' . implode(' AND ', $checks) . ')';
}
private static function jsonIntegerSql(
string $driver,
string $column,
string $path,
bool $nullable = false
): string {
$type = self::jsonTypeSql($driver, $column, $path);
$value = self::jsonValueSql($driver, $column, $path);
$integerType = $driver === 'sqlite' ? 'integer' : 'INTEGER';
$jsonNull = $driver === 'sqlite' ? 'null' : 'NULL';
$integer = '(' . $type . " = '{$integerType}' AND "
. $value . ' >= 0 AND ' . $value . ' <= ' . PHP_INT_MAX . ')';
return $nullable
? '(' . $type . " = '{$jsonNull}' OR " . $integer . ')'
: $integer;
}
private static function jsonTypeSql(
string $driver,
string $column,
string $path
): string {
return $driver === 'sqlite'
? "json_type({$column}, '" . $path . "')"
: "JSON_TYPE(JSON_EXTRACT({$column}, '" . $path . "'))";
}
private static function jsonValueSql(
string $driver,
string $column,
string $path
): string {
return $driver === 'sqlite'
? "json_extract({$column}, '" . $path . "')"
: "JSON_EXTRACT({$column}, '" . $path . "')";
}
private static function jsonArrayLengthSql(
string $driver,
string $column,
string $path
): string {
return $driver === 'sqlite'
? "json_array_length({$column}, '" . $path . "')"
: "JSON_LENGTH(JSON_EXTRACT({$column}, '" . $path . "'))";
}
private static function jsonByteLengthSql(
string $driver,
string $column
): string {
return $driver === 'sqlite'
? 'length(CAST(' . $column . ' AS BLOB))'
: 'OCTET_LENGTH(' . $column . ')';
}
private static function jsonProjectionSql(
string $driver,
string $column,
string $alias,
?string $predicate = null
): string {
$predicate ??= self::jsonByteLengthSql($driver, $column)
. ' <= ' . self::JSON_LIMIT;
return 'CASE WHEN COALESCE((' . $predicate . '), 0) = 1 THEN '
. $column . ' ELSE NULL END AS ' . $alias;
}
private static function decodeList(string $json): ?array
{
if ($json === '' || strlen($json) > self::JSON_LIMIT) {
return null;
}
try {
$value = json_decode($json, true, 12, JSON_THROW_ON_ERROR);
} catch (\JsonException) {
return null;
}
return is_array($value) && array_is_list($value) ? $value : null;
}
private static function nonNegativeInteger(mixed $value): ?int
{
return is_int($value) && $value >= 0 ? $value : null;
}
private static function rateDisplay(?int $value, bool $packets): string
{
if ($value === null) {
return '不可用';
}
if ($packets) {
return number_format($value, 0, '.', ',') . ' pps';
}
$units = ['bps', 'Kbps', 'Mbps', 'Gbps', 'Tbps'];
$scaled = (float) $value;
$unit = 0;
while ($scaled >= 1000 && $unit < count($units) - 1) {
$scaled /= 1000;
++$unit;
}
$precision = $scaled >= 100 || floor($scaled) === $scaled ? 0 : 1;
return number_format($scaled, $precision, '.', '') . ' '
. $units[$unit];
}
private static function timestamp(int $value): string
{
return gmdate('Y-m-d\TH:i:s\Z', max(1, $value));
}
private static function timestampOrNull(mixed $value): ?string
{
if ((!is_int($value) && !ctype_digit((string) $value))
|| (int) $value < 1) {
return null;
}
return self::timestamp((int) $value);
}
/** @param array<string,mixed> $filters */
private static function pageUrl(array $filters, int $page): string
{
$query = [];
foreach ([
'risk', 'direction', 'host_id', 'line_id', 'user_id',
'query', 'ip_version', 'per_page',
] as $key) {
$value = $filters[$key];
if ($value !== '' && $value !== null
&& !($key === 'per_page' && $value === 20)) {
$query[$key] = (string) $value;
}
}
if ($page > 1) {
$query['page'] = (string) $page;
}
return '/admin/abuse-monitoring'
. ($query === [] ? '' : '?' . http_build_query(
$query, '', '&', PHP_QUERY_RFC3986
));
}
private static function positiveInteger(mixed $value, string $name): int
{
return self::boundedInteger($value, 1, PHP_INT_MAX, $name);
}
private static function boundedInteger(
mixed $value,
int $minimum,
int $maximum,
string $name
): int {
if ((!is_int($value) && !is_string($value))
|| !ctype_digit((string) $value)) {
throw new CloudError($name . '无效', 400);
}
$parsed = filter_var((string) $value, FILTER_VALIDATE_INT, [
'options' => ['min_range' => $minimum, 'max_range' => $maximum],
]);
if ($parsed === false) {
throw new CloudError($name . '无效', 400);
}
return (int) $parsed;
}
}