Actually execute whatever commands the agent receives 😱

This commit is contained in:
R Tyler Croy 2023-01-28 17:08:53 -08:00
parent 6d963464cf
commit b47b1943cd
No known key found for this signature in database
GPG Key ID: E5C92681BEF6CEA2
3 changed files with 31 additions and 4 deletions

View File

@ -20,6 +20,7 @@ driftwood = "0"
handlebars = { version = "~3.4.0", features = ["dir_source"] }
html-escape = "~0.2.6"
log = "~0.4.8"
os_pipe = "1"
pretty_env_logger = "~0.3.1"
serde_json = "~1.0.0"
sqlx = { version = "~0.5.1", features = ["chrono", "json", "migrate", "offline", "sqlite", "uuid", "runtime-async-std-rustls"] }

View File

@ -33,8 +33,34 @@ mod routes {
* This will take in the commands to actually execute
*/
pub async fn execute(mut req: Request<()>) -> Result<Body, tide::Error> {
let commands: CommandRequest = req.body_json().await?;
debug!("Commands to exec: {:?}", commands);
let c: CommandRequest = req.body_json().await?;
debug!("Commands to exec: {:?}", c);
for command in c.commands.iter() {
use os_pipe::pipe;
use std::io::{BufRead, BufReader};
use std::process::Command;
let mut cmd = Command::new("sh");
cmd.args(["-c", &command.script]);
let (reader, writer) = pipe().unwrap();
let writer_clone = writer.try_clone().unwrap();
cmd.stdout(writer);
cmd.stderr(writer_clone);
let mut handle = cmd.spawn()?;
drop(cmd);
debug!("executing: {}", &command.script);
let bufr = BufReader::new(reader);
for line in bufr.lines() {
if let Ok(buffer) = line {
debug!("output: {}", buffer);
}
}
let status = handle.wait()?;
debug!("status of {}: {:?}", &command.script, status);
}
Ok("{}".into())
}

View File

@ -21,12 +21,12 @@ struct CapsResponse {
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
pub struct Command {
script: String,
pub script: String,
}
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
pub struct CommandRequest {
commands: Vec<Command>,
pub commands: Vec<Command>,
}
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]