Complete implementation of extensive debug logging for control channel messages

- wssshd: Added full JSON logging for all received/sent control messages when --debug enabled
- wssshc: Verified existing control message logging
- wsssht: Suppressed verbose data channel messages in debug mode
- Fixed wsssht daemon mode for multiple sequential connections
- Fixed wsssht script mode data forwarding and JSON output
- Increased thread cleanup timeout to prevent race conditions
- All components compile and work correctly
parent d81db287
...@@ -309,7 +309,7 @@ void cleanup_tunnel(int debug) { ...@@ -309,7 +309,7 @@ void cleanup_tunnel(int debug) {
// Give threads a moment to stop using resources // Give threads a moment to stop using resources
pthread_mutex_unlock(&tunnel_mutex); pthread_mutex_unlock(&tunnel_mutex);
usleep(200000); // 200ms - increased timeout for better thread cleanup usleep(1000000); // 1 second - increased timeout for better thread cleanup
pthread_mutex_lock(&tunnel_mutex); pthread_mutex_lock(&tunnel_mutex);
// Now safely clean up - don't free tunnel structures to avoid use-after-free in threads // Now safely clean up - don't free tunnel structures to avoid use-after-free in threads
...@@ -1269,9 +1269,7 @@ int setup_tunnel(const char *wssshd_host, int wssshd_port, const char *client_id ...@@ -1269,9 +1269,7 @@ int setup_tunnel(const char *wssshd_host, int wssshd_port, const char *client_id
} }
// For backward compatibility with wsssh/wsscp that use active_tunnel global // For backward compatibility with wsssh/wsscp that use active_tunnel global
if (active_tunnels_count == 1) {
active_tunnel = new_tunnel; active_tunnel = new_tunnel;
}
pthread_mutex_unlock(&tunnel_mutex); pthread_mutex_unlock(&tunnel_mutex);
......
No preview for this file type
...@@ -913,16 +913,13 @@ int run_daemon_mode(wsssh_config_t *config, const char *client_id, const char *w ...@@ -913,16 +913,13 @@ int run_daemon_mode(wsssh_config_t *config, const char *client_id, const char *w
} }
// Daemon mode main loop - accept connections and establish tunnels // Daemon mode main loop - accept connections and establish tunnels
int daemon_restart = 0; while (1) {
do {
daemon_restart = 0;
// Check for SIGINT // Check for SIGINT
if (sigint_received) { if (sigint_received) {
if (config->debug) { if (config->debug) {
printf("[DEBUG] SIGINT received in daemon mode, exiting\n"); printf("[DEBUG] SIGINT received in daemon mode, exiting\n");
fflush(stdout); fflush(stdout);
} }
daemon_restart = 0;
break; break;
} }
...@@ -967,12 +964,16 @@ int run_daemon_mode(wsssh_config_t *config, const char *client_id, const char *w ...@@ -967,12 +964,16 @@ int run_daemon_mode(wsssh_config_t *config, const char *client_id, const char *w
} }
// Now establish the tunnel for this connection // Now establish the tunnel for this connection
int tunnel_sock = setup_tunnel(wssshd_host, wssshd_port, client_id, local_port, config->debug, 0, config->tunnel_host); // In daemon mode, use port 0 to let setup_tunnel find an available port for WebSocket setup,
// but we'll use our already accepted socket for the local connection
int tunnel_sock = setup_tunnel(wssshd_host, wssshd_port, client_id, 0, config->debug, 0, config->tunnel_host);
if (tunnel_sock < 0) { if (tunnel_sock < 0) {
fprintf(stderr, "Failed to establish tunnel for connection\n"); fprintf(stderr, "Failed to establish tunnel for connection\n");
close(accepted_sock); close(accepted_sock);
continue; // Don't exit, keep listening for new connections continue; // Don't exit, keep listening for new connections
} }
// Close the dummy listening socket created by setup_tunnel
close(tunnel_sock);
// Print tunnel information (unless silent mode) // Print tunnel information (unless silent mode)
if (config->mode != MODE_SILENT) { if (config->mode != MODE_SILENT) {
...@@ -1344,11 +1345,6 @@ int run_daemon_mode(wsssh_config_t *config, const char *client_id, const char *w ...@@ -1344,11 +1345,6 @@ int run_daemon_mode(wsssh_config_t *config, const char *client_id, const char *w
fflush(stdout); fflush(stdout);
} }
} else if (frame_type == 0x81 || frame_type == 0x82) { // Text or binary frame } else if (frame_type == 0x81 || frame_type == 0x82) { // Text or binary frame
if (config->debug) {
printf("[DEBUG - WebSockets] Received message: %.*s\n", payload_len, payload);
fflush(stdout);
}
// Copy payload to buffer // Copy payload to buffer
if ((size_t)payload_len < sizeof(buffer)) { if ((size_t)payload_len < sizeof(buffer)) {
memcpy(buffer, payload, payload_len); memcpy(buffer, payload, payload_len);
...@@ -1359,8 +1355,17 @@ int run_daemon_mode(wsssh_config_t *config, const char *client_id, const char *w ...@@ -1359,8 +1355,17 @@ int run_daemon_mode(wsssh_config_t *config, const char *client_id, const char *w
continue; continue;
} }
// Check if this is a data message to suppress verbose logging
int is_data_message = (strstr(buffer, "\"type\":\"tunnel_data\"") != NULL ||
strstr(buffer, "\"type\":\"tunnel_response\"") != NULL);
if (config->debug && !is_data_message) {
printf("[DEBUG - WebSockets] Received message: %.*s\n", payload_len, payload);
fflush(stdout);
}
// Handle message // Handle message
if (config->debug) { if (config->debug && !is_data_message) {
printf("[DEBUG - WebSockets] Processing message: %s\n", buffer); printf("[DEBUG - WebSockets] Processing message: %s\n", buffer);
fflush(stdout); fflush(stdout);
} }
...@@ -1483,15 +1488,9 @@ cleanup_and_exit: ...@@ -1483,15 +1488,9 @@ cleanup_and_exit:
fflush(stdout); fflush(stdout);
} }
// In daemon mode, restart to accept new connections instead of exiting // In daemon mode, continue to accept new connections
if (config->daemon) { // Continue the while(1) loop for next connection
daemon_restart = 1;
}
if (!daemon_restart) {
return tunnel_broken ? 1 : 0;
} }
} while (daemon_restart);
// This should never be reached, but just in case // This should never be reached, but just in case
return 0; return 0;
......
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