more stuff

This commit is contained in:
2026-08-21 08:45:36 +02:00
parent 5e506c9921
commit ad728ba96c
12 changed files with 1148 additions and 64 deletions

View File

@ -39,6 +39,23 @@ MAX_SESSION_SECONDS = 8 * 3600
IDLE_TIMEOUT_SECONDS = 15 * 60
async def _reject(websocket: WebSocket, code: int, reason: str, *, accepted: bool) -> None:
"""Beendet eine SSH-Sitzung vor ihrem eigentlichen Beginn -- mit einem fuer
den Benutzer lesbaren Grund als WebSocket-Close-Reason, analog zu
app/rdp_proxy/ws_tunnel.py::_reject (Troubleshooting-Verbesserung:
vorher endeten diese Pfade in einem nackten `websocket.close(code=...)`,
static/js/terminal.js zeigte dann nur ein generisches
'Verbindung beendet' ohne jeden Grund an). Voraussetzung fuer eine
sichtbare Reason ist ein zustande gekommener Handshake -- vor accept()
sieht der Browser nur einen HTTP-/WS-Fehler ohne Text (siehe die
bewusste Ausnahme fuer den Nicht-angemeldet-Fall unten)."""
logger.warning("SSH-Verbindung abgelehnt (code=%s): %s", code, reason)
reason_bytes = reason.encode("utf-8")[:123]
if not accepted:
await websocket.accept()
await websocket.close(code=code, reason=reason_bytes.decode("utf-8", errors="ignore"))
async def _pump_ssh_to_ws(process: asyncssh.SSHClientProcess, websocket: WebSocket, recorder: SessionRecorder):
try:
while True:
@ -68,11 +85,11 @@ async def ssh_terminal(websocket: WebSocket, host_id: int):
if not user.is_admin and not await user_has_role_for_host(
conn, user_id=user.id, host_id=host_id, role_name="ssh_connect"
):
logger.warning(
"SSH-Verbindung abgelehnt: Benutzer %s hat keine Berechtigung 'ssh_connect' fuer Host %s",
user.username, host_id,
await _reject(
websocket, 4403,
f"Keine Berechtigung 'ssh_connect' fuer Host {host_id}",
accepted=False,
)
await websocket.close(code=4403)
return
await websocket.accept()
@ -81,9 +98,11 @@ async def ssh_terminal(websocket: WebSocket, host_id: int):
try:
host = await load_host(conn, host_id)
except HostNotConfiguredError as exc:
logger.warning("SSH-Verbindung abgelehnt (host_id=%s): %s", host_id, exc)
# Grund SOWOHL als JSON-Frame (falls der Client schon zuhoert) ALS
# AUCH als Close-Reason senden (falls nicht) -- terminal.js zeigt
# beides an, je nachdem, was zuerst ankommt.
await websocket.send_json({"type": "error", "message": str(exc)})
await websocket.close(code=4404)
await _reject(websocket, 4404, str(exc), accepted=True)
return
cursor = await conn.execute(