Add extensive debug logging for control channel messages

- wssshd: Log full content of all received and sent control messages when --debug is enabled
- wssshc: Already logs control messages, registration message logging enhanced
- wsssht: Fixed memory corruption issue with config struct allocations

Control channel messages include: register, registered, registration_error,
tunnel_request, tunnel_ack, tunnel_error, tunnel_close, tunnel_data, tunnel_response
parent 2ea94e22
......@@ -82,12 +82,12 @@ async def handle_websocket(websocket, path=None, *, server_password=None):
break
# Process the message (rest of the original logic)
# Only log debug info for non-data messages to reduce overhead
# Log debug info for all control channel messages when debug is enabled
try:
data = json.loads(message)
msg_type = data.get('type', 'unknown')
if debug and msg_type not in ('tunnel_data', 'tunnel_response'):
print(f"[DEBUG] [WebSocket] {msg_type} message received")
if debug:
print(f"[DEBUG] [WebSocket] Received {msg_type} message: {message}")
except json.JSONDecodeError as e:
if debug: print(f"[DEBUG] [WebSocket] Invalid JSON received: {e}")
continue
......@@ -129,13 +129,17 @@ async def handle_websocket(websocket, path=None, *, server_password=None):
else:
print(f"Client {client_id} registered")
try:
await websocket.send(REGISTERED_MSG % client_id)
response_msg = REGISTERED_MSG % client_id
if debug: print(f"[DEBUG] [WebSocket] Sending registration response: {response_msg}")
await websocket.send(response_msg)
except Exception:
if debug: print(f"[DEBUG] [WebSocket] Failed to send registration response to {client_id}")
else:
if debug: print(f"[DEBUG] [WebSocket] Client {client_id} registration failed: invalid password")
try:
await websocket.send(REGISTRATION_ERROR_MSG % "Invalid password")
error_msg = REGISTRATION_ERROR_MSG % "Invalid password"
if debug: print(f"[DEBUG] [WebSocket] Sending registration error: {error_msg}")
await websocket.send(error_msg)
except Exception:
if debug: print(f"[DEBUG] [WebSocket] Failed to send registration error to {client_id}")
elif data.get('type') == 'tunnel_request':
......@@ -177,8 +181,13 @@ async def handle_websocket(websocket, path=None, *, server_password=None):
# Forward tunnel request to client
try:
await client_info['websocket'].send(TUNNEL_REQUEST_MSG % request_id)
await websocket.send(TUNNEL_ACK_MSG % request_id)
request_msg = TUNNEL_REQUEST_MSG % request_id
ack_msg = TUNNEL_ACK_MSG % request_id
if debug:
print(f"[DEBUG] [WebSocket] Sending tunnel request to client: {request_msg}")
print(f"[DEBUG] [WebSocket] Sending tunnel ack to wsssh: {ack_msg}")
await client_info['websocket'].send(request_msg)
await websocket.send(ack_msg)
if not debug:
print(f"[EVENT] New tunnel {request_id} for client {client_id}")
else:
......@@ -187,12 +196,16 @@ async def handle_websocket(websocket, path=None, *, server_password=None):
tunnel.update_status(TunnelStatus.ERROR, str(e))
# Send error response for tunnel request failures
try:
await websocket.send(TUNNEL_ERROR_MSG % (request_id, "Failed to forward request"))
error_msg = TUNNEL_ERROR_MSG % (request_id, "Failed to forward request")
if debug: print(f"[DEBUG] [WebSocket] Sending tunnel error: {error_msg}")
await websocket.send(error_msg)
except Exception:
pass # Silent failure if even error response fails
else:
try:
await websocket.send(TUNNEL_ERROR_MSG % (request_id, "Client not registered or disconnected"))
error_msg = TUNNEL_ERROR_MSG % (request_id, "Client not registered or disconnected")
if debug: print(f"[DEBUG] [WebSocket] Sending tunnel error: {error_msg}")
await websocket.send(error_msg)
except Exception:
pass # Silent failure for error responses
elif data.get('type') == 'tunnel_data':
......@@ -232,7 +245,9 @@ async def handle_websocket(websocket, path=None, *, server_password=None):
client_info = clients.get(tunnel.client_id)
if client_info and client_info['status'] == 'active':
try:
await tunnel.client_ws.send(TUNNEL_CLOSE_MSG % request_id)
close_msg = TUNNEL_CLOSE_MSG % request_id
if debug: print(f"[DEBUG] [WebSocket] Sending tunnel close to client: {close_msg}")
await tunnel.client_ws.send(close_msg)
except Exception:
# Silent failure for performance
pass
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment