from A2.BruteForce import *
from A2.packet_sniffer import *
from configparser import ConfigParser
import requests


class MainBrokAuth:

    def __init__(self, url, path):
        self.url = url
        self.path = path
        self.log = list()
        self.log_csv = list()
        self.cek_sniff = False

        config = ConfigParser()
        config.read('/opt/asetulweb/config.ini')
        interface = config.get('settings', 'interface')
        db = str(config.get('settings', 'db'))

        self.list_url = file_to_set(path + "/crawled.txt")
        self.db_login = file_to_list(db + "db/db_login.txt")

        self.sniffer = Sniffer(interface)
        self.sniffer.start()

        time.sleep(10)
        self.boot()

    def boot(self):
        header = {
            "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:69.0) Gecko/20100101 Firefox/69.0",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
            'Accept-Language': "en-US,en;q=0.5"
        }
        url_login = list()
        for x in self.list_url:
            for y in self.db_login:
                if x.__contains__(y):
                    try:
                        re = requests.get(x, timeout=5, headers=header)
                        if re.status_code == 200:
                            url_login.append(x)
                    except requests.exceptions.HTTPError as err:
                        self.log.append(err)
                    except requests.exceptions.RequestException as err:
                        self.log.append(err)
                    break

        if len(url_login) > 0:
            self.log.append("result Brute Force")
            for url in url_login:
                self.log.append("url : " + url)
                try:
                    bf = BruteForce(url)
                    for output in bf.return_output():
                        self.log.append(output)
                    for output in bf.return_output_csv():
                        self.log_csv.append(output)
                except:
                    self.log.append("brute force failed - Error")
                self.log.append("")
        else:
            self.log.append("tidak menemukan link login")

        self.sniffer.join(2.0)
        if self.sniffer.isAlive():
            self.sniffer.socket.close()
        self.log.append("result Packet Sniffer")
        if self.sniffer.return_output() or self.sniffer.return_output_cookie():
            self.log_csv.append(["Packet Sniffer(Auth)", self.url])
            for output in self.sniffer.return_output():
                self.log.append(str(output))
            self.log_csv.append(["Packet Sniffer(Cookie)", self.url])
            for output in self.sniffer.return_output_cookie():
                self.log.append(str(output))
        elif self.sniffer.return_output_err():
            self.log.append("Sniffing Error please check interface or user permision")
        else:
            self.log.append("not vulnerabilty with Packet Sniffer")

    def return_output(self):
        output = self.log
        return output

    def return_output_csv(self):
        output = self.log_csv
        return output
