Skip to content

Commit

Permalink
refactor: move util files to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
cestef committed Jul 14, 2024
1 parent 9525600 commit eff7806
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 88 deletions.
6 changes: 3 additions & 3 deletions src/cli/interactive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ use serde::{de::DeserializeOwned, Serialize};
use serde_json::Value;
use tokio::sync::Mutex;

use crate::{
runner::scripting::ScriptingResponse,
utils::tree::{tree_data, tree_node, TreeData, TreeNode},
use crate::utils::{
scripting::{tree_data, tree_node, ScriptingResponse},
tree::{TreeData, TreeNode},
};

use super::opts::Opts;
Expand Down
6 changes: 2 additions & 4 deletions src/runner/classic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use std::{

use crate::{
cli::opts::Opts,
runner::scripting::ScriptingResponse,
utils::{
constants::{ERROR, PROGRESS_CHARS, PROGRESS_TEMPLATE, SUCCESS, WARNING},
scripting::{run_scripts, ScriptingResponse},
tree::{Tree, TreeData, UrlType},
},
};
Expand All @@ -22,9 +22,7 @@ use reqwest::Client;
use serde_json::json;
use url::Url;

use super::{
filters::utils::is_directory, scripting::run_scripts, wordlists::ParsedWordlist, Runner,
};
use super::{filters::utils::is_directory, wordlists::ParsedWordlist, Runner};

pub struct Classic {
url: String,
Expand Down
3 changes: 1 addition & 2 deletions src/runner/filters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ use crate::{
check_range,
constants::{ERROR, WARNING},
parse_range_input,
scripting::ScriptingResponse,
},
};

use super::scripting::ScriptingResponse;

pub mod utils;

// Returns true if the response should be kept
Expand Down
7 changes: 4 additions & 3 deletions src/runner/filters/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ use rhai::plugin::*;

use crate::{
cli::opts::Opts,
utils::constants::{ERROR, WARNING},
utils::{
constants::{ERROR, WARNING},
scripting::ScriptingResponse,
},
};
use color_eyre::eyre::Result;

use crate::runner::scripting::ScriptingResponse;

pub fn print_error(
opts: &Opts,
print_fn: impl FnOnce(String) -> Result<()>,
Expand Down
2 changes: 0 additions & 2 deletions src/runner/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
pub mod classic;
pub mod client;
pub mod extract;
pub mod filters;
pub mod recursive;
pub mod scripting;
pub mod spider;
pub mod wordlists;

Expand Down
3 changes: 2 additions & 1 deletion src/runner/recursive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ use crate::{
cli::opts::Opts,
utils::{
constants::{DEFAULT_DEPTH, ERROR, PROGRESS_CHARS, PROGRESS_TEMPLATE, SUCCESS, WARNING},
scripting::{run_scripts, ScriptingResponse},
tree::{Tree, TreeData, TreeNode, UrlType},
},
};

use super::{filters::utils::is_directory, scripting::run_scripts, scripting::ScriptingResponse};
use super::filters::utils::is_directory;

pub struct Recursive {
opts: Opts,
Expand Down
12 changes: 4 additions & 8 deletions src/runner/spider.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
use super::{
extract::{Document, LinkType},
filters::utils::is_directory,
scripting::ScriptingResponse,
Runner,
};
use super::{filters::utils::is_directory, Runner};
use crate::{
cli::opts::Opts,
utils::{
constants::{DEFAULT_DEPTH, ERROR, PROGRESS_CHARS, PROGRESS_TEMPLATE, SUCCESS, WARNING},
tree::{Tree, TreeData, UrlType},
extract::{Document, LinkType},
scripting::{run_scripts, ScriptingResponse},
tree::{Tree, TreeData, TreeNode, UrlType},
},
};
use crate::{runner::scripting::run_scripts, utils::tree::TreeNode};
use color_eyre::eyre::eyre;
use color_eyre::eyre::{Context, Ok, Result};
use colored::Colorize;
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use self::constants::DEFAULT_FILE_TYPE;

pub mod constants;
pub mod display;
pub mod extract;
pub mod logger;
pub mod scripting;
pub mod structs;
pub mod table;
pub mod tree;
Expand Down
65 changes: 61 additions & 4 deletions src/runner/scripting.rs → src/utils/scripting.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::utils::tree::UrlType;
use crate::utils::tree::{TreeData, TreeNode};
use rhai::plugin::*;
use std::collections::BTreeMap;

use crate::{
cli::opts::Opts,
utils::tree::{tree_data, tree_node, TreeData},
};
use crate::cli::opts::Opts;
use color_eyre::eyre::{eyre, Result};
use colored::Colorize;
use indicatif::ProgressBar;
Expand All @@ -13,6 +13,63 @@ use rhai::TypeBuilder;
use rhai::{exported_module, CustomType, Dynamic, Engine, Scope};
use serde::{Deserialize, Serialize};

#[export_module]
pub mod tree_node {

#[rhai_fn(get = "children")]
pub fn children(data: &mut TreeNode<TreeData>) -> Dynamic {
let mut children = Vec::new();
for child in &data.children {
children.push(child.lock().clone());
}
children.into()
}

#[rhai_fn(get = "data")]
pub fn data(data: &mut TreeNode<TreeData>) -> TreeData {
data.data.clone()
}

#[rhai_fn(global)]
pub fn to_string(data: &mut TreeNode<TreeData>) -> String {
format!(
"TreeNode {{ data: {}, children: [{}] }}",
tree_data::to_string(&mut data.data),
data.children.len()
)
}
}

#[export_module]
pub mod tree_data {

#[rhai_fn(global)]
pub fn to_string(data: &mut TreeData) -> String {
format!(
"TreeData {{ url: {}, depth: {}, path: {}, status_code: {}, url_type: {:?}, extra: {:?} }}",
data.url,
data.depth,
data.status_code,
data.path,
match &data.url_type {
UrlType::Directory => "dir",
UrlType::File(ext) => ext,
UrlType::Unknown => "unknown",
UrlType::None => "",
},
data.extra
)
}
#[rhai_fn(get = "response")]
pub fn get_response(data: &mut TreeData) -> Dynamic {
if let Some(response) = &data.response {
Dynamic::from(response.clone())
} else {
Dynamic::UNIT
}
}
}

#[derive(Clone, CustomType, Serialize, Deserialize, Debug, Default)]
pub struct ScriptingResponse {
pub status_code: u16,
Expand Down
64 changes: 3 additions & 61 deletions src/utils/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use colored::Colorize;
use log::{info, warn};
use parking_lot::Mutex;
use ptree::{print_tree, TreeItem};
use rhai::plugin::*;
use rhai::{CustomType, TypeBuilder};
use serde::{Deserialize, Serialize};
use serde_json::Value;
Expand All @@ -12,13 +11,11 @@ use std::{collections::HashMap, sync::Arc};

use crate::{
cli::opts::Opts,
runner::{
scripting::ScriptingResponse,
wordlists::{compute_checksum, ParsedWordlist},
},
utils::get_emoji_for_status_code_colored,
runner::wordlists::{compute_checksum, ParsedWordlist},
utils::{get_emoji_for_status_code_colored, scripting::ScriptingResponse},
Save,
};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TreeNode<T> {
pub data: T,
Expand All @@ -37,61 +34,6 @@ pub struct TreeData {
pub response: Option<ScriptingResponse>,
}

#[export_module]
pub mod tree_node {
#[rhai_fn(get = "children")]
pub fn children(data: &mut TreeNode<TreeData>) -> Dynamic {
let mut children = Vec::new();
for child in &data.children {
children.push(child.lock().clone());
}
children.into()
}

#[rhai_fn(get = "data")]
pub fn data(data: &mut TreeNode<TreeData>) -> TreeData {
data.data.clone()
}

#[rhai_fn(global)]
pub fn to_string(data: &mut TreeNode<TreeData>) -> String {
format!(
"TreeNode {{ data: {}, children: [{}] }}",
tree_data::to_string(&mut data.data),
data.children.len()
)
}
}

#[export_module]
pub mod tree_data {
#[rhai_fn(global)]
pub fn to_string(data: &mut TreeData) -> String {
format!(
"TreeData {{ url: {}, depth: {}, path: {}, status_code: {}, url_type: {:?}, extra: {:?} }}",
data.url,
data.depth,
data.status_code,
data.path,
match &data.url_type {
UrlType::Directory => "dir",
UrlType::File(ext) => ext,
UrlType::Unknown => "unknown",
UrlType::None => "",
},
data.extra
)
}
#[rhai_fn(get = "response")]
pub fn get_response(data: &mut TreeData) -> Dynamic {
if let Some(response) = &data.response {
Dynamic::from(response.clone())
} else {
Dynamic::UNIT
}
}
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub enum UrlType {
Directory,
Expand Down

0 comments on commit eff7806

Please sign in to comment.