| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- import os
- import time
- from typing import Any
- from flask import Blueprint, flash, jsonify, redirect, render_template, request
- from flask_babel import gettext as _
- from loguru import logger
- from config import Config
- from parsers.connections import group_by_user, parse_raw_data
- from parsers.squid_info import fetch_squid_info_stats
- from routes.admin.helpers import json_error, json_success, sanitize_error_page_message
- from services.notifications.notifications import get_all_notifications
- from services.squid.fetch_data import fetch_all_squid_data
- from services.system.system_info import get_system_type
- from utils.updateSquid import check_squid_update, update_squid
- from utils.updateSquidStats import (
- check_web_update,
- is_deb_installation,
- updateSquidStats,
- )
- main_bp = Blueprint("main", __name__)
- # Caché simple en memoria para el estado de actualizaciones (5 minutos)
- _update_status_cache = {"data": None, "timestamp": 0}
- _UPDATE_STATUS_TTL = 300
- def filter_valid_users(grouped_connections):
- """
- Filter valid users by removing anonymous and empty users
- This function centralizes the filtering logic that was previously in the template
- """
- valid_users = {}
- for user, user_data in grouped_connections.items():
- if user and user != "-" and user != "Anónimo":
- valid_users[user] = user_data
- return valid_users
- @main_bp.app_context_processor
- def inject_app_version():
- """Inject the application version into all templates"""
- version = getattr(Config, "VERSION", None) or os.getenv("VERSION", "-")
- return {"app_version": version, "is_deb_install": is_deb_installation()}
- def _build_error_page(message: str, status: int = 500):
- """
- Build a standardized error page
- """
- safe_message = sanitize_error_page_message(message)
- return (
- render_template(
- "error.html",
- message=safe_message,
- ),
- status,
- )
- @main_bp.route("/api/update-status")
- def update_status():
- """Devuelve el estado de actualizaciones disponibles para Squid y la web.
- Utiliza un caché en memoria de 5 minutos para no saturar las APIs de GitHub.
- """
- global _update_status_cache
- now = time.time()
- cached = _update_status_cache
- if cached["data"] and (now - cached["timestamp"] < _UPDATE_STATUS_TTL):
- return jsonify(cached["data"])
- try:
- squid_status = check_squid_update()
- except Exception:
- logger.exception("Error checking Squid update status")
- squid_status = {
- "available": False,
- "current": None,
- "latest": None,
- "error": "Error interno al verificar actualización de Squid",
- }
- try:
- current_version = getattr(Config, "VERSION", None) or os.getenv("VERSION", "0")
- web_status = check_web_update(current_version)
- except Exception:
- logger.exception("Error checking SquidStats update status")
- web_status = {
- "available": False,
- "current": getattr(Config, "VERSION", None) or os.getenv("VERSION", "0"),
- "latest": None,
- "error": "Error interno al verificar actualización de SquidStats",
- }
- data = {
- "squid": squid_status,
- "web": web_status,
- }
- _update_status_cache = {"data": data, "timestamp": now}
- return jsonify(data)
- def _get_dashboard_context() -> tuple[dict[str, Any] | None, tuple[Any, int] | None]:
- """
- Get and process the context for the dashboard
- Returns: (context_dict, error_response) - only one will be not None
- """
- t0 = time.time()
- try:
- proxy_results = fetch_all_squid_data()
- # Collect per-proxy status for the UI
- proxy_statuses: list[dict] = []
- all_connections: list = []
- for proxy in proxy_results:
- raw_data = proxy["data"]
- label = proxy["label"]
- ok = proxy["ok"]
- proxy_statuses.append({"label": label, "ok": ok})
- if not ok:
- logger.warning(f"Could not fetch data from proxy {label}: {raw_data}")
- continue
- try:
- conns = parse_raw_data(raw_data, source_host=label)
- all_connections.extend(conns)
- except Exception:
- logger.exception(f"Error parsing connections from proxy {label}")
- # if not all_connections and all(not p["ok"] for p in proxy_results):
- # logger.error("All Squid proxies returned errors")
- # return None, _build_error_page("No data from any Squid proxy", 502)
- connections = all_connections
- try:
- grouped_connections = group_by_user(connections)
- except Exception:
- logger.exception("Error grouping connections by user")
- grouped_connections = {}
- # CENTRALIZED FILTERING: Generate valid users here instead of in the template
- valid_users = filter_valid_users(grouped_connections)
- try:
- squid_info_stats = fetch_squid_info_stats()
- except Exception:
- logger.exception("Error getting detailed Squid statistics")
- squid_info_stats = {}
- squid_version = (
- connections[0].get("squid_version", "No disponible")
- if connections
- else "No disponible"
- )
- multi_proxy = len(proxy_results) > 1
- context: dict[str, Any] = {
- "grouped_connections": grouped_connections,
- "valid_users": valid_users,
- "squid_version": squid_version,
- "squid_info_stats": squid_info_stats,
- "page_icon": "favicon.ico",
- "page_title": _("Inicio Dashboard"),
- "build_time_ms": int((time.time() - t0) * 1000),
- "connection_count": len(connections),
- "system_type": get_system_type(),
- "proxy_statuses": proxy_statuses,
- "multi_proxy": multi_proxy,
- }
- return context, None
- except Exception: # Fallback catch-all
- logger.exception("Unexpected failure building dashboard context")
- return None, _build_error_page("Unexpected internal failure", 500)
- @main_bp.route("/")
- def index():
- # CHANGE: Detect if this is a request for partial content
- is_partial_request = request.args.get("partial") == "true"
- context, error_response = _get_dashboard_context()
- if error_response:
- return error_response
- # CHANGE: If it's a partial request, return only the connections template
- if is_partial_request:
- return render_template(
- "partials/conexiones.html",
- grouped_connections=context["grouped_connections"],
- valid_users=context["valid_users"],
- squid_version=context["squid_version"],
- squid_info_stats=context["squid_info_stats"],
- build_time_ms=context["build_time_ms"],
- connection_count=context["connection_count"],
- system_type=context["system_type"],
- proxy_statuses=context["proxy_statuses"],
- multi_proxy=context["multi_proxy"],
- )
- # Normal request: return the complete page
- return render_template("index.html", **context)
- @main_bp.route("/install", methods=["POST"])
- def install_package():
- """Route to install/update Squid packages"""
- try:
- ok, error_message = update_squid()
- if ok:
- logger.info("SquidStats update (install) completed successfully")
- message = _("Squid actualizado correctamente.")
- if request.accept_mimetypes.best == "application/json":
- return json_success(message)
- flash(message, "success")
- return redirect("/")
- logger.warning("update_squid() returned False in /install")
- message = error_message or _(
- "Error al actualizar Squid. Revise los logs del servidor."
- )
- if request.accept_mimetypes.best == "application/json":
- return json_error(message, 503)
- flash(message, "error")
- except Exception:
- logger.exception("Error executing update in /install")
- message = _(
- "Error inesperado al actualizar Squid. Revise los logs del servidor."
- )
- if request.accept_mimetypes.best == "application/json":
- return json_error(message, 500)
- flash(message, "error")
- return redirect("/")
- @main_bp.route("/update", methods=["POST"])
- def update_web():
- """Route to update the web application"""
- logger.info("Received web update request")
- ok = False
- try:
- ok = updateSquidStats()
- if ok:
- logger.info("SquidStats web update completed")
- flash(
- _(
- "SquidStats actualizado correctamente. El servicio se ha reiniciado."
- ),
- "success",
- )
- else:
- logger.warning("updateSquidStats() returned False in /update")
- flash(
- _("Error al actualizar SquidStats. Revise los logs del servidor."),
- "error",
- )
- except Exception:
- logger.exception("Error executing update in /update")
- flash(
- _(
- "Error inesperado al actualizar SquidStats. Revise los logs del servidor."
- ),
- "error",
- )
- return redirect("/")
- @main_bp.route("/all-notifications")
- def all_notifications():
- """Page to view all notifications with pagination"""
- try:
- # Get pagination parameters
- page = request.args.get("page", 1, type=int)
- per_page = request.args.get("per_page", 20, type=int)
- # Validate parameters
- if page < 1:
- page = 1
- if per_page not in [10, 20, 50, 100]:
- per_page = 20
- # Get notifications with pagination
- notifications_data = get_all_notifications(
- limit=None, page=page, per_page=per_page
- )
- return render_template(
- "all_notifications.html",
- page_title=_("Todas las Notificaciones"),
- page_icon="favicon.ico",
- subtitle=_("Historial completo de notificaciones del sistema"),
- icon="fas fa-bell",
- notifications=notifications_data["notifications"],
- unread_count=notifications_data["unread_count"],
- pagination=notifications_data.get(
- "pagination",
- {
- "current_page": page,
- "per_page": per_page,
- "total_pages": 1,
- "total_notifications": len(notifications_data["notifications"]),
- "has_prev": False,
- "has_next": False,
- },
- ),
- )
- except Exception as e:
- logger.error(f"Error loading notifications page: {e}")
- return render_template(
- "all_notifications.html",
- page_title=_("Todas las Notificaciones"),
- page_icon="favicon.ico",
- subtitle=_("Error al cargar las notificaciones"),
- icon="fa-bell",
- notifications=[],
- unread_count=0,
- pagination=None,
- )
|