-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebsocket.py
More file actions
168 lines (142 loc) · 6.16 KB
/
websocket.py
File metadata and controls
168 lines (142 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from __future__ import annotations
import asyncio
import json
import logging
from datetime import datetime
from typing import Any, TypeVar
from fastapi import APIRouter, WebSocket, WebSocketDisconnect
from sqlmodel import Session, select
import murfey.server.prometheus as prom
from murfey.server.murfey_db import get_murfey_db_session
from murfey.util import sanitise
from murfey.util.db import ClientEnvironment
T = TypeVar("T")
ws = APIRouter(prefix="/ws", tags=["Websocket"])
log = logging.getLogger("murfey.server.websocket")
class ConnectionManager:
def __init__(self):
self.active_connections: dict[int | str, WebSocket] = {}
async def connect(
self,
websocket: WebSocket,
client_id: int | str,
register_client: bool = True,
):
await websocket.accept()
self.active_connections[client_id] = websocket
if register_client:
if not isinstance(client_id, int):
raise ValueError(
"To register a client the client ID must be an integer"
)
self._register_new_client(client_id)
@staticmethod
def _register_new_client(client_id: int):
log.debug(f"Registering new client with ID {client_id}")
new_client = ClientEnvironment(client_id=client_id, connected=True)
murfey_db: Session = next(get_murfey_db_session())
murfey_db.add(new_client)
murfey_db.commit()
murfey_db.close()
def disconnect(self, client_id: int | str, unregister_client: bool = True):
self.active_connections.pop(client_id)
if unregister_client:
murfey_db: Session = next(get_murfey_db_session())
client_env = murfey_db.exec(
select(ClientEnvironment).where(
ClientEnvironment.client_id == client_id
)
).one()
murfey_db.delete(client_env)
murfey_db.commit()
murfey_db.close()
async def broadcast(self, message: str):
for connection in self.active_connections:
await self.active_connections[connection].send_text(message)
manager = ConnectionManager()
@ws.websocket("/test/{client_id}")
async def websocket_endpoint(websocket: WebSocket, client_id: int):
await manager.connect(websocket, client_id)
await manager.broadcast(f"Client {client_id} joined")
try:
while True:
data = await websocket.receive_text()
try:
json_data: dict = json.loads(data)
if json_data["type"] == "log": # and isinstance(json_data, dict)
json_data.pop("type")
await forward_log(json_data, websocket)
except Exception:
await manager.broadcast(f"Client #{client_id} sent message {data}")
except WebSocketDisconnect:
log.info(f"Disconnecting Client {int(sanitise(str(client_id)))}")
murfey_db = next(get_murfey_db_session())
client_env = murfey_db.exec(
select(ClientEnvironment).where(ClientEnvironment.client_id == client_id)
).one()
prom.monitoring_switch.labels(visit=client_env.visit).set(0)
manager.disconnect(client_id)
await manager.broadcast(f"Client #{client_id} disconnected")
@ws.websocket("/connect/{client_id}")
async def websocket_connection_endpoint(
websocket: WebSocket,
client_id: int | str,
):
await manager.connect(websocket, client_id, register_client=False)
await manager.broadcast(f"Client {client_id} joined")
try:
while True:
data = await websocket.receive_text()
try:
json_data: dict = json.loads(data)
if json_data.get("type") == "log": # and isinstance(json_data, dict)
json_data.pop("type")
await forward_log(json_data, websocket)
elif json_data.get("message") == "refresh":
await manager.broadcast(json.dumps(json_data))
except Exception:
await manager.broadcast(f"Client #{client_id} sent message {data}")
except WebSocketDisconnect:
log.info(f"Disconnecting Client {sanitise(str(client_id))}")
manager.disconnect(client_id, unregister_client=False)
await manager.broadcast(f"Client #{client_id} disconnected")
async def check_connections(active_connections: list[WebSocket]):
log.info("Checking connections")
for connection in active_connections:
log.info("Checking response")
try:
await asyncio.wait_for(connection.receive(), timeout=10)
except asyncio.TimeoutError:
log.info(f"Disconnecting Client {connection[0]}")
manager.disconnect(connection[0], connection[1])
async def forward_log(logrecord: dict[str, Any], websocket: WebSocket):
record_name = logrecord["name"]
logrecord.pop("msecs", None)
logrecord.pop("relativeCreated", None)
client_timestamp = logrecord.pop("created", 0)
if client_timestamp:
logrecord["client_time"] = datetime.fromtimestamp(client_timestamp).isoformat()
logrecord["client_host"] = websocket.client.host
logging.getLogger(record_name).handle(logging.makeLogRecord(logrecord))
@ws.delete("/test/{client_id}")
async def close_ws_connection(client_id: int):
murfey_db: Session = next(get_murfey_db_session())
client_env = murfey_db.exec(
select(ClientEnvironment).where(ClientEnvironment.client_id == client_id)
).one()
client_env.connected = False
visit_name = client_env.visit
murfey_db.add(client_env)
murfey_db.commit()
murfey_db.close()
client_id_str = str(client_id).replace("\r\n", "").replace("\n", "")
log.info(f"Disconnecting {client_id_str}")
manager.disconnect(client_id)
prom.monitoring_switch.labels(visit=visit_name).set(0)
await manager.broadcast(f"Client #{client_id} disconnected")
@ws.delete("/connect/{client_id}")
async def close_unrecorded_ws_connection(client_id: int | str):
client_id_str = str(client_id).replace("\r\n", "").replace("\n", "")
log.info(f"Disconnecting {client_id_str}")
manager.disconnect(client_id)
await manager.broadcast(f"Client #{client_id} disconnected")