feat: adding packet handling to server
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
use ansi_term::{Color, Style};
|
||||
use structopt::StructOpt;
|
||||
|
||||
use crate::commands::connect::shell::SparseCommands;
|
||||
|
||||
pub fn print_help(arg: Option<SparseCommands>) {
|
||||
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(""),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
pub mod help;
|
||||
pub mod sysinfo;
|
||||
|
||||
@@ -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("<unknown>"),
|
||||
ip.ip()
|
||||
ip
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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<Connection>,
|
||||
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::<Vec<_>>(), true)
|
||||
} else {
|
||||
(
|
||||
[&[""][..], &input.split(" ").collect::<Vec<_>>()].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(())
|
||||
|
||||
Reference in New Issue
Block a user