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
| const express = require('express');
const path = require('path');
const fs = require('fs');
const ws = require('ws');
const app = express();
const HOSTNAME = 'testing.com';
const PORT = 8443;
// TLS Options
const serverOptions = {
cert: fs.readFileSync(path.join(__dirname, 'certs/host.crt')),
key: fs.readFileSync(path.join(__dirname, 'certs/host.key'))
}
const httpServer = require('https').Server(serverOptions, app);
const io = require('socket.io')(httpServer);
// New WebSocket Connection
io.on('connection', (socket) => {
// Verify cookie is valid here
let cookie = socket.handshake?.headers?.cookie || 'N/A';
socket.on('/endpoint/1', (msg) => {
console.log('/endpoint/1', msg);
});
socket.on('/endpoint/2', (msg) => {
console.log('/endpoint/2', msg);
socket.emit('/api/connected', 'Connection Success');
});
socket.on('/close-socket', () => {
socket.disconnect();
});
socket.on('disconnect', () => {
console.log('Socket Closed:', socket.id);
});
});
// Don't allow standard WebSocket Connections
httpServer.on('upgrade', (request, socket) => {
console.log('here');
socket.destroy();
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
// Start the Server
httpServer.listen(PORT, () => {
console.log(`[-] Server Listening on Port ${PORT}`);
});
|