import os

from lxml import etree
from shutil import copyfile

from Spitzer import command
from Spitzer.config import config
from Spitzer.chache import chache

#big scanner, I do not like this.

category = ['intrusive']

def exploit(url):
    return#not in use
    scheme = ''
    port = 0
    if url.startswith('https://'):
        scheme = 'https'
        port = 443
        url = url[8:]
    elif url.startswith('http://'):
        scheme = 'http'
        port = 80
        url = url[7:]
    else:
        raise RuntimeError('no scheme specified!')

    print('burp is scanning')
    cmd = ['java', '-jar', '-Djava.awt.headless=true', config.get_data('burp'), scheme, url, str(port), '//']
    command.run(cmd, capture_output=False)
    print('burp is done!')

    html = ''
    path = ''

    #find html file
    for file in os.listdir(os.getcwd()):
        if file.startswith('IntegrisSecurity_Carbonator_') and file.endswith('.html'):
            path = os.getcwd() + '/' + file
            html = open(path, 'r').read()

    #get html result and check for errors
    html = etree.HTML(html)


    #xpaths of severity
    high = html.xpath('/html[1]/body[1]/div[1]/table[1]/tr[3]/td[6]/span[1]/text()')[0]
    medium = html.xpath('/html[1]/body[1]/div[1]/table[1]/tr[4]/td[5]/span[1]/text()')[0]
    low = html.xpath('/html[1]/body[1]/div[1]/table[1]/tr[5]/td[5]/span[1]/text()')[0]
    info = html.xpath('/html[1]/body[1]/div[1]/table[1]/tr[6]/td[5]/span[1]/text()')[0]

    if high != '0' or medium != '0' or low != '0' or info != '0':
        print('burp found:')
        print(high+' high severity errors')
        print(medium+' medium severity errors')
        print(low+' low severity errors')
        print(info+' informations')

        dst = os.getcwd() + '/burpresult.html'
        copyfile(path, dst)
        os.remove(path)
        print('[-] Placed burpresult.html in ' + dst)