From 0e3d5e1c1fa83a0f1c86a3d3b7aadca9bd57cd80 Mon Sep 17 00:00:00 2001 From: lelo Date: Sat, 22 Mar 2025 11:52:19 +0100 Subject: [PATCH] add recent connection --- app.py | 76 ++++++++++++++++++++++++++++++++++++- requirements.txt | 1 + templates/dashboard.html | 25 +++++++------ templates/network.html | 81 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 169 insertions(+), 14 deletions(-) create mode 100644 templates/network.html diff --git a/app.py b/app.py index 7fa2335..3a8af09 100755 --- a/app.py +++ b/app.py @@ -7,7 +7,10 @@ import mimetypes import sqlite3 from datetime import datetime, date, timedelta import diskcache +import threading import json +import time +from flask_socketio import SocketIO import geoip2.database from functools import lru_cache from urllib.parse import urlparse, unquote @@ -26,6 +29,13 @@ if os.environ.get('FLASK_ENV') == 'production': with open('folder_config.json') as file: app.config['folder_config'] = json.load(file) +socketio = SocketIO(app) + +# Global variables to track the number of connected clients and the background thread +clients_connected = 0 +background_thread = None +thread_lock = threading.Lock() + def require_secret(f): @wraps(f) def decorated_function(*args, **kwargs): @@ -558,6 +568,68 @@ def crawl_and_cache(subpath): # Return the list of cached files as a JSON response return json.dumps({"cached_files": cached_files}, indent=4), 200 +def query_recent_connections(): + """ + Every 5 seconds, query the database for connections in the last 60 seconds, + sorted by timestamp (most recent first), and emit the data to clients. + This loop will exit when there are no connected clients. + """ + global clients_connected + while clients_connected > 0: + cutoff = datetime.now() - timedelta(seconds=60) + cutoff_iso = cutoff.isoformat() + + # Query the SQLite database for recent connections + conn = sqlite3.connect('access_log.db') + cursor = conn.cursor() + cursor.execute(''' + SELECT * FROM file_access_log + WHERE timestamp >= ? + ORDER BY timestamp DESC + ''', (cutoff_iso,)) + rows = cursor.fetchall() + conn.close() + + # Convert rows to dictionaries for the client, including all columns. + connections = [] + for row in rows: + # Row order: (id, timestamp, full_path, ip_address, user_agent, referrer) + connections.append({ + 'id': row[0], + 'timestamp': row[1], + 'full_path': row[2], + 'ip_address': row[3], + 'user_agent': row[4], + 'referrer': row[5] + }) + + # Emit the result over Socket.IO (to the default namespace) + socketio.emit('recent_connections', connections) + time.sleep(5) + # When no clients are connected, exit the thread. + print("No clients connected; stopping query thread.") + +@socketio.on('connect') +def handle_connect(): + global clients_connected, background_thread + clients_connected += 1 + print("Client connected. Total clients:", clients_connected) + with thread_lock: + # Start the background task if it's not already running. + if background_thread is None or not background_thread.is_alive(): + background_thread = socketio.start_background_task(query_recent_connections) + print("Started background query task.") + +@socketio.on('disconnect') +def handle_disconnect(): + global clients_connected + clients_connected -= 1 + print("Client disconnected. Total clients:", clients_connected) + +@app.route('/network') +def network(): + return render_template('network.html') + # Catch-all route to serve the single-page application template. @app.route('/', defaults={'path': ''}) @app.route('/') @@ -565,5 +637,5 @@ def crawl_and_cache(subpath): def index(path): return render_template("app.html") -if __name__ == "__main__": - app.run(debug=True, host='0.0.0.0') \ No newline at end of file +if __name__ == '__main__': + socketio.run(app, debug=True, host='0.0.0.0') diff --git a/requirements.txt b/requirements.txt index fc1ad63..1ad2786 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ flask +flask_socketio pillow diskcache geoip2 diff --git a/templates/dashboard.html b/templates/dashboard.html index edc3ea4..df68640 100644 --- a/templates/dashboard.html +++ b/templates/dashboard.html @@ -1,22 +1,23 @@ - - - Web Analytics Dashboard - - - - + + + + Dashboard - Verbindungsanalyse + + +
-

Web Analytics Dashboard ({{ timeframe }})

+

Dashboard - Verbindungsanalyse({{ timeframe }})

Home + Netzwerk Today Last 7 Days Last 30 Days diff --git a/templates/network.html b/templates/network.html new file mode 100644 index 0000000..3c14589 --- /dev/null +++ b/templates/network.html @@ -0,0 +1,81 @@ + + + + + + Recent Connections + + + + +
+

kürzlich verbunden... (in der letzten Minute)

+
+ Home + Dashboard +
+
+ + + + + + + + + + + + + + +
IDTimestampFull PathIP AddressUser AgentReferrer
+
+
+ + + + + + + + +