From 47e321134ebd00eab90951bd992a1d18ccdceb08 Mon Sep 17 00:00:00 2001 From: lelo Date: Mon, 31 Mar 2025 21:00:01 +0000 Subject: [PATCH] fix --- analytics.py | 2 ++ app.py | 29 ++++++++++++++++++++++------- docker-compose.yml | 3 +-- templates/connections.html | 6 ++++++ 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/analytics.py b/analytics.py index 702a386..f580b6a 100644 --- a/analytics.py +++ b/analytics.py @@ -81,12 +81,14 @@ def get_device_type(user_agent): # Logging function that uses the singleton connection. def log_file_access(rel_path, filesize, mime, ip_address, user_agent, device_id, cached): + global file_access_temp timestamp = datetime.now() # Use datetime object directly with log_db.connection.cursor() as cursor: cursor.execute(''' INSERT INTO file_access_log (timestamp, rel_path, filesize, mime, ip_address, user_agent, device_id, cached) VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ''', (timestamp, rel_path, filesize, mime, ip_address, user_agent, device_id, cached)) + file_access_temp.insert(0, [timestamp, rel_path, filesize, mime, ip_address, user_agent, device_id, cached]) return timestamp.isoformat() def return_file_access(): diff --git a/app.py b/app.py index 46ab0e1..4c2969a 100755 --- a/app.py +++ b/app.py @@ -4,7 +4,6 @@ from PIL import Image import io from functools import wraps import mimetypes -import sqlite3 from datetime import datetime, date, timedelta import diskcache import threading @@ -15,7 +14,7 @@ import geoip2.database from functools import lru_cache from urllib.parse import urlparse, unquote from werkzeug.middleware.proxy_fix import ProxyFix - +import re import auth import analytics as a @@ -39,7 +38,17 @@ app.add_url_rule('/connections', view_func=a.connections) app.add_url_rule('/mylinks', view_func=auth.mylinks) app.add_url_rule('/remove_secret', view_func=auth.remove_secret, methods=['POST']) -socketio = SocketIO(app, async_mode='eventlet') +# Grab the HOST_RULE environment variable +host_rule = os.getenv("HOST_RULE", "") +# Use a regex to extract domain names between backticks in patterns like Host(`something`) +pattern = r"Host\(`([^`]+)`\)" +allowed_domains = re.findall(pattern, host_rule) + +socketio = SocketIO( + app, + async_mode='eventlet', + cors_allowed_origins=allowed_domains +) background_thread_running = False # Global variables to track the number of connected clients and the background thread @@ -355,8 +364,11 @@ def query_recent_connections(): { 'timestamp': datetime.strptime(row[0], '%Y-%m-%dT%H:%M:%S.%f').strftime('%d.%m.%Y %H:%M:%S'), 'full_path': row[1], - 'ip_address': row[2], - 'user_agent': row[3] + 'filesize' : row[2], + 'mime-typ' : row[3], + 'ip_address': row[4], + 'user_agent': row[5], + 'cached': row[7] } for row in rows ] @@ -393,8 +405,11 @@ def handle_request_initial_data(): { 'timestamp': datetime.strptime(row[0], '%Y-%m-%dT%H:%M:%S.%f').strftime('%d.%m.%Y %H:%M:%S'), 'full_path': row[1], - 'ip_address': row[2], - 'user_agent': row[3] + 'filesize' : row[2], + 'mime-typ' : row[3], + 'ip_address': row[4], + 'user_agent': row[5], + 'cached': row[7] } for row in rows ] diff --git a/docker-compose.yml b/docker-compose.yml index 6e6755e..d90563f 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -53,8 +53,7 @@ services: gunicorn --worker-class eventlet -w 1 -b 0.0.0.0:5000 app:app" postgres: - image: postgres:15 - container_name: "${CONTAINER_NAME}-db" + image: postgres:17 restart: always environment: POSTGRES_USER: ${DB_USER:?} diff --git a/templates/connections.html b/templates/connections.html index 68e1dd2..373eacb 100644 --- a/templates/connections.html +++ b/templates/connections.html @@ -41,6 +41,9 @@ IP Address User Agent File Path + File Size + MIME-Typ + Cached @@ -71,6 +74,9 @@ ${record.ip_address} ${record.user_agent} ${record.full_path} + ${record.filesize} + ${record.mime-typ} + ${record.cached} `; tbody.appendChild(row); });