Actually execute whatever commands the agent receives 😱
This commit is contained in:
parent
6d963464cf
commit
b47b1943cd
|
@ -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"] }
|
||||
|
|
|
@ -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())
|
||||
}
|
||||
|
||||
|
|
|
@ -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)]
|
||||
|
|
Loading…
Reference in New Issue