Skip to content

Commit

Permalink
Use Clone trait instead of Default
Browse files Browse the repository at this point in the history
Signed-off-by: Wiktor Kwapisiewicz <[email protected]>
  • Loading branch information
wiktor-k committed May 6, 2024
1 parent d212998 commit 8c4425d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 85 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
38 changes: 3 additions & 35 deletions examples/key_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -31,6 +31,7 @@ struct Identity {
comment: String,
}

#[derive(Default, Clone)]
struct KeyStorage {
identities: Arc<Mutex<Vec<Identity>>>,
}
Expand Down Expand Up @@ -224,39 +225,6 @@ impl Session for KeyStorage {
}
}

struct KeyStorageAgent {
identities: Arc<Mutex<Vec<Identity>>>,
}

impl KeyStorageAgent {
fn new() -> Self {
Self {
identities: Arc::new(Mutex::new(vec![])),
}
}
}

#[cfg(unix)]
impl Agent<Listener> for KeyStorageAgent {
fn new_session(&mut self, _socket: &tokio::net::UnixStream) -> impl Session {
KeyStorage {
identities: Arc::clone(&self.identities),
}
}
}

#[cfg(windows)]
impl Agent<Listener> 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();
Expand All @@ -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(())
}
48 changes: 5 additions & 43 deletions examples/openpgp-card-agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand All @@ -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<Cache<String, SecretString>>,
}

impl CardAgent {
impl CardSession {
pub fn new() -> Self {
let pwds: Arc<Cache<String, SecretString>> = 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<Listener> for CardAgent {
fn new_session(&mut self, _socket: &tokio::net::UnixStream) -> impl Session {
CardSession {
pwds: Arc::clone(&self.pwds),
}
}
}

#[cfg(unix)]
impl Agent<TcpListener> for CardAgent {
fn new_session(&mut self, _socket: &tokio::net::TcpStream) -> impl Session {
CardSession {
pwds: Arc::clone(&self.pwds),
}
}
}

#[cfg(windows)]
impl Agent<Listener> 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<Cache<String, SecretString>>,
}

impl CardSession {
async fn handle_sign(
&self,
request: SignRequest,
Expand Down Expand Up @@ -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(())
}
12 changes: 6 additions & 6 deletions src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,32 +287,32 @@ where
#[cfg(unix)]
impl<T> Agent<tokio::net::UnixListener> 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<T> Agent<tokio::net::TcpListener> 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<T> Agent<NamedPipeListener> 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)
}
}

Expand Down

0 comments on commit 8c4425d

Please sign in to comment.