@@ -24,28 +24,23 @@ public static function serverSshConfiguration(Server $server)
24
24
public static function ensureMultiplexedConnection (Server $ server )
25
25
{
26
26
if (! self ::isMultiplexingEnabled ()) {
27
- // ray('SSH Multiplexing: DISABLED')->red();
28
27
return ;
29
28
}
30
29
31
- // ray('SSH Multiplexing: ENABLED')->green();
32
- // ray('Ensuring multiplexed connection for server:', $server);
33
-
34
30
$ sshConfig = self ::serverSshConfiguration ($ server );
35
31
$ muxSocket = $ sshConfig ['muxFilename ' ];
36
32
$ sshKeyLocation = $ sshConfig ['sshKeyLocation ' ];
37
33
38
34
self ::validateSshKey ($ sshKeyLocation );
39
35
40
36
$ 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
+ }
41
40
$ process = Process::run ($ checkCommand );
42
41
43
42
if ($ process ->exitCode () !== 0 ) {
44
- // ray('SSH Multiplexing: Existing connection check failed or not found')->orange();
45
- // ray('Establishing new connection');
46
43
self ::establishNewMultiplexedConnection ($ server );
47
- } else {
48
- // ray('SSH Multiplexing: Existing connection is valid')->green();
49
44
}
50
45
}
51
46
@@ -55,10 +50,6 @@ public static function establishNewMultiplexedConnection(Server $server)
55
50
$ sshKeyLocation = $ sshConfig ['sshKeyLocation ' ];
56
51
$ muxSocket = $ sshConfig ['muxFilename ' ];
57
52
58
- // ray('Establishing new multiplexed connection')->blue();
59
- // ray('SSH Key Location:', $sshKeyLocation);
60
- // ray('Mux Socket:', $muxSocket);
61
-
62
53
$ connectionTimeout = config ('constants.ssh.connection_timeout ' );
63
54
$ serverInterval = config ('constants.ssh.server_interval ' );
64
55
$ muxPersistTime = config ('constants.ssh.mux_persist_time ' );
@@ -67,25 +58,15 @@ public static function establishNewMultiplexedConnection(Server $server)
67
58
.self ::getCommonSshOptions ($ server , $ sshKeyLocation , $ connectionTimeout , $ serverInterval )
68
59
."{$ server ->user }@ {$ server ->ip }" ;
69
60
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
+ }
71
64
72
65
$ establishProcess = Process::run ($ establishCommand );
73
66
74
- // ray('Establish Process Exit Code:', $establishProcess->exitCode());
75
- // ray('Establish Process Output:', $establishProcess->output());
76
- // ray('Establish Process Error Output:', $establishProcess->errorOutput());
77
-
78
67
if ($ establishProcess ->exitCode () !== 0 ) {
79
- // ray('Failed to establish multiplexed connection')->red();
80
68
throw new \RuntimeException ('Failed to establish multiplexed connection: ' .$ establishProcess ->errorOutput ());
81
69
}
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
- }
89
70
}
90
71
91
72
public static function removeMuxFile (Server $ server )
@@ -94,19 +75,10 @@ public static function removeMuxFile(Server $server)
94
75
$ muxSocket = $ sshConfig ['muxFilename ' ];
95
76
96
77
$ 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 ;
109
80
}
81
+ Process::run ($ closeCommand );
110
82
}
111
83
112
84
public static function generateScpCommand (Server $ server , string $ source , string $ dest )
@@ -116,16 +88,18 @@ public static function generateScpCommand(Server $server, string $source, string
116
88
$ muxSocket = $ sshConfig ['muxFilename ' ];
117
89
118
90
$ timeout = config ('constants.ssh.command_timeout ' );
91
+ $ muxPersistTime = config ('constants.ssh.mux_persist_time ' );
119
92
120
93
$ scp_command = "timeout $ timeout scp " ;
121
94
122
95
if (self ::isMultiplexingEnabled ()) {
123
- $ muxPersistTime = config ('constants.ssh.mux_persist_time ' );
124
96
$ scp_command .= "-o ControlMaster=auto -o ControlPath= $ muxSocket -o ControlPersist= {$ muxPersistTime } " ;
125
97
self ::ensureMultiplexedConnection ($ server );
126
98
}
127
99
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
+ }
129
103
130
104
$ scp_command .= self ::getCommonSshOptions ($ server , $ sshKeyLocation , config ('constants.ssh.connection_timeout ' ), config ('constants.ssh.server_interval ' ), isScp: true );
131
105
$ scp_command .= "{$ source } {$ server ->user }@ {$ server ->ip }: {$ dest }" ;
@@ -144,16 +118,18 @@ public static function generateSshCommand(Server $server, string $command)
144
118
$ muxSocket = $ sshConfig ['muxFilename ' ];
145
119
146
120
$ timeout = config ('constants.ssh.command_timeout ' );
121
+ $ muxPersistTime = config ('constants.ssh.mux_persist_time ' );
147
122
148
123
$ ssh_command = "timeout $ timeout ssh " ;
149
124
150
125
if (self ::isMultiplexingEnabled ()) {
151
- $ muxPersistTime = config ('constants.ssh.mux_persist_time ' );
152
126
$ ssh_command .= "-o ControlMaster=auto -o ControlPath= $ muxSocket -o ControlPersist= {$ muxPersistTime } " ;
153
127
self ::ensureMultiplexedConnection ($ server );
154
128
}
155
129
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
+ }
157
133
158
134
$ ssh_command .= self ::getCommonSshOptions ($ server , $ sshKeyLocation , config ('constants.ssh.connection_timeout ' ), config ('constants.ssh.server_interval ' ));
159
135
@@ -183,13 +159,6 @@ private static function validateSshKey(string $sshKeyLocation): void
183
159
}
184
160
}
185
161
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
-
193
162
private static function getCommonSshOptions (Server $ server , string $ sshKeyLocation , int $ connectionTimeout , int $ serverInterval , bool $ isScp = false ): string
194
163
{
195
164
$ options = "-i {$ sshKeyLocation } "
0 commit comments