From 890feee6d1cac3f43de15496ce99f9645672ba30 Mon Sep 17 00:00:00 2001 From: Noratrieb <48135649+Noratrieb@users.noreply.github.com> Date: Mon, 12 Aug 2024 16:51:13 +0200 Subject: [PATCH] improvements --- src/main.rs | 10 +++++----- ssh-connection/src/lib.rs | 32 +++++++++++++++++++------------- ssh-protocol/src/lib.rs | 8 ++++---- ssh-transport/src/lib.rs | 10 +++++----- 4 files changed, 33 insertions(+), 27 deletions(-) diff --git a/src/main.rs b/src/main.rs index 47e6c0b..e4ca9d3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,13 +33,13 @@ async fn main() -> eyre::Result<()> { .parse::() .wrap_err_with(|| format!("failed to parse listen addr '{addr}'"))?; - info!(?addr, "Starting server"); + info!(%addr, "Starting server"); let listener = TcpListener::bind(addr).await.wrap_err("binding listener")?; loop { let next = listener.accept().await?; - let span = info_span!("connection", addr = ?next.1); + let span = info_span!("connection", addr = %next.1); tokio::spawn( async { let mut total_sent_data = Vec::new(); @@ -48,7 +48,7 @@ async fn main() -> eyre::Result<()> { error!(?err, "error handling connection"); } - info!(stdin = ?String::from_utf8_lossy(&total_sent_data), "Finished connection"); + info!(stdin = %String::from_utf8_lossy(&total_sent_data), "Finished connection"); } .instrument(span), ); @@ -61,7 +61,7 @@ async fn handle_connection( ) -> Result<()> { let (mut conn, addr) = next; - info!(?addr, "Received a new connection"); + info!(%addr, "Received a new connection"); //let rng = vec![ // 0x14, 0xa2, 0x04, 0xa5, 0x4b, 0x2f, 0x5f, 0xa7, 0xff, 0x53, 0x13, 0x67, 0x57, 0x67, 0xbc, @@ -168,7 +168,7 @@ async fn handle_connection( } if is_eof { - debug!(channel = ?update.number, "Received EOF, closing channel"); + debug!(channel = %update.number, "Received EOF, closing channel"); state.do_operation(update.number.construct_op(ChannelOperationKind::Close)); } diff --git a/ssh-connection/src/lib.rs b/ssh-connection/src/lib.rs index aa14ed7..4326eb4 100644 --- a/ssh-connection/src/lib.rs +++ b/ssh-connection/src/lib.rs @@ -9,6 +9,12 @@ use ssh_transport::Result; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct ChannelNumber(pub u32); +impl std::fmt::Display for ChannelNumber { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + std::fmt::Display::fmt(&self.0, f) + } +} + pub struct ServerChannelsState { packets_to_send: VecDeque, channel_updates: VecDeque, @@ -115,7 +121,7 @@ impl ServerChannelsState { Packet::SSH_MSG_GLOBAL_REQUEST => { let request_name = packet.utf8_string()?; let want_reply = packet.bool()?; - debug!(?request_name, ?want_reply, "Received global request"); + debug!(%request_name, %want_reply, "Received global request"); self.packets_to_send .push_back(Packet::new_msg_request_failure()); @@ -127,7 +133,7 @@ impl ServerChannelsState { let initial_window_size = packet.u32()?; let max_packet_size = packet.u32()?; - debug!(?channel_type, ?sender_channel, "Opening channel"); + debug!(%channel_type, %sender_channel, "Opening channel"); let update_message = match channel_type { "session" => ChannelOpen::Session, @@ -170,7 +176,7 @@ impl ServerChannelsState { kind: ChannelUpdateKind::Open(update_message), }); - debug!(?channel_type, ?our_number, "Successfully opened channel"); + debug!(%channel_type, %our_number, "Successfully opened channel"); } Packet::SSH_MSG_CHANNEL_DATA => { let our_channel = packet.u32()?; @@ -219,7 +225,7 @@ impl ServerChannelsState { let request_type = packet.utf8_string()?; let want_reply = packet.bool()?; - debug!(?our_channel, ?request_type, "Got channel request"); + debug!(%our_channel, %request_type, "Got channel request"); let channel = self.channel(our_channel)?; let peer_channel = channel.peer_channel; @@ -234,10 +240,10 @@ impl ServerChannelsState { let term_modes = packet.string()?; debug!( - ?our_channel, - ?term, - ?width_chars, - ?height_rows, + %our_channel, + %term, + %width_chars, + %height_rows, "Trying to open a terminal" ); @@ -252,12 +258,12 @@ impl ServerChannelsState { } } "shell" => { - info!(?our_channel, "Opening shell"); + info!(%our_channel, "Opening shell"); ChannelRequest::Shell { want_reply } } "exec" => { let command = packet.string()?; - info!(?our_channel, command = ?String::from_utf8_lossy(command), "Executing command"); + info!(%our_channel, command = %String::from_utf8_lossy(command), "Executing command"); ChannelRequest::Exec { want_reply, command: command.to_owned(), @@ -267,7 +273,7 @@ impl ServerChannelsState { let name = packet.utf8_string()?; let value = packet.string()?; - info!(?our_channel, ?name, value = ?String::from_utf8_lossy(value), "Setting environment variable"); + info!(%our_channel, %name, value = %String::from_utf8_lossy(value), "Setting environment variable"); ChannelRequest::Env { want_reply, @@ -276,12 +282,12 @@ impl ServerChannelsState { } } "signal" => { - debug!(?our_channel, "Received signal"); + debug!(%our_channel, "Received signal"); // Ignore signals, something we can do. return Ok(()); } _ => { - warn!(?request_type, ?our_channel, "Unknown channel request"); + warn!(%request_type, %our_channel, "Unknown channel request"); self.send_channel_failure(peer_channel); return Ok(()); } diff --git a/ssh-protocol/src/lib.rs b/ssh-protocol/src/lib.rs index 9b8a1df..f7f119c 100644 --- a/ssh-protocol/src/lib.rs +++ b/ssh-protocol/src/lib.rs @@ -112,9 +112,9 @@ pub mod auth { let method_name = auth_req.utf8_string()?; info!( - ?username, - ?service_name, - ?method_name, + %username, + %service_name, + %method_name, "User trying to authenticate" ); @@ -132,7 +132,7 @@ pub mod auth { } let password = auth_req.utf8_string()?; - info!(?password, "Got password"); + info!(%password, "Got password"); // Don't worry queen, your password is correct! self.queue_packet(Packet::new_msg_userauth_success()); diff --git a/ssh-transport/src/lib.rs b/ssh-transport/src/lib.rs index eddf12a..e09a2b1 100644 --- a/ssh-transport/src/lib.rs +++ b/ssh-transport/src/lib.rs @@ -137,7 +137,7 @@ impl ServerConnection { self.packet_transport.recv_bytes(bytes)?; while let Some(packet) = self.packet_transport.recv_next_packet() { - trace!(packet_type = ?packet.payload.get(0), packet_len = ?packet.payload.len(), "Received packet"); + trace!(packet_type = %packet.payload.get(0).unwrap_or(&0xFF), packet_len = %packet.payload.len(), "Received packet"); // Handle some packets ignoring the state. match packet.payload.get(0).copied() { @@ -148,7 +148,7 @@ impl ServerConnection { let description = disconnect.utf8_string()?; let _language_tag = disconnect.utf8_string()?; - info!(?reason, ?description, "Client disconnecting"); + info!(%reason, %description, "Client disconnecting"); return Ok(()); } @@ -169,8 +169,8 @@ impl ServerConnection { Ok(expected) } else { Err(client_error!( - "client does not supported algorithm {expected}. supported: {list:?}", - )) + "client does not supported algorithm {expected}. supported: {list:?}", + )) } }; @@ -341,7 +341,7 @@ impl ServerConnection { } let mut p = Parser::new(&packet.payload[1..]); let service = p.utf8_string()?; - debug!(?service, "Client requesting service"); + debug!(%service, "Client requesting service"); if service != "ssh-userauth" { return Err(client_error!("only supports ssh-userauth"));