From 1bb192f3e216720db8dc1578988332b1ecfcd6cb Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Mon, 23 Sep 2024 19:23:46 +0200 Subject: [PATCH 1/3] Fix: Cloudflare tunnel --- app/Helpers/SshMultiplexingHelper.php | 80 +++++++++++++++------------ 1 file changed, 46 insertions(+), 34 deletions(-) diff --git a/app/Helpers/SshMultiplexingHelper.php b/app/Helpers/SshMultiplexingHelper.php index 71be77506..ca9035579 100644 --- a/app/Helpers/SshMultiplexingHelper.php +++ b/app/Helpers/SshMultiplexingHelper.php @@ -24,12 +24,12 @@ class SshMultiplexingHelper public static function ensureMultiplexedConnection(Server $server) { if (! self::isMultiplexingEnabled()) { - // ray('SSH Multiplexing: DISABLED')->red(); + ray('SSH Multiplexing: DISABLED')->red(); return; } - // ray('SSH Multiplexing: ENABLED')->green(); - // ray('Ensuring multiplexed connection for server:', $server); + ray('SSH Multiplexing: ENABLED')->green(); + ray('Ensuring multiplexed connection for server:', $server); $sshConfig = self::serverSshConfiguration($server); $muxSocket = $sshConfig['muxFilename']; @@ -38,14 +38,18 @@ class SshMultiplexingHelper self::validateSshKey($sshKeyLocation); $checkCommand = "ssh -O check -o ControlPath=$muxSocket {$server->user}@{$server->ip}"; + if (data_get($server, 'settings.is_cloudflare_tunnel')) { + $checkCommand = 'cloudflared access ssh --hostname %h -O check -o ControlPath=' . $muxSocket . ' ' . $server->user . '@' . $server->ip; + } + ray('Check Command:', $checkCommand); $process = Process::run($checkCommand); if ($process->exitCode() !== 0) { - // ray('SSH Multiplexing: Existing connection check failed or not found')->orange(); - // ray('Establishing new connection'); + ray('SSH Multiplexing: Existing connection check failed or not found')->orange(); + ray('Establishing new connection'); self::establishNewMultiplexedConnection($server); } else { - // ray('SSH Multiplexing: Existing connection is valid')->green(); + ray('SSH Multiplexing: Existing connection is valid')->green(); } } @@ -55,9 +59,9 @@ class SshMultiplexingHelper $sshKeyLocation = $sshConfig['sshKeyLocation']; $muxSocket = $sshConfig['muxFilename']; - // ray('Establishing new multiplexed connection')->blue(); - // ray('SSH Key Location:', $sshKeyLocation); - // ray('Mux Socket:', $muxSocket); + ray('Establishing new multiplexed connection')->blue(); + ray('SSH Key Location:', $sshKeyLocation); + ray('Mux Socket:', $muxSocket); $connectionTimeout = config('constants.ssh.connection_timeout'); $serverInterval = config('constants.ssh.server_interval'); @@ -67,24 +71,28 @@ class SshMultiplexingHelper .self::getCommonSshOptions($server, $sshKeyLocation, $connectionTimeout, $serverInterval) ."{$server->user}@{$server->ip}"; - // ray('Establish Command:', $establishCommand); + if (data_get($server, 'settings.is_cloudflare_tunnel')) { + $establishCommand = 'cloudflared access ssh --hostname %h -fNM -o ControlMaster=auto -o ControlPath=' . $muxSocket . ' -o ControlPersist=' . $muxPersistTime . ' ' . self::getCommonSshOptions($server, $sshKeyLocation, $connectionTimeout, $serverInterval) . $server->user . '@' . $server->ip; + } + + ray('Establish Command:', $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()); + ray('Establish Process Exit Code:', $establishProcess->exitCode()); + ray('Establish Process Output:', $establishProcess->output()); + ray('Establish Process Error Output:', $establishProcess->errorOutput()); if ($establishProcess->exitCode() !== 0) { - // ray('Failed to establish multiplexed connection')->red(); + ray('Failed to establish multiplexed connection')->red(); throw new \RuntimeException('Failed to establish multiplexed connection: '.$establishProcess->errorOutput()); } - // ray('Successfully established multiplexed connection')->green(); + ray('Successfully established multiplexed connection')->green(); // Check if the mux socket file was created if (! file_exists($muxSocket)) { - // ray('Mux socket file not found after connection establishment')->orange(); + ray('Mux socket file not found after connection establishment')->orange(); } } @@ -94,18 +102,21 @@ class SshMultiplexingHelper $muxSocket = $sshConfig['muxFilename']; $closeCommand = "ssh -O exit -o ControlPath=$muxSocket {$server->user}@{$server->ip}"; + if (data_get($server, 'settings.is_cloudflare_tunnel')) { + $closeCommand = 'cloudflared access ssh --hostname %h -O exit -o ControlPath=' . $muxSocket . ' ' . $server->user . '@' . $server->ip; + } $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()); + 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(); + ray('Failed to close multiplexed connection')->orange(); } else { - // ray('Successfully closed multiplexed connection')->green(); + ray('Successfully closed multiplexed connection')->green(); } } @@ -116,20 +127,24 @@ class SshMultiplexingHelper $muxSocket = $sshConfig['muxFilename']; $timeout = config('constants.ssh.command_timeout'); + $muxPersistTime = config('constants.ssh.mux_persist_time'); $scp_command = "timeout $timeout scp "; if (self::isMultiplexingEnabled()) { - $muxPersistTime = config('constants.ssh.mux_persist_time'); $scp_command .= "-o ControlMaster=auto -o ControlPath=$muxSocket -o ControlPersist={$muxPersistTime} "; self::ensureMultiplexedConnection($server); } - self::addCloudflareProxyCommand($scp_command, $server); + if (data_get($server, 'settings.is_cloudflare_tunnel')) { + $scp_command = 'timeout ' . $timeout . ' cloudflared access ssh --hostname %h -o ControlMaster=auto -o ControlPath=' . $muxSocket . ' -o ControlPersist=' . $muxPersistTime . ' '; + } $scp_command .= self::getCommonSshOptions($server, $sshKeyLocation, config('constants.ssh.connection_timeout'), config('constants.ssh.server_interval'), isScp: true); $scp_command .= "{$source} {$server->user}@{$server->ip}:{$dest}"; + ray('SCP Command:', $scp_command); + return $scp_command; } @@ -144,16 +159,18 @@ class SshMultiplexingHelper $muxSocket = $sshConfig['muxFilename']; $timeout = config('constants.ssh.command_timeout'); + $muxPersistTime = config('constants.ssh.mux_persist_time'); $ssh_command = "timeout $timeout ssh "; if (self::isMultiplexingEnabled()) { - $muxPersistTime = config('constants.ssh.mux_persist_time'); $ssh_command .= "-o ControlMaster=auto -o ControlPath=$muxSocket -o ControlPersist={$muxPersistTime} "; self::ensureMultiplexedConnection($server); } - self::addCloudflareProxyCommand($ssh_command, $server); + if (data_get($server, 'settings.is_cloudflare_tunnel')) { + $ssh_command = 'timeout ' . $timeout . ' cloudflared access ssh --hostname %h -o ControlMaster=auto -o ControlPath=' . $muxSocket . ' -o ControlPersist=' . $muxPersistTime . ' '; + } $ssh_command .= self::getCommonSshOptions($server, $sshKeyLocation, config('constants.ssh.connection_timeout'), config('constants.ssh.server_interval')); @@ -165,6 +182,8 @@ class SshMultiplexingHelper .$command.PHP_EOL .$delimiter; + ray('SSH Command:', $ssh_command); + return $ssh_command; } @@ -183,13 +202,6 @@ class SshMultiplexingHelper } } - private static function addCloudflareProxyCommand(string &$command, Server $server): void - { - if (data_get($server, 'settings.is_cloudflare_tunnel')) { - $command .= '-o ProxyCommand="/usr/local/bin/cloudflared access ssh --hostname %h" '; - } - } - private static function getCommonSshOptions(Server $server, string $sshKeyLocation, int $connectionTimeout, int $serverInterval, bool $isScp = false): string { $options = "-i {$sshKeyLocation} " From 6934a746f75fcd66f0f33606dc9d0ebe7bc60ebd Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Mon, 23 Sep 2024 19:57:42 +0200 Subject: [PATCH 2/3] Fix: Make helper text more clean to use a FQDN and not an URL --- .../livewire/server/configure-cloudflare-tunnels.blade.php | 2 +- resources/views/livewire/server/form.blade.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/resources/views/livewire/server/configure-cloudflare-tunnels.blade.php b/resources/views/livewire/server/configure-cloudflare-tunnels.blade.php index 509d57d1f..c340a558b 100644 --- a/resources/views/livewire/server/configure-cloudflare-tunnels.blade.php +++ b/resources/views/livewire/server/configure-cloudflare-tunnels.blade.php @@ -1,6 +1,6 @@
diff --git a/resources/views/livewire/server/form.blade.php b/resources/views/livewire/server/form.blade.php index ad3a34d33..4be803a95 100644 --- a/resources/views/livewire/server/form.blade.php +++ b/resources/views/livewire/server/form.blade.php @@ -68,7 +68,7 @@