1use crate::{
2 RemoteClientDelegate, RemotePlatform,
3 remote_client::{CommandTemplate, RemoteConnection, RemoteConnectionOptions},
4};
5use anyhow::{Context as _, Result, anyhow};
6use async_trait::async_trait;
7use collections::HashMap;
8use futures::{
9 AsyncReadExt as _, FutureExt as _,
10 channel::mpsc::{Sender, UnboundedReceiver, UnboundedSender},
11 select_biased,
12};
13use gpui::{App, AppContext as _, AsyncApp, SemanticVersion, Task};
14use parking_lot::Mutex;
15use paths::remote_server_dir_relative;
16use release_channel::{AppCommitSha, AppVersion, ReleaseChannel};
17use rpc::proto::Envelope;
18pub use settings::SshPortForwardOption;
19use smol::{
20 fs,
21 process::{self, Child, Stdio},
22};
23use std::{
24 path::{Path, PathBuf},
25 sync::Arc,
26 time::Instant,
27};
28use tempfile::TempDir;
29use util::{
30 paths::{PathStyle, RemotePathBuf},
31 rel_path::RelPath,
32 shell::ShellKind,
33};
34
35pub(crate) struct SshRemoteConnection {
36 socket: SshSocket,
37 master_process: Mutex<Option<MasterProcess>>,
38 remote_binary_path: Option<Arc<RelPath>>,
39 ssh_platform: RemotePlatform,
40 ssh_path_style: PathStyle,
41 ssh_shell: String,
42 ssh_shell_kind: ShellKind,
43 ssh_default_system_shell: String,
44 _temp_dir: TempDir,
45}
46
47#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]
48pub struct SshConnectionOptions {
49 pub host: String,
50 pub username: Option<String>,
51 pub port: Option<u16>,
52 pub password: Option<String>,
53 pub args: Option<Vec<String>>,
54 pub port_forwards: Option<Vec<SshPortForwardOption>>,
55
56 pub nickname: Option<String>,
57 pub upload_binary_over_ssh: bool,
58}
59
60impl From<settings::SshConnection> for SshConnectionOptions {
61 fn from(val: settings::SshConnection) -> Self {
62 SshConnectionOptions {
63 host: val.host.into(),
64 username: val.username,
65 port: val.port,
66 password: None,
67 args: Some(val.args),
68 nickname: val.nickname,
69 upload_binary_over_ssh: val.upload_binary_over_ssh.unwrap_or_default(),
70 port_forwards: val.port_forwards,
71 }
72 }
73}
74
75struct SshSocket {
76 connection_options: SshConnectionOptions,
77 #[cfg(not(target_os = "windows"))]
78 socket_path: std::path::PathBuf,
79 envs: HashMap<String, String>,
80 #[cfg(target_os = "windows")]
81 _proxy: askpass::PasswordProxy,
82}
83
84struct MasterProcess {
85 process: Child,
86}
87
88#[cfg(not(target_os = "windows"))]
89impl MasterProcess {
90 pub fn new(
91 askpass_script_path: &std::ffi::OsStr,
92 additional_args: Vec<String>,
93 socket_path: &std::path::Path,
94 url: &str,
95 ) -> Result<Self> {
96 let args = [
97 "-N",
98 "-o",
99 "ControlPersist=no",
100 "-o",
101 "ControlMaster=yes",
102 "-o",
103 ];
104
105 let mut master_process = util::command::new_smol_command("ssh");
106 master_process
107 .kill_on_drop(true)
108 .stdin(Stdio::null())
109 .stdout(Stdio::piped())
110 .stderr(Stdio::piped())
111 .env("SSH_ASKPASS_REQUIRE", "force")
112 .env("SSH_ASKPASS", askpass_script_path)
113 .args(additional_args)
114 .args(args);
115
116 master_process.arg(format!("ControlPath={}", socket_path.display()));
117
118 let process = master_process.arg(&url).spawn()?;
119
120 Ok(MasterProcess { process })
121 }
122
123 pub async fn wait_connected(&mut self) -> Result<()> {
124 let Some(mut stdout) = self.process.stdout.take() else {
125 anyhow::bail!("ssh process stdout capture failed");
126 };
127
128 let mut output = Vec::new();
129 stdout.read_to_end(&mut output).await?;
130 Ok(())
131 }
132}
133
134#[cfg(target_os = "windows")]
135impl MasterProcess {
136 const CONNECTION_ESTABLISHED_MAGIC: &str = "ZED_SSH_CONNECTION_ESTABLISHED";
137
138 pub fn new(
139 askpass_script_path: &std::ffi::OsStr,
140 additional_args: Vec<String>,
141 url: &str,
142 ) -> Result<Self> {
143 // On Windows, `ControlMaster` and `ControlPath` are not supported:
144 // https://github.com/PowerShell/Win32-OpenSSH/issues/405
145 // https://github.com/PowerShell/Win32-OpenSSH/wiki/Project-Scope
146 //
147 // Using an ugly workaround to detect connection establishment
148 // -N doesn't work with JumpHosts as windows openssh never closes stdin in that case
149 let args = [
150 "-t",
151 &format!("echo '{}'; exec $0", Self::CONNECTION_ESTABLISHED_MAGIC),
152 ];
153
154 let mut master_process = util::command::new_smol_command("ssh");
155 master_process
156 .kill_on_drop(true)
157 .stdin(Stdio::null())
158 .stdout(Stdio::piped())
159 .stderr(Stdio::piped())
160 .env("SSH_ASKPASS_REQUIRE", "force")
161 .env("SSH_ASKPASS", askpass_script_path)
162 .args(additional_args)
163 .arg(url)
164 .args(args);
165
166 let process = master_process.spawn()?;
167
168 Ok(MasterProcess { process })
169 }
170
171 pub async fn wait_connected(&mut self) -> Result<()> {
172 use smol::io::AsyncBufReadExt;
173
174 let Some(stdout) = self.process.stdout.take() else {
175 anyhow::bail!("ssh process stdout capture failed");
176 };
177
178 let mut reader = smol::io::BufReader::new(stdout);
179
180 let mut line = String::new();
181
182 loop {
183 let n = reader.read_line(&mut line).await?;
184 if n == 0 {
185 anyhow::bail!("ssh process exited before connection established");
186 }
187
188 if line.contains(Self::CONNECTION_ESTABLISHED_MAGIC) {
189 return Ok(());
190 }
191 }
192 }
193}
194
195impl AsRef<Child> for MasterProcess {
196 fn as_ref(&self) -> &Child {
197 &self.process
198 }
199}
200
201impl AsMut<Child> for MasterProcess {
202 fn as_mut(&mut self) -> &mut Child {
203 &mut self.process
204 }
205}
206
207#[async_trait(?Send)]
208impl RemoteConnection for SshRemoteConnection {
209 async fn kill(&self) -> Result<()> {
210 let Some(mut process) = self.master_process.lock().take() else {
211 return Ok(());
212 };
213 process.as_mut().kill().ok();
214 process.as_mut().status().await?;
215 Ok(())
216 }
217
218 fn has_been_killed(&self) -> bool {
219 self.master_process.lock().is_none()
220 }
221
222 fn connection_options(&self) -> RemoteConnectionOptions {
223 RemoteConnectionOptions::Ssh(self.socket.connection_options.clone())
224 }
225
226 fn shell(&self) -> String {
227 self.ssh_shell.clone()
228 }
229
230 fn default_system_shell(&self) -> String {
231 self.ssh_default_system_shell.clone()
232 }
233
234 fn build_command(
235 &self,
236 input_program: Option<String>,
237 input_args: &[String],
238 input_env: &HashMap<String, String>,
239 working_dir: Option<String>,
240 port_forward: Option<(u16, String, u16)>,
241 ) -> Result<CommandTemplate> {
242 let Self {
243 ssh_path_style,
244 socket,
245 ssh_shell_kind,
246 ssh_shell,
247 ..
248 } = self;
249 let env = socket.envs.clone();
250 build_command(
251 input_program,
252 input_args,
253 input_env,
254 working_dir,
255 port_forward,
256 env,
257 *ssh_path_style,
258 ssh_shell,
259 *ssh_shell_kind,
260 socket.ssh_args(),
261 )
262 }
263
264 fn build_forward_ports_command(
265 &self,
266 forwards: Vec<(u16, String, u16)>,
267 ) -> Result<CommandTemplate> {
268 let Self { socket, .. } = self;
269 let mut args = socket.ssh_args();
270 args.push("-N".into());
271 for (local_port, host, remote_port) in forwards {
272 args.push("-L".into());
273 args.push(format!("{local_port}:{host}:{remote_port}"));
274 }
275 Ok(CommandTemplate {
276 program: "ssh".into(),
277 args,
278 env: Default::default(),
279 })
280 }
281
282 fn upload_directory(
283 &self,
284 src_path: PathBuf,
285 dest_path: RemotePathBuf,
286 cx: &App,
287 ) -> Task<Result<()>> {
288 let dest_path_str = dest_path.to_string();
289 let src_path_display = src_path.display().to_string();
290
291 let mut sftp_command = self.build_sftp_command();
292 let mut scp_command =
293 self.build_scp_command(&src_path, &dest_path_str, Some(&["-C", "-r"]));
294
295 cx.background_spawn(async move {
296 // We will try SFTP first, and if that fails, we will fall back to SCP.
297 // If SCP fails also, we give up and return an error.
298 // The reason we allow a fallback from SFTP to SCP is that if the user has to specify a password,
299 // depending on the implementation of SSH stack, SFTP may disable interactive password prompts in batch mode.
300 // This is for example the case on Windows as evidenced by this implementation snippet:
301 // https://github.com/PowerShell/openssh-portable/blob/b8c08ef9da9450a94a9c5ef717d96a7bd83f3332/sshconnect2.c#L417
302 if Self::is_sftp_available().await {
303 log::debug!("using SFTP for directory upload");
304 let mut child = sftp_command.spawn()?;
305 if let Some(mut stdin) = child.stdin.take() {
306 use futures::AsyncWriteExt;
307 let sftp_batch = format!("put -r {src_path_display} {dest_path_str}\n");
308 stdin.write_all(sftp_batch.as_bytes()).await?;
309 stdin.flush().await?;
310 }
311
312 let output = child.output().await?;
313 if output.status.success() {
314 return Ok(());
315 }
316
317 let stderr = String::from_utf8_lossy(&output.stderr);
318 log::debug!("failed to upload directory via SFTP {src_path_display} -> {dest_path_str}: {stderr}");
319 }
320
321 log::debug!("using SCP for directory upload");
322 let output = scp_command.output().await?;
323
324 if output.status.success() {
325 return Ok(());
326 }
327
328 let stderr = String::from_utf8_lossy(&output.stderr);
329 log::debug!("failed to upload directory via SCP {src_path_display} -> {dest_path_str}: {stderr}");
330
331 anyhow::bail!(
332 "failed to upload directory via SFTP/SCP {} -> {}: {}",
333 src_path_display,
334 dest_path_str,
335 stderr,
336 );
337 })
338 }
339
340 fn start_proxy(
341 &self,
342 unique_identifier: String,
343 reconnect: bool,
344 incoming_tx: UnboundedSender<Envelope>,
345 outgoing_rx: UnboundedReceiver<Envelope>,
346 connection_activity_tx: Sender<()>,
347 delegate: Arc<dyn RemoteClientDelegate>,
348 cx: &mut AsyncApp,
349 ) -> Task<Result<i32>> {
350 delegate.set_status(Some("Starting proxy"), cx);
351
352 let Some(remote_binary_path) = self.remote_binary_path.clone() else {
353 return Task::ready(Err(anyhow!("Remote binary path not set")));
354 };
355
356 let mut proxy_args = vec![];
357 for env_var in ["RUST_LOG", "RUST_BACKTRACE", "ZED_GENERATE_MINIDUMPS"] {
358 if let Some(value) = std::env::var(env_var).ok() {
359 proxy_args.push(format!("{}='{}'", env_var, value));
360 }
361 }
362 proxy_args.push(remote_binary_path.display(self.path_style()).into_owned());
363 proxy_args.push("proxy".to_owned());
364 proxy_args.push("--identifier".to_owned());
365 proxy_args.push(unique_identifier);
366
367 if reconnect {
368 proxy_args.push("--reconnect".to_owned());
369 }
370
371 let ssh_proxy_process = match self
372 .socket
373 .ssh_command(self.ssh_shell_kind, "env", &proxy_args, false)
374 // IMPORTANT: we kill this process when we drop the task that uses it.
375 .kill_on_drop(true)
376 .spawn()
377 {
378 Ok(process) => process,
379 Err(error) => {
380 return Task::ready(Err(anyhow!("failed to spawn remote server: {}", error)));
381 }
382 };
383
384 super::handle_rpc_messages_over_child_process_stdio(
385 ssh_proxy_process,
386 incoming_tx,
387 outgoing_rx,
388 connection_activity_tx,
389 cx,
390 )
391 }
392
393 fn path_style(&self) -> PathStyle {
394 self.ssh_path_style
395 }
396}
397
398impl SshRemoteConnection {
399 pub(crate) async fn new(
400 connection_options: SshConnectionOptions,
401 delegate: Arc<dyn RemoteClientDelegate>,
402 cx: &mut AsyncApp,
403 ) -> Result<Self> {
404 use askpass::AskPassResult;
405
406 let url = connection_options.ssh_url();
407
408 let temp_dir = tempfile::Builder::new()
409 .prefix("zed-ssh-session")
410 .tempdir()?;
411 let askpass_delegate = askpass::AskPassDelegate::new(cx, {
412 let delegate = delegate.clone();
413 move |prompt, tx, cx| delegate.ask_password(prompt, tx, cx)
414 });
415
416 let mut askpass =
417 askpass::AskPassSession::new(cx.background_executor(), askpass_delegate).await?;
418
419 delegate.set_status(Some("Connecting"), cx);
420
421 // Start the master SSH process, which does not do anything except for establish
422 // the connection and keep it open, allowing other ssh commands to reuse it
423 // via a control socket.
424 #[cfg(not(target_os = "windows"))]
425 let socket_path = temp_dir.path().join("ssh.sock");
426
427 #[cfg(target_os = "windows")]
428 let mut master_process = MasterProcess::new(
429 askpass.script_path().as_ref(),
430 connection_options.additional_args(),
431 &url,
432 )?;
433 #[cfg(not(target_os = "windows"))]
434 let mut master_process = MasterProcess::new(
435 askpass.script_path().as_ref(),
436 connection_options.additional_args(),
437 &socket_path,
438 &url,
439 )?;
440
441 let result = select_biased! {
442 result = askpass.run().fuse() => {
443 match result {
444 AskPassResult::CancelledByUser => {
445 master_process.as_mut().kill().ok();
446 anyhow::bail!("SSH connection canceled")
447 }
448 AskPassResult::Timedout => {
449 anyhow::bail!("connecting to host timed out")
450 }
451 }
452 }
453 _ = master_process.wait_connected().fuse() => {
454 anyhow::Ok(())
455 }
456 };
457
458 if let Err(e) = result {
459 return Err(e.context("Failed to connect to host"));
460 }
461
462 if master_process.as_mut().try_status()?.is_some() {
463 let mut output = Vec::new();
464 output.clear();
465 let mut stderr = master_process.as_mut().stderr.take().unwrap();
466 stderr.read_to_end(&mut output).await?;
467
468 let error_message = format!(
469 "failed to connect: {}",
470 String::from_utf8_lossy(&output).trim()
471 );
472 anyhow::bail!(error_message);
473 }
474
475 #[cfg(not(target_os = "windows"))]
476 let socket = SshSocket::new(connection_options, socket_path).await?;
477 #[cfg(target_os = "windows")]
478 let socket = SshSocket::new(
479 connection_options,
480 askpass
481 .get_password()
482 .or_else(|| askpass::EncryptedPassword::try_from("").ok())
483 .context("Failed to fetch askpass password")?,
484 cx.background_executor().clone(),
485 )
486 .await?;
487 drop(askpass);
488
489 let ssh_shell = socket.shell().await;
490 log::info!("Remote shell discovered: {}", ssh_shell);
491 let ssh_platform = socket.platform(ShellKind::new(&ssh_shell, false)).await?;
492 log::info!("Remote platform discovered: {}", ssh_shell);
493 let ssh_path_style = match ssh_platform.os {
494 "windows" => PathStyle::Windows,
495 _ => PathStyle::Posix,
496 };
497 let ssh_default_system_shell = String::from("/bin/sh");
498 let ssh_shell_kind = ShellKind::new(
499 &ssh_shell,
500 match ssh_platform.os {
501 "windows" => true,
502 _ => false,
503 },
504 );
505
506 let mut this = Self {
507 socket,
508 master_process: Mutex::new(Some(master_process)),
509 _temp_dir: temp_dir,
510 remote_binary_path: None,
511 ssh_path_style,
512 ssh_platform,
513 ssh_shell,
514 ssh_shell_kind,
515 ssh_default_system_shell,
516 };
517
518 let (release_channel, version, commit) = cx.update(|cx| {
519 (
520 ReleaseChannel::global(cx),
521 AppVersion::global(cx),
522 AppCommitSha::try_global(cx),
523 )
524 })?;
525 this.remote_binary_path = Some(
526 this.ensure_server_binary(&delegate, release_channel, version, commit, cx)
527 .await?,
528 );
529
530 Ok(this)
531 }
532
533 async fn ensure_server_binary(
534 &self,
535 delegate: &Arc<dyn RemoteClientDelegate>,
536 release_channel: ReleaseChannel,
537 version: SemanticVersion,
538 commit: Option<AppCommitSha>,
539 cx: &mut AsyncApp,
540 ) -> Result<Arc<RelPath>> {
541 let version_str = match release_channel {
542 ReleaseChannel::Nightly => {
543 let commit = commit.map(|s| s.full()).unwrap_or_default();
544 format!("{}-{}", version, commit)
545 }
546 ReleaseChannel::Dev => "build".to_string(),
547 _ => version.to_string(),
548 };
549 let binary_name = format!(
550 "zed-remote-server-{}-{}",
551 release_channel.dev_name(),
552 version_str
553 );
554 let dst_path =
555 paths::remote_server_dir_relative().join(RelPath::unix(&binary_name).unwrap());
556
557 #[cfg(debug_assertions)]
558 if let Some(remote_server_path) =
559 super::build_remote_server_from_source(&self.ssh_platform, delegate.as_ref(), cx)
560 .await?
561 {
562 let tmp_path = paths::remote_server_dir_relative().join(
563 RelPath::unix(&format!(
564 "download-{}-{}",
565 std::process::id(),
566 remote_server_path.file_name().unwrap().to_string_lossy()
567 ))
568 .unwrap(),
569 );
570 self.upload_local_server_binary(&remote_server_path, &tmp_path, delegate, cx)
571 .await?;
572 self.extract_server_binary(&dst_path, &tmp_path, delegate, cx)
573 .await?;
574 return Ok(dst_path);
575 }
576
577 if self
578 .socket
579 .run_command(
580 self.ssh_shell_kind,
581 &dst_path.display(self.path_style()),
582 &["version"],
583 true,
584 )
585 .await
586 .is_ok()
587 {
588 return Ok(dst_path);
589 }
590
591 let wanted_version = cx.update(|cx| match release_channel {
592 ReleaseChannel::Nightly => Ok(None),
593 ReleaseChannel::Dev => {
594 anyhow::bail!(
595 "ZED_BUILD_REMOTE_SERVER is not set and no remote server exists at ({:?})",
596 dst_path
597 )
598 }
599 _ => Ok(Some(AppVersion::global(cx))),
600 })??;
601
602 let tmp_path_gz = remote_server_dir_relative().join(
603 RelPath::unix(&format!(
604 "{}-download-{}.gz",
605 binary_name,
606 std::process::id()
607 ))
608 .unwrap(),
609 );
610 if !self.socket.connection_options.upload_binary_over_ssh
611 && let Some((url, body)) = delegate
612 .get_download_params(self.ssh_platform, release_channel, wanted_version, cx)
613 .await?
614 {
615 match self
616 .download_binary_on_server(&url, &body, &tmp_path_gz, delegate, cx)
617 .await
618 {
619 Ok(_) => {
620 self.extract_server_binary(&dst_path, &tmp_path_gz, delegate, cx)
621 .await
622 .context("extracting server binary")?;
623 return Ok(dst_path);
624 }
625 Err(e) => {
626 log::error!(
627 "Failed to download binary on server, attempting to download locally and then upload it the server: {e:#}",
628 )
629 }
630 }
631 }
632
633 let src_path = delegate
634 .download_server_binary_locally(self.ssh_platform, release_channel, wanted_version, cx)
635 .await
636 .context("downloading server binary locally")?;
637 self.upload_local_server_binary(&src_path, &tmp_path_gz, delegate, cx)
638 .await
639 .context("uploading server binary")?;
640 self.extract_server_binary(&dst_path, &tmp_path_gz, delegate, cx)
641 .await
642 .context("extracting server binary")?;
643 Ok(dst_path)
644 }
645
646 async fn download_binary_on_server(
647 &self,
648 url: &str,
649 body: &str,
650 tmp_path_gz: &RelPath,
651 delegate: &Arc<dyn RemoteClientDelegate>,
652 cx: &mut AsyncApp,
653 ) -> Result<()> {
654 if let Some(parent) = tmp_path_gz.parent() {
655 self.socket
656 .run_command(
657 self.ssh_shell_kind,
658 "mkdir",
659 &["-p", parent.display(self.path_style()).as_ref()],
660 true,
661 )
662 .await?;
663 }
664
665 delegate.set_status(Some("Downloading remote development server on host"), cx);
666
667 match self
668 .socket
669 .run_command(
670 self.ssh_shell_kind,
671 "curl",
672 &[
673 "-f",
674 "-L",
675 "-X",
676 "GET",
677 "-H",
678 "Content-Type: application/json",
679 "-d",
680 body,
681 url,
682 "-o",
683 &tmp_path_gz.display(self.path_style()),
684 ],
685 true,
686 )
687 .await
688 {
689 Ok(_) => {}
690 Err(e) => {
691 if self
692 .socket
693 .run_command(self.ssh_shell_kind, "which", &["curl"], true)
694 .await
695 .is_ok()
696 {
697 return Err(e);
698 }
699
700 log::info!("curl is not available, trying wget");
701 match self
702 .socket
703 .run_command(
704 self.ssh_shell_kind,
705 "wget",
706 &[
707 "--header=Content-Type: application/json",
708 "--body-data",
709 body,
710 url,
711 "-O",
712 &tmp_path_gz.display(self.path_style()),
713 ],
714 true,
715 )
716 .await
717 {
718 Ok(_) => {}
719 Err(e) => {
720 if self
721 .socket
722 .run_command(self.ssh_shell_kind, "which", &["wget"], true)
723 .await
724 .is_ok()
725 {
726 return Err(e);
727 } else {
728 anyhow::bail!("Neither curl nor wget is available");
729 }
730 }
731 }
732 }
733 }
734
735 Ok(())
736 }
737
738 async fn upload_local_server_binary(
739 &self,
740 src_path: &Path,
741 tmp_path_gz: &RelPath,
742 delegate: &Arc<dyn RemoteClientDelegate>,
743 cx: &mut AsyncApp,
744 ) -> Result<()> {
745 if let Some(parent) = tmp_path_gz.parent() {
746 self.socket
747 .run_command(
748 self.ssh_shell_kind,
749 "mkdir",
750 &["-p", parent.display(self.path_style()).as_ref()],
751 true,
752 )
753 .await?;
754 }
755
756 let src_stat = fs::metadata(&src_path).await?;
757 let size = src_stat.len();
758
759 let t0 = Instant::now();
760 delegate.set_status(Some("Uploading remote development server"), cx);
761 log::info!(
762 "uploading remote development server to {:?} ({}kb)",
763 tmp_path_gz,
764 size / 1024
765 );
766 self.upload_file(src_path, tmp_path_gz)
767 .await
768 .context("failed to upload server binary")?;
769 log::info!("uploaded remote development server in {:?}", t0.elapsed());
770 Ok(())
771 }
772
773 async fn extract_server_binary(
774 &self,
775 dst_path: &RelPath,
776 tmp_path: &RelPath,
777 delegate: &Arc<dyn RemoteClientDelegate>,
778 cx: &mut AsyncApp,
779 ) -> Result<()> {
780 delegate.set_status(Some("Extracting remote development server"), cx);
781 let server_mode = 0o755;
782
783 let shell_kind = ShellKind::Posix;
784 let orig_tmp_path = tmp_path.display(self.path_style());
785 let server_mode = format!("{:o}", server_mode);
786 let server_mode = shell_kind
787 .try_quote(&server_mode)
788 .context("shell quoting")?;
789 let dst_path = dst_path.display(self.path_style());
790 let dst_path = shell_kind.try_quote(&dst_path).context("shell quoting")?;
791 let script = if let Some(tmp_path) = orig_tmp_path.strip_suffix(".gz") {
792 let orig_tmp_path = shell_kind
793 .try_quote(&orig_tmp_path)
794 .context("shell quoting")?;
795 let tmp_path = shell_kind.try_quote(&tmp_path).context("shell quoting")?;
796 format!(
797 "gunzip -f {orig_tmp_path} && chmod {server_mode} {tmp_path} && mv {tmp_path} {dst_path}",
798 )
799 } else {
800 let orig_tmp_path = shell_kind
801 .try_quote(&orig_tmp_path)
802 .context("shell quoting")?;
803 format!("chmod {server_mode} {orig_tmp_path} && mv {orig_tmp_path} {dst_path}",)
804 };
805 let args = shell_kind.args_for_shell(false, script.to_string());
806 self.socket
807 .run_command(shell_kind, "sh", &args, true)
808 .await?;
809 Ok(())
810 }
811
812 fn build_scp_command(
813 &self,
814 src_path: &Path,
815 dest_path_str: &str,
816 args: Option<&[&str]>,
817 ) -> process::Command {
818 let mut command = util::command::new_smol_command("scp");
819 self.socket.ssh_options(&mut command, false).args(
820 self.socket
821 .connection_options
822 .port
823 .map(|port| vec!["-P".to_string(), port.to_string()])
824 .unwrap_or_default(),
825 );
826 if let Some(args) = args {
827 command.args(args);
828 }
829 command.arg(src_path).arg(format!(
830 "{}:{}",
831 self.socket.connection_options.scp_url(),
832 dest_path_str
833 ));
834 command
835 }
836
837 fn build_sftp_command(&self) -> process::Command {
838 let mut command = util::command::new_smol_command("sftp");
839 self.socket.ssh_options(&mut command, false).args(
840 self.socket
841 .connection_options
842 .port
843 .map(|port| vec!["-P".to_string(), port.to_string()])
844 .unwrap_or_default(),
845 );
846 command.arg("-b").arg("-");
847 command.arg(self.socket.connection_options.scp_url());
848 command.stdin(Stdio::piped());
849 command
850 }
851
852 async fn upload_file(&self, src_path: &Path, dest_path: &RelPath) -> Result<()> {
853 log::debug!("uploading file {:?} to {:?}", src_path, dest_path);
854
855 let src_path_display = src_path.display().to_string();
856 let dest_path_str = dest_path.display(self.path_style());
857
858 // We will try SFTP first, and if that fails, we will fall back to SCP.
859 // If SCP fails also, we give up and return an error.
860 // The reason we allow a fallback from SFTP to SCP is that if the user has to specify a password,
861 // depending on the implementation of SSH stack, SFTP may disable interactive password prompts in batch mode.
862 // This is for example the case on Windows as evidenced by this implementation snippet:
863 // https://github.com/PowerShell/openssh-portable/blob/b8c08ef9da9450a94a9c5ef717d96a7bd83f3332/sshconnect2.c#L417
864 if Self::is_sftp_available().await {
865 log::debug!("using SFTP for file upload");
866 let mut command = self.build_sftp_command();
867 let sftp_batch = format!("put {src_path_display} {dest_path_str}\n");
868
869 let mut child = command.spawn()?;
870 if let Some(mut stdin) = child.stdin.take() {
871 use futures::AsyncWriteExt;
872 stdin.write_all(sftp_batch.as_bytes()).await?;
873 stdin.flush().await?;
874 }
875
876 let output = child.output().await?;
877 if output.status.success() {
878 return Ok(());
879 }
880
881 let stderr = String::from_utf8_lossy(&output.stderr);
882 log::debug!(
883 "failed to upload file via SFTP {src_path_display} -> {dest_path_str}: {stderr}"
884 );
885 }
886
887 log::debug!("using SCP for file upload");
888 let mut command = self.build_scp_command(src_path, &dest_path_str, None);
889 let output = command.output().await?;
890
891 if output.status.success() {
892 return Ok(());
893 }
894
895 let stderr = String::from_utf8_lossy(&output.stderr);
896 log::debug!(
897 "failed to upload file via SCP {src_path_display} -> {dest_path_str}: {stderr}",
898 );
899 anyhow::bail!(
900 "failed to upload file via STFP/SCP {} -> {}: {}",
901 src_path_display,
902 dest_path_str,
903 stderr,
904 );
905 }
906
907 async fn is_sftp_available() -> bool {
908 which::which("sftp").is_ok()
909 }
910}
911
912impl SshSocket {
913 #[cfg(not(target_os = "windows"))]
914 async fn new(options: SshConnectionOptions, socket_path: PathBuf) -> Result<Self> {
915 Ok(Self {
916 connection_options: options,
917 envs: HashMap::default(),
918 socket_path,
919 })
920 }
921
922 #[cfg(target_os = "windows")]
923 async fn new(
924 options: SshConnectionOptions,
925 password: askpass::EncryptedPassword,
926 executor: gpui::BackgroundExecutor,
927 ) -> Result<Self> {
928 let mut envs = HashMap::default();
929 let get_password =
930 move |_| Task::ready(std::ops::ControlFlow::Continue(Ok(password.clone())));
931
932 let _proxy = askpass::PasswordProxy::new(get_password, executor).await?;
933 envs.insert("SSH_ASKPASS_REQUIRE".into(), "force".into());
934 envs.insert(
935 "SSH_ASKPASS".into(),
936 _proxy.script_path().as_ref().display().to_string(),
937 );
938
939 Ok(Self {
940 connection_options: options,
941 envs,
942 _proxy,
943 })
944 }
945
946 // :WARNING: ssh unquotes arguments when executing on the remote :WARNING:
947 // e.g. $ ssh host sh -c 'ls -l' is equivalent to $ ssh host sh -c ls -l
948 // and passes -l as an argument to sh, not to ls.
949 // Furthermore, some setups (e.g. Coder) will change directory when SSH'ing
950 // into a machine. You must use `cd` to get back to $HOME.
951 // You need to do it like this: $ ssh host "cd; sh -c 'ls -l /tmp'"
952 fn ssh_command(
953 &self,
954 shell_kind: ShellKind,
955 program: &str,
956 args: &[impl AsRef<str>],
957 allow_pseudo_tty: bool,
958 ) -> process::Command {
959 let mut command = util::command::new_smol_command("ssh");
960 let program = shell_kind.prepend_command_prefix(program);
961 let mut to_run = shell_kind
962 .try_quote_prefix_aware(&program)
963 .expect("shell quoting")
964 .into_owned();
965 for arg in args {
966 // We're trying to work with: sh, bash, zsh, fish, tcsh, ...?
967 debug_assert!(
968 !arg.as_ref().contains('\n'),
969 "multiline arguments do not work in all shells"
970 );
971 to_run.push(' ');
972 to_run.push_str(&shell_kind.try_quote(arg.as_ref()).expect("shell quoting"));
973 }
974 let separator = shell_kind.sequential_commands_separator();
975 let to_run = format!("cd{separator} {to_run}");
976 self.ssh_options(&mut command, true)
977 .arg(self.connection_options.ssh_url());
978 if !allow_pseudo_tty {
979 command.arg("-T");
980 }
981 command.arg(to_run);
982 log::debug!("ssh {:?}", command);
983 command
984 }
985
986 async fn run_command(
987 &self,
988 shell_kind: ShellKind,
989 program: &str,
990 args: &[impl AsRef<str>],
991 allow_pseudo_tty: bool,
992 ) -> Result<String> {
993 let mut command = self.ssh_command(shell_kind, program, args, allow_pseudo_tty);
994 let output = command.output().await?;
995 anyhow::ensure!(
996 output.status.success(),
997 "failed to run command {command:?}: {}",
998 String::from_utf8_lossy(&output.stderr)
999 );
1000 Ok(String::from_utf8_lossy(&output.stdout).to_string())
1001 }
1002
1003 #[cfg(not(target_os = "windows"))]
1004 fn ssh_options<'a>(
1005 &self,
1006 command: &'a mut process::Command,
1007 include_port_forwards: bool,
1008 ) -> &'a mut process::Command {
1009 let args = if include_port_forwards {
1010 self.connection_options.additional_args()
1011 } else {
1012 self.connection_options.additional_args_for_scp()
1013 };
1014
1015 command
1016 .stdin(Stdio::piped())
1017 .stdout(Stdio::piped())
1018 .stderr(Stdio::piped())
1019 .args(args)
1020 .args(["-o", "ControlMaster=no", "-o"])
1021 .arg(format!("ControlPath={}", self.socket_path.display()))
1022 }
1023
1024 #[cfg(target_os = "windows")]
1025 fn ssh_options<'a>(
1026 &self,
1027 command: &'a mut process::Command,
1028 include_port_forwards: bool,
1029 ) -> &'a mut process::Command {
1030 let args = if include_port_forwards {
1031 self.connection_options.additional_args()
1032 } else {
1033 self.connection_options.additional_args_for_scp()
1034 };
1035
1036 command
1037 .stdin(Stdio::piped())
1038 .stdout(Stdio::piped())
1039 .stderr(Stdio::piped())
1040 .args(args)
1041 .envs(self.envs.clone())
1042 }
1043
1044 // On Windows, we need to use `SSH_ASKPASS` to provide the password to ssh.
1045 // On Linux, we use the `ControlPath` option to create a socket file that ssh can use to
1046 #[cfg(not(target_os = "windows"))]
1047 fn ssh_args(&self) -> Vec<String> {
1048 let mut arguments = self.connection_options.additional_args();
1049 arguments.extend(vec![
1050 "-o".to_string(),
1051 "ControlMaster=no".to_string(),
1052 "-o".to_string(),
1053 format!("ControlPath={}", self.socket_path.display()),
1054 self.connection_options.ssh_url(),
1055 ]);
1056 arguments
1057 }
1058
1059 #[cfg(target_os = "windows")]
1060 fn ssh_args(&self) -> Vec<String> {
1061 let mut arguments = self.connection_options.additional_args();
1062 arguments.push(self.connection_options.ssh_url());
1063 arguments
1064 }
1065
1066 async fn platform(&self, shell: ShellKind) -> Result<RemotePlatform> {
1067 let uname = self.run_command(shell, "uname", &["-sm"], false).await?;
1068 let Some((os, arch)) = uname.split_once(" ") else {
1069 anyhow::bail!("unknown uname: {uname:?}")
1070 };
1071
1072 let os = match os.trim() {
1073 "Darwin" => "macos",
1074 "Linux" => "linux",
1075 _ => anyhow::bail!(
1076 "Prebuilt remote servers are not yet available for {os:?}. See https://zed.dev/docs/remote-development"
1077 ),
1078 };
1079 // exclude armv5,6,7 as they are 32-bit.
1080 let arch = if arch.starts_with("armv8")
1081 || arch.starts_with("armv9")
1082 || arch.starts_with("arm64")
1083 || arch.starts_with("aarch64")
1084 {
1085 "aarch64"
1086 } else if arch.starts_with("x86") {
1087 "x86_64"
1088 } else {
1089 anyhow::bail!(
1090 "Prebuilt remote servers are not yet available for {arch:?}. See https://zed.dev/docs/remote-development"
1091 )
1092 };
1093
1094 Ok(RemotePlatform { os, arch })
1095 }
1096
1097 async fn shell(&self) -> String {
1098 let default_shell = "sh";
1099 match self
1100 .run_command(ShellKind::Posix, "sh", &["-c", "echo $SHELL"], false)
1101 .await
1102 {
1103 Ok(shell) => match shell.trim() {
1104 "" => {
1105 log::error!("$SHELL is not set, falling back to {default_shell}");
1106 default_shell.to_owned()
1107 }
1108 shell => shell.to_owned(),
1109 },
1110 Err(e) => {
1111 log::error!("Failed to get shell: {e}");
1112 default_shell.to_owned()
1113 }
1114 }
1115 }
1116}
1117
1118fn parse_port_number(port_str: &str) -> Result<u16> {
1119 port_str
1120 .parse()
1121 .with_context(|| format!("parsing port number: {port_str}"))
1122}
1123
1124fn parse_port_forward_spec(spec: &str) -> Result<SshPortForwardOption> {
1125 let parts: Vec<&str> = spec.split(':').collect();
1126
1127 match parts.len() {
1128 4 => {
1129 let local_port = parse_port_number(parts[1])?;
1130 let remote_port = parse_port_number(parts[3])?;
1131
1132 Ok(SshPortForwardOption {
1133 local_host: Some(parts[0].to_string()),
1134 local_port,
1135 remote_host: Some(parts[2].to_string()),
1136 remote_port,
1137 })
1138 }
1139 3 => {
1140 let local_port = parse_port_number(parts[0])?;
1141 let remote_port = parse_port_number(parts[2])?;
1142
1143 Ok(SshPortForwardOption {
1144 local_host: None,
1145 local_port,
1146 remote_host: Some(parts[1].to_string()),
1147 remote_port,
1148 })
1149 }
1150 _ => anyhow::bail!("Invalid port forward format"),
1151 }
1152}
1153
1154impl SshConnectionOptions {
1155 pub fn parse_command_line(input: &str) -> Result<Self> {
1156 let input = input.trim_start_matches("ssh ");
1157 let mut hostname: Option<String> = None;
1158 let mut username: Option<String> = None;
1159 let mut port: Option<u16> = None;
1160 let mut args = Vec::new();
1161 let mut port_forwards: Vec<SshPortForwardOption> = Vec::new();
1162
1163 // disallowed: -E, -e, -F, -f, -G, -g, -M, -N, -n, -O, -q, -S, -s, -T, -t, -V, -v, -W
1164 const ALLOWED_OPTS: &[&str] = &[
1165 "-4", "-6", "-A", "-a", "-C", "-K", "-k", "-X", "-x", "-Y", "-y",
1166 ];
1167 const ALLOWED_ARGS: &[&str] = &[
1168 "-B", "-b", "-c", "-D", "-F", "-I", "-i", "-J", "-l", "-m", "-o", "-P", "-p", "-R",
1169 "-w",
1170 ];
1171
1172 let mut tokens = ShellKind::Posix
1173 .split(input)
1174 .context("invalid input")?
1175 .into_iter();
1176
1177 'outer: while let Some(arg) = tokens.next() {
1178 if ALLOWED_OPTS.contains(&(&arg as &str)) {
1179 args.push(arg.to_string());
1180 continue;
1181 }
1182 if arg == "-p" {
1183 port = tokens.next().and_then(|arg| arg.parse().ok());
1184 continue;
1185 } else if let Some(p) = arg.strip_prefix("-p") {
1186 port = p.parse().ok();
1187 continue;
1188 }
1189 if arg == "-l" {
1190 username = tokens.next();
1191 continue;
1192 } else if let Some(l) = arg.strip_prefix("-l") {
1193 username = Some(l.to_string());
1194 continue;
1195 }
1196 if arg == "-L" || arg.starts_with("-L") {
1197 let forward_spec = if arg == "-L" {
1198 tokens.next()
1199 } else {
1200 Some(arg.strip_prefix("-L").unwrap().to_string())
1201 };
1202
1203 if let Some(spec) = forward_spec {
1204 port_forwards.push(parse_port_forward_spec(&spec)?);
1205 } else {
1206 anyhow::bail!("Missing port forward format");
1207 }
1208 }
1209
1210 for a in ALLOWED_ARGS {
1211 if arg == *a {
1212 args.push(arg);
1213 if let Some(next) = tokens.next() {
1214 args.push(next);
1215 }
1216 continue 'outer;
1217 } else if arg.starts_with(a) {
1218 args.push(arg);
1219 continue 'outer;
1220 }
1221 }
1222 if arg.starts_with("-") || hostname.is_some() {
1223 anyhow::bail!("unsupported argument: {:?}", arg);
1224 }
1225 let mut input = &arg as &str;
1226 // Destination might be: username1@username2@ip2@ip1
1227 if let Some((u, rest)) = input.rsplit_once('@') {
1228 input = rest;
1229 username = Some(u.to_string());
1230 }
1231 if let Some((rest, p)) = input.split_once(':') {
1232 input = rest;
1233 port = p.parse().ok()
1234 }
1235 hostname = Some(input.to_string())
1236 }
1237
1238 let Some(hostname) = hostname else {
1239 anyhow::bail!("missing hostname");
1240 };
1241
1242 let port_forwards = match port_forwards.len() {
1243 0 => None,
1244 _ => Some(port_forwards),
1245 };
1246
1247 Ok(Self {
1248 host: hostname,
1249 username,
1250 port,
1251 port_forwards,
1252 args: Some(args),
1253 password: None,
1254 nickname: None,
1255 upload_binary_over_ssh: false,
1256 })
1257 }
1258
1259 pub fn ssh_url(&self) -> String {
1260 let mut result = String::from("ssh://");
1261 if let Some(username) = &self.username {
1262 // Username might be: username1@username2@ip2
1263 let username = urlencoding::encode(username);
1264 result.push_str(&username);
1265 result.push('@');
1266 }
1267 result.push_str(&self.host);
1268 if let Some(port) = self.port {
1269 result.push(':');
1270 result.push_str(&port.to_string());
1271 }
1272 result
1273 }
1274
1275 pub fn additional_args_for_scp(&self) -> Vec<String> {
1276 self.args.iter().flatten().cloned().collect::<Vec<String>>()
1277 }
1278
1279 pub fn additional_args(&self) -> Vec<String> {
1280 let mut args = self.additional_args_for_scp();
1281
1282 if let Some(forwards) = &self.port_forwards {
1283 args.extend(forwards.iter().map(|pf| {
1284 let local_host = match &pf.local_host {
1285 Some(host) => host,
1286 None => "localhost",
1287 };
1288 let remote_host = match &pf.remote_host {
1289 Some(host) => host,
1290 None => "localhost",
1291 };
1292
1293 format!(
1294 "-L{}:{}:{}:{}",
1295 local_host, pf.local_port, remote_host, pf.remote_port
1296 )
1297 }));
1298 }
1299
1300 args
1301 }
1302
1303 fn scp_url(&self) -> String {
1304 if let Some(username) = &self.username {
1305 format!("{}@{}", username, self.host)
1306 } else {
1307 self.host.clone()
1308 }
1309 }
1310
1311 pub fn connection_string(&self) -> String {
1312 let host = if let Some(username) = &self.username {
1313 format!("{}@{}", username, self.host)
1314 } else {
1315 self.host.clone()
1316 };
1317 if let Some(port) = &self.port {
1318 format!("{}:{}", host, port)
1319 } else {
1320 host
1321 }
1322 }
1323}
1324
1325fn build_command(
1326 input_program: Option<String>,
1327 input_args: &[String],
1328 input_env: &HashMap<String, String>,
1329 working_dir: Option<String>,
1330 port_forward: Option<(u16, String, u16)>,
1331 ssh_env: HashMap<String, String>,
1332 ssh_path_style: PathStyle,
1333 ssh_shell: &str,
1334 ssh_shell_kind: ShellKind,
1335 ssh_args: Vec<String>,
1336) -> Result<CommandTemplate> {
1337 use std::fmt::Write as _;
1338
1339 let mut exec = String::new();
1340 if let Some(working_dir) = working_dir {
1341 let working_dir = RemotePathBuf::new(working_dir, ssh_path_style).to_string();
1342
1343 // shlex will wrap the command in single quotes (''), disabling ~ expansion,
1344 // replace with with something that works
1345 const TILDE_PREFIX: &'static str = "~/";
1346 if working_dir.starts_with(TILDE_PREFIX) {
1347 let working_dir = working_dir.trim_start_matches("~").trim_start_matches("/");
1348 write!(
1349 exec,
1350 "cd \"$HOME/{working_dir}\" {} ",
1351 ssh_shell_kind.sequential_and_commands_separator()
1352 )?;
1353 } else {
1354 write!(
1355 exec,
1356 "cd \"{working_dir}\" {} ",
1357 ssh_shell_kind.sequential_and_commands_separator()
1358 )?;
1359 }
1360 } else {
1361 write!(
1362 exec,
1363 "cd {} ",
1364 ssh_shell_kind.sequential_and_commands_separator()
1365 )?;
1366 };
1367 write!(exec, "exec env ")?;
1368
1369 for (k, v) in input_env.iter() {
1370 write!(
1371 exec,
1372 "{}={} ",
1373 k,
1374 ssh_shell_kind.try_quote(v).context("shell quoting")?
1375 )?;
1376 }
1377
1378 if let Some(input_program) = input_program {
1379 write!(
1380 exec,
1381 "{}",
1382 ssh_shell_kind
1383 .try_quote_prefix_aware(&input_program)
1384 .context("shell quoting")?
1385 )?;
1386 for arg in input_args {
1387 let arg = ssh_shell_kind.try_quote(&arg).context("shell quoting")?;
1388 write!(exec, " {}", &arg)?;
1389 }
1390 } else {
1391 write!(exec, "{ssh_shell} -l")?;
1392 };
1393
1394 let mut args = Vec::new();
1395 args.extend(ssh_args);
1396
1397 if let Some((local_port, host, remote_port)) = port_forward {
1398 args.push("-L".into());
1399 args.push(format!("{local_port}:{host}:{remote_port}"));
1400 }
1401
1402 args.push("-t".into());
1403 args.push(exec);
1404 Ok(CommandTemplate {
1405 program: "ssh".into(),
1406 args,
1407 env: ssh_env,
1408 })
1409}
1410
1411#[cfg(test)]
1412mod tests {
1413 use super::*;
1414
1415 #[test]
1416 fn test_build_command() -> Result<()> {
1417 let mut input_env = HashMap::default();
1418 input_env.insert("INPUT_VA".to_string(), "val".to_string());
1419 let mut env = HashMap::default();
1420 env.insert("SSH_VAR".to_string(), "ssh-val".to_string());
1421
1422 let command = build_command(
1423 Some("remote_program".to_string()),
1424 &["arg1".to_string(), "arg2".to_string()],
1425 &input_env,
1426 Some("~/work".to_string()),
1427 None,
1428 env.clone(),
1429 PathStyle::Posix,
1430 "/bin/fish",
1431 ShellKind::Fish,
1432 vec!["-p".to_string(), "2222".to_string()],
1433 )?;
1434
1435 assert_eq!(command.program, "ssh");
1436 assert_eq!(
1437 command.args.iter().map(String::as_str).collect::<Vec<_>>(),
1438 [
1439 "-p",
1440 "2222",
1441 "-t",
1442 "cd \"$HOME/work\" && exec env INPUT_VA=val remote_program arg1 arg2"
1443 ]
1444 );
1445 assert_eq!(command.env, env);
1446
1447 let mut input_env = HashMap::default();
1448 input_env.insert("INPUT_VA".to_string(), "val".to_string());
1449 let mut env = HashMap::default();
1450 env.insert("SSH_VAR".to_string(), "ssh-val".to_string());
1451
1452 let command = build_command(
1453 None,
1454 &["arg1".to_string(), "arg2".to_string()],
1455 &input_env,
1456 None,
1457 Some((1, "foo".to_owned(), 2)),
1458 env.clone(),
1459 PathStyle::Posix,
1460 "/bin/fish",
1461 ShellKind::Fish,
1462 vec!["-p".to_string(), "2222".to_string()],
1463 )?;
1464
1465 assert_eq!(command.program, "ssh");
1466 assert_eq!(
1467 command.args.iter().map(String::as_str).collect::<Vec<_>>(),
1468 [
1469 "-p",
1470 "2222",
1471 "-L",
1472 "1:foo:2",
1473 "-t",
1474 "cd && exec env INPUT_VA=val /bin/fish -l"
1475 ]
1476 );
1477 assert_eq!(command.env, env);
1478
1479 Ok(())
1480 }
1481
1482 #[test]
1483 fn scp_args_exclude_port_forward_flags() {
1484 let options = SshConnectionOptions {
1485 host: "example.com".into(),
1486 args: Some(vec![
1487 "-p".to_string(),
1488 "2222".to_string(),
1489 "-o".to_string(),
1490 "StrictHostKeyChecking=no".to_string(),
1491 ]),
1492 port_forwards: Some(vec![SshPortForwardOption {
1493 local_host: Some("127.0.0.1".to_string()),
1494 local_port: 8080,
1495 remote_host: Some("127.0.0.1".to_string()),
1496 remote_port: 80,
1497 }]),
1498 ..Default::default()
1499 };
1500
1501 let ssh_args = options.additional_args();
1502 assert!(
1503 ssh_args.iter().any(|arg| arg.starts_with("-L")),
1504 "expected ssh args to include port-forward: {ssh_args:?}"
1505 );
1506
1507 let scp_args = options.additional_args_for_scp();
1508 assert_eq!(
1509 scp_args,
1510 vec![
1511 "-p".to_string(),
1512 "2222".to_string(),
1513 "-o".to_string(),
1514 "StrictHostKeyChecking=no".to_string()
1515 ]
1516 );
1517 assert!(
1518 scp_args.iter().all(|arg| !arg.starts_with("-L")),
1519 "scp args should not contain port forward flags: {scp_args:?}"
1520 );
1521 }
1522}