-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.js
111 lines (92 loc) · 3.6 KB
/
server.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
// Copyright (c) 2021 Steve Seguin. All Rights Reserved.
// Use of this source code is governed by the APGLv3 open-source
//
// This is VDO.Ninja-specific handshake server implementation
// It has better routing isolation and performance than a generic fan-out implementation
//
// >> Use at your own risk, as it still may contain bugs or security vunlerabilities <<
//
///// INSTALLATION
// sudo apt-get update
// sudo apt-get upgrade
// sudo apt-get install nodejs -y
// sudo apt-get install npm -y
// npm install
// sudo add-apt-repository ppa:certbot/certbot
// sudo apt-get install certbot -y
// sudo certbot certonly // register your domain
// nodejs server.js // port 443 needs to be open. THIS STARTS THE SERVER (or create a service instead)
//
//// Finally, within VDO.Ninja, update index.html of the ninja installation as needed, such as with:
// session.wss = "wss://wss.contribute.cam:443";
// session.customWSS = true; # Please refer to the vdo.ninja instructions for exact details on settings; this is just a demo.
/////////////////////////
"use strict";
var fs = require("fs");
var https = require("https");
var express = require("express");
var app = express();
var WebSocket = require("ws");
var cors = require('cors');
var path = require("path");
const key = fs.readFileSync(process.env.KEY_PATH ?? "./key.pem"); /// UPDATE THIS PATH
const cert = fs.readFileSync(process.env.CERT_PATH ?? "./cert.pem"); /// UPDATE THIS PATH
var server = https.createServer({ key, cert }, app);
var websocketServer = new WebSocket.Server({ server });
app.use(cors({
origin: '*'
}));
websocketServer.on('connection', (webSocketClient) => {
var room = false;
webSocketClient.on('message', (message) => {
try {
var msg = JSON.parse(message);
} catch (e) {
return;
}
if (!msg.from) return;
if (!webSocketClient.uuid) {
let alreadyExists = Array.from(websocketServer.clients).some(client => client.uuid && client.uuid === msg.from && client != webSocketClient);
if (alreadyExists) {
webSocketClient.send(JSON.stringify({ error: "uuid already in use" }));
return;
}
webSocketClient.uuid = msg.from;
}
var streamID = false;
try {
if (msg.request == "seed" && msg.streamID) {
streamID = msg.streamID;
} else if (msg.request == "joinroom") {
room = msg.roomid + "";
webSocketClient.room = room;
if (msg.streamID) {
streamID = msg.streamID;
}
}
} catch (e) {
return;
}
if (streamID) {
if (webSocketClient.sid && streamID != webSocketClient.sid) {
webSocketClient.send(JSON.stringify({ error: "can't change sid" }));
return;
}
let alreadyExists = Array.from(websocketServer.clients).some(client => client.sid && client.sid === streamID && client != webSocketClient);
if (alreadyExists) {
webSocketClient.send(JSON.stringify({ error: "sid already in use" }));
return;
}
webSocketClient.sid = streamID;
}
websocketServer.clients.forEach(client => {
if (webSocketClient == client || (msg.UUID && msg.UUID != client.uuid) || (room && (!client.room || client.room !== room)) || (!room && client.room) || (msg.request == "play" && msg.streamID && (!client.sid || client.sid !== msg.streamID))) return;
client.send(message.toString());
});
});
webSocketClient.on('close', function(reasonCode, description) {});
});
app.use('/', express.static(path.join(__dirname, './../vdo.ninja/')))
const port = process.env.PORT ?? 443
server.listen(port, () => { console.log(`Server started on port ${port}`) });