# Path: lib/redisopt.py
# def get_hash(host, uri, postdata):
# def content_deal(headers, host, method, postdata, uri, packet):
# 
# Path: lib/out.py
# def error(msg):
#     if "Windows" not in platform.platform():
#         color = '\033[01;31m'
#         endc = '\033[0m'
#         print "[*] " + color + msg + endc
#     else:
#         windows_out(0x0c, "[+] %s" % msg)
# 
# Path: lib/config.py
# def load():
# def update(conf):
# def load_rule():
# def update_rule(rule):
# def rule_read(name, get_file_handle=None):
# def rule_write(name, rule):


import re
import logging
from scapy.all import *
from lib.redisopt import conn, content_deal
from lib.out import error
from lib import config
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)


def extract(data, out):
    try:
        methods = re.findall('(GET|POST) (.*) HTTP', data)[0]
        url = methods[1]
        method = methods[0]
        headers = {}
        if method == 'GET':
            head = data.split('\r\n')[1:]
            for h in head:
                if ': ' in h[2:]:
                    headers[h.split(': ')[0]] = h.split(': ')[1]
            host = headers['Host'].replace(' ', '')
            uri = "http://%s%s" % (host, url)
            if out == 'true':
                print_request(method, host, url)
            content_deal(headers, host, method, postdata='', uri=uri, packet=data)

        elif method == 'POST':
            body = data.split('\r\n\r\n')[1]
            head = data.split('\r\n\r\n')[0].split('\r\n')[1:]
            for h in head:
                if ': ' in h[2:]:
                    headers[h.split(': ')[0]] = h.split(': ')[1]
            host = headers['Host'].replace(' ', '')
            uri = "http://%s%s" % (host, url)
            if out == 'true':
                print_request(method, host, url)

# Path: lib/redisopt.py
# def get_hash(host, uri, postdata):
# def content_deal(headers, host, method, postdata, uri, packet):
# 
# Path: lib/out.py
# def error(msg):
#     if "Windows" not in platform.platform():
#         color = '\033[01;31m'
#         endc = '\033[0m'
#         print "[*] " + color + msg + endc
#     else:
#         windows_out(0x0c, "[+] %s" % msg)
# 
# Path: lib/config.py
# def load():
# def update(conf):
# def load_rule():
# def update_rule(rule):
# def rule_read(name, get_file_handle=None):
# def rule_write(name, rule):


# Path: proxy/pyscapy.py
import re
import logging
from scapy.all import *
from lib.redisopt import conn, content_deal
from lib.out import error
from lib import config

logging.getLogger("scapy.runtime").setLevel(logging.ERROR)


def extract(data, out):
    try:
        methods = re.findall('(GET|POST) (.*) HTTP', data)[0]
        url = methods[1]
        method = methods[0]
        headers = {}
        if method == 'GET':
            head = data.split('\r\n')[1:]
            for h in head:
                if ': ' in h[2:]:
                    headers[h.split(': ')[0]] = h.split(': ')[1]
            host = headers['Host'].replace(' ', '')
            uri = "http://%s%s" % (host, url)
            if out == 'true':
                print_request(method, host, url)
            content_deal(headers, host, method, postdata='', uri=uri, packet=data)

        elif method == 'POST':
            body = data.split('\r\n\r\n')[1]
            head = data.split('\r\n\r\n')[0].split('\r\n')[1:]
            for h in head:
                if ': ' in h[2:]:
                    headers[h.split(': ')[0]] = h.split(': ')[1]
            host = headers['Host'].replace(' ', '')
            uri = "http://%s%s" % (host, url)
            if out == 'true':
                print_request(method, host, url)

            content_deal(headers, host, method, postdata=body, uri=uri, packet=data)