Skip to content

Commit 1bb192f

Browse files
committed
Fix: Cloudflare tunnel
1 parent e039f18 commit 1bb192f

File tree

1 file changed

+46
-34
lines changed

1 file changed

+46
-34
lines changed

app/Helpers/SshMultiplexingHelper.php

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ public static function serverSshConfiguration(Server $server)
2424
public static function ensureMultiplexedConnection(Server $server)
2525
{
2626
if (! self::isMultiplexingEnabled()) {
27-
// ray('SSH Multiplexing: DISABLED')->red();
27+
ray('SSH Multiplexing: DISABLED')->red();
2828
return;
2929
}
3030

31-
// ray('SSH Multiplexing: ENABLED')->green();
32-
// ray('Ensuring multiplexed connection for server:', $server);
31+
ray('SSH Multiplexing: ENABLED')->green();
32+
ray('Ensuring multiplexed connection for server:', $server);
3333

3434
$sshConfig = self::serverSshConfiguration($server);
3535
$muxSocket = $sshConfig['muxFilename'];
@@ -38,14 +38,18 @@ public static function ensureMultiplexedConnection(Server $server)
3838
self::validateSshKey($sshKeyLocation);
3939

4040
$checkCommand = "ssh -O check -o ControlPath=$muxSocket {$server->user}@{$server->ip}";
41+
if (data_get($server, 'settings.is_cloudflare_tunnel')) {
42+
$checkCommand = 'cloudflared access ssh --hostname %h -O check -o ControlPath=' . $muxSocket . ' ' . $server->user . '@' . $server->ip;
43+
}
44+
ray('Check Command:', $checkCommand);
4145
$process = Process::run($checkCommand);
4246

4347
if ($process->exitCode() !== 0) {
44-
// ray('SSH Multiplexing: Existing connection check failed or not found')->orange();
45-
// ray('Establishing new connection');
48+
ray('SSH Multiplexing: Existing connection check failed or not found')->orange();
49+
ray('Establishing new connection');
4650
self::establishNewMultiplexedConnection($server);
4751
} else {
48-
// ray('SSH Multiplexing: Existing connection is valid')->green();
52+
ray('SSH Multiplexing: Existing connection is valid')->green();
4953
}
5054
}
5155

@@ -55,9 +59,9 @@ public static function establishNewMultiplexedConnection(Server $server)
5559
$sshKeyLocation = $sshConfig['sshKeyLocation'];
5660
$muxSocket = $sshConfig['muxFilename'];
5761

58-
// ray('Establishing new multiplexed connection')->blue();
59-
// ray('SSH Key Location:', $sshKeyLocation);
60-
// ray('Mux Socket:', $muxSocket);
62+
ray('Establishing new multiplexed connection')->blue();
63+
ray('SSH Key Location:', $sshKeyLocation);
64+
ray('Mux Socket:', $muxSocket);
6165

6266
$connectionTimeout = config('constants.ssh.connection_timeout');
6367
$serverInterval = config('constants.ssh.server_interval');
@@ -67,24 +71,28 @@ public static function establishNewMultiplexedConnection(Server $server)
6771
.self::getCommonSshOptions($server, $sshKeyLocation, $connectionTimeout, $serverInterval)
6872
."{$server->user}@{$server->ip}";
6973

70-
// ray('Establish Command:', $establishCommand);
74+
if (data_get($server, 'settings.is_cloudflare_tunnel')) {
75+
$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;
76+
}
77+
78+
ray('Establish Command:', $establishCommand);
7179

7280
$establishProcess = Process::run($establishCommand);
7381

74-
// ray('Establish Process Exit Code:', $establishProcess->exitCode());
75-
// ray('Establish Process Output:', $establishProcess->output());
76-
// ray('Establish Process Error Output:', $establishProcess->errorOutput());
82+
ray('Establish Process Exit Code:', $establishProcess->exitCode());
83+
ray('Establish Process Output:', $establishProcess->output());
84+
ray('Establish Process Error Output:', $establishProcess->errorOutput());
7785

7886
if ($establishProcess->exitCode() !== 0) {
79-
// ray('Failed to establish multiplexed connection')->red();
87+
ray('Failed to establish multiplexed connection')->red();
8088
throw new \RuntimeException('Failed to establish multiplexed connection: '.$establishProcess->errorOutput());
8189
}
8290

