Пример #1
0
def index():
    if request.method == 'POST':
        mail_data = request.form['headers'].strip().encode('ascii', 'ignore')
        r = {}
        n = HeaderParser().parsestr(mail_data)
        graph = []
        received = n.get_all('Received')
        if received:
            received = [i for i in received if ('from' in i or 'by' in i)]
        else:
            received = re.findall('Received:\s*(.*?)\n\S+:\s+', mail_data,
                                  re.X | re.DOTALL | re.I)
        c = len(received)
        for i in range(len(received)):
            if ';' in received[i]:
                line = received[i].split(';')
            else:
                line = received[i].split('\r\n')
            line = map(str.strip, line)
            line = map(lambda x: x.replace('\r\n', ' '), line)
            try:
                if ';' in received[i + 1]:
                    next_line = received[i + 1].split(';')
                else:
                    next_line = received[i + 1].split('\r\n')
                next_line = map(str.strip, next_line)
                next_line = map(lambda x: x.replace('\r\n', ''), next_line)
            except IndexError:
                next_line = None

            org_time = dateParser(line[-1])
            if not next_line:
                next_time = org_time
            else:
                next_time = dateParser(next_line[-1])

            if line[0].startswith('from'):
                data = re.findall(
                    """
                    from\s+
                    (.*?)\s+
                    by(.*?)
                    (?:
                        (?:with|via)
                        (.*?)
                        (?:\sid\s|$)
                        |\sid\s|$
                    )""", line[0], re.DOTALL | re.X)
            else:
                data = re.findall(
                    """
                    ()by
                    (.*?)
                    (?:
                        (?:with|via)
                        (.*?)
                        (?:\sid\s|$)
                        |\sid\s
                    )""", line[0], re.DOTALL | re.X)

            delay = (org_time - next_time).seconds
            if delay < 0:
                delay = 0

            try:
                ftime = org_time.utctimetuple()
                ftime = time.strftime('%m/%d/%Y %I:%M:%S %p', ftime)
                r[c] = {
                    'Timestmp':
                    org_time,
                    'Time':
                    ftime,
                    'Delay':
                    delay,
                    'Direction':
                    map(lambda x: x.replace('\n', ' '),
                        map(str.strip, data[0]))
                }
                c -= 1
            except IndexError:
                pass

        for i in r.values():
            if i['Direction'][0]:
                graph.append(["From: %s" % i['Direction'][0], i['Delay']])
            else:
                graph.append(["By: %s" % i['Direction'][1], i['Delay']])

        totalDelay = sum(map(lambda x: x['Delay'], r.values()))
        fTotalDelay = utility_processor()['duration'](totalDelay)
        delayed = True if totalDelay else False

        custom_style = Style(
            background='transparent',
            plot_background='transparent',
            font_family='googlefont:Open Sans',
            # title_font_size=12,
        )
        line_chart = pygal.HorizontalBar(style=custom_style,
                                         height=250,
                                         legend_at_bottom=True,
                                         tooltip_border_radius=10)
        line_chart.tooltip_fancy_mode = False
        line_chart.title = 'Total Delay is: %s' % fTotalDelay
        line_chart.x_title = 'Delay in seconds.'
        for i in graph:
            line_chart.add(i[0], i[1])
        chart = line_chart.render(is_unicode=True)

        summary = {
            'From':
            n.get('From') or getHeaderVal('from', mail_data),
            'To':
            n.get('to') or getHeaderVal('to', mail_data),
            'Cc':
            n.get('cc') or getHeaderVal('cc', mail_data),
            'Subject':
            n.get('Subject') or getHeaderVal('Subject', mail_data),
            'MessageID':
            n.get('Message-ID') or getHeaderVal('Message-ID', mail_data),
            'Date':
            n.get('Date') or getHeaderVal('Date', mail_data),
        }

        security_headers = [
            'Received-SPF', 'Authentication-Results', 'DKIM-Signature',
            'ARC-Authentication-Results'
        ]
        return render_template('index.html',
                               data=r,
                               delayed=delayed,
                               summary=summary,
                               n=n,
                               chart=chart,
                               security_headers=security_headers)
    else:
        return render_template('index.html')
Пример #2
0
def index():
    if request.method == 'POST':
        data = request.form['headers'].strip()
        r = {}
        n = HeaderParser().parsestr(data.encode('ascii', 'ignore'))
        graph = []
        c = len(n.get_all('Received'))
        for i in range(len(n.get_all('Received'))):
            line = n.get_all('Received')[i].split(';')
            try:
                next_line = n.get_all('Received')[i + 1].split(';')
            except IndexError:
                next_line = None
            org_time = email.utils.mktime_tz(email.utils.parsedate_tz(line[1]))
            if not next_line:
                next_time = org_time
            else:
                next_time = email.utils.mktime_tz(
                    email.utils.parsedate_tz(next_line[1]))

            if line[0].startswith('from'):
                data = re.findall(
                    """
                    from\s+
                    (.*?)\s+
                    by(.*?)
                    (?:
                        (?:with|via)
                        (.*?)
                        (?:id|$)
                        |id|$
                    )""", line[0], re.DOTALL | re.X)
            else:
                data = re.findall(
                    """
                    ()by
                    (.*?)
                    (?:
                        (?:with|via)
                        (.*?)
                        (?:id|$)
                        |id
                    )""", line[0], re.DOTALL | re.X)

            delay = org_time - next_time
            if delay < 0:
                delay = 0

            try:
                time = datetime.fromtimestamp(org_time)
                ftime = time.strftime('%m/%d/%Y %I:%M:%S %p')
                r[c] = {
                    'Timestmp':
                    org_time,
                    'Time':
                    ftime,
                    'Delay':
                    delay,
                    'Direction':
                    map(lambda x: x.replace('\n', ' '),
                        map(str.strip, data[0]))
                }
                c -= 1
            except IndexError:
                pass

        for i in r.values():
            if i['Direction'][0]:
                graph.append(["From: %s" % i['Direction'][0], i['Delay']])
            else:
                graph.append(["By: %s" % i['Direction'][1], i['Delay']])

        totalDelay = sum(map(lambda x: x['Delay'], r.values()))
        fTotalDelay = utility_processor()['duration'](totalDelay)
        delayed = True if totalDelay else False

        custom_style = Style(
            background='transparent',
            plot_background='transparent',
            font_family='googlefont:Open Sans',
            title_font_size=12,
        )
        line_chart = pygal.HorizontalBar(style=custom_style, height=200)
        line_chart.tooltip_fancy_mode = False
        line_chart.js = ['%s/js/pygal-tooltips.min.js' % app.static_url_path]
        line_chart.title = 'Total Delay is: %s' % fTotalDelay
        line_chart.x_title = 'Delay in seconds.'
        for i in graph:
            line_chart.add(i[0], i[1])
        chart = line_chart.render(is_unicode=True)

        summary = {
            'From': n.get('from'),
            'To': n.get('to'),
            'Cc': n.get('cc'),
            'Subject': n.get('Subject'),
            'MessageID': n.get('Message-ID'),
            'Date': n.get('Date'),
        }
        return render_template('index.html',
                               data=r,
                               delayed=delayed,
                               summary=summary,
                               n=n,
                               chart=chart)
    else:
        return render_template('index.html')