aboutsummaryrefslogtreecommitdiffstats
path: root/teleterm/src/cmd/stream.rs
blob: 4c3f22992cc25fceca54052e9e2559f9b3b3c3d3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
use crate::prelude::*;
use tokio::io::AsyncWrite as _;

#[derive(serde::Deserialize, Debug, Default)]
pub struct Config {
    #[serde(default)]
    client: crate::config::Client,

    #[serde(default)]
    command: crate::config::Command,
}

impl crate::config::Config for Config {
    fn merge_args<'a>(
        &mut self,
        matches: &clap::ArgMatches<'a>,
    ) -> Result<()> {
        self.client.merge_args(matches)?;
        self.command.merge_args(matches)?;
        Ok(())
    }

    fn run(
        &self,
    ) -> Box<dyn futures::Future<Item = (), Error = Error> + Send> {
        let auth = match self.client.auth {
            crate::protocol::AuthType::Plain => {
                let username = self
                    .client
                    .username
                    .clone()
                    .context(crate::error::CouldntFindUsername);
                match username {
                    Ok(username) => crate::protocol::Auth::plain(&username),
                    Err(e) => return Box::new(futures::future::err(e)),
                }
            }
            crate::protocol::AuthType::RecurseCenter => {
                let id = crate::oauth::load_client_auth_id(self.client.auth);
                crate::protocol::Auth::recurse_center(
                    id.as_ref().map(std::string::String::as_str),
                )
            }
        };

        let host = self.client.host().to_string();
        let address = *self.client.addr();
        if self.client.tls {
            let connector = match native_tls::TlsConnector::new()
                .context(crate::error::CreateConnector)
            {
                Ok(connector) => connector,
                Err(e) => return Box::new(futures::future::err(e)),
            };
            let connect: crate::client::Connector<_> = Box::new(move || {
                let host = host.clone();
                let connector = connector.clone();
                let connector = tokio_tls::TlsConnector::from(connector);
                let stream = tokio::net::tcp::TcpStream::connect(&address);
                Box::new(
                    stream
                        .context(crate::error::Connect { address })
                        .and_then(move |stream| {
                            connector
                                .connect(&host, stream)
                                .context(crate::error::ConnectTls { host })
                        }),
                )
            });
            Box::new(StreamSession::new(
                &self.command.command,
                &self.command.args,
                connect,
                &auth,
            ))
        } else {
            let connect: crate::client::Connector<_> = Box::new(move || {
                Box::new(
                    tokio::net::tcp::TcpStream::connect(&address)
                        .context(crate::error::Connect { address }),
                )
            });
            Box::new(StreamSession::new(
                &self.command.command,
                &self.command.args,
                connect,
                &auth,
            ))
        }
    }
}

pub fn cmd<'a, 'b>(app: clap::App<'a, 'b>) -> clap::App<'a, 'b> {
    crate::config::Client::cmd(crate::config::Command::cmd(
        app.about("Stream your terminal"),
    ))
}

pub fn config(
    mut config: Option<config::Config>,
) -> Result<Box<dyn crate::config::Config>> {
    if config.is_none() {
        config = crate::config::wizard::run()?;
    }
    let config: Config = if let Some(config) = config {
        config
            .try_into()
            .context(crate::error::CouldntParseConfig)?
    } else {
        Config::default()
    };
    Ok(Box::new(config))
}

struct StreamSession<
    S: tokio::io::AsyncRead + tokio::io::AsyncWrite + Send + 'static,
> {
    client: crate::client::Client<S>,
    connected: bool,

    process:
        tokio_pty_process_stream::ResizingProcess<crate::async_stdin::Stdin>,
    raw_screen: Option<crossterm::screen::RawScreen>,
    done: bool,

    term: vt100::Parser,
    last_screen: vt100::Screen,
    needs_screen_update: bool,

    stdout: tokio::io::Stdout,
    to_print: std::collections::VecDeque<u8>,
    needs_flush: bool,
}

impl<S: tokio::io::AsyncRead + tokio::io::AsyncWrite + Send + 'static>
    StreamSession<S>
{
    fn new(
        cmd: &str,
        args: &[String],
        connect: crate::client::Connector<S>,
        auth: &crate::protocol::Auth,
    ) -> Self {
        let term_type =
            std::env::var("TERM").unwrap_or_else(|_| "".to_string());
        let client = crate::client::Client::stream(
            &term_type,
            connect,
            auth,
            crate::protocol::AuthClient::Cli,
        );

        // TODO: tokio::io::stdin is broken (it's blocking)
        // see https://github.com/tokio-rs/tokio/issues/589
        // let input = tokio::io::stdin();
        let input = crate::async_stdin::Stdin::new();

        let process = tokio_pty_process_stream::ResizingProcess::new(
            tokio_pty_process_stream::Process::new(cmd, args, input),
        );

        let term = vt100::Parser::default();
        let screen = term.screen().clone();

        Self {
            client,
            connected: false,

            process,
            raw_screen: None,
            done: false,

            term,
            last_screen: screen,
            needs_screen_update: false,

            stdout: tokio::io::stdout(),
            to_print: std::collections::VecDeque::new(),
            needs_flush: false,
        }
    }

    fn record_bytes(&mut self, buf: &[u8]) {
        self.to_print.extend(buf);
        self.term.process(buf);
        self.needs_screen_update = true;
    }
}

