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 drop(stdin);
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)
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 let ssh_platform = socket.platform(ShellKind::new(&ssh_shell, false)).await?;
491 let ssh_path_style = match ssh_platform.os {
492 "windows" => PathStyle::Windows,
493 _ => PathStyle::Posix,
494 };
495 let ssh_default_system_shell = String::from("/bin/sh");
496 let ssh_shell_kind = ShellKind::new(
497 &ssh_shell,
498 match ssh_platform.os {
499 "windows" => true,
500 _ => false,
501 },
502 );
503
504 let mut this = Self {
505 socket,
506 master_process: Mutex::new(Some(master_process)),
507 _temp_dir: temp_dir,
508 remote_binary_path: None,
509 ssh_path_style,
510 ssh_platform,
511 ssh_shell,
512 ssh_shell_kind,
513 ssh_default_system_shell,
514 };
515
516 let (release_channel, version, commit) = cx.update(|cx| {
517 (
518 ReleaseChannel::global(cx),
519 AppVersion::global(cx),
520 AppCommitSha::try_global(cx),
521 )
522 })?;
523 this.remote_binary_path = Some(
524 this.ensure_server_binary(&delegate, release_channel, version, commit, cx)
525 .await?,
526 );
527
528 Ok(this)
529 }
530
531 async fn ensure_server_binary(
532 &self,
533 delegate: &Arc<dyn RemoteClientDelegate>,
534 release_channel: ReleaseChannel,
535 version: SemanticVersion,
536 commit: Option<AppCommitSha>,
537 cx: &mut AsyncApp,
538 ) -> Result<Arc<RelPath>> {
539 let version_str = match release_channel {
540 ReleaseChannel::Nightly => {
541 let commit = commit.map(|s| s.full()).unwrap_or_default();
542 format!("{}-{}", version, commit)
543 }
544 ReleaseChannel::Dev => "build".to_string(),
545 _ => version.to_string(),
546 };
547 let binary_name = format!(
548 "zed-remote-server-{}-{}",
549 release_channel.dev_name(),
550 version_str
551 );
552 let dst_path =
553 paths::remote_server_dir_relative().join(RelPath::unix(&binary_name).unwrap());
554
555 #[cfg(debug_assertions)]
556 if let Some(remote_server_path) =
557 super::build_remote_server_from_source(&self.ssh_platform, delegate.as_ref(), cx)
558 .await?
559 {
560 let tmp_path = paths::remote_server_dir_relative().join(
561 RelPath::unix(&format!(
562 "download-{}-{}",
563 std::process::id(),
564 remote_server_path.file_name().unwrap().to_string_lossy()
565 ))
566 .unwrap(),
567 );
568 self.upload_local_server_binary(&remote_server_path, &tmp_path, delegate, cx)
569 .await?;
570 self.extract_server_binary(&dst_path, &tmp_path, delegate, cx)
571 .await?;
572 return Ok(dst_path);
573 }
574
575 if self
576 .socket
577 .run_command(
578 self.ssh_shell_kind,
579 &dst_path.display(self.path_style()),
580 &["version"],
581 )
582 .await
583 .is_ok()
584 {
585 return Ok(dst_path);
586 }
587
588 let wanted_version = cx.update(|cx| match release_channel {
589 ReleaseChannel::Nightly => Ok(None),
590 ReleaseChannel::Dev => {
591 anyhow::bail!(
592 "ZED_BUILD_REMOTE_SERVER is not set and no remote server exists at ({:?})",
593 dst_path
594 )
595 }
596 _ => Ok(Some(AppVersion::global(cx))),
597 })??;
598
599 let tmp_path_gz = remote_server_dir_relative().join(
600 RelPath::unix(&format!(
601 "{}-download-{}.gz",
602 binary_name,
603 std::process::id()
604 ))
605 .unwrap(),
606 );
607 if !self.socket.connection_options.upload_binary_over_ssh
608 && let Some((url, body)) = delegate
609 .get_download_params(self.ssh_platform, release_channel, wanted_version, cx)
610 .await?
611 {
612 match self
613 .download_binary_on_server(&url, &body, &tmp_path_gz, delegate, cx)
614 .await
615 {
616 Ok(_) => {
617 self.extract_server_binary(&dst_path, &tmp_path_gz, delegate, cx)
618 .await?;
619 return Ok(dst_path);
620 }
621 Err(e) => {
622 log::error!(
623 "Failed to download binary on server, attempting to upload server: {}",
624 e
625 )
626 }
627 }
628 }
629
630 let src_path = delegate
631 .download_server_binary_locally(self.ssh_platform, release_channel, wanted_version, cx)
632 .await?;
633 self.upload_local_server_binary(&src_path, &tmp_path_gz, delegate, cx)
634 .await?;
635 self.extract_server_binary(&dst_path, &tmp_path_gz, delegate, cx)
636 .await?;
637 Ok(dst_path)
638 }
639
640 async fn download_binary_on_server(
641 &self,
642 url: &str,
643 body: &str,
644 tmp_path_gz: &RelPath,
645 delegate: &Arc<dyn RemoteClientDelegate>,
646 cx: &mut AsyncApp,
647 ) -> Result<()> {
648 if let Some(parent) = tmp_path_gz.parent() {
649 self.socket
650 .run_command(
651 self.ssh_shell_kind,
652 "mkdir",
653 &["-p", parent.display(self.path_style()).as_ref()],
654 )
655 .await?;
656 }
657
658 delegate.set_status(Some("Downloading remote development server on host"), cx);
659
660 match self
661 .socket
662 .run_command(
663 self.ssh_shell_kind,
664 "curl",
665 &[
666 "-f",
667 "-L",
668 "-X",
669 "GET",
670 "-H",
671 "Content-Type: application/json",
672 "-d",
673 body,
674 url,
675 "-o",
676 &tmp_path_gz.display(self.path_style()),
677 ],
678 )
679 .await
680 {
681 Ok(_) => {}
682 Err(e) => {
683 if self
684 .socket
685 .run_command(self.ssh_shell_kind, "which", &["curl"])
686 .await
687 .is_ok()
688 {
689 return Err(e);
690 }
691
692 match self
693 .socket
694 .run_command(
695 self.ssh_shell_kind,
696 "wget",
697 &[
698 "--header=Content-Type: application/json",
699 "--body-data",
700 body,
701 url,
702 "-O",
703 &tmp_path_gz.display(self.path_style()),
704 ],
705 )
706 .await
707 {
708 Ok(_) => {}
709 Err(e) => {
710 if self
711 .socket
712 .run_command(self.ssh_shell_kind, "which", &["wget"])
713 .await
714 .is_ok()
715 {
716 return Err(e);
717 } else {
718 anyhow::bail!("Neither curl nor wget is available");
719 }
720 }
721 }
722 }
723 }
724
725 Ok(())
726 }
727
728 async fn upload_local_server_binary(
729 &self,
730 src_path: &Path,
731 tmp_path_gz: &RelPath,
732 delegate: &Arc<dyn RemoteClientDelegate>,
733 cx: &mut AsyncApp,
734 ) -> Result<()> {
735 if let Some(parent) = tmp_path_gz.parent() {
736 self.socket
737 .run_command(
738 self.ssh_shell_kind,
739 "mkdir",
740 &["-p", parent.display(self.path_style()).as_ref()],
741 )
742 .await?;
743 }
744
745 let src_stat = fs::metadata(&src_path).await?;
746 let size = src_stat.len();
747
748 let t0 = Instant::now();
749 delegate.set_status(Some("Uploading remote development server"), cx);
750 log::info!(
751 "uploading remote development server to {:?} ({}kb)",
752 tmp_path_gz,
753 size / 1024
754 );
755 self.upload_file(src_path, tmp_path_gz)
756 .await
757 .context("failed to upload server binary")?;
758 log::info!("uploaded remote development server in {:?}", t0.elapsed());
759 Ok(())
760 }
761
762 async fn extract_server_binary(
763 &self,
764 dst_path: &RelPath,
765 tmp_path: &RelPath,
766 delegate: &Arc<dyn RemoteClientDelegate>,
767 cx: &mut AsyncApp,
768 ) -> Result<()> {
769 delegate.set_status(Some("Extracting remote development server"), cx);
770 let server_mode = 0o755;
771
772 let shell_kind = ShellKind::Posix;
773 let orig_tmp_path = tmp_path.display(self.path_style());
774 let server_mode = format!("{:o}", server_mode);
775 let server_mode = shell_kind
776 .try_quote(&server_mode)
777 .context("shell quoting")?;
778 let dst_path = dst_path.display(self.path_style());
779 let dst_path = shell_kind.try_quote(&dst_path).context("shell quoting")?;
780 let script = if let Some(tmp_path) = orig_tmp_path.strip_suffix(".gz") {
781 format!(
782 "gunzip -f {orig_tmp_path} && chmod {server_mode} {tmp_path} && mv {tmp_path} {dst_path}",
783 )
784 } else {
785 format!("chmod {server_mode} {orig_tmp_path} && mv {orig_tmp_path} {dst_path}",)
786 };
787 let args = shell_kind.args_for_shell(false, script.to_string());
788 self.socket.run_command(shell_kind, "sh", &args).await?;
789 Ok(())
790 }
791
792 fn build_scp_command(
793 &self,
794 src_path: &Path,
795 dest_path_str: &str,
796 args: Option<&[&str]>,
797 ) -> process::Command {
798 let mut command = util::command::new_smol_command("scp");
799 self.socket.ssh_options(&mut command, false).args(
800 self.socket
801 .connection_options
802 .port
803 .map(|port| vec!["-P".to_string(), port.to_string()])
804 .unwrap_or_default(),
805 );
806 if let Some(args) = args {
807 command.args(args);
808 }
809 command.arg(src_path).arg(format!(
810 "{}:{}",
811 self.socket.connection_options.scp_url(),
812 dest_path_str
813 ));
814 command
815 }
816
817 fn build_sftp_command(&self) -> process::Command {
818 let mut command = util::command::new_smol_command("sftp");
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 command.arg("-b").arg("-");
827 command.arg(self.socket.connection_options.scp_url());
828 command.stdin(Stdio::piped());
829 command
830 }
831
832 async fn upload_file(&self, src_path: &Path, dest_path: &RelPath) -> Result<()> {
833 log::debug!("uploading file {:?} to {:?}", src_path, dest_path);
834
835 let src_path_display = src_path.display().to_string();
836 let dest_path_str = dest_path.display(self.path_style());
837
838 // We will try SFTP first, and if that fails, we will fall back to SCP.
839 // If SCP fails also, we give up and return an error.
840 // The reason we allow a fallback from SFTP to SCP is that if the user has to specify a password,
841 // depending on the implementation of SSH stack, SFTP may disable interactive password prompts in batch mode.
842 // This is for example the case on Windows as evidenced by this implementation snippet:
843 // https://github.com/PowerShell/openssh-portable/blob/b8c08ef9da9450a94a9c5ef717d96a7bd83f3332/sshconnect2.c#L417
844 if Self::is_sftp_available().await {
845 log::debug!("using SFTP for file upload");
846 let mut command = self.build_sftp_command();
847 let sftp_batch = format!("put {src_path_display} {dest_path_str}\n");
848
849 let mut child = command.spawn()?;
850 if let Some(mut stdin) = child.stdin.take() {
851 use futures::AsyncWriteExt;
852 stdin.write_all(sftp_batch.as_bytes()).await?;
853 drop(stdin);
854 }
855
856 let output = child.output().await?;
857 if output.status.success() {
858 return Ok(());
859 }
860
861 let stderr = String::from_utf8_lossy(&output.stderr);
862 log::debug!(
863 "failed to upload file via SFTP {src_path_display} -> {dest_path_str}: {stderr}"
864 );
865 }
866
867 log::debug!("using SCP for file upload");
868 let mut command = self.build_scp_command(src_path, &dest_path_str, None);
869 let output = command.output().await?;
870
871 if output.status.success() {
872 return Ok(());
873 }
874
875 let stderr = String::from_utf8_lossy(&output.stderr);
876 log::debug!(
877 "failed to upload file via SCP {src_path_display} -> {dest_path_str}: {stderr}",
878 );
879 anyhow::bail!(
880 "failed to upload file via STFP/SCP {} -> {}: {}",
881 src_path_display,
882 dest_path_str,
883 stderr,
884 );
885 }
886
887 async fn is_sftp_available() -> bool {
888 which::which("sftp").is_ok()
889 }
890}
891
892impl SshSocket {
893 #[cfg(not(target_os = "windows"))]
894 async fn new(options: SshConnectionOptions, socket_path: PathBuf) -> Result<Self> {
895 Ok(Self {
896 connection_options: options,
897 envs: HashMap::default(),
898 socket_path,
899 })
900 }
901
902 #[cfg(target_os = "windows")]
903 async fn new(
904 options: SshConnectionOptions,
905 password: askpass::EncryptedPassword,
906 executor: gpui::BackgroundExecutor,
907 ) -> Result<Self> {
908 let mut envs = HashMap::default();
909 let get_password =
910 move |_| Task::ready(std::ops::ControlFlow::Continue(Ok(password.clone())));
911
912 let _proxy = askpass::PasswordProxy::new(get_password, executor).await?;
913 envs.insert("SSH_ASKPASS_REQUIRE".into(), "force".into());
914 envs.insert(
915 "SSH_ASKPASS".into(),
916 _proxy.script_path().as_ref().display().to_string(),
917 );
918
919 Ok(Self {
920 connection_options: options,
921 envs,
922 _proxy,
923 })
924 }
925
926 // :WARNING: ssh unquotes arguments when executing on the remote :WARNING:
927 // e.g. $ ssh host sh -c 'ls -l' is equivalent to $ ssh host sh -c ls -l
928 // and passes -l as an argument to sh, not to ls.
929 // Furthermore, some setups (e.g. Coder) will change directory when SSH'ing
930 // into a machine. You must use `cd` to get back to $HOME.
931 // You need to do it like this: $ ssh host "cd; sh -c 'ls -l /tmp'"
932 fn ssh_command(
933 &self,
934 shell_kind: ShellKind,
935 program: &str,
936 args: &[impl AsRef<str>],
937 ) -> process::Command {
938 let mut command = util::command::new_smol_command("ssh");
939 let program = shell_kind.prepend_command_prefix(program);
940 let mut to_run = shell_kind
941 .try_quote_prefix_aware(&program)
942 .expect("shell quoting")
943 .into_owned();
944 for arg in args {
945 // We're trying to work with: sh, bash, zsh, fish, tcsh, ...?
946 debug_assert!(
947 !arg.as_ref().contains('\n'),
948 "multiline arguments do not work in all shells"
949 );
950 to_run.push(' ');
951 to_run.push_str(&shell_kind.try_quote(arg.as_ref()).expect("shell quoting"));
952 }
953 let separator = shell_kind.sequential_commands_separator();
954 let to_run = format!("cd{separator} {to_run}");
955 self.ssh_options(&mut command, true)
956 .arg(self.connection_options.ssh_url())
957 .arg("-T")
958 .arg(to_run);
959 log::debug!("ssh {:?}", command);
960 command
961 }
962
963 async fn run_command(
964 &self,
965 shell_kind: ShellKind,
966 program: &str,
967 args: &[impl AsRef<str>],
968 ) -> Result<String> {
969 let output = self.ssh_command(shell_kind, program, args).output().await?;
970 anyhow::ensure!(
971 output.status.success(),
972 "failed to run command: {}",
973 String::from_utf8_lossy(&output.stderr)
974 );
975 Ok(String::from_utf8_lossy(&output.stdout).to_string())
976 }
977
978 #[cfg(not(target_os = "windows"))]
979 fn ssh_options<'a>(
980 &self,
981 command: &'a mut process::Command,
982 include_port_forwards: bool,
983 ) -> &'a mut process::Command {
984 let args = if include_port_forwards {
985 self.connection_options.additional_args()
986 } else {
987 self.connection_options.additional_args_for_scp()
988 };
989
990 command
991 .stdin(Stdio::piped())
992 .stdout(Stdio::piped())
993 .stderr(Stdio::piped())
994 .args(args)
995 .args(["-o", "ControlMaster=no", "-o"])
996 .arg(format!("ControlPath={}", self.socket_path.display()))
997 }
998
999 #[cfg(target_os = "windows")]
1000 fn ssh_options<'a>(
1001 &self,
1002 command: &'a mut process::Command,
1003 include_port_forwards: bool,
1004 ) -> &'a mut process::Command {
1005 let args = if include_port_forwards {
1006 self.connection_options.additional_args()
1007 } else {
1008 self.connection_options.additional_args_for_scp()
1009 };
1010
1011 command
1012 .stdin(Stdio::piped())
1013 .stdout(Stdio::piped())
1014 .stderr(Stdio::piped())
1015 .args(args)
1016 .envs(self.envs.clone())
1017 }
1018
1019 // On Windows, we need to use `SSH_ASKPASS` to provide the password to ssh.
1020 // On Linux, we use the `ControlPath` option to create a socket file that ssh can use to
1021 #[cfg(not(target_os = "windows"))]
1022 fn ssh_args(&self) -> Vec<String> {
1023 let mut arguments = self.connection_options.additional_args();
1024 arguments.extend(vec![
1025 "-o".to_string(),
1026 "ControlMaster=no".to_string(),
1027 "-o".to_string(),
1028 format!("ControlPath={}", self.socket_path.display()),
1029 self.connection_options.ssh_url(),
1030 ]);
1031 arguments
1032 }
1033
1034 #[cfg(target_os = "windows")]
1035 fn ssh_args(&self) -> Vec<String> {
1036 let mut arguments = self.connection_options.additional_args();
1037 arguments.push(self.connection_options.ssh_url());
1038 arguments
1039 }
1040
1041 async fn platform(&self, shell: ShellKind) -> Result<RemotePlatform> {
1042 let uname = self.run_command(shell, "uname", &["-sm"]).await?;
1043 let Some((os, arch)) = uname.split_once(" ") else {
1044 anyhow::bail!("unknown uname: {uname:?}")
1045 };
1046
1047 let os = match os.trim() {
1048 "Darwin" => "macos",
1049 "Linux" => "linux",
1050 _ => anyhow::bail!(
1051 "Prebuilt remote servers are not yet available for {os:?}. See https://zed.dev/docs/remote-development"
1052 ),
1053 };
1054 // exclude armv5,6,7 as they are 32-bit.
1055 let arch = if arch.starts_with("armv8")
1056 || arch.starts_with("armv9")
1057 || arch.starts_with("arm64")
1058 || arch.starts_with("aarch64")
1059 {
1060 "aarch64"
1061 } else if arch.starts_with("x86") {
1062 "x86_64"
1063 } else {
1064 anyhow::bail!(
1065 "Prebuilt remote servers are not yet available for {arch:?}. See https://zed.dev/docs/remote-development"
1066 )
1067 };
1068
1069 Ok(RemotePlatform { os, arch })
1070 }
1071
1072 async fn shell(&self) -> String {
1073 let default_shell = "sh";
1074 match self
1075 .run_command(ShellKind::Posix, "sh", &["-c", "echo $SHELL"])
1076 .await
1077 {
1078 Ok(shell) => match shell.trim() {
1079 "" => {
1080 log::error!("$SHELL is not set, falling back to {default_shell}");
1081 default_shell.to_owned()
1082 }
1083 shell => shell.to_owned(),
1084 },
1085 Err(e) => {
1086 log::error!("Failed to get shell: {e}");
1087 default_shell.to_owned()
1088 }
1089 }
1090 }
1091}
1092
1093fn parse_port_number(port_str: &str) -> Result<u16> {
1094 port_str
1095 .parse()
1096 .with_context(|| format!("parsing port number: {port_str}"))
1097}
1098
1099fn parse_port_forward_spec(spec: &str) -> Result<SshPortForwardOption> {
1100 let parts: Vec<&str> = spec.split(':').collect();
1101
1102 match parts.len() {
1103 4 => {
1104 let local_port = parse_port_number(parts[1])?;
1105 let remote_port = parse_port_number(parts[3])?;
1106
1107 Ok(SshPortForwardOption {
1108 local_host: Some(parts[0].to_string()),
1109 local_port,
1110 remote_host: Some(parts[2].to_string()),
1111 remote_port,
1112 })
1113 }
1114 3 => {
1115 let local_port = parse_port_number(parts[0])?;
1116 let remote_port = parse_port_number(parts[2])?;
1117
1118 Ok(SshPortForwardOption {
1119 local_host: None,
1120 local_port,
1121 remote_host: Some(parts[1].to_string()),
1122 remote_port,
1123 })
1124 }
1125 _ => anyhow::bail!("Invalid port forward format"),
1126 }
1127}
1128
1129impl SshConnectionOptions {
1130 pub fn parse_command_line(input: &str) -> Result<Self> {
1131 let input = input.trim_start_matches("ssh ");
1132 let mut hostname: Option<String> = None;
1133 let mut username: Option<String> = None;
1134 let mut port: Option<u16> = None;
1135 let mut args = Vec::new();
1136 let mut port_forwards: Vec<SshPortForwardOption> = Vec::new();
1137
1138 // disallowed: -E, -e, -F, -f, -G, -g, -M, -N, -n, -O, -q, -S, -s, -T, -t, -V, -v, -W
1139 const ALLOWED_OPTS: &[&str] = &[
1140 "-4", "-6", "-A", "-a", "-C", "-K", "-k", "-X", "-x", "-Y", "-y",
1141 ];
1142 const ALLOWED_ARGS: &[&str] = &[
1143 "-B", "-b", "-c", "-D", "-F", "-I", "-i", "-J", "-l", "-m", "-o", "-P", "-p", "-R",
1144 "-w",
1145 ];
1146
1147 let mut tokens = ShellKind::Posix
1148 .split(input)
1149 .context("invalid input")?
1150 .into_iter();
1151
1152 'outer: while let Some(arg) = tokens.next() {
1153 if ALLOWED_OPTS.contains(&(&arg as &str)) {
1154 args.push(arg.to_string());
1155 continue;
1156 }
1157 if arg == "-p" {
1158 port = tokens.next().and_then(|arg| arg.parse().ok());
1159 continue;
1160 } else if let Some(p) = arg.strip_prefix("-p") {
1161 port = p.parse().ok();
1162 continue;
1163 }
1164 if arg == "-l" {
1165 username = tokens.next();
1166 continue;
1167 } else if let Some(l) = arg.strip_prefix("-l") {
1168 username = Some(l.to_string());
1169 continue;
1170 }
1171 if arg == "-L" || arg.starts_with("-L") {
1172 let forward_spec = if arg == "-L" {
1173 tokens.next()
1174 } else {
1175 Some(arg.strip_prefix("-L").unwrap().to_string())
1176 };
1177
1178 if let Some(spec) = forward_spec {
1179 port_forwards.push(parse_port_forward_spec(&spec)?);
1180 } else {
1181 anyhow::bail!("Missing port forward format");
1182 }
1183 }
1184
1185 for a in ALLOWED_ARGS {
1186 if arg == *a {
1187 args.push(arg);
1188 if let Some(next) = tokens.next() {
1189 args.push(next);
1190 }
1191 continue 'outer;
1192 } else if arg.starts_with(a) {
1193 args.push(arg);
1194 continue 'outer;
1195 }
1196 }
1197 if arg.starts_with("-") || hostname.is_some() {
1198 anyhow::bail!("unsupported argument: {:?}", arg);
1199 }
1200 let mut input = &arg as &str;
1201 // Destination might be: username1@username2@ip2@ip1
1202 if let Some((u, rest)) = input.rsplit_once('@') {
1203 input = rest;
1204 username = Some(u.to_string());
1205 }
1206 if let Some((rest, p)) = input.split_once(':') {
1207 input = rest;
1208 port = p.parse().ok()
1209 }
1210 hostname = Some(input.to_string())
1211 }
1212
1213 let Some(hostname) = hostname else {
1214 anyhow::bail!("missing hostname");
1215 };
1216
1217 let port_forwards = match port_forwards.len() {
1218 0 => None,
1219 _ => Some(port_forwards),
1220 };
1221
1222 Ok(Self {
1223 host: hostname,
1224 username,
1225 port,
1226 port_forwards,
1227 args: Some(args),
1228 password: None,
1229 nickname: None,
1230 upload_binary_over_ssh: false,
1231 })
1232 }
1233
1234 pub fn ssh_url(&self) -> String {
1235 let mut result = String::from("ssh://");
1236 if let Some(username) = &self.username {
1237 // Username might be: username1@username2@ip2
1238 let username = urlencoding::encode(username);
1239 result.push_str(&username);
1240 result.push('@');
1241 }
1242 result.push_str(&self.host);
1243 if let Some(port) = self.port {
1244 result.push(':');
1245 result.push_str(&port.to_string());
1246 }
1247 result
1248 }
1249
1250 pub fn additional_args_for_scp(&self) -> Vec<String> {
1251 self.args.iter().flatten().cloned().collect::<Vec<String>>()
1252 }
1253
1254 pub fn additional_args(&self) -> Vec<String> {
1255 let mut args = self.additional_args_for_scp();
1256
1257 if let Some(forwards) = &self.port_forwards {
1258 args.extend(forwards.iter().map(|pf| {
1259 let local_host = match &pf.local_host {
1260 Some(host) => host,
1261 None => "localhost",
1262 };
1263 let remote_host = match &pf.remote_host {
1264 Some(host) => host,
1265 None => "localhost",
1266 };
1267
1268 format!(
1269 "-L{}:{}:{}:{}",
1270 local_host, pf.local_port, remote_host, pf.remote_port
1271 )
1272 }));
1273 }
1274
1275 args
1276 }
1277
1278 fn scp_url(&self) -> String {
1279 if let Some(username) = &self.username {
1280 format!("{}@{}", username, self.host)
1281 } else {
1282 self.host.clone()
1283 }
1284 }
1285
1286 pub fn connection_string(&self) -> String {
1287 let host = if let Some(username) = &self.username {
1288 format!("{}@{}", username, self.host)
1289 } else {
1290 self.host.clone()
1291 };
1292 if let Some(port) = &self.port {
1293 format!("{}:{}", host, port)
1294 } else {
1295 host
1296 }
1297 }
1298}
1299
1300fn build_command(
1301 input_program: Option<String>,
1302 input_args: &[String],
1303 input_env: &HashMap<String, String>,
1304 working_dir: Option<String>,
1305 port_forward: Option<(u16, String, u16)>,
1306 ssh_env: HashMap<String, String>,
1307 ssh_path_style: PathStyle,
1308 ssh_shell: &str,
1309 ssh_shell_kind: ShellKind,
1310 ssh_args: Vec<String>,
1311) -> Result<CommandTemplate> {
1312 use std::fmt::Write as _;
1313
1314 let mut exec = String::new();
1315 if let Some(working_dir) = working_dir {
1316 let working_dir = RemotePathBuf::new(working_dir, ssh_path_style).to_string();
1317
1318 // shlex will wrap the command in single quotes (''), disabling ~ expansion,
1319 // replace with with something that works
1320 const TILDE_PREFIX: &'static str = "~/";
1321 if working_dir.starts_with(TILDE_PREFIX) {
1322 let working_dir = working_dir.trim_start_matches("~").trim_start_matches("/");
1323 write!(
1324 exec,
1325 "cd \"$HOME/{working_dir}\" {} ",
1326 ssh_shell_kind.sequential_and_commands_separator()
1327 )?;
1328 } else {
1329 write!(
1330 exec,
1331 "cd \"{working_dir}\" {} ",
1332 ssh_shell_kind.sequential_and_commands_separator()
1333 )?;
1334 }
1335 } else {
1336 write!(
1337 exec,
1338 "cd {} ",
1339 ssh_shell_kind.sequential_and_commands_separator()
1340 )?;
1341 };
1342 write!(exec, "exec env ")?;
1343
1344 for (k, v) in input_env.iter() {
1345 write!(
1346 exec,
1347 "{}={} ",
1348 k,
1349 ssh_shell_kind.try_quote(v).context("shell quoting")?
1350 )?;
1351 }
1352
1353 if let Some(input_program) = input_program {
1354 write!(
1355 exec,
1356 "{}",
1357 ssh_shell_kind
1358 .try_quote_prefix_aware(&input_program)
1359 .context("shell quoting")?
1360 )?;
1361 for arg in input_args {
1362 let arg = ssh_shell_kind.try_quote(&arg).context("shell quoting")?;
1363 write!(exec, " {}", &arg)?;
1364 }
1365 } else {
1366 write!(exec, "{ssh_shell} -l")?;
1367 };
1368
1369 let mut args = Vec::new();
1370 args.extend(ssh_args);
1371
1372 if let Some((local_port, host, remote_port)) = port_forward {
1373 args.push("-L".into());
1374 args.push(format!("{local_port}:{host}:{remote_port}"));
1375 }
1376
1377 args.push("-t".into());
1378 args.push(exec);
1379 Ok(CommandTemplate {
1380 program: "ssh".into(),
1381 args,
1382 env: ssh_env,
1383 })
1384}
1385
1386#[cfg(test)]
1387mod tests {
1388 use super::*;
1389
1390 #[test]
1391 fn test_build_command() -> Result<()> {
1392 let mut input_env = HashMap::default();
1393 input_env.insert("INPUT_VA".to_string(), "val".to_string());
1394 let mut env = HashMap::default();
1395 env.insert("SSH_VAR".to_string(), "ssh-val".to_string());
1396
1397 let command = build_command(
1398 Some("remote_program".to_string()),
1399 &["arg1".to_string(), "arg2".to_string()],
1400 &input_env,
1401 Some("~/work".to_string()),
1402 None,
1403 env.clone(),
1404 PathStyle::Posix,
1405 "/bin/fish",
1406 ShellKind::Fish,
1407 vec!["-p".to_string(), "2222".to_string()],
1408 )?;
1409
1410 assert_eq!(command.program, "ssh");
1411 assert_eq!(
1412 command.args.iter().map(String::as_str).collect::<Vec<_>>(),
1413 [
1414 "-p",
1415 "2222",
1416 "-t",
1417 "cd \"$HOME/work\" && exec env INPUT_VA=val remote_program arg1 arg2"
1418 ]
1419 );
1420 assert_eq!(command.env, env);
1421
1422 let mut input_env = HashMap::default();
1423 input_env.insert("INPUT_VA".to_string(), "val".to_string());
1424 let mut env = HashMap::default();
1425 env.insert("SSH_VAR".to_string(), "ssh-val".to_string());
1426
1427 let command = build_command(
1428 None,
1429 &["arg1".to_string(), "arg2".to_string()],
1430 &input_env,
1431 None,
1432 Some((1, "foo".to_owned(), 2)),
1433 env.clone(),
1434 PathStyle::Posix,
1435 "/bin/fish",
1436 ShellKind::Fish,
1437 vec!["-p".to_string(), "2222".to_string()],
1438 )?;
1439
1440 assert_eq!(command.program, "ssh");
1441 assert_eq!(
1442 command.args.iter().map(String::as_str).collect::<Vec<_>>(),
1443 [
1444 "-p",
1445 "2222",
1446 "-L",
1447 "1:foo:2",
1448 "-t",
1449 "cd && exec env INPUT_VA=val /bin/fish -l"
1450 ]
1451 );
1452 assert_eq!(command.env, env);
1453
1454 Ok(())
1455 }
1456
1457 #[test]
1458 fn scp_args_exclude_port_forward_flags() {
1459 let options = SshConnectionOptions {
1460 host: "example.com".into(),
1461 args: Some(vec![
1462 "-p".to_string(),
1463 "2222".to_string(),
1464 "-o".to_string(),
1465 "StrictHostKeyChecking=no".to_string(),
1466 ]),
1467 port_forwards: Some(vec![SshPortForwardOption {
1468 local_host: Some("127.0.0.1".to_string()),
1469 local_port: 8080,
1470 remote_host: Some("127.0.0.1".to_string()),
1471 remote_port: 80,
1472 }]),
1473 ..Default::default()
1474 };
1475
1476 let ssh_args = options.additional_args();
1477 assert!(
1478 ssh_args.iter().any(|arg| arg.starts_with("-L")),
1479 "expected ssh args to include port-forward: {ssh_args:?}"
1480 );
1481
1482 let scp_args = options.additional_args_for_scp();
1483 assert_eq!(
1484 scp_args,
1485 vec![
1486 "-p".to_string(),
1487 "2222".to_string(),
1488 "-o".to_string(),
1489 "StrictHostKeyChecking=no".to_string()
1490 ]
1491 );
1492 assert!(
1493 scp_args.iter().all(|arg| !arg.starts_with("-L")),
1494 "scp args should not contain port forward flags: {scp_args:?}"
1495 );
1496 }
1497}