Fix web server startup and HTTP response issues

- Move web_start_server() before websocket_start_server() to ensure web server starts
- Implement full HTTP server with request parsing and response handling
- Fix HTTP redirect to properly send Location header instead of Set-Cookie
- Remove non-blocking socket setting that caused inconsistent responses
- Update shutdown sequence to stop web server before WebSocket server
parent 9e469d87
......@@ -21,6 +21,7 @@
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#include <time.h>
#include "config.h"
......@@ -53,18 +54,25 @@ static void *cleanup_thread(void *arg) {
websocket_cleanup_expired_clients(state);
websocket_check_keepalive_timeouts(state);
// Print status every 60 seconds
// Print status every 60 seconds (only when not in debug mode)
static time_t last_status_time = 0;
time_t current_time = time(NULL);
if (current_time - last_status_time >= 60) {
if (!state->debug && current_time - last_status_time >= 60) {
size_t active_clients = 0;
for (size_t i = 0; i < state->clients_count; i++) {
if (state->clients[i].active) active_clients++;
}
size_t active_tunnels = 0;
unsigned long long total_transfer_rate = 0;
for (size_t i = 0; i < state->tunnels_count; i++) {
if (state->tunnels[i]->status == TUNNEL_STATUS_ACTIVE) active_tunnels++;
if (state->tunnels[i]->status == TUNNEL_STATUS_ACTIVE) {
active_tunnels++;
total_transfer_rate += state->tunnels[i]->bytes_last_period;
// Reset bytes_last_period for next period
state->tunnels[i]->bytes_last_period = 0;
state->tunnels[i]->last_stats_reset = current_time;
}
}
time_t uptime = current_time - state->start_time;
......@@ -72,9 +80,25 @@ static void *cleanup_thread(void *arg) {
int minutes = (uptime % 3600) / 60;
int seconds = uptime % 60;
printf("[STATUS] Uptime: %02d:%02d:%02d | Clients: %zu/%zu active | Tunnels: %zu/%zu active\n",
hours, minutes, seconds, active_clients, state->clients_count,
active_tunnels, state->tunnels_count);
printf("[STATUS] Server uptime: %02d:%02d:%02d\n", hours, minutes, seconds);
printf("[STATUS] Connected wssshc clients (%zu):\n", active_clients);
for (size_t i = 0; i < state->clients_count; i++) {
if (state->clients[i].active) {
printf("[STATUS] - %s\n", state->clients[i].client_id);
}
}
printf("[STATUS] Active tunnels (%zu/%zu):\n", active_tunnels, state->tunnels_count);
for (size_t i = 0; i < state->tunnels_count; i++) {
if (state->tunnels[i]->status == TUNNEL_STATUS_ACTIVE) {
time_t tunnel_uptime = current_time - state->tunnels[i]->created_at;
int t_hours = tunnel_uptime / 3600;
int t_minutes = (tunnel_uptime % 3600) / 60;
int t_seconds = tunnel_uptime % 60;
printf("[STATUS] - %s: uptime %02d:%02d:%02d\n",
state->tunnels[i]->request_id, t_hours, t_minutes, t_seconds);
}
}
printf("[STATUS] Transfer rate since last statistic: %llu bytes\n", total_transfer_rate);
last_status_time = current_time;
}
......@@ -84,6 +108,9 @@ static void *cleanup_thread(void *arg) {
}
int main(int argc, char *argv[]) {
// Create new process group to avoid receiving SIGINT from terminal
setpgid(0, 0);
// Parse configuration
wssshd_config_t *config = load_config(argc, argv);
if (!config) {
......@@ -106,22 +133,24 @@ int main(int argc, char *argv[]) {
// Set up signal handlers
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGPIPE, SIG_IGN); // Ignore SIGPIPE to prevent crashes on broken connections
printf("WSSSH Daemon starting...\n");
// Start web interface if configured
if (web_start_server(config, state) != 0) {
fprintf(stderr, "Warning: Failed to start web interface\n");
}
// Start WebSocket server
if (websocket_start_server(config, state) != 0) {
fprintf(stderr, "Failed to start WebSocket server\n");
web_stop_server();
websocket_free_state(state);
free_config(config);
return 1;
}
// Start web interface if configured
if (web_start_server(config, state) != 0) {
fprintf(stderr, "Warning: Failed to start web interface\n");
}
printf("WSSSH Daemon running on %s:%d\n", config->host, config->port);
printf("Press Ctrl+C to stop the server\n");
......@@ -142,8 +171,8 @@ int main(int argc, char *argv[]) {
printf("\nShutting down WSSSH Daemon...\n");
// Stop servers
websocket_stop_server();
web_stop_server();
websocket_stop_server();
// Clean up state
websocket_free_state(state);
......
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