Fix: Multiplexing do not write file manually

This commit is contained in:
peaklabs-dev
2024-09-17 16:22:53 +02:00
parent d13e2c0865
commit d9181bd00b
2 changed files with 35 additions and 30 deletions

View File

@@ -5,7 +5,6 @@ namespace App\Helpers;
use App\Models\Server; use App\Models\Server;
use App\Models\PrivateKey; use App\Models\PrivateKey;
use Illuminate\Support\Facades\Process; use Illuminate\Support\Facades\Process;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Hash;
class SshMultiplexingHelper class SshMultiplexingHelper
@@ -56,6 +55,10 @@ class SshMultiplexingHelper
$sshKeyLocation = $sshConfig['sshKeyLocation']; $sshKeyLocation = $sshConfig['sshKeyLocation'];
$muxSocket = $sshConfig['muxFilename']; $muxSocket = $sshConfig['muxFilename'];
// ray('Establishing new multiplexed connection')->blue();
// ray('SSH Key Location:', $sshKeyLocation);
// ray('Mux Socket:', $muxSocket);
$connectionTimeout = config('constants.ssh.connection_timeout'); $connectionTimeout = config('constants.ssh.connection_timeout');
$serverInterval = config('constants.ssh.server_interval'); $serverInterval = config('constants.ssh.server_interval');
$muxPersistTime = config('constants.ssh.mux_persist_time'); $muxPersistTime = config('constants.ssh.mux_persist_time');
@@ -64,26 +67,46 @@ class SshMultiplexingHelper
. self::getCommonSshOptions($server, $sshKeyLocation, $connectionTimeout, $serverInterval) . self::getCommonSshOptions($server, $sshKeyLocation, $connectionTimeout, $serverInterval)
. "{$server->user}@{$server->ip}"; . "{$server->user}@{$server->ip}";
// ray('Establish Command:', $establishCommand);
$establishProcess = Process::run($establishCommand); $establishProcess = Process::run($establishCommand);
// ray('Establish Process Exit Code:', $establishProcess->exitCode());
// ray('Establish Process Output:', $establishProcess->output());
// ray('Establish Process Error Output:', $establishProcess->errorOutput());
if ($establishProcess->exitCode() !== 0) { if ($establishProcess->exitCode() !== 0) {
// ray('Failed to establish multiplexed connection')->red();
throw new \RuntimeException('Failed to establish multiplexed connection: ' . $establishProcess->errorOutput()); throw new \RuntimeException('Failed to establish multiplexed connection: ' . $establishProcess->errorOutput());
} }
$muxContent = "Multiplexed connection established at " . now()->toDateTimeString(); // ray('Successfully established multiplexed connection')->green();
$muxFilename = basename($muxSocket);
if (!Storage::disk('ssh-mux')->put($muxFilename, $muxContent)) { // Check if the mux socket file was created
throw new \RuntimeException('Failed to write mux file to disk: ' . $muxFilename); if (!file_exists($muxSocket)) {
// ray('Mux socket file not found after connection establishment')->orange();
} }
} }
public static function removeMuxFile(Server $server) public static function removeMuxFile(Server $server)
{ {
$sshConfig = self::serverSshConfiguration($server); $sshConfig = self::serverSshConfiguration($server);
$muxFilename = basename($sshConfig['muxFilename']); $muxSocket = $sshConfig['muxFilename'];
$closeCommand = "ssh -O exit -o ControlPath=/var/www/html/storage/app/ssh/mux/{$muxFilename} {$server->user}@{$server->ip}"; $closeCommand = "ssh -O exit -o ControlPath=$muxSocket {$server->user}@{$server->ip}";
Process::run($closeCommand); $process = Process::run($closeCommand);
// ray('Closing multiplexed connection')->blue();
// ray('Close command:', $closeCommand);
// ray('Close process exit code:', $process->exitCode());
// ray('Close process output:', $process->output());
// ray('Close process error output:', $process->errorOutput());
if ($process->exitCode() !== 0) {
// ray('Failed to close multiplexed connection')->orange();
} else {
// ray('Successfully closed multiplexed connection')->green();
}
} }
public static function generateScpCommand(Server $server, string $source, string $dest) public static function generateScpCommand(Server $server, string $source, string $dest)
@@ -97,17 +120,9 @@ class SshMultiplexingHelper
$scp_command = "timeout $timeout scp "; $scp_command = "timeout $timeout scp ";
if (self::isMultiplexingEnabled()) { if (self::isMultiplexingEnabled()) {
// ray('SSH Multiplexing: Enabled for SCP command')->green();
$muxPersistTime = config('constants.ssh.mux_persist_time'); $muxPersistTime = config('constants.ssh.mux_persist_time');
$scp_command .= "-o ControlMaster=auto -o ControlPath=$muxSocket -o ControlPersist={$muxPersistTime} "; $scp_command .= "-o ControlMaster=auto -o ControlPath=$muxSocket -o ControlPersist={$muxPersistTime} ";
self::ensureMultiplexedConnection($server); self::ensureMultiplexedConnection($server);
// ray('SSH Multiplexing: Verifying usage')->blue();
$checkCommand = "ssh -O check -o ControlPath=$muxSocket {$server->user}@{$server->ip}";
$checkProcess = Process::run($checkCommand);
// ray('SSH Multiplexing: ' . ($checkProcess->exitCode() === 0 ? 'Active' : 'Not Active'))->color($checkProcess->exitCode() === 0 ? 'green' : 'red');
} else {
// ray('SSH Multiplexing: Disabled for SCP command')->orange();
} }
self::addCloudflareProxyCommand($scp_command, $server); self::addCloudflareProxyCommand($scp_command, $server);
@@ -133,17 +148,9 @@ class SshMultiplexingHelper
$ssh_command = "timeout $timeout ssh "; $ssh_command = "timeout $timeout ssh ";
if (self::isMultiplexingEnabled()) { if (self::isMultiplexingEnabled()) {
// ray('SSH Multiplexing: Enabled for SSH command')->green();
$muxPersistTime = config('constants.ssh.mux_persist_time'); $muxPersistTime = config('constants.ssh.mux_persist_time');
$ssh_command .= "-o ControlMaster=auto -o ControlPath=$muxSocket -o ControlPersist={$muxPersistTime} "; $ssh_command .= "-o ControlMaster=auto -o ControlPath=$muxSocket -o ControlPersist={$muxPersistTime} ";
self::ensureMultiplexedConnection($server); self::ensureMultiplexedConnection($server);
// ray('SSH Multiplexing: Verifying usage')->blue();
$checkCommand = "ssh -O check -o ControlPath=$muxSocket {$server->user}@{$server->ip}";
$checkProcess = Process::run($checkCommand);
// ray('SSH Multiplexing: ' . ($checkProcess->exitCode() === 0 ? 'Active' : 'Not Active'))->color($checkProcess->exitCode() === 0 ? 'green' : 'red');
} else {
// ray('SSH Multiplexing: Disabled for SSH command')->orange();
} }
self::addCloudflareProxyCommand($ssh_command, $server); self::addCloudflareProxyCommand($ssh_command, $server);
@@ -163,9 +170,7 @@ class SshMultiplexingHelper
private static function isMultiplexingEnabled(): bool private static function isMultiplexingEnabled(): bool
{ {
$isEnabled = config('constants.ssh.mux_enabled') && !config('coolify.is_windows_docker_desktop'); return config('constants.ssh.mux_enabled') && !config('coolify.is_windows_docker_desktop');
// ray('SSH Multiplexing Status:', $isEnabled ? 'ENABLED' : 'DISABLED')->color($isEnabled ? 'green' : 'red');
return $isEnabled;
} }
private static function validateSshKey(string $sshKeyLocation): void private static function validateSshKey(string $sshKeyLocation): void

View File

@@ -137,20 +137,20 @@ class PrivateKey extends BaseModel
public function storeInFileSystem() public function storeInFileSystem()
{ {
$filename = "ssh@{$this->uuid}"; $filename = "ssh_key@{$this->uuid}";
Storage::disk('ssh-keys')->put($filename, $this->private_key); Storage::disk('ssh-keys')->put($filename, $this->private_key);
return "/var/www/html/storage/app/ssh/keys/{$filename}"; return "/var/www/html/storage/app/ssh/keys/{$filename}";
} }
public static function deleteFromStorage(self $privateKey) public static function deleteFromStorage(self $privateKey)
{ {
$filename = "ssh@{$privateKey->uuid}"; $filename = "ssh_key@{$privateKey->uuid}";
Storage::disk('ssh-keys')->delete($filename); Storage::disk('ssh-keys')->delete($filename);
} }
public function getKeyLocation() public function getKeyLocation()
{ {
return "/var/www/html/storage/app/ssh/keys/ssh@{$this->uuid}"; return "/var/www/html/storage/app/ssh/keys/ssh_key@{$this->uuid}";
} }
public function updatePrivateKey(array $data) public function updatePrivateKey(array $data)