Skip to content

Commit

Permalink
added Server::handle_session_error and session closure logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugeny committed Nov 15, 2023
1 parent c66f4b0 commit c0f3458
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
1 change: 0 additions & 1 deletion russh/src/client/encrypted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,6 @@ impl Session {
// write responses
enc.client_send_auth_response(&responses)?;
return Ok((client, self));
} else {
}

// continue with userauth_pk_ok
Expand Down
4 changes: 2 additions & 2 deletions russh/src/server/encrypted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ impl Session {
handler,
&mut enc.write,
auth,
&mut self.common.auth_user,
&self.common.auth_user,
buf,
)
.await?;
Expand Down Expand Up @@ -530,7 +530,7 @@ async fn read_userauth_info_response<H: Handler + Send>(
mut handler: H,
write: &mut CryptoVec,
auth_request: &mut AuthRequest,
user: &mut str,
user: &str,
b: &[u8],
) -> Result<(H, bool), H::Error> {
if let Some(CurrentRequest::KeyboardInteractive { ref submethods }) = auth_request.current {
Expand Down
1 change: 1 addition & 0 deletions russh/src/server/kex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl KexInit {
session_id: self.session_id,
})
} else {
debug!("unknown key {:?}", algo.key);
return Err(Error::UnknownKey);
};

Expand Down
45 changes: 40 additions & 5 deletions russh/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ use std::task::{Context, Poll};

use async_trait::async_trait;
use futures::future::Future;
use log::error;
use log::{debug, error};
use russh_keys::key;
use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt};
use tokio::net::{TcpListener, ToSocketAddrs};
Expand Down Expand Up @@ -619,6 +619,8 @@ pub trait Server {
type Handler: Handler + Send;
/// Called when a new client connects.
fn new_client(&mut self, peer_addr: Option<std::net::SocketAddr>) -> Self::Handler;
/// Called when an active connection fails.
fn handle_session_error(&mut self, _error: <Self::Handler as Handler>::Error) {}
}

/// Run a server.
Expand All @@ -636,11 +638,44 @@ pub async fn run<H: Server + Send + 'static, A: ToSocketAddrs>(
config.maximum_packet_size
);
}
while let Ok((socket, _)) = socket.accept().await {
let config = config.clone();
let server = server.new_client(socket.peer_addr().ok());
tokio::spawn(run_stream(config, socket, server));

let (error_tx, mut error_rx) = tokio::sync::mpsc::unbounded_channel();

loop {
tokio::select! {
accept_result = socket.accept() => {
match accept_result {
Ok((socket, _)) => {
let config = config.clone();
let handler = server.new_client(socket.peer_addr().ok());
let error_tx = error_tx.clone();
tokio::spawn(async move {
let session = match run_stream(config, socket, handler).await {
Ok(s) => s,
Err(e) => {
debug!("Connection setup failed");
let _ = error_tx.send(e);
return
}
};
match session.await {
Ok(_) => debug!("Connection closed"),
Err(e) => {
debug!("Connection closed with error");
let _ = error_tx.send(e);
}
}
});
}
_ => break,
}
},
Some(error) = error_rx.recv() => {
server.handle_session_error(error);
}
}
}

Ok(())
}

Expand Down

0 comments on commit c0f3458

Please sign in to comment.