import json
#from urlparse import urlparse
#import httplib, sys

from Spitzer.exploiters.web import *
from Spitzer.exploiters.web import screenshot
from Spitzer.config import config

category = ['safe']

def exploit(host, nmap, port):
    
    if not host.startswith('http'):
        if port == 80:
            host = 'http://' + host
        elif port == 443:
            host = 'https://' + host
        else:
            host = 'http://' + host#check_protocol(host)

    url = host + ':' + str(port)
    modules = config.get_data('web')
    categories = config.get_config('categories').replace(' ', '').split(',')

    for module in modules:

        category = eval(module + '.category')
        
        if not any(cat in category  for cat in categories):
            continue

        eval(module + '.exploit(\''+url+'\', ' + json.dumps(nmap) + ')')

'''def check_protocol(host): #TODO
    host = 'https://' + host
    url = urlparse(host)
    conn = httplib.HTTPConnection(url.netloc)   
    conn.request("HEAD", url.path)
    if conn.getresponse():
        return host #uses https
    else:
        return 'http://' + host.replace('https://', '') #uses http'''