From c2fb3cb760adbd3739b2571d862195073f863adc Mon Sep 17 00:00:00 2001 From: emc2314 Date: Mon, 6 May 2024 04:43:52 +0800 Subject: [PATCH] Fix SSH config %h using Host (#5163) * Fix SSH config %h using Host * Fix test and use workaround for 2nd pass parsing * Better implementation from @wheatdog --- wezterm-ssh/src/config.rs | 46 +++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/wezterm-ssh/src/config.rs b/wezterm-ssh/src/config.rs index 0ccba7572..b96c10237 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()); + result + .entry("hostname".to_string()) + .and_modify(|curr| { + if let Some(tokens) = self.should_expand_tokens("hostname") { + self.expand_tokens(curr, tokens, &token_map); + } + }) + .or_insert_with(|| host.to_string()); + token_map.insert("%h".to_string(), result["hostname"].to_string()); token_map.insert("%n".to_string(), host.to_string()); token_map.insert("%r".to_string(), target_user.to_string()); token_map.insert( @@ -553,10 +562,6 @@ impl Config { } } - result - .entry("hostname".to_string()) - .or_insert_with(|| host.to_string()); - result .entry("port".to_string()) .or_insert_with(|| "22".to_string()); @@ -960,6 +965,39 @@ Config { ); } + #[test] + fn parse_proxy_command_hostname_expansion() { + let mut config = Config::new(); + + 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); + + config.add_config_string( + r#" + Host foo + HostName server-%h + ProxyCommand nc -x localhost:1080 %h %p + "#, + ); + + let opts = config.for_host("foo"); + snapshot!( + opts, + r#" +{ + "hostname": "server-foo", + "identityfile": "/home/me/.ssh/id_dsa /home/me/.ssh/id_ecdsa /home/me/.ssh/id_ed25519 /home/me/.ssh/id_rsa", + "port": "22", + "proxycommand": "nc -x localhost:1080 server-foo 22", + "user": "me", + "userknownhostsfile": "/home/me/.ssh/known_hosts /home/me/.ssh/known_hosts2", +} +"# + ); + } + #[test] fn multiple_identityfile() { let mut config = Config::new();