Preserve SSH connection settings when opening new windows

Forward the invoking shell's remote executable and agent through the Linux prefork launcher. Resolve mounted tabs by stable ID for the New Window shortcut and reconnect to nested destinations from the local host.
This commit is contained in:
James Magahern
2026-11-17 16:20:54 -08:00
parent 153838061e
commit cc2acb51e4
9 changed files with 408 additions and 125 deletions
+93 -22
View File
@@ -1,6 +1,7 @@
//! Display-only client transport. No terminal parser, PTY, or shell lives here.
use crate::protocol::{
self, ClientMessage, ServerMessage, SessionIdentity, SessionTarget, Snapshot, SshOptions,
self, ClientMessage, RemoteWindow, ServerMessage, SessionIdentity, SessionTarget, Snapshot,
SshOptions, TabId,
};
use anyhow::{Context, Result, bail, ensure};
use std::{
@@ -54,8 +55,7 @@ struct RemoteSession {
}
struct Connection {
launcher_socket: PathBuf,
remote: Option<String>,
remote_executable: Option<String>,
remote: Option<RemoteWindow>,
commands: mpsc::Sender<ClientMessage>,
socket: Option<UnixStream>,
ssh: Mutex<Option<Child>>,
@@ -146,7 +146,7 @@ impl Client {
};
let remote_command = format!("exec {} --bridge{attach}", shell_quote(executable));
let mut ssh_command = Command::new("ssh");
if let Some(socket) = options.agent_socket {
if let Some(socket) = &options.agent_socket {
ssh_command.env("SSH_AUTH_SOCK", socket);
} else {
ssh_command.env_remove("SSH_AUTH_SOCK");
@@ -189,8 +189,10 @@ impl Client {
let pending = Arc::new(Mutex::new(Update::default()));
let connection = Arc::new(Connection {
launcher_socket: launcher_socket.to_owned(),
remote: remote.map(str::to_owned),
remote_executable: options.executable,
remote: remote.map(|destination| RemoteWindow {
destination: destination.into(),
ssh: options,
}),
commands,
socket,
ssh: Mutex::new(ssh),
@@ -264,20 +266,23 @@ impl Client {
}
pub fn remote(&self) -> Option<&str> {
self.connection.remote.as_deref()
self.connection
.remote
.as_ref()
.map(|remote| remote.destination.as_str())
}
pub fn remote_identity(&self) -> Option<SessionIdentity> {
self.remote_session.as_ref().map(|session| session.identity)
}
pub fn remote_executable(&self) -> Option<&str> {
self.connection.remote_executable.as_deref()
}
/// Remember a direct SSH window on its local launcher before detaching it.
pub fn remember_remote_detach(&self) -> Result<()> {
let destination = self.remote().context("this window is local")?.to_owned();
let remote = self
.connection
.remote
.as_ref()
.context("this window is local")?;
let session = self
.remote_session
.as_ref()
@@ -288,23 +293,25 @@ impl Client {
crate::launcher::control(
&self.connection.launcher_socket,
ClientMessage::RememberDetachedRemote(protocol::DetachedRemote {
destination,
destination: remote.destination.clone(),
identity: session.identity,
executable: self.remote_executable().map(str::to_owned),
executable: remote.ssh.executable.clone(),
}),
)?;
Ok(())
}
/// Launch through the local daemon even when this terminal connects over SSH.
pub fn open_window(&self, remote: Option<String>, cwd: Option<String>) -> Result<()> {
crate::launcher::control(
&self.connection.launcher_socket,
ClientMessage::OpenWindow {
target: remote,
session: SessionTarget::New { cwd },
},
)?;
pub fn open_window(
&self,
remote_tab: Option<(TabId, String)>,
cwd: Option<String>,
) -> Result<()> {
let request = new_window_request(self.connection.remote.as_ref(), remote_tab, cwd);
if matches!(request, ClientMessage::OpenWindowForRemoteTab { .. }) {
return self.send(request);
}
crate::launcher::control(&self.connection.launcher_socket, request)?;
Ok(())
}
@@ -325,6 +332,33 @@ impl Client {
}
}
fn new_window_request(
direct: Option<&RemoteWindow>,
remote_tab: Option<(TabId, String)>,
cwd: Option<String>,
) -> ClientMessage {
match (direct, remote_tab) {
// The local daemon owns the mounted connection, including its SSH options.
(None, Some((tab, _))) => ClientMessage::OpenWindowForRemoteTab {
tab,
cwd,
fallback: SshOptions::from_env(),
},
(None, None) => ClientMessage::OpenWindow {
target: None,
session: SessionTarget::New { cwd },
},
(Some(direct), None) => ClientMessage::OpenRemoteWindow {
target: direct.clone(),
session: SessionTarget::New { cwd },
},
(Some(_), Some((_, destination))) => ClientMessage::OpenRemoteWindow {
target: RemoteWindow::from_env(destination),
session: SessionTarget::New { cwd },
},
}
}
fn read_updates(
mut reader: impl Read,
pending: &Mutex<Update>,
@@ -610,6 +644,43 @@ fn copy_and_flush(reader: &mut impl Read, writer: &mut impl Write) -> std::io::R
mod tests {
use super::*;
#[test]
fn new_window_routes_local_direct_mounted_and_nested_tabs() {
let direct = RemoteWindow {
destination: "outer".into(),
ssh: SshOptions {
executable: Some("/custom/bin/psyterm".into()),
agent_socket: Some("/custom/agent.sock".into()),
},
};
let request = |host, tab| new_window_request(host, tab, Some("/work".into()));
assert!(matches!(
request(None, None),
ClientMessage::OpenWindow { target: None, .. }
));
assert!(matches!(
request(None, Some((42, "outer".into()))),
ClientMessage::OpenWindowForRemoteTab { tab: 42, .. }
));
let ClientMessage::OpenRemoteWindow { target, session } = request(Some(&direct), None)
else {
panic!("expected direct SSH window")
};
assert!(matches!(session, SessionTarget::New { cwd: Some(cwd) } if cwd == "/work"));
assert_eq!(target.destination, direct.destination);
assert_eq!(target.ssh.executable, direct.ssh.executable);
assert_eq!(target.ssh.agent_socket, direct.ssh.agent_socket);
let ClientMessage::OpenRemoteWindow { target, .. } =
request(Some(&direct), Some((42, "inner".into())))
else {
panic!("expected nested SSH window")
};
assert_eq!(target.destination, "inner");
let fallback = SshOptions::from_env();
assert_eq!(target.ssh.executable, fallback.executable);
assert_eq!(target.ssh.agent_socket, fallback.agent_socket);
}
#[test]
fn invalid_frame_does_not_disconnect_local_or_remote_workspaces() {
let remote = RemoteSession {