From 1e5f515a2510f86ee3aadabaf9739f9bdcb5add9 Mon Sep 17 00:00:00 2001 From: Andrew Rioux Date: Mon, 4 Sep 2023 19:10:43 -0400 Subject: [PATCH] feat: adding packet handling to server --- .../src/commands/connect/commands/help.rs | 27 +++++++++ .../src/commands/connect/commands/mod.rs | 1 + .../src/commands/connect/commands/sysinfo.rs | 6 +- .../src/commands/connect/shell.rs | 59 +++++++++++-------- sparse-05/sparse-05-common/src/lib.rs | 5 +- sparse-05/sparse-05-server/src/connection.rs | 43 ++++++++++++++ .../src/connection/command.rs | 0 7 files changed, 112 insertions(+), 29 deletions(-) create mode 100644 sparse-05/sparse-05-client/src/commands/connect/commands/help.rs create mode 100644 sparse-05/sparse-05-server/src/connection/command.rs diff --git a/sparse-05/sparse-05-client/src/commands/connect/commands/help.rs b/sparse-05/sparse-05-client/src/commands/connect/commands/help.rs new file mode 100644 index 0000000..e290d5d --- /dev/null +++ b/sparse-05/sparse-05-client/src/commands/connect/commands/help.rs @@ -0,0 +1,27 @@ +use ansi_term::{Color, Style}; +use structopt::StructOpt; + +use crate::commands::connect::shell::SparseCommands; + +pub fn print_help(arg: Option) { + match arg { + Some(SparseCommands::Exit) => println!( + "Exits from the shell and disconnects from the binary" + ), + Some(SparseCommands::SysInfo) => println!( + "Prints system information from the system you are connecting to" + ), + None => println!( + "\n{}{}\n\ + \n\ + The following shell commands are available:\n\ + \n\ + - #sysinfo\t\tprint information about the system you are connecting to + - #help\t\tprints this help page, or alternatively prints info about a command passed as an argument\n\ + - #exit\t\texit from the shell and disconnect from the binary\n\ + ", + Style::new().bold().paint("SHELL COMMANDS"), + Style::new().paint(""), + ) + } +} diff --git a/sparse-05/sparse-05-client/src/commands/connect/commands/mod.rs b/sparse-05/sparse-05-client/src/commands/connect/commands/mod.rs index 4c0d66f..35bdc0c 100644 --- a/sparse-05/sparse-05-client/src/commands/connect/commands/mod.rs +++ b/sparse-05/sparse-05-client/src/commands/connect/commands/mod.rs @@ -1 +1,2 @@ +pub mod help; pub mod sysinfo; diff --git a/sparse-05/sparse-05-client/src/commands/connect/commands/sysinfo.rs b/sparse-05/sparse-05-client/src/commands/connect/commands/sysinfo.rs index b64e3ea..c51e34b 100644 --- a/sparse-05/sparse-05-client/src/commands/connect/commands/sysinfo.rs +++ b/sparse-05/sparse-05-client/src/commands/connect/commands/sysinfo.rs @@ -1,10 +1,10 @@ use std::net::IpAddr; +use ansi_term::Colour as Color; + use sparse_05_common::messages::{Capabilities, OperatingSystem}; pub fn print_capabilities(capabilities: &Capabilities, ip: &IpAddr) { - use ansi_term::Colour as Color; - println!("Capabilities of remote host:"); println!( "\tOperating system: \t{}", @@ -69,6 +69,6 @@ pub fn print_capabilities(capabilities: &Capabilities, ip: &IpAddr) { println!( "\tHost name (IP): \t{} ({})", capabilities.hostname.as_deref().unwrap_or(""), - ip.ip() + ip ); } diff --git a/sparse-05/sparse-05-client/src/commands/connect/shell.rs b/sparse-05/sparse-05-client/src/commands/connect/shell.rs index aef88aa..6aa1369 100644 --- a/sparse-05/sparse-05-client/src/commands/connect/shell.rs +++ b/sparse-05/sparse-05-client/src/commands/connect/shell.rs @@ -1,15 +1,23 @@ use std::{ io::{self, Read, Write}, os::fd::AsRawFd, - path::PathBuf, sync::Arc, }; use sparse_05_common::messages::Capabilities; -use tokio::net::UdpSocket; +use structopt::StructOpt; use super::{commands, Connection}; +#[derive(StructOpt)] +#[structopt()] +pub enum SparseCommands { + #[structopt(name = "#sysinfo")] + SysInfo, + #[structopt(name = "#exit")] + Exit, +} + macro_rules! libc_try { ($expr:expr) => { if unsafe { $expr } == -1 { @@ -41,6 +49,7 @@ pub(super) async fn shell( connection: Arc, mut capabilities: Capabilities, ) -> anyhow::Result<()> { + println!("Source code available at https://github.com/r-a303931/sparse (feel free to give it a star!)"); println!("Type #help to view a list of sparse commands\n"); let mut stdin = io::stdin(); @@ -49,20 +58,6 @@ pub(super) async fn shell( let mut raw_term_attrs = get_term_attrs(&stdin)?; convert_termios_raw(&mut raw_term_attrs)?; - macro_rules! cmd_matcher { - ($input:expr, $(($check:ident, $matcher:expr) => {$($body:tt)*}),+, _ => {$($ebody:tt)*}) => { - match &$input[..] { - $($check if $check.len() >= $matcher.len() && $check[..$matcher.len()] == $matcher[..] => { - let $check = &$check[$matcher.len()..]; - $($body)* - }),+ - _ => { - $($ebody)* - } - } - } - } - let mut cwd = "/".to_string(); loop { @@ -85,17 +80,33 @@ pub(super) async fn shell( break; } - cmd_matcher!( - cmd[..amount], - (_sysinfo, b"#sysinfo") => { + let Ok(input) = std::str::from_utf8(&cmd[..amount]) else { + continue; + }; + + let (args, help) = if input.starts_with("#help") { + (input.split(" ").collect::>(), true) + } else { + ( + [&[""][..], &input.split(" ").collect::>()].concat(), + false, + ) + }; + + let parsed = SparseCommands::from_iter_safe(args.iter()); + + match (parsed, help) { + (help_info, true) => { + commands::help::print_help(help_info.ok()); + } + (Ok(SparseCommands::SysInfo), _) => { commands::sysinfo::print_capabilities(&capabilities, &connection.ip.ip()) - }, - (_help, b"#help") => {}, - (_exit, b"#exit") => { + } + (Ok(SparseCommands::Exit), _) => { break; - }, + } _ => {} - ); + } } Ok(()) diff --git a/sparse-05/sparse-05-common/src/lib.rs b/sparse-05/sparse-05-common/src/lib.rs index 2dbf989..10a141b 100644 --- a/sparse-05/sparse-05-common/src/lib.rs +++ b/sparse-05/sparse-05-common/src/lib.rs @@ -20,7 +20,8 @@ pub mod messages { OpenTTY, CloseTTY(u64), - SendRawData(Vec, u64), + SendTTYData(u64, Vec), + SendTTYSignal(u64, u64), StartUploadFile(PathBuf, u64), SendFileSegment(u64, u64, Vec), @@ -69,7 +70,7 @@ pub mod messages { OpenedTTY(u64), ClosedTTY(u64), - SendRawData(Vec, u64), + SendTTYData(u64, Vec), UploadFileID(u64), UploadFileStatus(u64, Result<(), Vec>), diff --git a/sparse-05/sparse-05-server/src/connection.rs b/sparse-05/sparse-05-server/src/connection.rs index 4b3b21f..e122d9b 100644 --- a/sparse-05/sparse-05-server/src/connection.rs +++ b/sparse-05/sparse-05-server/src/connection.rs @@ -1,4 +1,5 @@ use std::{ + collections::HashMap, net::{Ipv4Addr, UdpSocket}, sync::{ mpsc::{channel, Receiver, Sender}, @@ -214,6 +215,8 @@ fn authenticate( handle_full_connection(capabilities, packet_handler, conninfo, close) } +mod command; + fn handle_full_connection( capabilities: Arc, packet_handler: Receiver, @@ -223,8 +226,48 @@ fn handle_full_connection( where F: Fn(), { + use packets::*; + + let mut commands = HashMap::new(); + let mut uploaded_files = HashMap::new(); + let mut downloaded_files = HashMap::new(); + loop { let msg = packet_handler.recv()?; + let pkt = msg.pkt(); + + let Layer3Pkt::IPv4Pkt(ip_pkt) = pkt.get_layer3_pkt()?; + let Layer4Pkt::UDP(udp_pkt) = ip_pkt.get_layer4_packet()?; + + if ip_pkt.source_ip() != conninfo.srcip || udp_pkt.srcport() != conninfo.srcport { + continue; + } + + let Ok(data) = conninfo.try_decrypt_and_verify_comm(udp_pkt.get_data()) else { + continue; + }; + + match data { + Command::RunCommand(_) => {} + Command::SendStdin(_, _) => {} + + Command::Cd(_) => {} + Command::Ls(_) => {} + + Command::OpenTTY => {} + Command::CloseTTY(_) => {} + Command::SendTTYData(_, _) => {} + Command::SendTTYSignal(_, _) => {} + + Command::StartUploadFile(_, _) => {} + Command::SendFileSegment(_, _, _) => {} + Command::StartDownloadFile(_) => {} + Command::DownloadFileStatus(_) => {} + + Command::Disconnect => { + break; + } + } } close(); diff --git a/sparse-05/sparse-05-server/src/connection/command.rs b/sparse-05/sparse-05-server/src/connection/command.rs new file mode 100644 index 0000000..e69de29