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 collections::HashMap;
  10use futures::{
  11    channel::{
  12        mpsc::{self, Sender, UnboundedReceiver, UnboundedSender},
  13        oneshot,
  14    },
  15    future::BoxFuture,
  16    select_biased, AsyncReadExt as _, AsyncWriteExt as _, Future, FutureExt as _, SinkExt,
  17    StreamExt as _,
  18};
  19use gpui::{
  20    AppContext, AsyncAppContext, Context, EventEmitter, Model, ModelContext, SemanticVersion, Task,
  21    WeakModel,
  22};
  23use parking_lot::Mutex;
  24use rpc::{
  25    proto::{self, build_typed_envelope, Envelope, EnvelopedMessage, PeerId, RequestMessage},
  26    AnyProtoClient, EntityMessageSubscriber, ProtoClient, ProtoMessageHandlerSet, RpcError,
  27};
  28use smol::{
  29    fs,
  30    process::{self, Child, Stdio},
  31};
  32use std::{
  33    any::TypeId,
  34    ffi::OsStr,
  35    fmt,
  36    ops::ControlFlow,
  37    path::{Path, PathBuf},
  38    sync::{
  39        atomic::{AtomicU32, Ordering::SeqCst},
  40        Arc,
  41    },
  42    time::{Duration, Instant},
  43};
  44use tempfile::TempDir;
  45use util::ResultExt;
  46
  47#[derive(
  48    Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, serde::Serialize, serde::Deserialize,
  49)]
  50pub struct SshProjectId(pub u64);
  51
  52#[derive(Clone)]
  53pub struct SshSocket {
  54    connection_options: SshConnectionOptions,
  55    socket_path: PathBuf,
  56}
  57
  58#[derive(Debug, Default, Clone, PartialEq, Eq)]
  59pub struct SshConnectionOptions {
  60    pub host: String,
  61    pub username: Option<String>,
  62    pub port: Option<u16>,
  63    pub password: Option<String>,
  64}
  65
  66impl SshConnectionOptions {
  67    pub fn ssh_url(&self) -> String {
  68        let mut result = String::from("ssh://");
  69        if let Some(username) = &self.username {
  70            result.push_str(username);
  71            result.push('@');
  72        }
  73        result.push_str(&self.host);
  74        if let Some(port) = self.port {
  75            result.push(':');
  76            result.push_str(&port.to_string());
  77        }
  78        result
  79    }
  80
  81    fn scp_url(&self) -> String {
  82        if let Some(username) = &self.username {
  83            format!("{}@{}", username, self.host)
  84        } else {
  85            self.host.clone()
  86        }
  87    }
  88
  89    pub fn connection_string(&self) -> String {
  90        let host = if let Some(username) = &self.username {
  91            format!("{}@{}", username, self.host)
  92        } else {
  93            self.host.clone()
  94        };
  95        if let Some(port) = &self.port {
  96            format!("{}:{}", host, port)
  97        } else {
  98            host
  99        }
 100    }
 101
 102    // Uniquely identifies dev server projects on a remote host. Needs to be
 103    // stable for the same dev server project.
 104    pub fn dev_server_identifier(&self) -> String {
 105        let mut identifier = format!("dev-server-{:?}", self.host);
 106        if let Some(username) = self.username.as_ref() {
 107            identifier.push('-');
 108            identifier.push_str(&username);
 109        }
 110        identifier
 111    }
 112}
 113
 114#[derive(Copy, Clone, Debug)]
 115pub struct SshPlatform {
 116    pub os: &'static str,
 117    pub arch: &'static str,
 118}
 119
 120impl SshPlatform {
 121    pub fn triple(&self) -> Option<String> {
 122        Some(format!(
 123            "{}-{}",
 124            self.arch,
 125            match self.os {
 126                "linux" => "unknown-linux-gnu",
 127                "macos" => "apple-darwin",
 128                _ => return None,
 129            }
 130        ))
 131    }
 132}
 133
 134pub trait SshClientDelegate: Send + Sync {
 135    fn ask_password(
 136        &self,
 137        prompt: String,
 138        cx: &mut AsyncAppContext,
 139    ) -> oneshot::Receiver<Result<String>>;
 140    fn remote_server_binary_path(
 141        &self,
 142        platform: SshPlatform,
 143        cx: &mut AsyncAppContext,
 144    ) -> Result<PathBuf>;
 145    fn get_server_binary(
 146        &self,
 147        platform: SshPlatform,
 148        cx: &mut AsyncAppContext,
 149    ) -> oneshot::Receiver<Result<(PathBuf, SemanticVersion)>>;
 150    fn set_status(&self, status: Option<&str>, cx: &mut AsyncAppContext);
 151    fn set_error(&self, error_message: String, cx: &mut AsyncAppContext);
 152}
 153
 154impl SshSocket {
 155    fn ssh_command<S: AsRef<OsStr>>(&self, program: S) -> process::Command {
 156        let mut command = process::Command::new("ssh");
 157        self.ssh_options(&mut command)
 158            .arg(self.connection_options.ssh_url())
 159            .arg(program);
 160        command
 161    }
 162
 163    fn ssh_options<'a>(&self, command: &'a mut process::Command) -> &'a mut process::Command {
 164        command
 165            .stdin(Stdio::piped())
 166            .stdout(Stdio::piped())
 167            .stderr(Stdio::piped())
 168            .args(["-o", "ControlMaster=no", "-o"])
 169            .arg(format!("ControlPath={}", self.socket_path.display()))
 170    }
 171
 172    fn ssh_args(&self) -> Vec<String> {
 173        vec![
 174            "-o".to_string(),
 175            "ControlMaster=no".to_string(),
 176            "-o".to_string(),
 177            format!("ControlPath={}", self.socket_path.display()),
 178            self.connection_options.ssh_url(),
 179        ]
 180    }
 181}
 182
 183async fn run_cmd(command: &mut process::Command) -> Result<String> {
 184    let output = command.output().await?;
 185    if output.status.success() {
 186        Ok(String::from_utf8_lossy(&output.stdout).to_string())
 187    } else {
 188        Err(anyhow!(
 189            "failed to run command: {}",
 190            String::from_utf8_lossy(&output.stderr)
 191        ))
 192    }
 193}
 194
 195struct ChannelForwarder {
 196    quit_tx: UnboundedSender<()>,
 197    forwarding_task: Task<(UnboundedSender<Envelope>, UnboundedReceiver<Envelope>)>,
 198}
 199
 200impl ChannelForwarder {
 201    fn new(
 202        mut incoming_tx: UnboundedSender<Envelope>,
 203        mut outgoing_rx: UnboundedReceiver<Envelope>,
 204        cx: &AsyncAppContext,
 205    ) -> (Self, UnboundedSender<Envelope>, UnboundedReceiver<Envelope>) {
 206        let (quit_tx, mut quit_rx) = mpsc::unbounded::<()>();
 207
 208        let (proxy_incoming_tx, mut proxy_incoming_rx) = mpsc::unbounded::<Envelope>();
 209        let (mut proxy_outgoing_tx, proxy_outgoing_rx) = mpsc::unbounded::<Envelope>();
 210
 211        let forwarding_task = cx.background_executor().spawn(async move {
 212            loop {
 213                select_biased! {
 214                    _ = quit_rx.next().fuse() => {
 215                        break;
 216                    },
 217                    incoming_envelope = proxy_incoming_rx.next().fuse() => {
 218                        if let Some(envelope) = incoming_envelope {
 219                            if incoming_tx.send(envelope).await.is_err() {
 220                                break;
 221                            }
 222                        } else {
 223                            break;
 224                        }
 225                    }
 226                    outgoing_envelope = outgoing_rx.next().fuse() => {
 227                        if let Some(envelope) = outgoing_envelope {
 228                            if proxy_outgoing_tx.send(envelope).await.is_err() {
 229                                break;
 230                            }
 231                        } else {
 232                            break;
 233                        }
 234                    }
 235                }
 236            }
 237
 238            (incoming_tx, outgoing_rx)
 239        });
 240
 241        (
 242            Self {
 243                forwarding_task,
 244                quit_tx,
 245            },
 246            proxy_incoming_tx,
 247            proxy_outgoing_rx,
 248        )
 249    }
 250
 251    async fn into_channels(mut self) -> (UnboundedSender<Envelope>, UnboundedReceiver<Envelope>) {
 252        let _ = self.quit_tx.send(()).await;
 253        self.forwarding_task.await
 254    }
 255}
 256
 257const MAX_MISSED_HEARTBEATS: usize = 5;
 258const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5);
 259const HEARTBEAT_TIMEOUT: Duration = Duration::from_secs(5);
 260
 261const MAX_RECONNECT_ATTEMPTS: usize = 3;
 262
 263enum State {
 264    Connecting,
 265    Connected {
 266        ssh_connection: SshRemoteConnection,
 267        delegate: Arc<dyn SshClientDelegate>,
 268        forwarder: ChannelForwarder,
 269
 270        multiplex_task: Task<Result<()>>,
 271        heartbeat_task: Task<Result<()>>,
 272    },
 273    HeartbeatMissed {
 274        missed_heartbeats: usize,
 275
 276        ssh_connection: SshRemoteConnection,
 277        delegate: Arc<dyn SshClientDelegate>,
 278        forwarder: ChannelForwarder,
 279
 280        multiplex_task: Task<Result<()>>,
 281        heartbeat_task: Task<Result<()>>,
 282    },
 283    Reconnecting,
 284    ReconnectFailed {
 285        ssh_connection: SshRemoteConnection,
 286        delegate: Arc<dyn SshClientDelegate>,
 287        forwarder: ChannelForwarder,
 288
 289        error: anyhow::Error,
 290        attempts: usize,
 291    },
 292    ReconnectExhausted,
 293    ServerNotRunning,
 294}
 295
 296impl fmt::Display for State {
 297    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 298        match self {
 299            Self::Connecting => write!(f, "connecting"),
 300            Self::Connected { .. } => write!(f, "connected"),
 301            Self::Reconnecting => write!(f, "reconnecting"),
 302            Self::ReconnectFailed { .. } => write!(f, "reconnect failed"),
 303            Self::ReconnectExhausted => write!(f, "reconnect exhausted"),
 304            Self::HeartbeatMissed { .. } => write!(f, "heartbeat missed"),
 305            Self::ServerNotRunning { .. } => write!(f, "server not running"),
 306        }
 307    }
 308}
 309
 310impl State {
 311    fn ssh_connection(&self) -> Option<&SshRemoteConnection> {
 312        match self {
 313            Self::Connected { ssh_connection, .. } => Some(ssh_connection),
 314            Self::HeartbeatMissed { ssh_connection, .. } => Some(ssh_connection),
 315            Self::ReconnectFailed { ssh_connection, .. } => Some(ssh_connection),
 316            _ => None,
 317        }
 318    }
 319
 320    fn can_reconnect(&self) -> bool {
 321        match self {
 322            Self::Connected { .. }
 323            | Self::HeartbeatMissed { .. }
 324            | Self::ReconnectFailed { .. } => true,
 325            State::Connecting
 326            | State::Reconnecting
 327            | State::ReconnectExhausted
 328            | State::ServerNotRunning => false,
 329        }
 330    }
 331
 332    fn is_reconnect_failed(&self) -> bool {
 333        matches!(self, Self::ReconnectFailed { .. })
 334    }
 335
 336    fn is_reconnect_exhausted(&self) -> bool {
 337        matches!(self, Self::ReconnectExhausted { .. })
 338    }
 339
 340    fn is_reconnecting(&self) -> bool {
 341        matches!(self, Self::Reconnecting { .. })
 342    }
 343
 344    fn heartbeat_recovered(self) -> Self {
 345        match self {
 346            Self::HeartbeatMissed {
 347                ssh_connection,
 348                delegate,
 349                forwarder,
 350                multiplex_task,
 351                heartbeat_task,
 352                ..
 353            } => Self::Connected {
 354                ssh_connection,
 355                delegate,
 356                forwarder,
 357                multiplex_task,
 358                heartbeat_task,
 359            },
 360            _ => self,
 361        }
 362    }
 363
 364    fn heartbeat_missed(self) -> Self {
 365        match self {
 366            Self::Connected {
 367                ssh_connection,
 368                delegate,
 369                forwarder,
 370                multiplex_task,
 371                heartbeat_task,
 372            } => Self::HeartbeatMissed {
 373                missed_heartbeats: 1,
 374                ssh_connection,
 375                delegate,
 376                forwarder,
 377                multiplex_task,
 378                heartbeat_task,
 379            },
 380            Self::HeartbeatMissed {
 381                missed_heartbeats,
 382                ssh_connection,
 383                delegate,
 384                forwarder,
 385                multiplex_task,
 386                heartbeat_task,
 387            } => Self::HeartbeatMissed {
 388                missed_heartbeats: missed_heartbeats + 1,
 389                ssh_connection,
 390                delegate,
 391                forwarder,
 392                multiplex_task,
 393                heartbeat_task,
 394            },
 395            _ => self,
 396        }
 397    }
 398}
 399
 400/// The state of the ssh connection.
 401#[derive(Clone, Copy, Debug, PartialEq, Eq)]
 402pub enum ConnectionState {
 403    Connecting,
 404    Connected,
 405    HeartbeatMissed,
 406    Reconnecting,
 407    Disconnected,
 408}
 409
 410impl From<&State> for ConnectionState {
 411    fn from(value: &State) -> Self {
 412        match value {
 413            State::Connecting => Self::Connecting,
 414            State::Connected { .. } => Self::Connected,
 415            State::Reconnecting | State::ReconnectFailed { .. } => Self::Reconnecting,
 416            State::HeartbeatMissed { .. } => Self::HeartbeatMissed,
 417            State::ReconnectExhausted => Self::Disconnected,
 418            State::ServerNotRunning => Self::Disconnected,
 419        }
 420    }
 421}
 422
 423pub struct SshRemoteClient {
 424    client: Arc<ChannelClient>,
 425    unique_identifier: String,
 426    connection_options: SshConnectionOptions,
 427    state: Arc<Mutex<Option<State>>>,
 428}
 429
 430#[derive(Debug)]
 431pub enum SshRemoteEvent {
 432    Disconnected,
 433}
 434
 435impl EventEmitter<SshRemoteEvent> for SshRemoteClient {}
 436
 437impl SshRemoteClient {
 438    pub fn new(
 439        unique_identifier: String,
 440        connection_options: SshConnectionOptions,
 441        delegate: Arc<dyn SshClientDelegate>,
 442        cx: &AppContext,
 443    ) -> Task<Result<Model<Self>>> {
 444        cx.spawn(|mut cx| async move {
 445            let (outgoing_tx, outgoing_rx) = mpsc::unbounded::<Envelope>();
 446            let (incoming_tx, incoming_rx) = mpsc::unbounded::<Envelope>();
 447            let (connection_activity_tx, connection_activity_rx) = mpsc::channel::<()>(1);
 448
 449            let client = cx.update(|cx| ChannelClient::new(incoming_rx, outgoing_tx, cx))?;
 450            let this = cx.new_model(|_| Self {
 451                client: client.clone(),
 452                unique_identifier: unique_identifier.clone(),
 453                connection_options: connection_options.clone(),
 454                state: Arc::new(Mutex::new(Some(State::Connecting))),
 455            })?;
 456
 457            let (proxy, proxy_incoming_tx, proxy_outgoing_rx) =
 458                ChannelForwarder::new(incoming_tx, outgoing_rx, &mut cx);
 459
 460            let (ssh_connection, ssh_proxy_process) = Self::establish_connection(
 461                unique_identifier,
 462                false,
 463                connection_options,
 464                delegate.clone(),
 465                &mut cx,
 466            )
 467            .await?;
 468
 469            let multiplex_task = Self::multiplex(
 470                this.downgrade(),
 471                ssh_proxy_process,
 472                proxy_incoming_tx,
 473                proxy_outgoing_rx,
 474                connection_activity_tx,
 475                &mut cx,
 476            );
 477
 478            if let Err(error) = client.ping(HEARTBEAT_TIMEOUT).await {
 479                log::error!("failed to establish connection: {}", error);
 480                delegate.set_error(error.to_string(), &mut cx);
 481                return Err(error);
 482            }
 483
 484            let heartbeat_task = Self::heartbeat(this.downgrade(), connection_activity_rx, &mut cx);
 485
 486            this.update(&mut cx, |this, _| {
 487                *this.state.lock() = Some(State::Connected {
 488                    ssh_connection,
 489                    delegate,
 490                    forwarder: proxy,
 491                    multiplex_task,
 492                    heartbeat_task,
 493                });
 494            })?;
 495
 496            Ok(this)
 497        })
 498    }
 499
 500    pub fn shutdown_processes<T: RequestMessage>(
 501        &self,
 502        shutdown_request: Option<T>,
 503    ) -> Option<impl Future<Output = ()>> {
 504        let state = self.state.lock().take()?;
 505        log::info!("shutting down ssh processes");
 506
 507        let State::Connected {
 508            multiplex_task,
 509            heartbeat_task,
 510            ssh_connection,
 511            delegate,
 512            forwarder,
 513        } = state
 514        else {
 515            return None;
 516        };
 517
 518        let client = self.client.clone();
 519
 520        Some(async move {
 521            if let Some(shutdown_request) = shutdown_request {
 522                client.send(shutdown_request).log_err();
 523                // We wait 50ms instead of waiting for a response, because
 524                // waiting for a response would require us to wait on the main thread
 525                // which we want to avoid in an `on_app_quit` callback.
 526                smol::Timer::after(Duration::from_millis(50)).await;
 527            }
 528
 529            // Drop `multiplex_task` because it owns our ssh_proxy_process, which is a
 530            // child of master_process.
 531            drop(multiplex_task);
 532            // Now drop the rest of state, which kills master process.
 533            drop(heartbeat_task);
 534            drop(ssh_connection);
 535            drop(delegate);
 536            drop(forwarder);
 537        })
 538    }
 539
 540    fn reconnect(&mut self, cx: &mut ModelContext<Self>) -> Result<()> {
 541        let mut lock = self.state.lock();
 542
 543        let can_reconnect = lock
 544            .as_ref()
 545            .map(|state| state.can_reconnect())
 546            .unwrap_or(false);
 547        if !can_reconnect {
 548            let error = if let Some(state) = lock.as_ref() {
 549                format!("invalid state, cannot reconnect while in state {state}")
 550            } else {
 551                "no state set".to_string()
 552            };
 553            log::info!("aborting reconnect, because not in state that allows reconnecting");
 554            return Err(anyhow!(error));
 555        }
 556
 557        let state = lock.take().unwrap();
 558        let (attempts, mut ssh_connection, delegate, forwarder) = match state {
 559            State::Connected {
 560                ssh_connection,
 561                delegate,
 562                forwarder,
 563                multiplex_task,
 564                heartbeat_task,
 565            }
 566            | State::HeartbeatMissed {
 567                ssh_connection,
 568                delegate,
 569                forwarder,
 570                multiplex_task,
 571                heartbeat_task,
 572                ..
 573            } => {
 574                drop(multiplex_task);
 575                drop(heartbeat_task);
 576                (0, ssh_connection, delegate, forwarder)
 577            }
 578            State::ReconnectFailed {
 579                attempts,
 580                ssh_connection,
 581                delegate,
 582                forwarder,
 583                ..
 584            } => (attempts, ssh_connection, delegate, forwarder),
 585            State::Connecting
 586            | State::Reconnecting
 587            | State::ReconnectExhausted
 588            | State::ServerNotRunning => unreachable!(),
 589        };
 590
 591        let attempts = attempts + 1;
 592        if attempts > MAX_RECONNECT_ATTEMPTS {
 593            log::error!(
 594                "Failed to reconnect to after {} attempts, giving up",
 595                MAX_RECONNECT_ATTEMPTS
 596            );
 597            drop(lock);
 598            self.set_state(State::ReconnectExhausted, cx);
 599            return Ok(());
 600        }
 601        drop(lock);
 602
 603        self.set_state(State::Reconnecting, cx);
 604
 605        log::info!("Trying to reconnect to ssh server... Attempt {}", attempts);
 606
 607        let identifier = self.unique_identifier.clone();
 608        let client = self.client.clone();
 609        let reconnect_task = cx.spawn(|this, mut cx| async move {
 610            macro_rules! failed {
 611                ($error:expr, $attempts:expr, $ssh_connection:expr, $delegate:expr, $forwarder:expr) => {
 612                    return State::ReconnectFailed {
 613                        error: anyhow!($error),
 614                        attempts: $attempts,
 615                        ssh_connection: $ssh_connection,
 616                        delegate: $delegate,
 617                        forwarder: $forwarder,
 618                    };
 619                };
 620            }
 621
 622            if let Err(error) = ssh_connection.master_process.kill() {
 623                failed!(error, attempts, ssh_connection, delegate, forwarder);
 624            };
 625
 626            if let Err(error) = ssh_connection
 627                .master_process
 628                .status()
 629                .await
 630                .context("Failed to kill ssh process")
 631            {
 632                failed!(error, attempts, ssh_connection, delegate, forwarder);
 633            }
 634
 635            let connection_options = ssh_connection.socket.connection_options.clone();
 636
 637            let (incoming_tx, outgoing_rx) = forwarder.into_channels().await;
 638            let (forwarder, proxy_incoming_tx, proxy_outgoing_rx) =
 639                ChannelForwarder::new(incoming_tx, outgoing_rx, &mut cx);
 640            let (connection_activity_tx, connection_activity_rx) = mpsc::channel::<()>(1);
 641
 642            let (ssh_connection, ssh_process) = match Self::establish_connection(
 643                identifier,
 644                true,
 645                connection_options,
 646                delegate.clone(),
 647                &mut cx,
 648            )
 649            .await
 650            {
 651                Ok((ssh_connection, ssh_process)) => (ssh_connection, ssh_process),
 652                Err(error) => {
 653                    failed!(error, attempts, ssh_connection, delegate, forwarder);
 654                }
 655            };
 656
 657            let multiplex_task = Self::multiplex(
 658                this.clone(),
 659                ssh_process,
 660                proxy_incoming_tx,
 661                proxy_outgoing_rx,
 662                connection_activity_tx,
 663                &mut cx,
 664            );
 665
 666            if let Err(error) = client.ping(HEARTBEAT_TIMEOUT).await {
 667                failed!(error, attempts, ssh_connection, delegate, forwarder);
 668            };
 669
 670            State::Connected {
 671                ssh_connection,
 672                delegate,
 673                forwarder,
 674                multiplex_task,
 675                heartbeat_task: Self::heartbeat(this.clone(), connection_activity_rx, &mut cx),
 676            }
 677        });
 678
 679        cx.spawn(|this, mut cx| async move {
 680            let new_state = reconnect_task.await;
 681            this.update(&mut cx, |this, cx| {
 682                this.try_set_state(cx, |old_state| {
 683                    if old_state.is_reconnecting() {
 684                        match &new_state {
 685                            State::Connecting
 686                            | State::Reconnecting { .. }
 687                            | State::HeartbeatMissed { .. }
 688                            | State::ServerNotRunning => {}
 689                            State::Connected { .. } => {
 690                                log::info!("Successfully reconnected");
 691                            }
 692                            State::ReconnectFailed {
 693                                error, attempts, ..
 694                            } => {
 695                                log::error!(
 696                                    "Reconnect attempt {} failed: {:?}. Starting new attempt...",
 697                                    attempts,
 698                                    error
 699                                );
 700                            }
 701                            State::ReconnectExhausted => {
 702                                log::error!("Reconnect attempt failed and all attempts exhausted");
 703                            }
 704                        }
 705                        Some(new_state)
 706                    } else {
 707                        None
 708                    }
 709                });
 710
 711                if this.state_is(State::is_reconnect_failed) {
 712                    this.reconnect(cx)
 713                } else if this.state_is(State::is_reconnect_exhausted) {
 714                    cx.emit(SshRemoteEvent::Disconnected);
 715                    Ok(())
 716                } else {
 717                    log::debug!("State has transition from Reconnecting into new state while attempting reconnect. Ignoring new state.");
 718                    Ok(())
 719                }
 720            })
 721        })
 722        .detach_and_log_err(cx);
 723
 724        Ok(())
 725    }
 726
 727    fn heartbeat(
 728        this: WeakModel<Self>,
 729        mut connection_activity_rx: mpsc::Receiver<()>,
 730        cx: &mut AsyncAppContext,
 731    ) -> Task<Result<()>> {
 732        let Ok(client) = this.update(cx, |this, _| this.client.clone()) else {
 733            return Task::ready(Err(anyhow!("SshRemoteClient lost")));
 734        };
 735
 736        cx.spawn(|mut cx| {
 737            let this = this.clone();
 738            async move {
 739                let mut missed_heartbeats = 0;
 740
 741                let keepalive_timer = cx.background_executor().timer(HEARTBEAT_INTERVAL).fuse();
 742                futures::pin_mut!(keepalive_timer);
 743
 744                loop {
 745                    select_biased! {
 746                        _ = connection_activity_rx.next().fuse() => {
 747                            keepalive_timer.set(cx.background_executor().timer(HEARTBEAT_INTERVAL).fuse());
 748                        }
 749                        _ = keepalive_timer => {
 750                            log::debug!("Sending heartbeat to server...");
 751
 752                            let result = select_biased! {
 753                                _ = connection_activity_rx.next().fuse() => {
 754                                    Ok(())
 755                                }
 756                                ping_result = client.ping(HEARTBEAT_TIMEOUT).fuse() => {
 757                                    ping_result
 758                                }
 759                            };
 760                            if result.is_err() {
 761                                missed_heartbeats += 1;
 762                                log::warn!(
 763                                    "No heartbeat from server after {:?}. Missed heartbeat {} out of {}.",
 764                                    HEARTBEAT_TIMEOUT,
 765                                    missed_heartbeats,
 766                                    MAX_MISSED_HEARTBEATS
 767                                );
 768                            } else if missed_heartbeats != 0 {
 769                                missed_heartbeats = 0;
 770                            } else {
 771                                continue;
 772                            }
 773
 774                            let result = this.update(&mut cx, |this, mut cx| {
 775                                this.handle_heartbeat_result(missed_heartbeats, &mut cx)
 776                            })?;
 777                            if result.is_break() {
 778                                return Ok(());
 779                            }
 780                        }
 781                    }
 782                }
 783            }
 784        })
 785    }
 786
 787    fn handle_heartbeat_result(
 788        &mut self,
 789        missed_heartbeats: usize,
 790        cx: &mut ModelContext<Self>,
 791    ) -> ControlFlow<()> {
 792        let state = self.state.lock().take().unwrap();
 793        let next_state = if missed_heartbeats > 0 {
 794            state.heartbeat_missed()
 795        } else {
 796            state.heartbeat_recovered()
 797        };
 798
 799        self.set_state(next_state, cx);
 800
 801        if missed_heartbeats >= MAX_MISSED_HEARTBEATS {
 802            log::error!(
 803                "Missed last {} heartbeats. Reconnecting...",
 804                missed_heartbeats
 805            );
 806
 807            self.reconnect(cx)
 808                .context("failed to start reconnect process after missing heartbeats")
 809                .log_err();
 810            ControlFlow::Break(())
 811        } else {
 812            ControlFlow::Continue(())
 813        }
 814    }
 815
 816    fn multiplex(
 817        this: WeakModel<Self>,
 818        mut ssh_proxy_process: Child,
 819        incoming_tx: UnboundedSender<Envelope>,
 820        mut outgoing_rx: UnboundedReceiver<Envelope>,
 821        mut connection_activity_tx: Sender<()>,
 822        cx: &AsyncAppContext,
 823    ) -> Task<Result<()>> {
 824        let mut child_stderr = ssh_proxy_process.stderr.take().unwrap();
 825        let mut child_stdout = ssh_proxy_process.stdout.take().unwrap();
 826        let mut child_stdin = ssh_proxy_process.stdin.take().unwrap();
 827
 828        let io_task = cx.background_executor().spawn(async move {
 829            let mut stdin_buffer = Vec::new();
 830            let mut stdout_buffer = Vec::new();
 831            let mut stderr_buffer = Vec::new();
 832            let mut stderr_offset = 0;
 833
 834            loop {
 835                stdout_buffer.resize(MESSAGE_LEN_SIZE, 0);
 836                stderr_buffer.resize(stderr_offset + 1024, 0);
 837
 838                select_biased! {
 839                    outgoing = outgoing_rx.next().fuse() => {
 840                        let Some(outgoing) = outgoing else {
 841                            return anyhow::Ok(None);
 842                        };
 843
 844                        write_message(&mut child_stdin, &mut stdin_buffer, outgoing).await?;
 845                    }
 846
 847                    result = child_stdout.read(&mut stdout_buffer).fuse() => {
 848                        match result {
 849                            Ok(0) => {
 850                                child_stdin.close().await?;
 851                                outgoing_rx.close();
 852                                let status = ssh_proxy_process.status().await?;
 853                                // If we don't have a code, we assume process
 854                                // has been killed and treat it as non-zero exit
 855                                // code
 856                                return Ok(status.code().or_else(|| Some(1)));
 857                            }
 858                            Ok(len) => {
 859                                if len < stdout_buffer.len() {
 860                                    child_stdout.read_exact(&mut stdout_buffer[len..]).await?;
 861                                }
 862
 863                                let message_len = message_len_from_buffer(&stdout_buffer);
 864                                match read_message_with_len(&mut child_stdout, &mut stdout_buffer, message_len).await {
 865                                    Ok(envelope) => {
 866                                        connection_activity_tx.try_send(()).ok();
 867                                        incoming_tx.unbounded_send(envelope).ok();
 868                                    }
 869                                    Err(error) => {
 870                                        log::error!("error decoding message {error:?}");
 871                                    }
 872                                }
 873                            }
 874                            Err(error) => {
 875                                Err(anyhow!("error reading stdout: {error:?}"))?;
 876                            }
 877                        }
 878                    }
 879
 880                    result = child_stderr.read(&mut stderr_buffer[stderr_offset..]).fuse() => {
 881                        match result {
 882                            Ok(len) => {
 883                                stderr_offset += len;
 884                                let mut start_ix = 0;
 885                                while let Some(ix) = stderr_buffer[start_ix..stderr_offset].iter().position(|b| b == &b'\n') {
 886                                    let line_ix = start_ix + ix;
 887                                    let content = &stderr_buffer[start_ix..line_ix];
 888                                    start_ix = line_ix + 1;
 889                                    if let Ok(record) = serde_json::from_slice::<LogRecord>(content) {
 890                                        record.log(log::logger())
 891                                    } else {
 892                                        eprintln!("(remote) {}", String::from_utf8_lossy(content));
 893                                    }
 894                                }
 895                                stderr_buffer.drain(0..start_ix);
 896                                stderr_offset -= start_ix;
 897
 898                                connection_activity_tx.try_send(()).ok();
 899                            }
 900                            Err(error) => {
 901                                Err(anyhow!("error reading stderr: {error:?}"))?;
 902                            }
 903                        }
 904                    }
 905                }
 906            }
 907        });
 908
 909        cx.spawn(|mut cx| async move {
 910            let result = io_task.await;
 911
 912            match result {
 913                Ok(Some(exit_code)) => {
 914                    if let Some(error) = ProxyLaunchError::from_exit_code(exit_code) {
 915                        match error {
 916                            ProxyLaunchError::ServerNotRunning => {
 917                                log::error!("failed to reconnect because server is not running");
 918                                this.update(&mut cx, |this, cx| {
 919                                    this.set_state(State::ServerNotRunning, cx);
 920                                    cx.emit(SshRemoteEvent::Disconnected);
 921                                })?;
 922                            }
 923                        }
 924                    } else if exit_code > 0 {
 925                        log::error!("proxy process terminated unexpectedly");
 926                        this.update(&mut cx, |this, cx| {
 927                            this.reconnect(cx).ok();
 928                        })?;
 929                    }
 930                }
 931                Ok(None) => {}
 932                Err(error) => {
 933                    log::warn!("ssh io task died with error: {:?}. reconnecting...", error);
 934                    this.update(&mut cx, |this, cx| {
 935                        this.reconnect(cx).ok();
 936                    })?;
 937                }
 938            }
 939            Ok(())
 940        })
 941    }
 942
 943    fn state_is(&self, check: impl FnOnce(&State) -> bool) -> bool {
 944        self.state.lock().as_ref().map_or(false, check)
 945    }
 946
 947    fn try_set_state(
 948        &self,
 949        cx: &mut ModelContext<Self>,
 950        map: impl FnOnce(&State) -> Option<State>,
 951    ) {
 952        let mut lock = self.state.lock();
 953        let new_state = lock.as_ref().and_then(map);
 954
 955        if let Some(new_state) = new_state {
 956            lock.replace(new_state);
 957            cx.notify();
 958        }
 959    }
 960
 961    fn set_state(&self, state: State, cx: &mut ModelContext<Self>) {
 962        log::info!("setting state to '{}'", &state);
 963        self.state.lock().replace(state);
 964        cx.notify();
 965    }
 966
 967    async fn establish_connection(
 968        unique_identifier: String,
 969        reconnect: bool,
 970        connection_options: SshConnectionOptions,
 971        delegate: Arc<dyn SshClientDelegate>,
 972        cx: &mut AsyncAppContext,
 973    ) -> Result<(SshRemoteConnection, Child)> {
 974        let ssh_connection =
 975            SshRemoteConnection::new(connection_options, delegate.clone(), cx).await?;
 976
 977        let platform = ssh_connection.query_platform().await?;
 978        let (local_binary_path, version) = delegate.get_server_binary(platform, cx).await??;
 979        let remote_binary_path = delegate.remote_server_binary_path(platform, cx)?;
 980        ssh_connection
 981            .ensure_server_binary(
 982                &delegate,
 983                &local_binary_path,
 984                &remote_binary_path,
 985                version,
 986                cx,
 987            )
 988            .await?;
 989
 990        let socket = ssh_connection.socket.clone();
 991        run_cmd(socket.ssh_command(&remote_binary_path).arg("version")).await?;
 992
 993        delegate.set_status(Some("Starting proxy"), cx);
 994
 995        let mut start_proxy_command = format!(
 996            "RUST_LOG={} RUST_BACKTRACE={} {:?} proxy --identifier {}",
 997            std::env::var("RUST_LOG").unwrap_or_default(),
 998            std::env::var("RUST_BACKTRACE").unwrap_or_default(),
 999            remote_binary_path,
1000            unique_identifier,
1001        );
1002        if reconnect {
1003            start_proxy_command.push_str(" --reconnect");
1004        }
1005
1006        let ssh_proxy_process = socket
1007            .ssh_command(start_proxy_command)
1008            // IMPORTANT: we kill this process when we drop the task that uses it.
1009            .kill_on_drop(true)
1010            .spawn()
1011            .context("failed to spawn remote server")?;
1012
1013        Ok((ssh_connection, ssh_proxy_process))
1014    }
1015
1016    pub fn subscribe_to_entity<E: 'static>(&self, remote_id: u64, entity: &Model<E>) {
1017        self.client.subscribe_to_entity(remote_id, entity);
1018    }
1019
1020    pub fn ssh_args(&self) -> Option<Vec<String>> {
1021        self.state
1022            .lock()
1023            .as_ref()
1024            .and_then(|state| state.ssh_connection())
1025            .map(|ssh_connection| ssh_connection.socket.ssh_args())
1026    }
1027
1028    pub fn proto_client(&self) -> AnyProtoClient {
1029        self.client.clone().into()
1030    }
1031
1032    pub fn connection_string(&self) -> String {
1033        self.connection_options.connection_string()
1034    }
1035
1036    pub fn connection_options(&self) -> SshConnectionOptions {
1037        self.connection_options.clone()
1038    }
1039
1040    #[cfg(not(any(test, feature = "test-support")))]
1041    pub fn connection_state(&self) -> ConnectionState {
1042        self.state
1043            .lock()
1044            .as_ref()
1045            .map(ConnectionState::from)
1046            .unwrap_or(ConnectionState::Disconnected)
1047    }
1048
1049    #[cfg(any(test, feature = "test-support"))]
1050    pub fn connection_state(&self) -> ConnectionState {
1051        ConnectionState::Connected
1052    }
1053
1054    pub fn is_disconnected(&self) -> bool {
1055        self.connection_state() == ConnectionState::Disconnected
1056    }
1057
1058    #[cfg(any(test, feature = "test-support"))]
1059    pub fn fake(
1060        client_cx: &mut gpui::TestAppContext,
1061        server_cx: &mut gpui::TestAppContext,
1062    ) -> (Model<Self>, Arc<ChannelClient>) {
1063        use gpui::Context;
1064
1065        let (server_to_client_tx, server_to_client_rx) = mpsc::unbounded();
1066        let (client_to_server_tx, client_to_server_rx) = mpsc::unbounded();
1067
1068        (
1069            client_cx.update(|cx| {
1070                let client = ChannelClient::new(server_to_client_rx, client_to_server_tx, cx);
1071                cx.new_model(|_| Self {
1072                    client,
1073                    unique_identifier: "fake".to_string(),
1074                    connection_options: SshConnectionOptions::default(),
1075                    state: Arc::new(Mutex::new(None)),
1076                })
1077            }),
1078            server_cx.update(|cx| ChannelClient::new(client_to_server_rx, server_to_client_tx, cx)),
1079        )
1080    }
1081}
1082
1083impl From<SshRemoteClient> for AnyProtoClient {
1084    fn from(client: SshRemoteClient) -> Self {
1085        AnyProtoClient::new(client.client.clone())
1086    }
1087}
1088
1089struct SshRemoteConnection {
1090    socket: SshSocket,
1091    master_process: process::Child,
1092    _temp_dir: TempDir,
1093}
1094
1095impl Drop for SshRemoteConnection {
1096    fn drop(&mut self) {
1097        if let Err(error) = self.master_process.kill() {
1098            log::error!("failed to kill SSH master process: {}", error);
1099        }
1100    }
1101}
1102
1103impl SshRemoteConnection {
1104    #[cfg(not(unix))]
1105    async fn new(
1106        _connection_options: SshConnectionOptions,
1107        _delegate: Arc<dyn SshClientDelegate>,
1108        _cx: &mut AsyncAppContext,
1109    ) -> Result<Self> {
1110        Err(anyhow!("ssh is not supported on this platform"))
1111    }
1112
1113    #[cfg(unix)]
1114    async fn new(
1115        connection_options: SshConnectionOptions,
1116        delegate: Arc<dyn SshClientDelegate>,
1117        cx: &mut AsyncAppContext,
1118    ) -> Result<Self> {
1119        use futures::{io::BufReader, AsyncBufReadExt as _};
1120        use smol::{fs::unix::PermissionsExt as _, net::unix::UnixListener};
1121        use util::ResultExt as _;
1122
1123        delegate.set_status(Some("connecting"), cx);
1124
1125        let url = connection_options.ssh_url();
1126        let temp_dir = tempfile::Builder::new()
1127            .prefix("zed-ssh-session")
1128            .tempdir()?;
1129
1130        // Create a domain socket listener to handle requests from the askpass program.
1131        let askpass_socket = temp_dir.path().join("askpass.sock");
1132        let (askpass_opened_tx, askpass_opened_rx) = oneshot::channel::<()>();
1133        let listener =
1134            UnixListener::bind(&askpass_socket).context("failed to create askpass socket")?;
1135
1136        let askpass_task = cx.spawn({
1137            let delegate = delegate.clone();
1138            |mut cx| async move {
1139                let mut askpass_opened_tx = Some(askpass_opened_tx);
1140
1141                while let Ok((mut stream, _)) = listener.accept().await {
1142                    if let Some(askpass_opened_tx) = askpass_opened_tx.take() {
1143                        askpass_opened_tx.send(()).ok();
1144                    }
1145                    let mut buffer = Vec::new();
1146                    let mut reader = BufReader::new(&mut stream);
1147                    if reader.read_until(b'\0', &mut buffer).await.is_err() {
1148                        buffer.clear();
1149                    }
1150                    let password_prompt = String::from_utf8_lossy(&buffer);
1151                    if let Some(password) = delegate
1152                        .ask_password(password_prompt.to_string(), &mut cx)
1153                        .await
1154                        .context("failed to get ssh password")
1155                        .and_then(|p| p)
1156                        .log_err()
1157                    {
1158                        stream.write_all(password.as_bytes()).await.log_err();
1159                    }
1160                }
1161            }
1162        });
1163
1164        // Create an askpass script that communicates back to this process.
1165        let askpass_script = format!(
1166            "{shebang}\n{print_args} | nc -U {askpass_socket} 2> /dev/null \n",
1167            askpass_socket = askpass_socket.display(),
1168            print_args = "printf '%s\\0' \"$@\"",
1169            shebang = "#!/bin/sh",
1170        );
1171        let askpass_script_path = temp_dir.path().join("askpass.sh");
1172        fs::write(&askpass_script_path, askpass_script).await?;
1173        fs::set_permissions(&askpass_script_path, std::fs::Permissions::from_mode(0o755)).await?;
1174
1175        // Start the master SSH process, which does not do anything except for establish
1176        // the connection and keep it open, allowing other ssh commands to reuse it
1177        // via a control socket.
1178        let socket_path = temp_dir.path().join("ssh.sock");
1179        let mut master_process = process::Command::new("ssh")
1180            .stdin(Stdio::null())
1181            .stdout(Stdio::piped())
1182            .stderr(Stdio::piped())
1183            .env("SSH_ASKPASS_REQUIRE", "force")
1184            .env("SSH_ASKPASS", &askpass_script_path)
1185            .args(["-N", "-o", "ControlMaster=yes", "-o"])
1186            .arg(format!("ControlPath={}", socket_path.display()))
1187            .arg(&url)
1188            .spawn()?;
1189
1190        // Wait for this ssh process to close its stdout, indicating that authentication
1191        // has completed.
1192        let stdout = master_process.stdout.as_mut().unwrap();
1193        let mut output = Vec::new();
1194        let connection_timeout = Duration::from_secs(10);
1195
1196        let result = select_biased! {
1197            _ = askpass_opened_rx.fuse() => {
1198                // If the askpass script has opened, that means the user is typing
1199                // their password, in which case we don't want to timeout anymore,
1200                // since we know a connection has been established.
1201                stdout.read_to_end(&mut output).await?;
1202                Ok(())
1203            }
1204            result = stdout.read_to_end(&mut output).fuse() => {
1205                result?;
1206                Ok(())
1207            }
1208            _ = futures::FutureExt::fuse(smol::Timer::after(connection_timeout)) => {
1209                Err(anyhow!("Exceeded {:?} timeout trying to connect to host", connection_timeout))
1210            }
1211        };
1212
1213        if let Err(e) = result {
1214            let error_message = format!("Failed to connect to host: {}.", e);
1215            delegate.set_error(error_message, cx);
1216            return Err(e);
1217        }
1218
1219        drop(askpass_task);
1220
1221        if master_process.try_status()?.is_some() {
1222            output.clear();
1223            let mut stderr = master_process.stderr.take().unwrap();
1224            stderr.read_to_end(&mut output).await?;
1225
1226            let error_message = format!("failed to connect: {}", String::from_utf8_lossy(&output));
1227            delegate.set_error(error_message.clone(), cx);
1228            Err(anyhow!(error_message))?;
1229        }
1230
1231        Ok(Self {
1232            socket: SshSocket {
1233                connection_options,
1234                socket_path,
1235            },
1236            master_process,
1237            _temp_dir: temp_dir,
1238        })
1239    }
1240
1241    async fn ensure_server_binary(
1242        &self,
1243        delegate: &Arc<dyn SshClientDelegate>,
1244        src_path: &Path,
1245        dst_path: &Path,
1246        version: SemanticVersion,
1247        cx: &mut AsyncAppContext,
1248    ) -> Result<()> {
1249        let mut dst_path_gz = dst_path.to_path_buf();
1250        dst_path_gz.set_extension("gz");
1251
1252        if let Some(parent) = dst_path.parent() {
1253            run_cmd(self.socket.ssh_command("mkdir").arg("-p").arg(parent)).await?;
1254        }
1255
1256        let mut server_binary_exists = false;
1257        if cfg!(not(debug_assertions)) {
1258            if let Ok(installed_version) =
1259                run_cmd(self.socket.ssh_command(dst_path).arg("version")).await
1260            {
1261                if installed_version.trim() == version.to_string() {
1262                    server_binary_exists = true;
1263                }
1264            }
1265        }
1266
1267        if server_binary_exists {
1268            log::info!("remote development server already present",);
1269            return Ok(());
1270        }
1271
1272        let src_stat = fs::metadata(src_path).await?;
1273        let size = src_stat.len();
1274        let server_mode = 0o755;
1275
1276        let t0 = Instant::now();
1277        delegate.set_status(Some("uploading remote development server"), cx);
1278        log::info!("uploading remote development server ({}kb)", size / 1024);
1279        self.upload_file(src_path, &dst_path_gz)
1280            .await
1281            .context("failed to upload server binary")?;
1282        log::info!("uploaded remote development server in {:?}", t0.elapsed());
1283
1284        delegate.set_status(Some("extracting remote development server"), cx);
1285        run_cmd(
1286            self.socket
1287                .ssh_command("gunzip")
1288                .arg("--force")
1289                .arg(&dst_path_gz),
1290        )
1291        .await?;
1292
1293        delegate.set_status(Some("unzipping remote development server"), cx);
1294        run_cmd(
1295            self.socket
1296                .ssh_command("chmod")
1297                .arg(format!("{:o}", server_mode))
1298                .arg(dst_path),
1299        )
1300        .await?;
1301
1302        Ok(())
1303    }
1304
1305    async fn query_platform(&self) -> Result<SshPlatform> {
1306        let os = run_cmd(self.socket.ssh_command("uname").arg("-s")).await?;
1307        let arch = run_cmd(self.socket.ssh_command("uname").arg("-m")).await?;
1308
1309        let os = match os.trim() {
1310            "Darwin" => "macos",
1311            "Linux" => "linux",
1312            _ => Err(anyhow!("unknown uname os {os:?}"))?,
1313        };
1314        let arch = if arch.starts_with("arm") || arch.starts_with("aarch64") {
1315            "aarch64"
1316        } else if arch.starts_with("x86") || arch.starts_with("i686") {
1317            "x86_64"
1318        } else {
1319            Err(anyhow!("unknown uname architecture {arch:?}"))?
1320        };
1321
1322        Ok(SshPlatform { os, arch })
1323    }
1324
1325    async fn upload_file(&self, src_path: &Path, dest_path: &Path) -> Result<()> {
1326        let mut command = process::Command::new("scp");
1327        let output = self
1328            .socket
1329            .ssh_options(&mut command)
1330            .args(
1331                self.socket
1332                    .connection_options
1333                    .port
1334                    .map(|port| vec!["-P".to_string(), port.to_string()])
1335                    .unwrap_or_default(),
1336            )
1337            .arg(src_path)
1338            .arg(format!(
1339                "{}:{}",
1340                self.socket.connection_options.scp_url(),
1341                dest_path.display()
1342            ))
1343            .output()
1344            .await?;
1345
1346        if output.status.success() {
1347            Ok(())
1348        } else {
1349            Err(anyhow!(
1350                "failed to upload file {} -> {}: {}",
1351                src_path.display(),
1352                dest_path.display(),
1353                String::from_utf8_lossy(&output.stderr)
1354            ))
1355        }
1356    }
1357}
1358
1359type ResponseChannels = Mutex<HashMap<MessageId, oneshot::Sender<(Envelope, oneshot::Sender<()>)>>>;
1360
1361pub struct ChannelClient {
1362    next_message_id: AtomicU32,
1363    outgoing_tx: mpsc::UnboundedSender<Envelope>,
1364    response_channels: ResponseChannels,             // Lock
1365    message_handlers: Mutex<ProtoMessageHandlerSet>, // Lock
1366}
1367
1368impl ChannelClient {
1369    pub fn new(
1370        incoming_rx: mpsc::UnboundedReceiver<Envelope>,
1371        outgoing_tx: mpsc::UnboundedSender<Envelope>,
1372        cx: &AppContext,
1373    ) -> Arc<Self> {
1374        let this = Arc::new(Self {
1375            outgoing_tx,
1376            next_message_id: AtomicU32::new(0),
1377            response_channels: ResponseChannels::default(),
1378            message_handlers: Default::default(),
1379        });
1380
1381        Self::start_handling_messages(this.clone(), incoming_rx, cx);
1382
1383        this
1384    }
1385
1386    fn start_handling_messages(
1387        this: Arc<Self>,
1388        mut incoming_rx: mpsc::UnboundedReceiver<Envelope>,
1389        cx: &AppContext,
1390    ) {
1391        cx.spawn(|cx| {
1392            let this = Arc::downgrade(&this);
1393            async move {
1394                let peer_id = PeerId { owner_id: 0, id: 0 };
1395                while let Some(incoming) = incoming_rx.next().await {
1396                    let Some(this) = this.upgrade() else {
1397                        return anyhow::Ok(());
1398                    };
1399
1400                    if let Some(request_id) = incoming.responding_to {
1401                        let request_id = MessageId(request_id);
1402                        let sender = this.response_channels.lock().remove(&request_id);
1403                        if let Some(sender) = sender {
1404                            let (tx, rx) = oneshot::channel();
1405                            if incoming.payload.is_some() {
1406                                sender.send((incoming, tx)).ok();
1407                            }
1408                            rx.await.ok();
1409                        }
1410                    } else if let Some(envelope) =
1411                        build_typed_envelope(peer_id, Instant::now(), incoming)
1412                    {
1413                        let type_name = envelope.payload_type_name();
1414                        if let Some(future) = ProtoMessageHandlerSet::handle_message(
1415                            &this.message_handlers,
1416                            envelope,
1417                            this.clone().into(),
1418                            cx.clone(),
1419                        ) {
1420                            log::debug!("ssh message received. name:{type_name}");
1421                            cx.foreground_executor().spawn(async move {
1422                                match future.await {
1423                                    Ok(_) => {
1424                                        log::debug!("ssh message handled. name:{type_name}");
1425                                    }
1426                                    Err(error) => {
1427                                        log::error!(
1428                                            "error handling message. type:{type_name}, error:{error}",
1429                                        );
1430                                    }
1431                                }
1432                            }).detach();
1433
1434                        } else {
1435                            log::error!("unhandled ssh message name:{type_name}");
1436                        }
1437                    }
1438                }
1439                anyhow::Ok(())
1440            }
1441        })
1442        .detach();
1443    }
1444
1445    pub fn subscribe_to_entity<E: 'static>(&self, remote_id: u64, entity: &Model<E>) {
1446        let id = (TypeId::of::<E>(), remote_id);
1447
1448        let mut message_handlers = self.message_handlers.lock();
1449        if message_handlers
1450            .entities_by_type_and_remote_id
1451            .contains_key(&id)
1452        {
1453            panic!("already subscribed to entity");
1454        }
1455
1456        message_handlers.entities_by_type_and_remote_id.insert(
1457            id,
1458            EntityMessageSubscriber::Entity {
1459                handle: entity.downgrade().into(),
1460            },
1461        );
1462    }
1463
1464    pub fn request<T: RequestMessage>(
1465        &self,
1466        payload: T,
1467    ) -> impl 'static + Future<Output = Result<T::Response>> {
1468        log::debug!("ssh request start. name:{}", T::NAME);
1469        let response = self.request_dynamic(payload.into_envelope(0, None, None), T::NAME);
1470        async move {
1471            let response = response.await?;
1472            log::debug!("ssh request finish. name:{}", T::NAME);
1473            T::Response::from_envelope(response)
1474                .ok_or_else(|| anyhow!("received a response of the wrong type"))
1475        }
1476    }
1477
1478    pub async fn ping(&self, timeout: Duration) -> Result<()> {
1479        smol::future::or(
1480            async {
1481                self.request(proto::Ping {}).await?;
1482                Ok(())
1483            },
1484            async {
1485                smol::Timer::after(timeout).await;
1486                Err(anyhow!("Timeout detected"))
1487            },
1488        )
1489        .await
1490    }
1491
1492    pub fn send<T: EnvelopedMessage>(&self, payload: T) -> Result<()> {
1493        log::debug!("ssh send name:{}", T::NAME);
1494        self.send_dynamic(payload.into_envelope(0, None, None))
1495    }
1496
1497    pub fn request_dynamic(
1498        &self,
1499        mut envelope: proto::Envelope,
1500        type_name: &'static str,
1501    ) -> impl 'static + Future<Output = Result<proto::Envelope>> {
1502        envelope.id = self.next_message_id.fetch_add(1, SeqCst);
1503        let (tx, rx) = oneshot::channel();
1504        let mut response_channels_lock = self.response_channels.lock();
1505        response_channels_lock.insert(MessageId(envelope.id), tx);
1506        drop(response_channels_lock);
1507        let result = self.outgoing_tx.unbounded_send(envelope);
1508        async move {
1509            if let Err(error) = &result {
1510                log::error!("failed to send message: {}", error);
1511                return Err(anyhow!("failed to send message: {}", error));
1512            }
1513
1514            let response = rx.await.context("connection lost")?.0;
1515            if let Some(proto::envelope::Payload::Error(error)) = &response.payload {
1516                return Err(RpcError::from_proto(error, type_name));
1517            }
1518            Ok(response)
1519        }
1520    }
1521
1522    pub fn send_dynamic(&self, mut envelope: proto::Envelope) -> Result<()> {
1523        envelope.id = self.next_message_id.fetch_add(1, SeqCst);
1524        self.outgoing_tx.unbounded_send(envelope)?;
1525        Ok(())
1526    }
1527}
1528
1529impl ProtoClient for ChannelClient {
1530    fn request(
1531        &self,
1532        envelope: proto::Envelope,
1533        request_type: &'static str,
1534    ) -> BoxFuture<'static, Result<proto::Envelope>> {
1535        self.request_dynamic(envelope, request_type).boxed()
1536    }
1537
1538    fn send(&self, envelope: proto::Envelope, _message_type: &'static str) -> Result<()> {
1539        self.send_dynamic(envelope)
1540    }
1541
1542    fn send_response(&self, envelope: Envelope, _message_type: &'static str) -> anyhow::Result<()> {
1543        self.send_dynamic(envelope)
1544    }
1545
1546    fn message_handler_set(&self) -> &Mutex<ProtoMessageHandlerSet> {
1547        &self.message_handlers
1548    }
1549
1550    fn is_via_collab(&self) -> bool {
1551        false
1552    }
1553}