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                    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        match self
1074            .run_command(ShellKind::Posix, "sh", &["-c", "echo $SHELL"])
1075            .await
1076        {
1077            Ok(shell) => shell.trim().to_owned(),
1078            Err(e) => {
1079                log::error!("Failed to get shell: {e}");
1080                "sh".to_owned()
1081            }
1082        }
1083    }
1084}
1085
1086fn parse_port_number(port_str: &str) -> Result<u16> {
1087    port_str
1088        .parse()
1089        .with_context(|| format!("parsing port number: {port_str}"))
1090}
1091
1092fn parse_port_forward_spec(spec: &str) -> Result<SshPortForwardOption> {
1093    let parts: Vec<&str> = spec.split(':').collect();
1094
1095    match parts.len() {
1096        4 => {
1097            let local_port = parse_port_number(parts[1])?;
1098            let remote_port = parse_port_number(parts[3])?;
1099
1100            Ok(SshPortForwardOption {
1101                local_host: Some(parts[0].to_string()),
1102                local_port,
1103                remote_host: Some(parts[2].to_string()),
1104                remote_port,
1105            })
1106        }
1107        3 => {
1108            let local_port = parse_port_number(parts[0])?;
1109            let remote_port = parse_port_number(parts[2])?;
1110
1111            Ok(SshPortForwardOption {
1112                local_host: None,
1113                local_port,
1114                remote_host: Some(parts[1].to_string()),
1115                remote_port,
1116            })
1117        }
1118        _ => anyhow::bail!("Invalid port forward format"),
1119    }
1120}
1121
1122impl SshConnectionOptions {
1123    pub fn parse_command_line(input: &str) -> Result<Self> {
1124        let input = input.trim_start_matches("ssh ");
1125        let mut hostname: Option<String> = None;
1126        let mut username: Option<String> = None;
1127        let mut port: Option<u16> = None;
1128        let mut args = Vec::new();
1129        let mut port_forwards: Vec<SshPortForwardOption> = Vec::new();
1130
1131        // disallowed: -E, -e, -F, -f, -G, -g, -M, -N, -n, -O, -q, -S, -s, -T, -t, -V, -v, -W
1132        const ALLOWED_OPTS: &[&str] = &[
1133            "-4", "-6", "-A", "-a", "-C", "-K", "-k", "-X", "-x", "-Y", "-y",
1134        ];
1135        const ALLOWED_ARGS: &[&str] = &[
1136            "-B", "-b", "-c", "-D", "-F", "-I", "-i", "-J", "-l", "-m", "-o", "-P", "-p", "-R",
1137            "-w",
1138        ];
1139
1140        let mut tokens = ShellKind::Posix
1141            .split(input)
1142            .context("invalid input")?
1143            .into_iter();
1144
1145        'outer: while let Some(arg) = tokens.next() {
1146            if ALLOWED_OPTS.contains(&(&arg as &str)) {
1147                args.push(arg.to_string());
1148                continue;
1149            }
1150            if arg == "-p" {
1151                port = tokens.next().and_then(|arg| arg.parse().ok());
1152                continue;
1153            } else if let Some(p) = arg.strip_prefix("-p") {
1154                port = p.parse().ok();
1155                continue;
1156            }
1157            if arg == "-l" {
1158                username = tokens.next();
1159                continue;
1160            } else if let Some(l) = arg.strip_prefix("-l") {
1161                username = Some(l.to_string());
1162                continue;
1163            }
1164            if arg == "-L" || arg.starts_with("-L") {
1165                let forward_spec = if arg == "-L" {
1166                    tokens.next()
1167                } else {
1168                    Some(arg.strip_prefix("-L").unwrap().to_string())
1169                };
1170
1171                if let Some(spec) = forward_spec {
1172                    port_forwards.push(parse_port_forward_spec(&spec)?);
1173                } else {
1174                    anyhow::bail!("Missing port forward format");
1175                }
1176            }
1177
1178            for a in ALLOWED_ARGS {
1179                if arg == *a {
1180                    args.push(arg);
1181                    if let Some(next) = tokens.next() {
1182                        args.push(next);
1183                    }
1184                    continue 'outer;
1185                } else if arg.starts_with(a) {
1186                    args.push(arg);
1187                    continue 'outer;
1188                }
1189            }
1190            if arg.starts_with("-") || hostname.is_some() {
1191                anyhow::bail!("unsupported argument: {:?}", arg);
1192            }
1193            let mut input = &arg as &str;
1194            // Destination might be: username1@username2@ip2@ip1
1195            if let Some((u, rest)) = input.rsplit_once('@') {
1196                input = rest;
1197                username = Some(u.to_string());
1198            }
1199            if let Some((rest, p)) = input.split_once(':') {
1200                input = rest;
1201                port = p.parse().ok()
1202            }
1203            hostname = Some(input.to_string())
1204        }
1205
1206        let Some(hostname) = hostname else {
1207            anyhow::bail!("missing hostname");
1208        };
1209
1210        let port_forwards = match port_forwards.len() {
1211            0 => None,
1212            _ => Some(port_forwards),
1213        };
1214
1215        Ok(Self {
1216            host: hostname,
1217            username,
1218            port,
1219            port_forwards,
1220            args: Some(args),
1221            password: None,
1222            nickname: None,
1223            upload_binary_over_ssh: false,
1224        })
1225    }
1226
1227    pub fn ssh_url(&self) -> String {
1228        let mut result = String::from("ssh://");
1229        if let Some(username) = &self.username {
1230            // Username might be: username1@username2@ip2
1231            let username = urlencoding::encode(username);
1232            result.push_str(&username);
1233            result.push('@');
1234        }
1235        result.push_str(&self.host);
1236        if let Some(port) = self.port {
1237            result.push(':');
1238            result.push_str(&port.to_string());
1239        }
1240        result
1241    }
1242
1243    pub fn additional_args_for_scp(&self) -> Vec<String> {
1244        self.args.iter().flatten().cloned().collect::<Vec<String>>()
1245    }
1246
1247    pub fn additional_args(&self) -> Vec<String> {
1248        let mut args = self.additional_args_for_scp();
1249
1250        if let Some(forwards) = &self.port_forwards {
1251            args.extend(forwards.iter().map(|pf| {
1252                let local_host = match &pf.local_host {
1253                    Some(host) => host,
1254                    None => "localhost",
1255                };
1256                let remote_host = match &pf.remote_host {
1257                    Some(host) => host,
1258                    None => "localhost",
1259                };
1260
1261                format!(
1262                    "-L{}:{}:{}:{}",
1263                    local_host, pf.local_port, remote_host, pf.remote_port
1264                )
1265            }));
1266        }
1267
1268        args
1269    }
1270
1271    fn scp_url(&self) -> String {
1272        if let Some(username) = &self.username {
1273            format!("{}@{}", username, self.host)
1274        } else {
1275            self.host.clone()
1276        }
1277    }
1278
1279    pub fn connection_string(&self) -> String {
1280        let host = if let Some(username) = &self.username {
1281            format!("{}@{}", username, self.host)
1282        } else {
1283            self.host.clone()
1284        };
1285        if let Some(port) = &self.port {
1286            format!("{}:{}", host, port)
1287        } else {
1288            host
1289        }
1290    }
1291}
1292
1293fn build_command(
1294    input_program: Option<String>,
1295    input_args: &[String],
1296    input_env: &HashMap<String, String>,
1297    working_dir: Option<String>,
1298    port_forward: Option<(u16, String, u16)>,
1299    ssh_env: HashMap<String, String>,
1300    ssh_path_style: PathStyle,
1301    ssh_shell: &str,
1302    ssh_shell_kind: ShellKind,
1303    ssh_args: Vec<String>,
1304) -> Result<CommandTemplate> {
1305    use std::fmt::Write as _;
1306
1307    let mut exec = String::new();
1308    if let Some(working_dir) = working_dir {
1309        let working_dir = RemotePathBuf::new(working_dir, ssh_path_style).to_string();
1310
1311        // shlex will wrap the command in single quotes (''), disabling ~ expansion,
1312        // replace with with something that works
1313        const TILDE_PREFIX: &'static str = "~/";
1314        if working_dir.starts_with(TILDE_PREFIX) {
1315            let working_dir = working_dir.trim_start_matches("~").trim_start_matches("/");
1316            write!(
1317                exec,
1318                "cd \"$HOME/{working_dir}\" {} ",
1319                ssh_shell_kind.sequential_and_commands_separator()
1320            )?;
1321        } else {
1322            write!(
1323                exec,
1324                "cd \"{working_dir}\" {} ",
1325                ssh_shell_kind.sequential_and_commands_separator()
1326            )?;
1327        }
1328    } else {
1329        write!(
1330            exec,
1331            "cd {} ",
1332            ssh_shell_kind.sequential_and_commands_separator()
1333        )?;
1334    };
1335    write!(exec, "exec env ")?;
1336
1337    for (k, v) in input_env.iter() {
1338        write!(
1339            exec,
1340            "{}={} ",
1341            k,
1342            ssh_shell_kind.try_quote(v).context("shell quoting")?
1343        )?;
1344    }
1345
1346    if let Some(input_program) = input_program {
1347        write!(
1348            exec,
1349            "{}",
1350            ssh_shell_kind
1351                .try_quote_prefix_aware(&input_program)
1352                .context("shell quoting")?
1353        )?;
1354        for arg in input_args {
1355            let arg = ssh_shell_kind.try_quote(&arg).context("shell quoting")?;
1356            write!(exec, " {}", &arg)?;
1357        }
1358    } else {
1359        write!(exec, "{ssh_shell} -l")?;
1360    };
1361
1362    let mut args = Vec::new();
1363    args.extend(ssh_args);
1364
1365    if let Some((local_port, host, remote_port)) = port_forward {
1366        args.push("-L".into());
1367        args.push(format!("{local_port}:{host}:{remote_port}"));
1368    }
1369
1370    args.push("-t".into());
1371    args.push(exec);
1372    Ok(CommandTemplate {
1373        program: "ssh".into(),
1374        args,
1375        env: ssh_env,
1376    })
1377}
1378
1379#[cfg(test)]
1380mod tests {
1381    use super::*;
1382
1383    #[test]
1384    fn test_build_command() -> Result<()> {
1385        let mut input_env = HashMap::default();
1386        input_env.insert("INPUT_VA".to_string(), "val".to_string());
1387        let mut env = HashMap::default();
1388        env.insert("SSH_VAR".to_string(), "ssh-val".to_string());
1389
1390        let command = build_command(
1391            Some("remote_program".to_string()),
1392            &["arg1".to_string(), "arg2".to_string()],
1393            &input_env,
1394            Some("~/work".to_string()),
1395            None,
1396            env.clone(),
1397            PathStyle::Posix,
1398            "/bin/fish",
1399            ShellKind::Fish,
1400            vec!["-p".to_string(), "2222".to_string()],
1401        )?;
1402
1403        assert_eq!(command.program, "ssh");
1404        assert_eq!(
1405            command.args.iter().map(String::as_str).collect::<Vec<_>>(),
1406            [
1407                "-p",
1408                "2222",
1409                "-t",
1410                "cd \"$HOME/work\" && exec env INPUT_VA=val remote_program arg1 arg2"
1411            ]
1412        );
1413        assert_eq!(command.env, env);
1414
1415        let mut input_env = HashMap::default();
1416        input_env.insert("INPUT_VA".to_string(), "val".to_string());
1417        let mut env = HashMap::default();
1418        env.insert("SSH_VAR".to_string(), "ssh-val".to_string());
1419
1420        let command = build_command(
1421            None,
1422            &["arg1".to_string(), "arg2".to_string()],
1423            &input_env,
1424            None,
1425            Some((1, "foo".to_owned(), 2)),
1426            env.clone(),
1427            PathStyle::Posix,
1428            "/bin/fish",
1429            ShellKind::Fish,
1430            vec!["-p".to_string(), "2222".to_string()],
1431        )?;
1432
1433        assert_eq!(command.program, "ssh");
1434        assert_eq!(
1435            command.args.iter().map(String::as_str).collect::<Vec<_>>(),
1436            [
1437                "-p",
1438                "2222",
1439                "-L",
1440                "1:foo:2",
1441                "-t",
1442                "cd && exec env INPUT_VA=val /bin/fish -l"
1443            ]
1444        );
1445        assert_eq!(command.env, env);
1446
1447        Ok(())
1448    }
1449
1450    #[test]
1451    fn scp_args_exclude_port_forward_flags() {
1452        let options = SshConnectionOptions {
1453            host: "example.com".into(),
1454            args: Some(vec![
1455                "-p".to_string(),
1456                "2222".to_string(),
1457                "-o".to_string(),
1458                "StrictHostKeyChecking=no".to_string(),
1459            ]),
1460            port_forwards: Some(vec![SshPortForwardOption {
1461                local_host: Some("127.0.0.1".to_string()),
1462                local_port: 8080,
1463                remote_host: Some("127.0.0.1".to_string()),
1464                remote_port: 80,
1465            }]),
1466            ..Default::default()
1467        };
1468
1469        let ssh_args = options.additional_args();
1470        assert!(
1471            ssh_args.iter().any(|arg| arg.starts_with("-L")),
1472            "expected ssh args to include port-forward: {ssh_args:?}"
1473        );
1474
1475        let scp_args = options.additional_args_for_scp();
1476        assert_eq!(
1477            scp_args,
1478            vec![
1479                "-p".to_string(),
1480                "2222".to_string(),
1481                "-o".to_string(),
1482                "StrictHostKeyChecking=no".to_string()
1483            ]
1484        );
1485        assert!(
1486            scp_args.iter().all(|arg| !arg.starts_with("-L")),
1487            "scp args should not contain port forward flags: {scp_args:?}"
1488        );
1489    }
1490}