-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrm.js
42 lines (34 loc) · 1.06 KB
/
wrm.js
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
const http = require('http');
const { WebSocketServer } = require('ws');
// Arg parse
let minimist_opts = {
default: {
debug: false,
port: 3000
},
};
let args = require('minimist')(process.argv.slice(2), minimist_opts);
let { debug, port } = args;
// Create server
const wss = new WebSocketServer({ port });
// Initiate plugboard
let wrmholes = {};
wss.on('connection', (socket, request, client) => {
if (wrmholes[request.url] == undefined) {
wrmholes[request.url] = [];
}
wrmholes[request.url].push(socket);
socket.on('close', (code, reason) => {
wrmholes[request.url] = wrmholes[request.url].filter(
s => s != socket
);
});
socket.on('message', (data) => {
if (debug)
console.log(`${request.socket.remoteAddress} in ${request.url} sent: %s`, data);
wrmholes[request.url].filter(s => s != socket)
.map(client => client.send(data));
});
});
console.log(`Swizzling the spacetime continuum... (on port ${port})`);
console.log(`Debug mode: ${debug}`);