From 7d344eb7b18ce8ddaa3df6476120b10ec18c277e Mon Sep 17 00:00:00 2001 From: Maximilien Rigaut Date: Sat, 25 Jun 2022 04:58:04 +0200 Subject: [PATCH] Updated readability --- discordHelperWin.py | 2 +- modules/agnostic_paths.py | 10 +++++- modules/argument_parser.py | 4 +-- modules/main_runner.py | 67 ++++++++++++++++++++++++++++++++++---- modules/spidey_test.py | 17 +++++----- 5 files changed, 82 insertions(+), 18 deletions(-) diff --git a/discordHelperWin.py b/discordHelperWin.py index 051b82e..1b9a7d7 100644 --- a/discordHelperWin.py +++ b/discordHelperWin.py @@ -25,7 +25,7 @@ if __name__ == "__main__": # print(" > No user database was found, the bundled one will be used.") run_db = os.path.join(directory_path, "discordHelper_last_run.json") - argv = f"--all --autodetect --launch --continue --timeout 1 {db_path}--gen-data {run_db}.yaml".split(" ") + argv = f"--all --autodetect --launch --continue --timeout 10 {db_path}--gen-data {run_db}.yaml".split(" ") # print(" > Starting the discordHelper with the following arguments:") # print(" > " + " ".join(argv)) diff --git a/modules/agnostic_paths.py b/modules/agnostic_paths.py index 11b0cc8..477b1c5 100644 --- a/modules/agnostic_paths.py +++ b/modules/agnostic_paths.py @@ -19,6 +19,7 @@ class AgnosticPaths: self.args = args self.ptb = args.ptb self.os = self.get_os() + self.default_database = "" if args.force_path is not None: self.force_path = args.force_path[0] else: @@ -39,10 +40,13 @@ class AgnosticPaths: platform = sys.platform if platform.startswith('win'): self.os = 'windows' + self.default_database = 'windows_base' elif platform.startswith('linux'): self.os = 'linux' + self.default_database = 'linux_base' elif platform.startswith('darwin'): self.os = 'mac' + self.default_database = 'mac_base' else: raise OSError("This script can only check for Spideys on Windows, Linux and Mac.") return self.os @@ -63,13 +67,17 @@ class AgnosticPaths: if os.path.exists(ptb_path): self.ptb = "PTB" self.args.ptb = "PTB" + self.default_database = "windows_ptb" return ptb_path else: + self.default_database = "windows_base" return base_path if self.ptb == 'PTB': + self.default_database = "windows_base" return base_path+"PTB" else: - return base_path+"PTB" # Try to find the PTB version + self.default_database = "windows_ptb" + return base_path # Try to find the default version elif self.os == 'linux': return os.path.join("/", "opt", "discord") # return os.path.join("usr","share","discord") diff --git a/modules/argument_parser.py b/modules/argument_parser.py index 9faed5d..ab906f4 100644 --- a/modules/argument_parser.py +++ b/modules/argument_parser.py @@ -24,10 +24,10 @@ def parse(_args): parser.add_argument("--force-path", nargs=1, default=None, help="Bypass the automatic path discovery with given path") parser.add_argument("--only-known-paths", "--fast", action='store_true', help="Only run the check on the known paths") parser.add_argument("--gen-data", "--generate-data", "--gen", nargs=1, default="", help="Generate data for the analysis") - parser.add_argument("--database", "--db", nargs=1, default="", help="Alternative database for the analysis") + parser.add_argument("--database", "--db", type=str, help="Alternative database for the analysis") parser.add_argument("--pollrate", "--poll", default=0.1, type=float, help="Refresh rate of the status, 0.1 is the default") parser.add_argument("--printrate", "-refresh", default=5, type=int, help="Print the tests every X seconds") - + parser.add_argument("--size", "--window-size", default=90, type=int, help="Size of the message window") # Tests parser.add_argument('--spidey', action='store_true', help='Runs the spidey test') parser.add_argument('--line-count', action='store_true', help='Test all known files for line count') diff --git a/modules/main_runner.py b/modules/main_runner.py index 3a9d3a7..4fd2f5d 100644 --- a/modules/main_runner.py +++ b/modules/main_runner.py @@ -85,10 +85,27 @@ class TestRunner: self.dict_of_processes = {} # To get a more granular view of the processes self.update_module_list() + self.base_data = {} self.finished_tests = deque() + self.open_base_data() self.exec_all_tests() + def open_base_data(self): + """ + Opens the base data file + """ + print(self.args.database) + if os.path.exists(self.args.database): + with open(resource_path(self.args.database), "r") as f: + db_data = f.read() + # print(db_data) + else: # Try opening the default database + print(colored("\n > The database file does not exist, trying to open the default one...\n", "yellow")) + with open(resource_path('databases/windows_base.json'), "r") as f: + db_data = f.read() + # print(db_data) + def update_module_list(self): """ Gets the list of modules to be loaded (in accordance to the config) @@ -114,8 +131,6 @@ class TestRunner: def exec_all_tests(self): """ Executes all the tests, get their result as they finishes - :return: The number of test ran, success, failures, skipped (with ran = success+failures+skip) - :rtype: (int,int,int,int) # TODO: Change this to a nametuple """ self.list_of_tests = [] for module_name, test_class in self.loaded_test_classes.items(): @@ -151,7 +166,6 @@ class TestRunner: tc.daemon = True tc.start() - return self.is_infected, self.detections, self.test_run def controller_test(self): """ @@ -387,6 +401,16 @@ def run_check(args, agnpath): sys.exit(tr.get_exit_code()) +def resource_path(relative_path): + """ Get absolute path to resource, works for dev and for PyInstaller """ + try: + # PyInstaller creates a temp folder and stores path in _MEIPASS + base_path = sys._MEIPASS + except Exception: + base_path = os.path.abspath(".") + return os.path.join(base_path, relative_path) + + def print_final_status(testrunner, start_time, args): """ Prints the final status @@ -399,12 +423,43 @@ def print_final_status(testrunner, start_time, args): color = "red" print(INFECTED_CODE) print(colored(f"\nThe test sequence is now finished, DiscordHelper ran for {round(time.time() - start_time,2)} seconds", color)) - print_status(status, start_time) - + SIZE = args.size + for test, test_object in testrunner.dict_of_processes.items(): + if test_object.status_code == "success": + print(colored(f" > {fill_it(SIZE,test,'SUCCESS')}", "green")) + elif test_object.status_code == "failure": + print(colored(f" > {fill_it(SIZE,test,'FAILURE')}", "red")) + print(colored(f"{fit_it_under(test_object.status ,SIZE-3, ' > ')}", "red")) + else: + print(colored(f" > {test} {fill_it(SIZE,test,test_object.status_code.upper())}", "yellow")) msg_error = "sucessfully" if status.tests_failed == 0 else f"with {status.tests_error} errors" print(colored(f"\nThe program executed {msg_error}, exiting in {args.timeout} seconds...", color)) +def fill_it(size, string1, string2): + """ + Fills a string with a character + """ + return string1 + max(size - len(string1)-len(string2), 0) * "." + string2 + + +def fit_it_under(message, size, start_line=""): + """ + Fits a message under a size + """ + final_message = "" + for line in message.split('\n'): + if len(line) > size: + final_message += start_line + line[:size] + "\n" + final_message += fit_it_under(line[size:], size, start_line) + else: + final_message += start_line + line + "\n" + # final_message += start_line + line + '\n' + # if len(message) > size: + # return message[:size-3] + "..." + return final_message + + def gen_save_data(args, test_runner): """ Generates the save data @@ -416,7 +471,7 @@ def gen_save_data(args, test_runner): if not os.path.exists(folder): print(colored(" > Making folder...", "blue")) os.makedirs(folder) - print(colored(f" > Exporting the test data to {file_path}", "blue")) # TODO : Advertize where the data is exported + print(colored(f" > Exporting the test data to {file_path}", "blue")) main_data = export_data(test_runner, data) # print(main_data) with open(file_path, "w") as f: diff --git a/modules/spidey_test.py b/modules/spidey_test.py index b3fa3f8..4716346 100644 --- a/modules/spidey_test.py +++ b/modules/spidey_test.py @@ -40,16 +40,17 @@ class SpideyTest(test_template.TestTemplate): if lines > 1: self.is_infected = True - # self.is_infected = True - if self.is_infected: # TODO: set a status here, explaining and why the test failed, the scary part will be handled by the main program - return self.finish("failure", "\n!!! WARNING !!!\n" - "Unless you've installed a plugin you really trust, chances are your discord install " - "is corrupted. We suggest uninstalling discord and deleting all its files before reinstalling.\n" - "If you've recently downloaded a .exe directly sent from someone, " - "do also consider a clean windows reinstall.") + if self.is_infected: + return self.finish( + "failure", + "Unless you've installed a plugin you really trust, chances are your discord install " + "is corrupted. We suggest uninstalling discord and deleting all its files before reinstalling.\n" + "If you've recently downloaded a .exe directly sent from someone, " + "do also consider a clean windows reinstall." + ) self.progress = 100 - return self.finish("success") + return self.finish("success", "Your discord is not infected.") def get_path(self): """