Files
dune-dashboard/app/factory.py
T
Reditus 6e7a850ba2 Fix setup flow: ask remote access first, auto-detect VM IP, validate local_ip
- Ask 'Enable remote access?' before any IP questions
- Default to local-only (n) for simpler setup
- Skip SSL questions when local-only
- Fix Hyper-V VM IP detection (handle single-element arrays properly)
- Validate local_ip in config.py before using as host override
- Fix factory.py to use settings['server']['host'] instead of local_ip
- VM Host prompt shows '(detected)' when auto-detected IP is found
2026-05-19 02:57:50 +01:00

388 lines
15 KiB
Python

"""Application factory - creates and configures the Flask app"""
import os
import sys
import time
import logging
import logging.handlers
import threading
from flask import Flask, request, g
from flask_login import current_user
from flask_socketio import SocketIO
from flask_limiter import Limiter
from flask_wtf.csrf import CSRFProtect
from flask_limiter.util import get_remote_address
from app.config import load_settings
from app.services.database import DatabaseService
from app.services.ssh import SSHService
from app.services.k8s import K8sService
from app.services.player import PlayerService
from app.services.vehicle import VehicleService
from app.services.chat import ChatService
from app.services.admin import AdminService
from app.services.updater import UpdateService
from app.services.director import DirectorService
from app.services.miner_protection import init_miner_protection
from app.utils.cache import MultiCache
from app.routes.main import register_routes
from app.routes.api import register_api_routes
from app.routes.auth import init_auth
from app.websocket.shell import register_websocket_handlers
def create_app(settings_path=None):
settings = load_settings(settings_path)
base_dir = os.path.dirname(os.path.dirname(__file__))
app = Flask(
__name__,
template_folder=os.path.join(base_dir, 'templates'),
static_folder=os.path.join(base_dir, 'static'),
static_url_path='/static'
)
app.config['SECRET_KEY'] = settings['dashboard']['secret_key']
app.config['SESSION_COOKIE_HTTPONLY'] = True
app.config['SESSION_COOKIE_SAMESITE'] = 'Lax'
app.config['WTF_CSRF_ENABLED'] = True
app.config['WTF_CSRF_TIME_LIMIT'] = None
csrf = CSRFProtect(app)
csrf.init_app(app)
ssl_enabled = bool(
settings['dashboard'].get('ssl_cert')
and settings['dashboard'].get('ssl_key')
and settings['dashboard']['ssl_cert'] != 'null'
and settings['dashboard']['ssl_key'] != 'null'
)
# Set SESSION_COOKIE_SECURE conditional on SSL being enabled
app.config['SESSION_COOKIE_SECURE'] = ssl_enabled
# Add security headers manually (replaces deprecated Flask-Talisman)
if not settings['dashboard']['debug']:
@app.after_request
def add_security_headers(response):
# HSTS - only when SSL is enabled
if ssl_enabled:
response.headers['Strict-Transport-Security'] = 'max-age=31536000; includeSubDomains'
# Prevent clickjacking
response.headers['X-Frame-Options'] = 'SAMEORIGIN'
# Prevent MIME-type sniffing
response.headers['X-Content-Type-Options'] = 'nosniff'
# XSS protection
response.headers['X-XSS-Protection'] = '1; mode=block'
# Content Security Policy
response.headers['Content-Security-Policy'] = (
"default-src 'self'; "
"script-src 'self' 'unsafe-inline' https://cdn.jsdelivr.net; "
"style-src 'self' 'unsafe-inline' https://cdn.jsdelivr.net; "
"img-src 'self' data:; "
"connect-src 'self' ws: wss: https://cdn.jsdelivr.net; "
"frame-ancestors 'self'; "
"base-uri 'self'; "
"form-action 'self'"
)
# Referrer Policy - use strict-origin-when-cross-origin to allow CSRF checks
response.headers['Referrer-Policy'] = 'strict-origin-when-cross-origin'
# Permissions Policy
response.headers['Permissions-Policy'] = 'geolocation=(), microphone=(), camera=()'
return response
# Global error handlers
@app.errorhandler(Exception)
def handle_exception(e):
"""Handle all unhandled exceptions."""
logging.exception(f"Unhandled exception: {str(e)}")
from flask import jsonify
error_type = type(e).__name__
error_msg = str(e)
# Don't expose internal error details in production
if settings['dashboard'].get('debug', False):
return jsonify({
'error': error_msg,
'type': error_type,
'trace': str(e)
}), 500
return jsonify({
'error': 'An internal error occurred',
'type': error_type
}), 500
@app.errorhandler(404)
def handle_not_found(e):
from flask import jsonify
return jsonify({'error': 'Resource not found', 'type': 'NotFound'}), 404
@app.errorhandler(405)
def handle_method_not_allowed(e):
from flask import jsonify
return jsonify({'error': 'Method not allowed', 'type': 'MethodNotAllowed'}), 405
# Request logging middleware
@app.before_request
def before_request_logging():
from flask import request
from flask_login import current_user
import time
g._request_start_time = time.time()
user = current_user.id if current_user.is_authenticated else 'anonymous'
logging.debug(f"Request: {request.method} {request.path} from {user}")
@app.after_request
def after_request_logging(response):
from flask import request, g
import time
duration = time.time() - g.get('_request_start_time', time.time())
user = current_user.id if current_user.is_authenticated else 'anonymous'
# Only log API requests to avoid noise
if request.path.startswith('/api') or request.path.startswith('/server'):
logging.info(f"{request.method} {request.path} {response.status_code} {duration:.3f}s user={user}")
# Add timing header
response.headers['X-Response-Time'] = f"{duration:.3f}s"
return response
_setup_logging(settings)
db_host = settings['database']['host']
db_config = {
'host': db_host,
'port': settings['database']['port'],
'user': settings['database']['user'],
'password': settings['database']['password'],
'database': settings['database']['name'],
}
db_service = DatabaseService(
db_config,
min_conn=settings['database']['min_connections'],
max_conn=settings['database']['max_connections']
)
db_service.ensure_tables()
# Create performance indexes if they don't exist
try:
admin_svc_for_indexes = AdminService(db_service, None)
success, created, error = admin_svc_for_indexes.create_indexes()
if success and created:
logging.info(f"Created {len(created)} database indexes")
except Exception as e:
logging.warning(f"Could not create indexes: {e}")
ssh_service = SSHService(
host=settings['server']['host'],
user=settings['server']['user'],
ssh_key=settings['server'].get('ssh_key')
)
k8s_service = K8sService(
ssh_service=ssh_service,
namespace=settings['kubernetes']['namespace']
)
static_cache = MultiCache(ttl_seconds=settings['cache']['static_data_ttl'])
player_svc = PlayerService(db_service)
vehicle_svc = VehicleService(db_service)
chat_svc = ChatService(db_service, k8s_service, ssh_service, static_cache)
admin_svc = AdminService(db_service, ssh_service)
updater_svc = UpdateService(base_dir)
director_svc = DirectorService(
host='127.0.0.1',
node_port=settings.get('director', {}).get('port', 32479),
k8s_service=k8s_service,
ssh_service=ssh_service,
namespace=settings['kubernetes']['namespace'],
)
services = {
'db': db_service,
'ssh': ssh_service,
'k8s': k8s_service,
'player': player_svc,
'vehicle': vehicle_svc,
'chat': chat_svc,
'admin': admin_svc,
'static_cache': static_cache,
'updater': updater_svc,
'director': director_svc,
}
# Initialize and start miner protection
miner_protection = init_miner_protection(settings, ssh_service)
miner_protection.start()
services['miner_protection'] = miner_protection
# Initialize audit service
from app.services.audit import AuditService
audit_svc = AuditService()
services['audit'] = audit_svc
# Track start time for uptime calculation
import time
app._start_time = time.time()
socketio = SocketIO(app, cors_allowed_origins=[], async_mode='threading')
# Initialize rate limiter
limiter = Limiter(
app=app,
key_func=get_remote_address,
default_limits=["200 per day", "50 per hour"],
storage_uri="memory://",
)
app.limiter = limiter
if settings['auth']['enabled']:
init_auth(app, settings, limiter, audit_svc)
register_routes(app, services, settings)
register_api_routes(app, services, settings)
register_websocket_handlers(socketio, settings)
# Start update checker
updater_svc.start_checker()
@app.teardown_appcontext
def cleanup_db(exc):
pass
if settings['kubernetes']['namespace'] == '':
try:
k8s_service.auto_detect_namespace()
if k8s_service.namespace:
settings['kubernetes']['namespace'] = k8s_service.namespace
except Exception as e:
logging.warning(f"Could not auto-detect K8s namespace: {e}")
# Check SSL certificate expiry and start background monitor
if ssl_enabled:
try:
from app.utils.ssl import check_cert_expiry, generate_cert
cert_path = str(settings['dashboard']['ssl_cert']).strip("'\"")
key_path = str(settings['dashboard']['ssl_key']).strip("'\"")
is_expiring, days_remaining, msg = check_cert_expiry(cert_path)
if is_expiring:
logging.warning(f"SSL: {msg}")
else:
logging.info(f"SSL: {msg}")
def _cert_monitor():
"""Background thread that checks and regenerates expiring SSL certs."""
import time
check_interval = settings.get('ssl', {}).get('check_interval_hours', 24) * 3600
renewal_days = settings.get('ssl', {}).get('renewal_days_before_expiry', 30)
server_ip = settings['server'].get('host', '')
ssl_dir = os.path.dirname(cert_path)
ca_cert = os.path.join(ssl_dir, 'ca.pem')
ca_key = os.path.join(ssl_dir, 'ca-key.pem')
ca_available = os.path.exists(ca_cert) and os.path.exists(ca_key)
is_le = 'letsencrypt' in cert_path.lower() or 'certbot' in cert_path.lower()
le_domain = settings['dashboard'].get('ssl_domain')
def _restart_app():
import subprocess
if os.name == 'nt':
launcher = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'start.bat')
if os.path.exists(launcher):
subprocess.Popen(['cmd', '/c', 'start', '', launcher], shell=True)
else:
subprocess.Popen([sys.executable, os.path.join(os.path.dirname(os.path.dirname(__file__)), 'run.py')], creationflags=subprocess.CREATE_NEW_CONSOLE)
else:
launcher = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'start.sh')
if os.path.exists(launcher):
subprocess.Popen(['bash', launcher], start_new_session=True)
else:
subprocess.Popen([sys.executable, os.path.join(os.path.dirname(os.path.dirname(__file__)), 'run.py')], start_new_session=True)
os._exit(0)
while True:
time.sleep(check_interval)
try:
is_expiring, days_left, _ = check_cert_expiry(cert_path, warning_days=renewal_days)
if is_expiring:
if is_le and le_domain:
logging.info(f"SSL: Let's Encrypt certificate expiring soon ({days_left} days). Running certbot renew...")
import subprocess
result = subprocess.run(['certbot', 'renew', '--quiet'], capture_output=True, timeout=300)
if result.returncode == 0:
logging.info("SSL: Let's Encrypt certificate renewed. Restarting to apply...")
_restart_app()
else:
logging.error(f"SSL: certbot renew failed: {result.stderr.decode()}")
elif ca_available:
logging.info(f"SSL: Certificate expiring soon ({days_left} days). Regenerating...")
san_ips = [server_ip, '127.0.0.1'] if server_ip else ['127.0.0.1']
generate_cert(
cert_path=cert_path,
key_path=key_path,
common_name=server_ip or 'localhost',
san_ips=san_ips,
san_dns=['localhost'],
ca_cert_path=ca_cert,
ca_key_path=ca_key,
)
logging.info("SSL: Certificate regenerated. Restarting to apply...")
_restart_app()
else:
logging.warning("SSL: Certificate expiring but no CA key available. Cannot regenerate.")
except Exception as e:
logging.error(f"SSL: Background cert check failed: {e}")
monitor_thread = threading.Thread(target=_cert_monitor, daemon=True)
monitor_thread.start()
logging.info("SSL: Background certificate monitor started")
except Exception as e:
logging.debug(f"Could not initialize SSL cert monitor: {e}")
app.dune_settings = settings
app.dune_services = services
return app, socketio
def _setup_logging(settings):
log_level = getattr(logging, settings['logging']['level'].upper(), logging.INFO)
log_file = settings['logging']['file']
os.makedirs(os.path.dirname(log_file), exist_ok=True)
handler = logging.handlers.RotatingFileHandler(
log_file,
maxBytes=settings['logging']['max_bytes'],
backupCount=settings['logging']['backup_count']
)
handler.setFormatter(logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'
))
handler.setLevel(log_level)
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(logging.Formatter(
'%(asctime)s - %(levelname)s - %(message)s'
))
console_handler.setLevel(log_level)
root_logger = logging.getLogger()
root_logger.setLevel(log_level)
root_logger.addHandler(handler)
root_logger.addHandler(console_handler)
audit_logger = logging.getLogger('audit')
audit_handler = logging.handlers.RotatingFileHandler(
'logs/audit.log',
maxBytes=settings['logging']['max_bytes'],
backupCount=settings['logging']['backup_count']
)
audit_handler.setFormatter(logging.Formatter(
'%(asctime)s - %(message)s'
))
audit_logger.addHandler(audit_handler)
audit_logger.setLevel(logging.INFO)