impl<S: tokio::io::AsyncRead + tokio::io::AsyncWrite + Send + 'static>
    StreamSession<S>
{
    const POLL_FNS:
        &'static [&'static dyn for<'a> Fn(
            &'a mut Self,
        )
            -> component_future::Poll<
            (),
            Error,
        >] = &[
        &Self::poll_read_client,
        &Self::poll_read_process,
        &Self::poll_write_terminal,
        &Self::poll_flush_terminal,
        &Self::poll_write_server,
    ];

    // this should never return Err, because we don't want server
    // communication issues to ever interrupt a running process
    fn poll_read_client(&mut self) -> component_future::Poll<(), Error> {
        match self.client.poll() {
            Ok(futures::Async::Ready(Some(e))) => match e {
                crate::client::Event::Disconnect => {
                    self.connected = false;
                    Ok(component_future::Async::DidWork)
                }
                crate::client::Event::Connect => {
                    self.connected = true;
                    self.client.send_message(
                        crate::protocol::Message::terminal_output(
                            &self.last_screen.contents_formatted(),
                        ),
                    );
                    Ok(component_future::Async::DidWork)
                }
                crate::client::Event::ServerMessage(..) => {
                    // we don't expect to ever see a server message once we
                    // start streaming, so if one comes through, assume
                    // something is messed up and try again
                    self.client.reconnect();
                    Ok(component_future::Async::DidWork)
                }
            },
            Ok(futures::Async::Ready(None)) => {
                // the client should never exit on its own
                unreachable!()
            }
            Ok(futures::Async::NotReady) => {
                Ok(component_future::Async::NotReady)
            }
            Err(..) => {
                self.client.reconnect();
                Ok(component_future::Async::DidWork)
            }
        }
    }

    fn poll_read_process(&mut self) -> component_future::Poll<(), Error> {
        match component_future::try_ready!(self
            .process
            .poll()
            .context(crate::error::Subprocess))
        {
            Some(tokio_pty_process_stream::Event::CommandStart {
                ..
            }) => {
                if self.raw_screen.is_none() {
                    self.raw_screen = Some(
                        crossterm::screen::RawScreen::into_raw_mode()
                            .context(crate::error::ToRawMode)?,
                    );
                }
            }
            Some(tokio_pty_process_stream::Event::CommandExit { .. }) => {
                self.done = true;
            }
            Some(tokio_pty_process_stream::Event::Output { data }) => {
                self.record_bytes(&data);
            }
            Some(tokio_pty_process_stream::Event::Resize {
                size: (rows, cols),
            }) => {
                self.term.set_size(rows, cols);
                self.client.send_message(crate::protocol::Message::resize(
                    crate::term::Size { rows, cols },
                ));
            }
            None => {
                if !self.done {
                    unreachable!()
                }
                // don't return final event here - wait until we are done
                // sending all data to the server (see poll_write_server)
            }
        }
        Ok(component_future::Async::DidWork)
    }

    fn poll_write_terminal(&mut self) -> component_future::Poll<(), Error> {
        if self.to_print.is_empty() {
            return Ok(component_future::Async::NothingToDo);
        }

        let (a, b) = self.to_print.as_slices();
        let buf = if a.is_empty() { b } else { a };
        let n = component_future::try_ready!(self
            .stdout
            .poll_write(buf)
            .context(crate::error::WriteTerminal));
        for _ in 0..n {
            self.to_print.pop_front();
        }
        self.needs_flush = true;
        Ok(component_future::Async::DidWork)
    }

    fn poll_flush_terminal(&mut self) -> component_future::Poll<(), Error> {
        if !self.needs_flush {
            return Ok(component_future::Async::NothingToDo);
        }

        component_future::try_ready!(self
            .stdout
            .poll_flush()
            .context(crate::error::FlushTerminal));
        self.needs_flush = false;
        Ok(component_future::Async::DidWork)
    }

    fn poll_write_server(&mut self) -> component_future::Poll<(), Error> {
        if !self.connected || !self.needs_screen_update {
            // ship all data to the server before actually ending
            if self.done {
                return Ok(component_future::Async::Ready(()));
            } else {
                return Ok(component_future::Async::NothingToDo);
            }
        }

        let screen = self.term.screen().clone();
        self.client
            .send_message(crate::protocol::Message::terminal_output(
                &screen.contents_diff(&self.last_screen),
            ));
        self.last_screen = screen;
        self.needs_screen_update = false;

        Ok(component_future::Async::DidWork)
    }
}

#[must_use = "futures do nothing unless polled"]
impl<S: tokio::io::AsyncRead + tokio::io::AsyncWrite + Send + 'static>
    futures::Future for StreamSession<S>
{
    type Item = ();
    type Error = Error;

    fn poll(&mut self) -> futures::Poll<Self::Item, Self::Error> {
        component_future::poll_future(self, Self::POLL_FNS)
    }
}