This commit is contained in:
lelo 2025-03-31 21:00:01 +00:00
parent 80220e1250
commit 47e321134e
4 changed files with 31 additions and 9 deletions

View File

@ -81,12 +81,14 @@ def get_device_type(user_agent):
# Logging function that uses the singleton connection. # Logging function that uses the singleton connection.
def log_file_access(rel_path, filesize, mime, ip_address, user_agent, device_id, cached): 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 timestamp = datetime.now() # Use datetime object directly
with log_db.connection.cursor() as cursor: with log_db.connection.cursor() as cursor:
cursor.execute(''' cursor.execute('''
INSERT INTO file_access_log (timestamp, rel_path, filesize, mime, ip_address, user_agent, device_id, cached) 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) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
''', (timestamp, rel_path, filesize, mime, ip_address, user_agent, device_id, cached)) ''', (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() return timestamp.isoformat()
def return_file_access(): def return_file_access():

29
app.py
View File

@ -4,7 +4,6 @@ from PIL import Image
import io import io
from functools import wraps from functools import wraps
import mimetypes import mimetypes
import sqlite3
from datetime import datetime, date, timedelta from datetime import datetime, date, timedelta
import diskcache import diskcache
import threading import threading
@ -15,7 +14,7 @@ import geoip2.database
from functools import lru_cache from functools import lru_cache
from urllib.parse import urlparse, unquote from urllib.parse import urlparse, unquote
from werkzeug.middleware.proxy_fix import ProxyFix from werkzeug.middleware.proxy_fix import ProxyFix
import re
import auth import auth
import analytics as a 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('/mylinks', view_func=auth.mylinks)
app.add_url_rule('/remove_secret', view_func=auth.remove_secret, methods=['POST']) 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 background_thread_running = False
# Global variables to track the number of connected clients and the background thread # 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'), 'timestamp': datetime.strptime(row[0], '%Y-%m-%dT%H:%M:%S.%f').strftime('%d.%m.%Y %H:%M:%S'),
'full_path': row[1], 'full_path': row[1],
'ip_address': row[2], 'filesize' : row[2],
'user_agent': row[3] 'mime-typ' : row[3],
'ip_address': row[4],
'user_agent': row[5],
'cached': row[7]
} }
for row in rows 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'), 'timestamp': datetime.strptime(row[0], '%Y-%m-%dT%H:%M:%S.%f').strftime('%d.%m.%Y %H:%M:%S'),
'full_path': row[1], 'full_path': row[1],
'ip_address': row[2], 'filesize' : row[2],
'user_agent': row[3] 'mime-typ' : row[3],
'ip_address': row[4],
'user_agent': row[5],
'cached': row[7]
} }
for row in rows for row in rows
] ]

View File

@ -53,8 +53,7 @@ services:
gunicorn --worker-class eventlet -w 1 -b 0.0.0.0:5000 app:app" gunicorn --worker-class eventlet -w 1 -b 0.0.0.0:5000 app:app"
postgres: postgres:
image: postgres:15 image: postgres:17
container_name: "${CONTAINER_NAME}-db"
restart: always restart: always
environment: environment:
POSTGRES_USER: ${DB_USER:?} POSTGRES_USER: ${DB_USER:?}

View File

@ -41,6 +41,9 @@
<th>IP Address</th> <th>IP Address</th>
<th>User Agent</th> <th>User Agent</th>
<th>File Path</th> <th>File Path</th>
<td>File Size</td>
<td>MIME-Typ</td>
<td>Cached</td>
</tr> </tr>
</thead> </thead>
<tbody id="connectionsTableBody"> <tbody id="connectionsTableBody">
@ -71,6 +74,9 @@
<td>${record.ip_address}</td> <td>${record.ip_address}</td>
<td>${record.user_agent}</td> <td>${record.user_agent}</td>
<td>${record.full_path}</td> <td>${record.full_path}</td>
<td>${record.filesize}</td>
<td>${record.mime-typ}</td>
<td>${record.cached}</td>
`; `;
tbody.appendChild(row); tbody.appendChild(row);
}); });