-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathshell.rs
157 lines (131 loc) Β· 4.19 KB
/
shell.rs
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
use super::super::ShellStream;
use super::{InputStream, Interpreter, OutputStream, Token};
use crate::shell::{proto::WindowSize, server::shell::Shell};
use anyhow::{Error, Result};
use async_trait::async_trait;
use futures::{Future, Stream};
use std::{
collections::HashMap,
io::Write,
path::PathBuf,
pin::Pin,
sync::{Arc, Mutex},
};
use tokio::io::AsyncRead;
use tokio::task::JoinHandle;
/// In unix environments which do not support pty's we use this
/// bare-bones shell implementation
pub(crate) struct FallbackShell {
_interpreter_task: JoinHandle<Result<()>>,
state: SharedState,
}
#[derive(Clone)]
pub(super) struct SharedState {
pub(super) inner: Arc<Mutex<Inner>>,
}
pub(super) struct Inner {
pub(super) input: InputStream,
pub(super) output: OutputStream,
pub(super) pwd: PathBuf,
pub(super) env: HashMap<String, String>,
pub(super) size: WindowSize,
pub(super) exit_code: Option<u8>,
}
impl FallbackShell {
pub(in super::super) fn new(_term: &str, size: WindowSize) -> Self {
let state = SharedState::new(size);
let mut shell = Self {
_interpreter_task: Interpreter::start(state.clone()),
state,
};
shell.write_notice().unwrap();
shell
}
fn write_notice(&mut self) -> Result<()> {
let mut state = self.state.inner.lock().unwrap();
state.output.write("\r\n".as_bytes())?;
state.output.write("NOTICE: Tunshell is running in a limited environment and is unable to allocate a pty for a real shell. ".as_bytes())?;
state.output.write(
"Falling back to a built-in pseudo-shell with very limited functionality".as_bytes(),
)?;
state.output.write("\r\n\r\n".as_bytes())?;
Ok(())
}
}
#[async_trait]
impl Shell for FallbackShell {
async fn read(&mut self, buff: &mut [u8]) -> Result<usize> {
if self.exit_code().is_ok() {
return Ok(0);
}
self.state.read_output(buff).await
}
async fn write(&mut self, buff: &[u8]) -> Result<()> {
if self.exit_code().is_ok() {
return Err(Error::msg("shell has exited"));
}
// echo chars
let mut state = self.state.inner.lock().unwrap();
state.input.write_all(buff).map_err(Error::from)?;
Ok(())
}
fn resize(&mut self, size: WindowSize) -> Result<()> {
let mut state = self.state.inner.lock().unwrap();
state.size = size;
Ok(())
}
fn exit_code(&self) -> Result<u8> {
let state = self.state.inner.lock().unwrap();
state
.exit_code
.ok_or_else(|| Error::msg("shell has not closed"))
}
fn custom_io_handling(&self) -> bool {
false
}
async fn stream_io(&mut self, _stream: &mut ShellStream) -> Result<()> {
unreachable!()
}
}
impl Drop for FallbackShell {
fn drop(&mut self) {}
}
impl SharedState {
pub(super) fn new(size: WindowSize) -> Self {
Self {
inner: Arc::new(Mutex::new(Inner {
size,
input: InputStream::new(),
output: OutputStream::new(),
pwd: std::env::current_dir().unwrap(),
env: HashMap::new(),
exit_code: None,
})),
}
}
pub(super) fn exit_code(&self) -> Option<u8> {
self.inner.lock().unwrap().exit_code
}
pub(super) fn read_input<'a>(&'a mut self) -> impl Future<Output = Result<Token>> + 'a {
futures::future::poll_fn(move |cx| {
let mut state = self.inner.lock().unwrap();
let result = Pin::new(&mut state.input).poll_next(cx);
result.map(|i| i.ok_or_else(|| Error::msg("input stream ended unexpectedly")))
})
}
pub(super) fn read_output<'a>(
&'a mut self,
buff: &'a mut [u8],
) -> impl Future<Output = Result<usize>> + 'a {
futures::future::poll_fn(move |cx| {
let mut state = self.inner.lock().unwrap();
let result = Pin::new(&mut state.output).poll_read(cx, buff);
result.map_err(Error::from)
})
}
}
#[cfg(test)]
mod tests {
// use super::*;
// use std::time::Duration;
}