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): ...@@ -82,12 +82,12 @@ async def handle_websocket(websocket, path=None, *, server_password=None):
break break
# Process the message (rest of the original logic) # 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: try:
data = json.loads(message) data = json.loads(message)
msg_type = data.get('type', 'unknown') msg_type = data.get('type', 'unknown')
if debug and msg_type not in ('tunnel_data', 'tunnel_response'): if debug:
print(f"[DEBUG] [WebSocket] {msg_type} message received") print(f"[DEBUG] [WebSocket] Received {msg_type} message: {message}")
except json.JSONDecodeError as e: except json.JSONDecodeError as e:
if debug: print(f"[DEBUG] [WebSocket] Invalid JSON received: {e}") if debug: print(f"[DEBUG] [WebSocket] Invalid JSON received: {e}")
continue continue
...@@ -129,13 +129,17 @@ async def handle_websocket(websocket, path=None, *, server_password=None): ...@@ -129,13 +129,17 @@ async def handle_websocket(websocket, path=None, *, server_password=None):
else: else:
print(f"Client {client_id} registered") print(f"Client {client_id} registered")
try: 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: except Exception:
if debug: print(f"[DEBUG] [WebSocket] Failed to send registration response to {client_id}") if debug: print(f"[DEBUG] [WebSocket] Failed to send registration response to {client_id}")
else: else:
if debug: print(f"[DEBUG] [WebSocket] Client {client_id} registration failed: invalid password") if debug: print(f"[DEBUG] [WebSocket] Client {client_id} registration failed: invalid password")
try: 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: except Exception:
if debug: print(f"[DEBUG] [WebSocket] Failed to send registration error to {client_id}") if debug: print(f"[DEBUG] [WebSocket] Failed to send registration error to {client_id}")
elif data.get('type') == 'tunnel_request': elif data.get('type') == 'tunnel_request':
...@@ -177,8 +181,13 @@ async def handle_websocket(websocket, path=None, *, server_password=None): ...@@ -177,8 +181,13 @@ async def handle_websocket(websocket, path=None, *, server_password=None):
# Forward tunnel request to client # Forward tunnel request to client
try: try:
await client_info['websocket'].send(TUNNEL_REQUEST_MSG % request_id) request_msg = TUNNEL_REQUEST_MSG % request_id
await websocket.send(TUNNEL_ACK_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: if not debug:
print(f"[EVENT] New tunnel {request_id} for client {client_id}") print(f"[EVENT] New tunnel {request_id} for client {client_id}")
else: else:
...@@ -187,12 +196,16 @@ async def handle_websocket(websocket, path=None, *, server_password=None): ...@@ -187,12 +196,16 @@ async def handle_websocket(websocket, path=None, *, server_password=None):
tunnel.update_status(TunnelStatus.ERROR, str(e)) tunnel.update_status(TunnelStatus.ERROR, str(e))
# Send error response for tunnel request failures # Send error response for tunnel request failures
try: 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: except Exception:
pass # Silent failure if even error response fails pass # Silent failure if even error response fails
else: else:
try: 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: except Exception:
pass # Silent failure for error responses pass # Silent failure for error responses
elif data.get('type') == 'tunnel_data': elif data.get('type') == 'tunnel_data':
...@@ -232,7 +245,9 @@ async def handle_websocket(websocket, path=None, *, server_password=None): ...@@ -232,7 +245,9 @@ async def handle_websocket(websocket, path=None, *, server_password=None):
client_info = clients.get(tunnel.client_id) client_info = clients.get(tunnel.client_id)
if client_info and client_info['status'] == 'active': if client_info and client_info['status'] == 'active':
try: 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: except Exception:
# Silent failure for performance # Silent failure for performance
pass 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