Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions russh/examples/echoserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ impl server::Server for Server {

impl server::Handler for Server {
type Error = russh::Error;
type Data = ();

async fn channel_open_session(
&mut self,
Expand Down
1 change: 1 addition & 0 deletions russh/examples/echoserver_certificates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ impl server::Server for Server {

impl server::Handler for Server {
type Error = russh::Error;
type Data = ();

async fn channel_open_session(
&mut self,
Expand Down
1 change: 1 addition & 0 deletions russh/examples/ratatui_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ impl Server for AppServer {

impl Handler for AppServer {
type Error = anyhow::Error;
type Data = ();

async fn channel_open_session(
&mut self,
Expand Down
1 change: 1 addition & 0 deletions russh/examples/ratatui_shared_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ impl Server for AppServer {

impl Handler for AppServer {
type Error = anyhow::Error;
type Data = ();

async fn channel_open_session(
&mut self,
Expand Down
1 change: 1 addition & 0 deletions russh/examples/sftp_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ impl SshSession {

impl russh::server::Handler for SshSession {
type Error = anyhow::Error;
type Data = ();

async fn auth_password(&mut self, user: &str, password: &str) -> Result<Auth, Self::Error> {
info!("credentials: {user}, {password}");
Expand Down
1 change: 1 addition & 0 deletions russh/examples/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl server::Server for Server {

impl server::Handler for Server {
type Error = anyhow::Error;
type Data = ();

async fn channel_open_session(
&mut self,
Expand Down
1 change: 1 addition & 0 deletions russh/src/client/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ mod tests {

impl ServerHandler for TestServer {
type Error = Error;
type Data = ();

async fn channel_open_session(
&mut self,
Expand Down
3 changes: 3 additions & 0 deletions russh/src/server/encrypted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ mod tests {

impl Handler for Probe {
type Error = Error;
type Data = ();

async fn auth_publickey_offered(
&mut self,
Expand Down Expand Up @@ -426,6 +427,7 @@ mod tests {

impl Handler for Probe {
type Error = Error;
type Data = ();

async fn auth_publickey_offered(
&mut self,
Expand Down Expand Up @@ -526,6 +528,7 @@ mod tests {

impl Handler for ChannelCallbackProbe {
type Error = Error;
type Data = ();

async fn exec_request(
&mut self,
Expand Down
53 changes: 53 additions & 0 deletions russh/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ impl Auth {
#[cfg_attr(feature = "async-trait", async_trait::async_trait)]
pub trait Handler: Sized {
type Error: From<crate::Error> + Send;
type Data: Send;

/// Check authentication using the "none" method.
///
Expand Down Expand Up @@ -849,6 +850,58 @@ pub trait Handler: Sized {
Ok(Some(best_group.clone()))
}
}

/// Called when the handler needs to be updated.
/// ['trigger'] should be used with ['process']
///
/// # Cancel safety
///
/// The safety of this method depends entirely on how you implement it;
/// it provides no inherent security guarantees.
///
/// # Example
///
/// ```
/// use tokio::sync::mpsc::Receiver;
/// use russh::server::{Handler, Session};
///
/// struct App{
/// foo: String,
/// recv: Receiver<String>,
/// trigger: Receiver<String>,
/// }
///
/// impl Handler for App {
/// type Error = russh::Error;
/// type Data = String;
/// async fn trigger(&mut self) -> Result<Self::Data, Self::Error> {
/// match self.trigger.recv().await {
/// Some(d) => Ok(d),
/// None => std::future::pending().await,
/// }
/// }
///
/// async fn process(&mut self, s: Self::Data, session: &mut Session) -> Result<(), Self::Error> {
/// let s = self.recv.recv().await.unwrap();
/// self.foo = s;
/// Ok(())
/// }
/// }
/// ```
///
fn trigger(&mut self) -> impl Future<Output = Result<Self::Data, Self::Error>> + Send {
std::future::pending()
}

/// Called after [`trigger`], See [`trigger`] for more.
#[allow(unused_variables)]
fn process(
&mut self,
data: Self::Data,
session: &mut Session,
) -> impl Future<Output = Result<(), Self::Error>> + Send {
async { Ok(()) }
}
}

pub struct RunningServerHandle {
Expand Down
8 changes: 8 additions & 0 deletions russh/src/server/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,13 @@ impl Session {
}
reading.set(start_reading(stream_read, buffer, opening_cipher));
}
t = handler.trigger() => {
debug!("handler trigger is invoked");
match t {
Ok(d) => handler.process(d,&mut self).await?,
Err(e) => return Err(e)
}
}
() = &mut keepalive_timer => {
self.common.alive_timeouts = self.common.alive_timeouts.saturating_add(1);
if self.common.config.keepalive_max != 0 && self.common.alive_timeouts > self.common.config.keepalive_max {
Expand Down Expand Up @@ -1515,6 +1522,7 @@ mod tests {

impl crate::server::Handler for TestHandler {
type Error = crate::Error;
type Data = ();
}

fn authenticated_session() -> Session {
Expand Down
9 changes: 9 additions & 0 deletions russh/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ mod compress {

impl server::Handler for Server {
type Error = super::Error;
type Data = ();

async fn channel_open_session(
&mut self,
Expand Down Expand Up @@ -289,6 +290,7 @@ mod channels {

impl server::Handler for ServerHandle {
type Error = crate::Error;
type Data = ();

async fn auth_publickey(
&mut self,
Expand Down Expand Up @@ -360,6 +362,7 @@ mod channels {

impl server::Handler for ServerHandle {
type Error = crate::Error;
type Data = ();

async fn auth_publickey(
&mut self,
Expand Down Expand Up @@ -446,6 +449,7 @@ mod channels {

impl server::Handler for ServerHandle {
type Error = crate::Error;
type Data = ();

async fn auth_publickey(
&mut self,
Expand Down Expand Up @@ -532,6 +536,7 @@ mod channels {

impl server::Handler for ServerHandle {
type Error = crate::Error;
type Data = ();

async fn auth_publickey(
&mut self,
Expand Down Expand Up @@ -614,6 +619,7 @@ mod channels {

impl server::Handler for ServerHandle {
type Error = crate::Error;
type Data = ();

async fn auth_publickey(
&mut self,
Expand Down Expand Up @@ -755,6 +761,7 @@ mod server_kex_junk {

impl server::Handler for Server {
type Error = super::Error;
type Data = ();
}
}

Expand Down Expand Up @@ -1206,6 +1213,7 @@ pub(crate) mod raw_no_crypto {

impl server::Handler for MalformedInputServer {
type Error = Error;
type Data = ();

async fn auth_none(&mut self, _user: &str) -> Result<server::Auth, Self::Error> {
self.record("auth_none");
Expand Down Expand Up @@ -1409,6 +1417,7 @@ mod future_certificate {

impl server::Handler for CertHandler {
type Error = crate::Error;
type Data = ();

async fn auth_publickey_offered(
&mut self,
Expand Down
1 change: 1 addition & 0 deletions russh/tests/auth_state_reset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct RemainingMethodsUserSwitchServer;

impl server::Handler for RemainingMethodsUserSwitchServer {
type Error = russh::Error;
type Data = ();

async fn auth_none(&mut self, user: &str) -> Result<server::Auth, Self::Error> {
if user == "alice" {
Expand Down
2 changes: 2 additions & 0 deletions russh/tests/test_backpressure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ impl russh::server::Server for Server {

impl russh::server::Handler for Server {
type Error = anyhow::Error;
type Data = ();

async fn auth_publickey(
&mut self,
Expand Down Expand Up @@ -242,6 +243,7 @@ impl russh::server::Server for HandleBackpressureServer {

impl russh::server::Handler for HandleBackpressureServer {
type Error = anyhow::Error;
type Data = ();

async fn auth_publickey(
&mut self,
Expand Down
1 change: 1 addition & 0 deletions russh/tests/test_contention.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ impl russh::server::Server for Server {

impl russh::server::Handler for Server {
type Error = anyhow::Error;
type Data = ();

async fn auth_publickey(
&mut self,
Expand Down
1 change: 1 addition & 0 deletions russh/tests/test_data_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ impl russh::server::Server for Server {

impl russh::server::Handler for Server {
type Error = anyhow::Error;
type Data = ();

async fn auth_publickey(
&mut self,
Expand Down
1 change: 1 addition & 0 deletions russh/tests/test_kex_shared_secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ struct TestServer {}

impl server::Handler for TestServer {
type Error = russh::Error;
type Data = ();

async fn auth_publickey(
&mut self,
Expand Down
1 change: 1 addition & 0 deletions russh/tests/test_max_channel_packet_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ struct EchoServer {}

impl server::Handler for EchoServer {
type Error = russh::Error;
type Data = ();

async fn auth_publickey(
&mut self,
Expand Down
1 change: 1 addition & 0 deletions russh/tests/test_mlkem_kex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ struct TestServer {}

impl server::Handler for TestServer {
type Error = russh::Error;
type Data = ();

async fn auth_publickey(
&mut self,
Expand Down
1 change: 1 addition & 0 deletions russh/tests/test_rekey_strict_kex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ struct TestServer {}
// Insecure server that accepts any public key and echos back data it receives; ONLY FOR TESTS
impl server::Handler for TestServer {
type Error = russh::Error;
type Data = ();

async fn auth_publickey(
&mut self,
Expand Down
1 change: 1 addition & 0 deletions russh/tests/test_server_cert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ struct TestServer {}

impl server::Handler for TestServer {
type Error = russh::Error;
type Data = ();

async fn auth_publickey(
&mut self,
Expand Down
Loading