summaryrefslogtreecommitdiff
path: root/src/ssh_node5_net.py
blob: 06151473265047de40d0c37c11dd7b7cafae4e1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import json
import logging
import datetime
import random

import flask

import db_handler


class CustomFormatter(logging.Formatter):
    grey = "\x1b[90;20m"
    blue = "\x1b[34;20m"
    yellow = "\x1b[33;20m"
    red = "\x1b[31;20m"
    bold_red = "\x1b[31;1m"
    reset = "\x1b[0m"
    format = "%(asctime)s,%(msecs)03d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s"

    FORMATS = {
        logging.DEBUG: grey + format + reset,
        logging.INFO: blue + format + reset,
        logging.WARNING: yellow + format + reset,
        logging.ERROR: red + format + reset,
        logging.CRITICAL: bold_red + format + reset
    }

    def format(self, record):
        log_fmt = self.FORMATS.get(record.levelno)
        formatter = logging.Formatter(log_fmt)
        return formatter.format(record)


logger = logging.getLogger(__name__)
logger.root.setLevel(logging.INFO)

stream_handler = logging.StreamHandler()
stream_handler.setFormatter(CustomFormatter())
logger.root.addHandler(stream_handler)

app = flask.Flask(__name__, template_folder='templates', static_folder='static', static_url_path='')


@app.route("/potm")
def password_of_the_month():
    password = db_handler.get_password_of_the_month()
    return password


def format_time(input: datetime.datetime) -> str:
    return input.strftime('%e %b %Y %H:%M')


'''
    histogram = db_handler.get_histogram()
    histogram_data = [a['total_count'] for a in histogram]
    histogram_labels = [format_time(a['date']) for a in histogram]
'''


def get_chart():
    histogram = db_handler.get_histogram_detailed()
    all_dates = sorted(list({d['date'] for d in histogram}))
    by_ip = {}
    for data in histogram:
        if data['ip'] not in by_ip:
            by_ip[data['ip']] = [0] * len(all_dates)
        by_ip[data['ip']][all_dates.index(data['date'])] = data['total_count']

    histogram_chartjs = json.dumps([{
        'label': str(ip),
        'data': data,
        'backgroundColor': f'hsl({random.randrange(0, 360)}, 50%, 50%)',
        'fill': 'start'
    } for index, (ip, data) in enumerate(by_ip.items())])

    histogram_labels = [str(a) for a in all_dates]
    return histogram_chartjs, histogram_labels


@app.route('/chart')
def chart_page():
    histogram_chartjs, histogram_labels = get_chart()
    return flask.render_template(
        'chart_page.html',
        histogram_data=histogram_chartjs,
        histogram_labels=histogram_labels
    )


@app.route("/")
def index():
    latest_loging_attempts = db_handler.get_latest_login_attempts()
    for login_attempt in latest_loging_attempts[0]:
        login_attempt['timestamp'] = format_time(login_attempt['timestamp'])

    top_usernames = db_handler.get_top('username')
    top_passwords = db_handler.get_top('password')

    histogram_chartjs, histogram_labels = get_chart()

    return flask.render_template(
        'index.html',
        latest_login_attempts=latest_loging_attempts,
        top_usernames=top_usernames,
        top_passwords=top_passwords,
        histogram_data=histogram_chartjs,
        histogram_labels=histogram_labels)