Просмотр исходного кода

feat: support stats realtime for squid V7

Manuel 1 год назад
Родитель
Сommit
4fdc6d88f0
2 измененных файлов с 58 добавлено и 16 удалено
  1. 58 8
      parsers/squid_info.py
  2. 0 8
      routes/main_routes.py

+ 58 - 8
parsers/squid_info.py

@@ -1,3 +1,4 @@
+import base64
 import os
 import re
 import socket
@@ -9,6 +10,8 @@ load_dotenv()
 
 SQUID_HOST = os.getenv("SQUID_HOST", "127.0.0.1")
 SQUID_PORT = int(os.getenv("SQUID_PORT", "3128"))
+SQUID_MGR_USER = os.getenv("SQUID_MGR_USER")
+SQUID_MGR_PASS = os.getenv("SQUID_MGR_PASS")
 
 _SQUID_DATE_FMT = "%a, %d %b %Y %H:%M:%S %Z"
 
@@ -27,6 +30,25 @@ def _re_int(key: str, text: str, default=0):
     return int(_re_float(key, text, default))
 
 
+def _format_host_header(host: str, port: int) -> str:
+    # Bracket IPv6 literals
+    if ":" in host and not host.startswith("["):
+        return f"[{host}]:{port}"
+    return f"{host}:{port}"
+
+
+def _send_http_request(host: str, port: int, request: str, timeout: float = 5.0) -> str:
+    with socket.create_connection((host, port), timeout=timeout) as s:
+        s.sendall(request.encode("utf-8"))
+        response = b""
+        while True:
+            chunk = s.recv(4096)
+            if not chunk:
+                break
+            response += chunk
+    return response.decode("utf-8", errors="replace")
+
+
 def fetch_squid_info_stats():
     default_stats = {
         "start_time": None,
@@ -65,15 +87,43 @@ def fetch_squid_info_stats():
     }
 
     try:
-        with socket.create_connection((SQUID_HOST, SQUID_PORT), timeout=5) as s:
-            request = f"GET cache_object://{SQUID_HOST}/info HTTP/1.0\r\n\r\n"
-            s.sendall(request.encode())
-            response = b""
-            while chunk := s.recv(4096):
-                response += chunk
-        data = response.decode("utf-8")
+        host_header = _format_host_header(SQUID_HOST, int(SQUID_PORT))
+        headers = [
+            f"Host: {host_header}",
+            "User-Agent: SquidStats/1.0",
+            "Accept: */*",
+            "Connection: close",
+        ]
+        if SQUID_MGR_USER and SQUID_MGR_PASS:
+            token = base64.b64encode(
+                f"{SQUID_MGR_USER}:{SQUID_MGR_PASS}".encode()
+            ).decode()
+            headers.append(f"Authorization: Basic {token}")
+
+        modern_req = (
+            "GET /squid-internal-mgr/info HTTP/1.1\r\n"
+            + "\r\n".join(headers)
+            + "\r\n\r\n"
+        )
+        response_text = _send_http_request(
+            SQUID_HOST, int(SQUID_PORT), modern_req, timeout=5.0
+        )
+
+        first_line = response_text.splitlines()[0] if response_text else ""
+        if " 400 " in first_line or "Bad Request" in response_text:
+            legacy_req = (
+                f"GET cache_object://{SQUID_HOST}/info HTTP/1.0\r\n"
+                f"Host: {host_header}\r\n"
+                "User-Agent: SquidStats/1.0\r\n"
+                "Accept: */*\r\n\r\n"
+            )
+            response_text = _send_http_request(
+                SQUID_HOST, int(SQUID_PORT), legacy_req, timeout=5.0
+            )
+
+        parts = response_text.split("\r\n\r\n", 1)
+        data = parts[1] if len(parts) > 1 else response_text
     except Exception as e:
-        # Replicar la misma lógica de errores que en el ejemplo
         default_stats["error"] = str(e)
         if isinstance(e, TimeoutError):
             default_stats["connection_status"] = "timeout"

+ 0 - 8
routes/main_routes.py

@@ -35,7 +35,6 @@ def initialize_proxy_detection():
 
 
 def _build_error_page(message: str, status: int = 500, details: str | None = None):
-    """Helper to render a standardized error page."""
     if details:
         logger.debug(f"Error details: {details}")
     return (
@@ -49,10 +48,6 @@ def _build_error_page(message: str, status: int = 500, details: str | None = Non
 
 
 def _get_dashboard_context() -> tuple[dict[str, Any] | None, tuple[Any, int] | None]:
-    """
-    Build context for the dashboard.
-    Returns (context_dict, error_response) where only one will be not None.
-    """
     t0 = time.time()
     try:
         raw_data = fetch_squid_data()
@@ -124,9 +119,7 @@ def index():
 def actualizar_conexiones():
     context, error_response = _get_dashboard_context()
     if error_response:
-        # Para la petición parcial devolvemos solo texto simple o el fragmento de error
         return ("Error interno al refrescar conexiones", error_response[1])
-    # Reutilizamos mismo contexto pero plantilla parcial
     return render_template(
         "partials/conexiones.html",
         grouped_connections=context["grouped_connections"],
@@ -149,7 +142,6 @@ def install_package():
             logger.warning("update_squid() retornó False en /install")
     except Exception:
         logger.exception("Error ejecutando actualización en /install")
-    # Podríamos agregar un query param para feedback
     return redirect(f"/?install_status={'ok' if ok else 'fail'}")