This commit expands the set of data that we track for each client to include the SSH_AUTH_SOCK. This defaults to the value of that env var on the machine where the ClientId is constructed, which may be remote from the mux server. For the proxy scenario, a remote SSH_AUTH_SOCK path is not addressable, and what we really want is the local SSH_AUTH_SOCK path from that SSH session, so we introduce awareness of whether the current session is a proxy session. Proxy sessions register the mux-local-proxy-id during the setup of the proxy, but we don't apply the identity to the mux. Instead, we wait for the remote client to send their identity and that is annotate to show that it has been routed via a proxy and, crucially, has the SSH_AUTH_SOCK replaced with the mux-local-proxy-id SSH_AUTH_SOCK. ``` ; wezterm cli list-clients USER HOST PID CONNECTED IDLE WORKSPACE FOCUS SSH_AUTH_SOCK wez foo 336500 86.944834352s 944.834352ms default 3 /home/wez/.1password/agent.sock wez foo (via proxy pid 337457) 337314 1.944834352s 944.834352ms 4 /tmp/ssh-XXXXTfGSp7/agent.337456 ``` For the SSH_AUTH_SOCK to be populated, ssh agent forwarding also needs to be enabled for SSH exec() calls; previously it was enabled only for pty channels. Since this commit changes the ABI of the mux protocol, the codec version has been bumped.
86 lines
2.6 KiB
Rust
86 lines
2.6 KiB
Rust
use clap::Parser;
|
|
use codec::{Pdu, SetClientId};
|
|
use config::ConfigHandle;
|
|
use mux::activity::Activity;
|
|
use mux::client::ClientId;
|
|
use mux::Mux;
|
|
use std::io::{Read, Write};
|
|
use std::sync::Arc;
|
|
use wezterm_client::client::{unix_connect_with_retry, Client};
|
|
use wezterm_client::domain::ClientDomainConfig;
|
|
|
|
#[derive(Debug, Parser, Clone)]
|
|
pub struct ProxyCommand {}
|
|
|
|
impl ProxyCommand {
|
|
pub async fn run(&self, client: Client, _config: &ConfigHandle) -> anyhow::Result<()> {
|
|
// The client object we created above will have spawned
|
|
// the server if needed, so now all we need to do is turn
|
|
// ourselves into basically netcat.
|
|
|
|
// Extract the selected configuration from the client,
|
|
// closing it in the process
|
|
let ClientDomainConfig::Unix(unix_dom) = client.into_client_domain_config() else {
|
|
anyhow::bail!("expected client to have connected to a unix domain");
|
|
};
|
|
|
|
let mux = Arc::new(mux::Mux::new(None));
|
|
Mux::set_mux(&mux);
|
|
|
|
let target = unix_dom.target();
|
|
let mut stream = unix_connect_with_retry(&target, false, None)?;
|
|
|
|
let pdu = Pdu::SetClientId(SetClientId {
|
|
client_id: ClientId::new(),
|
|
is_proxy: true,
|
|
});
|
|
let serial = 1;
|
|
pdu.encode(&mut stream, serial)?;
|
|
Pdu::decode(&mut stream)?;
|
|
|
|
// Spawn a thread to pull data from the socket and write
|
|
// it to stdout
|
|
let duped = stream.try_clone()?;
|
|
let activity = Activity::new();
|
|
std::thread::spawn(move || {
|
|
let stdout = std::io::stdout();
|
|
consume_stream_then_exit_process(duped, stdout.lock(), activity);
|
|
});
|
|
|
|
// and pull data from stdin and write it to the socket
|
|
let activity = Activity::new();
|
|
std::thread::spawn(move || {
|
|
let stdin = std::io::stdin();
|
|
consume_stream_then_exit_process(stdin.lock(), stream, activity);
|
|
});
|
|
|
|
// Wait forever; the stdio threads will terminate on EOF
|
|
smol::future::pending().await
|
|
}
|
|
}
|
|
|
|
fn consume_stream<F: Read, T: Write>(mut from_stream: F, mut to_stream: T) -> anyhow::Result<()> {
|
|
let mut buf = [0u8; 8192];
|
|
|
|
loop {
|
|
let size = from_stream.read(&mut buf)?;
|
|
if size == 0 {
|
|
break;
|
|
}
|
|
to_stream.write_all(&buf[0..size])?;
|
|
to_stream.flush()?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn consume_stream_then_exit_process<F: Read, T: Write>(
|
|
from_stream: F,
|
|
to_stream: T,
|
|
activity: Activity,
|
|
) -> ! {
|
|
consume_stream(from_stream, to_stream).ok();
|
|
std::thread::sleep(std::time::Duration::new(2, 0));
|
|
drop(activity);
|
|
std::process::exit(0);
|
|
}
|