Skip to content

Commit 6a1b49e

Browse files
bee-sanbee
andauthored
Add custom script directory (#758)
* Add custom script directory * run just --------- Co-authored-by: bee <[email protected]>
1 parent c123457 commit 6a1b49e

File tree

2 files changed

+63
-10
lines changed

2 files changed

+63
-10
lines changed

fixtures/.rustscan_scripts.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
tags = ["core_approved", "example"]
2+
ports = ["80", "443", "8080"]
3+
developer = ["example", "https://example.org"]
4+
directory = "fixtures/.rustscan_scripts"

src/scripts/mod.rs

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,21 @@ pub fn init_scripts(scripts: &ScriptsRequired) -> Result<Vec<ScriptFile>> {
108108
scripts_to_run.push(default_script);
109109
}
110110
ScriptsRequired::Custom => {
111-
let scripts_dir_base =
112-
dirs::home_dir().ok_or_else(|| anyhow!("Could not infer scripts path."))?;
113-
let script_paths = find_scripts(scripts_dir_base)?;
111+
let script_config = ScriptConfig::read_config()?;
112+
debug!("Script config \n{:?}", script_config);
113+
114+
let script_dir_base = if let Some(config_directory) = &script_config.directory {
115+
PathBuf::from(config_directory)
116+
} else {
117+
dirs::home_dir().ok_or_else(|| anyhow!("Could not infer scripts path."))?
118+
};
119+
120+
let script_paths = find_scripts(script_dir_base)?;
114121
debug!("Scripts paths \n{:?}", script_paths);
115122

116123
let parsed_scripts = parse_scripts(script_paths);
117124
debug!("Scripts parsed \n{:?}", parsed_scripts);
118125

119-
let script_config = ScriptConfig::read_config()?;
120-
debug!("Script config \n{:?}", script_config);
121-
122126
// Only Scripts that contain all the tags found in ScriptConfig will be selected.
123127
if let Some(config_hashset) = script_config.tags {
124128
for script in parsed_scripts {
@@ -316,8 +320,7 @@ fn execute_script(script: &str) -> Result<String> {
316320
}
317321
}
318322

319-
pub fn find_scripts(mut path: PathBuf) -> Result<Vec<PathBuf>> {
320-
path.push(".rustscan_scripts");
323+
pub fn find_scripts(path: PathBuf) -> Result<Vec<PathBuf>> {
321324
if path.is_dir() {
322325
debug!("Scripts folder found {}", &path.display());
323326
let mut files_vec: Vec<PathBuf> = Vec::new();
@@ -382,6 +385,7 @@ pub struct ScriptConfig {
382385
pub tags: Option<Vec<String>>,
383386
pub ports: Option<Vec<String>>,
384387
pub developer: Option<Vec<String>>,
388+
pub directory: Option<String>,
385389
}
386390

387391
#[cfg(not(tarpaulin_include))]
@@ -400,7 +404,7 @@ impl ScriptConfig {
400404

401405
#[cfg(test)]
402406
mod tests {
403-
use super::{find_scripts, parse_scripts, Script, ScriptFile};
407+
use super::*;
404408

405409
// Function for testing only, it inserts static values into ip and open_ports
406410
// Doesn't use impl in case it's implemented in the super module at some point
@@ -418,7 +422,7 @@ mod tests {
418422

419423
#[test]
420424
fn find_and_parse_scripts() {
421-
let scripts = find_scripts("fixtures/".into()).unwrap();
425+
let scripts = find_scripts("fixtures/.rustscan_scripts".into()).unwrap();
422426
let scripts = parse_scripts(scripts);
423427
assert_eq!(scripts.len(), 4);
424428
}
@@ -515,4 +519,49 @@ mod tests {
515519
// output has a newline at the end by default, .trim() trims it
516520
assert_eq!(output.trim(), "Total args passed to fixtures/.rustscan_scripts/test_script.pl : 2\nArg # 1 : 127.0.0.1\nArg # 2 : 80,8080");
517521
}
522+
523+
#[test]
524+
fn test_custom_directory_config() {
525+
// Create test config
526+
let config_str = r#"
527+
tags = ["core_approved", "example"]
528+
directory = "fixtures/.rustscan_scripts"
529+
"#;
530+
531+
let config: ScriptConfig = toml::from_str(config_str).unwrap();
532+
assert_eq!(
533+
config.directory,
534+
Some("fixtures/.rustscan_scripts".to_string())
535+
);
536+
537+
// Test that the directory is actually used
538+
let script_dir_base = PathBuf::from(config.directory.unwrap());
539+
let scripts = find_scripts(script_dir_base).unwrap();
540+
541+
// Verify we found the test script
542+
assert!(scripts.iter().any(|p| p
543+
.file_name()
544+
.and_then(|f| f.to_str())
545+
.map(|s| s == "test_script.txt")
546+
.unwrap_or(false)));
547+
}
548+
549+
#[test]
550+
fn test_default_directory_fallback() {
551+
let config_str = r#"
552+
tags = ["core_approved", "example"]
553+
"#;
554+
555+
let config: ScriptConfig = toml::from_str(config_str).unwrap();
556+
assert_eq!(config.directory, None);
557+
558+
// Test fallback to home directory
559+
let script_dir_base = if let Some(config_directory) = &config.directory {
560+
PathBuf::from(config_directory)
561+
} else {
562+
dirs::home_dir().unwrap()
563+
};
564+
565+
assert_eq!(script_dir_base, dirs::home_dir().unwrap());
566+
}
518567
}

0 commit comments

Comments
 (0)