|
|
|
@@ -16,6 +16,7 @@ use Illuminate\Support\Facades\Storage;
|
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
use Spatie\Activitylog\Contracts\Activity;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function remote_process(
|
|
|
|
|
Collection|array $command,
|
|
|
|
|
Server $server,
|
|
|
|
@@ -26,19 +27,18 @@ function remote_process(
|
|
|
|
|
$callEventOnFinish = null,
|
|
|
|
|
$callEventData = null
|
|
|
|
|
): Activity {
|
|
|
|
|
if (is_null($type)) {
|
|
|
|
|
$type = ActivityTypes::INLINE->value;
|
|
|
|
|
}
|
|
|
|
|
if ($command instanceof Collection) {
|
|
|
|
|
$command = $command->toArray();
|
|
|
|
|
}
|
|
|
|
|
$type = $type ?? ActivityTypes::INLINE->value;
|
|
|
|
|
$command = $command instanceof Collection ? $command->toArray() : $command;
|
|
|
|
|
|
|
|
|
|
if ($server->isNonRoot()) {
|
|
|
|
|
$command = parseCommandsByLineForSudo(collect($command), $server);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$command_string = implode("\n", $command);
|
|
|
|
|
|
|
|
|
|
if (auth()->user()) {
|
|
|
|
|
$teams = auth()->user()->teams->pluck('id');
|
|
|
|
|
if (! $teams->contains($server->team_id) && ! $teams->contains(0)) {
|
|
|
|
|
if (!$teams->contains($server->team_id) && !$teams->contains(0)) {
|
|
|
|
|
throw new \Exception('User is not part of the team that owns this server');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@@ -46,9 +46,7 @@ function remote_process(
|
|
|
|
|
return resolve(PrepareCoolifyTask::class, [
|
|
|
|
|
'remoteProcessArgs' => new CoolifyTaskArgs(
|
|
|
|
|
server_uuid: $server->uuid,
|
|
|
|
|
command: <<<EOT
|
|
|
|
|
{$command_string}
|
|
|
|
|
EOT,
|
|
|
|
|
command: $command_string,
|
|
|
|
|
type: $type,
|
|
|
|
|
type_uuid: $type_uuid,
|
|
|
|
|
model: $model,
|
|
|
|
@@ -58,40 +56,27 @@ function remote_process(
|
|
|
|
|
),
|
|
|
|
|
])();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function server_ssh_configuration(Server $server)
|
|
|
|
|
{
|
|
|
|
|
$uuid = data_get($server, 'uuid');
|
|
|
|
|
if (is_null($uuid)) {
|
|
|
|
|
throw new \Exception('Server does not have a uuid');
|
|
|
|
|
}
|
|
|
|
|
$private_key_filename = "id.root@{$server->uuid}";
|
|
|
|
|
$location = '/var/www/html/storage/app/ssh/keys/'.$private_key_filename;
|
|
|
|
|
$sshKeyLocation = $server->privateKey->getKeyLocation();
|
|
|
|
|
ray($sshKeyLocation);
|
|
|
|
|
$mux_filename = '/var/www/html/storage/app/ssh/mux/'.$server->muxFilename();
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'location' => $location,
|
|
|
|
|
'mux_filename' => $mux_filename,
|
|
|
|
|
'private_key_filename' => $private_key_filename,
|
|
|
|
|
'sshKeyLocation' => $sshKeyLocation,
|
|
|
|
|
'muxFilename' => $mux_filename,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
function savePrivateKeyToFs(Server $server)
|
|
|
|
|
{
|
|
|
|
|
if (data_get($server, 'privateKey.private_key') === null) {
|
|
|
|
|
throw new \Exception("Server {$server->name} does not have a private key");
|
|
|
|
|
}
|
|
|
|
|
['location' => $location, 'private_key_filename' => $private_key_filename] = server_ssh_configuration($server);
|
|
|
|
|
Storage::disk('ssh-keys')->makeDirectory('.');
|
|
|
|
|
Storage::disk('ssh-mux')->makeDirectory('.');
|
|
|
|
|
Storage::disk('ssh-keys')->put($private_key_filename, $server->privateKey->private_key);
|
|
|
|
|
|
|
|
|
|
return $location;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function generateScpCommand(Server $server, string $source, string $dest)
|
|
|
|
|
{
|
|
|
|
|
$sshConfig = server_ssh_configuration($server);
|
|
|
|
|
$sshKeyLocation = $sshConfig['sshKeyLocation'];
|
|
|
|
|
$muxSocket = $sshConfig['muxFilename'];
|
|
|
|
|
|
|
|
|
|
$user = $server->user;
|
|
|
|
|
$port = $server->port;
|
|
|
|
|
$privateKeyLocation = savePrivateKeyToFs($server);
|
|
|
|
|
$timeout = config('constants.ssh.command_timeout');
|
|
|
|
|
$connectionTimeout = config('constants.ssh.connection_timeout');
|
|
|
|
|
$serverInterval = config('constants.ssh.server_interval');
|
|
|
|
@@ -99,21 +84,20 @@ function generateScpCommand(Server $server, string $source, string $dest)
|
|
|
|
|
|
|
|
|
|
$scp_command = "timeout $timeout scp ";
|
|
|
|
|
$muxEnabled = config('constants.ssh.mux_enabled', true) && config('coolify.is_windows_docker_desktop') == false;
|
|
|
|
|
// ray('SSH Multiplexing Enabled:', $muxEnabled)->blue();
|
|
|
|
|
ray('SSH Multiplexing Enabled:', $muxEnabled)->blue();
|
|
|
|
|
|
|
|
|
|
if ($muxEnabled) {
|
|
|
|
|
$muxSocket = "/var/www/html/storage/app/ssh/mux/{$server->muxFilename()}";
|
|
|
|
|
$scp_command .= "-o ControlMaster=auto -o ControlPath=$muxSocket -o ControlPersist={$muxPersistTime} ";
|
|
|
|
|
ensureMultiplexedConnection($server);
|
|
|
|
|
// ray('Using SSH Multiplexing')->green();
|
|
|
|
|
ray('Using SSH Multiplexing')->green();
|
|
|
|
|
} else {
|
|
|
|
|
// ray('Not using SSH Multiplexing')->red();
|
|
|
|
|
ray('Not using SSH Multiplexing')->red();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data_get($server, 'settings.is_cloudflare_tunnel')) {
|
|
|
|
|
$scp_command .= '-o ProxyCommand="/usr/local/bin/cloudflared access ssh --hostname %h" ';
|
|
|
|
|
}
|
|
|
|
|
$scp_command .= "-i {$privateKeyLocation} "
|
|
|
|
|
$scp_command .= "-i {$sshKeyLocation} "
|
|
|
|
|
.'-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null '
|
|
|
|
|
.'-o PasswordAuthentication=no '
|
|
|
|
|
."-o ConnectTimeout=$connectionTimeout "
|
|
|
|
@@ -126,6 +110,7 @@ function generateScpCommand(Server $server, string $source, string $dest)
|
|
|
|
|
|
|
|
|
|
return $scp_command;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function instant_scp(string $source, string $dest, Server $server, $throwError = true)
|
|
|
|
|
{
|
|
|
|
|
$timeout = config('constants.ssh.command_timeout');
|
|
|
|
@@ -146,48 +131,52 @@ function instant_scp(string $source, string $dest, Server $server, $throwError =
|
|
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function generateSshCommand(Server $server, string $command)
|
|
|
|
|
{
|
|
|
|
|
if ($server->settings->force_disabled) {
|
|
|
|
|
throw new \RuntimeException('Server is disabled.');
|
|
|
|
|
}
|
|
|
|
|
$user = $server->user;
|
|
|
|
|
$port = $server->port;
|
|
|
|
|
$privateKeyLocation = savePrivateKeyToFs($server);
|
|
|
|
|
|
|
|
|
|
$sshConfig = server_ssh_configuration($server);
|
|
|
|
|
$sshKeyLocation = $sshConfig['sshKeyLocation'];
|
|
|
|
|
$muxSocket = $sshConfig['muxFilename'];
|
|
|
|
|
|
|
|
|
|
$timeout = config('constants.ssh.command_timeout');
|
|
|
|
|
$connectionTimeout = config('constants.ssh.connection_timeout');
|
|
|
|
|
$serverInterval = config('constants.ssh.server_interval');
|
|
|
|
|
$muxPersistTime = config('constants.ssh.mux_persist_time');
|
|
|
|
|
$muxEnabled = config('constants.ssh.mux_enabled') && !config('coolify.is_windows_docker_desktop');
|
|
|
|
|
|
|
|
|
|
$ssh_command = "timeout $timeout ssh ";
|
|
|
|
|
|
|
|
|
|
$muxEnabled = config('constants.ssh.mux_enabled') && config('coolify.is_windows_docker_desktop') == false;
|
|
|
|
|
// ray('SSH Multiplexing Enabled:', $muxEnabled)->blue();
|
|
|
|
|
ray('SSH Multiplexing Enabled:', $muxEnabled)->blue();
|
|
|
|
|
|
|
|
|
|
if ($muxEnabled) {
|
|
|
|
|
// Always use multiplexing when enabled
|
|
|
|
|
$muxSocket = "/var/www/html/storage/app/ssh/mux/{$server->muxFilename()}";
|
|
|
|
|
$ssh_command .= "-o ControlMaster=auto -o ControlPath=$muxSocket -o ControlPersist={$muxPersistTime} ";
|
|
|
|
|
ensureMultiplexedConnection($server);
|
|
|
|
|
// ray('Using SSH Multiplexing')->green();
|
|
|
|
|
ray('Using SSH Multiplexing')->green();
|
|
|
|
|
} else {
|
|
|
|
|
// ray('Not using SSH Multiplexing')->red();
|
|
|
|
|
ray('Not using SSH Multiplexing')->red();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data_get($server, 'settings.is_cloudflare_tunnel')) {
|
|
|
|
|
$ssh_command .= '-o ProxyCommand="/usr/local/bin/cloudflared access ssh --hostname %h" ';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$command = "PATH=\$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/host/usr/local/sbin:/host/usr/local/bin:/host/usr/sbin:/host/usr/bin:/host/sbin:/host/bin && $command";
|
|
|
|
|
$delimiter = Hash::make($command);
|
|
|
|
|
$command = str_replace($delimiter, '', $command);
|
|
|
|
|
$ssh_command .= "-i {$privateKeyLocation} "
|
|
|
|
|
|
|
|
|
|
$ssh_command .= "-i {$sshKeyLocation} "
|
|
|
|
|
.'-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null '
|
|
|
|
|
.'-o PasswordAuthentication=no '
|
|
|
|
|
."-o ConnectTimeout=$connectionTimeout "
|
|
|
|
|
."-o ServerAliveInterval=$serverInterval "
|
|
|
|
|
.'-o RequestTTY=no '
|
|
|
|
|
.'-o LogLevel=ERROR '
|
|
|
|
|
."-p {$port} "
|
|
|
|
|
."{$user}@{$server->ip} "
|
|
|
|
|
."-p {$server->port} "
|
|
|
|
|
."{$server->user}@{$server->ip} "
|
|
|
|
|
." 'bash -se' << \\$delimiter".PHP_EOL
|
|
|
|
|
.$command.PHP_EOL
|
|
|
|
|
.$delimiter;
|
|
|
|
@@ -197,52 +186,33 @@ function generateSshCommand(Server $server, string $command)
|
|
|
|
|
|
|
|
|
|
function ensureMultiplexedConnection(Server $server)
|
|
|
|
|
{
|
|
|
|
|
if (! (config('constants.ssh.mux_enabled') && config('coolify.is_windows_docker_desktop') == false)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static $ensuredConnections = [];
|
|
|
|
|
|
|
|
|
|
if (isset($ensuredConnections[$server->id])) {
|
|
|
|
|
if (! shouldResetMultiplexedConnection($server)) {
|
|
|
|
|
// ray('Using Existing Multiplexed Connection')->green();
|
|
|
|
|
$sshConfig = server_ssh_configuration($server);
|
|
|
|
|
$muxSocket = $sshConfig['muxFilename'];
|
|
|
|
|
|
|
|
|
|
if (isset($ensuredConnections[$server->id]) && !shouldResetMultiplexedConnection($server)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$muxSocket = "/var/www/html/storage/app/ssh/mux/{$server->muxFilename()}";
|
|
|
|
|
$checkCommand = "ssh -O check -o ControlPath=$muxSocket ";
|
|
|
|
|
if (data_get($server, 'settings.is_cloudflare_tunnel')) {
|
|
|
|
|
$checkCommand .= '-o ProxyCommand="/usr/local/bin/cloudflared access ssh --hostname %h" ';
|
|
|
|
|
}
|
|
|
|
|
$checkCommand .= " {$server->user}@{$server->ip}";
|
|
|
|
|
|
|
|
|
|
$checkCommand = "ssh -O check -o ControlPath=$muxSocket {$server->user}@{$server->ip}";
|
|
|
|
|
$process = Process::run($checkCommand);
|
|
|
|
|
|
|
|
|
|
if ($process->exitCode() === 0) {
|
|
|
|
|
// ray('Existing Multiplexed Connection is Valid')->green();
|
|
|
|
|
$ensuredConnections[$server->id] = [
|
|
|
|
|
'timestamp' => now(),
|
|
|
|
|
'muxSocket' => $muxSocket,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ray('Establishing New Multiplexed Connection')->orange();
|
|
|
|
|
|
|
|
|
|
$privateKeyLocation = savePrivateKeyToFs($server);
|
|
|
|
|
$sshKeyLocation = $sshConfig['sshKeyLocation'];
|
|
|
|
|
$connectionTimeout = config('constants.ssh.connection_timeout');
|
|
|
|
|
$serverInterval = config('constants.ssh.server_interval');
|
|
|
|
|
$muxPersistTime = config('constants.ssh.mux_persist_time');
|
|
|
|
|
|
|
|
|
|
$establishCommand = "ssh -fNM -o ControlMaster=auto -o ControlPath=$muxSocket -o ControlPersist={$muxPersistTime} ";
|
|
|
|
|
|
|
|
|
|
if (data_get($server, 'settings.is_cloudflare_tunnel')) {
|
|
|
|
|
$establishCommand .= '-o ProxyCommand="/usr/local/bin/cloudflared access ssh --hostname %h" ';
|
|
|
|
|
}
|
|
|
|
|
$establishCommand .= "-i {$privateKeyLocation} "
|
|
|
|
|
$establishCommand = "ssh -fNM -o ControlMaster=auto -o ControlPath=$muxSocket -o ControlPersist={$muxPersistTime} "
|
|
|
|
|
."-i {$sshKeyLocation} "
|
|
|
|
|
.'-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null '
|
|
|
|
|
.'-o PasswordAuthentication=no '
|
|
|
|
|
."-o ConnectTimeout=$connectionTimeout "
|
|
|
|
@@ -262,8 +232,6 @@ function ensureMultiplexedConnection(Server $server)
|
|
|
|
|
'timestamp' => now(),
|
|
|
|
|
'muxSocket' => $muxSocket,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// ray('Established New Multiplexed Connection')->green();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function shouldResetMultiplexedConnection(Server $server)
|
|
|
|
@@ -320,7 +288,7 @@ function instant_remote_process(Collection|array $command, Server $server, bool
|
|
|
|
|
$process = Process::timeout($timeout)->run($sshCommand);
|
|
|
|
|
$end_time = microtime(true);
|
|
|
|
|
|
|
|
|
|
$execution_time = ($end_time - $start_time) * 1000; // Convert to milliseconds
|
|
|
|
|
// $execution_time = ($end_time - $start_time) * 1000; // Convert to milliseconds
|
|
|
|
|
// ray('SSH command execution time:', $execution_time.' ms')->orange();
|
|
|
|
|
|
|
|
|
|
$output = trim($process->output());
|
|
|
|
@@ -339,6 +307,7 @@ function instant_remote_process(Collection|array $command, Server $server, bool
|
|
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function excludeCertainErrors(string $errorOutput, ?int $exitCode = null)
|
|
|
|
|
{
|
|
|
|
|
$ignoredErrors = collect([
|
|
|
|
@@ -358,6 +327,7 @@ function excludeCertainErrors(string $errorOutput, ?int $exitCode = null)
|
|
|
|
|
}
|
|
|
|
|
throw new \RuntimeException($errorOutput, $exitCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function decode_remote_command_output(?ApplicationDeploymentQueue $application_deployment_queue = null): Collection
|
|
|
|
|
{
|
|
|
|
|
$application = Application::find(data_get($application_deployment_queue, 'application_id'));
|
|
|
|
@@ -424,16 +394,20 @@ function decode_remote_command_output(?ApplicationDeploymentQueue $application_d
|
|
|
|
|
|
|
|
|
|
return $formatted;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function remove_iip($text)
|
|
|
|
|
{
|
|
|
|
|
$text = preg_replace('/x-access-token:.*?(?=@)/', 'x-access-token:'.REDACTED, $text);
|
|
|
|
|
|
|
|
|
|
return preg_replace('/\x1b\[[0-9;]*m/', '', $text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function remove_mux_and_private_key(Server $server)
|
|
|
|
|
{
|
|
|
|
|
$muxFilename = $server->muxFilename();
|
|
|
|
|
$privateKeyLocation = savePrivateKeyToFs($server);
|
|
|
|
|
$sshConfig = server_ssh_configuration($server);
|
|
|
|
|
$privateKeyLocation = $sshConfig['sshKeyLocation'];
|
|
|
|
|
$muxFilename = basename($sshConfig['muxFilename']);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$closeCommand = "ssh -O exit -o ControlPath=/var/www/html/storage/app/ssh/mux/{$muxFilename} {$server->user}@{$server->ip}";
|
|
|
|
|
Process::run($closeCommand);
|
|
|
|
@@ -441,13 +415,15 @@ function remove_mux_and_private_key(Server $server)
|
|
|
|
|
Storage::disk('ssh-mux')->delete($muxFilename);
|
|
|
|
|
Storage::disk('ssh-keys')->delete($privateKeyLocation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function refresh_server_connection(?PrivateKey $private_key = null)
|
|
|
|
|
{
|
|
|
|
|
if (is_null($private_key)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
foreach ($private_key->servers as $server) {
|
|
|
|
|
$muxFilename = $server->muxFilename();
|
|
|
|
|
$sshConfig = server_ssh_configuration($server);
|
|
|
|
|
$muxFilename = basename($sshConfig['muxFilename']);
|
|
|
|
|
$closeCommand = "ssh -O exit -o ControlPath=/var/www/html/storage/app/ssh/mux/{$muxFilename} {$server->user}@{$server->ip}";
|
|
|
|
|
Process::run($closeCommand);
|
|
|
|
|
Storage::disk('ssh-mux')->delete($muxFilename);
|
|
|
|
|