forked from miroslavpejic85/mirotalk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·430 lines (382 loc) · 13.2 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
http://patorjk.com/software/taag/#p=display&f=ANSI%20Regular&t=Server
███████ ███████ ██████ ██ ██ ███████ ██████
██ ██ ██ ██ ██ ██ ██ ██ ██
███████ █████ ██████ ██ ██ █████ ██████
██ ██ ██ ██ ██ ██ ██ ██ ██
███████ ███████ ██ ██ ████ ███████ ██ ██
*/
"use strict"; // https://www.w3schools.com/js/js_strict.asp
require("dotenv").config();
const express = require("express");
const path = require("path");
const app = express();
const http = require("http");
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server().listen(server);
const ngrok = require("ngrok");
var PORT = process.env.PORT || 3000; // signalingServerPort
var localHost = "http://localhost:" + PORT; // http
var channels = {}; // collect channels
var sockets = {}; // collect sockets
var peers = {}; // collect peers info grp by channels
var ngrokEnabled = process.env.NGROK_ENABLED;
var ngrokAuthToken = process.env.NGROK_AUTH_TOKEN;
var turnEnabled = process.env.TURN_ENABLED;
var turnUrls = process.env.TURN_URLS;
var turnUsername = process.env.TURN_USERNAME;
var turnCredential = process.env.TURN_PASSWORD;
// Use all static files from the www folder
app.use(express.static(path.join(__dirname, "www")));
// Remove trailing slashes in url
app.use(function (req, res, next) {
if (req.path.substr(-1) === "/" && req.path.length > 1) {
let query = req.url.slice(req.path.length);
res.redirect(301, req.path.slice(0, -1) + query);
} else {
next();
}
});
/*
app.get(["/"], (req, res) =>
res.sendFile(path.join(__dirname, "www/client.html"))
); */
// all start from here
app.get(["/"], (req, res) =>
res.sendFile(path.join(__dirname, "www/landing.html"))
);
// set new room name and join
app.get(["/newcall"], (req, res) =>
res.sendFile(path.join(__dirname, "www/newcall.html"))
);
// if not allow video/audio
app.get(["/permission"], (req, res) =>
res.sendFile(path.join(__dirname, "www/permission.html"))
);
// no room name specified to join
app.get("/join/", function (req, res) {
res.redirect("/");
});
// join to room
app.get("/join/*", function (req, res) {
if (Object.keys(req.query).length > 0) {
console.log("redirect:" + req.url + " to " + url.parse(req.url).pathname);
res.redirect(url.parse(req.url).pathname);
} else {
res.sendFile(path.join(__dirname, "www/client.html"));
}
});
/**
* You should probably use a different stun-turn server
* doing commercial stuff, also see:
*
* https://gist.github.com/zziuni/3741933
* https://www.twilio.com/docs/stun-turn
* https://github.com/coturn/coturn
*
* Check the functionality of STUN/TURN servers:
* https://webrtc.github.io/samples/src/content/peerconnection/trickle-ice/
*/
var iceServers = [{ urls: "stun:stun.l.google.com:19302" }];
if (turnEnabled == "true") {
iceServers.push({
urls: turnUrls,
username: turnUsername,
credential: turnCredential,
});
}
/**
* Expose server to external with https tunnel using ngrok
* https://ngrok.com
*/
async function ngrokStart() {
try {
await ngrok.authtoken(ngrokAuthToken);
await ngrok.connect(PORT);
let api = ngrok.getApi();
let data = await api.get("api/tunnels");
data = JSON.parse(data);
// console.log(data);
let pu0 = data.tunnels[0].public_url;
let pu1 = data.tunnels[1].public_url;
let tunnelHttps = pu0.startsWith("https") ? pu0 : pu1;
// server settings
console.log("settings", {
http: localHost,
https: tunnelHttps,
iceServers: iceServers,
ngrok: {
ngrok_enabled: ngrokEnabled,
ngrok_token: ngrokAuthToken,
},
});
} catch (e) {
console.error("[Error] ngrokStart", e);
}
}
/**
* Start Local Server with ngrok https tunnel (optional)
*/
server.listen(PORT, null, function () {
console.log(
`%c
███████╗██╗ ██████╗ ███╗ ██╗ ███████╗███████╗██████╗ ██╗ ██╗███████╗██████╗
██╔════╝██║██╔════╝ ████╗ ██║ ██╔════╝██╔════╝██╔══██╗██║ ██║██╔════╝██╔══██╗
███████╗██║██║ ███╗██╔██╗ ██║█████╗███████╗█████╗ ██████╔╝██║ ██║█████╗ ██████╔╝
╚════██║██║██║ ██║██║╚██╗██║╚════╝╚════██║██╔══╝ ██╔══██╗╚██╗ ██╔╝██╔══╝ ██╔══██╗
███████║██║╚██████╔╝██║ ╚████║ ███████║███████╗██║ ██║ ╚████╔╝ ███████╗██║ ██║
╚══════╝╚═╝ ╚═════╝ ╚═╝ ╚═══╝ ╚══════╝╚══════╝╚═╝ ╚═╝ ╚═══╝ ╚══════╝╚═╝ ╚═╝ started...
`,
"font-family:monospace"
);
// https tunnel
if (ngrokEnabled == "true") {
ngrokStart();
} else {
// server settings
console.log("settings", {
http: localHost,
iceServers: iceServers,
});
}
});
/**
* Users will connect to the signaling server, after which they'll issue a "join"
* to join a particular channel. The signaling server keeps track of all sockets
* who are in a channel, and on join will send out 'addPeer' events to each pair
* of users in a channel. When clients receive the 'addPeer' even they'll begin
* setting up an RTCPeerConnection with one another. During this process they'll
* need to relay ICECandidate information to one another, as well as SessionDescription
* information. After all of that happens, they'll finally be able to complete
* the peer connection and will be in streaming audio/video between eachother.
* On peer connected
*/
io.sockets.on("connect", (socket) => {
console.log("[" + socket.id + "] --> connection accepted");
socket.channels = {};
sockets[socket.id] = socket;
/**
* On peer diconnected
*/
socket.on("disconnect", () => {
for (var channel in socket.channels) {
removePeerFrom(channel);
}
console.log("[" + socket.id + "] <--> disconnected");
delete sockets[socket.id];
});
/**
* On peer join
*/
socket.on("join", (config) => {
console.log("[" + socket.id + "] --> join ", config);
var channel = config.channel;
var peer_name = config.peerName;
var peer_video = config.peerVideo;
var peer_audio = config.peerAudio;
if (channel in socket.channels) {
console.log("[" + socket.id + "] [Warning] already joined", channel);
return;
}
// no channel aka room in channels init
if (!(channel in channels)) {
channels[channel] = {};
}
// no channel aka room in peers init
if (!(channel in peers)) {
peers[channel] = {};
}
// collect peers info grp by channels
peers[channel][socket.id] = {
peer_name: peer_name,
peer_video: peer_video,
peer_audio: peer_audio,
};
console.log("connected peers grp by roomId", peers);
for (var id in channels[channel]) {
// offer false
channels[channel][id].emit("addPeer", {
peer_id: socket.id,
peers: peers[channel],
should_create_offer: false,
iceServers: iceServers,
});
// offer true
socket.emit("addPeer", {
peer_id: id,
peers: peers[channel],
should_create_offer: true,
iceServers: iceServers,
});
console.log("[" + socket.id + "] emit add Peer [" + id + "]");
}
channels[channel][socket.id] = socket;
socket.channels[channel] = channel;
});
/**
* Remove peers from channel aka room
* @param {*} channel
*/
async function removePeerFrom(channel) {
if (!(channel in socket.channels)) {
console.log("[" + socket.id + "] [Warning] not in ", channel);
return;
}
delete socket.channels[channel];
delete channels[channel][socket.id];
delete peers[channel][socket.id];
// if not channel aka room in peers remove it
if (Object.keys(peers[channel]).length === 0) {
delete peers[channel];
}
for (var id in channels[channel]) {
await channels[channel][id].emit("removePeer", { peer_id: socket.id });
await socket.emit("removePeer", { peer_id: id });
console.log("[" + socket.id + "] emit remove Peer [" + id + "]");
}
}
/**
* Relay ICE to peers
*/
socket.on("relayICE", (config) => {
let peer_id = config.peer_id;
let ice_candidate = config.ice_candidate;
/*
console.log(
"[" + socket.id + "] relay ICE-candidate to [" + peer_id + "] ",
{ address: config.ice_candidate.address }
); // ice_candidate
*/
if (peer_id in sockets) {
sockets[peer_id].emit("iceCandidate", {
peer_id: socket.id,
ice_candidate: ice_candidate,
});
}
});
/**
* Relay SDP to peers
*/
socket.on("relaySDP", (config) => {
let peer_id = config.peer_id;
let session_description = config.session_description;
console.log(
"[" + socket.id + "] relay SessionDescription to [" + peer_id + "] ",
{ type: session_description.type }
); // session_description
if (peer_id in sockets) {
sockets[peer_id].emit("sessionDescription", {
peer_id: socket.id,
session_description: session_description,
});
}
});
/**
* Relay MSG to peers
*/
socket.on("msg", (config) => {
let peerConnections = config.peerConnections;
let room_id = config.room_id;
let name = config.name;
let msg = config.msg;
console.log(
"[" + socket.id + "] emit onMessage to [room_id: " + room_id + "]",
{
name: name,
msg: msg,
}
);
for (var peer_id in peerConnections) {
sockets[peer_id].emit("onMessage", {
name: name,
msg: msg,
});
}
});
/**
* Relay NAME to peers
*/
socket.on("cName", (config) => {
let peerConnections = config.peerConnections;
let room_id = config.room_id;
let peer_name_old = config.peer_name_old;
let peer_name_new = config.peer_name_new;
let peer_id_to_update = null;
// update peers new name in the specified room
for (var peer_id in peers[room_id]) {
if (peers[room_id][peer_id]["peer_name"] == peer_name_old) {
peers[room_id][peer_id]["peer_name"] = peer_name_new;
peer_id_to_update = peer_id;
/*
console.log("[" + socket.id + "] change peer name", {
room_id: room_id,
peer_id: peer_id,
peer_name_old: peer_name_old,
peer_name_new: peer_name_new,
});
*/
}
}
// refresh if found
if (peer_id_to_update && Object.keys(peerConnections).length != 0) {
console.log(
"[" + socket.id + "] emit onCName to [room_id: " + room_id + "]",
{
peer_id: peer_id_to_update,
peer_name: peer_name_new,
}
);
for (var peer_id in peerConnections) {
sockets[peer_id].emit("onCName", {
peer_id: peer_id_to_update,
peer_name: peer_name_new,
});
}
}
});
/**
* Relay Audio Video Status to peers
*/
socket.on("vaStatus", (config) => {
let peerConnections = config.peerConnections;
let room_id = config.room_id;
let peer_name = config.peer_name;
let element = config.element;
let status = config.status;
// update peers video-audio status in the specified room
for (var peer_id in peers[room_id]) {
if (peers[room_id][peer_id]["peer_name"] == peer_name) {
element == "video"
? (peers[room_id][peer_id]["peer_video"] = status)
: (peers[room_id][peer_id]["peer_audio"] = status);
/*
console.log("[" + socket.id + "] change " + element + " status", {
room_id: room_id,
peer_name: peer_name,
element: element,
status: status,
});
*/
}
}
// socket.id aka peer that send this status
if (Object.keys(peerConnections).length != 0) {
console.log(
"[" + socket.id + "] emit onVAStatus to [room_id: " + room_id + "]",
{
peer_id: socket.id,
element: element,
status: status,
}
);
for (var peer_id in peerConnections) {
sockets[peer_id].emit("onVAStatus", {
peer_id: socket.id,
element: element,
status: status,
});
}
}
});
}); // end [sockets.on-connect]