Skip to content

Commit cf3a671

Browse files
authored
Merge pull request #91 from openssh-rust/feature/jump-host
Impl `SessionBuilder::jump_hosts`
2 parents e343cf4 + 3ec0e2b commit cf3a671

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/builder.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use super::{Error, Session};
33
use std::borrow::Cow;
44
use std::ffi::OsString;
55
use std::fs;
6+
use std::iter::IntoIterator;
67
use std::path::{Path, PathBuf};
78
use std::process::Stdio;
89
use std::str;
@@ -45,6 +46,7 @@ pub struct SessionBuilder {
4546
control_dir: Option<PathBuf>,
4647
config_file: Option<PathBuf>,
4748
compression: Option<bool>,
49+
jump_hosts: Vec<Box<str>>,
4850
user_known_hosts_file: Option<Box<Path>>,
4951
}
5052

@@ -60,6 +62,7 @@ impl Default for SessionBuilder {
6062
control_dir: None,
6163
config_file: None,
6264
compression: None,
65+
jump_hosts: Vec::new(),
6366
user_known_hosts_file: None,
6467
}
6568
}
@@ -153,6 +156,27 @@ impl SessionBuilder {
153156
self
154157
}
155158

159+
/// Specify one or multiple jump hosts.
160+
///
161+
/// Connect to the target host by first making a ssh connection to the
162+
/// jump host described by destination and then establishing a TCP
163+
/// forwarding to the ultimate destination from there.
164+
///
165+
/// Multiple jump hops may be specified.
166+
/// This is a shortcut to specify a ProxyJump configuration directive.
167+
///
168+
/// Note that configuration directives specified by [`SessionBuilder`]
169+
/// do not apply to the jump hosts.
170+
///
171+
/// Use ~/.ssh/config to specify configuration for jump hosts.
172+
pub fn jump_hosts<T: AsRef<str>>(&mut self, hosts: impl IntoIterator<Item = T>) -> &mut Self {
173+
self.jump_hosts = hosts
174+
.into_iter()
175+
.map(|s| s.as_ref().to_string().into_boxed_str())
176+
.collect();
177+
self
178+
}
179+
156180
/// Specify the path to the `known_hosts` file.
157181
///
158182
/// The path provided may use tilde notation (`~`) to refer to the user's
@@ -317,6 +341,20 @@ impl SessionBuilder {
317341
init.arg("-o").arg(format!("Compression={}", arg));
318342
}
319343

344+
let mut it = self.jump_hosts.iter();
345+
346+
if let Some(jump_host) = it.next() {
347+
let s = jump_host.to_string();
348+
349+
let dest = it.fold(s, |mut s, jump_host| {
350+
s.push(',');
351+
s.push_str(jump_host);
352+
s
353+
});
354+
355+
init.arg("-J").arg(&dest);
356+
}
357+
320358
if let Some(user_known_hosts_file) = &self.user_known_hosts_file {
321359
let mut option: OsString = "UserKnownHostsFile=".into();
322360
option.push(&**user_known_hosts_file);

src/changelog.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
use crate::*;
33

44
/// TODO: RENAME THIS INTO THE NEXT VERSION BEFORE RELEASE
5+
///
6+
/// ## Added
7+
/// - [`SessionBuilder::jump_hosts`]
58
#[doc(hidden)]
69
pub mod unreleased {}
710

0 commit comments

Comments
 (0)