From c3472cc969d7e28aa63c4ad7d0d792f7ea40292e Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Mon, 3 Apr 2023 17:04:53 -0700 Subject: [PATCH] ssh: fix token expansion for ProxyCommand refs: https://github.com/wez/wezterm/issues/3437 --- docs/changelog.md | 2 ++ wezterm-ssh/src/config.rs | 40 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/docs/changelog.md b/docs/changelog.md index c1fd302ab..3d5c7dd8a 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -81,6 +81,8 @@ As features stabilize some brief notes about them will accumulate here. * `wezterm connect --workspace WS DOM` didn't use the provided workspace when starting up the mux server. #2734 * mux: `ClearScrollback` was not forwarded to remote server. #2624 +* ssh: `%r` `%h` and `%p` tokens were implicitly supported by libssh but stopped + working in `ProxyCommand` when we took over running the proxy command. #3437 ### 20230326-111934-3666303c diff --git a/wezterm-ssh/src/config.rs b/wezterm-ssh/src/config.rs index cb752040b..0ccba7572 100644 --- a/wezterm-ssh/src/config.rs +++ b/wezterm-ssh/src/config.rs @@ -533,6 +533,15 @@ impl Config { let mut token_map = self.tokens.clone(); token_map.insert("%h".to_string(), host.to_string()); + token_map.insert("%n".to_string(), host.to_string()); + token_map.insert("%r".to_string(), target_user.to_string()); + token_map.insert( + "%p".to_string(), + result + .get("port") + .map(|p| p.to_string()) + .unwrap_or_else(|| "22".to_string()), + ); for (k, v) in &mut result { if let Some(tokens) = self.should_expand_tokens(k) { @@ -736,6 +745,37 @@ mod test { use super::*; use k9::snapshot; + #[test] + fn parse_proxy_command_tokens() { + let mut config = Config::new(); + config.add_config_string( + r#" + Host foo + ProxyCommand /usr/bin/corp-ssh-helper -dst_username=%r %h %p + Port 2222 + "#, + ); + let mut fake_env = ConfigMap::new(); + fake_env.insert("HOME".to_string(), "/home/me".to_string()); + fake_env.insert("USER".to_string(), "me".to_string()); + config.assign_environment(fake_env); + + let opts = config.for_host("foo"); + snapshot!( + opts, + r#" +{ + "hostname": "foo", + "identityfile": "/home/me/.ssh/id_dsa /home/me/.ssh/id_ecdsa /home/me/.ssh/id_ed25519 /home/me/.ssh/id_rsa", + "port": "2222", + "proxycommand": "/usr/bin/corp-ssh-helper -dst_username=me foo 2222", + "user": "me", + "userknownhostsfile": "/home/me/.ssh/known_hosts /home/me/.ssh/known_hosts2", +} +"# + ); + } + #[test] fn parse_proxy_command() { let mut config = Config::new();