from robobrowser import RoboBrowser
from bs4 import BeautifulSoup
from general import *
from configparser import ConfigParser
from A4.trav_attack import TravAttack


class TravLogin:

    def __init__(self, alamatURL):
        self.url = alamatURL
        self.list_atr = ("id", "class", "name", "action")
        self.atr_form = None
        self.atr_value = None
        self.log = list()
        self.log_csv = list()

        self.boot()

    def boot(self):
        htmlLogin = self.get_form_login_html(self.url)
        if htmlLogin is not False:
            self.name_input = self.get_input_name(htmlLogin)

            value = None
            for atr in self.list_atr:
                value = self.get_value_atr_form(htmlLogin, atr)
                if value is not None:
                    self.atr_form = atr
                    self.atr_value = value
                    self.attack()
                    break
            if value is None:
                self.log.append("not vulnerability - not found or not have form login")
        else:
            self.log.append("not vulnerability - not found or do have form login")

    def attack(self):
        robo = RoboBrowser(history=True)
        robo.open(self.url)
        if self.atr_form is "id":
            form = robo.get_form(id=self.atr_value)
        elif self.atr_form is "class":
            form = robo.get_form(class_=self.atr_value)
        elif self.atr_form is "name":
            form = robo.get_form(name=self.atr_value)
        elif self.atr_form is "action":
            form = robo.get_form(action=self.atr_value)
        else:
            self.log.append("Tidak Menemukan Form Login")
            return None

        form[self.name_input[0]].value = "xxxxx"
        form[self.name_input[1]].value = "xxxxx"
        robo.submit_form(form)
        # melakukan traversal path setelah login
        if cek_link_with_parm(robo.url):
            trav = TravAttack(robo.url)
            for output in trav.return_output():
                self.log.append(output)
            for output in trav.return_output_csv():
                self.log_csv.append(output)

        else:
            self.log.append("not vulnerability with traversal path login")

    def get_form_login_html(self, alamatURL):
        config = ConfigParser()
        config.read('/opt/asetulweb/config.ini')
        self.db = str(config.get('settings', 'db'))
        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"
        }
        r = requests.get(alamatURL, timeout=5, headers=header)
        login = file_to_list("db/db_login.txt")
        try:
            soup = BeautifulSoup(r.text, 'html.parser')
            forms = soup.find_all('form')
            for form in forms:
                html = str(form)
                for log in login:
                    if html.__contains__(log):
                        return html
                else:
                    return False
            return False
        except:
            return False

    def get_value_atr_form(self, html, atr):
        soup = BeautifulSoup(html, 'html.parser')
        value = soup.find('form').get(atr)
        return value

    def get_input_name(self, html):
        soup = BeautifulSoup(html, 'html.parser')
        value = list()
        for item in soup.find_all('input'):
            try:
                value.append(item['name'])
            except:
                pass
        return value

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

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