|
3 | 3 | import json |
4 | 4 | import os |
5 | 5 | import re |
| 6 | +import socket |
6 | 7 | import subprocess |
7 | 8 | from dataclasses import dataclass |
8 | 9 | from pathlib import Path |
|
26 | 27 | @dataclass |
27 | 28 | class BrainSnapshot: |
28 | 29 | api_base_url: str |
| 30 | + local_node: dict[str, str] |
| 31 | + remote_nodes: list[dict[str, str]] |
29 | 32 | cloud_online: bool |
30 | 33 | cloud_version: str |
31 | 34 | notifications_total: int |
@@ -221,6 +224,81 @@ def list_api_endpoints() -> list[tuple[str, str, str]]: |
221 | 224 | ] |
222 | 225 |
|
223 | 226 |
|
| 227 | +def get_local_node() -> dict[str, str]: |
| 228 | + hostname = socket.gethostname() |
| 229 | + ip = "" |
| 230 | + for iface in ("en0", "en1", "bridge0"): |
| 231 | + try: |
| 232 | + out = subprocess.check_output(["ipconfig", "getifaddr", iface], text=True).strip() |
| 233 | + if out: |
| 234 | + ip = out |
| 235 | + break |
| 236 | + except Exception: |
| 237 | + continue |
| 238 | + if not ip: |
| 239 | + try: |
| 240 | + ip = socket.gethostbyname(hostname) |
| 241 | + except Exception: |
| 242 | + ip = "未识别" |
| 243 | + return { |
| 244 | + "name": "本机终端", |
| 245 | + "node_id": "local-terminal", |
| 246 | + "hostname": hostname, |
| 247 | + "ip": ip or "未识别", |
| 248 | + "environment": "local", |
| 249 | + "region": "asia-shanghai", |
| 250 | + "source": "cortexos", |
| 251 | + } |
| 252 | + |
| 253 | + |
| 254 | +def _extract_public_ip(item: dict) -> str: |
| 255 | + metadata = item.get("metadata") or {} |
| 256 | + for key in ("publicIp", "public_ip", "nodeIp", "node_ip", "ip"): |
| 257 | + value = str(metadata.get(key) or "").strip() |
| 258 | + if value: |
| 259 | + return value |
| 260 | + content = str(item.get("content") or "") |
| 261 | + matches = re.findall(r"\b(?:\d{1,3}\.){3}\d{1,3}\b", content) |
| 262 | + for ip in matches: |
| 263 | + if not ip.startswith("127."): |
| 264 | + return ip |
| 265 | + return "未上报" |
| 266 | + |
| 267 | + |
| 268 | +def list_remote_nodes(notifications: list[dict], limit: int = 8) -> list[dict[str, str]]: |
| 269 | + seen: set[str] = set() |
| 270 | + nodes: list[dict[str, str]] = [] |
| 271 | + for item in notifications: |
| 272 | + metadata = item.get("metadata") or {} |
| 273 | + node_id = str( |
| 274 | + metadata.get("nodeId") |
| 275 | + or metadata.get("node_id") |
| 276 | + or item.get("source") |
| 277 | + or item.get("agent") |
| 278 | + or "" |
| 279 | + ).strip() |
| 280 | + if not node_id or node_id == "cortexos": |
| 281 | + continue |
| 282 | + if node_id in seen: |
| 283 | + continue |
| 284 | + seen.add(node_id) |
| 285 | + nodes.append( |
| 286 | + { |
| 287 | + "name": str(metadata.get("nodeName") or metadata.get("node_name") or item.get("source") or node_id), |
| 288 | + "node_id": node_id, |
| 289 | + "source": str(item.get("source") or item.get("agent") or "unknown"), |
| 290 | + "model": str(metadata.get("model") or item.get("agent") or "unknown"), |
| 291 | + "environment": str(metadata.get("environment") or "unknown"), |
| 292 | + "region": str(metadata.get("region") or "unknown"), |
| 293 | + "hostname": str(metadata.get("hostname") or "unknown"), |
| 294 | + "ip": _extract_public_ip(item), |
| 295 | + } |
| 296 | + ) |
| 297 | + if len(nodes) >= limit: |
| 298 | + break |
| 299 | + return nodes |
| 300 | + |
| 301 | + |
224 | 302 | def _parse_listen_name(name: str) -> tuple[str, str, str]: |
225 | 303 | text = (name or "").strip() |
226 | 304 | if not text: |
@@ -304,6 +382,8 @@ def build_snapshot() -> BrainSnapshot: |
304 | 382 |
|
305 | 383 | return BrainSnapshot( |
306 | 384 | api_base_url=BRAIN_API_URL, |
| 385 | + local_node=get_local_node(), |
| 386 | + remote_nodes=list_remote_nodes(notifications), |
307 | 387 | cloud_online=cloud_online, |
308 | 388 | cloud_version=cloud_version, |
309 | 389 | notifications_total=len(notifications), |
|
0 commit comments