Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client Timeout #211

Merged
merged 2 commits into from
Nov 10, 2023
Merged
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
17 changes: 13 additions & 4 deletions russh/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ use crate::key::PubKey;
use crate::session::{CommonSession, EncryptedState, Exchange, Kex, KexDhDone, KexInit, NewKeys};
use crate::ssh_read::SshRead;
use crate::sshbuffer::{SSHBuffer, SshId};
use crate::{auth, msg, negotiation, ChannelId, ChannelOpenFailure, Disconnect, Limits, Sig};
use crate::{
auth, msg, negotiation, timeout, ChannelId, ChannelOpenFailure, Disconnect, Limits, Sig,
};

mod encrypted;
mod kex;
Expand Down Expand Up @@ -772,6 +774,8 @@ impl Session {
pin!(reading);
pin!(time_for_keepalive);

let delay = self.common.config.inactivity_timeout;

#[allow(clippy::panic)] // false positive in select! macro
while !self.common.disconnected {
tokio::select! {
Expand Down Expand Up @@ -852,6 +856,10 @@ impl Session {
}
}
}
_ = timeout(delay) => {
debug!("timeout");
break
},
}
self.flush()?;
if !self.common.write_buffer.buffer.is_empty() {
Expand All @@ -874,9 +882,10 @@ impl Session {
}
}
debug!("disconnected");
if self.common.disconnected {
stream_write.shutdown().await.map_err(crate::Error::from)?;
}
self.receiver.close();
self.inbound_channel_receiver.close();
stream_write.shutdown().await.map_err(crate::Error::from)?;

Ok(())
}

Expand Down
8 changes: 8 additions & 0 deletions russh/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,3 +465,11 @@ impl ChannelParams {
self.confirmed = true;
}
}

pub(crate) async fn timeout(delay: Option<std::time::Duration>) {
if let Some(delay) = delay {
tokio::time::sleep(delay).await
} else {
futures::future::pending().await
};
}
4 changes: 2 additions & 2 deletions russh/src/server/encrypted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl Session {
// Either this packet is a KEXINIT, in which case we start a key re-exchange.

#[allow(clippy::unwrap_used)]
let mut enc = self.common.encrypted.as_mut().unwrap();
let enc = self.common.encrypted.as_mut().unwrap();
if buf.first() == Some(&msg::KEXINIT) {
debug!("Received rekeying request");
// If we're not currently rekeying, but `buf` is a rekey request
Expand Down Expand Up @@ -142,7 +142,7 @@ impl Session {
};

#[allow(clippy::unwrap_used)]
let mut enc = self.common.encrypted.as_mut().unwrap();
let enc = self.common.encrypted.as_mut().unwrap();
// If we've successfully read a packet.
match enc.state {
EncryptedState::WaitingAuthServiceRequest {
Expand Down
8 changes: 0 additions & 8 deletions russh/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,14 +650,6 @@ thread_local! {
static B2: RefCell<CryptoVec> = RefCell::new(CryptoVec::new());
}

pub(crate) async fn timeout(delay: Option<std::time::Duration>) {
if let Some(delay) = delay {
tokio::time::sleep(delay).await
} else {
futures::future::pending().await
};
}

async fn start_reading<R: AsyncRead + Unpin>(
mut stream_read: R,
mut buffer: SSHBuffer,
Expand Down
Loading