83-
// ray('Successfully established multiplexed connection')->green();
91+
ray('Successfully established multiplexed connection')->green();
8492

8593
// Check if the mux socket file was created
8694
if (! file_exists($muxSocket)) {
87-
// ray('Mux socket file not found after connection establishment')->orange();
95+
ray('Mux socket file not found after connection establishment')->orange();
8896
}
8997
}
9098

@@ -94,18 +102,21 @@ public static function removeMuxFile(Server $server)
94102
$muxSocket = $sshConfig['muxFilename'];
95103

96104
$closeCommand = "ssh -O exit -o ControlPath=$muxSocket {$server->user}@{$server->ip}";
105+
if (data_get($server, 'settings.is_cloudflare_tunnel')) {
106+
$closeCommand = 'cloudflared access ssh --hostname %h -O exit -o ControlPath=' . $muxSocket . ' ' . $server->user . '@' . $server->ip;
107+
}
97108
$process = Process::run($closeCommand);
98109

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());
110+
ray('Closing multiplexed connection')->blue();
111+
ray('Close command:', $closeCommand);
112+
ray('Close process exit code:', $process->exitCode());
113+
ray('Close process output:', $process->output());
114+
ray('Close process error output:', $process->errorOutput());
104115

105116
if ($process->exitCode() !== 0) {
106-
// ray('Failed to close multiplexed connection')->orange();
117+
ray('Failed to close multiplexed connection')->orange();
107118
} else {
108-
// ray('Successfully closed multiplexed connection')->green();
119+
ray('Successfully closed multiplexed connection')->green();
109120
}
110121
}
111122

@@ -116,20 +127,24 @@ public static function generateScpCommand(Server $server, string $source, string
116127
$muxSocket = $sshConfig['muxFilename'];
117128

118129
$timeout = config('constants.ssh.command_timeout');
130+
$muxPersistTime = config('constants.ssh.mux_persist_time');
119131

120132
$scp_command = "timeout $timeout scp ";
121133

122134
if (self::isMultiplexingEnabled()) {
123-
$muxPersistTime = config('constants.ssh.mux_persist_time');
124135
$scp_command .= "-o ControlMaster=auto -o ControlPath=$muxSocket -o ControlPersist={$muxPersistTime} ";
125136
self::ensureMultiplexedConnection($server);
126137
}
127138

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

130143
$scp_command .= self::getCommonSshOptions($server, $sshKeyLocation, config('constants.ssh.connection_timeout'), config('constants.ssh.server_interval'), isScp: true);
131144
$scp_command .= "{$source} {$server->user}@{$server->ip}:{$dest}";
132145

146+
ray('SCP Command:', $scp_command);
147+
133148
return $scp_command;
134149
}
135150

@@ -144,16 +159,18 @@ public static function generateSshCommand(Server $server, string $command)
144159
$muxSocket = $sshConfig['muxFilename'];
145160

146161
$timeout = config('constants.ssh.command_timeout');
162+
$muxPersistTime = config('constants.ssh.mux_persist_time');
147163

148164
$ssh_command = "timeout $timeout ssh ";
149165

150166
if (self::isMultiplexingEnabled()) {
151-
$muxPersistTime = config('constants.ssh.mux_persist_time');
152167
$ssh_command .= "-o ControlMaster=auto -o ControlPath=$muxSocket -o ControlPersist={$muxPersistTime} ";
153168
self::ensureMultiplexedConnection($server);
154169
}
155170

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

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

@@ -165,6 +182,8 @@ public static function generateSshCommand(Server $server, string $command)
165182
.$command.PHP_EOL
166183
.$delimiter;
167184

185+
ray('SSH Command:', $ssh_command);
186+
168187
return $ssh_command;
169188
}
170189

@@ -183,13 +202,6 @@ private static function validateSshKey(string $sshKeyLocation): void
183202
}
184203
}
185204

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-
193205
private static function getCommonSshOptions(Server $server, string $sshKeyLocation, int $connectionTimeout, int $serverInterval, bool $isScp = false): string
194206
{
195207
$options = "-i {$sshKeyLocation} "

0 commit comments

Comments
 (0)