|
| 1 | +//! This example illustrates a couple of features: First, it |
| 2 | +//! implements a forwarder, exposing an SSH agent socket and |
| 3 | +//! forwarding to a different one. Secondly it shows how to work with |
| 4 | +//! low-level handling of messages instead of parsed high-level |
| 5 | +//! structures. |
| 6 | +//! |
| 7 | +//! Run with |
| 8 | +//! RUST_LOG=info cargo run --example proto-dumper -- --target unix://$SSH_AUTH_SOCK -H unix:///tmp/test.sock |
| 9 | +
|
| 10 | +use clap::Parser; |
| 11 | +use service_binding::Binding; |
| 12 | +use ssh_agent_lib::{ |
| 13 | + agent::Agent, |
| 14 | + agent::Session, |
| 15 | + async_trait, |
| 16 | + client::connect, |
| 17 | + error::AgentError, |
| 18 | + proto::{Request, Response}, |
| 19 | +}; |
| 20 | +use ssh_encoding::Encode; |
| 21 | + |
| 22 | +struct DumpAndForward { |
| 23 | + target: Box<dyn Session>, |
| 24 | + session: u64, |
| 25 | + id: u64, |
| 26 | +} |
| 27 | + |
| 28 | +#[async_trait] |
| 29 | +impl Session for DumpAndForward { |
| 30 | + async fn handle(&mut self, message: Request) -> Result<Response, AgentError> { |
| 31 | + use std::io::Write; |
| 32 | + |
| 33 | + self.id += 1; |
| 34 | + let req_file = format!("req-{}-{}.bin", self.session, self.id); |
| 35 | + log::info!("Writing request {message:?} to {req_file}"); |
| 36 | + |
| 37 | + let mut req = std::fs::File::create(req_file)?; |
| 38 | + let mut buf = vec![]; |
| 39 | + message.encode(&mut buf).map_err(AgentError::other)?; |
| 40 | + req.write_all(&buf)?; |
| 41 | + drop(req); |
| 42 | + |
| 43 | + let response = self.target.handle(message).await?; |
| 44 | + |
| 45 | + let resp_file = format!("resp-{}-{}.bin", self.session, self.id); |
| 46 | + log::info!("Writing response {response:?} to {resp_file}"); |
| 47 | + let mut resp = std::fs::File::create(resp_file)?; |
| 48 | + let mut buf = vec![]; |
| 49 | + response.encode(&mut buf).map_err(AgentError::other)?; |
| 50 | + resp.write_all(&buf)?; |
| 51 | + drop(resp); |
| 52 | + |
| 53 | + Ok(response) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +struct Forwarder { |
| 58 | + target: Binding, |
| 59 | + id: u64, |
| 60 | +} |
| 61 | + |
| 62 | +impl Agent for Forwarder { |
| 63 | + fn new_session(&mut self) -> impl Session { |
| 64 | + self.id += 1; |
| 65 | + DumpAndForward { |
| 66 | + target: connect(self.target.clone().try_into().unwrap()).unwrap(), |
| 67 | + session: self.id, |
| 68 | + id: 0, |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +#[derive(Debug, Parser)] |
| 74 | +struct Args { |
| 75 | + /// Target SSH agent to which we will proxy all requests. |
| 76 | + #[clap(long)] |
| 77 | + target: Binding, |
| 78 | + |
| 79 | + /// Source that we will bind to. |
| 80 | + #[clap(long, short = 'H')] |
| 81 | + host: Binding, |
| 82 | +} |
| 83 | + |
| 84 | +#[tokio::main] |
| 85 | +async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 86 | + env_logger::init(); |
| 87 | + |
| 88 | + let args = Args::parse(); |
| 89 | + |
| 90 | + Forwarder { |
| 91 | + target: args.target, |
| 92 | + id: 0, |
| 93 | + } |
| 94 | + .bind(args.host.try_into()?) |
| 95 | + .await?; |
| 96 | + |
| 97 | + Ok(()) |
| 98 | +} |
0 commit comments