summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs68
-rw-r--r--src/main.rs12
-rw-r--r--src/ssh.rs45
-rw-r--r--src/tmux.rs4
4 files changed, 105 insertions, 24 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 8d33eac..4bb2310 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,6 +1,11 @@
-use std::{collections::HashMap, fmt::Display, iter::IntoIterator};
+use std::{
+ collections::{hash_map::Entry, HashMap},
+ fmt::Display,
+ iter::IntoIterator,
+ time::Duration,
+};
-use serde::Deserialize;
+use serde::{Deserialize, Serialize};
pub use config::Config;
pub use history::History;
@@ -8,10 +13,9 @@ pub use tmux::Tmux;
mod config;
mod history;
+mod ssh;
mod tmux;
-pub type Timestamp = Option<u64>;
-
pub trait SessionSource {
type Error: std::error::Error;
@@ -21,28 +25,60 @@ pub trait SessionSource {
fn update(
&self,
- mut hash_map: HashMap<String, Timestamp>,
- ) -> Result<HashMap<String, Timestamp>, Self::Error> {
+ mut hash_map: HashMap<String, Session>,
+ ) -> Result<HashMap<String, Session>, Self::Error> {
for session in self.sessions()? {
- hash_map
- .entry(session.name)
- .and_modify(|o| {
- if let Some(timestamp) = session.timestamp {
- *o = o.map(|t| timestamp.max(t));
- }
- })
- .or_insert(session.timestamp);
+ match hash_map.entry(session.name.to_owned()) {
+ Entry::Occupied(mut o) if &session > o.get() => {
+ o.insert(session);
+ }
+ Entry::Vacant(v) => {
+ v.insert(session);
+ }
+ _ => {}
+ }
}
Ok(hash_map)
}
}
-#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Deserialize)]
+#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
+pub enum State {
+ Discovered,
+ Connected,
+ Updated,
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Session {
- pub timestamp: Timestamp,
+ pub state: State,
+
+ #[serde(with = "epoch_timestamp")]
+ pub timestamp: Duration,
+
pub name: String,
}
+mod epoch_timestamp {
+ use std::time::Duration;
+
+ use serde::{self, Deserialize, Deserializer, Serializer};
+
+ pub fn serialize<S>(duration: &Duration, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ serializer.serialize_u64(duration.as_secs())
+ }
+
+ pub fn deserialize<'de, D>(d: D) -> Result<Duration, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ u64::deserialize(d).map(Duration::from_secs)
+ }
+}
+
impl Display for Session {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name)
diff --git a/src/main.rs b/src/main.rs
index 7534bb1..2357ab2 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -2,7 +2,7 @@ use std::collections::HashMap;
use clap::Parser;
-use sshr::{Config, History, Session, SessionSource, Timestamp, Tmux};
+use sshr::{Config, History, Session, SessionSource, Tmux};
fn main() -> anyhow::Result<()> {
let config = Config::parse();
@@ -20,7 +20,10 @@ fn main() -> anyhow::Result<()> {
}
fn list_sessions(config: Config) -> anyhow::Result<()> {
- let mut sessions: HashMap<String, Timestamp> = HashMap::new();
+ let mut sessions = HashMap::new();
+
+ let ssh = ssh2::Session::new()?;
+ sessions = ssh.update(sessions)?;
let tmux = Tmux::new(config.socket_name);
sessions = tmux.update(sessions)?;
@@ -31,10 +34,7 @@ fn list_sessions(config: Config) -> anyhow::Result<()> {
}
}
- let mut sessions: Vec<Session> = sessions
- .into_iter()
- .map(|(name, timestamp)| Session { name, timestamp })
- .collect();
+ let mut sessions: Vec<Session> = sessions.into_values().collect();
sessions.sort();
diff --git a/src/ssh.rs b/src/ssh.rs
new file mode 100644
index 0000000..29c6de3
--- /dev/null
+++ b/src/ssh.rs
@@ -0,0 +1,45 @@
+use std::time::{SystemTime, UNIX_EPOCH};
+
+use directories::UserDirs;
+use ssh2::KnownHostFileKind;
+
+use crate::{Session, SessionSource, State};
+
+impl SessionSource for ssh2::Session {
+ type Error = ssh2::Error;
+
+ type Iter = Vec<Session>;
+
+ fn sessions(&self) -> Result<Self::Iter, Self::Error> {
+ let timestamp = SystemTime::now()
+ .duration_since(UNIX_EPOCH)
+ .expect("Current time is pre-epoch. (Time traveler?)");
+
+ let mut known_hosts = self.known_hosts()?;
+
+ let file = UserDirs::new()
+ .ok_or_else(ssh2::Error::unknown)?
+ .home_dir()
+ .join(".ssh/known_hosts");
+
+ known_hosts.read_file(&file, KnownHostFileKind::OpenSSH)?;
+
+ let sessions = known_hosts
+ .hosts()?
+ .into_iter()
+ .filter_map(|h| match h.name() {
+ Some(name) => Some(Session {
+ state: State::Discovered,
+ timestamp,
+ name: name.to_owned(),
+ }),
+ None => {
+ tracing::warn!("Invalid host: No plain text host name exists");
+ None
+ }
+ })
+ .collect();
+
+ Ok(sessions)
+ }
+}
diff --git a/src/tmux.rs b/src/tmux.rs
index c6e83b1..6c53831 100644
--- a/src/tmux.rs
+++ b/src/tmux.rs
@@ -12,7 +12,7 @@ pub struct Tmux {
}
impl Tmux {
- const SESSION_FORMAT: &str = r##"Session( name: "#S", timestamp: Some(#{?session_last_attached,#{session_last_attached},#{session_created}}))"##;
+ const SESSION_FORMAT: &str = r##"Session(name: "#S", #{?session_last_attached,timestamp: #{session_last_attached}#, state: Updated,timestamp: #{session_created}#, state: Connected})"##;
pub fn new(socket_name: String) -> Self {
Self { socket_name }
@@ -39,7 +39,7 @@ impl SessionSource for Tmux {
.filter_map(|s| match ron::from_str(s) {
Ok(session) => Some(session),
Err(err) => {
- tracing::warn!(?err, "Invalid session format");
+ tracing::warn!(%err, "Invalid session format");
None
}
})