websocket.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. import asyncio
  2. import json
  3. import logging
  4. from typing import Any
  5. from fastapi import WebSocket
  6. logger = logging.getLogger(__name__)
  7. def _message_printer_id(message: dict[str, Any]) -> int | None:
  8. """The printer a broadcast is about, if any: top-level or inside ``data``."""
  9. printer_id = message.get("printer_id")
  10. if printer_id is None:
  11. data = message.get("data")
  12. if isinstance(data, dict):
  13. printer_id = data.get("printer_id")
  14. return printer_id if isinstance(printer_id, int) else None
  15. def _may_receive(connection: WebSocket, printer_id: int | None) -> bool:
  16. """Whether ``connection``'s printer scope (#1727) covers ``printer_id``.
  17. The scope is stamped on the socket at connect (``routes/websocket.py``).
  18. A socket without one is refused anything printer-bound, so a connection
  19. that slipped past the stamping can't receive every printer's events.
  20. """
  21. if printer_id is None:
  22. return True
  23. scope = getattr(connection.state, "bambuddy_printer_scope", None)
  24. return scope is not None and scope.allows(printer_id)
  25. class ConnectionManager:
  26. """Manages WebSocket connections and broadcasts."""
  27. def __init__(self):
  28. self.active_connections: list[WebSocket] = []
  29. self._lock = asyncio.Lock()
  30. async def connect(self, websocket: WebSocket):
  31. """Accept a new WebSocket connection."""
  32. await websocket.accept()
  33. async with self._lock:
  34. self.active_connections.append(websocket)
  35. async def disconnect(self, websocket: WebSocket):
  36. """Remove a WebSocket connection."""
  37. async with self._lock:
  38. if websocket in self.active_connections:
  39. self.active_connections.remove(websocket)
  40. async def broadcast(self, message: dict[str, Any]):
  41. """Broadcast a message to all connected clients."""
  42. if not self.active_connections:
  43. return
  44. data = json.dumps(message)
  45. printer_id = _message_printer_id(message)
  46. async with self._lock:
  47. disconnected = []
  48. for connection in self.active_connections:
  49. if not _may_receive(connection, printer_id):
  50. continue
  51. try:
  52. await connection.send_text(data)
  53. except Exception:
  54. disconnected.append(connection)
  55. # Clean up disconnected clients
  56. for conn in disconnected:
  57. if conn in self.active_connections:
  58. self.active_connections.remove(conn)
  59. async def broadcast_to_user(self, user_id: int | None, message: dict[str, Any]):
  60. """Send a message to every connection authenticated as the given user.
  61. When ``user_id`` is None the message fans out to all connections —
  62. this is the auth-disabled single-user path, where neither the queue
  63. item's ``created_by_id`` nor the WS principal is set, and the
  64. existing fan-out semantics are exactly what the user wants.
  65. Per-user routing reads ``websocket.state.bambuddy_principal_user_id``
  66. stamped at connect time (``routes/websocket.py``). Connections
  67. without a stamped id are skipped on the targeted path so an
  68. anonymous reader never receives another user's dispatch toast.
  69. """
  70. if user_id is None:
  71. await self.broadcast(message)
  72. return
  73. if not self.active_connections:
  74. return
  75. data = json.dumps(message)
  76. printer_id = _message_printer_id(message)
  77. async with self._lock:
  78. disconnected = []
  79. for connection in self.active_connections:
  80. conn_uid = getattr(connection.state, "bambuddy_principal_user_id", None)
  81. if conn_uid != user_id or not _may_receive(connection, printer_id):
  82. continue
  83. try:
  84. await connection.send_text(data)
  85. except Exception:
  86. disconnected.append(connection)
  87. for conn in disconnected:
  88. if conn in self.active_connections:
  89. self.active_connections.remove(conn)
  90. async def close_user(self, user_id: int):
  91. """Close a deleted user's sockets, which would otherwise keep receiving
  92. the messages of whoever is given their id next."""
  93. await self._close(lambda c: getattr(c.state, "bambuddy_principal_user_id", None) == user_id)
  94. async def close_signed_in(self):
  95. """Close every signed-in user's socket, after everyone is signed out (a
  96. backup restore). API-key and anonymous sockets stay: nothing ended
  97. theirs."""
  98. await self._close(lambda c: getattr(c.state, "bambuddy_principal_user_id", None) is not None)
  99. async def close_anonymous(self):
  100. """Close the sockets opened while authentication was off, which have
  101. every printer, once it is turned on."""
  102. await self._close(
  103. lambda c: getattr(c.state, "bambuddy_scope_principal", (None, None)) in ((None, None), ("", None))
  104. )
  105. async def close_api_keys(self, key_ids):
  106. """Close the sockets opened with deleted API keys. Their scope is looked
  107. up by key id on every refresh: SQLite gives the id to the next key."""
  108. key_ids = set(key_ids)
  109. await self._close(lambda c: getattr(c.state, "bambuddy_scope_principal", (None, None))[1] in key_ids)
  110. async def _close(self, matches):
  111. async with self._lock:
  112. theirs = [c for c in self.active_connections if matches(c)]
  113. for connection in theirs:
  114. self.active_connections.remove(connection)
  115. for connection in theirs:
  116. try:
  117. await connection.close(code=4401)
  118. except Exception: # noqa: BLE001 -- already gone
  119. pass
  120. async def refresh_printer_scopes(self):
  121. """Recompute every connection's printer scope (#1727).
  122. Called after an admin changes which printers a group may see, or who
  123. is in a group, so open dashboards stop (or start) receiving those
  124. printers' events without a reconnect.
  125. """
  126. from backend.app.core.auth import (
  127. is_auth_enabled,
  128. principal_may_connect,
  129. principal_printer_scope,
  130. usable_api_key_prefix,
  131. )
  132. from backend.app.core.database import async_session
  133. from backend.app.core.printer_scope import ALL_PRINTERS, PrinterScope
  134. async with self._lock:
  135. connections = list(self.active_connections)
  136. if not connections:
  137. return
  138. orphaned = []
  139. try:
  140. async with async_session() as db:
  141. auth_enabled = await is_auth_enabled(db)
  142. for connection in connections:
  143. if not auth_enabled:
  144. connection.state.bambuddy_printer_scope = ALL_PRINTERS
  145. continue
  146. username, api_key_id = getattr(connection.state, "bambuddy_scope_principal", (None, None))
  147. if api_key_id is not None:
  148. # The key is looked up by id, which SQLite gives to the
  149. # next key created: a socket whose key is gone, or whose
  150. # id another key now has, is closed, not re-scoped (a key
  151. # rotated from the command line, which can't close it).
  152. # Disabled or expired counts as gone: REST refuses it.
  153. prefix = await usable_api_key_prefix(db, api_key_id)
  154. if prefix is None or prefix != getattr(connection.state, "bambuddy_api_key_prefix", prefix):
  155. connection.state.bambuddy_printer_scope = PrinterScope(frozenset())
  156. orphaned.append(connection)
  157. continue
  158. if not await principal_may_connect(db, username, api_key_id):
  159. # websocket:connect was taken away (a group or key change)
  160. connection.state.bambuddy_printer_scope = PrinterScope(frozenset())
  161. orphaned.append(connection)
  162. continue
  163. connection.state.bambuddy_printer_scope = await principal_printer_scope(db, username, api_key_id)
  164. except Exception: # SEC-AUTH-EXC: refresh failed → fail closed (empty scope, then disconnect to re-auth)
  165. # The old scopes may be wider than what was just granted, so they
  166. # can't be kept. Drop every socket to no printers and close it with
  167. # the "unauthorised" code: the SPA mints a new token and reconnects,
  168. # and its scope is worked out afresh at connect.
  169. logger.warning("WebSocket printer scope refresh failed; disconnecting clients", exc_info=True)
  170. for connection in connections:
  171. connection.state.bambuddy_printer_scope = PrinterScope(frozenset())
  172. try:
  173. await connection.close(code=4401)
  174. except Exception: # noqa: BLE001 -- already gone; disconnect() cleans it up
  175. pass
  176. return
  177. if orphaned:
  178. await self._close(lambda c: c in orphaned)
  179. async def send_printer_status(self, printer_id: int, status: dict):
  180. """Send printer status update to all clients."""
  181. await self.broadcast(
  182. {
  183. "type": "printer_status",
  184. "printer_id": printer_id,
  185. "data": status,
  186. }
  187. )
  188. async def send_print_start(self, printer_id: int, data: dict):
  189. """Notify clients that a print has started."""
  190. await self.broadcast(
  191. {
  192. "type": "print_start",
  193. "printer_id": printer_id,
  194. "data": data,
  195. }
  196. )
  197. async def send_print_complete(self, printer_id: int, data: dict):
  198. """Notify clients that a print has completed."""
  199. await self.broadcast(
  200. {
  201. "type": "print_complete",
  202. "printer_id": printer_id,
  203. "data": data,
  204. }
  205. )
  206. async def send_print_confirm_request(self, printer_id: int, data: dict):
  207. """Ask connected clients for a post-print outcome verdict (#1898)."""
  208. await self.broadcast(
  209. {
  210. "type": "print_confirm_request",
  211. "printer_id": printer_id,
  212. "data": data,
  213. }
  214. )
  215. async def send_archive_created(self, archive: dict):
  216. """Notify clients that a new archive was created."""
  217. await self.broadcast(
  218. {
  219. "type": "archive_created",
  220. "data": archive,
  221. }
  222. )
  223. async def send_archive_updated(self, archive: dict):
  224. """Notify clients that an archive was updated."""
  225. await self.broadcast(
  226. {
  227. "type": "archive_updated",
  228. "data": archive,
  229. }
  230. )
  231. async def send_queue_item_uploading(
  232. self,
  233. user_id: int | None,
  234. queue_item_id: int,
  235. printer_id: int,
  236. printer_name: str | None,
  237. file_name: str,
  238. total_bytes: int,
  239. ):
  240. """Toast trigger: scheduler picked the item up, FTP upload starts."""
  241. await self.broadcast_to_user(
  242. user_id,
  243. {
  244. "type": "queue_item_uploading",
  245. "queue_item_id": queue_item_id,
  246. "printer_id": printer_id,
  247. "printer_name": printer_name,
  248. "file_name": file_name,
  249. "total_bytes": total_bytes,
  250. },
  251. )
  252. async def send_queue_item_upload_progress(
  253. self,
  254. user_id: int | None,
  255. queue_item_id: int,
  256. bytes_transferred: int,
  257. total_bytes: int,
  258. ):
  259. """Toast update: throttled byte-level progress during the FTP upload."""
  260. pct = int(round(100 * bytes_transferred / total_bytes)) if total_bytes else 0
  261. await self.broadcast_to_user(
  262. user_id,
  263. {
  264. "type": "queue_item_upload_progress",
  265. "queue_item_id": queue_item_id,
  266. "bytes_transferred": bytes_transferred,
  267. "total_bytes": total_bytes,
  268. "pct": pct,
  269. },
  270. )
  271. async def send_queue_item_acked(
  272. self,
  273. user_id: int | None,
  274. queue_item_id: int,
  275. printer_id: int,
  276. ):
  277. """Toast trigger: watchdog confirmed the printer transitioned out of pre_state."""
  278. await self.broadcast_to_user(
  279. user_id,
  280. {
  281. "type": "queue_item_acked",
  282. "queue_item_id": queue_item_id,
  283. "printer_id": printer_id,
  284. },
  285. )
  286. async def send_queue_item_failed(
  287. self,
  288. user_id: int | None,
  289. queue_item_id: int,
  290. printer_id: int | None,
  291. reason: str,
  292. ):
  293. """Toast trigger: dispatch failed at any stage. Toast turns red, auto-dismisses."""
  294. await self.broadcast_to_user(
  295. user_id,
  296. {
  297. "type": "queue_item_failed",
  298. "queue_item_id": queue_item_id,
  299. "printer_id": printer_id,
  300. "reason": reason,
  301. },
  302. )
  303. async def send_missing_spool_assignment(
  304. self,
  305. printer_id: int,
  306. printer_name: str,
  307. missing_slots: list[dict[str, str]],
  308. ):
  309. """Notify clients that a print started with missing spool assignments."""
  310. await self.broadcast(
  311. {
  312. "type": "missing_spool_assignment",
  313. "printer_id": printer_id,
  314. "printer_name": printer_name,
  315. "missing_slots": missing_slots,
  316. }
  317. )
  318. # Global connection manager
  319. ws_manager = ConnectionManager()