diff --git a/README.md b/README.md index 9484c26..f41aa51 100644 --- a/README.md +++ b/README.md @@ -14,9 +14,9 @@ On Unix it uses `ssh-agent.sock` Unix domain socket while on Windows it uses a n ```rust,no_run #[cfg(not(windows))] -use tokio::net::UnixListener; +use tokio::net::UnixListener as Listener; #[cfg(windows)] -use ssh_agent_lib::agent::NamedPipeListener; +use ssh_agent_lib::agent::NamedPipeListener as Listener; use ssh_agent_lib::agent::{Session, Agent}; use ssh_agent_lib::proto::message::Message; @@ -43,19 +43,15 @@ impl Session for MyAgent { } #[tokio::main] -#[cfg(not(windows))] async fn main() -> Result<(), Box> { + #[cfg(not(windows))] let socket = "ssh-agent.sock"; - let _ = std::fs::remove_file(socket); // remove the socket if exists + #[cfg(windows)] + let socket = r"\\.\pipe\agent"; - MyAgent.listen(UnixListener::bind(socket)?).await?; - Ok(()) -} + let _ = std::fs::remove_file(socket); // remove the socket if exists -#[tokio::main] -#[cfg(windows)] -async fn main() -> Result<(), Box> { - MyAgent.listen(NamedPipeListener::new(r"\\.\pipe\agent".into())?).await?; + MyAgent.listen(Listener::bind(socket)?).await?; Ok(()) } ``` diff --git a/examples/key_storage.rs b/examples/key_storage.rs index 25ea65e..b0faba3 100644 --- a/examples/key_storage.rs +++ b/examples/key_storage.rs @@ -1,10 +1,10 @@ use async_trait::async_trait; use log::info; #[cfg(windows)] -use ssh_agent_lib::agent::NamedPipeListener; +use ssh_agent_lib::agent::NamedPipeListener as Listener; use ssh_agent_lib::proto::extension::SessionBind; #[cfg(not(windows))] -use tokio::net::UnixListener; +use tokio::net::UnixListener as Listener; use ssh_agent_lib::agent::{Agent, Session}; use ssh_agent_lib::proto::message::{self, Message, SignRequest}; @@ -221,26 +221,22 @@ impl Agent for KeyStorageAgent { } #[tokio::main] -#[cfg(not(windows))] async fn main() -> Result<(), Box> { env_logger::init(); + + #[cfg(not(windows))] let socket = "ssh-agent.sock"; - let _ = std::fs::remove_file(socket); // remove the socket if exists + #[cfg(windows)] + let socket = r"\\.\pipe\agent"; - KeyStorageAgent::new() - .listen(UnixListener::bind(socket)?) - .await?; - Ok(()) -} + let _ = std::fs::remove_file(socket); // remove the socket if exists -#[tokio::main] -#[cfg(windows)] -async fn main() -> Result<(), Box> { // This is only used for integration tests on Windows: + #[cfg(windows)] std::fs::File::create("server-started")?; - // ^ You can remove this line + KeyStorageAgent::new() - .listen(NamedPipeListener::new(r"\\.\pipe\agent".into())?) + .listen(Listener::bind(socket)?) .await?; Ok(()) } diff --git a/src/agent.rs b/src/agent.rs index 47e4d84..b8a2cd1 100644 --- a/src/agent.rs +++ b/src/agent.rs @@ -92,7 +92,8 @@ pub struct NamedPipeListener(NamedPipeServer, std::ffi::OsString); #[cfg(windows)] impl NamedPipeListener { - pub fn new(pipe: std::ffi::OsString) -> std::io::Result { + pub fn bind(pipe: impl Into) -> std::io::Result { + let pipe = pipe.into(); Ok(NamedPipeListener( ServerOptions::new() .first_pipe_instance(true) @@ -185,7 +186,7 @@ pub trait Agent: 'static + Sync + Send + Sized { } #[cfg(windows)] service_binding::Listener::NamedPipe(pipe) => { - self.listen(NamedPipeListener::new(pipe)?).await + self.listen(NamedPipeListener::bind(pipe)?).await } #[cfg(not(windows))] service_binding::Listener::NamedPipe(_) => Err(AgentError::IO(std::io::Error::other(