def __valid_address(self, data: Data): """ This function checks if the URL, port and IP are valid @param data: The data object of the program. @type data: Data @return: None """ port: int = data.port url: str = data.url ip: str = data.ip scheme: str = "http" path: str = "/" query: str = "" if url: # Start by checking the URL. if not url.startswith(scheme): # Does not start with http or https. raise Exception( "The URL is not in the right format of 'scheme://domain:port/path'.", "\t") parse = urlparse(url=data.url) # Parsing the URL. try: port = parse.port if parse.port else port # Select a port to use (from a url or from data). scheme = parse.scheme # Must have a scheme (http or https). path = parse.path # Must have a path (even "/" counts). query = "?" + parse.query if parse.query else query except Exception: print( f"\t[{COLOR_MANAGER.YELLOW}%{COLOR_MANAGER.ENDC}]{COLOR_MANAGER.YELLOW}" f" The port which was specified in the URL is invalid.{COLOR_MANAGER.ENDC}" ) try: ip = socket.gethostbyname(parse.hostname) except Exception: pass if ip: # Found IP in the URL or was already specified in data. if ip.count(".") != 3 or not all( field.isnumeric() and 0 <= int(field) <= 255 for field in ip.split(".")): # If the number of fields is not 4 or any of them are not in range of 0-255. raise Exception( f"The IP is not in the right of format of [0-255].[0-255].[0-255].[0-255].", "\t") else: # The IP is not specified and the URL was not found. raise Exception("No IP was specified or found through the URL.", "\t") if port: # A port was specified in the user's input. if self.__MAXIMUM_PORT <= port or port <= 0: # Port out of range. raise Exception( f"Port is out of range ('{port}' is not between 0-{self.__MAXIMUM_PORT})", "\t") elif data.port == 0: # -P was specified. port = 0 else: # No port specified and -P was not specified. print( f"\t[{COLOR_MANAGER.YELLOW}%{COLOR_MANAGER.ENDC}]{COLOR_MANAGER.YELLOW} " f"Using default port 80.{COLOR_MANAGER.ENDC}") port = 80 # Setting address into the data object. data.url = f"{scheme}://{ip}:{port}{path}{query}" data.port = port data.ip = ip
def __get_final_args(self, data: Data, args: argparse.Namespace): """ This function gets the arguments from the argparse namespace and inserts them into a Data object which is returned to the main program. @param data: The data object of the program. @type data: Data @param args: All the command line arguments. @type args: argparse.Namespace @return: The returned data object, will be processed furthermore in the Main Core. @rtype: Data """ # Set the `Username and Password`. if type(args.login) is not None: if len(args.login) == 2: data.username = self.__char_arr_to_string(args.login[0]) data.password = self.__char_arr_to_string(args.login[1]) # Set the `cookies`. data.cookies = args.cookies # Set the `Host IP` Address. data.ip = args.ip # Set the `Website URL`. data.url = args.url # Check if `all_ports` flag is set. if args.all_ports: data.port = 0 else: # Set the `Host Port`. data.port = args.port # Set the `maximum number of pages`. if args.number_of_pages and args.number_of_pages <= 0: # If the given number is invalid. COLOR_MANAGER.print_error( "Invalid number of pages! Running with unlimited pages.") data.max_pages = None else: # If the number wasn't specified or it was specified and is valid. data.max_pages = args.number_of_pages # Set the `output file` name and path. data.output = args.output # Set `blacklist` file path. if args.blacklist is not None: if args.blacklist.endswith(".txt"): data.blacklist = args.blacklist else: data.blacklist = args.blacklist + ".txt" else: data.blacklist = args.blacklist # Set `whitelist` file path. if args.whitelist is not None: if args.whitelist.endswith(".txt"): data.whitelist = args.whitelist else: data.whitelist = args.whitelist + ".txt" else: data.whitelist = args.whitelist # Set `recursive` flag. data.recursive = args.recursive # Set `verbose` flag. data.verbose = args.verbose if args.verbose: # Print startup logo and current time. print(startup()) print( f"{COLOR_MANAGER.GREEN}Started on: {datetime.datetime.now()}{COLOR_MANAGER.ENDC}" ) # Set `aggressive` flag. data.aggressive = args.aggressive