-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathws.js
More file actions
151 lines (144 loc) · 5.75 KB
/
Copy pathws.js
File metadata and controls
151 lines (144 loc) · 5.75 KB
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
import { WebSocketServer } from "ws";
import * as config from "#src/config.js";
import { WS_CLOSE_CODE } from "#src/shared/enums.js";
import { Bus } from "#src/shared/bus.js";
import { Logger, extractRequestInfo } from "#src/utils/utils.js";
import { AuthenticationError, OvercrowdedError } from "#src/utils/errors.js";
import { SESSION_CLOSE_CODE } from "#src/models/session.js";
import { Channel } from "#src/models/channel.js";
import { JsonWebToken } from "#src/services/auth.js";
/**
* @typedef Credentials
* @property {string} channelUUID deprecated, this is obtained from the jwt
* @property {string} jwt
*/
const logger = new Logger("WS");
/** @type {Map<number, import("ws").WebSocket>} */
const unauthenticatedWebSockets = new Map();
const authenticatedWebSockets = new Set();
let pendingId = 0;
/** @type {import("ws").WebSocketServer} */
let server;
/**
* @param {Parameters<import("ws").WebSocketServer>[0]} options WebSocketServer options
* @returns {Promise<import("ws").WebSocketServer>}
*/
export async function start(options) {
logger.info("starting...");
server = new WebSocketServer(options);
/**
* This is the entry point for clients, from here the client initiates a webSocket connection to the server,
* provides the secret/jwt for authentication, and if the authentication is successful, the server creates a session
* and connects it, otherwise the server closes the webSocket (and no rtc session is created).
*/
server.on("connection", (webSocket, req) => {
const { remoteAddress } = extractRequestInfo(req);
logger.info(`new webSocket connection from ${remoteAddress}`);
const currentPendingId = pendingId++;
unauthenticatedWebSockets.set(currentPendingId, webSocket);
const timeout = setTimeout(() => {
if (webSocket.readyState > webSocket.OPEN) {
return;
}
webSocket.close(WS_CLOSE_CODE.TIMEOUT);
logger.warn(`${remoteAddress} WS timed out, closing it`);
unauthenticatedWebSockets.delete(currentPendingId);
}, config.timeouts.authentication);
webSocket.once("message", (message) => {
try {
/** @type {Credentials | String} can be a string (the jwt) for backwards compatibility with version 1.1 and earlier */
const credentials = JSON.parse(message);
const session = connect(webSocket, {
jwt: credentials.jwt || credentials,
});
session.remote = remoteAddress;
logger.info(`session [${session.name}] authenticated and created`);
} catch (error) {
logger.warn(`${error.message} : ${error.cause ?? ""}`);
if (error instanceof AuthenticationError) {
webSocket.close(WS_CLOSE_CODE.AUTHENTICATION_FAILED);
} else if (error instanceof OvercrowdedError) {
webSocket.close(WS_CLOSE_CODE.CHANNEL_FULL);
} else {
webSocket.close(WS_CLOSE_CODE.ERROR);
}
}
unauthenticatedWebSockets.delete(currentPendingId);
authenticatedWebSockets.add(webSocket);
clearTimeout(timeout);
});
});
return server;
}
/**
* @param {WS_CLOSE_CODE[keyof WS_CLOSE_CODE]} [closeCode]
*/
export function closeAllWebSockets(closeCode = WS_CLOSE_CODE.CLEAN) {
for (const webSocket of authenticatedWebSockets) {
if (webSocket.readyState < webSocket.CLOSING) {
webSocket.close(closeCode);
}
}
for (const webSocket of unauthenticatedWebSockets.values()) {
if (webSocket.readyState < webSocket.CLOSING) {
webSocket.close(closeCode);
}
}
unauthenticatedWebSockets.clear();
}
export function close() {
closeAllWebSockets();
server?.close();
}
/**
* @param {import("ws").WebSocket} webSocket
* @param {Credentials}
*/
function connect(webSocket, { jwt }) {
const token = new JsonWebToken(jwt);
const channel = Channel.records.get(token.unsafe.claims.sfu_channel_uuid);
if (!channel) {
throw new AuthenticationError(`Channel does not exist`);
}
const authResult = token.verify(channel.key);
const { session_id, ice_servers } = authResult;
if (!session_id) {
throw new AuthenticationError("Malformed JWT payload");
}
webSocket.send(); // client can start using ws after this message.
const bus = new Bus(webSocket, { batchDelay: config.timeouts.busBatch });
const { session } = Channel.join(channel.uuid, session_id);
session.once("close", ({ code }) => {
let wsCloseCode = WS_CLOSE_CODE.CLEAN;
switch (code) {
case SESSION_CLOSE_CODE.ERROR:
wsCloseCode = WS_CLOSE_CODE.ERROR;
break;
case SESSION_CLOSE_CODE.KICKED:
case SESSION_CLOSE_CODE.REPLACED:
case SESSION_CLOSE_CODE.CHANNEL_CLOSED:
wsCloseCode = WS_CLOSE_CODE.KICKED;
break;
case SESSION_CLOSE_CODE.C_TIMEOUT:
case SESSION_CLOSE_CODE.P_TIMEOUT:
wsCloseCode = WS_CLOSE_CODE.TIMEOUT;
break;
}
if (webSocket.readyState < webSocket.CLOSING) {
webSocket.close(wsCloseCode);
}
});
webSocket.once("close", (code, message) => {
authenticatedWebSockets.delete(webSocket);
session.close({
code: SESSION_CLOSE_CODE.WS_CLOSED,
cause: `ws closed with code ${code}: ${message}`,
});
});
webSocket.on("error", (error) =>
session.close({ code: SESSION_CLOSE_CODE.WS_ERROR, cause: error.message })
);
// Not awaiting connect
session.connect(bus, ice_servers);
return session;
}