use std::ffi::OsStr;
use std::path::Path;
use std::process::{Command, Stdio};
pub(crate) struct Ssh(Command);
const THROUGH_OPTIONS: [&str; 4] = ["-o", "BatchMode=yes", "-o", "ConnectTimeout=10"];
const KEEPALIVE_OPTIONS: [&str; 4] = [
"-o",
"ServerAliveInterval=10",
"-o",
"ServerAliveCountMax=3",
];
const BULK_MASTER_OPTIONS: [&str; 8] = [
"-o",
"BatchMode=yes",
"-o",
"IPQoS=throughput",
"-o",
"ConnectTimeout=10",
"-o",
"ServerAliveInterval=15",
];
pub(crate) const DIALOG_MASTER_OPTIONS: [&str; 6] = [
"-o",
"BatchMode=no",
"-o",
"ConnectTimeout=10",
"-o",
"ServerAliveInterval=15",
];
const FRESH_CHECK_OPTIONS: [&str; 8] = [
"-o",
"BatchMode=yes",
"-o",
"ControlMaster=no",
"-o",
"ControlPath=none",
"-o",
"ConnectTimeout=5",
];
impl Ssh {
fn new() -> Self {
Self(crate::proc::command("ssh"))
}
pub(crate) fn bare_client(socket: &Path) -> Self {
let mut ssh = Self::new();
ssh.0
.arg("-S")
.arg(socket)
.arg("-o")
.arg("ControlMaster=no")
.args(THROUGH_OPTIONS);
ssh
}
pub(crate) fn client(socket: &Path) -> Self {
let mut ssh = Self::bare_client(socket);
ssh.0.args(KEEPALIVE_OPTIONS);
ssh
}
pub(crate) fn no_stdin(mut self) -> Self {
self.0.arg("-n").stdin(Stdio::null());
self
}
pub(crate) fn quiet(mut self) -> Self {
self.0
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null());
self
}
pub(crate) fn background(self) -> Self {
self.no_stdin().quiet()
}
pub(crate) fn control(socket: &Path, op: &str) -> Self {
let mut ssh = Self::new();
ssh.0.arg("-S").arg(socket).arg("-O").arg(op);
ssh
}
fn master(socket: &Path) -> Self {
let mut ssh = Self::new();
ssh.0
.arg("-M")
.arg("-S")
.arg(socket)
.args(["-f", "-N", "-T"]);
ssh
}
pub(crate) fn session_master(socket: &Path) -> Self {
let mut ssh = Self::master(socket);
ssh.0
.args(["-o", "ConnectTimeout=10"])
.args(KEEPALIVE_OPTIONS);
ssh
}
pub(crate) fn bulk_master(socket: &Path) -> Self {
let mut ssh = Self::master(socket);
ssh.0.args(BULK_MASTER_OPTIONS);
ssh
}
pub(crate) fn fresh_check(host: &str) -> Self {
let mut ssh = Self::new();
ssh.0.args(FRESH_CHECK_OPTIONS).arg(host).arg("true");
ssh
}
pub(crate) fn direct(host: &str) -> Self {
let mut ssh = Self::new();
ssh.0.arg("--").arg(host);
ssh
}
pub(crate) fn arg(mut self, arg: impl AsRef<OsStr>) -> Self {
self.0.arg(arg);
self
}
pub(crate) fn into_command(self) -> Command {
self.0
}
}
pub(crate) fn rsync_transport(socket: &Path) -> String {
format!(
"ssh -S {} -o ControlMaster=no {}",
super::shell_quote_for_e_arg(socket),
THROUGH_OPTIONS.join(" "),
)
}
pub(crate) fn dialog_master_args(socket: &Path, host: &str) -> Vec<std::ffi::OsString> {
let mut args: Vec<std::ffi::OsString> = vec!["-M".into(), "-S".into(), socket.into()];
args.extend(["-N", "-T"].map(Into::into));
args.extend(DIALOG_MASTER_OPTIONS.map(Into::into));
args.push(host.into());
args
}
#[cfg(test)]
mod tests {
use super::*;
fn args(ssh: Ssh) -> Vec<String> {
ssh.into_command()
.get_args()
.map(|a| a.to_string_lossy().into_owned())
.collect()
}
#[test]
fn a_client_through_the_connection_never_asks_for_a_login() {
let socket = Path::new("/tmp/croft-ssh/ctl");
for client in [
args(Ssh::client(socket)),
args(Ssh::bare_client(socket)),
args(Ssh::client(socket).background()),
] {
let joined = client.join(" ");
assert!(joined.contains("-o BatchMode=yes"), "{joined}");
assert!(joined.contains("-o ConnectTimeout=10"), "{joined}");
}
let rsync = rsync_transport(socket);
assert!(rsync.contains("-o BatchMode=yes"), "{rsync}");
assert!(rsync.contains("-o ConnectTimeout=10"), "{rsync}");
}
#[test]
fn the_session_master_may_prompt() {
let master = args(Ssh::session_master(Path::new("/tmp/croft-ssh/ctl"))).join(" ");
assert!(!master.contains("BatchMode"), "{master}");
assert!(master.contains("-o ConnectTimeout=10"), "{master}");
}
}