import requests
import argparse
import queue
from urllib3.exceptions import InsecureRequestWarning


class DirectoryBuster:

    def __init__(self):
        super().__init__()
        requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
        self.parser = argparse.ArgumentParser()
        self.setupArgs()
        self.initiateBusting()

    def setupArgs(self):
        self.parser.add_argument('-url', help='Url to bust')
        self.parser.add_argument('-dict', help='Dictionary to use')
        self.parser.add_argument('-r', help='Be recursive', action='store_true')
        self.parser.add_argument('-v', help='Show all url attempts', action='store_true')
        args = self.parser.parse_args()

        if (args.url is None):
            print('Required url argument. Try using -url [url]')
            exit()
        if (args.dict is None):
            print('Required dict argument. Try using -dict [dictionary]')
            exit()

    def initiateBusting(self):
        args = self.parser.parse_args()

        try:
            with open(args.dict) as dictionary:
                words = dictionary.readlines()
                initialWords = len(words)
                totalWords = initialWords
                attemptCount = 0
                args.url = args.url.strip()
                foundSubdirectories = queue.Queue()
                foundSubdirectories.put('/')
                print('====================================')
                if (not args.v):
                    print('Links found:')
                else:
                    print('Links attempted:')
                print('====================================')

                while not foundSubdirectories.empty():

                    subdirectory = foundSubdirectories.get()
                    for link in words:

                        if (link.startswith('#')):
                            continue
                        attemptCount += 1
                        link = link.strip()
                        print('\u001b[37m{}/{}'.format(attemptCount,totalWords), end='\r')
                        dest = args.url + subdirectory + link
                        resp = requests.head(dest, headers={'User-Agent': 'Mozilla/5.0 (X11; Linux i686; rv:45.0) Gecko/20100101 Firefox/45.0'}, verify=False)

                        if (resp.status_code < 400 or resp.status_code >= 500):
                            if (resp.status_code >= 200 and resp.status_code < 300 and link != ''):
                                print('\033[92m{} (status code: {})'.format(dest, resp.status_code))
                            else:
                                print('\033[93m{} (status code: {})'.format(dest, resp.status_code))

                            if (link != '' and args.r):
                                foundSubdirectories.put('/{}/'.format(link))
                                totalWords += initialWords

                        elif(args.v):
                            print('\033[91m{}status code: {}'.format(dest, resp.status_code))


        except KeyboardInterrupt:
            print('\n\033[93mProgram terminated by user!')
            exit()
        except FileNotFoundError:
            print('\033[93mDictionary file not found!')
            exit()


DirectoryBuster()

import requests
import argparse
import queue
from urllib3.exceptions import InsecureRequestWarning


class DirectoryBuster:

    def __init__(self):
        super().__init__()
        requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
        self.parser = argparse.ArgumentParser()
        self.setupArgs()
        self.initiateBusting()

    def setupArgs(self):
        self.parser.add_argument('-url', help='Url to bust')
        self.parser.add_argument('-dict', help='Dictionary to use')
        self.parser.add_argument('-r', help='Be recursive', action='store_true')
        self.parser.add_argument('-v', help='Show all url attempts', action='store_true')
        args = self.parser.parse_args()

        if (args.url is None):
            print('Required url argument. Try using -url [url]')
            exit()
        if (args.dict is None):
            print('Required dict argument. Try using -dict [dictionary]')
            exit()

    def initiateBusting(self):
        args = self.parser.parse_args()

        try:
            with open(args.dict) as dictionary:
                words = dictionary.readlines()
                initialWords = len(words)
                totalWords = initialWords
                attemptCount = 0
                args.url = args.url.strip()
                foundSubdirectories = queue.Queue()
                foundSubdirectories.put('/')
                print('====================================')
                if (not args.v):
                    print('Links found:')
                else:
                    print('Links attempted:')
                print('====================================')

                while not foundSubdirectories.empty():

                    subdirectory = foundSubdirectories.get()
                    for link in words:

                        if (link.startswith('#')):
                            continue
                        attemptCount += 1
                        link = link.strip()
                        print('\u001b[37m{}/{}'.format(attemptCount,totalWords), end='\r')
                        dest = args.url + subdirectory + link
                        resp = requests.head(dest, headers={'User-Agent': 'Mozilla/5.0 (X11; Linux i686; rv:45.0) Gecko/20100101 Firefox/45.0'}, verify=False)

                        if (resp.status_code < 400 or resp.status_code >= 500):
                            if (resp.status_code >= 200 and resp.status_code < 300 and link != ''):
                                print('\033[92m{} (status code: {})'.format(dest, resp.status_code))
                            else:
                                print('\033[93m{} (status code: {})'.format(dest, resp.status_code))

                            if (link != '' and args.r):
                                foundSubdirectories.put('/{}/'.format(link))
                                totalWords += initialWords

                        elif(args.v):
                            print('\033[91m{}status code: {}'.format(dest, resp.status_code))


        except KeyboardInterrupt:
            print('\n\033[93mProgram terminated by user!')
            exit()
        except FileNotFoundError:
            print('\033[93mDictionary file not found!')
            exit()


DirectoryBuster()
