use super::*;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RemoteTarget {
pub alias: String,
pub host_name: Option<String>,
pub user: Option<String>,
}
impl RemoteTarget {
pub fn detail(&self) -> String {
match (&self.user, &self.host_name) {
(Some(user), Some(host)) => format!("{user}@{host}"),
(Some(user), None) => format!("{user}@{}", self.alias),
(None, Some(host)) if host != &self.alias => host.clone(),
_ => String::new(),
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct SshConfigState {
entries: Vec<SshConfigEntryState>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) struct SshConfigEntryState {
path: PathBuf,
modified: Option<std::time::SystemTime>,
len: Option<u64>,
}
pub fn ssh_config_state() -> SshConfigState {
SshConfigState {
entries: ssh_config_paths()
.into_iter()
.map(|path| {
let meta = std::fs::metadata(&path).ok();
SshConfigEntryState {
path,
modified: meta.as_ref().and_then(|m| m.modified().ok()),
len: meta.map(|m| m.len()),
}
})
.collect(),
}
}
#[derive(Default)]
pub(super) struct HostBlock {
aliases: Vec<String>,
host_name: Option<String>,
user: Option<String>,
}
pub(super) const SSH_PROGRAMS: [&str; 1] = ["ssh"];
pub(super) const SSH_FLAGS_WITH_ARG: [char; 22] = [
'B', 'b', 'c', 'D', 'E', 'e', 'F', 'I', 'i', 'J', 'L', 'l', 'm', 'O', 'o', 'P', 'p', 'Q', 'R',
'S', 'W', 'w',
];
pub fn ssh_destination(cmd: &[&str]) -> Option<String> {
let program = cmd.first()?.rsplit('/').next()?;
if !SSH_PROGRAMS.contains(&program) {
return None;
}
let takes_arg = |c: char| SSH_FLAGS_WITH_ARG.contains(&c);
let mut rest = cmd[1..].iter();
while let Some(word) = rest.next() {
if *word == "--" {
return rest
.next()
.and_then(|w| (!w.is_empty()).then(|| (*w).to_string()));
}
let Some(flags) = word.strip_prefix('-') else {
return (!word.is_empty()).then(|| (*word).to_string());
};
if flags.is_empty() {
continue;
}
for (i, c) in flags.char_indices() {
if takes_arg(c) {
if i + c.len_utf8() == flags.len() {
rest.next();
}
break;
}
}
}
None
}
pub fn discover_ssh_targets() -> Vec<RemoteTarget> {
ssh_config_paths()
.into_iter()
.filter_map(|path| std::fs::read_to_string(path).ok())
.flat_map(|content| parse_ssh_config(&content))
.fold(
(BTreeSet::new(), Vec::new()),
|(mut seen, mut targets), target| {
if seen.insert(target.alias.clone()) {
targets.push(target);
}
(seen, targets)
},
)
.1
}
pub(super) fn ssh_config_paths() -> Vec<PathBuf> {
let mut paths = Vec::new();
if let Some(home) = std::env::var_os("HOME") {
paths.push(PathBuf::from(home).join(".ssh/config"));
}
paths
}
pub fn primary_ssh_config_path() -> Option<PathBuf> {
ssh_config_paths().into_iter().next()
}
pub fn parse_ssh_config(input: &str) -> Vec<RemoteTarget> {
let mut targets = Vec::new();
let mut block = HostBlock::default();
for raw in input.lines() {
let line = strip_comment(raw).trim();
if line.is_empty() {
continue;
}
let mut parts = line.split_whitespace();
let Some(key) = parts.next() else {
continue;
};
let key = key.to_ascii_lowercase();
if key == "host" {
flush_block(&mut targets, &mut block);
block.aliases = parts
.filter(|alias| is_explicit_host_alias(alias))
.map(ToString::to_string)
.collect();
continue;
}
if block.aliases.is_empty() {
continue;
}
match key.as_str() {
"hostname" => block.host_name = parts.next().map(ToString::to_string),
"user" => block.user = parts.next().map(ToString::to_string),
_ => {}
}
}
flush_block(&mut targets, &mut block);
targets
}
pub(super) fn flush_block(targets: &mut Vec<RemoteTarget>, block: &mut HostBlock) {
for alias in block.aliases.drain(..) {
targets.push(RemoteTarget {
alias,
host_name: block.host_name.clone(),
user: block.user.clone(),
});
}
block.host_name = None;
block.user = None;
}
pub(super) fn strip_comment(line: &str) -> &str {
line.split_once('#').map(|(head, _)| head).unwrap_or(line)
}
pub(super) fn is_explicit_host_alias(alias: &str) -> bool {
!alias.starts_with('!') && !alias.contains('*') && !alias.contains('?')
}