|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | +Defines basic scripts runner |
| 5 | +""" |
| 6 | + |
| 7 | +from concurrent.futures import ThreadPoolExecutor |
| 8 | +from functools import partial |
| 9 | +from importlib.machinery import SourceFileLoader |
| 10 | +from logging import basicConfig, INFO |
| 11 | +from pathlib import Path |
| 12 | +from types import ModuleType |
| 13 | + |
| 14 | +from src.core.utils.response import ScriptResponse |
| 15 | + |
| 16 | +basicConfig(level=INFO) |
| 17 | + |
| 18 | + |
| 19 | +class Defaults: |
| 20 | + SCRIPTS_BASE = Path("src/scripts/") |
| 21 | + |
| 22 | + |
| 23 | +class ScriptRunnerPaths: |
| 24 | + OSINT = Defaults.SCRIPTS_BASE.joinpath("osint") |
| 25 | + RECON = Defaults.SCRIPTS_BASE.joinpath("recon") |
| 26 | + CONVERT = Defaults.SCRIPTS_BASE.joinpath("convert") |
| 27 | + OTHER = Defaults.SCRIPTS_BASE.joinpath("other") |
| 28 | + |
| 29 | + |
| 30 | +class ScriptRunner: |
| 31 | + def __init__(self): |
| 32 | + self.scripts = {} |
| 33 | + self.results = {} |
| 34 | + |
| 35 | + def exec_script( |
| 36 | + self, |
| 37 | + path: str or Path, |
| 38 | + script_class: str = "Runner", |
| 39 | + function: str = "run", |
| 40 | + args: list or None = None, |
| 41 | + kwargs: dict or None = None, |
| 42 | + ) -> ScriptResponse: |
| 43 | + """ |
| 44 | + Load and exec python script |
| 45 | + :param path: name of the script to load |
| 46 | + :param script_class: class to initialize when script is started |
| 47 | + :param function: name of the function to run from script |
| 48 | + :param args: args to pass into the module |
| 49 | + :param kwargs: kwargs to pass into the module |
| 50 | + :return: result of the function |
| 51 | + """ |
| 52 | + if args is None: |
| 53 | + args = [] |
| 54 | + if kwargs is None: |
| 55 | + kwargs = {} |
| 56 | + loader = SourceFileLoader(fullname=script_class, path=str(path)) |
| 57 | + module = ModuleType(name=loader.name) |
| 58 | + loader.exec_module(module) |
| 59 | + class_instance = getattr(module, script_class)(logger=path.parent.stem) |
| 60 | + try: |
| 61 | + result = getattr(class_instance, function)(*args, **kwargs) |
| 62 | + except Exception as unexp_err: |
| 63 | + result = ScriptResponse.error(message=str(unexp_err)) |
| 64 | + result.update({"script": Path(path).parent.stem}) |
| 65 | + return result |
| 66 | + |
| 67 | + def get_scripts(self) -> dict: |
| 68 | + """ |
| 69 | + Load the dictionary with all of the scripts |
| 70 | + :return: dict {'dir':['script1', 'script2', ...]} |
| 71 | + """ |
| 72 | + for directory in [ |
| 73 | + ScriptRunnerPaths.OSINT, |
| 74 | + ScriptRunnerPaths.RECON, |
| 75 | + ScriptRunnerPaths.OTHER, |
| 76 | + ScriptRunnerPaths.CONVERT, |
| 77 | + ]: |
| 78 | + self.scripts.update({directory.stem: list()}) |
| 79 | + for file in directory.glob("*/__main__.py"): |
| 80 | + self.scripts[directory.stem].append(file) |
| 81 | + return self.scripts |
| 82 | + |
| 83 | + def run_category(self, category: str, *args, **kwargs) -> None: |
| 84 | + """ |
| 85 | + Run a category with scripts |
| 86 | + :return: nothing |
| 87 | + """ |
| 88 | + if not self.scripts: |
| 89 | + self.get_scripts() |
| 90 | + futures = [] |
| 91 | + with ThreadPoolExecutor(max_workers=10) as executor: |
| 92 | + for script in self.scripts.get(category, []): |
| 93 | + futures.append( |
| 94 | + executor.submit( |
| 95 | + fn=partial( |
| 96 | + self.exec_script, path=script, args=args, kwargs=kwargs |
| 97 | + ) |
| 98 | + ) |
| 99 | + ) |
| 100 | + for future in futures: |
| 101 | + result = future.result() |
| 102 | + self.results.update({result.get("script"): result}) |
| 103 | + |
| 104 | + def get_results(self) -> dict: |
| 105 | + """ |
| 106 | + Return retrieved results |
| 107 | + :return: |
| 108 | + """ |
| 109 | + return self.results |
0 commit comments