replx-agent 2.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of replx-agent might be problematic. Click here for more details.
- replx_agent/__init__.py +3 -0
- replx_agent/__main__.py +5 -0
- replx_agent/commands.py +131 -0
- replx_agent/helpers/__init__.py +3 -0
- replx_agent/helpers/scanner.py +308 -0
- replx_agent/pkg/__init__.py +11 -0
- replx_agent/pkg/compiler.py +153 -0
- replx_agent/pkg/registry.py +387 -0
- replx_agent/pkg/service.py +690 -0
- replx_agent/pkg/store.py +83 -0
- replx_agent/protocol/__init__.py +11 -0
- replx_agent/protocol/repl.py +1029 -0
- replx_agent/protocol/storage.py +1018 -0
- replx_agent/rpc/__init__.py +3 -0
- replx_agent/rpc/protocol.py +220 -0
- replx_agent/server/__init__.py +3 -0
- replx_agent/server/__main__.py +4 -0
- replx_agent/server/command_dispatcher.py +49 -0
- replx_agent/server/connection_manager.py +654 -0
- replx_agent/server/core.py +1221 -0
- replx_agent/server/handlers/__init__.py +24 -0
- replx_agent/server/handlers/exec.py +553 -0
- replx_agent/server/handlers/filesystem.py +383 -0
- replx_agent/server/handlers/firmware.py +118 -0
- replx_agent/server/handlers/i2c.py +23 -0
- replx_agent/server/handlers/package.py +182 -0
- replx_agent/server/handlers/repl.py +188 -0
- replx_agent/server/handlers/session.py +205 -0
- replx_agent/server/handlers/spi.py +23 -0
- replx_agent/server/handlers/transfer.py +600 -0
- replx_agent/server/handlers/uart.py +32 -0
- replx_agent/server/session_manager.py +391 -0
- replx_agent/server/tcp.py +207 -0
- replx_agent/terminal.py +566 -0
- replx_agent/transport/__init__.py +32 -0
- replx_agent/transport/serial.py +220 -0
- replx_agent/utils/__init__.py +95 -0
- replx_agent/utils/constants.py +78 -0
- replx_agent/utils/device_info.py +183 -0
- replx_agent/utils/exceptions.py +31 -0
- replx_agent-2.0.0.dist-info/METADATA +74 -0
- replx_agent-2.0.0.dist-info/RECORD +46 -0
- replx_agent-2.0.0.dist-info/WHEEL +5 -0
- replx_agent-2.0.0.dist-info/entry_points.txt +2 -0
- replx_agent-2.0.0.dist-info/licenses/LICENSE +21 -0
- replx_agent-2.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import sys
|
|
3
|
+
import time
|
|
4
|
+
import threading
|
|
5
|
+
import ipaddress
|
|
6
|
+
from dataclasses import dataclass, field
|
|
7
|
+
from typing import Optional, Dict, Any, List, Set, Tuple, Hashable
|
|
8
|
+
|
|
9
|
+
from replx_agent.utils import canon_port as _canon_port
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass
|
|
13
|
+
class Session:
|
|
14
|
+
ppid: int
|
|
15
|
+
client_ip: str = '127.0.0.1'
|
|
16
|
+
foreground: Optional[str] = None
|
|
17
|
+
# Ordered list (most-recently added last). When the foreground is removed,
|
|
18
|
+
# the most recent background is promoted, giving deterministic behaviour.
|
|
19
|
+
backgrounds: List[str] = field(default_factory=list)
|
|
20
|
+
last_access: float = field(default_factory=time.time)
|
|
21
|
+
default_port: Optional[str] = None
|
|
22
|
+
|
|
23
|
+
def _remove_bg(self, port: str) -> bool:
|
|
24
|
+
try:
|
|
25
|
+
self.backgrounds.remove(port)
|
|
26
|
+
return True
|
|
27
|
+
except ValueError:
|
|
28
|
+
return False
|
|
29
|
+
|
|
30
|
+
def add_connection(self, port: str, as_foreground: bool = False):
|
|
31
|
+
port = _canon_port(port)
|
|
32
|
+
if as_foreground:
|
|
33
|
+
if self.foreground and self.foreground != port:
|
|
34
|
+
# Demote previous foreground to most-recent background.
|
|
35
|
+
self._remove_bg(self.foreground)
|
|
36
|
+
self.backgrounds.append(self.foreground)
|
|
37
|
+
self.foreground = port
|
|
38
|
+
self._remove_bg(port)
|
|
39
|
+
else:
|
|
40
|
+
if not self.foreground or self.foreground != port:
|
|
41
|
+
# Move-to-end semantics so promoted bg is the most recent.
|
|
42
|
+
self._remove_bg(port)
|
|
43
|
+
self.backgrounds.append(port)
|
|
44
|
+
self.last_access = time.time()
|
|
45
|
+
|
|
46
|
+
def remove_connection(self, port: str) -> bool:
|
|
47
|
+
port = _canon_port(port)
|
|
48
|
+
was_foreground = self.foreground and self.foreground == port
|
|
49
|
+
if was_foreground:
|
|
50
|
+
# Promote the most recently used background.
|
|
51
|
+
self.foreground = self.backgrounds.pop() if self.backgrounds else None
|
|
52
|
+
else:
|
|
53
|
+
self._remove_bg(port)
|
|
54
|
+
self.last_access = time.time()
|
|
55
|
+
return was_foreground
|
|
56
|
+
|
|
57
|
+
def get_all_connections(self) -> List[str]:
|
|
58
|
+
result = list(self.backgrounds)
|
|
59
|
+
if self.foreground:
|
|
60
|
+
result.insert(0, self.foreground)
|
|
61
|
+
return result
|
|
62
|
+
|
|
63
|
+
def has_connection(self, port: str) -> bool:
|
|
64
|
+
port = _canon_port(port)
|
|
65
|
+
if self.foreground and self.foreground == port:
|
|
66
|
+
return True
|
|
67
|
+
return port in self.backgrounds
|
|
68
|
+
|
|
69
|
+
def is_empty(self) -> bool:
|
|
70
|
+
return self.foreground is None and not self.backgrounds
|
|
71
|
+
|
|
72
|
+
def switch_foreground(self, port: str) -> bool:
|
|
73
|
+
port = _canon_port(port)
|
|
74
|
+
if self.foreground and self.foreground == port:
|
|
75
|
+
return True
|
|
76
|
+
|
|
77
|
+
if port not in self.backgrounds:
|
|
78
|
+
return False
|
|
79
|
+
|
|
80
|
+
if self.foreground:
|
|
81
|
+
self._remove_bg(self.foreground)
|
|
82
|
+
self.backgrounds.append(self.foreground)
|
|
83
|
+
|
|
84
|
+
self._remove_bg(port)
|
|
85
|
+
self.foreground = port
|
|
86
|
+
self.last_access = time.time()
|
|
87
|
+
|
|
88
|
+
return True
|
|
89
|
+
|
|
90
|
+
class SessionManager:
|
|
91
|
+
REMOTE_SESSION_TTL = 300.0
|
|
92
|
+
|
|
93
|
+
def __init__(self):
|
|
94
|
+
self._sessions: Dict[Hashable, Session] = {}
|
|
95
|
+
self._sessions_lock = threading.RLock()
|
|
96
|
+
self._last_zombie_check = time.time()
|
|
97
|
+
self._zombie_check_interval = 60
|
|
98
|
+
|
|
99
|
+
@staticmethod
|
|
100
|
+
def session_key(ppid: int, client_ip: str = None) -> Optional[Hashable]:
|
|
101
|
+
if ppid is None:
|
|
102
|
+
return None
|
|
103
|
+
normalized_ip = str(client_ip or '127.0.0.1').split('%', 1)[0]
|
|
104
|
+
try:
|
|
105
|
+
normalized_ip = ipaddress.ip_address(normalized_ip).compressed
|
|
106
|
+
except ValueError:
|
|
107
|
+
pass
|
|
108
|
+
if normalized_ip in {'127.0.0.1', '::1'}:
|
|
109
|
+
return ppid
|
|
110
|
+
return normalized_ip, ppid
|
|
111
|
+
|
|
112
|
+
@staticmethod
|
|
113
|
+
def _session_identity(session_id: Hashable) -> tuple[int, str]:
|
|
114
|
+
if isinstance(session_id, tuple) and len(session_id) == 2:
|
|
115
|
+
return session_id[1], str(session_id[0])
|
|
116
|
+
return session_id, '127.0.0.1'
|
|
117
|
+
|
|
118
|
+
def get_session(self, ppid: Hashable) -> Optional[Session]:
|
|
119
|
+
with self._sessions_lock:
|
|
120
|
+
session = self._sessions.get(ppid)
|
|
121
|
+
if session:
|
|
122
|
+
session.last_access = time.time()
|
|
123
|
+
return session
|
|
124
|
+
|
|
125
|
+
def get_or_create_session(self, ppid: Hashable) -> Session:
|
|
126
|
+
with self._sessions_lock:
|
|
127
|
+
if ppid not in self._sessions:
|
|
128
|
+
terminal_id, client_ip = self._session_identity(ppid)
|
|
129
|
+
self._sessions[ppid] = Session(ppid=terminal_id, client_ip=client_ip)
|
|
130
|
+
session = self._sessions[ppid]
|
|
131
|
+
session.last_access = time.time()
|
|
132
|
+
return session
|
|
133
|
+
|
|
134
|
+
def get_all_sessions(self) -> Dict[Hashable, Session]:
|
|
135
|
+
with self._sessions_lock:
|
|
136
|
+
return dict(self._sessions)
|
|
137
|
+
|
|
138
|
+
def remove_session(self, ppid: Hashable) -> Optional[Session]:
|
|
139
|
+
with self._sessions_lock:
|
|
140
|
+
return self._sessions.pop(ppid, None)
|
|
141
|
+
|
|
142
|
+
def add_connection_to_session(
|
|
143
|
+
self,
|
|
144
|
+
ppid: Hashable,
|
|
145
|
+
port: str,
|
|
146
|
+
as_foreground: bool = False,
|
|
147
|
+
default_port: str = None
|
|
148
|
+
) -> Session:
|
|
149
|
+
port = _canon_port(port)
|
|
150
|
+
default_port = _canon_port(default_port) if default_port else None
|
|
151
|
+
|
|
152
|
+
with self._sessions_lock:
|
|
153
|
+
if ppid not in self._sessions:
|
|
154
|
+
terminal_id, client_ip = self._session_identity(ppid)
|
|
155
|
+
self._sessions[ppid] = Session(ppid=terminal_id, client_ip=client_ip)
|
|
156
|
+
session = self._sessions[ppid]
|
|
157
|
+
session.last_access = time.time()
|
|
158
|
+
|
|
159
|
+
if as_foreground:
|
|
160
|
+
session.add_connection(port, as_foreground=True)
|
|
161
|
+
elif session.foreground is None:
|
|
162
|
+
if default_port and default_port != port:
|
|
163
|
+
session.add_connection(default_port, as_foreground=True)
|
|
164
|
+
session.add_connection(port, as_foreground=False)
|
|
165
|
+
else:
|
|
166
|
+
session.add_connection(port, as_foreground=True)
|
|
167
|
+
else:
|
|
168
|
+
session.add_connection(port, as_foreground=False)
|
|
169
|
+
|
|
170
|
+
for other_ppid in list(self._sessions.keys()):
|
|
171
|
+
if other_ppid == ppid:
|
|
172
|
+
continue
|
|
173
|
+
other_sess = self._sessions.get(other_ppid)
|
|
174
|
+
if other_sess and other_sess.has_connection(port) and not self.is_session_alive(other_sess):
|
|
175
|
+
other_sess.remove_connection(port)
|
|
176
|
+
if other_sess.is_empty():
|
|
177
|
+
self._sessions.pop(other_ppid, None)
|
|
178
|
+
|
|
179
|
+
return session
|
|
180
|
+
|
|
181
|
+
def remove_connection_from_session(self, ppid: Hashable, port: str) -> Tuple[bool, bool]:
|
|
182
|
+
port = _canon_port(port)
|
|
183
|
+
session = self.get_session(ppid)
|
|
184
|
+
if not session:
|
|
185
|
+
return False, False
|
|
186
|
+
|
|
187
|
+
with self._sessions_lock:
|
|
188
|
+
was_foreground = session.remove_connection(port)
|
|
189
|
+
if session.is_empty():
|
|
190
|
+
self._sessions.pop(ppid, None)
|
|
191
|
+
return True, was_foreground
|
|
192
|
+
|
|
193
|
+
def remove_connection_from_all_sessions(self, port: str) -> List[Hashable]:
|
|
194
|
+
port = _canon_port(port)
|
|
195
|
+
affected_sessions = []
|
|
196
|
+
|
|
197
|
+
with self._sessions_lock:
|
|
198
|
+
for ppid, session in list(self._sessions.items()):
|
|
199
|
+
if session.has_connection(port):
|
|
200
|
+
session.remove_connection(port)
|
|
201
|
+
affected_sessions.append(ppid)
|
|
202
|
+
if session.is_empty():
|
|
203
|
+
self._sessions.pop(ppid, None)
|
|
204
|
+
|
|
205
|
+
return affected_sessions
|
|
206
|
+
|
|
207
|
+
def get_foreground(self, ppid: Hashable) -> Optional[str]:
|
|
208
|
+
session = self.get_session(ppid)
|
|
209
|
+
return session.foreground if session else None
|
|
210
|
+
|
|
211
|
+
def switch_foreground(self, ppid: Hashable, port: str) -> bool:
|
|
212
|
+
port = _canon_port(port)
|
|
213
|
+
session = self.get_session(ppid)
|
|
214
|
+
if not session:
|
|
215
|
+
return False
|
|
216
|
+
|
|
217
|
+
with self._sessions_lock:
|
|
218
|
+
return session.switch_foreground(port)
|
|
219
|
+
|
|
220
|
+
def resolve_port(self, ppid: Hashable, explicit_port: str = None, default_port: str = None) -> Optional[str]:
|
|
221
|
+
if explicit_port:
|
|
222
|
+
return _canon_port(explicit_port)
|
|
223
|
+
|
|
224
|
+
session = self.get_session(ppid)
|
|
225
|
+
if session and session.foreground:
|
|
226
|
+
return session.foreground
|
|
227
|
+
|
|
228
|
+
if default_port:
|
|
229
|
+
return _canon_port(default_port)
|
|
230
|
+
|
|
231
|
+
return None
|
|
232
|
+
|
|
233
|
+
def is_process_alive(self, pid: int) -> bool:
|
|
234
|
+
if not pid or pid <= 0:
|
|
235
|
+
return False
|
|
236
|
+
try:
|
|
237
|
+
import psutil
|
|
238
|
+
if not psutil.pid_exists(pid):
|
|
239
|
+
return False
|
|
240
|
+
proc = psutil.Process(pid)
|
|
241
|
+
return proc.status() != psutil.STATUS_ZOMBIE
|
|
242
|
+
except Exception:
|
|
243
|
+
pass
|
|
244
|
+
try:
|
|
245
|
+
if sys.platform == 'win32':
|
|
246
|
+
import ctypes
|
|
247
|
+
from ctypes import wintypes
|
|
248
|
+
kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
|
|
249
|
+
kernel32.OpenProcess.argtypes = [wintypes.DWORD, wintypes.BOOL, wintypes.DWORD]
|
|
250
|
+
kernel32.OpenProcess.restype = wintypes.HANDLE
|
|
251
|
+
kernel32.GetExitCodeProcess.argtypes = [wintypes.HANDLE, ctypes.POINTER(wintypes.DWORD)]
|
|
252
|
+
kernel32.GetExitCodeProcess.restype = wintypes.BOOL
|
|
253
|
+
kernel32.CloseHandle.argtypes = [wintypes.HANDLE]
|
|
254
|
+
kernel32.CloseHandle.restype = wintypes.BOOL
|
|
255
|
+
|
|
256
|
+
PROCESS_QUERY_LIMITED_INFORMATION = 0x1000
|
|
257
|
+
STILL_ACTIVE = 259
|
|
258
|
+
|
|
259
|
+
handle = kernel32.OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, False, pid)
|
|
260
|
+
if handle:
|
|
261
|
+
try:
|
|
262
|
+
exit_code = wintypes.DWORD()
|
|
263
|
+
if kernel32.GetExitCodeProcess(handle, ctypes.byref(exit_code)):
|
|
264
|
+
return exit_code.value == STILL_ACTIVE
|
|
265
|
+
return False
|
|
266
|
+
finally:
|
|
267
|
+
kernel32.CloseHandle(handle)
|
|
268
|
+
return False
|
|
269
|
+
else:
|
|
270
|
+
os.kill(pid, 0)
|
|
271
|
+
return True
|
|
272
|
+
except PermissionError:
|
|
273
|
+
# Process exists but we lack permission to signal it — treat as alive.
|
|
274
|
+
return True
|
|
275
|
+
except OSError:
|
|
276
|
+
return False
|
|
277
|
+
|
|
278
|
+
@staticmethod
|
|
279
|
+
def _is_local_ip(host: str) -> bool:
|
|
280
|
+
try:
|
|
281
|
+
address = ipaddress.ip_address(str(host).split('%', 1)[0])
|
|
282
|
+
except ValueError:
|
|
283
|
+
return False
|
|
284
|
+
if address.is_loopback:
|
|
285
|
+
return True
|
|
286
|
+
try:
|
|
287
|
+
import psutil
|
|
288
|
+
for interface_addresses in psutil.net_if_addrs().values():
|
|
289
|
+
for interface_address in interface_addresses:
|
|
290
|
+
candidate = interface_address.address.split('%', 1)[0]
|
|
291
|
+
if candidate == str(address):
|
|
292
|
+
return True
|
|
293
|
+
except Exception:
|
|
294
|
+
pass
|
|
295
|
+
return False
|
|
296
|
+
|
|
297
|
+
def is_session_alive(self, session: Session) -> bool:
|
|
298
|
+
if self._is_local_ip(session.client_ip):
|
|
299
|
+
return self.is_process_alive(session.ppid)
|
|
300
|
+
return time.time() - session.last_access <= self.REMOTE_SESSION_TTL
|
|
301
|
+
|
|
302
|
+
def cleanup_zombie_sessions(self, connected_ports: Optional[Set[str]] = None) -> List[Hashable]:
|
|
303
|
+
with self._sessions_lock:
|
|
304
|
+
zombie_sessions = []
|
|
305
|
+
live_ports: Set[str] = set()
|
|
306
|
+
canon_connected = {_canon_port(p) for p in connected_ports} if connected_ports is not None else None
|
|
307
|
+
|
|
308
|
+
for session_id, sess in list(self._sessions.items()):
|
|
309
|
+
if not self.is_session_alive(sess):
|
|
310
|
+
zombie_sessions.append(session_id)
|
|
311
|
+
else:
|
|
312
|
+
for p in sess.get_all_connections():
|
|
313
|
+
live_ports.add(_canon_port(p))
|
|
314
|
+
|
|
315
|
+
for session_id in zombie_sessions:
|
|
316
|
+
sess = self._sessions.get(session_id)
|
|
317
|
+
if not sess:
|
|
318
|
+
continue
|
|
319
|
+
for p in list(sess.get_all_connections()):
|
|
320
|
+
cp = _canon_port(p)
|
|
321
|
+
if (canon_connected is not None and cp not in canon_connected) or (cp in live_ports):
|
|
322
|
+
sess.remove_connection(cp)
|
|
323
|
+
else:
|
|
324
|
+
live_ports.add(cp)
|
|
325
|
+
if sess.is_empty():
|
|
326
|
+
self._sessions.pop(session_id, None)
|
|
327
|
+
|
|
328
|
+
self._last_zombie_check = time.time()
|
|
329
|
+
return zombie_sessions
|
|
330
|
+
|
|
331
|
+
def get_session_info(self) -> Dict[str, Any]:
|
|
332
|
+
sessions_info = []
|
|
333
|
+
|
|
334
|
+
with self._sessions_lock:
|
|
335
|
+
for session_id, session in self._sessions.items():
|
|
336
|
+
sessions_info.append({
|
|
337
|
+
'ppid': session.ppid,
|
|
338
|
+
'sid': session.ppid,
|
|
339
|
+
'client_ip': session.client_ip,
|
|
340
|
+
'session_key': f'{session.client_ip}/{session.ppid}',
|
|
341
|
+
'foreground': session.foreground,
|
|
342
|
+
'backgrounds': list(session.backgrounds),
|
|
343
|
+
'last_access': session.last_access
|
|
344
|
+
})
|
|
345
|
+
|
|
346
|
+
return {'sessions': sessions_info}
|
|
347
|
+
|
|
348
|
+
def find_sessions_using_port(self, port: str) -> List[Dict[str, Any]]:
|
|
349
|
+
result = []
|
|
350
|
+
|
|
351
|
+
with self._sessions_lock:
|
|
352
|
+
for session_id, session in self._sessions.items():
|
|
353
|
+
if session.has_connection(port):
|
|
354
|
+
is_fg = session.foreground and session.foreground == port
|
|
355
|
+
result.append({
|
|
356
|
+
'ppid': session.ppid,
|
|
357
|
+
'sid': session.ppid,
|
|
358
|
+
'client_ip': session.client_ip,
|
|
359
|
+
'session_key': f'{session.client_ip}/{session.ppid}',
|
|
360
|
+
'is_foreground': is_fg
|
|
361
|
+
})
|
|
362
|
+
|
|
363
|
+
return result
|
|
364
|
+
|
|
365
|
+
def remove_client(self, client_ip: str) -> list[tuple[Hashable, Session]]:
|
|
366
|
+
normalized_ip = str(ipaddress.ip_address(str(client_ip).split('%', 1)[0]))
|
|
367
|
+
with self._sessions_lock:
|
|
368
|
+
session_ids = [
|
|
369
|
+
session_id
|
|
370
|
+
for session_id, session in self._sessions.items()
|
|
371
|
+
if session.client_ip == normalized_ip
|
|
372
|
+
]
|
|
373
|
+
return [
|
|
374
|
+
(session_id, self._sessions.pop(session_id))
|
|
375
|
+
for session_id in session_ids
|
|
376
|
+
]
|
|
377
|
+
|
|
378
|
+
def clear_all_sessions(self):
|
|
379
|
+
with self._sessions_lock:
|
|
380
|
+
self._sessions.clear()
|
|
381
|
+
|
|
382
|
+
def cleanup_empty_sessions(self) -> List[Hashable]:
|
|
383
|
+
with self._sessions_lock:
|
|
384
|
+
empty = [ppid for ppid, sess in self._sessions.items() if sess.is_empty()]
|
|
385
|
+
for ppid in empty:
|
|
386
|
+
self._sessions.pop(ppid, None)
|
|
387
|
+
return empty
|
|
388
|
+
|
|
389
|
+
def has_sessions(self) -> bool:
|
|
390
|
+
with self._sessions_lock:
|
|
391
|
+
return len(self._sessions) > 0
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import logging
|
|
3
|
+
import socket
|
|
4
|
+
import socketserver
|
|
5
|
+
import sys
|
|
6
|
+
import threading
|
|
7
|
+
from typing import Optional
|
|
8
|
+
|
|
9
|
+
from replx_agent.rpc.protocol import AgentProtocol, FrameType, FrameReader
|
|
10
|
+
|
|
11
|
+
logger = logging.getLogger(__name__)
|
|
12
|
+
|
|
13
|
+
LOOP_COMMANDS: frozenset[str] = frozenset({
|
|
14
|
+
"ping",
|
|
15
|
+
"status",
|
|
16
|
+
"session_info",
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
FAST_COMMANDS: frozenset[str] = frozenset({
|
|
20
|
+
"repl_read",
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
_RESPONSE_CACHE_MAX_BYTES = 16 * 1024 * 1024
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class AgentTcpPeer:
|
|
27
|
+
def __init__(self, sock: socket.socket, address: tuple):
|
|
28
|
+
self.sock = sock
|
|
29
|
+
self.address = address
|
|
30
|
+
self._send_lock = threading.Lock()
|
|
31
|
+
self.closed = False
|
|
32
|
+
self.authenticated = False
|
|
33
|
+
self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
|
|
34
|
+
|
|
35
|
+
def send(self, data: bytes) -> None:
|
|
36
|
+
with self._send_lock:
|
|
37
|
+
if self.closed:
|
|
38
|
+
raise ConnectionError("Client connection is closed")
|
|
39
|
+
self.sock.sendall(data)
|
|
40
|
+
|
|
41
|
+
def close(self) -> None:
|
|
42
|
+
with self._send_lock:
|
|
43
|
+
if self.closed:
|
|
44
|
+
return
|
|
45
|
+
self.closed = True
|
|
46
|
+
try:
|
|
47
|
+
self.sock.shutdown(socket.SHUT_RDWR)
|
|
48
|
+
except OSError:
|
|
49
|
+
pass
|
|
50
|
+
try:
|
|
51
|
+
self.sock.close()
|
|
52
|
+
except OSError:
|
|
53
|
+
pass
|
|
54
|
+
|
|
55
|
+
def __repr__(self) -> str:
|
|
56
|
+
return f"AgentTcpPeer({self.address!r})"
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class _AgentRequestHandler(socketserver.BaseRequestHandler):
|
|
60
|
+
def handle(self) -> None:
|
|
61
|
+
peer = AgentTcpPeer(self.request, self.client_address)
|
|
62
|
+
reader = FrameReader(self.request)
|
|
63
|
+
server = self.server.agent_server
|
|
64
|
+
try:
|
|
65
|
+
while server.running:
|
|
66
|
+
try:
|
|
67
|
+
frame = reader.receive()
|
|
68
|
+
if frame is None:
|
|
69
|
+
continue
|
|
70
|
+
frame_type, seq, payload = AgentProtocol.decode_frame(frame)
|
|
71
|
+
except ConnectionError:
|
|
72
|
+
break
|
|
73
|
+
except (OSError, ValueError) as exc:
|
|
74
|
+
logger.warning("Invalid TCP frame from %s: %s", peer, exc)
|
|
75
|
+
break
|
|
76
|
+
|
|
77
|
+
if frame_type is FrameType.BINARY:
|
|
78
|
+
if server.auth_token and not peer.authenticated:
|
|
79
|
+
logger.warning("Unauthenticated binary frame from %s", peer)
|
|
80
|
+
break
|
|
81
|
+
server._handle_binary_frame(peer, seq, payload)
|
|
82
|
+
continue
|
|
83
|
+
|
|
84
|
+
try:
|
|
85
|
+
msg = json.loads(payload.decode("utf-8"))
|
|
86
|
+
except (UnicodeDecodeError, json.JSONDecodeError) as exc:
|
|
87
|
+
logger.warning("Invalid JSON message from %s: %s", peer, exc)
|
|
88
|
+
break
|
|
89
|
+
if not isinstance(msg, dict):
|
|
90
|
+
logger.warning("Invalid message from %s", peer)
|
|
91
|
+
break
|
|
92
|
+
|
|
93
|
+
msg_type = msg.get("type")
|
|
94
|
+
if msg_type in {"input", "stream_ack", "transfer_end", "transfer_abort"}:
|
|
95
|
+
server._handle_request(msg, peer)
|
|
96
|
+
continue
|
|
97
|
+
|
|
98
|
+
command = msg.get("command")
|
|
99
|
+
if command in {"transfer_start", "firmware_stage"}:
|
|
100
|
+
server._handle_request(msg, peer)
|
|
101
|
+
continue
|
|
102
|
+
if command == "shutdown":
|
|
103
|
+
server._handle_request(msg, peer)
|
|
104
|
+
continue
|
|
105
|
+
if command in LOOP_COMMANDS:
|
|
106
|
+
server._handle_direct(msg, peer)
|
|
107
|
+
continue
|
|
108
|
+
|
|
109
|
+
executor = (
|
|
110
|
+
server._fast_executor
|
|
111
|
+
if command in FAST_COMMANDS
|
|
112
|
+
else server._slow_executor
|
|
113
|
+
)
|
|
114
|
+
executor.submit(server._handle_request, msg, peer)
|
|
115
|
+
finally:
|
|
116
|
+
server._handle_peer_disconnect(peer)
|
|
117
|
+
peer.close()
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
class AgentTCPServer(socketserver.ThreadingTCPServer):
|
|
121
|
+
allow_reuse_address = True
|
|
122
|
+
daemon_threads = True
|
|
123
|
+
block_on_close = False
|
|
124
|
+
request_queue_size = 32
|
|
125
|
+
|
|
126
|
+
def __init__(self, address: tuple[str, int], agent_server):
|
|
127
|
+
self.agent_server = agent_server
|
|
128
|
+
self.address_family = socket.AF_INET6 if ':' in address[0] else socket.AF_INET
|
|
129
|
+
super().__init__(address, _AgentRequestHandler)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
class TcpReliabilityMixin:
|
|
133
|
+
def _init_tcp_reliability(
|
|
134
|
+
self,
|
|
135
|
+
*,
|
|
136
|
+
response_cache_max: int = 512,
|
|
137
|
+
response_cache_max_bytes: int = _RESPONSE_CACHE_MAX_BYTES,
|
|
138
|
+
last_seq_max: int = 256,
|
|
139
|
+
) -> None:
|
|
140
|
+
self.last_seq: dict[AgentTcpPeer, int] = {}
|
|
141
|
+
self._last_seq_lock = threading.Lock()
|
|
142
|
+
self._MAX_LAST_SEQ = last_seq_max
|
|
143
|
+
self._response_cache: dict[tuple[AgentTcpPeer, int], bytes] = {}
|
|
144
|
+
self._response_cache_bytes = 0
|
|
145
|
+
self._response_cache_lock = threading.Lock()
|
|
146
|
+
self._RESPONSE_CACHE_MAX = response_cache_max
|
|
147
|
+
self._RESPONSE_CACHE_MAX_BYTES = response_cache_max_bytes
|
|
148
|
+
|
|
149
|
+
def _safe_send(self, data: bytes, peer: AgentTcpPeer) -> bool:
|
|
150
|
+
try:
|
|
151
|
+
peer.send(data)
|
|
152
|
+
return True
|
|
153
|
+
except (ConnectionError, OSError) as exc:
|
|
154
|
+
if not peer.closed:
|
|
155
|
+
logger.warning("TCP response to %s failed: %s", peer, exc)
|
|
156
|
+
return False
|
|
157
|
+
|
|
158
|
+
def _cache_response(self, peer: AgentTcpPeer, seq: int, data: bytes) -> None:
|
|
159
|
+
key = (peer, seq)
|
|
160
|
+
with self._response_cache_lock:
|
|
161
|
+
previous = self._response_cache.pop(key, None)
|
|
162
|
+
if previous is not None:
|
|
163
|
+
self._response_cache_bytes -= len(previous)
|
|
164
|
+
self._response_cache[key] = data
|
|
165
|
+
self._response_cache_bytes += len(data)
|
|
166
|
+
|
|
167
|
+
while (
|
|
168
|
+
len(self._response_cache) > self._RESPONSE_CACHE_MAX
|
|
169
|
+
or self._response_cache_bytes > self._RESPONSE_CACHE_MAX_BYTES
|
|
170
|
+
):
|
|
171
|
+
oldest = next(iter(self._response_cache))
|
|
172
|
+
evicted = self._response_cache.pop(oldest)
|
|
173
|
+
self._response_cache_bytes -= len(evicted)
|
|
174
|
+
|
|
175
|
+
def _lookup_response(self, peer: AgentTcpPeer, seq: int) -> Optional[bytes]:
|
|
176
|
+
with self._response_cache_lock:
|
|
177
|
+
return self._response_cache.get((peer, seq))
|
|
178
|
+
|
|
179
|
+
def _forget_peer(self, peer: AgentTcpPeer) -> None:
|
|
180
|
+
with self._last_seq_lock:
|
|
181
|
+
self.last_seq.pop(peer, None)
|
|
182
|
+
with self._response_cache_lock:
|
|
183
|
+
for key in [key for key in self._response_cache if key[0] is peer]:
|
|
184
|
+
response = self._response_cache.pop(key)
|
|
185
|
+
self._response_cache_bytes -= len(response)
|
|
186
|
+
|
|
187
|
+
def _check_and_record_seq(self, peer: AgentTcpPeer, seq: int) -> bool:
|
|
188
|
+
with self._last_seq_lock:
|
|
189
|
+
if peer in self.last_seq and seq == self.last_seq[peer]:
|
|
190
|
+
return False
|
|
191
|
+
self.last_seq[peer] = seq
|
|
192
|
+
if len(self.last_seq) > self._MAX_LAST_SEQ:
|
|
193
|
+
oldest = next(iter(self.last_seq))
|
|
194
|
+
del self.last_seq[oldest]
|
|
195
|
+
return True
|
|
196
|
+
|
|
197
|
+
def send_completion(self, encoded: bytes, peer: AgentTcpPeer, seq: int) -> bool:
|
|
198
|
+
return self._safe_send(encoded, peer)
|
|
199
|
+
|
|
200
|
+
def _ack_stream_completion(self, peer: AgentTcpPeer, seq: int) -> None:
|
|
201
|
+
return None
|
|
202
|
+
|
|
203
|
+
def _send_duplicate_reply(self, peer: AgentTcpPeer, seq: int) -> None:
|
|
204
|
+
cached = self._lookup_response(peer, seq)
|
|
205
|
+
self._safe_send(cached or AgentProtocol.encode_message(
|
|
206
|
+
AgentProtocol.create_ack(seq)
|
|
207
|
+
), peer)
|