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
#[deny(warnings)]
use channel_protocol::ChannelProtocol;
use clap::{value_t, App, AppSettings, Arg, SubCommand};
use failure::{bail, Error};
use std::collections::HashMap;
use std::io::{self, Write};
use std::time::Duration;
fn start_session(channel_proto: &ChannelProtocol) -> Result<(), Error> {
let channel_id = channel_protocol::generate_channel();
println!("Starting shell session -> {}", channel_id);
channel_proto.send(&shell_protocol::messages::spawn::to_cbor(
channel_id,
&"/bin/sh".to_owned(),
None,
)?)?;
run_shell(channel_proto, channel_id)?;
Ok(())
}
fn list_sessions(channel_proto: &ChannelProtocol) -> Result<(), Error> {
channel_proto.send(&shell_protocol::messages::list::to_cbor(
channel_protocol::generate_channel(),
None,
)?)?;
let parsed_msg = shell_protocol::messages::parse_message(
&channel_proto.recv_message(Some(Duration::from_millis(100)))?,
)?;
match parsed_msg {
shell_protocol::messages::Message::List {
channel_id: _channel_id,
process_list,
} => {
let process_list = match process_list {
Some(l) => l,
None => HashMap::<u32, (String, u32)>::new(),
};
if process_list.is_empty() {
println!("\tNo active sessions");
} else {
for (channel_id, (path, pid)) in process_list.iter() {
println!("\t{}\t{{ path = '{}', pid = {} }}", channel_id, path, pid);
}
}
}
_ => bail!("Shell service is not responding correctly".to_owned()),
}
Ok(())
}
fn kill_session(
channel_proto: &ChannelProtocol,
channel_id: u32,
signal: Option<u32>,
) -> Result<(), Error> {
channel_proto.send(&shell_protocol::messages::kill::to_cbor(
channel_id, signal,
)?)?;
Ok(())
}
fn run_shell(channel_proto: &ChannelProtocol, channel_id: u32) -> Result<(), Error> {
println!("Press enter to send input to the shell session");
println!("Press Control-D to detach from the session");
loop {
let mut input = String::new();
print!(" $ ");
let _ = io::stdout().flush();
match io::stdin().read_line(&mut input) {
Ok(n) => {
if n == 0 {
return Ok(());
}
channel_proto.send(&shell_protocol::messages::stdin::to_cbor(
channel_id,
Some(&input),
)?)?;
loop {
match channel_proto.recv_message(Some(Duration::from_millis(100))) {
Ok(m) => match shell_protocol::messages::parse_message(&m) {
Ok(shell_protocol::messages::Message::Stdout {
channel_id: _channel_id,
data: Some(data),
}) => print!("{}", data),
Ok(shell_protocol::messages::Message::Stderr {
channel_id: _channel_id,
data: Some(data),
}) => eprint!("{}", data),
Ok(shell_protocol::messages::Message::Exit { .. }) => {
return Ok(());
}
Ok(shell_protocol::messages::Message::Error {
channel_id: _,
message,
}) => {
eprintln!("Error received from service: {}", message);
return Ok(());
}
_ => {}
},
_ => break,
}
}
}
Err(err) => bail!("Error encountered: {}", err),
}
}
}
fn main() -> Result<(), failure::Error> {
let args = App::new("Shell client")
.subcommand(SubCommand::with_name("start").about("Starts new shell session"))
.subcommand(SubCommand::with_name("list").about("Lists existing shell sessions"))
.subcommand(
SubCommand::with_name("join")
.about("Joins an existing shell session")
.arg(
Arg::with_name("channel_id")
.help("Channel ID of shell session to join")
.short("c")
.takes_value(true)
.required(true),
),
)
.subcommand(
SubCommand::with_name("kill")
.about("Kills an existing shell session")
.arg(
Arg::with_name("channel_id")
.help("Channel ID of shell session to kill")
.short("c")
.takes_value(true)
.required(true),
)
.arg(
Arg::with_name("signal")
.help("Signal to send to shell session")
.short("s")
.takes_value(true),
),
)
.arg(
Arg::with_name("service_ip")
.help("IP address of remote shell service")
.short("i")
.takes_value(true)
.default_value("0.0.0.0"),
)
.arg(
Arg::with_name("service_port")
.help("Port number of remote shell service")
.short("p")
.takes_value(true)
.default_value(shell_protocol::PORT),
)
.setting(AppSettings::SubcommandRequiredElseHelp)
.setting(AppSettings::DeriveDisplayOrder)
.get_matches();
let ip = args.value_of("service_ip").unwrap();
let port = args.value_of("service_port").unwrap();
let remote = format!("{}:{}", ip, port);
let channel_proto =
channel_protocol::ChannelProtocol::new("0.0.0.0", &remote, shell_protocol::CHUNK_SIZE);
println!("Starting shell client -> {}", remote);
match args.subcommand_name() {
Some("start") => start_session(&channel_proto),
Some("list") => {
println!("Fetching existing shell sessions:");
list_sessions(&channel_proto)
}
Some("join") => {
let channel_id = if let Some(kill_args) = args.subcommand_matches("join") {
value_t!(kill_args, "channel_id", u32).unwrap_or_else(|e| e.exit())
} else {
bail!("No arguments found for join");
};
println!("Joining existing shell session: {}", channel_id);
run_shell(&channel_proto, channel_id)
}
Some("kill") => {
let channel_id = if let Some(kill_args) = args.subcommand_matches("kill") {
value_t!(kill_args, "channel_id", u32).unwrap_or_else(|e| e.exit())
} else {
bail!("No arguments found for kill");
};
let signal = if let Some(kill_args) = args.subcommand_matches("kill") {
if let Ok(s) = value_t!(kill_args, "signal", u32) {
if s > 0 && s < 35 {
Some(s)
} else {
bail!("Invalid signal specified");
}
} else {
None
}
} else {
None
};
println!(
"Killing existing shell session {} with signal {}",
channel_id,
signal.unwrap_or(9)
);
kill_session(&channel_proto, channel_id, signal)
}
_ => panic!("Invalid command"),
}
}