2020-05-27 20:33:51 +00:00
|
|
|
const fs = require("fs");
|
|
|
|
const https = require("https");
|
|
|
|
const WebSocket = require("ws");
|
|
|
|
|
2020-12-12 20:33:38 +00:00
|
|
|
const PING_INTERVAL = 30000; // 30 seconds
|
|
|
|
|
2020-05-27 20:33:51 +00:00
|
|
|
const server = https.createServer({
|
|
|
|
cert: fs.readFileSync("cert.pem"),
|
|
|
|
key: fs.readFileSync("key.pem")
|
|
|
|
});
|
|
|
|
const wss = new WebSocket.Server({
|
2020-06-04 19:44:25 +00:00
|
|
|
...(process.env.NODE_ENV === "development" ? { port: 8081 } : { server }),
|
2020-05-27 20:33:51 +00:00
|
|
|
verifyClient: info =>
|
2020-07-19 18:40:40 +00:00
|
|
|
!!info.origin.match(
|
2020-11-29 20:34:05 +00:00
|
|
|
/^https?:\/\/([^.]+\.github\.io|localhost|live\.clocktower\.online|eddbra1nprivatetownsquare\.xyz)/i
|
2020-07-19 18:40:40 +00:00
|
|
|
)
|
2020-05-27 20:33:51 +00:00
|
|
|
});
|
|
|
|
|
2020-06-04 19:44:25 +00:00
|
|
|
function noop() {}
|
|
|
|
|
2020-11-29 20:34:05 +00:00
|
|
|
// calculate latency on heartbeat
|
2020-06-04 19:44:25 +00:00
|
|
|
function heartbeat() {
|
2020-09-21 09:19:46 +00:00
|
|
|
this.latency = Math.round((new Date().getTime() - this.pingStart) / 2);
|
2020-12-12 20:33:38 +00:00
|
|
|
this.counter = 0;
|
2020-06-04 19:44:25 +00:00
|
|
|
this.isAlive = true;
|
|
|
|
}
|
|
|
|
|
2020-11-29 20:34:05 +00:00
|
|
|
// map of channels currently in use
|
|
|
|
const channels = {};
|
|
|
|
|
|
|
|
// a new client connects
|
2020-05-27 20:33:51 +00:00
|
|
|
wss.on("connection", function connection(ws, req) {
|
|
|
|
ws.channel = req.url
|
|
|
|
.split("/")
|
|
|
|
.pop()
|
|
|
|
.toLocaleLowerCase();
|
2020-09-21 09:19:46 +00:00
|
|
|
if (ws.channel.match(/-host$/i)) {
|
|
|
|
ws.isHost = true;
|
|
|
|
ws.channel = ws.channel.substr(0, ws.channel.length - 5);
|
|
|
|
// check for another host on this channel
|
|
|
|
if (
|
2020-11-29 20:34:05 +00:00
|
|
|
channels[ws.channel] &&
|
|
|
|
channels[ws.channel].some(
|
2020-09-21 09:19:46 +00:00
|
|
|
client =>
|
2020-11-29 20:34:05 +00:00
|
|
|
client !== ws && client.readyState === WebSocket.OPEN && client.isHost
|
2020-09-21 09:19:46 +00:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
console.log(ws.channel, "duplicate host");
|
|
|
|
ws.close(1000, `The channel "${ws.channel}" already has a host`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-06-04 19:44:25 +00:00
|
|
|
ws.isAlive = true;
|
2020-09-21 09:19:46 +00:00
|
|
|
ws.pingStart = new Date().getTime();
|
2020-12-12 20:33:38 +00:00
|
|
|
ws.counter = 0;
|
2020-11-29 20:34:05 +00:00
|
|
|
// add channel to list
|
|
|
|
if (!channels[ws.channel]) {
|
|
|
|
channels[ws.channel] = [];
|
|
|
|
}
|
|
|
|
channels[ws.channel].push(ws);
|
|
|
|
// start ping pong
|
2020-09-21 09:19:46 +00:00
|
|
|
ws.ping(noop);
|
2020-06-04 19:44:25 +00:00
|
|
|
ws.on("pong", heartbeat);
|
2020-11-29 20:34:05 +00:00
|
|
|
// remove client from channels on close
|
|
|
|
ws.on("close", () => {
|
|
|
|
const index = channels[ws.channel].indexOf(ws);
|
|
|
|
if (index >= 0) {
|
|
|
|
channels[ws.channel].splice(index, 1);
|
|
|
|
}
|
|
|
|
if (!channels[ws.channel].length) delete channels[ws.channel];
|
|
|
|
});
|
|
|
|
// handle message
|
2020-05-27 20:33:51 +00:00
|
|
|
ws.on("message", function incoming(data) {
|
2020-12-12 20:33:38 +00:00
|
|
|
// check rate limit (max 5msg/second)
|
|
|
|
ws.counter++;
|
|
|
|
if (ws.counter > (5 * PING_INTERVAL) / 1000) {
|
|
|
|
console.log(ws.channel, "disconnecting user due to spam");
|
|
|
|
ws.close(
|
|
|
|
1000,
|
|
|
|
"Your app seems to be malfunctioning, please clear your browser cache."
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2020-09-21 09:19:46 +00:00
|
|
|
const isPing = data.match(/^\["ping/i);
|
|
|
|
if (!isPing) {
|
|
|
|
console.log(new Date(), wss.clients.size, ws.channel, data);
|
2020-05-30 20:01:42 +00:00
|
|
|
}
|
2020-11-29 20:34:05 +00:00
|
|
|
channels[ws.channel].forEach(function each(client) {
|
|
|
|
if (client !== ws && client.readyState === WebSocket.OPEN) {
|
2020-09-21 09:19:46 +00:00
|
|
|
// inject latency between both clients if ping message
|
|
|
|
if (isPing && client.latency && ws.latency) {
|
|
|
|
client.send(data.replace(/latency/, client.latency + ws.latency));
|
|
|
|
} else {
|
|
|
|
client.send(data);
|
|
|
|
}
|
2020-05-27 20:33:51 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-11-29 20:34:05 +00:00
|
|
|
// start ping interval timer
|
2020-06-04 19:44:25 +00:00
|
|
|
const interval = setInterval(function ping() {
|
|
|
|
wss.clients.forEach(function each(ws) {
|
|
|
|
if (ws.isAlive === false) return ws.terminate();
|
|
|
|
ws.isAlive = false;
|
2020-09-21 09:19:46 +00:00
|
|
|
ws.pingStart = new Date().getTime();
|
2020-06-04 19:44:25 +00:00
|
|
|
ws.ping(noop);
|
|
|
|
});
|
2020-12-12 20:33:38 +00:00
|
|
|
}, PING_INTERVAL);
|
2020-06-04 19:44:25 +00:00
|
|
|
|
2020-11-29 20:34:05 +00:00
|
|
|
// handle server shutdown
|
2020-06-04 19:44:25 +00:00
|
|
|
wss.on("close", function close() {
|
|
|
|
clearInterval(interval);
|
|
|
|
});
|
|
|
|
|
2020-11-29 20:34:05 +00:00
|
|
|
// prod mode with stats API
|
2020-06-04 19:44:25 +00:00
|
|
|
if (process.env.NODE_ENV !== "development") {
|
2020-11-29 20:34:05 +00:00
|
|
|
console.log("server starting");
|
2020-06-04 19:44:25 +00:00
|
|
|
server.listen(8080);
|
2020-11-29 20:34:05 +00:00
|
|
|
server.on("request", (req, res) => {
|
|
|
|
res.writeHead(200);
|
|
|
|
res.end(
|
|
|
|
JSON.stringify({
|
|
|
|
players: wss.clients.size,
|
|
|
|
channels: Object.keys(channels).length
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
2020-06-04 19:44:25 +00:00
|
|
|
}
|