Skip to content

Commit 12a0d71

Browse files
authored
Merge pull request coollabsio#3543 from peaklabs-dev/cf-production-ready
Feat: Make cloudflare production ready
2 parents 6dd3adb + 5ed7ae3 commit 12a0d71

File tree

3 files changed

+19
-50
lines changed

3 files changed

+19
-50
lines changed

app/Helpers/SshMultiplexingHelper.php

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,23 @@ public static function serverSshConfiguration(Server $server)
2424
public static function ensureMultiplexedConnection(Server $server)
2525
{
2626
if (! self::isMultiplexingEnabled()) {
27-
// ray('SSH Multiplexing: DISABLED')->red();
2827
return;
2928
}
3029

31-
// ray('SSH Multiplexing: ENABLED')->green();
32-
// ray('Ensuring multiplexed connection for server:', $server);
33-
3430
$sshConfig = self::serverSshConfiguration($server);
3531
$muxSocket = $sshConfig['muxFilename'];
3632
$sshKeyLocation = $sshConfig['sshKeyLocation'];
3733

3834
self::validateSshKey($sshKeyLocation);
3935

4036
$checkCommand = "ssh -O check -o ControlPath=$muxSocket {$server->user}@{$server->ip}";
37+
if (data_get($server, 'settings.is_cloudflare_tunnel')) {
38+
$checkCommand = 'cloudflared access ssh --hostname %h -O check -o ControlPath=' . $muxSocket . ' ' . $server->user . '@' . $server->ip;
39+
}
4140
$process = Process::run($checkCommand);
4241

4342
if ($process->exitCode() !== 0) {
44-
// ray('SSH Multiplexing: Existing connection check failed or not found')->orange();
45-
// ray('Establishing new connection');
4643
self::establishNewMultiplexedConnection($server);
47-
} else {
48-
// ray('SSH Multiplexing: Existing connection is valid')->green();
4944
}
5045
}
5146

@@ -55,10 +50,6 @@ public static function establishNewMultiplexedConnection(Server $server)
5550
$sshKeyLocation = $sshConfig['sshKeyLocation'];
5651
$muxSocket = $sshConfig['muxFilename'];
5752

58-
// ray('Establishing new multiplexed connection')->blue();
59-
// ray('SSH Key Location:', $sshKeyLocation);
60-
// ray('Mux Socket:', $muxSocket);
61-
6253
$connectionTimeout = config('constants.ssh.connection_timeout');
6354
$serverInterval = config('constants.ssh.server_interval');
6455
$muxPersistTime = config('constants.ssh.mux_persist_time');
@@ -67,25 +58,15 @@ public static function establishNewMultiplexedConnection(Server $server)
6758
.self::getCommonSshOptions($server, $sshKeyLocation, $connectionTimeout, $serverInterval)
6859
."{$server->user}@{$server->ip}";
6960

70-
// ray('Establish Command:', $establishCommand);
61+
if (data_get($server, 'settings.is_cloudflare_tunnel')) {
62+
$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;
63+
}
7164

7265
$establishProcess = Process::run($establishCommand);
7366

74-
// ray('Establish Process Exit Code:', $establishProcess->exitCode());
75-
// ray('Establish Process Output:', $establishProcess->output());
76-
// ray('Establish Process Error Output:', $establishProcess->errorOutput());
77-
7867
if ($establishProcess->exitCode() !== 0) {
79-
// ray('Failed to establish multiplexed connection')->red();
8068
throw new \RuntimeException('Failed to establish multiplexed connection: '.$establishProcess->errorOutput());
8169
}
82-
83-
// ray('Successfully established multiplexed connection')->green();
84-
85-
// Check if the mux socket file was created
86-
if (! file_exists($muxSocket)) {
87-
// ray('Mux socket file not found after connection establishment')->orange();
88-
}
8970
}
9071

9172
public static function removeMuxFile(Server $server)
@@ -94,19 +75,10 @@ public static function removeMuxFile(Server $server)
9475
$muxSocket = $sshConfig['muxFilename'];
9576

9677
$closeCommand = "ssh -O exit -o ControlPath=$muxSocket {$server->user}@{$server->ip}";
97-
$process = Process::run($closeCommand);
98-
99-
// ray('Closing multiplexed connection')->blue();
100-
// ray('Close command:', $closeCommand);
101-
// ray('Close process exit code:', $process->exitCode());
102-
// ray('Close process output:', $process->output());
103-
// ray('Close process error output:', $process->errorOutput());
104-
105-
if ($process->exitCode() !== 0) {
106-
// ray('Failed to close multiplexed connection')->orange();
107-
} else {
108-
// ray('Successfully closed multiplexed connection')->green();
78+
if (data_get($server, 'settings.is_cloudflare_tunnel')) {
79+
$closeCommand = 'cloudflared access ssh --hostname %h -O exit -o ControlPath=' . $muxSocket . ' ' . $server->user . '@' . $server->ip;
10980
}
81+
Process::run($closeCommand);
11082
}
11183

