feat: enhance ServerMonitoring to resolve container names based on protocol and improve client metrics handling

This commit is contained in:
infosave2007
2026-04-04 16:24:21 +03:00
parent bb960645d7
commit d6eda37ebd
+40 -1
View File
@@ -275,7 +275,7 @@ class ServerMonitoring
// this->fetchXrayStats() call moved to collectClientMetrics to handle failure gracefully
// Get current stats from server
$containerName = $this->serverData['container_name'];
$containerName = (string) ($this->serverData['container_name'] ?? '');
$bytesReceived = 0;
$bytesSent = 0;
$speedUp = 0;
@@ -356,6 +356,10 @@ class ServerMonitoring
stripos($protocolSlug, 'wireguard') !== false
);
if ($isWireguardClient) {
$containerName = $this->resolveContainerForProtocol($protocolSlug);
}
if ($isAivpnClient) {
$aivpn = $this->getAivpnClientStats($client);
if (is_array($aivpn)) {
@@ -810,6 +814,41 @@ class ServerMonitoring
return '';
}
private function resolveContainerForProtocol(string $protocolSlug): string
{
$default = trim((string) ($this->serverData['container_name'] ?? ''));
if ($protocolSlug === '') {
return $default;
}
try {
$db = DB::conn();
$stmt = $db->prepare('SELECT definition FROM protocols WHERE slug = ? LIMIT 1');
$stmt->execute([$protocolSlug]);
$definitionJson = $stmt->fetchColumn();
if (is_string($definitionJson) && $definitionJson !== '') {
$definition = json_decode($definitionJson, true);
if (is_array($definition)) {
$candidate = trim((string) ($definition['metadata']['container_name'] ?? ''));
if ($candidate !== '') {
return $candidate;
}
}
}
} catch (Throwable $e) {
// Fallback to default container.
}
if ($protocolSlug === 'awg2') {
return 'amnezia-awg2';
}
if (stripos($protocolSlug, 'aivpn') !== false) {
return 'aivpn-server';
}
return $default;
}
/**
* Enforce single IP per user for Xray connections
* If a user is connected from multiple IPs, block all but the first one