Files
adcs_api
cbor_protocol
channel_protocol
clyde_3g_eps_api
clyde_3g_eps_service
comms_service
db_test
eps_api
example_rust_c_service
example_rust_service
extern_lib
file_protocol
file_service
iobc_supervisor_service
isis_ants
isis_ants_api
isis_ants_service
isis_imtq_api
isis_iobc_supervisor
kubos_app
kubos_app_service
kubos_build_helper
kubos_file_client
kubos_service
kubos_shell_client
kubos_system
kubos_telemetry_db
large_download
large_upload
local_comms_service
mai400
mai400_api
mai400_service
monitor_service
novatel_oem6_api
novatel_oem6_service
nsl_duplex_d2
nsl_duplex_d2_comms_service
obc_hs
radio_api
rust_i2c
rust_mission_app
rust_uart
scheduler_service
serial_comms_service
shell_protocol
shell_service
telemetry_service
uart_comms_client
udp_client
utils
  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
//
// Copyright (C) 2018 Kubos Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License")
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#![deny(warnings)]
use channel_protocol::{ChannelProtocol, ProtocolError};
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 run_command(channel_proto: &ChannelProtocol, command: &str) -> Result<(), Error> {
    let channel_id = channel_protocol::generate_channel();
    let command = format!("{}\n", command);

    channel_proto.send(&shell_protocol::messages::spawn::to_cbor(
        channel_id,
        &"/bin/sh".to_owned(),
        None,
    )?)?;

    channel_proto.send(&shell_protocol::messages::stdin::to_cbor(
        channel_id,
        Some(&command),
    )?)?;

    loop {
        let recvd_data = match channel_proto.recv_message(Some(Duration::from_millis(100))) {
            Ok(data) => data,
            Err(ProtocolError::ReceiveTimeout) => break,
            Err(e) => {
                eprintln!("Recv Error: {}", e);
                break;
            }
        };
        let parsed_msg = match shell_protocol::messages::parse_message(&recvd_data) {
            Ok(msg) => msg,
            Err(e) => {
                eprintln!("Msg Parse Error: {}", e);
                break;
            }
        };

        match parsed_msg {
            shell_protocol::messages::Message::Exit { .. } => {
                break;
            }
            shell_protocol::messages::Message::Stdout { data, .. } => {
                if let Some(data) = data {
                    print!("{}", data);
                }
            }
            shell_protocol::messages::Message::Stderr { data, .. } => {
                if let Some(data) = data {
                    print!("{}", data);
                }
            }
            shell_protocol::messages::Message::Error { message, .. } => {
                eprintln!("Shell Protocol Error: {}", message);
                break;
            }
            _ => {}
        }
    }

    channel_proto.send(&shell_protocol::messages::kill::to_cbor(channel_id, None)?)?;

    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),
                )?)?;

                while let Ok(m) = channel_proto.recv_message(Some(Duration::from_millis(100))) {
                    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 { message, .. }) => {
                            eprintln!("Error received from service: {}", message);
                            return Ok(());
                        }
                        _ => {}
                    }
                }
            }
            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),
                ),
        )
        .subcommand(
            SubCommand::with_name("run")
                .about("Runs a single remote command")
                .arg(
                    Arg::with_name("command")
                        .help("Remote command to execute")
                        .short("c")
                        .takes_value(true)
                        .required(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("run") => {
            let command = if let Some(command_matches) = args.subcommand_matches("run") {
                command_matches.value_of("command").unwrap()
            } else {
                bail!("No arguments found for run");
            };

            println!("Running remote command: '{}'", &command);
            run_command(&channel_proto, command)
        }
        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"),
    }
}