Skip to content
Merged
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
149 changes: 147 additions & 2 deletions pkgs/id/src/world_ssh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ const REFUSED_SHELL_DELAY: Duration = Duration::from_secs(1);
const PROMPT: &str = "id> ";
/// Longest line the explorer keeps; the rest of a longer line is dropped.
const MAX_LINE: usize = 1024;
/// Enter the alternate screen buffer, so a game's repaints stay out of scrollback.
const ALT_SCREEN_ENTER: &str = "\x1b[?1049h";
/// Leave the alternate screen buffer and restore the screen the user came from.
const ALT_SCREEN_LEAVE: &str = "\x1b[?1049l";
/// Input bytes dropped before the session tells the viewer about it.
const INPUT_DROP_NOTICE_BYTES: usize = 1024;

/// Serve SSH connections until the listener fails.
pub async fn serve(
Expand Down Expand Up @@ -114,6 +120,7 @@ impl Server for SshServer {
login: None,
input: None,
viewport: None,
dropped_input: 0,
}
}
}
Expand All @@ -140,6 +147,8 @@ struct SshClient {
login: Option<Login>,
input: Option<mpsc::Sender<SshInputFrame>>,
viewport: Option<(u32, u32)>,
/// Bytes the input queue refused; handed to the shell so the drop is visible.
dropped_input: usize,
}

