Skip to content

Commit

Permalink
execute the target command
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyYe committed Mar 1, 2024
1 parent 5057d6e commit 49c66b9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
19 changes: 19 additions & 0 deletions src/executor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::process::Command;

pub fn execute_command(command: &str) -> Result<(), Box<dyn std::error::Error>> {
let mut parts = command.split_whitespace();
let command = parts.next().unwrap();
let args = parts;

Command::new(command).args(args).spawn()?.wait()?;

Ok(())
}

mod tests {
#[test]
fn test_execute_command() {
let result = crate::executor::execute_command("ls -lh ./");
assert!(result.is_ok());
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use sequence::PortSequenceDetector;
use server::Server;

mod config;
mod executor;
mod sequence;
mod server;

Expand Down
36 changes: 30 additions & 6 deletions src/sequence/port_sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,44 @@ use std::thread;
use std::time::{SystemTime, UNIX_EPOCH};

use crate::config::Config;
use crate::executor;
use crate::sequence::SequenceDetector;

#[derive(Debug)]
pub struct PortSequenceDetector {
timeout: u64,
sequence_set: HashSet<i32>,
sequence_rules: Vec<Vec<i32>>,
sequence_rules: HashMap<String, Vec<i32>>,
rules_map: HashMap<String, String>,
client_sequences: Arc<Mutex<HashMap<String, Vec<i32>>>>,
client_timeout: Arc<Mutex<HashMap<String, u64>>>,
}

impl PortSequenceDetector {
#[must_use]
pub fn new(config: Config) -> PortSequenceDetector {
let mut sequence_rules = Vec::new();
let mut sequence_rules = HashMap::new();
for rule in config.rules.clone() {
sequence_rules.push(rule.sequence);
sequence_rules.insert(rule.name, rule.sequence);
}

let mut sequence_set = HashSet::new();
for rule in config.rules {
for rule in config.rules.clone() {
for sequence in rule.sequence {
sequence_set.insert(sequence);
}
}

let mut rules_map = HashMap::new();
for rule in config.rules {
rules_map.insert(rule.name, rule.command);
}

PortSequenceDetector {
timeout: config.timeout,
sequence_set,
sequence_rules,
rules_map,
client_sequences: Arc::new(Mutex::new(HashMap::new())),
client_timeout: Arc::new(Mutex::new(HashMap::new())),
}
Expand Down Expand Up @@ -76,11 +84,27 @@ impl SequenceDetector for PortSequenceDetector {
let mut client_sequence = self.client_sequences.lock().unwrap();
let client_sequence = client_sequence.get_mut(client_ip);
if let Some(sequence) = client_sequence {
for rule in &self.sequence_rules {
if sequence.ends_with(rule) {
for (name, rule) in &self.sequence_rules {
if sequence.ends_with(&rule) {
println!("Matched knock sequence: {:?} from: {}", rule, client_ip);
// clear the sequence
sequence.clear();

// execute the command
let command = self.rules_map.get(name).unwrap();
let formatted_cmd = command.replace("%IP%", client_ip);
println!("Executing command: {}", formatted_cmd);

// format the command with the client ip
match executor::execute_command(&formatted_cmd) {
Ok(_) => {
println!("Command executed successfully");
}
Err(e) => {
println!("Error executing command: {:?}", e);
}
}

return true;
}
}
Expand Down

0 comments on commit 49c66b9

Please sign in to comment.