ssh.rs

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