Move run_command_async to bot-coreutils
Signed-off-by: trivernis <trivernis@protonmail.com>pull/7/head
parent
0bbbf3594f
commit
d2ea577762
@ -1,5 +1,6 @@
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
||||||
|
pub mod process;
|
||||||
/// Utilities to quickly check strings that represent urls
|
/// Utilities to quickly check strings that represent urls
|
||||||
pub mod url;
|
pub mod url;
|
||||||
|
@ -0,0 +1,24 @@
|
|||||||
|
use std::io;
|
||||||
|
use std::process::Stdio;
|
||||||
|
use tokio::io::AsyncReadExt;
|
||||||
|
use tokio::process::Command;
|
||||||
|
|
||||||
|
/// Asynchronously runs a given command and returns the output
|
||||||
|
pub async fn run_command_async(command: &str, args: &[&str]) -> io::Result<String> {
|
||||||
|
log::trace!("Running command '{}' with args {:?}", command, args);
|
||||||
|
let cmd = Command::new(command)
|
||||||
|
.args(args)
|
||||||
|
.stdout(Stdio::piped())
|
||||||
|
.stderr(Stdio::piped())
|
||||||
|
.spawn()?;
|
||||||
|
let mut stderr = String::new();
|
||||||
|
let mut output = String::new();
|
||||||
|
cmd.stderr.unwrap().read_to_string(&mut stderr).await?;
|
||||||
|
if stderr.len() != 0 {
|
||||||
|
log::debug!("STDERR of command {}: {}", command, stderr);
|
||||||
|
}
|
||||||
|
cmd.stdout.unwrap().read_to_string(&mut output).await?;
|
||||||
|
log::trace!("Command output is {}", output);
|
||||||
|
|
||||||
|
Ok(output)
|
||||||
|
}
|
@ -1,9 +1,12 @@
|
|||||||
use crate::utils::error::BotResult;
|
use crate::utils::error::BotResult;
|
||||||
use crate::utils::process::{run_command_async, sanitize_argument};
|
use bot_coreutils::process::run_command_async;
|
||||||
|
|
||||||
/// Runs the qalc command with the given expression
|
/// Runs the qalc command with the given expression
|
||||||
pub async fn qalc(expression: &str) -> BotResult<String> {
|
pub async fn qalc(expression: &str) -> BotResult<String> {
|
||||||
let expression = sanitize_argument(expression, true)?;
|
let result = run_command_async(
|
||||||
let result = run_command_async("qalc", &["-m", "1000", &*expression]).await?;
|
"qalc",
|
||||||
|
&["-m", "1000", format!("\"{}\"", &*expression).as_str()],
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
Ok(result)
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
@ -1,51 +0,0 @@
|
|||||||
use crate::utils::error::{BotError, BotResult};
|
|
||||||
use regex::Regex;
|
|
||||||
use std::io;
|
|
||||||
use std::process::Stdio;
|
|
||||||
use tokio::io::AsyncReadExt;
|
|
||||||
use tokio::process::Command;
|
|
||||||
|
|
||||||
/// Asynchronously runs a given command and returns the output
|
|
||||||
pub async fn run_command_async(command: &str, args: &[&str]) -> io::Result<String> {
|
|
||||||
log::trace!("Running command '{}' with args {:?}", command, args);
|
|
||||||
let cmd = Command::new(command)
|
|
||||||
.args(args)
|
|
||||||
.stdout(Stdio::piped())
|
|
||||||
.stderr(Stdio::piped())
|
|
||||||
.spawn()?;
|
|
||||||
let mut stderr = String::new();
|
|
||||||
let mut output = String::new();
|
|
||||||
cmd.stderr.unwrap().read_to_string(&mut stderr).await?;
|
|
||||||
if stderr.len() != 0 {
|
|
||||||
log::debug!("STDERR of command {}: {}", command, stderr);
|
|
||||||
}
|
|
||||||
cmd.stdout.unwrap().read_to_string(&mut output).await?;
|
|
||||||
log::trace!("Command output is {}", output);
|
|
||||||
|
|
||||||
Ok(output)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Sanitizes a command line argument and throws an error
|
|
||||||
/// on a possible injection attempt
|
|
||||||
pub fn sanitize_argument(arg: &str, detect_help: bool) -> BotResult<String> {
|
|
||||||
log::debug!("Sanitizing argument '{}'", arg);
|
|
||||||
lazy_static::lazy_static! {
|
|
||||||
static ref HELP_FLAG: Regex = Regex::new(r"^\s*(-*)h(elp)?\s*$").unwrap();
|
|
||||||
static ref FLAG_REGEX: Regex = Regex::new(r"^\s*-\w*\s*$").unwrap();
|
|
||||||
}
|
|
||||||
if FLAG_REGEX.is_match(arg) {
|
|
||||||
log::debug!("Detected STDIN injection");
|
|
||||||
return Err(BotError::CliInject);
|
|
||||||
}
|
|
||||||
if detect_help && HELP_FLAG.is_match(arg) {
|
|
||||||
log::debug!("Detected help injection");
|
|
||||||
return Err(BotError::CliInject);
|
|
||||||
}
|
|
||||||
let arg = arg.replace("--", "\\-\\-");
|
|
||||||
if arg.is_empty() {
|
|
||||||
return Err(BotError::CliInject);
|
|
||||||
}
|
|
||||||
log::debug!("Sanitized argument is '{}'", arg);
|
|
||||||
|
|
||||||
Ok(arg)
|
|
||||||
}
|
|
Loading…
Reference in New Issue