fix: stop panel stats from hanging on Clash /traffic WebSocket

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-16 10:59:45 +08:00
parent d75193d527
commit db251c39bf
2 changed files with 33 additions and 24 deletions
+20 -19
View File
@@ -18,6 +18,7 @@ CLASH_ADDR = "127.0.0.1:9090"
_speed_cache: dict[int, tuple[float, int, int]] = {}
_conn_cache: dict[str, dict[str, int | str]] = {}
_global_bytes_cache: tuple[float, int, int] | None = None
def _load_env() -> dict[str, str]:
@@ -63,20 +64,21 @@ def fetch_clash_connections() -> tuple[list[dict], bool]:
return payload.get("connections") or [], True
def fetch_clash_traffic() -> tuple[int, int, bool]:
"""返回 (upload B/s, download B/s, ok)"""
env = _load_env()
secret = env.get("CLASH_API_SECRET", "")
url = f"http://{CLASH_ADDR}/traffic"
req = urllib.request.Request(url)
if secret:
req.add_header("Authorization", f"Bearer {secret}")
try:
with urllib.request.urlopen(req, timeout=3) as resp:
payload = json.loads(resp.read().decode("utf-8"))
except (urllib.error.URLError, TimeoutError, json.JSONDecodeError, OSError):
return 0, 0, False
return int(payload.get("up") or 0), int(payload.get("down") or 0), True
def _global_conn_speed(connections: list[dict]) -> tuple[float, float]:
"""从 /connections 汇总字节增量估算全局速率(/traffic 为 WebSocket 流,不能同步 HTTP 读)"""
total_up = sum(int(c.get("upload") or 0) for c in connections)
total_down = sum(int(c.get("download") or 0) for c in connections)
now = time.time()
global _global_bytes_cache
prev = _global_bytes_cache
_global_bytes_cache = (now, total_up, total_down)
if not prev:
return 0.0, 0.0
t0, u0, d0 = prev
dt = now - t0
if dt <= 0:
return 0.0, 0.0
return max(0.0, (total_up - u0) / dt), max(0.0, (total_down - d0) / dt)
def _connection_user(conn: dict) -> str:
@@ -254,12 +256,11 @@ def collect_node_stats() -> dict:
nodes = list_nodes()
uuid_to_node = {node["uuid"]: int(node["id"]) for node in nodes}
connections, clash_ok = fetch_clash_connections()
traffic_up, traffic_down, traffic_ok = fetch_clash_traffic()
active_by_uuid = _sync_connections(connections, nodes, uuid_to_node)
singbox_ok = clash_ok or traffic_ok
single_node = len(nodes) == 1
has_connections = len(connections) > 0
global_active = (traffic_up + traffic_down) > 512
global_up_speed, global_down_speed = _global_conn_speed(connections) if clash_ok else (0.0, 0.0)
global_active = (global_up_speed + global_down_speed) > 512
result_nodes: dict[str, dict] = {}
summary_online = 0
@@ -279,8 +280,8 @@ def collect_node_stats() -> dict:
if not matched and single_node and has_connections:
matched = connections
if not matched and single_node and global_active:
up_speed = float(traffic_up)
down_speed = float(traffic_down)
up_speed = global_up_speed
down_speed = global_down_speed
online = (
len(matched) > 0