diff --git a/README.md b/README.md index fcc26cc..0d618e1 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ use ssh_agent_lib::agent::{Session, listen}; use ssh_agent_lib::proto::{Identity, SignRequest}; use ssh_key::{Algorithm, Signature}; -#[derive(Default)] +#[derive(Default, Clone)] struct MyAgent; #[ssh_agent_lib::async_trait] diff --git a/examples/key_storage.rs b/examples/key_storage.rs index 3aebfcb..44b7650 100644 --- a/examples/key_storage.rs +++ b/examples/key_storage.rs @@ -9,7 +9,7 @@ use rsa::BigUint; use sha1::Sha1; #[cfg(windows)] use ssh_agent_lib::agent::NamedPipeListener as Listener; -use ssh_agent_lib::agent::{listen, Agent, Session}; +use ssh_agent_lib::agent::{listen, Session}; use ssh_agent_lib::error::AgentError; use ssh_agent_lib::proto::extension::{QueryResponse, RestrictDestination, SessionBind}; use ssh_agent_lib::proto::{ @@ -31,6 +31,7 @@ struct Identity { comment: String, } +#[derive(Default, Clone)] struct KeyStorage { identities: Arc>>, } @@ -224,39 +225,6 @@ impl Session for KeyStorage { } } -struct KeyStorageAgent { - identities: Arc>>, -} - -impl KeyStorageAgent { - fn new() -> Self { - Self { - identities: Arc::new(Mutex::new(vec![])), - } - } -} - -#[cfg(unix)] -impl Agent for KeyStorageAgent { - fn new_session(&mut self, _socket: &tokio::net::UnixStream) -> impl Session { - KeyStorage { - identities: Arc::clone(&self.identities), - } - } -} - -#[cfg(windows)] -impl Agent for KeyStorageAgent { - fn new_session( - &mut self, - _socket: &tokio::net::windows::named_pipe::NamedPipeServer, - ) -> impl Session { - KeyStorage { - identities: Arc::clone(&self.identities), - } - } -} - #[tokio::main] async fn main() -> Result<(), AgentError> { env_logger::init(); @@ -272,6 +240,6 @@ async fn main() -> Result<(), AgentError> { #[cfg(windows)] std::fs::File::create("server-started")?; - listen(Listener::bind(socket)?, KeyStorageAgent::new()).await?; + listen(Listener::bind(socket)?, KeyStorage::default()).await?; Ok(()) } diff --git a/examples/openpgp-card-agent.rs b/examples/openpgp-card-agent.rs index ba27283..16e7117 100644 --- a/examples/openpgp-card-agent.rs +++ b/examples/openpgp-card-agent.rs @@ -29,7 +29,7 @@ use service_binding::Binding; #[cfg(windows)] use ssh_agent_lib::agent::NamedPipeListener as Listener; use ssh_agent_lib::{ - agent::{bind, Agent, Session}, + agent::{bind, Session}, error::AgentError, proto::{AddSmartcardKeyConstrained, Identity, KeyConstraint, SignRequest, SmartcardKey}, }; @@ -38,58 +38,20 @@ use ssh_key::{ Algorithm, Signature, }; use testresult::TestResult; -use tokio::net::TcpListener; -#[cfg(not(windows))] -use tokio::net::UnixListener as Listener; -struct CardAgent { +#[derive(Clone)] +struct CardSession { pwds: Arc>, } -impl CardAgent { +impl CardSession { pub fn new() -> Self { let pwds: Arc> = Arc::new(Default::default()); let clone = Arc::clone(&pwds); tokio::spawn(async move { clone.monitor(4, 0.25, Duration::from_secs(3)).await }); Self { pwds } } -} - -#[cfg(unix)] -impl Agent for CardAgent { - fn new_session(&mut self, _socket: &tokio::net::UnixStream) -> impl Session { - CardSession { - pwds: Arc::clone(&self.pwds), - } - } -} - -#[cfg(unix)] -impl Agent for CardAgent { - fn new_session(&mut self, _socket: &tokio::net::TcpStream) -> impl Session { - CardSession { - pwds: Arc::clone(&self.pwds), - } - } -} - -#[cfg(windows)] -impl Agent for CardAgent { - fn new_session( - &mut self, - _socket: &tokio::net::windows::named_pipe::NamedPipeServer, - ) -> impl Session { - CardSession { - pwds: Arc::clone(&self.pwds), - } - } -} -struct CardSession { - pwds: Arc>, -} - -impl CardSession { async fn handle_sign( &self, request: SignRequest, @@ -227,6 +189,6 @@ async fn main() -> TestResult { env_logger::init(); let args = Args::parse(); - bind(args.host.try_into()?, CardAgent::new()).await?; + bind(args.host.try_into()?, CardSession::new()).await?; Ok(()) } diff --git a/src/agent.rs b/src/agent.rs index 61a5ecd..6e2261a 100644 --- a/src/agent.rs +++ b/src/agent.rs @@ -287,32 +287,32 @@ where #[cfg(unix)] impl Agent for T where - T: Default + Send + Sync + Session, + T: Clone + Send + Sync + Session, { fn new_session(&mut self, _socket: &tokio::net::UnixStream) -> impl Session { - Self::default() + Self::clone(self) } } impl Agent for T where - T: Default + Send + Sync + Session, + T: Clone + Send + Sync + Session, { fn new_session(&mut self, _socket: &tokio::net::TcpStream) -> impl Session { - Self::default() + Self::clone(self) } } #[cfg(windows)] impl Agent for T where - T: Default + Send + Sync + Session, + T: Clone + Send + Sync + Session, { fn new_session( &mut self, _socket: &tokio::net::windows::named_pipe::NamedPipeServer, ) -> impl Session { - Self::default() + Self::clone(self) } }