ssh_session.rs

   1use crate::{
   2    json_log::LogRecord,
   3    protocol::{
   4        MESSAGE_LEN_SIZE, MessageId, message_len_from_buffer, read_message_with_len, write_message,
   5    },
   6    proxy::ProxyLaunchError,
   7};
   8use anyhow::{Context as _, Result, anyhow};
   9use async_trait::async_trait;
  10use collections::HashMap;
  11use futures::{
  12    AsyncReadExt as _, Future, FutureExt as _, StreamExt as _,
  13    channel::{
  14        mpsc::{self, Sender, UnboundedReceiver, UnboundedSender},
  15        oneshot,
  16    },
  17    future::{BoxFuture, Shared},
  18    select, select_biased,
  19};
  20use gpui::{
  21    App, AppContext as _, AsyncApp, BackgroundExecutor, BorrowAppContext, Context, Entity,
  22    EventEmitter, Global, SemanticVersion, Task, WeakEntity,
  23};
  24use itertools::Itertools;
  25use parking_lot::Mutex;
  26
  27use release_channel::{AppCommitSha, AppVersion, ReleaseChannel};
  28use rpc::{
  29    AnyProtoClient, EntityMessageSubscriber, ErrorExt, ProtoClient, ProtoMessageHandlerSet,
  30    RpcError,
  31    proto::{self, Envelope, EnvelopedMessage, PeerId, RequestMessage, build_typed_envelope},
  32};
  33use schemars::JsonSchema;
  34use serde::{Deserialize, Serialize};
  35use smol::{
  36    fs,
  37    process::{self, Child, Stdio},
  38};
  39use std::{
  40    any::TypeId,
  41    collections::VecDeque,
  42    fmt, iter,
  43    ops::ControlFlow,
  44    path::{Path, PathBuf},
  45    sync::{
  46        Arc, Weak,
  47        atomic::{AtomicU32, AtomicU64, Ordering::SeqCst},
  48    },
  49    time::{Duration, Instant},
  50};
  51use tempfile::TempDir;
  52use util::ResultExt;
  53
  54#[derive(
  55    Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, serde::Serialize, serde::Deserialize,
  56)]
  57pub struct SshProjectId(pub u64);
  58
  59#[derive(Clone)]
  60pub struct SshSocket {
  61    connection_options: SshConnectionOptions,
  62    socket_path: PathBuf,
  63}
  64
  65#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize, JsonSchema)]
  66pub struct SshPortForwardOption {
  67    #[serde(skip_serializing_if = "Option::is_none")]
  68    pub local_host: Option<String>,
  69    pub local_port: u16,
  70    #[serde(skip_serializing_if = "Option::is_none")]
  71    pub remote_host: Option<String>,
  72    pub remote_port: u16,
  73}
  74
  75#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]
  76pub struct SshConnectionOptions {
  77    pub host: String,
  78    pub username: Option<String>,
  79    pub port: Option<u16>,
  80    pub password: Option<String>,
  81    pub args: Option<Vec<String>>,
  82    pub port_forwards: Option<Vec<SshPortForwardOption>>,
  83
  84    pub nickname: Option<String>,
  85    pub upload_binary_over_ssh: bool,
  86}
  87
  88#[macro_export]
  89macro_rules! shell_script {
  90    ($fmt:expr, $($name:ident = $arg:expr),+ $(,)?) => {{
  91        format!(
  92            $fmt,
  93            $(
  94                $name = shlex::try_quote($arg).unwrap()
  95            ),+
  96        )
  97    }};
  98}
  99
 100fn parse_port_number(port_str: &str) -> Result<u16> {
 101    port_str
 102        .parse()
 103        .with_context(|| format!("parsing port number: {port_str}"))
 104}
 105
 106fn parse_port_forward_spec(spec: &str) -> Result<SshPortForwardOption> {
 107    let parts: Vec<&str> = spec.split(':').collect();
 108
 109    match parts.len() {
 110        4 => {
 111            let local_port = parse_port_number(parts[1])?;
 112            let remote_port = parse_port_number(parts[3])?;
 113
 114            Ok(SshPortForwardOption {
 115                local_host: Some(parts[0].to_string()),
 116                local_port,
 117                remote_host: Some(parts[2].to_string()),
 118                remote_port,
 119            })
 120        }
 121        3 => {
 122            let local_port = parse_port_number(parts[0])?;
 123            let remote_port = parse_port_number(parts[2])?;
 124
 125            Ok(SshPortForwardOption {
 126                local_host: None,
 127                local_port,
 128                remote_host: Some(parts[1].to_string()),
 129                remote_port,
 130            })
 131        }
 132        _ => anyhow::bail!("Invalid port forward format"),
 133    }
 134}
 135
 136impl SshConnectionOptions {
 137    pub fn parse_command_line(input: &str) -> Result<Self> {
 138        let input = input.trim_start_matches("ssh ");
 139        let mut hostname: Option<String> = None;
 140        let mut username: Option<String> = None;
 141        let mut port: Option<u16> = None;
 142        let mut args = Vec::new();
 143        let mut port_forwards: Vec<SshPortForwardOption> = Vec::new();
 144
 145        // disallowed: -E, -e, -F, -f, -G, -g, -M, -N, -n, -O, -q, -S, -s, -T, -t, -V, -v, -W
 146        const ALLOWED_OPTS: &[&str] = &[
 147            "-4", "-6", "-A", "-a", "-C", "-K", "-k", "-X", "-x", "-Y", "-y",
 148        ];
 149        const ALLOWED_ARGS: &[&str] = &[
 150            "-B", "-b", "-c", "-D", "-F", "-I", "-i", "-J", "-l", "-m", "-o", "-P", "-p", "-R",
 151            "-w",
 152        ];
 153
 154        let mut tokens = shlex::split(input).context("invalid input")?.into_iter();
 155
 156        'outer: while let Some(arg) = tokens.next() {
 157            if ALLOWED_OPTS.contains(&(&arg as &str)) {
 158                args.push(arg.to_string());
 159                continue;
 160            }
 161            if arg == "-p" {
 162                port = tokens.next().and_then(|arg| arg.parse().ok());
 163                continue;
 164            } else if let Some(p) = arg.strip_prefix("-p") {
 165                port = p.parse().ok();
 166                continue;
 167            }
 168            if arg == "-l" {
 169                username = tokens.next();
 170                continue;
 171            } else if let Some(l) = arg.strip_prefix("-l") {
 172                username = Some(l.to_string());
 173                continue;
 174            }
 175            if arg == "-L" || arg.starts_with("-L") {
 176                let forward_spec = if arg == "-L" {
 177                    tokens.next()
 178                } else {
 179                    Some(arg.strip_prefix("-L").unwrap().to_string())
 180                };
 181
 182                if let Some(spec) = forward_spec {
 183                    port_forwards.push(parse_port_forward_spec(&spec)?);
 184                } else {
 185                    anyhow::bail!("Missing port forward format");
 186                }
 187            }
 188
 189            for a in ALLOWED_ARGS {
 190                if arg == *a {
 191                    args.push(arg);
 192                    if let Some(next) = tokens.next() {
 193                        args.push(next);
 194                    }
 195                    continue 'outer;
 196                } else if arg.starts_with(a) {
 197                    args.push(arg);
 198                    continue 'outer;
 199                }
 200            }
 201            if arg.starts_with("-") || hostname.is_some() {
 202                anyhow::bail!("unsupported argument: {:?}", arg);
 203            }
 204            let mut input = &arg as &str;
 205            // Destination might be: username1@username2@ip2@ip1
 206            if let Some((u, rest)) = input.rsplit_once('@') {
 207                input = rest;
 208                username = Some(u.to_string());
 209            }
 210            if let Some((rest, p)) = input.split_once(':') {
 211                input = rest;
 212                port = p.parse().ok()
 213            }
 214            hostname = Some(input.to_string())
 215        }
 216
 217        let Some(hostname) = hostname else {
 218            anyhow::bail!("missing hostname");
 219        };
 220
 221        let port_forwards = match port_forwards.len() {
 222            0 => None,
 223            _ => Some(port_forwards),
 224        };
 225
 226        Ok(Self {
 227            host: hostname.to_string(),
 228            username: username.clone(),
 229            port,
 230            port_forwards,
 231            args: Some(args),
 232            password: None,
 233            nickname: None,
 234            upload_binary_over_ssh: false,
 235        })
 236    }
 237
 238    pub fn ssh_url(&self) -> String {
 239        let mut result = String::from("ssh://");
 240        if let Some(username) = &self.username {
 241            // Username might be: username1@username2@ip2
 242            let username = urlencoding::encode(username);
 243            result.push_str(&username);
 244            result.push('@');
 245        }
 246        result.push_str(&self.host);
 247        if let Some(port) = self.port {
 248            result.push(':');
 249            result.push_str(&port.to_string());
 250        }
 251        result
 252    }
 253
 254    pub fn additional_args(&self) -> Vec<String> {
 255        let mut args = self.args.iter().flatten().cloned().collect::<Vec<String>>();
 256
 257        if let Some(forwards) = &self.port_forwards {
 258            args.extend(forwards.iter().map(|pf| {
 259                let local_host = match &pf.local_host {
 260                    Some(host) => host,
 261                    None => "localhost",
 262                };
 263                let remote_host = match &pf.remote_host {
 264                    Some(host) => host,
 265                    None => "localhost",
 266                };
 267
 268                format!(
 269                    "-L{}:{}:{}:{}",
 270                    local_host, pf.local_port, remote_host, pf.remote_port
 271                )
 272            }));
 273        }
 274
 275        args
 276    }
 277
 278    fn scp_url(&self) -> String {
 279        if let Some(username) = &self.username {
 280            format!("{}@{}", username, self.host)
 281        } else {
 282            self.host.clone()
 283        }
 284    }
 285
 286    pub fn connection_string(&self) -> String {
 287        let host = if let Some(username) = &self.username {
 288            format!("{}@{}", username, self.host)
 289        } else {
 290            self.host.clone()
 291        };
 292        if let Some(port) = &self.port {
 293            format!("{}:{}", host, port)
 294        } else {
 295            host
 296        }
 297    }
 298}
 299
 300#[derive(Copy, Clone, Debug)]
 301pub struct SshPlatform {
 302    pub os: &'static str,
 303    pub arch: &'static str,
 304}
 305
 306impl SshPlatform {
 307    pub fn triple(&self) -> Option<String> {
 308        Some(format!(
 309            "{}-{}",
 310            self.arch,
 311            match self.os {
 312                "linux" => "unknown-linux-gnu",
 313                "macos" => "apple-darwin",
 314                _ => return None,
 315            }
 316        ))
 317    }
 318}
 319
 320pub trait SshClientDelegate: Send + Sync {
 321    fn ask_password(&self, prompt: String, tx: oneshot::Sender<String>, cx: &mut AsyncApp);
 322    fn get_download_params(
 323        &self,
 324        platform: SshPlatform,
 325        release_channel: ReleaseChannel,
 326        version: Option<SemanticVersion>,
 327        cx: &mut AsyncApp,
 328    ) -> Task<Result<Option<(String, String)>>>;
 329
 330    fn download_server_binary_locally(
 331        &self,
 332        platform: SshPlatform,
 333        release_channel: ReleaseChannel,
 334        version: Option<SemanticVersion>,
 335        cx: &mut AsyncApp,
 336    ) -> Task<Result<PathBuf>>;
 337    fn set_status(&self, status: Option<&str>, cx: &mut AsyncApp);
 338}
 339
 340impl SshSocket {
 341    // :WARNING: ssh unquotes arguments when executing on the remote :WARNING:
 342    // e.g. $ ssh host sh -c 'ls -l' is equivalent to $ ssh host sh -c ls -l
 343    // and passes -l as an argument to sh, not to ls.
 344    // Furthermore, some setups (e.g. Coder) will change directory when SSH'ing
 345    // into a machine. You must use `cd` to get back to $HOME.
 346    // You need to do it like this: $ ssh host "cd; sh -c 'ls -l /tmp'"
 347    fn ssh_command(&self, program: &str, args: &[&str]) -> process::Command {
 348        let mut command = util::command::new_smol_command("ssh");
 349        let to_run = iter::once(&program)
 350            .chain(args.iter())
 351            .map(|token| {
 352                // We're trying to work with: sh, bash, zsh, fish, tcsh, ...?
 353                debug_assert!(
 354                    !token.contains('\n'),
 355                    "multiline arguments do not work in all shells"
 356                );
 357                shlex::try_quote(token).unwrap()
 358            })
 359            .join(" ");
 360        let to_run = format!("cd; {to_run}");
 361        log::debug!("ssh {} {:?}", self.connection_options.ssh_url(), to_run);
 362        self.ssh_options(&mut command)
 363            .arg(self.connection_options.ssh_url())
 364            .arg(to_run);
 365        command
 366    }
 367
 368    async fn run_command(&self, program: &str, args: &[&str]) -> Result<String> {
 369        let output = self.ssh_command(program, args).output().await?;
 370        anyhow::ensure!(
 371            output.status.success(),
 372            "failed to run command: {}",
 373            String::from_utf8_lossy(&output.stderr)
 374        );
 375        Ok(String::from_utf8_lossy(&output.stdout).to_string())
 376    }
 377
 378    fn ssh_options<'a>(&self, command: &'a mut process::Command) -> &'a mut process::Command {
 379        command
 380            .stdin(Stdio::piped())
 381            .stdout(Stdio::piped())
 382            .stderr(Stdio::piped())
 383            .args(["-o", "ControlMaster=no", "-o"])
 384            .arg(format!("ControlPath={}", self.socket_path.display()))
 385    }
 386
 387    fn ssh_args(&self) -> Vec<String> {
 388        vec![
 389            "-o".to_string(),
 390            "ControlMaster=no".to_string(),
 391            "-o".to_string(),
 392            format!("ControlPath={}", self.socket_path.display()),
 393            self.connection_options.ssh_url(),
 394        ]
 395    }
 396}
 397
 398const MAX_MISSED_HEARTBEATS: usize = 5;
 399const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5);
 400const HEARTBEAT_TIMEOUT: Duration = Duration::from_secs(5);
 401
 402const MAX_RECONNECT_ATTEMPTS: usize = 3;
 403
 404enum State {
 405    Connecting,
 406    Connected {
 407        ssh_connection: Arc<dyn RemoteConnection>,
 408        delegate: Arc<dyn SshClientDelegate>,
 409
 410        multiplex_task: Task<Result<()>>,
 411        heartbeat_task: Task<Result<()>>,
 412    },
 413    HeartbeatMissed {
 414        missed_heartbeats: usize,
 415
 416        ssh_connection: Arc<dyn RemoteConnection>,
 417        delegate: Arc<dyn SshClientDelegate>,
 418
 419        multiplex_task: Task<Result<()>>,
 420        heartbeat_task: Task<Result<()>>,
 421    },
 422    Reconnecting,
 423    ReconnectFailed {
 424        ssh_connection: Arc<dyn RemoteConnection>,
 425        delegate: Arc<dyn SshClientDelegate>,
 426
 427        error: anyhow::Error,
 428        attempts: usize,
 429    },
 430    ReconnectExhausted,
 431    ServerNotRunning,
 432}
 433
 434impl fmt::Display for State {
 435    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 436        match self {
 437            Self::Connecting => write!(f, "connecting"),
 438            Self::Connected { .. } => write!(f, "connected"),
 439            Self::Reconnecting => write!(f, "reconnecting"),
 440            Self::ReconnectFailed { .. } => write!(f, "reconnect failed"),
 441            Self::ReconnectExhausted => write!(f, "reconnect exhausted"),
 442            Self::HeartbeatMissed { .. } => write!(f, "heartbeat missed"),
 443            Self::ServerNotRunning { .. } => write!(f, "server not running"),
 444        }
 445    }
 446}
 447
 448impl State {
 449    fn ssh_connection(&self) -> Option<&dyn RemoteConnection> {
 450        match self {
 451            Self::Connected { ssh_connection, .. } => Some(ssh_connection.as_ref()),
 452            Self::HeartbeatMissed { ssh_connection, .. } => Some(ssh_connection.as_ref()),
 453            Self::ReconnectFailed { ssh_connection, .. } => Some(ssh_connection.as_ref()),
 454            _ => None,
 455        }
 456    }
 457
 458    fn can_reconnect(&self) -> bool {
 459        match self {
 460            Self::Connected { .. }
 461            | Self::HeartbeatMissed { .. }
 462            | Self::ReconnectFailed { .. } => true,
 463            State::Connecting
 464            | State::Reconnecting
 465            | State::ReconnectExhausted
 466            | State::ServerNotRunning => false,
 467        }
 468    }
 469
 470    fn is_reconnect_failed(&self) -> bool {
 471        matches!(self, Self::ReconnectFailed { .. })
 472    }
 473
 474    fn is_reconnect_exhausted(&self) -> bool {
 475        matches!(self, Self::ReconnectExhausted { .. })
 476    }
 477
 478    fn is_server_not_running(&self) -> bool {
 479        matches!(self, Self::ServerNotRunning)
 480    }
 481
 482    fn is_reconnecting(&self) -> bool {
 483        matches!(self, Self::Reconnecting { .. })
 484    }
 485
 486    fn heartbeat_recovered(self) -> Self {
 487        match self {
 488            Self::HeartbeatMissed {
 489                ssh_connection,
 490                delegate,
 491                multiplex_task,
 492                heartbeat_task,
 493                ..
 494            } => Self::Connected {
 495                ssh_connection,
 496                delegate,
 497                multiplex_task,
 498                heartbeat_task,
 499            },
 500            _ => self,
 501        }
 502    }
 503
 504    fn heartbeat_missed(self) -> Self {
 505        match self {
 506            Self::Connected {
 507                ssh_connection,
 508                delegate,
 509                multiplex_task,
 510                heartbeat_task,
 511            } => Self::HeartbeatMissed {
 512                missed_heartbeats: 1,
 513                ssh_connection,
 514                delegate,
 515                multiplex_task,
 516                heartbeat_task,
 517            },
 518            Self::HeartbeatMissed {
 519                missed_heartbeats,
 520                ssh_connection,
 521                delegate,
 522                multiplex_task,
 523                heartbeat_task,
 524            } => Self::HeartbeatMissed {
 525                missed_heartbeats: missed_heartbeats + 1,
 526                ssh_connection,
 527                delegate,
 528                multiplex_task,
 529                heartbeat_task,
 530            },
 531            _ => self,
 532        }
 533    }
 534}
 535
 536/// The state of the ssh connection.
 537#[derive(Clone, Copy, Debug, PartialEq, Eq)]
 538pub enum ConnectionState {
 539    Connecting,
 540    Connected,
 541    HeartbeatMissed,
 542    Reconnecting,
 543    Disconnected,
 544}
 545
 546impl From<&State> for ConnectionState {
 547    fn from(value: &State) -> Self {
 548        match value {
 549            State::Connecting => Self::Connecting,
 550            State::Connected { .. } => Self::Connected,
 551            State::Reconnecting | State::ReconnectFailed { .. } => Self::Reconnecting,
 552            State::HeartbeatMissed { .. } => Self::HeartbeatMissed,
 553            State::ReconnectExhausted => Self::Disconnected,
 554            State::ServerNotRunning => Self::Disconnected,
 555        }
 556    }
 557}
 558
 559pub struct SshRemoteClient {
 560    client: Arc<ChannelClient>,
 561    unique_identifier: String,
 562    connection_options: SshConnectionOptions,
 563    state: Arc<Mutex<Option<State>>>,
 564}
 565
 566#[derive(Debug)]
 567pub enum SshRemoteEvent {
 568    Disconnected,
 569}
 570
 571impl EventEmitter<SshRemoteEvent> for SshRemoteClient {}
 572
 573// Identifies the socket on the remote server so that reconnects
 574// can re-join the same project.
 575pub enum ConnectionIdentifier {
 576    Setup(u64),
 577    Workspace(i64),
 578}
 579
 580static NEXT_ID: AtomicU64 = AtomicU64::new(1);
 581
 582impl ConnectionIdentifier {
 583    pub fn setup() -> Self {
 584        Self::Setup(NEXT_ID.fetch_add(1, SeqCst))
 585    }
 586    // This string gets used in a socket name, and so must be relatively short.
 587    // The total length of:
 588    //   /home/{username}/.local/share/zed/server_state/{name}/stdout.sock
 589    // Must be less than about 100 characters
 590    //   https://unix.stackexchange.com/questions/367008/why-is-socket-path-length-limited-to-a-hundred-chars
 591    // So our strings should be at most 20 characters or so.
 592    fn to_string(&self, cx: &App) -> String {
 593        let identifier_prefix = match ReleaseChannel::global(cx) {
 594            ReleaseChannel::Stable => "".to_string(),
 595            release_channel => format!("{}-", release_channel.dev_name()),
 596        };
 597        match self {
 598            Self::Setup(setup_id) => format!("{identifier_prefix}setup-{setup_id}"),
 599            Self::Workspace(workspace_id) => {
 600                format!("{identifier_prefix}workspace-{workspace_id}",)
 601            }
 602        }
 603    }
 604}
 605
 606impl SshRemoteClient {
 607    pub fn new(
 608        unique_identifier: ConnectionIdentifier,
 609        connection_options: SshConnectionOptions,
 610        cancellation: oneshot::Receiver<()>,
 611        delegate: Arc<dyn SshClientDelegate>,
 612        cx: &mut App,
 613    ) -> Task<Result<Option<Entity<Self>>>> {
 614        let unique_identifier = unique_identifier.to_string(cx);
 615        cx.spawn(async move |cx| {
 616            let success = Box::pin(async move {
 617                let (outgoing_tx, outgoing_rx) = mpsc::unbounded::<Envelope>();
 618                let (incoming_tx, incoming_rx) = mpsc::unbounded::<Envelope>();
 619                let (connection_activity_tx, connection_activity_rx) = mpsc::channel::<()>(1);
 620
 621                let client =
 622                    cx.update(|cx| ChannelClient::new(incoming_rx, outgoing_tx, cx, "client"))?;
 623                let this = cx.new(|_| Self {
 624                    client: client.clone(),
 625                    unique_identifier: unique_identifier.clone(),
 626                    connection_options: connection_options.clone(),
 627                    state: Arc::new(Mutex::new(Some(State::Connecting))),
 628                })?;
 629
 630                let ssh_connection = cx
 631                    .update(|cx| {
 632                        cx.update_default_global(|pool: &mut ConnectionPool, cx| {
 633                            pool.connect(connection_options, &delegate, cx)
 634                        })
 635                    })?
 636                    .await
 637                    .map_err(|e| e.cloned())?;
 638
 639                let io_task = ssh_connection.start_proxy(
 640                    unique_identifier,
 641                    false,
 642                    incoming_tx,
 643                    outgoing_rx,
 644                    connection_activity_tx,
 645                    delegate.clone(),
 646                    cx,
 647                );
 648
 649                let multiplex_task = Self::monitor(this.downgrade(), io_task, &cx);
 650
 651                if let Err(error) = client.ping(HEARTBEAT_TIMEOUT).await {
 652                    log::error!("failed to establish connection: {}", error);
 653                    return Err(error);
 654                }
 655
 656                let heartbeat_task = Self::heartbeat(this.downgrade(), connection_activity_rx, cx);
 657
 658                this.update(cx, |this, _| {
 659                    *this.state.lock() = Some(State::Connected {
 660                        ssh_connection,
 661                        delegate,
 662                        multiplex_task,
 663                        heartbeat_task,
 664                    });
 665                })?;
 666
 667                Ok(Some(this))
 668            });
 669
 670            select! {
 671                _ = cancellation.fuse() => {
 672                    Ok(None)
 673                }
 674                result = success.fuse() =>  result
 675            }
 676        })
 677    }
 678
 679    pub fn shutdown_processes<T: RequestMessage>(
 680        &self,
 681        shutdown_request: Option<T>,
 682        executor: BackgroundExecutor,
 683    ) -> Option<impl Future<Output = ()> + use<T>> {
 684        let state = self.state.lock().take()?;
 685        log::info!("shutting down ssh processes");
 686
 687        let State::Connected {
 688            multiplex_task,
 689            heartbeat_task,
 690            ssh_connection,
 691            delegate,
 692        } = state
 693        else {
 694            return None;
 695        };
 696
 697        let client = self.client.clone();
 698
 699        Some(async move {
 700            if let Some(shutdown_request) = shutdown_request {
 701                client.send(shutdown_request).log_err();
 702                // We wait 50ms instead of waiting for a response, because
 703                // waiting for a response would require us to wait on the main thread
 704                // which we want to avoid in an `on_app_quit` callback.
 705                executor.timer(Duration::from_millis(50)).await;
 706            }
 707
 708            // Drop `multiplex_task` because it owns our ssh_proxy_process, which is a
 709            // child of master_process.
 710            drop(multiplex_task);
 711            // Now drop the rest of state, which kills master process.
 712            drop(heartbeat_task);
 713            drop(ssh_connection);
 714            drop(delegate);
 715        })
 716    }
 717
 718    fn reconnect(&mut self, cx: &mut Context<Self>) -> Result<()> {
 719        let mut lock = self.state.lock();
 720
 721        let can_reconnect = lock
 722            .as_ref()
 723            .map(|state| state.can_reconnect())
 724            .unwrap_or(false);
 725        if !can_reconnect {
 726            log::info!("aborting reconnect, because not in state that allows reconnecting");
 727            let error = if let Some(state) = lock.as_ref() {
 728                format!("invalid state, cannot reconnect while in state {state}")
 729            } else {
 730                "no state set".to_string()
 731            };
 732            anyhow::bail!(error);
 733        }
 734
 735        let state = lock.take().unwrap();
 736        let (attempts, ssh_connection, delegate) = match state {
 737            State::Connected {
 738                ssh_connection,
 739                delegate,
 740                multiplex_task,
 741                heartbeat_task,
 742            }
 743            | State::HeartbeatMissed {
 744                ssh_connection,
 745                delegate,
 746                multiplex_task,
 747                heartbeat_task,
 748                ..
 749            } => {
 750                drop(multiplex_task);
 751                drop(heartbeat_task);
 752                (0, ssh_connection, delegate)
 753            }
 754            State::ReconnectFailed {
 755                attempts,
 756                ssh_connection,
 757                delegate,
 758                ..
 759            } => (attempts, ssh_connection, delegate),
 760            State::Connecting
 761            | State::Reconnecting
 762            | State::ReconnectExhausted
 763            | State::ServerNotRunning => unreachable!(),
 764        };
 765
 766        let attempts = attempts + 1;
 767        if attempts > MAX_RECONNECT_ATTEMPTS {
 768            log::error!(
 769                "Failed to reconnect to after {} attempts, giving up",
 770                MAX_RECONNECT_ATTEMPTS
 771            );
 772            drop(lock);
 773            self.set_state(State::ReconnectExhausted, cx);
 774            return Ok(());
 775        }
 776        drop(lock);
 777
 778        self.set_state(State::Reconnecting, cx);
 779
 780        log::info!("Trying to reconnect to ssh server... Attempt {}", attempts);
 781
 782        let unique_identifier = self.unique_identifier.clone();
 783        let client = self.client.clone();
 784        let reconnect_task = cx.spawn(async move |this, cx| {
 785            macro_rules! failed {
 786                ($error:expr, $attempts:expr, $ssh_connection:expr, $delegate:expr) => {
 787                    return State::ReconnectFailed {
 788                        error: anyhow!($error),
 789                        attempts: $attempts,
 790                        ssh_connection: $ssh_connection,
 791                        delegate: $delegate,
 792                    };
 793                };
 794            }
 795
 796            if let Err(error) = ssh_connection
 797                .kill()
 798                .await
 799                .context("Failed to kill ssh process")
 800            {
 801                failed!(error, attempts, ssh_connection, delegate);
 802            };
 803
 804            let connection_options = ssh_connection.connection_options();
 805
 806            let (outgoing_tx, outgoing_rx) = mpsc::unbounded::<Envelope>();
 807            let (incoming_tx, incoming_rx) = mpsc::unbounded::<Envelope>();
 808            let (connection_activity_tx, connection_activity_rx) = mpsc::channel::<()>(1);
 809
 810            let (ssh_connection, io_task) = match async {
 811                let ssh_connection = cx
 812                    .update_global(|pool: &mut ConnectionPool, cx| {
 813                        pool.connect(connection_options, &delegate, cx)
 814                    })?
 815                    .await
 816                    .map_err(|error| error.cloned())?;
 817
 818                let io_task = ssh_connection.start_proxy(
 819                    unique_identifier,
 820                    true,
 821                    incoming_tx,
 822                    outgoing_rx,
 823                    connection_activity_tx,
 824                    delegate.clone(),
 825                    cx,
 826                );
 827                anyhow::Ok((ssh_connection, io_task))
 828            }
 829            .await
 830            {
 831                Ok((ssh_connection, io_task)) => (ssh_connection, io_task),
 832                Err(error) => {
 833                    failed!(error, attempts, ssh_connection, delegate);
 834                }
 835            };
 836
 837            let multiplex_task = Self::monitor(this.clone(), io_task, &cx);
 838            client.reconnect(incoming_rx, outgoing_tx, &cx);
 839
 840            if let Err(error) = client.resync(HEARTBEAT_TIMEOUT).await {
 841                failed!(error, attempts, ssh_connection, delegate);
 842            };
 843
 844            State::Connected {
 845                ssh_connection,
 846                delegate,
 847                multiplex_task,
 848                heartbeat_task: Self::heartbeat(this.clone(), connection_activity_rx, cx),
 849            }
 850        });
 851
 852        cx.spawn(async move |this, cx| {
 853            let new_state = reconnect_task.await;
 854            this.update(cx, |this, cx| {
 855                this.try_set_state(cx, |old_state| {
 856                    if old_state.is_reconnecting() {
 857                        match &new_state {
 858                            State::Connecting
 859                            | State::Reconnecting { .. }
 860                            | State::HeartbeatMissed { .. }
 861                            | State::ServerNotRunning => {}
 862                            State::Connected { .. } => {
 863                                log::info!("Successfully reconnected");
 864                            }
 865                            State::ReconnectFailed {
 866                                error, attempts, ..
 867                            } => {
 868                                log::error!(
 869                                    "Reconnect attempt {} failed: {:?}. Starting new attempt...",
 870                                    attempts,
 871                                    error
 872                                );
 873                            }
 874                            State::ReconnectExhausted => {
 875                                log::error!("Reconnect attempt failed and all attempts exhausted");
 876                            }
 877                        }
 878                        Some(new_state)
 879                    } else {
 880                        None
 881                    }
 882                });
 883
 884                if this.state_is(State::is_reconnect_failed) {
 885                    this.reconnect(cx)
 886                } else if this.state_is(State::is_reconnect_exhausted) {
 887                    Ok(())
 888                } else {
 889                    log::debug!("State has transition from Reconnecting into new state while attempting reconnect.");
 890                    Ok(())
 891                }
 892            })
 893        })
 894        .detach_and_log_err(cx);
 895
 896        Ok(())
 897    }
 898
 899    fn heartbeat(
 900        this: WeakEntity<Self>,
 901        mut connection_activity_rx: mpsc::Receiver<()>,
 902        cx: &mut AsyncApp,
 903    ) -> Task<Result<()>> {
 904        let Ok(client) = this.read_with(cx, |this, _| this.client.clone()) else {
 905            return Task::ready(Err(anyhow!("SshRemoteClient lost")));
 906        };
 907
 908        cx.spawn(async move |cx| {
 909                let mut missed_heartbeats = 0;
 910
 911                let keepalive_timer = cx.background_executor().timer(HEARTBEAT_INTERVAL).fuse();
 912                futures::pin_mut!(keepalive_timer);
 913
 914                loop {
 915                    select_biased! {
 916                        result = connection_activity_rx.next().fuse() => {
 917                            if result.is_none() {
 918                                log::warn!("ssh heartbeat: connection activity channel has been dropped. stopping.");
 919                                return Ok(());
 920                            }
 921
 922                            if missed_heartbeats != 0 {
 923                                missed_heartbeats = 0;
 924                                let _ =this.update(cx, |this, mut cx| {
 925                                    this.handle_heartbeat_result(missed_heartbeats, &mut cx)
 926                                })?;
 927                            }
 928                        }
 929                        _ = keepalive_timer => {
 930                            log::debug!("Sending heartbeat to server...");
 931
 932                            let result = select_biased! {
 933                                _ = connection_activity_rx.next().fuse() => {
 934                                    Ok(())
 935                                }
 936                                ping_result = client.ping(HEARTBEAT_TIMEOUT).fuse() => {
 937                                    ping_result
 938                                }
 939                            };
 940
 941                            if result.is_err() {
 942                                missed_heartbeats += 1;
 943                                log::warn!(
 944                                    "No heartbeat from server after {:?}. Missed heartbeat {} out of {}.",
 945                                    HEARTBEAT_TIMEOUT,
 946                                    missed_heartbeats,
 947                                    MAX_MISSED_HEARTBEATS
 948                                );
 949                            } else if missed_heartbeats != 0 {
 950                                missed_heartbeats = 0;
 951                            } else {
 952                                continue;
 953                            }
 954
 955                            let result = this.update(cx, |this, mut cx| {
 956                                this.handle_heartbeat_result(missed_heartbeats, &mut cx)
 957                            })?;
 958                            if result.is_break() {
 959                                return Ok(());
 960                            }
 961                        }
 962                    }
 963
 964                    keepalive_timer.set(cx.background_executor().timer(HEARTBEAT_INTERVAL).fuse());
 965                }
 966
 967        })
 968    }
 969
 970    fn handle_heartbeat_result(
 971        &mut self,
 972        missed_heartbeats: usize,
 973        cx: &mut Context<Self>,
 974    ) -> ControlFlow<()> {
 975        let state = self.state.lock().take().unwrap();
 976        let next_state = if missed_heartbeats > 0 {
 977            state.heartbeat_missed()
 978        } else {
 979            state.heartbeat_recovered()
 980        };
 981
 982        self.set_state(next_state, cx);
 983
 984        if missed_heartbeats >= MAX_MISSED_HEARTBEATS {
 985            log::error!(
 986                "Missed last {} heartbeats. Reconnecting...",
 987                missed_heartbeats
 988            );
 989
 990            self.reconnect(cx)
 991                .context("failed to start reconnect process after missing heartbeats")
 992                .log_err();
 993            ControlFlow::Break(())
 994        } else {
 995            ControlFlow::Continue(())
 996        }
 997    }
 998
 999    fn monitor(
1000        this: WeakEntity<Self>,
1001        io_task: Task<Result<i32>>,
1002        cx: &AsyncApp,
1003    ) -> Task<Result<()>> {
1004        cx.spawn(async move |cx| {
1005            let result = io_task.await;
1006
1007            match result {
1008                Ok(exit_code) => {
1009                    if let Some(error) = ProxyLaunchError::from_exit_code(exit_code) {
1010                        match error {
1011                            ProxyLaunchError::ServerNotRunning => {
1012                                log::error!("failed to reconnect because server is not running");
1013                                this.update(cx, |this, cx| {
1014                                    this.set_state(State::ServerNotRunning, cx);
1015                                })?;
1016                            }
1017                        }
1018                    } else if exit_code > 0 {
1019                        log::error!("proxy process terminated unexpectedly");
1020                        this.update(cx, |this, cx| {
1021                            this.reconnect(cx).ok();
1022                        })?;
1023                    }
1024                }
1025                Err(error) => {
1026                    log::warn!("ssh io task died with error: {:?}. reconnecting...", error);
1027                    this.update(cx, |this, cx| {
1028                        this.reconnect(cx).ok();
1029                    })?;
1030                }
1031            }
1032
1033            Ok(())
1034        })
1035    }
1036
1037    fn state_is(&self, check: impl FnOnce(&State) -> bool) -> bool {
1038        self.state.lock().as_ref().map_or(false, check)
1039    }
1040
1041    fn try_set_state(&self, cx: &mut Context<Self>, map: impl FnOnce(&State) -> Option<State>) {
1042        let mut lock = self.state.lock();
1043        let new_state = lock.as_ref().and_then(map);
1044
1045        if let Some(new_state) = new_state {
1046            lock.replace(new_state);
1047            cx.notify();
1048        }
1049    }
1050
1051    fn set_state(&self, state: State, cx: &mut Context<Self>) {
1052        log::info!("setting state to '{}'", &state);
1053
1054        let is_reconnect_exhausted = state.is_reconnect_exhausted();
1055        let is_server_not_running = state.is_server_not_running();
1056        self.state.lock().replace(state);
1057
1058        if is_reconnect_exhausted || is_server_not_running {
1059            cx.emit(SshRemoteEvent::Disconnected);
1060        }
1061        cx.notify();
1062    }
1063
1064    pub fn subscribe_to_entity<E: 'static>(&self, remote_id: u64, entity: &Entity<E>) {
1065        self.client.subscribe_to_entity(remote_id, entity);
1066    }
1067
1068    pub fn ssh_args(&self) -> Option<Vec<String>> {
1069        self.state
1070            .lock()
1071            .as_ref()
1072            .and_then(|state| state.ssh_connection())
1073            .map(|ssh_connection| ssh_connection.ssh_args())
1074    }
1075
1076    pub fn upload_directory(
1077        &self,
1078        src_path: PathBuf,
1079        dest_path: PathBuf,
1080        cx: &App,
1081    ) -> Task<Result<()>> {
1082        let state = self.state.lock();
1083        let Some(connection) = state.as_ref().and_then(|state| state.ssh_connection()) else {
1084            return Task::ready(Err(anyhow!("no ssh connection")));
1085        };
1086        connection.upload_directory(src_path, dest_path, cx)
1087    }
1088
1089    pub fn proto_client(&self) -> AnyProtoClient {
1090        self.client.clone().into()
1091    }
1092
1093    pub fn connection_string(&self) -> String {
1094        self.connection_options.connection_string()
1095    }
1096
1097    pub fn connection_options(&self) -> SshConnectionOptions {
1098        self.connection_options.clone()
1099    }
1100
1101    pub fn connection_state(&self) -> ConnectionState {
1102        self.state
1103            .lock()
1104            .as_ref()
1105            .map(ConnectionState::from)
1106            .unwrap_or(ConnectionState::Disconnected)
1107    }
1108
1109    pub fn is_disconnected(&self) -> bool {
1110        self.connection_state() == ConnectionState::Disconnected
1111    }
1112
1113    #[cfg(any(test, feature = "test-support"))]
1114    pub fn simulate_disconnect(&self, client_cx: &mut App) -> Task<()> {
1115        let opts = self.connection_options();
1116        client_cx.spawn(async move |cx| {
1117            let connection = cx
1118                .update_global(|c: &mut ConnectionPool, _| {
1119                    if let Some(ConnectionPoolEntry::Connecting(c)) = c.connections.get(&opts) {
1120                        c.clone()
1121                    } else {
1122                        panic!("missing test connection")
1123                    }
1124                })
1125                .unwrap()
1126                .await
1127                .unwrap();
1128
1129            connection.simulate_disconnect(&cx);
1130        })
1131    }
1132
1133    #[cfg(any(test, feature = "test-support"))]
1134    pub fn fake_server(
1135        client_cx: &mut gpui::TestAppContext,
1136        server_cx: &mut gpui::TestAppContext,
1137    ) -> (SshConnectionOptions, Arc<ChannelClient>) {
1138        let port = client_cx
1139            .update(|cx| cx.default_global::<ConnectionPool>().connections.len() as u16 + 1);
1140        let opts = SshConnectionOptions {
1141            host: "<fake>".to_string(),
1142            port: Some(port),
1143            ..Default::default()
1144        };
1145        let (outgoing_tx, _) = mpsc::unbounded::<Envelope>();
1146        let (_, incoming_rx) = mpsc::unbounded::<Envelope>();
1147        let server_client =
1148            server_cx.update(|cx| ChannelClient::new(incoming_rx, outgoing_tx, cx, "fake-server"));
1149        let connection: Arc<dyn RemoteConnection> = Arc::new(fake::FakeRemoteConnection {
1150            connection_options: opts.clone(),
1151            server_cx: fake::SendableCx::new(server_cx),
1152            server_channel: server_client.clone(),
1153        });
1154
1155        client_cx.update(|cx| {
1156            cx.update_default_global(|c: &mut ConnectionPool, cx| {
1157                c.connections.insert(
1158                    opts.clone(),
1159                    ConnectionPoolEntry::Connecting(
1160                        cx.background_spawn({
1161                            let connection = connection.clone();
1162                            async move { Ok(connection.clone()) }
1163                        })
1164                        .shared(),
1165                    ),
1166                );
1167            })
1168        });
1169
1170        (opts, server_client)
1171    }
1172
1173    #[cfg(any(test, feature = "test-support"))]
1174    pub async fn fake_client(
1175        opts: SshConnectionOptions,
1176        client_cx: &mut gpui::TestAppContext,
1177    ) -> Entity<Self> {
1178        let (_tx, rx) = oneshot::channel();
1179        client_cx
1180            .update(|cx| {
1181                Self::new(
1182                    ConnectionIdentifier::setup(),
1183                    opts,
1184                    rx,
1185                    Arc::new(fake::Delegate),
1186                    cx,
1187                )
1188            })
1189            .await
1190            .unwrap()
1191            .unwrap()
1192    }
1193}
1194
1195enum ConnectionPoolEntry {
1196    Connecting(Shared<Task<Result<Arc<dyn RemoteConnection>, Arc<anyhow::Error>>>>),
1197    Connected(Weak<dyn RemoteConnection>),
1198}
1199
1200#[derive(Default)]
1201struct ConnectionPool {
1202    connections: HashMap<SshConnectionOptions, ConnectionPoolEntry>,
1203}
1204
1205impl Global for ConnectionPool {}
1206
1207impl ConnectionPool {
1208    pub fn connect(
1209        &mut self,
1210        opts: SshConnectionOptions,
1211        delegate: &Arc<dyn SshClientDelegate>,
1212        cx: &mut App,
1213    ) -> Shared<Task<Result<Arc<dyn RemoteConnection>, Arc<anyhow::Error>>>> {
1214        let connection = self.connections.get(&opts);
1215        match connection {
1216            Some(ConnectionPoolEntry::Connecting(task)) => {
1217                let delegate = delegate.clone();
1218                cx.spawn(async move |cx| {
1219                    delegate.set_status(Some("Waiting for existing connection attempt"), cx);
1220                })
1221                .detach();
1222                return task.clone();
1223            }
1224            Some(ConnectionPoolEntry::Connected(ssh)) => {
1225                if let Some(ssh) = ssh.upgrade() {
1226                    if !ssh.has_been_killed() {
1227                        return Task::ready(Ok(ssh)).shared();
1228                    }
1229                }
1230                self.connections.remove(&opts);
1231            }
1232            None => {}
1233        }
1234
1235        let task = cx
1236            .spawn({
1237                let opts = opts.clone();
1238                let delegate = delegate.clone();
1239                async move |cx| {
1240                    let connection = SshRemoteConnection::new(opts.clone(), delegate, cx)
1241                        .await
1242                        .map(|connection| Arc::new(connection) as Arc<dyn RemoteConnection>);
1243
1244                    cx.update_global(|pool: &mut Self, _| {
1245                        debug_assert!(matches!(
1246                            pool.connections.get(&opts),
1247                            Some(ConnectionPoolEntry::Connecting(_))
1248                        ));
1249                        match connection {
1250                            Ok(connection) => {
1251                                pool.connections.insert(
1252                                    opts.clone(),
1253                                    ConnectionPoolEntry::Connected(Arc::downgrade(&connection)),
1254                                );
1255                                Ok(connection)
1256                            }
1257                            Err(error) => {
1258                                pool.connections.remove(&opts);
1259                                Err(Arc::new(error))
1260                            }
1261                        }
1262                    })?
1263                }
1264            })
1265            .shared();
1266
1267        self.connections
1268            .insert(opts.clone(), ConnectionPoolEntry::Connecting(task.clone()));
1269        task
1270    }
1271}
1272
1273impl From<SshRemoteClient> for AnyProtoClient {
1274    fn from(client: SshRemoteClient) -> Self {
1275        AnyProtoClient::new(client.client.clone())
1276    }
1277}
1278
1279#[async_trait(?Send)]
1280trait RemoteConnection: Send + Sync {
1281    fn start_proxy(
1282        &self,
1283        unique_identifier: String,
1284        reconnect: bool,
1285        incoming_tx: UnboundedSender<Envelope>,
1286        outgoing_rx: UnboundedReceiver<Envelope>,
1287        connection_activity_tx: Sender<()>,
1288        delegate: Arc<dyn SshClientDelegate>,
1289        cx: &mut AsyncApp,
1290    ) -> Task<Result<i32>>;
1291    fn upload_directory(&self, src_path: PathBuf, dest_path: PathBuf, cx: &App)
1292    -> Task<Result<()>>;
1293    async fn kill(&self) -> Result<()>;
1294    fn has_been_killed(&self) -> bool;
1295    fn ssh_args(&self) -> Vec<String>;
1296    fn connection_options(&self) -> SshConnectionOptions;
1297
1298    #[cfg(any(test, feature = "test-support"))]
1299    fn simulate_disconnect(&self, _: &AsyncApp) {}
1300}
1301
1302struct SshRemoteConnection {
1303    socket: SshSocket,
1304    master_process: Mutex<Option<Child>>,
1305    remote_binary_path: Option<PathBuf>,
1306    _temp_dir: TempDir,
1307}
1308
1309#[async_trait(?Send)]
1310impl RemoteConnection for SshRemoteConnection {
1311    async fn kill(&self) -> Result<()> {
1312        let Some(mut process) = self.master_process.lock().take() else {
1313            return Ok(());
1314        };
1315        process.kill().ok();
1316        process.status().await?;
1317        Ok(())
1318    }
1319
1320    fn has_been_killed(&self) -> bool {
1321        self.master_process.lock().is_none()
1322    }
1323
1324    fn ssh_args(&self) -> Vec<String> {
1325        self.socket.ssh_args()
1326    }
1327
1328    fn connection_options(&self) -> SshConnectionOptions {
1329        self.socket.connection_options.clone()
1330    }
1331
1332    fn upload_directory(
1333        &self,
1334        src_path: PathBuf,
1335        dest_path: PathBuf,
1336        cx: &App,
1337    ) -> Task<Result<()>> {
1338        let mut command = util::command::new_smol_command("scp");
1339        let output = self
1340            .socket
1341            .ssh_options(&mut command)
1342            .args(
1343                self.socket
1344                    .connection_options
1345                    .port
1346                    .map(|port| vec!["-P".to_string(), port.to_string()])
1347                    .unwrap_or_default(),
1348            )
1349            .arg("-C")
1350            .arg("-r")
1351            .arg(&src_path)
1352            .arg(format!(
1353                "{}:{}",
1354                self.socket.connection_options.scp_url(),
1355                dest_path.display()
1356            ))
1357            .output();
1358
1359        cx.background_spawn(async move {
1360            let output = output.await?;
1361
1362            anyhow::ensure!(
1363                output.status.success(),
1364                "failed to upload directory {} -> {}: {}",
1365                src_path.display(),
1366                dest_path.display(),
1367                String::from_utf8_lossy(&output.stderr)
1368            );
1369
1370            Ok(())
1371        })
1372    }
1373
1374    fn start_proxy(
1375        &self,
1376        unique_identifier: String,
1377        reconnect: bool,
1378        incoming_tx: UnboundedSender<Envelope>,
1379        outgoing_rx: UnboundedReceiver<Envelope>,
1380        connection_activity_tx: Sender<()>,
1381        delegate: Arc<dyn SshClientDelegate>,
1382        cx: &mut AsyncApp,
1383    ) -> Task<Result<i32>> {
1384        delegate.set_status(Some("Starting proxy"), cx);
1385
1386        let Some(remote_binary_path) = self.remote_binary_path.clone() else {
1387            return Task::ready(Err(anyhow!("Remote binary path not set")));
1388        };
1389
1390        let mut start_proxy_command = shell_script!(
1391            "exec {binary_path} proxy --identifier {identifier}",
1392            binary_path = &remote_binary_path.to_string_lossy(),
1393            identifier = &unique_identifier,
1394        );
1395
1396        if let Some(rust_log) = std::env::var("RUST_LOG").ok() {
1397            start_proxy_command = format!(
1398                "RUST_LOG={} {}",
1399                shlex::try_quote(&rust_log).unwrap(),
1400                start_proxy_command
1401            )
1402        }
1403        if let Some(rust_backtrace) = std::env::var("RUST_BACKTRACE").ok() {
1404            start_proxy_command = format!(
1405                "RUST_BACKTRACE={} {}",
1406                shlex::try_quote(&rust_backtrace).unwrap(),
1407                start_proxy_command
1408            )
1409        }
1410        if reconnect {
1411            start_proxy_command.push_str(" --reconnect");
1412        }
1413
1414        let ssh_proxy_process = match self
1415            .socket
1416            .ssh_command("sh", &["-c", &start_proxy_command])
1417            // IMPORTANT: we kill this process when we drop the task that uses it.
1418            .kill_on_drop(true)
1419            .spawn()
1420        {
1421            Ok(process) => process,
1422            Err(error) => {
1423                return Task::ready(Err(anyhow!("failed to spawn remote server: {}", error)));
1424            }
1425        };
1426
1427        Self::multiplex(
1428            ssh_proxy_process,
1429            incoming_tx,
1430            outgoing_rx,
1431            connection_activity_tx,
1432            &cx,
1433        )
1434    }
1435}
1436
1437impl SshRemoteConnection {
1438    #[cfg(not(unix))]
1439    async fn new(
1440        _connection_options: SshConnectionOptions,
1441        _delegate: Arc<dyn SshClientDelegate>,
1442        _cx: &mut AsyncApp,
1443    ) -> Result<Self> {
1444        anyhow::bail!("ssh is not supported on this platform");
1445    }
1446
1447    #[cfg(unix)]
1448    async fn new(
1449        connection_options: SshConnectionOptions,
1450        delegate: Arc<dyn SshClientDelegate>,
1451        cx: &mut AsyncApp,
1452    ) -> Result<Self> {
1453        use askpass::AskPassResult;
1454
1455        delegate.set_status(Some("Connecting"), cx);
1456
1457        let url = connection_options.ssh_url();
1458
1459        let temp_dir = tempfile::Builder::new()
1460            .prefix("zed-ssh-session")
1461            .tempdir()?;
1462        let askpass_delegate = askpass::AskPassDelegate::new(cx, {
1463            let delegate = delegate.clone();
1464            move |prompt, tx, cx| delegate.ask_password(prompt, tx, cx)
1465        });
1466
1467        let mut askpass =
1468            askpass::AskPassSession::new(cx.background_executor(), askpass_delegate).await?;
1469
1470        // Start the master SSH process, which does not do anything except for establish
1471        // the connection and keep it open, allowing other ssh commands to reuse it
1472        // via a control socket.
1473        let socket_path = temp_dir.path().join("ssh.sock");
1474
1475        let mut master_process = process::Command::new("ssh")
1476            .stdin(Stdio::null())
1477            .stdout(Stdio::piped())
1478            .stderr(Stdio::piped())
1479            .env("SSH_ASKPASS_REQUIRE", "force")
1480            .env("SSH_ASKPASS", &askpass.script_path())
1481            .args(connection_options.additional_args())
1482            .args([
1483                "-N",
1484                "-o",
1485                "ControlPersist=no",
1486                "-o",
1487                "ControlMaster=yes",
1488                "-o",
1489            ])
1490            .arg(format!("ControlPath={}", socket_path.display()))
1491            .arg(&url)
1492            .kill_on_drop(true)
1493            .spawn()?;
1494        // Wait for this ssh process to close its stdout, indicating that authentication
1495        // has completed.
1496        let mut stdout = master_process.stdout.take().unwrap();
1497        let mut output = Vec::new();
1498
1499        let result = select_biased! {
1500            result = askpass.run().fuse() => {
1501                match result {
1502                    AskPassResult::CancelledByUser => {
1503                        master_process.kill().ok();
1504                        anyhow::bail!("SSH connection canceled")
1505                    }
1506                    AskPassResult::Timedout => {
1507                        anyhow::bail!("connecting to host timed out")
1508                    }
1509                }
1510            }
1511            _ = stdout.read_to_end(&mut output).fuse() => {
1512                anyhow::Ok(())
1513            }
1514        };
1515
1516        if let Err(e) = result {
1517            return Err(e.context("Failed to connect to host"));
1518        }
1519
1520        if master_process.try_status()?.is_some() {
1521            output.clear();
1522            let mut stderr = master_process.stderr.take().unwrap();
1523            stderr.read_to_end(&mut output).await?;
1524
1525            let error_message = format!(
1526                "failed to connect: {}",
1527                String::from_utf8_lossy(&output).trim()
1528            );
1529            anyhow::bail!(error_message);
1530        }
1531
1532        drop(askpass);
1533
1534        let socket = SshSocket {
1535            connection_options,
1536            socket_path,
1537        };
1538
1539        let mut this = Self {
1540            socket,
1541            master_process: Mutex::new(Some(master_process)),
1542            _temp_dir: temp_dir,
1543            remote_binary_path: None,
1544        };
1545
1546        let (release_channel, version, commit) = cx.update(|cx| {
1547            (
1548                ReleaseChannel::global(cx),
1549                AppVersion::global(cx),
1550                AppCommitSha::try_global(cx),
1551            )
1552        })?;
1553        this.remote_binary_path = Some(
1554            this.ensure_server_binary(&delegate, release_channel, version, commit, cx)
1555                .await?,
1556        );
1557
1558        Ok(this)
1559    }
1560
1561    async fn platform(&self) -> Result<SshPlatform> {
1562        let uname = self.socket.run_command("sh", &["-c", "uname -sm"]).await?;
1563        let Some((os, arch)) = uname.split_once(" ") else {
1564            anyhow::bail!("unknown uname: {uname:?}")
1565        };
1566
1567        let os = match os.trim() {
1568            "Darwin" => "macos",
1569            "Linux" => "linux",
1570            _ => anyhow::bail!(
1571                "Prebuilt remote servers are not yet available for {os:?}. See https://zed.dev/docs/remote-development"
1572            ),
1573        };
1574        // exclude armv5,6,7 as they are 32-bit.
1575        let arch = if arch.starts_with("armv8")
1576            || arch.starts_with("armv9")
1577            || arch.starts_with("arm64")
1578            || arch.starts_with("aarch64")
1579        {
1580            "aarch64"
1581        } else if arch.starts_with("x86") {
1582            "x86_64"
1583        } else {
1584            anyhow::bail!(
1585                "Prebuilt remote servers are not yet available for {arch:?}. See https://zed.dev/docs/remote-development"
1586            )
1587        };
1588
1589        Ok(SshPlatform { os, arch })
1590    }
1591
1592    fn multiplex(
1593        mut ssh_proxy_process: Child,
1594        incoming_tx: UnboundedSender<Envelope>,
1595        mut outgoing_rx: UnboundedReceiver<Envelope>,
1596        mut connection_activity_tx: Sender<()>,
1597        cx: &AsyncApp,
1598    ) -> Task<Result<i32>> {
1599        let mut child_stderr = ssh_proxy_process.stderr.take().unwrap();
1600        let mut child_stdout = ssh_proxy_process.stdout.take().unwrap();
1601        let mut child_stdin = ssh_proxy_process.stdin.take().unwrap();
1602
1603        let mut stdin_buffer = Vec::new();
1604        let mut stdout_buffer = Vec::new();
1605        let mut stderr_buffer = Vec::new();
1606        let mut stderr_offset = 0;
1607
1608        let stdin_task = cx.background_spawn(async move {
1609            while let Some(outgoing) = outgoing_rx.next().await {
1610                write_message(&mut child_stdin, &mut stdin_buffer, outgoing).await?;
1611            }
1612            anyhow::Ok(())
1613        });
1614
1615        let stdout_task = cx.background_spawn({
1616            let mut connection_activity_tx = connection_activity_tx.clone();
1617            async move {
1618                loop {
1619                    stdout_buffer.resize(MESSAGE_LEN_SIZE, 0);
1620                    let len = child_stdout.read(&mut stdout_buffer).await?;
1621
1622                    if len == 0 {
1623                        return anyhow::Ok(());
1624                    }
1625
1626                    if len < MESSAGE_LEN_SIZE {
1627                        child_stdout.read_exact(&mut stdout_buffer[len..]).await?;
1628                    }
1629
1630                    let message_len = message_len_from_buffer(&stdout_buffer);
1631                    let envelope =
1632                        read_message_with_len(&mut child_stdout, &mut stdout_buffer, message_len)
1633                            .await?;
1634                    connection_activity_tx.try_send(()).ok();
1635                    incoming_tx.unbounded_send(envelope).ok();
1636                }
1637            }
1638        });
1639
1640        let stderr_task: Task<anyhow::Result<()>> = cx.background_spawn(async move {
1641            loop {
1642                stderr_buffer.resize(stderr_offset + 1024, 0);
1643
1644                let len = child_stderr
1645                    .read(&mut stderr_buffer[stderr_offset..])
1646                    .await?;
1647                if len == 0 {
1648                    return anyhow::Ok(());
1649                }
1650
1651                stderr_offset += len;
1652                let mut start_ix = 0;
1653                while let Some(ix) = stderr_buffer[start_ix..stderr_offset]
1654                    .iter()
1655                    .position(|b| b == &b'\n')
1656                {
1657                    let line_ix = start_ix + ix;
1658                    let content = &stderr_buffer[start_ix..line_ix];
1659                    start_ix = line_ix + 1;
1660                    if let Ok(record) = serde_json::from_slice::<LogRecord>(content) {
1661                        record.log(log::logger())
1662                    } else {
1663                        eprintln!("(remote) {}", String::from_utf8_lossy(content));
1664                    }
1665                }
1666                stderr_buffer.drain(0..start_ix);
1667                stderr_offset -= start_ix;
1668
1669                connection_activity_tx.try_send(()).ok();
1670            }
1671        });
1672
1673        cx.spawn(async move |_| {
1674            let result = futures::select! {
1675                result = stdin_task.fuse() => {
1676                    result.context("stdin")
1677                }
1678                result = stdout_task.fuse() => {
1679                    result.context("stdout")
1680                }
1681                result = stderr_task.fuse() => {
1682                    result.context("stderr")
1683                }
1684            };
1685
1686            let status = ssh_proxy_process.status().await?.code().unwrap_or(1);
1687            match result {
1688                Ok(_) => Ok(status),
1689                Err(error) => Err(error),
1690            }
1691        })
1692    }
1693
1694    #[allow(unused)]
1695    async fn ensure_server_binary(
1696        &self,
1697        delegate: &Arc<dyn SshClientDelegate>,
1698        release_channel: ReleaseChannel,
1699        version: SemanticVersion,
1700        commit: Option<AppCommitSha>,
1701        cx: &mut AsyncApp,
1702    ) -> Result<PathBuf> {
1703        let version_str = match release_channel {
1704            ReleaseChannel::Nightly => {
1705                let commit = commit.map(|s| s.full()).unwrap_or_default();
1706
1707                format!("{}-{}", version, commit)
1708            }
1709            ReleaseChannel::Dev => "build".to_string(),
1710            _ => version.to_string(),
1711        };
1712        let binary_name = format!(
1713            "zed-remote-server-{}-{}",
1714            release_channel.dev_name(),
1715            version_str
1716        );
1717        let dst_path = paths::remote_server_dir_relative().join(binary_name);
1718
1719        let build_remote_server = std::env::var("ZED_BUILD_REMOTE_SERVER").ok();
1720        #[cfg(debug_assertions)]
1721        if let Some(build_remote_server) = build_remote_server {
1722            let src_path = self
1723                .build_local(build_remote_server, self.platform().await?, delegate, cx)
1724                .await?;
1725            let tmp_path = paths::remote_server_dir_relative().join(format!(
1726                "download-{}-{}",
1727                std::process::id(),
1728                src_path.file_name().unwrap().to_string_lossy()
1729            ));
1730            self.upload_local_server_binary(&src_path, &tmp_path, delegate, cx)
1731                .await?;
1732            self.extract_server_binary(&dst_path, &tmp_path, delegate, cx)
1733                .await?;
1734            return Ok(dst_path);
1735        }
1736
1737        if self
1738            .socket
1739            .run_command(&dst_path.to_string_lossy(), &["version"])
1740            .await
1741            .is_ok()
1742        {
1743            return Ok(dst_path);
1744        }
1745
1746        let wanted_version = cx.update(|cx| match release_channel {
1747            ReleaseChannel::Nightly => Ok(None),
1748            ReleaseChannel::Dev => {
1749                anyhow::bail!(
1750                    "ZED_BUILD_REMOTE_SERVER is not set and no remote server exists at ({:?})",
1751                    dst_path
1752                )
1753            }
1754            _ => Ok(Some(AppVersion::global(cx))),
1755        })??;
1756
1757        let platform = self.platform().await?;
1758
1759        let tmp_path_gz = PathBuf::from(format!(
1760            "{}-download-{}.gz",
1761            dst_path.to_string_lossy(),
1762            std::process::id()
1763        ));
1764        if !self.socket.connection_options.upload_binary_over_ssh {
1765            if let Some((url, body)) = delegate
1766                .get_download_params(platform, release_channel, wanted_version, cx)
1767                .await?
1768            {
1769                match self
1770                    .download_binary_on_server(&url, &body, &tmp_path_gz, delegate, cx)
1771                    .await
1772                {
1773                    Ok(_) => {
1774                        self.extract_server_binary(&dst_path, &tmp_path_gz, delegate, cx)
1775                            .await?;
1776                        return Ok(dst_path);
1777                    }
1778                    Err(e) => {
1779                        log::error!(
1780                            "Failed to download binary on server, attempting to upload server: {}",
1781                            e
1782                        )
1783                    }
1784                }
1785            }
1786        }
1787
1788        let src_path = delegate
1789            .download_server_binary_locally(platform, release_channel, wanted_version, cx)
1790            .await?;
1791        self.upload_local_server_binary(&src_path, &tmp_path_gz, delegate, cx)
1792            .await?;
1793        self.extract_server_binary(&dst_path, &tmp_path_gz, delegate, cx)
1794            .await?;
1795        return Ok(dst_path);
1796    }
1797
1798    async fn download_binary_on_server(
1799        &self,
1800        url: &str,
1801        body: &str,
1802        tmp_path_gz: &Path,
1803        delegate: &Arc<dyn SshClientDelegate>,
1804        cx: &mut AsyncApp,
1805    ) -> Result<()> {
1806        if let Some(parent) = tmp_path_gz.parent() {
1807            self.socket
1808                .run_command("mkdir", &["-p", &parent.to_string_lossy()])
1809                .await?;
1810        }
1811
1812        delegate.set_status(Some("Downloading remote development server on host"), cx);
1813
1814        match self
1815            .socket
1816            .run_command(
1817                "curl",
1818                &[
1819                    "-f",
1820                    "-L",
1821                    "-X",
1822                    "GET",
1823                    "-H",
1824                    "Content-Type: application/json",
1825                    "-d",
1826                    &body,
1827                    &url,
1828                    "-o",
1829                    &tmp_path_gz.to_string_lossy(),
1830                ],
1831            )
1832            .await
1833        {
1834            Ok(_) => {}
1835            Err(e) => {
1836                if self.socket.run_command("which", &["curl"]).await.is_ok() {
1837                    return Err(e);
1838                }
1839
1840                match self
1841                    .socket
1842                    .run_command(
1843                        "wget",
1844                        &[
1845                            "--method=GET",
1846                            "--header=Content-Type: application/json",
1847                            "--body-data",
1848                            &body,
1849                            &url,
1850                            "-O",
1851                            &tmp_path_gz.to_string_lossy(),
1852                        ],
1853                    )
1854                    .await
1855                {
1856                    Ok(_) => {}
1857                    Err(e) => {
1858                        if self.socket.run_command("which", &["wget"]).await.is_ok() {
1859                            return Err(e);
1860                        } else {
1861                            anyhow::bail!("Neither curl nor wget is available");
1862                        }
1863                    }
1864                }
1865            }
1866        }
1867
1868        Ok(())
1869    }
1870
1871    async fn upload_local_server_binary(
1872        &self,
1873        src_path: &Path,
1874        tmp_path_gz: &Path,
1875        delegate: &Arc<dyn SshClientDelegate>,
1876        cx: &mut AsyncApp,
1877    ) -> Result<()> {
1878        if let Some(parent) = tmp_path_gz.parent() {
1879            self.socket
1880                .run_command("mkdir", &["-p", &parent.to_string_lossy()])
1881                .await?;
1882        }
1883
1884        let src_stat = fs::metadata(&src_path).await?;
1885        let size = src_stat.len();
1886
1887        let t0 = Instant::now();
1888        delegate.set_status(Some("Uploading remote development server"), cx);
1889        log::info!(
1890            "uploading remote development server to {:?} ({}kb)",
1891            tmp_path_gz,
1892            size / 1024
1893        );
1894        self.upload_file(&src_path, &tmp_path_gz)
1895            .await
1896            .context("failed to upload server binary")?;
1897        log::info!("uploaded remote development server in {:?}", t0.elapsed());
1898        Ok(())
1899    }
1900
1901    async fn extract_server_binary(
1902        &self,
1903        dst_path: &Path,
1904        tmp_path: &Path,
1905        delegate: &Arc<dyn SshClientDelegate>,
1906        cx: &mut AsyncApp,
1907    ) -> Result<()> {
1908        delegate.set_status(Some("Extracting remote development server"), cx);
1909        let server_mode = 0o755;
1910
1911        let orig_tmp_path = tmp_path.to_string_lossy();
1912        let script = if let Some(tmp_path) = orig_tmp_path.strip_suffix(".gz") {
1913            shell_script!(
1914                "gunzip -f {orig_tmp_path} && chmod {server_mode} {tmp_path} && mv {tmp_path} {dst_path}",
1915                server_mode = &format!("{:o}", server_mode),
1916                dst_path = &dst_path.to_string_lossy()
1917            )
1918        } else {
1919            shell_script!(
1920                "chmod {server_mode} {orig_tmp_path} && mv {orig_tmp_path} {dst_path}",
1921                server_mode = &format!("{:o}", server_mode),
1922                dst_path = &dst_path.to_string_lossy()
1923            )
1924        };
1925        self.socket.run_command("sh", &["-c", &script]).await?;
1926        Ok(())
1927    }
1928
1929    async fn upload_file(&self, src_path: &Path, dest_path: &Path) -> Result<()> {
1930        log::debug!("uploading file {:?} to {:?}", src_path, dest_path);
1931        let mut command = util::command::new_smol_command("scp");
1932        let output = self
1933            .socket
1934            .ssh_options(&mut command)
1935            .args(
1936                self.socket
1937                    .connection_options
1938                    .port
1939                    .map(|port| vec!["-P".to_string(), port.to_string()])
1940                    .unwrap_or_default(),
1941            )
1942            .arg(src_path)
1943            .arg(format!(
1944                "{}:{}",
1945                self.socket.connection_options.scp_url(),
1946                dest_path.display()
1947            ))
1948            .output()
1949            .await?;
1950
1951        anyhow::ensure!(
1952            output.status.success(),
1953            "failed to upload file {} -> {}: {}",
1954            src_path.display(),
1955            dest_path.display(),
1956            String::from_utf8_lossy(&output.stderr)
1957        );
1958        Ok(())
1959    }
1960
1961    #[cfg(debug_assertions)]
1962    async fn build_local(
1963        &self,
1964        build_remote_server: String,
1965        platform: SshPlatform,
1966        delegate: &Arc<dyn SshClientDelegate>,
1967        cx: &mut AsyncApp,
1968    ) -> Result<PathBuf> {
1969        use smol::process::{Command, Stdio};
1970
1971        async fn run_cmd(command: &mut Command) -> Result<()> {
1972            let output = command
1973                .kill_on_drop(true)
1974                .stderr(Stdio::inherit())
1975                .output()
1976                .await?;
1977            anyhow::ensure!(
1978                output.status.success(),
1979                "Failed to run command: {command:?}"
1980            );
1981            Ok(())
1982        }
1983
1984        if platform.arch == std::env::consts::ARCH && platform.os == std::env::consts::OS {
1985            delegate.set_status(Some("Building remote server binary from source"), cx);
1986            log::info!("building remote server binary from source");
1987            run_cmd(Command::new("cargo").args([
1988                "build",
1989                "--package",
1990                "remote_server",
1991                "--features",
1992                "debug-embed",
1993                "--target-dir",
1994                "target/remote_server",
1995            ]))
1996            .await?;
1997
1998            delegate.set_status(Some("Compressing binary"), cx);
1999
2000            run_cmd(Command::new("gzip").args([
2001                "-9",
2002                "-f",
2003                "target/remote_server/debug/remote_server",
2004            ]))
2005            .await?;
2006
2007            let path = std::env::current_dir()?.join("target/remote_server/debug/remote_server.gz");
2008            return Ok(path);
2009        }
2010        let Some(triple) = platform.triple() else {
2011            anyhow::bail!("can't cross compile for: {:?}", platform);
2012        };
2013        smol::fs::create_dir_all("target/remote_server").await?;
2014
2015        if build_remote_server.contains("zigbuild") {
2016            delegate.set_status(
2017                Some(&format!(
2018                    "Building remote binary from source for {triple} with Zig"
2019                )),
2020                cx,
2021            );
2022            log::info!("building remote binary from source for {triple} with Zig");
2023            run_cmd(Command::new("cargo").args([
2024                "zigbuild",
2025                "--package",
2026                "remote_server",
2027                "--features",
2028                "debug-embed",
2029                "--target-dir",
2030                "target/remote_server",
2031                "--target",
2032                &triple,
2033            ]))
2034            .await?;
2035        } else {
2036            delegate.set_status(Some("Installing cross.rs for cross-compilation"), cx);
2037            log::info!("installing cross");
2038            run_cmd(Command::new("cargo").args([
2039                "install",
2040                "cross",
2041                "--git",
2042                "https://github.com/cross-rs/cross",
2043            ]))
2044            .await?;
2045
2046            delegate.set_status(
2047                Some(&format!(
2048                    "Building remote server binary from source for {} with Docker",
2049                    &triple
2050                )),
2051                cx,
2052            );
2053            log::info!("building remote server binary from source for {}", &triple);
2054            run_cmd(
2055                Command::new("cross")
2056                    .args([
2057                        "build",
2058                        "--package",
2059                        "remote_server",
2060                        "--features",
2061                        "debug-embed",
2062                        "--target-dir",
2063                        "target/remote_server",
2064                        "--target",
2065                        &triple,
2066                    ])
2067                    .env(
2068                        "CROSS_CONTAINER_OPTS",
2069                        "--mount type=bind,src=./target,dst=/app/target",
2070                    ),
2071            )
2072            .await?;
2073        }
2074
2075        delegate.set_status(Some("Compressing binary"), cx);
2076
2077        let mut path = format!("target/remote_server/{triple}/debug/remote_server").into();
2078        if !build_remote_server.contains("nocompress") {
2079            run_cmd(Command::new("gzip").args([
2080                "-9",
2081                "-f",
2082                &format!("target/remote_server/{}/debug/remote_server", triple),
2083            ]))
2084            .await?;
2085
2086            path = std::env::current_dir()?.join(format!(
2087                "target/remote_server/{triple}/debug/remote_server.gz"
2088            ));
2089        }
2090
2091        return Ok(path);
2092    }
2093}
2094
2095type ResponseChannels = Mutex<HashMap<MessageId, oneshot::Sender<(Envelope, oneshot::Sender<()>)>>>;
2096
2097pub struct ChannelClient {
2098    next_message_id: AtomicU32,
2099    outgoing_tx: Mutex<mpsc::UnboundedSender<Envelope>>,
2100    buffer: Mutex<VecDeque<Envelope>>,
2101    response_channels: ResponseChannels,
2102    message_handlers: Mutex<ProtoMessageHandlerSet>,
2103    max_received: AtomicU32,
2104    name: &'static str,
2105    task: Mutex<Task<Result<()>>>,
2106}
2107
2108impl ChannelClient {
2109    pub fn new(
2110        incoming_rx: mpsc::UnboundedReceiver<Envelope>,
2111        outgoing_tx: mpsc::UnboundedSender<Envelope>,
2112        cx: &App,
2113        name: &'static str,
2114    ) -> Arc<Self> {
2115        Arc::new_cyclic(|this| Self {
2116            outgoing_tx: Mutex::new(outgoing_tx),
2117            next_message_id: AtomicU32::new(0),
2118            max_received: AtomicU32::new(0),
2119            response_channels: ResponseChannels::default(),
2120            message_handlers: Default::default(),
2121            buffer: Mutex::new(VecDeque::new()),
2122            name,
2123            task: Mutex::new(Self::start_handling_messages(
2124                this.clone(),
2125                incoming_rx,
2126                &cx.to_async(),
2127            )),
2128        })
2129    }
2130
2131    fn start_handling_messages(
2132        this: Weak<Self>,
2133        mut incoming_rx: mpsc::UnboundedReceiver<Envelope>,
2134        cx: &AsyncApp,
2135    ) -> Task<Result<()>> {
2136        cx.spawn(async move |cx| {
2137            let peer_id = PeerId { owner_id: 0, id: 0 };
2138            while let Some(incoming) = incoming_rx.next().await {
2139                let Some(this) = this.upgrade() else {
2140                    return anyhow::Ok(());
2141                };
2142                if let Some(ack_id) = incoming.ack_id {
2143                    let mut buffer = this.buffer.lock();
2144                    while buffer.front().is_some_and(|msg| msg.id <= ack_id) {
2145                        buffer.pop_front();
2146                    }
2147                }
2148                if let Some(proto::envelope::Payload::FlushBufferedMessages(_)) = &incoming.payload
2149                {
2150                    log::debug!(
2151                        "{}:ssh message received. name:FlushBufferedMessages",
2152                        this.name
2153                    );
2154                    {
2155                        let buffer = this.buffer.lock();
2156                        for envelope in buffer.iter() {
2157                            this.outgoing_tx
2158                                .lock()
2159                                .unbounded_send(envelope.clone())
2160                                .ok();
2161                        }
2162                    }
2163                    let mut envelope = proto::Ack {}.into_envelope(0, Some(incoming.id), None);
2164                    envelope.id = this.next_message_id.fetch_add(1, SeqCst);
2165                    this.outgoing_tx.lock().unbounded_send(envelope).ok();
2166                    continue;
2167                }
2168
2169                this.max_received.store(incoming.id, SeqCst);
2170
2171                if let Some(request_id) = incoming.responding_to {
2172                    let request_id = MessageId(request_id);
2173                    let sender = this.response_channels.lock().remove(&request_id);
2174                    if let Some(sender) = sender {
2175                        let (tx, rx) = oneshot::channel();
2176                        if incoming.payload.is_some() {
2177                            sender.send((incoming, tx)).ok();
2178                        }
2179                        rx.await.ok();
2180                    }
2181                } else if let Some(envelope) =
2182                    build_typed_envelope(peer_id, Instant::now(), incoming)
2183                {
2184                    let type_name = envelope.payload_type_name();
2185                    if let Some(future) = ProtoMessageHandlerSet::handle_message(
2186                        &this.message_handlers,
2187                        envelope,
2188                        this.clone().into(),
2189                        cx.clone(),
2190                    ) {
2191                        log::debug!("{}:ssh message received. name:{type_name}", this.name);
2192                        cx.foreground_executor()
2193                            .spawn(async move {
2194                                match future.await {
2195                                    Ok(_) => {
2196                                        log::debug!(
2197                                            "{}:ssh message handled. name:{type_name}",
2198                                            this.name
2199                                        );
2200                                    }
2201                                    Err(error) => {
2202                                        log::error!(
2203                                            "{}:error handling message. type:{}, error:{}",
2204                                            this.name,
2205                                            type_name,
2206                                            format!("{error:#}").lines().fold(
2207                                                String::new(),
2208                                                |mut message, line| {
2209                                                    if !message.is_empty() {
2210                                                        message.push(' ');
2211                                                    }
2212                                                    message.push_str(line);
2213                                                    message
2214                                                }
2215                                            )
2216                                        );
2217                                    }
2218                                }
2219                            })
2220                            .detach()
2221                    } else {
2222                        log::error!("{}:unhandled ssh message name:{type_name}", this.name);
2223                    }
2224                }
2225            }
2226            anyhow::Ok(())
2227        })
2228    }
2229
2230    pub fn reconnect(
2231        self: &Arc<Self>,
2232        incoming_rx: UnboundedReceiver<Envelope>,
2233        outgoing_tx: UnboundedSender<Envelope>,
2234        cx: &AsyncApp,
2235    ) {
2236        *self.outgoing_tx.lock() = outgoing_tx;
2237        *self.task.lock() = Self::start_handling_messages(Arc::downgrade(self), incoming_rx, cx);
2238    }
2239
2240    pub fn subscribe_to_entity<E: 'static>(&self, remote_id: u64, entity: &Entity<E>) {
2241        let id = (TypeId::of::<E>(), remote_id);
2242
2243        let mut message_handlers = self.message_handlers.lock();
2244        if message_handlers
2245            .entities_by_type_and_remote_id
2246            .contains_key(&id)
2247        {
2248            panic!("already subscribed to entity");
2249        }
2250
2251        message_handlers.entities_by_type_and_remote_id.insert(
2252            id,
2253            EntityMessageSubscriber::Entity {
2254                handle: entity.downgrade().into(),
2255            },
2256        );
2257    }
2258
2259    pub fn request<T: RequestMessage>(
2260        &self,
2261        payload: T,
2262    ) -> impl 'static + Future<Output = Result<T::Response>> {
2263        self.request_internal(payload, true)
2264    }
2265
2266    fn request_internal<T: RequestMessage>(
2267        &self,
2268        payload: T,
2269        use_buffer: bool,
2270    ) -> impl 'static + Future<Output = Result<T::Response>> {
2271        log::debug!("ssh request start. name:{}", T::NAME);
2272        let response =
2273            self.request_dynamic(payload.into_envelope(0, None, None), T::NAME, use_buffer);
2274        async move {
2275            let response = response.await?;
2276            log::debug!("ssh request finish. name:{}", T::NAME);
2277            T::Response::from_envelope(response).context("received a response of the wrong type")
2278        }
2279    }
2280
2281    pub async fn resync(&self, timeout: Duration) -> Result<()> {
2282        smol::future::or(
2283            async {
2284                self.request_internal(proto::FlushBufferedMessages {}, false)
2285                    .await?;
2286
2287                for envelope in self.buffer.lock().iter() {
2288                    self.outgoing_tx
2289                        .lock()
2290                        .unbounded_send(envelope.clone())
2291                        .ok();
2292                }
2293                Ok(())
2294            },
2295            async {
2296                smol::Timer::after(timeout).await;
2297                anyhow::bail!("Timeout detected")
2298            },
2299        )
2300        .await
2301    }
2302
2303    pub async fn ping(&self, timeout: Duration) -> Result<()> {
2304        smol::future::or(
2305            async {
2306                self.request(proto::Ping {}).await?;
2307                Ok(())
2308            },
2309            async {
2310                smol::Timer::after(timeout).await;
2311                anyhow::bail!("Timeout detected")
2312            },
2313        )
2314        .await
2315    }
2316
2317    pub fn send<T: EnvelopedMessage>(&self, payload: T) -> Result<()> {
2318        log::debug!("ssh send name:{}", T::NAME);
2319        self.send_dynamic(payload.into_envelope(0, None, None))
2320    }
2321
2322    fn request_dynamic(
2323        &self,
2324        mut envelope: proto::Envelope,
2325        type_name: &'static str,
2326        use_buffer: bool,
2327    ) -> impl 'static + Future<Output = Result<proto::Envelope>> {
2328        envelope.id = self.next_message_id.fetch_add(1, SeqCst);
2329        let (tx, rx) = oneshot::channel();
2330        let mut response_channels_lock = self.response_channels.lock();
2331        response_channels_lock.insert(MessageId(envelope.id), tx);
2332        drop(response_channels_lock);
2333
2334        let result = if use_buffer {
2335            self.send_buffered(envelope)
2336        } else {
2337            self.send_unbuffered(envelope)
2338        };
2339        async move {
2340            if let Err(error) = &result {
2341                log::error!("failed to send message: {error}");
2342                anyhow::bail!("failed to send message: {error}");
2343            }
2344
2345            let response = rx.await.context("connection lost")?.0;
2346            if let Some(proto::envelope::Payload::Error(error)) = &response.payload {
2347                return Err(RpcError::from_proto(error, type_name));
2348            }
2349            Ok(response)
2350        }
2351    }
2352
2353    pub fn send_dynamic(&self, mut envelope: proto::Envelope) -> Result<()> {
2354        envelope.id = self.next_message_id.fetch_add(1, SeqCst);
2355        self.send_buffered(envelope)
2356    }
2357
2358    fn send_buffered(&self, mut envelope: proto::Envelope) -> Result<()> {
2359        envelope.ack_id = Some(self.max_received.load(SeqCst));
2360        self.buffer.lock().push_back(envelope.clone());
2361        // ignore errors on send (happen while we're reconnecting)
2362        // assume that the global "disconnected" overlay is sufficient.
2363        self.outgoing_tx.lock().unbounded_send(envelope).ok();
2364        Ok(())
2365    }
2366
2367    fn send_unbuffered(&self, mut envelope: proto::Envelope) -> Result<()> {
2368        envelope.ack_id = Some(self.max_received.load(SeqCst));
2369        self.outgoing_tx.lock().unbounded_send(envelope).ok();
2370        Ok(())
2371    }
2372}
2373
2374impl ProtoClient for ChannelClient {
2375    fn request(
2376        &self,
2377        envelope: proto::Envelope,
2378        request_type: &'static str,
2379    ) -> BoxFuture<'static, Result<proto::Envelope>> {
2380        self.request_dynamic(envelope, request_type, true).boxed()
2381    }
2382
2383    fn send(&self, envelope: proto::Envelope, _message_type: &'static str) -> Result<()> {
2384        self.send_dynamic(envelope)
2385    }
2386
2387    fn send_response(&self, envelope: Envelope, _message_type: &'static str) -> anyhow::Result<()> {
2388        self.send_dynamic(envelope)
2389    }
2390
2391    fn message_handler_set(&self) -> &Mutex<ProtoMessageHandlerSet> {
2392        &self.message_handlers
2393    }
2394
2395    fn is_via_collab(&self) -> bool {
2396        false
2397    }
2398}
2399
2400#[cfg(any(test, feature = "test-support"))]
2401mod fake {
2402    use std::{path::PathBuf, sync::Arc};
2403
2404    use anyhow::Result;
2405    use async_trait::async_trait;
2406    use futures::{
2407        FutureExt, SinkExt, StreamExt,
2408        channel::{
2409            mpsc::{self, Sender},
2410            oneshot,
2411        },
2412        select_biased,
2413    };
2414    use gpui::{App, AppContext as _, AsyncApp, SemanticVersion, Task, TestAppContext};
2415    use release_channel::ReleaseChannel;
2416    use rpc::proto::Envelope;
2417
2418    use super::{
2419        ChannelClient, RemoteConnection, SshClientDelegate, SshConnectionOptions, SshPlatform,
2420    };
2421
2422    pub(super) struct FakeRemoteConnection {
2423        pub(super) connection_options: SshConnectionOptions,
2424        pub(super) server_channel: Arc<ChannelClient>,
2425        pub(super) server_cx: SendableCx,
2426    }
2427
2428    pub(super) struct SendableCx(AsyncApp);
2429    impl SendableCx {
2430        // SAFETY: When run in test mode, GPUI is always single threaded.
2431        pub(super) fn new(cx: &TestAppContext) -> Self {
2432            Self(cx.to_async())
2433        }
2434
2435        // SAFETY: Enforce that we're on the main thread by requiring a valid AsyncApp
2436        fn get(&self, _: &AsyncApp) -> AsyncApp {
2437            self.0.clone()
2438        }
2439    }
2440
2441    // SAFETY: There is no way to access a SendableCx from a different thread, see [`SendableCx::new`] and [`SendableCx::get`]
2442    unsafe impl Send for SendableCx {}
2443    unsafe impl Sync for SendableCx {}
2444
2445    #[async_trait(?Send)]
2446    impl RemoteConnection for FakeRemoteConnection {
2447        async fn kill(&self) -> Result<()> {
2448            Ok(())
2449        }
2450
2451        fn has_been_killed(&self) -> bool {
2452            false
2453        }
2454
2455        fn ssh_args(&self) -> Vec<String> {
2456            Vec::new()
2457        }
2458        fn upload_directory(
2459            &self,
2460            _src_path: PathBuf,
2461            _dest_path: PathBuf,
2462            _cx: &App,
2463        ) -> Task<Result<()>> {
2464            unreachable!()
2465        }
2466
2467        fn connection_options(&self) -> SshConnectionOptions {
2468            self.connection_options.clone()
2469        }
2470
2471        fn simulate_disconnect(&self, cx: &AsyncApp) {
2472            let (outgoing_tx, _) = mpsc::unbounded::<Envelope>();
2473            let (_, incoming_rx) = mpsc::unbounded::<Envelope>();
2474            self.server_channel
2475                .reconnect(incoming_rx, outgoing_tx, &self.server_cx.get(&cx));
2476        }
2477
2478        fn start_proxy(
2479            &self,
2480
2481            _unique_identifier: String,
2482            _reconnect: bool,
2483            mut client_incoming_tx: mpsc::UnboundedSender<Envelope>,
2484            mut client_outgoing_rx: mpsc::UnboundedReceiver<Envelope>,
2485            mut connection_activity_tx: Sender<()>,
2486            _delegate: Arc<dyn SshClientDelegate>,
2487            cx: &mut AsyncApp,
2488        ) -> Task<Result<i32>> {
2489            let (mut server_incoming_tx, server_incoming_rx) = mpsc::unbounded::<Envelope>();
2490            let (server_outgoing_tx, mut server_outgoing_rx) = mpsc::unbounded::<Envelope>();
2491
2492            self.server_channel.reconnect(
2493                server_incoming_rx,
2494                server_outgoing_tx,
2495                &self.server_cx.get(cx),
2496            );
2497
2498            cx.background_spawn(async move {
2499                loop {
2500                    select_biased! {
2501                        server_to_client = server_outgoing_rx.next().fuse() => {
2502                            let Some(server_to_client) = server_to_client else {
2503                                return Ok(1)
2504                            };
2505                            connection_activity_tx.try_send(()).ok();
2506                            client_incoming_tx.send(server_to_client).await.ok();
2507                        }
2508                        client_to_server = client_outgoing_rx.next().fuse() => {
2509                            let Some(client_to_server) = client_to_server else {
2510                                return Ok(1)
2511                            };
2512                            server_incoming_tx.send(client_to_server).await.ok();
2513                        }
2514                    }
2515                }
2516            })
2517        }
2518    }
2519
2520    pub(super) struct Delegate;
2521
2522    impl SshClientDelegate for Delegate {
2523        fn ask_password(&self, _: String, _: oneshot::Sender<String>, _: &mut AsyncApp) {
2524            unreachable!()
2525        }
2526
2527        fn download_server_binary_locally(
2528            &self,
2529            _: SshPlatform,
2530            _: ReleaseChannel,
2531            _: Option<SemanticVersion>,
2532            _: &mut AsyncApp,
2533        ) -> Task<Result<PathBuf>> {
2534            unreachable!()
2535        }
2536
2537        fn get_download_params(
2538            &self,
2539            _platform: SshPlatform,
2540            _release_channel: ReleaseChannel,
2541            _version: Option<SemanticVersion>,
2542            _cx: &mut AsyncApp,
2543        ) -> Task<Result<Option<(String, String)>>> {
2544            unreachable!()
2545        }
2546
2547        fn set_status(&self, _: Option<&str>, _: &mut AsyncApp) {}
2548    }
2549}