|
|
|
@ -3,8 +3,8 @@
|
|
|
|
|
import debug from 'debug';
|
|
|
|
|
import dotenv from 'dotenv';
|
|
|
|
|
import express from 'express';
|
|
|
|
|
import http from 'http';
|
|
|
|
|
import {Server} from 'socket.io';
|
|
|
|
|
import { createServer } from 'node:http';
|
|
|
|
|
import { Server } from 'socket.io';
|
|
|
|
|
/*
|
|
|
|
|
import * as prometheus from 'socket.io-prometheus-metrics';
|
|
|
|
|
|
|
|
|
@ -13,10 +13,12 @@ wich is moderate vulnerable to regular expression denial of service when untrust
|
|
|
|
|
input is passed into the o formatter.
|
|
|
|
|
|
|
|
|
|
alternatively could be used prom-client
|
|
|
|
|
import
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
const serverDebug = debug('server');
|
|
|
|
|
const serverDebug = debug('httpServer');
|
|
|
|
|
const app = express();
|
|
|
|
|
const port = process.env.PORT || 80; // default port to listen
|
|
|
|
|
const httpServer = createServer(app);
|
|
|
|
|
|
|
|
|
|
dotenv.config(
|
|
|
|
|
process.env.NODE_ENV === 'development'
|
|
|
|
@ -24,25 +26,24 @@ dotenv.config(
|
|
|
|
|
: { path: '.env.production' }
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
|
const port = process.env.PORT || 80; // default port to listen
|
|
|
|
|
|
|
|
|
|
app.get('/', (req, res) => {
|
|
|
|
|
res.send('Excalidraw backend is up :)');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const server = http.createServer(app);
|
|
|
|
|
|
|
|
|
|
server.listen(port, () => {
|
|
|
|
|
httpServer.listen(port, () => {
|
|
|
|
|
serverDebug(`listening on port: ${port}`);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const io = require("socket.io")(Server, {
|
|
|
|
|
cors: {
|
|
|
|
|
origin: "https://jitsi.test.meissa.de",
|
|
|
|
|
credentials: true
|
|
|
|
|
},
|
|
|
|
|
maxHttpBufferSize: 10e6,
|
|
|
|
|
const corsOptions = {
|
|
|
|
|
origin: 'https://meet.jit.si',
|
|
|
|
|
methods: ["GET", "POST"],
|
|
|
|
|
credentials: true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const io = new Server(httpServer, {
|
|
|
|
|
allowEIO3: true,
|
|
|
|
|
cors: corsOptions,
|
|
|
|
|
maxHttpBufferSize: 1e6,
|
|
|
|
|
pingTimeout: 10000
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
@ -65,21 +66,19 @@ or more:
|
|
|
|
|
https://codersociety.com/blog/articles/nodejs-application-monitoring-with-prometheus-and-grafana
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
io.on('connection', socket => {
|
|
|
|
|
io.on('connection', (socket) => {
|
|
|
|
|
serverDebug(`connection established! ${socket.conn.request.url}`);
|
|
|
|
|
io.to(`${socket.id}`).emit('init-room');
|
|
|
|
|
socket.on('join-room', roomID => {
|
|
|
|
|
serverDebug(`${socket.id} has joined ${roomID} for url ${socket.conn.request.url}`);
|
|
|
|
|
socket.join(roomID);
|
|
|
|
|
if (io.sockets.adapter.rooms[roomID].length <= 1) {
|
|
|
|
|
if (io.sockets.adapter.rooms.get(roomID)?.size ?? 0 <= 1) {
|
|
|
|
|
io.to(`${socket.id}`).emit('first-in-room');
|
|
|
|
|
} else {
|
|
|
|
|
socket.broadcast.to(roomID).emit('new-user', socket.id);
|
|
|
|
|
}
|
|
|
|
|
io.in(roomID).emit(
|
|
|
|
|
'room-user-change',
|
|
|
|
|
Object.keys(io.sockets.adapter.rooms[roomID].sockets)
|
|
|
|
|
'room-user-change', Array.from(io.sockets.adapter.rooms.get(roomID) ?? [])
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
@ -87,8 +86,7 @@ io.on('connection', socket => {
|
|
|
|
|
'server-broadcast',
|
|
|
|
|
(roomID: string, encryptedData: ArrayBuffer, iv: Uint8Array) => {
|
|
|
|
|
socket.broadcast.to(roomID).emit('client-broadcast', encryptedData, iv);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
socket.on(
|
|
|
|
|
'server-volatile-broadcast',
|
|
|
|
@ -96,15 +94,14 @@ io.on('connection', socket => {
|
|
|
|
|
socket.volatile.broadcast
|
|
|
|
|
.to(roomID)
|
|
|
|
|
.emit('client-broadcast', encryptedData, iv);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
socket.on('disconnecting', () => {
|
|
|
|
|
const rooms = io.sockets.adapter.rooms;
|
|
|
|
|
|
|
|
|
|
for (const roomID of Object.keys(socket.rooms)) {
|
|
|
|
|
const clients = Object.keys(rooms[roomID].sockets).filter(id => id !== socket.id);
|
|
|
|
|
|
|
|
|
|
const clients = Array.from(rooms.get(roomID) ?? []).filter(id => id !== socket.id);
|
|
|
|
|
|
|
|
|
|
if (roomID !== socket.id) {
|
|
|
|
|
socket.to(roomID).emit('user has left', socket.id);
|
|
|
|
|
}
|
|
|
|
|