Skip to content

Commit

Permalink
Merge pull request #29 from wiktor-k/use-bind
Browse files Browse the repository at this point in the history
Rename `NamedPipeListener::new` to `bind` for consistency
  • Loading branch information
wiktor-k authored Apr 5, 2024
2 parents d605c3b + d287f17 commit 323f787
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 27 deletions.
18 changes: 7 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -43,19 +43,15 @@ impl Session for MyAgent {
}
#[tokio::main]
#[cfg(not(windows))]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
#[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<dyn std::error::Error>> {
MyAgent.listen(NamedPipeListener::new(r"\\.\pipe\agent".into())?).await?;
MyAgent.listen(Listener::bind(socket)?).await?;
Ok(())
}
```
Expand Down
24 changes: 10 additions & 14 deletions examples/key_storage.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -221,26 +221,22 @@ impl Agent for KeyStorageAgent {
}

#[tokio::main]
#[cfg(not(windows))]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
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<dyn std::error::Error>> {
// 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(())
}
5 changes: 3 additions & 2 deletions src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
pub fn bind(pipe: impl Into<std::ffi::OsString>) -> std::io::Result<Self> {
let pipe = pipe.into();
Ok(NamedPipeListener(
ServerOptions::new()
.first_pipe_instance(true)
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 323f787

Please sign in to comment.