A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.
Rust agentagent-orchestrationaiai-agentsclaude-codecodexcoding-agentlinuxmacosmultiplayermultiplexerrustterminalterminal-emulatorterminal-multiplexertmuxtuiweztermwindows
feat(ssh): try a host's other addresses
RoversX03ad5db63cfe
diff --git a/Cargo.lock b/Cargo.lockindex 3e4a22fa..7889b28f 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -8502,6 +8502,7 @@ dependencies = ["serde","serde_json","tempfile",+ "wezterm-ssh",][[package]]diff --git a/thinkterm-core/Cargo.toml b/thinkterm-core/Cargo.tomlindex 051d68c3..c794a9a2 100644--- a/thinkterm-core/Cargo.toml+++ b/thinkterm-core/Cargo.toml@@ -15,3 +15,4 @@ serde_json.workspace = true[dev-dependencies]tempfile.workspace = true+wezterm-ssh.workspace = truediff --git a/thinkterm-core/src/ssh_hosts.rs b/thinkterm-core/src/ssh_hosts.rsindex dee5353d..7a14fbfd 100644--- a/thinkterm-core/src/ssh_hosts.rs+++ b/thinkterm-core/src/ssh_hosts.rs@@ -8,6 +8,10 @@ use std::fs;use std::path::Path;pub type SshHostId = String;++/// The ssh option carrying a host's other addresses to the connection:+/// wezterm-ssh's `OTHER_ADDRESSES`.+pub const OTHER_ADDRESSES_OPTION: &str = "thinkterm_other_addresses";pub const DEFAULT_MOSH_SERVER_COMMAND: &str = "mosh-server new -s -l LANG=en_US.UTF-8";#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]@@ -16,6 +20,13 @@ pub struct SshHostSpec {pub host: String,#[serde(default)]pub port: Option<u16>,+ /// Other addresses the host answers at -- a LAN name, a Tailscale name,+ /// a public address with a forwarded port -- tried after `host`, in+ /// order, for whichever answers first. Each is `host` or `host:port`;+ /// without a port, `port` applies. The host key is checked against+ /// `host` whichever one carries the connection.+ #[serde(default, skip_serializing_if = "Vec::is_empty")]+ pub other_addresses: Vec<String>,#[serde(default)]pub username: Option<String>,#[serde(default)]@@ -181,6 +192,7 @@ pub fn parse_system_ssh_config(content: &str) -> Vec<SshHostEntry> {label: alias.clone(),host: alias,port: block.port,+ other_addresses: Vec::new(),username: block.user.clone(),identity_file: block.identity_file.clone(),password: None,@@ -285,6 +297,28 @@ pub fn ssh_domain_name(spec: &SshHostSpec) -> String {format!("ssh:{}", endpoint(spec))}+/// The host's other addresses as they are tried: trimmed, without blanks,+/// repeats or the host's own address.+pub fn other_addresses(spec: &SshHostSpec) -> Vec<String> {+ let mut addresses: Vec<String> = Vec::new();+ for address in &spec.other_addresses {+ let address = address.trim();+ if address.is_empty() || address == spec.host || addresses.iter().any(|a| a == address) {+ continue;+ }+ addresses.push(address.to_string());+ }+ addresses+}++/// Other addresses as a person types them: separated by commas or spaces.+pub fn parse_other_addresses(text: &str) -> Vec<String> {+ text.split(|c: char| c == ',' || c.is_whitespace())+ .filter(|address| !address.is_empty())+ .map(str::to_string)+ .collect()+}+pub fn build_ssh_domain(spec: &SshHostSpec) -> SshDomain {let mut ssh_option = spec.ssh_options@@ -299,6 +333,10 @@ pub fn build_ssh_domain(spec: &SshHostSpec) -> SshDomain {ssh_option.entry("connecttimeout".into()).or_insert_with(|| "10".into());+ let other_addresses = other_addresses(spec);+ if !other_addresses.is_empty() {+ ssh_option.insert(OTHER_ADDRESSES_OPTION.into(), other_addresses.join(" "));+ }SshDomain {name: ssh_domain_name(spec),remote_address: match spec.port {@@ -322,6 +360,42 @@ pub fn build_ssh_domain(spec: &SshHostSpec) -> SshDomain {mod tests {use super::*;+ #[test]+ fn the_option_is_the_one_the_connection_reads() {+ assert_eq!(OTHER_ADDRESSES_OPTION, wezterm_ssh::OTHER_ADDRESSES);+ }++ #[test]+ fn other_addresses_reach_the_connection_cleaned_up() {+ let mut spec: SshHostSpec =+ serde_json::from_value(serde_json::json!({"label": "myhost", "host": "myhost"}))+ .unwrap();+ assert!(+ spec.other_addresses.is_empty(),+ "a host saved before them has none"+ );+ assert!(!build_ssh_domain(&spec)+ .ssh_option+ .contains_key(OTHER_ADDRESSES_OPTION));+ assert!(!serde_json::to_string(&spec)+ .unwrap()+ .contains("other_addresses"));++ spec.other_addresses =+ parse_other_addresses("myhost.local, 203.0.113.7:2200 myhost ,myhost.local");+ assert_eq!(+ spec.other_addresses,+ vec!["myhost.local", "203.0.113.7:2200", "myhost", "myhost.local"]+ );+ assert_eq!(+ build_ssh_domain(&spec)+ .ssh_option+ .get(OTHER_ADDRESSES_OPTION)+ .map(String::as_str),+ Some("myhost.local 203.0.113.7:2200")+ );+ }+#[test]fn parses_only_concrete_system_hosts() {let entries = parse_system_ssh_config(@@ -357,6 +431,7 @@ mod tests {label: "server".into(),host: "example.test".into(),port: Some(22),+ other_addresses: Vec::new(),username: Some("root".into()),identity_file: None,password: None,diff --git a/thinkterm-i18n/i18n/de-DE.ftl b/thinkterm-i18n/i18n/de-DE.ftlindex ab144837..f186090d 100644--- a/thinkterm-i18n/i18n/de-DE.ftl+++ b/thinkterm-i18n/i18n/de-DE.ftl@@ -526,6 +526,7 @@ ssh-new-host = Neuer Hostssh-field-name = Namessh-field-host = Hostssh-field-port = Port+ssh-field-other-addresses = Weitere Adressenssh-field-user = Benutzerssh-field-password = Passwortssh-field-identity = Identitätsdateidiff --git a/thinkterm-i18n/i18n/en-US.ftl b/thinkterm-i18n/i18n/en-US.ftlindex 6fc8e242..8605c60b 100644--- a/thinkterm-i18n/i18n/en-US.ftl+++ b/thinkterm-i18n/i18n/en-US.ftl@@ -526,6 +526,7 @@ ssh-new-host = New Hostssh-field-name = Namessh-field-host = Hostssh-field-port = Port+ssh-field-other-addresses = Other addressesssh-field-user = Userssh-field-password = Passwordssh-field-identity = Identity filediff --git a/thinkterm-i18n/i18n/fr-FR.ftl b/thinkterm-i18n/i18n/fr-FR.ftlindex 4e83cd27..2149f97c 100644--- a/thinkterm-i18n/i18n/fr-FR.ftl+++ b/thinkterm-i18n/i18n/fr-FR.ftl@@ -526,6 +526,7 @@ ssh-new-host = Nouvel hôtessh-field-name = Nomssh-field-host = Hôtessh-field-port = Port+ssh-field-other-addresses = Autres adressesssh-field-user = Utilisateurssh-field-password = Mot de passessh-field-identity = Fichier d’identitédiff --git a/thinkterm-i18n/i18n/ja-JP.ftl b/thinkterm-i18n/i18n/ja-JP.ftlindex 4bb70c0c..200d73c4 100644--- a/thinkterm-i18n/i18n/ja-JP.ftl+++ b/thinkterm-i18n/i18n/ja-JP.ftl@@ -517,6 +517,7 @@ ssh-new-host = 新しいホストssh-field-name = 名前ssh-field-host = ホストssh-field-port = ポート+ssh-field-other-addresses = その他のアドレスssh-field-user = ユーザーssh-field-password = パスワードssh-field-identity = Identity ファイルdiff --git a/thinkterm-i18n/i18n/zh-CN.ftl b/thinkterm-i18n/i18n/zh-CN.ftlindex e512e339..f0512d5b 100644--- a/thinkterm-i18n/i18n/zh-CN.ftl+++ b/thinkterm-i18n/i18n/zh-CN.ftl@@ -517,6 +517,7 @@ ssh-new-host = 新建主机ssh-field-name = 名称ssh-field-host = 主机ssh-field-port = 端口+ssh-field-other-addresses = 其他地址ssh-field-user = 用户ssh-field-password = 密码ssh-field-identity = 私钥文件diff --git a/wezterm-gui/src/ssh_hosts.rs b/wezterm-gui/src/ssh_hosts.rsindex 4bac40d1..7a30ea3e 100644--- a/wezterm-gui/src/ssh_hosts.rs+++ b/wezterm-gui/src/ssh_hosts.rs@@ -23,8 +23,8 @@ use std::sync::Arc;use wezterm_ssh::{Session, SessionEvent};pub use thinkterm_core::ssh_hosts::{- default_mosh_server_command, ShellIntegrationFailure, SshHostEntry, SshHostId, SshHostSource,- SshHostSpec, DEFAULT_MOSH_SERVER_COMMAND,+ default_mosh_server_command, parse_other_addresses, ShellIntegrationFailure, SshHostEntry,+ SshHostId, SshHostSource, SshHostSpec, DEFAULT_MOSH_SERVER_COMMAND,};#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]@@ -530,7 +530,10 @@ fn read_fd_to_string(mut fd: FileDescriptor) -> std::io::Result<String> {async fn connect_mosh_bootstrap_session(spec: &SshHostSpec) -> Result<(Session, String)> {let dom = build_ssh_domain(spec);- let ssh_config = mux::ssh::ssh_domain_to_ssh_config(&dom).context("build SSH config")?;+ let mut ssh_config = mux::ssh::ssh_domain_to_ssh_config(&dom).context("build SSH config")?;+ // mosh-client goes to the host's own address over UDP, so the+ // bootstrap that starts mosh-server there must not land elsewhere.+ ssh_config.remove(wezterm_ssh::OTHER_ADDRESSES);let mosh_client_host = ssh_config.get("hostname").cloned()@@ -777,6 +780,7 @@ fn parse_system_ssh_config_str(content: &str) -> Vec<SshHostEntry> {// alias-scoped options such as ProxyJump still apply.host: alias.clone(),port: block.port,+ other_addresses: Vec::new(),username: block.user.clone(),identity_file: block.identity_file.clone(),password: None,@@ -1041,6 +1045,7 @@ Host prod *.internallabel: "t".to_string(),host: "example.com".to_string(),port: None,+ other_addresses: Vec::new(),username: None,identity_file: None,password: None,diff --git a/wezterm-gui/src/termwindow/ssh_hosts_view.rs b/wezterm-gui/src/termwindow/ssh_hosts_view.rsindex 083fe0bf..afe97790 100644--- a/wezterm-gui/src/termwindow/ssh_hosts_view.rs+++ b/wezterm-gui/src/termwindow/ssh_hosts_view.rs@@ -35,13 +35,14 @@ pub(crate) const SSH_HOSTS_CONTENT_VIEW_KEY: &str = "ssh-hosts";const FIELD_NAME: usize = 0;const FIELD_HOST: usize = 1;const FIELD_PORT: usize = 2;-const FIELD_USER: usize = 3;-const FIELD_PASSWORD: usize = 4;-const FIELD_IDENTITY: usize = 5;-const FIELD_WORKSPACE: usize = 6;-const FIELD_MOSH_SERVER: usize = 7;-const BASE_FIELD_COUNT: usize = 7;-const FIELD_COUNT: usize = 8;+const FIELD_OTHER_ADDRESSES: usize = 3;+const FIELD_USER: usize = 4;+const FIELD_PASSWORD: usize = 5;+const FIELD_IDENTITY: usize = 6;+const FIELD_WORKSPACE: usize = 7;+const FIELD_MOSH_SERVER: usize = 8;+const BASE_FIELD_COUNT: usize = 8;+const FIELD_COUNT: usize = 9;/// Where focus lands when the Advanced section is collapsed. Rows inside it/// stop being drawn, so a focus still pointing at one would silently edit an@@ -342,6 +343,7 @@ impl SshHostsView {fields[FIELD_NAME] = spec.label.clone();fields[FIELD_HOST] = spec.host.clone();fields[FIELD_PORT] = spec.port.map(|p| p.to_string()).unwrap_or_default();+ fields[FIELD_OTHER_ADDRESSES] = spec.other_addresses.join(", ");fields[FIELD_USER] = spec.username.clone().unwrap_or_default();fields[FIELD_PASSWORD] = spec.password@@ -435,6 +437,7 @@ impl SshHostsView {label: label.clone(),host: host.clone(),port,+ other_addresses: Vec::new(),username: None,identity_file: None,password: None,@@ -454,6 +457,8 @@ impl SshHostsView {spec.label = label;spec.host = host;spec.port = port;+ spec.other_addresses =+ ssh_hosts::parse_other_addresses(&form.fields[FIELD_OTHER_ADDRESSES].text());spec.username = opt(&form.fields[FIELD_USER].text());// Encrypt the password before it is persisted to ssh_hosts.json. Never// fall back to plaintext: if encryption fails, keep the form open.@@ -2016,6 +2021,28 @@ impl SshHostsView {x: f32,y: f32,width: f32,+ ) -> anyhow::Result<f32> {+ self.paint_field_hinted(+ ctx, layers, font, palette, tokens, cursor_on, label, index, shown, "", x, y, width,+ )+ }++ #[allow(clippy::too_many_arguments)]+ fn paint_field_hinted(+ &mut self,+ ctx: &DrawContext,+ layers: &mut TripleLayerQuadAllocator<'_>,+ font: &Rc<LoadedFont>,+ palette: UiPalette,+ tokens: UiTokens,+ cursor_on: bool,+ label: &str,+ index: usize,+ shown: &str,+ placeholder: &str,+ x: f32,+ y: f32,+ width: f32,) -> anyhow::Result<f32> {let line_h = ctx.metrics.cell_size.height as f32;ctx.draw_text(layers, font, x, y, label, palette.muted_text, width)?;@@ -2035,7 +2062,7 @@ impl SshHostsView {text_pad,cursor_on,TextInputSpec {- placeholder: "",+ placeholder,text: shown,rect: rect(x, input_y, width, ctx.px(INPUT_H)),focused,@@ -2269,7 +2296,8 @@ impl SshHostsView {let mut body_layers = TripleLayerQuadAllocator::Heap(&mut heap);// ---- Connection- let conn_h = Self::inspector_card_height(ctx, section_h, &[field_h, field_h, field_h]);+ let conn_h =+ Self::inspector_card_height(ctx, section_h, &[field_h, field_h, field_h, field_h]);let mut fy = Self::paint_inspector_card(ctx,section_ctx,@@ -2326,6 +2354,22 @@ impl SshHostsView {fy,port_w,)? + gap;+ // Tried after Host, in order, when it does not answer.+ fy = self.paint_field_hinted(+ ctx,+ &mut body_layers,+ font,+ palette,+ tokens,+ cursor_on,+ &crate::i18n::tr("ssh-field-other-addresses"),+ FIELD_OTHER_ADDRESSES,+ &fields[FIELD_OTHER_ADDRESSES].text(),+ "myhost.local, 127.0.0.1",+ x + pad,+ fy,+ inner,+ )? + gap;self.paint_field(ctx,&mut body_layers,@@ -2954,6 +2998,7 @@ mod tests {label: label.to_string(),host: host.to_string(),port: Some(2222),+ other_addresses: Vec::new(),username: Some("deploy".to_string()),identity_file: Some("~/.ssh/id_ed25519".to_string()),password: None,diff --git a/wezterm-ssh/src/addresses.rs b/wezterm-ssh/src/addresses.rsnew file mode 100644index 00000000..48362f58--- /dev/null+++ b/wezterm-ssh/src/addresses.rs@@ -0,0 +1,336 @@+//! Reaching a host at one of several addresses.+//!+//! A host can be listed with addresses besides its own, say a LAN name, a+//! Tailscale name and a public address with a forwarded port. They are+//! tried in order, each a moment after the one before (or at once when+//! that one has failed), and the first to accept a connection is used.+//! Only the TCP connection is raced: the ssh handshake, the host key check+//! -- made against the host's own name and port, whichever address carried+//! the connection -- and authentication then happen once, on that socket.+//! An other address whose server holds a different key than the one on+//! record for the host is some other machine, and is passed over for the+//! rest. Whichever address got a session through last is tried first next+//! time, for a while.++use crate::config::ConfigMap;+use anyhow::{bail, Context};+use socket2::{Domain, Socket, Type};+use std::collections::VecDeque;+use std::net::ToSocketAddrs;+use std::sync::Mutex;+use std::time::{Duration, Instant};++/// The config key that lists a host's other addresses, separated by+/// spaces: each `host` or `host:port` (`[v6]:port` for an IPv6 address+/// with a port). Without a port the host's port applies.+pub const OTHER_ADDRESSES: &str = "thinkterm_other_addresses";++/// How long an address has before the next one is tried as well.+const STAGGER: Duration = Duration::from_millis(300);++/// How many hosts' last good address are remembered, and for how long.+const REMEMBERED: usize = 64;+const REMEMBERED_FOR: Duration = Duration::from_secs(60 * 60);++/// The address that last got a session through, per host and port, and+/// when.+static LAST_REACHED: Mutex<VecDeque<(String, (String, u16), Instant)>> =+ Mutex::new(VecDeque::new());++/// `host`, `host:port` or `[v6]:port`.+pub fn parse_address(text: &str, default_port: u16) -> Option<(String, u16)> {+ let text = text.trim();+ if text.is_empty() {+ return None;+ }+ if let Some(rest) = text.strip_prefix('[') {+ let (host, rest) = rest.split_once(']')?;+ return match rest.strip_prefix(':') {+ Some(port) => Some((host.to_string(), port.parse().ok()?)),+ None if rest.is_empty() => Some((host.to_string(), default_port)),+ None => None,+ };+ }+ match text.split_once(':') {+ // One colon: a port. More than one: an IPv6 address without one.+ Some((host, port)) if !port.contains(':') => Some((host.to_string(), port.parse().ok()?)),+ _ => Some((text.to_string(), default_port)),+ }+}++/// The addresses to try for `hostname:port`, in order: the one that last+/// got through first, then the host's own, then the others it lists.+pub fn candidates(config: &ConfigMap, hostname: &str, port: u16) -> Vec<(String, u16)> {+ let mut list = vec![(hostname.to_string(), port)];+ if let Some(others) = config.get(OTHER_ADDRESSES) {+ for address in others.split_whitespace() {+ if let Some(address) = parse_address(address, port) {+ if !list.contains(&address) {+ list.push(address);+ }+ }+ }+ }+ if list.len() > 1 {+ if let Some(last) = last_reached(hostname, port) {+ if let Some(at) = list.iter().position(|address| *address == last) {+ let last = list.remove(at);+ list.insert(0, last);+ }+ }+ }+ list+}++fn last_reached(hostname: &str, port: u16) -> Option<(String, u16)> {+ let key = format!("{hostname}:{port}");+ let mut last = LAST_REACHED.lock().unwrap();+ last.retain(|(_, _, at)| at.elapsed() < REMEMBERED_FOR);+ last.iter()+ .find(|(k, _, _)| *k == key)+ .map(|(_, address, _)| address.clone())+}++pub fn remember_reached(hostname: &str, port: u16, address: &(String, u16)) {+ let key = format!("{hostname}:{port}");+ let mut last = LAST_REACHED.lock().unwrap();+ last.retain(|(k, _, at)| *k != key && at.elapsed() < REMEMBERED_FOR);+ if last.len() >= REMEMBERED {+ last.pop_front();+ }+ last.push_back((key, address.clone(), Instant::now()));+}++/// None of the host's addresses works now: the next attempt starts over+/// from the host's own.+pub fn forget_reached(hostname: &str, port: u16) {+ let key = format!("{hostname}:{port}");+ LAST_REACHED.lock().unwrap().retain(|(k, _, _)| *k != key);+}++/// What opening one TCP connection needs from the config, so that it can+/// be done off the session's thread.+pub struct Dialer {+ family: Option<String>,+ bind: Option<String>,+ timeout: Option<Duration>,+}++impl Dialer {+ pub fn from_config(config: &ConfigMap) -> Self {+ Self {+ family: config.get("addressfamily").cloned(),+ bind: config.get("bindaddress").cloned(),+ timeout: config+ .get("connecttimeout")+ .and_then(|value| value.parse::<u64>().ok())+ .filter(|seconds| *seconds > 0)+ .map(Duration::from_secs),+ }+ }++ fn family_allows(&self, addr: &std::net::SocketAddr) -> bool {+ match self.family.as_deref() {+ Some("inet") => addr.is_ipv4(),+ Some("inet6") => addr.is_ipv6(),+ None | Some("any") | Some(_) => true,+ }+ }++ pub fn dial(&self, host: &str, port: u16) -> anyhow::Result<Socket> {+ self.dial_with(host, port, false, |_| Ok(()))+ }++ /// `dial`, logging what it resolves and binds when `verbose`, and+ /// handing the socket to `before_connect` before the connect that can+ /// block: a session watches it there, to cut it short on shutdown.+ pub fn dial_with(+ &self,+ host: &str,+ port: u16,+ verbose: bool,+ before_connect: impl FnOnce(&Socket) -> anyhow::Result<()>,+ ) -> anyhow::Result<Socket> {+ let addr = (host, port)+ .to_socket_addrs()?+ .find(|addr| self.family_allows(addr))+ .with_context(|| format!("resolving address for {host}"))?;+ if verbose {+ log::info!("resolved {host}:{port} -> {addr:?}");+ }+ let sock = Socket::new(Domain::for_address(addr), Type::STREAM, None)?;+ before_connect(&sock)?;+ if let Some(bind_addr) = &self.bind {+ let bind_addr = (bind_addr.as_str(), 0)+ .to_socket_addrs()?+ .find(|addr| self.family_allows(addr))+ .with_context(|| format!("resolving bind address {bind_addr:?}"))?;+ if verbose {+ log::info!("binding to {bind_addr:?}");+ }+ sock.bind(&bind_addr.into())+ .with_context(|| format!("binding to {bind_addr:?}"))?;+ }+ match self.timeout {+ Some(timeout) => {+ sock.connect_timeout(&addr.into(), timeout)+ .with_context(|| {+ format!("Connecting to {host}:{port} ({addr:?}) within {timeout:?}")+ })?;+ sock.set_nonblocking(false).with_context(|| {+ format!("restoring blocking mode after connect to {host}:{port}")+ })?;+ }+ None => sock+ .connect(&addr.into())+ .with_context(|| format!("Connecting to {host}:{port} ({addr:?})"))?,+ }+ Ok(sock)+ }+}++/// Connect to the first of `candidates` that answers, starting each a+/// moment after the one before, or as soon as every one started so far has+/// failed. Gives back the socket and which candidate it reached. `stop`+/// is asked between waits, to give up when the session is shutting down.+pub fn race(+ candidates: &[(String, u16)],+ dialer: Dialer,+ stop: impl Fn() -> anyhow::Result<()>,+) -> anyhow::Result<(Socket, usize)> {+ let dialer = std::sync::Arc::new(dialer);+ let (tx, rx) = std::sync::mpsc::channel();+ let start = Instant::now();+ let mut started = 0;+ let mut pending = 0;+ let mut failures = vec![];+ loop {+ let due = start + STAGGER * started as u32;+ if started < candidates.len() && (pending == 0 || Instant::now() >= due) {+ let (host, port) = candidates[started].clone();+ let index = started;+ let tx = tx.clone();+ let dialer = std::sync::Arc::clone(&dialer);+ std::thread::spawn(move || {+ // Nobody listening any more just means another won.+ let _ = tx.send((index, dialer.dial(&host, port)));+ });+ started += 1;+ pending += 1;+ continue;+ }+ if pending == 0 {+ break;+ }+ let wait = match started < candidates.len() {+ true => due.saturating_duration_since(Instant::now()),+ false => Duration::from_millis(100),+ };+ match rx.recv_timeout(wait) {+ Ok((index, Ok(sock))) => return Ok((sock, index)),+ Ok((index, Err(err))) => {+ pending -= 1;+ let (host, port) = &candidates[index];+ failures.push(format!("{host}:{port}: {err:#}"));+ }+ Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {}+ Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => break,+ }+ stop()?;+ }+ bail!(+ "none of the host's addresses answered: {}",+ failures.join("; ")+ )+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn addresses_take_a_port_or_the_hosts_own() {+ assert_eq!(+ parse_address("myhost.local", 22),+ Some(("myhost.local".into(), 22))+ );+ assert_eq!(+ parse_address("203.0.113.7:2222", 22),+ Some(("203.0.113.7".into(), 2222))+ );+ assert_eq!(+ parse_address("[2001:db8::1]:2200", 22),+ Some(("2001:db8::1".into(), 2200))+ );+ assert_eq!(+ parse_address("[2001:db8::1]", 22),+ Some(("2001:db8::1".into(), 22))+ );+ assert_eq!(+ parse_address("2001:db8::1", 22),+ Some(("2001:db8::1".into(), 22))+ );+ assert_eq!(parse_address("myhost:port", 22), None);+ assert_eq!(parse_address(" ", 22), None);+ }++ #[test]+ fn the_address_that_got_through_last_goes_first() {+ let mut config = ConfigMap::new();+ config.insert(+ OTHER_ADDRESSES.to_string(),+ "server.lan server.tailnet:2200 server.lan".to_string(),+ );+ let list = candidates(&config, "server", 22);+ assert_eq!(+ list,+ vec![+ ("server".to_string(), 22),+ ("server.lan".to_string(), 22),+ ("server.tailnet".to_string(), 2200),+ ]+ );+ remember_reached("server", 22, &("server.tailnet".to_string(), 2200));+ assert_eq!(+ candidates(&config, "server", 22)[0],+ ("server.tailnet".to_string(), 2200)+ );+ // When none of them works, the next attempt starts from the host's own.+ forget_reached("server", 22);+ assert_eq!(+ candidates(&config, "server", 22)[0],+ ("server".to_string(), 22)+ );+ // Without other addresses there is nothing to reorder.+ assert_eq!(+ candidates(&ConfigMap::new(), "server", 22),+ vec![("server".to_string(), 22)]+ );+ }++ #[test]+ fn an_address_that_does_not_answer_gives_way_to_one_that_does() {+ let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();+ let port = listener.local_addr().unwrap().port();+ // Bound and closed again: nothing answers there.+ let refused = std::net::TcpListener::bind("127.0.0.1:0").unwrap();+ let refused_port = refused.local_addr().unwrap().port();+ drop(refused);++ let mut config = ConfigMap::new();+ config.insert("connecttimeout".to_string(), "5".to_string());+ let candidates = vec![+ ("127.0.0.1".to_string(), refused_port),+ ("127.0.0.1".to_string(), port),+ ];+ let started = Instant::now();+ let (_sock, index) = race(&candidates, Dialer::from_config(&config), || Ok(())).unwrap();+ assert_eq!(index, 1);+ // The refusal starts the next one at once, without the stagger.+ assert!(started.elapsed() < STAGGER, "took {:?}", started.elapsed());++ let all_refused = vec![("127.0.0.1".to_string(), refused_port)];+ let err = race(&all_refused, Dialer::from_config(&config), || Ok(())).unwrap_err();+ assert!(format!("{err:#}").contains("none of the host's addresses answered"));+ }+}diff --git a/wezterm-ssh/src/host.rs b/wezterm-ssh/src/host.rsindex bb8d6779..55b22b79 100644--- a/wezterm-ssh/src/host.rs+++ b/wezterm-ssh/src/host.rs@@ -92,6 +92,40 @@ impl crate::sessioninner::SessionInner {}}+ /// Whether the keys on record for `hostname:port` say this session's+ /// server is some other machine: one is there, and the server's key is+ /// not it. Nothing is asked or reported.+ #[cfg(feature = "ssh2")]+ pub fn known_key_differs(+ &self,+ sess: &ssh2::Session,+ hostname: &str,+ port: u16,+ ) -> anyhow::Result<bool> {+ let Some((key, _)) = sess.host_key() else {+ return Ok(false);+ };+ let mut known_hosts = sess.known_hosts().context("preparing known hosts")?;+ let files = self+ .config+ .get("userknownhostsfile")+ .map(|files| files.split_whitespace().map(str::to_string).collect())+ .unwrap_or_else(Vec::new);+ for file in files {+ let file = std::path::Path::new(&file);+ if !file.exists() {+ continue;+ }+ known_hosts+ .read_file(file, ssh2::KnownHostFileKind::OpenSSH)+ .with_context(|| format!("reading known_hosts file {}", file.display()))?;+ if let ssh2::CheckResult::Mismatch = known_hosts.check_port(hostname, port, key) {+ return Ok(true);+ }+ }+ Ok(false)+ }+#[cfg(feature = "ssh2")]pub fn host_verification(&mut self,diff --git a/wezterm-ssh/src/lib.rs b/wezterm-ssh/src/lib.rsindex abf4837a..3b2e4911 100644--- a/wezterm-ssh/src/lib.rs+++ b/wezterm-ssh/src/lib.rs@@ -1,6 +1,7 @@#[cfg(not(any(feature = "libssh-rs", feature = "ssh2")))]compile_error!("Either libssh-rs or ssh2 must be enabled!");+mod addresses;mod auth;mod channelwrap;mod config;@@ -14,6 +15,7 @@ mod sessionwrap;mod sftp;mod sftpwrap;+pub use addresses::OTHER_ADDRESSES;pub use auth::*;pub use config::*;pub use host::*;diff --git a/wezterm-ssh/src/sessioninner.rs b/wezterm-ssh/src/sessioninner.rsindex 1c9bb95a..ab6701c8 100644--- a/wezterm-ssh/src/sessioninner.rs+++ b/wezterm-ssh/src/sessioninner.rs@@ -18,10 +18,9 @@ use filedescriptor::{};use portable_pty::ExitStatus;use smol::channel::{bounded, Receiver, Sender, TryRecvError};-use socket2::{Domain, Socket, Type};+use socket2::Socket;use std::collections::{HashMap, VecDeque};use std::io::{Read, Write};-use std::net::ToSocketAddrs;use std::sync::Arc;use std::time::{Duration, Instant};@@ -36,6 +35,11 @@ pub(crate) struct DescriptorState {/// throughput of a busy channel; 8 KiB made a mux client's output crawl.pub(crate) const CHANNEL_BUFFER: usize = 64 * 1024;+/// Whether `address` is the host's own, as opposed to one of its others.+fn is_own(address: &(String, u16), hostname: &str, port: u16) -> bool {+ address.0 == hostname && address.1 == port+}+pub(crate) struct ChannelInfo {pub channel_id: ChannelId,pub channel: ChannelWrap,@@ -142,7 +146,6 @@ impl SessionInner {)))).context("notifying user of banner")?;- let sess = libssh_rs::Session::new()?;let verbose = self.config.get("wezterm_ssh_verbose")@@ -150,7 +153,6 @@ impl SessionInner {.unwrap_or("false")== "true";if verbose {- sess.set_option(libssh_rs::SshOption::LogLevel(libssh_rs::LogLevel::Packet))?;/// libssh logs to stderr, but on Windows in the GUI there isn't a valid/// stderr for it to log to.@@ -188,55 +190,44 @@ impl SessionInner {libssh_rs::sys::ssh_set_log_callback(Some(log_callback));}}- sess.set_option(libssh_rs::SshOption::Hostname(hostname.clone()))?;- sess.set_option(libssh_rs::SshOption::User(Some(user)))?;- sess.set_option(libssh_rs::SshOption::Port(port))?;- sess.options_parse_config(None)?; // FIXME: overridden config path?- if let Some(agent) = self.config.get("identityagent") {- sess.set_option(libssh_rs::SshOption::IdentityAgent(Some(agent.clone())))?;- }- if let Some(files) = self.config.get("identityfile") {- for file in files.split_whitespace() {- sess.set_option(libssh_rs::SshOption::AddIdentity(file.to_string()))?;- }- }- if let Some(kh) = self.config.get("userknownhostsfile") {- for file in kh.split_whitespace() {- sess.set_option(libssh_rs::SshOption::KnownHosts(Some(file.to_string())))?;- break;- }- }- if let Some(types) = self.config.get("pubkeyacceptedtypes") {- sess.set_option(libssh_rs::SshOption::PublicKeyAcceptedTypes(- types.to_string(),- ))?;- }- if let Some(bind_addr) = self.config.get("bindaddress") {- sess.set_option(libssh_rs::SshOption::BindAddress(bind_addr.to_string()))?;- }- if let Some(host_key) = self.config.get("hostkeyalgorithms") {- sess.set_option(libssh_rs::SshOption::HostKeys(host_key.to_string()))?;- }- let (sock, _child) = self.connect_to_host(&hostname, port, verbose)?;- self.shutdown.watch(&sock)?;- let raw = {- #[cfg(unix)]- {- use std::os::unix::io::IntoRawFd;- sock.into_raw_fd()- }- #[cfg(windows)]- {- use std::os::windows::io::IntoRawSocket;- sock.into_raw_socket()- }- };+ // A session per address tried: see `connect_to_host`.+ let mut not_this_host = vec![];+ let (sess, _child, reached) = loop {+ let sess = self.libssh_session(&hostname, &user, port, verbose)?;+ let (sock, child, reached) =+ self.connect_to_host(&hostname, port, verbose, ¬_this_host)?;+ self.shutdown.watch(&sock)?;+ let raw = {+ #[cfg(unix)]+ {+ use std::os::unix::io::IntoRawFd;+ sock.into_raw_fd()+ }+ #[cfg(windows)]+ {+ use std::os::windows::io::IntoRawSocket;+ sock.into_raw_socket()+ }+ };- sess.set_option(libssh_rs::SshOption::Socket(raw))?;+ sess.set_option(libssh_rs::SshOption::Socket(raw))?;- sess.connect()- .with_context(|| format!("Connecting to {hostname}:{port}"))?;+ sess.connect()+ .with_context(|| format!("Connecting to {hostname}:{port}"))?;++ if let Some(other) = reached.as_ref().filter(|at| !is_own(at, &hostname, port)) {+ if matches!(+ sess.is_known_server()?,+ libssh_rs::KnownHosts::Changed | libssh_rs::KnownHosts::Other+ ) {+ self.pass_over(&hostname, other)?;+ not_this_host.push(other.clone());+ continue;+ }+ }+ break (sess, child, reached);+ };let banner = sess.get_server_banner()?;self.tx_event@@ -245,6 +236,9 @@ impl SessionInner {self.host_verification_libssh(&sess, &hostname, port)?;self.authenticate_libssh(&sess)?;+ if let Some(reached) = &reached {+ crate::addresses::remember_reached(&hostname, port, reached);+ }if let Ok(banner) = sess.get_issue_banner() {self.tx_event@@ -270,6 +264,52 @@ impl SessionInner {result}+ /// A libssh session for the host, with every option set except the+ /// socket.+ #[cfg(feature = "libssh-rs")]+ fn libssh_session(+ &self,+ hostname: &str,+ user: &str,+ port: u16,+ verbose: bool,+ ) -> anyhow::Result<libssh_rs::Session> {+ let sess = libssh_rs::Session::new()?;+ if verbose {+ sess.set_option(libssh_rs::SshOption::LogLevel(libssh_rs::LogLevel::Packet))?;+ }+ sess.set_option(libssh_rs::SshOption::Hostname(hostname.to_string()))?;+ sess.set_option(libssh_rs::SshOption::User(Some(user.to_string())))?;+ sess.set_option(libssh_rs::SshOption::Port(port))?;+ sess.options_parse_config(None)?; // FIXME: overridden config path?+ if let Some(agent) = self.config.get("identityagent") {+ sess.set_option(libssh_rs::SshOption::IdentityAgent(Some(agent.clone())))?;+ }+ if let Some(files) = self.config.get("identityfile") {+ for file in files.split_whitespace() {+ sess.set_option(libssh_rs::SshOption::AddIdentity(file.to_string()))?;+ }+ }+ if let Some(kh) = self.config.get("userknownhostsfile") {+ for file in kh.split_whitespace() {+ sess.set_option(libssh_rs::SshOption::KnownHosts(Some(file.to_string())))?;+ break;+ }+ }+ if let Some(types) = self.config.get("pubkeyacceptedtypes") {+ sess.set_option(libssh_rs::SshOption::PublicKeyAcceptedTypes(+ types.to_string(),+ ))?;+ }+ if let Some(bind_addr) = self.config.get("bindaddress") {+ sess.set_option(libssh_rs::SshOption::BindAddress(bind_addr.to_string()))?;+ }+ if let Some(host_key) = self.config.get("hostkeyalgorithms") {+ sess.set_option(libssh_rs::SshOption::HostKeys(host_key.to_string()))?;+ }+ Ok(sess)+ }+#[cfg(feature = "ssh2")]fn run_impl_ssh2(&mut self) -> anyhow::Result<()> {let verbose = self@@ -303,17 +343,31 @@ impl SessionInner {)))).context("notifying user of banner")?;- let (sock, _child) = self.connect_to_host(&hostname, port, verbose)?;- self.shutdown.watch(&sock)?;+ // A session per address tried: see `connect_to_host`.+ let mut not_this_host = vec![];+ let (sess, _child, reached) = loop {+ let (sock, child, reached) =+ self.connect_to_host(&hostname, port, verbose, ¬_this_host)?;+ self.shutdown.watch(&sock)?;- let mut sess = ssh2::Session::new()?;- if verbose {- sess.trace(ssh2::TraceFlags::all());- }- sess.set_blocking(true);- sess.set_tcp_stream(sock);- sess.handshake()- .with_context(|| format!("ssh handshake with {}", remote_address))?;+ let mut sess = ssh2::Session::new()?;+ if verbose {+ sess.trace(ssh2::TraceFlags::all());+ }+ sess.set_blocking(true);+ sess.set_tcp_stream(sock);+ sess.handshake()+ .with_context(|| format!("ssh handshake with {}", remote_address))?;++ if let Some(other) = reached.as_ref().filter(|at| !is_own(at, &hostname, port)) {+ if self.known_key_differs(&sess, &hostname, port)? {+ self.pass_over(&hostname, other)?;+ not_this_host.push(other.clone());+ continue;+ }+ }+ break (sess, child, reached);+ };self.tx_event.try_send(SessionEvent::Banner(sess.banner().map(|s| s.to_string())))@@ -324,6 +378,9 @@ impl SessionInner {self.authenticate(&sess, &user, &hostname).context("authentication")?;+ if let Some(reached) = &reached {+ crate::addresses::remember_reached(&hostname, port, reached);+ }self.tx_event.try_send(SessionEvent::Authenticated)@@ -343,12 +400,16 @@ impl SessionInner {/// If proxy_command is set, then we execute that process for ourselves/// too, as proxy commands are not supported by libssh2 and are not supported/// on Windows in libssh.+ /// With other addresses for the host (see `crate::addresses`), also+ /// gives back the one that answered, and leaves out those in+ /// `not_this_host`: other addresses found serving some other machine.fn connect_to_host(&self,hostname: &str,port: u16,verbose: bool,- ) -> anyhow::Result<(Socket, Option<KillOnDropChild>)> {+ not_this_host: &[(String, u16)],+ ) -> anyhow::Result<(Socket, Option<KillOnDropChild>, Option<(String, u16)>)> {self.shutdown.check()?;match self.config.get("proxycommand").map(|s| s.as_str()) {Some("none") | None => {}@@ -390,74 +451,64 @@ impl SessionInner {_ => a.into_raw_fd(),};- return Ok((Socket::from_raw_fd(dest), Some(child)));+ return Ok((Socket::from_raw_fd(dest), Some(child), None));}#[cfg(windows)]unsafe {use std::os::windows::io::{FromRawSocket, IntoRawSocket};- return Ok((Socket::from_raw_socket(a.into_raw_socket()), Some(child)));+ return Ok((+ Socket::from_raw_socket(a.into_raw_socket()),+ Some(child),+ None,+ ));}}}- let addr = (hostname, port)- .to_socket_addrs()?- .find(|addr| self.filter_sock_addr(addr))- .with_context(|| format!("resolving address for {}", hostname))?;- if verbose {- log::info!("resolved {hostname}:{port} -> {addr:?}");- }- let sock = Socket::new(Domain::for_address(addr), Type::STREAM, None)?;- self.shutdown.watch(&sock)?;- if let Some(bind_addr) = self.config.get("bindaddress") {- let bind_addr = (bind_addr.as_str(), 0)- .to_socket_addrs()?- .find(|addr| self.filter_sock_addr(addr))- .with_context(|| format!("resolving bind address {bind_addr:?}"))?;+ let mut candidates = crate::addresses::candidates(&self.config, hostname, port);+ candidates.retain(|address| !not_this_host.contains(address));+ let dialer = crate::addresses::Dialer::from_config(&self.config);+ if candidates.len() > 1 {if verbose {- log::info!("binding to {bind_addr:?}");+ log::info!("trying {hostname}:{port} at {candidates:?}");}- sock.bind(&bind_addr.into())- .with_context(|| format!("binding to {bind_addr:?}"))?;- }-- // Honor a connect timeout so that an unreachable host fails promptly- // instead of blocking for the OS default (~75s on macOS, or indefinitely- // for blackhole routes). `connect_timeout` leaves the socket in- // non-blocking mode, so restore blocking afterwards for the handshake.- match self- .config- .get("connecttimeout")- .and_then(|value| value.parse::<u64>().ok())- .filter(|seconds| *seconds > 0)- .map(Duration::from_secs)- {- Some(timeout) => {- sock.connect_timeout(&addr.into(), timeout)- .with_context(|| {- format!("Connecting to {hostname}:{port} ({addr:?}) within {timeout:?}")- })?;- sock.set_nonblocking(false).with_context(|| {- format!("restoring blocking mode after connect to {hostname}:{port}")- })?;- }- None => {- sock.connect(&addr.into())- .with_context(|| format!("Connecting to {hostname}:{port} ({addr:?})"))?;+ let raced = crate::addresses::race(&candidates, dialer, || self.shutdown.check());+ let (sock, index) = match raced {+ Ok(won) => won,+ Err(err) => {+ crate::addresses::forget_reached(hostname, port);+ return Err(err);+ }+ };+ let reached = candidates.swap_remove(index);+ if !is_own(&reached, hostname, port) {+ self.tx_event+ .try_send(SessionEvent::Banner(Some(format!(+ "Reached {hostname} at {}:{}",+ reached.0, reached.1+ ))))+ .context("notifying user of banner")?;}+ self.shutdown.check()?;+ return Ok((sock, None, Some(reached)));}++ let sock = dialer.dial_with(hostname, port, verbose, |sock| self.shutdown.watch(sock))?;self.shutdown.check()?;- Ok((sock, None))+ Ok((sock, None, None))}- /// Used to restrict to_socket_addrs results to the address- /// family specified by the config- fn filter_sock_addr(&self, addr: &std::net::SocketAddr) -> bool {- match self.config.get("addressfamily").map(|s| s.as_str()) {- Some("inet") => addr.is_ipv4(),- Some("inet6") => addr.is_ipv6(),- None | Some("any") | Some(_) => true,- }+ /// An other address answered as some other machine: say so, and the+ /// caller tries the rest.+ fn pass_over(&self, hostname: &str, (host, port): &(String, u16)) -> anyhow::Result<()> {+ log::warn!(+ "{host}:{port} holds a different host key than {hostname}; trying its other addresses"+ );+ self.tx_event+ .try_send(SessionEvent::Banner(Some(format!(+ "{host}:{port} is not {hostname} (its host key differs); trying the others"+ ))))+ .context("notifying user of banner")}fn do_keepalive(&mut self, sess: &mut SessionWrap) -> anyhow::Result<()> {