1
//! Client data on the channels that buffer it: ssh-copy-id uploads and TUI input.
2

            
3
use super::{Session_, upload};
4
use crate::key_decoder::feed_shared;
5
use crate::tui_transport::restore;
6
use russh::ChannelId;
7
use russh::server::Session;
8
use tui::transport::warn_failed;
9

            
10
/// Sent after the terminal restore when a TUI channel is closed for overflowing its queue.
11
const OVERFLOW_REPLY: &[u8] = b"input overflow; session closed\r\n";
12

            
13
impl Session_ {
14
    /// Appends to an upload; one over the cap is dropped with its channel.
15
    /// False when `channel` is not an upload.
16
222
    pub(super) fn feed_upload(
17
222
        &mut self,
18
222
        channel: ChannelId,
19
222
        data: &[u8],
20
222
        session: &mut Session,
21
222
    ) -> Result<bool, russh::Error> {
22
222
        let Some(buf) = self.pubkey_uploads.get_mut(&channel) else {
23
212
            return Ok(false);
24
        };
25
10
        if !upload::append_upload(buf, data) {
26
1
            tracing::warn!(
27
                "ssh-copy-id upload over {} bytes; closing",
28
                upload::MAX_UPLOAD_BYTES
29
            );
30
1
            self.pubkey_uploads.remove(&channel);
31
1
            session.close(channel)?;
32
9
        }
33
10
        Ok(true)
34
222
    }
35

            
36
    /// Decodes TUI input; a full queue closes the channel. False when `channel`
37
    /// is not a TUI channel.
38
212
    pub(super) fn feed_tui(
39
212
        &mut self,
40
212
        channel: ChannelId,
41
212
        data: &[u8],
42
212
        session: &mut Session,
43
212
    ) -> Result<bool, russh::Error> {
44
212
        let Some(state) = self.tui_channels.get_mut(&channel) else {
45
177
            return Ok(false);
46
        };
47
35
        let overflowed = state
48
35
            .input_tx
49
35
            .as_ref()
50
35
            .is_some_and(|tx| feed_shared(&state.decoder, data, tx).is_err());
51
35
        if overflowed {
52
1
            tracing::warn!("tui input backlog on channel {channel:?}; closing it");
53
1
            state.input_tx = None;
54
1
            let mut farewell = Vec::new();
55
1
            warn_failed(restore(&mut farewell));
56
1
            farewell.extend_from_slice(OVERFLOW_REPLY);
57
1
            session.data(channel, farewell)?;
58
1
            session.close(channel)?;
59
34
        }
60
35
        Ok(true)
61
212
    }
62
}