All files / server server.js

53.84% Statements 21/39
20% Branches 2/10
25% Functions 2/8
54.05% Lines 20/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85  2x 2x 2x               2x       1x                                             1x 1x     1x 1x     1x 1x     1x     1x                   1x   1x 1x   1x 1x         1x         1x          
import path from 'path';
import { existsSync, readFileSync } from 'fs';
import { createServer as createHttpServer } from 'http';
import { createServer as createHttpsServer } from 'https';
import express from 'express';
import session from 'express-session';
import expressWs from 'express-ws';
import { cleanHeaders } from './utils.js';
import { webSocketInit, onWebSocketConnection } from './websockets.js';
import text0wnz from './text0wnz.js';
 
const startServer = config => {
	let server;
 
	// Check if SSL certificates exist and use HTTPS, otherwise fallback to HTTP
	Iif (config.ssl) {
		const certPath = path.join(config.sslDir, 'letsencrypt-domain.pem');
		const keyPath = path.join(config.sslDir, 'letsencrypt-domain.key');
 
		try {
			if (existsSync(certPath) && existsSync(keyPath)) {
				server = createHttpsServer({
					cert: readFileSync(certPath),
					key: readFileSync(keyPath),
				});
				console.log(
					'* Using HTTPS server with SSL certificates from:',
					config.sslDir,
				);
			} else {
				throw new Error(`! SSL certificates not found in: ${config.sslDir}`);
			}
		} catch (err) {
			console.error('! SSL Error:', err.message);
			console.log('* Falling back to HTTP server');
			server = createHttpServer();
		}
	} else {
		server = createHttpServer();
		console.log('* Using HTTP server (SSL disabled)');
	}
 
	const app = express();
	const allClients = new Set();
 
	// Important: Set up session middleware before WebSocket handling
	app.use(session({ resave: false, saveUninitialized: true, secret: 'sauce' }));
	app.use(express.static('public'));
 
	// Initialize express-ws with the server AFTER session middleware
	expressWs(app, server);
 
	// Debugging middleware for WebSocket upgrade requests
	app.use('/server', (req, _res, next) => {
		if (config.debug) {
			console.log(`* Request to /server endpoint:
  - Method: ${req.method}
  - Headers: ${JSON.stringify(cleanHeaders(req.headers))}`);
		}
		next();
	});
 
	// WebSocket handler function
	webSocketInit(config, allClients);
	// WebSocket routes for both direct and proxy connections
	app.ws('/', onWebSocketConnection);
	app.ws('/server', onWebSocketConnection);
 
	server.listen(config.port, () => {
		Iif (config.debug) {
			console.log(`* Server listening on port: ${config.port}`);
		}
	});
 
	setInterval(() => {
		text0wnz.saveSessionWithTimestamp(() => {});
		text0wnz.saveSession(() => {});
	}, config.saveInterval);
 
	process.on('SIGINT', () => {
		text0wnz.saveSession(() => process.exit());
	});
};
export { startServer };