11284
public static function generateScpCommand(Server $server, string $source, string $dest)
@@ -116,16 +88,18 @@ public static function generateScpCommand(Server $server, string $source, string
11688
$muxSocket = $sshConfig['muxFilename'];
11789

11890
$timeout = config('constants.ssh.command_timeout');
91+
$muxPersistTime = config('constants.ssh.mux_persist_time');
11992

12093
$scp_command = "timeout $timeout scp ";
12194

12295
if (self::isMultiplexingEnabled()) {
123-
$muxPersistTime = config('constants.ssh.mux_persist_time');
12496
$scp_command .= "-o ControlMaster=auto -o ControlPath=$muxSocket -o ControlPersist={$muxPersistTime} ";
12597
self::ensureMultiplexedConnection($server);
12698
}
12799

128-
self::addCloudflareProxyCommand($scp_command, $server);
100+
if (data_get($server, 'settings.is_cloudflare_tunnel')) {
101+
$scp_command = 'timeout ' . $timeout . ' cloudflared access ssh --hostname %h -o ControlMaster=auto -o ControlPath=' . $muxSocket . ' -o ControlPersist=' . $muxPersistTime . ' ';
102+
}
129103

130104
$scp_command .= self::getCommonSshOptions($server, $sshKeyLocation, config('constants.ssh.connection_timeout'), config('constants.ssh.server_interval'), isScp: true);
131105
$scp_command .= "{$source} {$server->user}@{$server->ip}:{$dest}";
@@ -144,16 +118,18 @@ public static function generateSshCommand(Server $server, string $command)
144118
$muxSocket = $sshConfig['muxFilename'];
145119

146120
$timeout = config('constants.ssh.command_timeout');
121+
$muxPersistTime = config('constants.ssh.mux_persist_time');
147122

148123
$ssh_command = "timeout $timeout ssh ";
149124

150125
if (self::isMultiplexingEnabled()) {
151-
$muxPersistTime = config('constants.ssh.mux_persist_time');
152126
$ssh_command .= "-o ControlMaster=auto -o ControlPath=$muxSocket -o ControlPersist={$muxPersistTime} ";
153127
self::ensureMultiplexedConnection($server);
154128
}
155129

156-
self::addCloudflareProxyCommand($ssh_command, $server);
130+
if (data_get($server, 'settings.is_cloudflare_tunnel')) {
131+
$ssh_command = 'timeout ' . $timeout . ' cloudflared access ssh --hostname %h -o ControlMaster=auto -o ControlPath=' . $muxSocket . ' -o ControlPersist=' . $muxPersistTime . ' ';
132+
}
157133

158134
$ssh_command .= self::getCommonSshOptions($server, $sshKeyLocation, config('constants.ssh.connection_timeout'), config('constants.ssh.server_interval'));
159135

@@ -183,13 +159,6 @@ private static function validateSshKey(string $sshKeyLocation): void
183159
}
184160
}
185161

186-
private static function addCloudflareProxyCommand(string &$command, Server $server): void
187-
{
188-
if (data_get($server, 'settings.is_cloudflare_tunnel')) {
189-
$command .= '-o ProxyCommand="/usr/local/bin/cloudflared access ssh --hostname %h" ';
190-
}
191-
}
192-
193162
private static function getCommonSshOptions(Server $server, string $sshKeyLocation, int $connectionTimeout, int $serverInterval, bool $isScp = false): string
194163
{
195164
$options = "-i {$sshKeyLocation} "
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<form wire:submit.prevent='submit' class="flex flex-col w-full gap-2">
22
<x-forms.input id="cloudflare_token" required label="Cloudflare Token" />
33
<x-forms.input id="ssh_domain" label="Configured SSH Domain" required
4-
helper="The SSH Domain you configured in Cloudflare" />
4+
helper="The SSH Domain you configured in Cloudflare. Make sure there is no protocol like http(s):// so you provide a FQDN not a URL." />
55
<x-forms.button type="submit" isHighlighted @click="modalOpen=false">Automated Configuration</x-forms.button>
66
</form>

resources/views/livewire/server/form.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class="mt-8 mb-4 w-full font-bold box-without-bg bg-coollabs hover:bg-coollabs-1
6868
</div>
6969
<div class="flex flex-col gap-2 w-full lg:flex-row">
7070
<x-forms.input type="password" id="server.ip" label="IP Address/Domain"
71-
helper="An IP Address (127.0.0.1) or domain (example.com)." required />
71+
helper="An IP Address (127.0.0.1) or domain (example.com). Make sure there is no protocol like http(s):// so you provide a FQDN not a URL." required />
7272
<div class="flex gap-2">
7373
<x-forms.input id="server.user" label="User" required />
7474
<x-forms.input type="number" id="server.port" label="Port" required />

0 commit comments

Comments
 (0)