use std::path::PathBuf;
use std::time::Duration;
use tracing::debug;
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum HostKeyCheckingMode {
#[default]
Strict,
AcceptNew,
Disable,
}
impl std::fmt::Display for HostKeyCheckingMode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Strict => write!(f, "strict"),
Self::AcceptNew => write!(f, "accept-new"),
Self::Disable => write!(f, "disable"),
}
}
}
#[derive(Debug, Clone)]
pub struct SshOptions {
pub host: String,
pub port: u16,
pub username: String,
pub password: Option<String>,
pub private_key_path: Option<String>,
pub private_key_passphrase: Option<String>,
pub connect_timeout: Duration,
pub read_timeout: Duration,
pub host_key_mode: HostKeyCheckingMode,
pub host_key_fingerprint: Option<String>,
pub compression: bool,
pub keepalive_interval: Option<Duration>,
pub preferred_ciphers: Vec<String>,
}
impl Default for SshOptions {
fn default() -> Self {
Self {
host: "localhost".to_string(),
port: 22,
username: String::new(),
password: None,
private_key_path: None,
private_key_passphrase: None,
connect_timeout: Duration::from_secs(15),
read_timeout: Duration::from_secs(30),
host_key_mode: HostKeyCheckingMode::default(),
host_key_fingerprint: None,
compression: false,
keepalive_interval: Some(Duration::from_secs(60)),
preferred_ciphers: Vec::new(),
}
}
}
impl SshOptions {
pub fn new(host: &str, username: &str) -> Self {
Self {
host: host.to_string(),
port: 22,
username: username.to_string(),
..Default::default()
}
}
pub fn with_port(mut self, port: u16) -> Self {
self.port = port;
self
}
pub fn with_password(mut self, password: &str) -> Self {
self.password = Some(password.to_string());
self
}
pub fn with_private_key(mut self, path: &str) -> Self {
self.private_key_path = Some(path.to_string());
self
}
pub fn with_passphrase(mut self, passphrase: &str) -> Self {
self.private_key_passphrase = Some(passphrase.to_string());
self
}
pub fn with_host_key_fingerprint(mut self, fingerprint: impl Into<String>) -> Self {
self.host_key_fingerprint = Some(fingerprint.into());
self
}
pub fn with_host_key_mode(mut self, mode: HostKeyCheckingMode) -> Self {
self.host_key_mode = mode;
self
}
pub fn with_timeouts(mut self, connect: Duration, read: Duration) -> Self {
self.connect_timeout = connect;
self.read_timeout = read;
self
}
pub fn with_compression(mut self, enabled: bool) -> Self {
self.compression = enabled;
self
}
pub fn target(&self) -> String {
format!("{}@{}:{}", self.username, self.host, self.port)
}
pub fn has_auth_credentials(&self) -> bool {
self.password.is_some() || self.private_key_path.is_some()
}
pub fn resolve_key_path(&self) -> Option<PathBuf> {
if let Some(ref path) = self.private_key_path {
return Some(PathBuf::from(path));
}
if self.password.is_none() {
let home = dirs::home_dir();
if let Some(home) = home {
for key_name in &["id_ed25519", "id_rsa", "id_ecdsa"] {
let candidate = home.join(".ssh").join(key_name);
if candidate.exists() {
debug!("auto-detected SSH key: {}", candidate.display());
return Some(candidate);
}
}
}
}
None
}
}