From 191f85562a6c481eea8acda3cb90479fd2ecb789 Mon Sep 17 00:00:00 2001 From: infosave2007 Date: Sat, 11 Apr 2026 10:59:34 +0300 Subject: [PATCH] feat: implement container name resolution and persistence for AIVPN servers --- inc/InstallProtocolManager.php | 61 +++++++++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 4 deletions(-) diff --git a/inc/InstallProtocolManager.php b/inc/InstallProtocolManager.php index d2f2d34..c5c9576 100644 --- a/inc/InstallProtocolManager.php +++ b/inc/InstallProtocolManager.php @@ -6,6 +6,62 @@ class InstallProtocolManager private const DEFAULT_SLUG = 'amnezia-wg'; private const SESSION_KEY = 'pending_deploy_decisions'; + private static function resolveAivpnContainerName(VpnServer $server, array $options = []): string + { + $serverData = $server->getData(); + $candidates = array_values(array_unique(array_filter([ + trim((string) ($options['container_name'] ?? '')), + trim((string) ($serverData['container_name'] ?? '')), + 'aivpn-server', + ], static function ($value) { + return is_string($value) && $value !== ''; + }))); + + $namesRaw = (string) $server->executeCommand("docker ps -a --format '{{.Names}}' 2>/dev/null", true); + $names = array_values(array_filter(array_map('trim', preg_split('/\r?\n/', $namesRaw) ?: []), static function ($value) { + return $value !== ''; + })); + + foreach ($candidates as $candidate) { + if (in_array($candidate, $names, true)) { + return $candidate; + } + } + + foreach ($candidates as $candidate) { + foreach ($names as $name) { + if (stripos($name, $candidate) !== false) { + self::persistServerContainerName($server->getId(), $name); + return $name; + } + } + } + + foreach ($names as $name) { + if (stripos($name, 'aivpn-server') !== false || stripos($name, 'aivpn') !== false) { + self::persistServerContainerName($server->getId(), $name); + return $name; + } + } + + return $candidates[0] ?? 'aivpn-server'; + } + + private static function persistServerContainerName(int $serverId, string $containerName): void + { + $containerName = trim($containerName); + if ($serverId <= 0 || $containerName === '') { + return; + } + + try { + $pdo = DB::conn(); + $stmt = $pdo->prepare('UPDATE vpn_servers SET container_name = ? WHERE id = ?'); + $stmt->execute([$containerName, $serverId]); + } catch (Throwable $e) { + } + } + public static function getDefaultSlug(): string { return self::DEFAULT_SLUG; @@ -1442,10 +1498,7 @@ class InstallProtocolManager private static function runBuiltinAivpnAddClient(VpnServer $server, array $options): array { $serverData = $server->getData(); - $containerName = trim((string) ($options['container_name'] ?? ($serverData['container_name'] ?? ''))); - if ($containerName === '' || stripos($containerName, 'aivpn') === false) { - $containerName = 'aivpn-server'; - } + $containerName = self::resolveAivpnContainerName($server, $options); $clientName = trim((string) ($options['login'] ?? ($options['name'] ?? ''))); if ($clientName === '') {