File size: 1,687 Bytes
6f494de
 
 
 
 
 
 
 
 
 
 
 
d996ccd
6f494de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import http from 'http';
import { WebSocketServer } from 'ws';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { handleTerminalConnection, setSharedTerminalMode } from './terminal.js';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// Config
setSharedTerminalMode(false); // Set this to false to allow a shared session
const port = 7860;

const server = http.createServer((req, res) => {
    if (req.method === 'GET') {
        const routeName = req.url.slice(1);
        const assetObj = {
            '': { file: "index.html", contentType: "text/html" },
            'client.js': { file: "client.js", contentType: "text/javascript" }
        }[routeName];

        if (!assetObj) {
            res.writeHead(404, { 'Content-Type': 'text/plain' });
            return res.end('Path not found');
        }

        const filePath = path.join(__dirname, assetObj.file);

        fs.readFile(filePath, (err, data) => {
            if (err) {
                res.writeHead(500, { 'Content-Type': 'text/plain' });
                res.end('Failed to load file');
            } else {
                res.writeHead(200, { 'Content-Type': assetObj.contentType });
                res.end(data);
            }
        });
    }
});

const wss = new WebSocketServer({ noServer: true });

wss.on('connection', handleTerminalConnection);

server.on('upgrade', (request, socket, head) => {
    wss.handleUpgrade(request, socket, head, (ws) => {
        wss.emit('connection', ws, request);
    });
});

server.listen(port, () => {
    console.log(`HTTP and WebSocket server is running on port ${port}`);
});