enum SshInputFrame {
Expand Down Expand Up @@ -306,6 +315,11 @@ impl Handler for SshClient {
}
let first = join_frame(login);
session.channel_success(channel)?;
let alt_screen = if !login.command_mode {
Some(ALT_SCREEN_ENTER.to_owned())

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Send the enter sequence before starting the raw session

For every raw shell, this only stores ALT_SCREEN_ENTER; no startup path writes it to the channel. When run_session finishes, leave_alt_screen() sends that stored enter sequence, so gameplay remains on the primary screen while cleanup can switch the client into the alternate buffer immediately before closing. Send the enter sequence at startup and retain ALT_SCREEN_LEAVE for teardown.

Useful? React with 👍 / 👎.

} else {
None
};

let (input, frames) = mpsc::channel(INPUT_QUEUE);
let first_resize = self.viewport.take();
Expand All @@ -324,6 +338,10 @@ impl Handler for SshClient {
first_resize,
capability: login.capability.clone(),
mode,
alt_screen,
pending_notice: None,
last_error: None,
dropped_input: self.dropped_input,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Share dropped-input counts with the active shell

When post-shell input fills the queue, data() increments SshClient::dropped_input, but this line copied the counter into an independently owned SshIo before any such input could arrive. Consequently SshIo::note_dropped_input() continues reading zero throughout the active shell and never displays a notice; use shared state or send a drop notification through a channel.

Useful? React with 👍 / 👎.

lines: WorldLineBuffer::default(),
commands: VecDeque::new(),
records_prefix: None,
Expand All @@ -335,6 +353,7 @@ impl Handler for SshClient {
if io.errored {
tokio::time::sleep(REFUSED_SHELL_DELAY).await;
}
io.leave_alt_screen().await;
let _ = io.handle.close(channel).await;
});
Ok(())
Expand All @@ -354,7 +373,15 @@ impl Handler for SshClient {
if let Err(error) = input.try_send(SshInputFrame::Bytes(data.to_vec())) {
match error {
mpsc::error::TrySendError::Full(_) => {
tracing::debug!("world ssh input queue full; dropping input");
// The handler shares the loop that flushes output, so it must
// not wait for queue space. Count the loss instead and let the
// shell tell the viewer once, instead of dropping silently.
self.dropped_input += data.len();
tracing::warn!(
"world ssh input queue full; dropped {} bytes ({} total)",
data.len(),
self.dropped_input
);
Comment on lines +380 to +384

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Rate-limit warnings for refused input packets

Whenever the queue is full, every subsequent SSH data packet now emits a warning. A remote client does not need a valid capability to reach this path because password authentication accepts first and join validation happens inside the shell; during the one-second refused-shell delay its queue is no longer drained, so it can generate a high-volume warning-log flood. Aggregate or rate-limit these messages rather than logging once per refused packet.

Useful? React with 👍 / 👎.

}
mpsc::error::TrySendError::Closed(_) => self.input = None,
}
Expand Down Expand Up @@ -400,13 +427,52 @@ struct SshIo {
first_resize: Option<(u32, u32)>,
capability: String,
mode: ShellMode,
/// Set while the terminal is inside the alternate screen buffer (raw sessions).
alt_screen: Option<String>,
/// Text to write before the next server frame, once.
pending_notice: Option<String>,
/// The last error message, re-sent after leaving the alternate screen.
last_error: Option<String>,
/// Bytes the input queue refused, handed over when the shell opened.
dropped_input: usize,
lines: WorldLineBuffer,
commands: VecDeque<WorldCommand>,
/// `Some(prefix)` while draining records pages from a `/records` command.
records_prefix: Option<Option<String>>,
errored: bool,
}

/// The notice shown to a viewer after `bytes` input bytes were refused. Empty
/// when nothing was dropped.
fn dropped_input_notice(bytes: usize) -> Option<String> {
(bytes > 0).then(|| {
format!("\r\n[-- input dropped: {bytes} bytes refused by the SSH input queue --]\r\n")
})
}

impl SshIo {
/// Tell the viewer that input was dropped, before the next frame.
fn note_dropped_input(&mut self) {
if let Some(notice) = dropped_input_notice(self.dropped_input) {
self.dropped_input = 0;
self.pending_notice = Some(notice);
}
}

/// Leave the alternate screen and re-send the last error, which the
/// alternate screen may have overwritten.
async fn leave_alt_screen(&mut self) {
let Some(sequence) = self.alt_screen.take() else {
return;
};
let text = match self.last_error.take() {
Some(error) => format!("{sequence}\r\n{error}\r\n"),
None => sequence,
};
let _ = self.handle.data(self.channel, text).await;
}
}

impl SessionIo for SshIo {
async fn recv(&mut self) -> Inbound {
if let Some(join) = self.first.take() {
Expand Down Expand Up @@ -523,6 +589,12 @@ impl SessionIo for SshIo {
}

async fn send(&mut self, frame: String) -> bool {
self.note_dropped_input();
if let Some(notice) = self.pending_notice.take() {
if self.handle.data(self.channel, notice).await.is_err() {
Comment on lines +592 to +594

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve the drop notice across the next repaint

If a dropped-input count reaches SshIo, consuming and writing the notice before dispatching the current frame makes it effectively invisible when that frame is a view: every raw view rendering path starts with ESC[H ESC[2J and immediately erases the notice. The normal input-driven event-to-view sequence has the same result, so the notice needs to be incorporated after the repaint or displayed in a reserved status area.

Useful? React with 👍 / 👎.

return false;
}
}
let Ok(value) = serde_json::from_str::<serde_json::Value>(&frame) else {
return true;
};
Expand Down Expand Up @@ -570,7 +642,8 @@ impl SessionIo for SshIo {
self.errored = true;
self.records_prefix = None;
let message = value["message"].as_str().unwrap_or("error");
format!("\r\n{}\r\n", terminal_text(message))
self.last_error = Some(format!("\r\n{}\r\n", terminal_text(message)));
self.last_error.clone().unwrap_or_default()
}
Some("records") => {
let records = value["records"].as_object();
Expand Down Expand Up @@ -1515,6 +1588,78 @@ mod tests {
WorldHub::single(WorldService::new(world, Some("admin".to_owned())))
}

#[tokio::test]
async fn a_raw_shell_enters_and_leaves_the_alternate_screen() {
let (port, server) = start(lobby_hub()).await;
let session = connect(port, &"0".repeat(64)).await;
let mut channel = shell(&session).await;

let mut bytes = Vec::new();
tokio::time::timeout(Duration::from_secs(10), async {
while !bytes
.windows(ALT_SCREEN_ENTER.len())
.any(|window| window == ALT_SCREEN_ENTER.as_bytes())
{
match channel.wait().await {
Some(ChannelMsg::Data { data }) => bytes.extend_from_slice(&data),
Some(_) => {}
None => break,
}
}
})
.await
.expect("timed out waiting for the alternate screen to open");

drop(channel);
drop(session);
server.abort();
}

#[tokio::test]
async fn a_command_shell_stays_on_the_primary_screen() {
let hub = lobby_hub();
let invite = hub
.lease(None, Some("admin"))
.await
.unwrap()
.service()
.invite(Some("admin"), "ssh-cmd-alt".to_owned(), None, None)
.await
.unwrap();
let capability = serde_json::to_value(invite).unwrap()["capability"]
.as_str()
.unwrap()
.to_owned();
let (port, server) = start(hub).await;
let session = connect_as(port, "cmd:", &capability).await;
let mut channel = shell(&session).await;
let mut text = String::new();
tokio::time::timeout(Duration::from_secs(10), async {
while text.len() < 32 {
if let Some(ChannelMsg::Data { data }) = channel.wait().await {
text.push_str(&String::from_utf8_lossy(&data));
}
}
})
.await
.expect("timed out waiting for the command prompt");
assert!(
!text
.as_bytes()
.windows(ALT_SCREEN_ENTER.len())
.any(|window| window == ALT_SCREEN_ENTER.as_bytes())
);
server.abort();
}

#[test]
fn dropped_input_notice_counts_and_quiet_zero() {
assert_eq!(dropped_input_notice(0), None);
let notice = dropped_input_notice(1024).unwrap();
assert!(notice.contains("1024"));
assert!(notice.contains("dropped"));
}

#[tokio::test]
async fn a_wrong_password_is_refused_inside_the_shell() {
let (port, server) = start(lobby_hub()).await;
Expand Down
Loading