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
use channel_protocol::{ChannelMessage, ChannelProtocol};
use error::ProtocolError;
use messages;
use process::ProcessHandler;
use std::net::SocketAddr;
use std::time::Duration;
pub struct Protocol {
channel_protocol: ChannelProtocol,
process: Box<ProcessHandler>,
channel_id: u32,
}
impl Protocol {
pub fn new(
channel_protocol: ChannelProtocol,
channel_id: u32,
process: Box<ProcessHandler>,
) -> Self {
Protocol {
channel_protocol,
process,
channel_id,
}
}
pub fn message_engine<F>(&mut self, pump: F, timeout: Duration) -> Result<(), ProtocolError>
where
F: Fn(Duration) -> Result<(ChannelMessage, SocketAddr), ProtocolError>,
{
loop {
{
let process = self.process.as_mut();
if process.stdout_reader.is_some() {
match process.read_stdout() {
Ok(Some(data)) => {
self.channel_protocol
.send(messages::stdout::to_cbor(self.channel_id, Some(&data))?)?;
}
Err(ProtocolError::ReadTimeout) => {}
_ => {
self.channel_protocol
.send(messages::stdout::to_cbor(self.channel_id, None)?)?;
process.stdout_reader = None;
}
}
}
if process.stderr_reader.is_some() {
match process.read_stderr() {
Ok(Some(data)) => {
self.channel_protocol
.send(messages::stderr::to_cbor(self.channel_id, Some(&data))?)?;
}
Err(ProtocolError::ReadTimeout) => {}
_ => {
self.channel_protocol
.send(messages::stderr::to_cbor(self.channel_id, None)?)?;
process.stderr_reader = None;
}
}
}
if process.stdout_reader.is_none() && process.stderr_reader.is_none() {
if let Some((code, signal)) = process.status()? {
self.channel_protocol.send(messages::exit::to_cbor(
self.channel_id,
code,
signal,
)?)?;
return Ok(());
}
}
}
let (message, remote) = match pump(timeout) {
Ok(message) => message,
Err(ProtocolError::ReceiveTimeout) => {
continue;
}
Err(e) => return Err(e),
};
self.channel_protocol.set_remote(remote);
self.process_message(message)?;
}
}
fn process_message(&mut self, message: ChannelMessage) -> Result<(), ProtocolError> {
let parsed_message = messages::parse_message(message)?;
match parsed_message {
messages::Message::Stdin { channel_id, data } => {
info!("<- {{ {}, stdin, {:?} }}", channel_id, data);
{
let process = self.process.as_mut();
match data {
Some(data) => process.write_stdin(&data.as_bytes())?,
None => process.close_stdin()?,
}
}
}
messages::Message::Kill { channel_id, signal } => {
info!("<- {{ {}, kill, {:?} }}", channel_id, signal);
{
let process = self.process.as_mut();
process.kill(signal)?;
}
}
message => warn!("Shell service received unexpected message: {:?}", message),
}
Ok(())
}
}