ssh_session.rs

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