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 = [format_time(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)