summaryrefslogtreecommitdiff
path: root/src/ssh_login_attempts.py
blob: 3da31cc5a3f670eb06c39c2006b9659c9b841fc5 (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
import logging

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("/password_of_the_month")
@app.route("/password of the month")
@app.route("/passwd")
def password_of_the_month():
    password = db_handler.get_password_of_the_month()
    return password


@app.route("/")
def index():
    latest_loging_attempts = db_handler.get_latest_login_attempts()
    top_usernames = db_handler.get_top('username')
    top_passwords = db_handler.get_top('password')
    return flask.render_template(
        'index.html',
        latest_login_attempts=latest_loging_attempts,
        top_usernames=top_usernames,
        top_passwords=top_passwords)