-
Notifications
You must be signed in to change notification settings - Fork 4
/
http.js
215 lines (205 loc) · 7.95 KB
/
http.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
import http from "node:http";
import * as ws from "#src/services/ws.js";
import * as auth from "#src/services/auth.js";
import * as config from "#src/config.js";
import { Logger, parseBody, extractRequestInfo } from "#src/utils/utils.js";
import { SESSION_CLOSE_CODE } from "#src/models/session.js";
import { Channel } from "#src/models/channel.js";
/**
* @typedef {function} routeCallback
* @param {http.IncomingMessage} req
* @param {http.ServerResponse} res
* @param {Object} param2
* @param {string} param2.remoteAddress
* @param {string} param2.protocol
* @param {string} param2.host
* @param {URLSearchParams} param2.searchParams
* @return {http.ServerResponse}
*/
export const API_VERSION = 1;
const logger = new Logger("HTTP");
let httpServer;
/**
* @param {Object} [options]
* @param {string} [options.httpInterface]
* @param {number} [options.port]
*/
export async function start({ httpInterface = config.HTTP_INTERFACE, port = config.PORT } = {}) {
logger.info("starting...");
const routeListener = new RouteListener();
routeListener.get(`/v${API_VERSION}/noop`, {
callback: (req, res) => {
res.statusCode = 200;
return res.end(JSON.stringify({ result: "ok" }));
},
});
routeListener.get(`/v${API_VERSION}/stats`, {
callback: async (req, res) => {
const proms = [];
for (const channel of Channel.records.values()) {
proms.push(channel.getStats());
}
const channelStats = await Promise.all(proms);
res.statusCode = 200;
return res.end(JSON.stringify(channelStats));
},
});
routeListener.get(`/v${API_VERSION}/channel`, {
callback: async (req, res, { host, protocol, remoteAddress, searchParams }) => {
try {
const jsonWebToken = req.headers.authorization?.split(" ")[1];
/** @type {{ iss: string, key: string || undefined }} */
const claims = await auth.verify(jsonWebToken);
if (!claims.iss) {
logger.warn(`${remoteAddress}: missing issuer claim when creating channel`);
res.statusCode = 403; // forbidden
return res.end();
}
const channel = await Channel.create(remoteAddress, claims.iss, {
key: claims.key,
useWebRtc: searchParams.get("webRTC") !== "false",
});
res.setHeader("Content-Type", "application/json");
res.statusCode = 200;
return res.end(
JSON.stringify({
uuid: channel.uuid,
url: `${protocol}://${host}`,
})
);
} catch (error) {
logger.warn(`[${remoteAddress}] failed to create channel: ${error.message}`);
}
return res.end();
},
});
routeListener.post(`/v${API_VERSION}/disconnect`, {
callback: async (req, res, { remoteAddress }) => {
try {
const jsonWebToken = await parseBody(req);
/** @type {{ sessionIdsByChannel: Object<string, number[]> }} */
const claims = await auth.verify(jsonWebToken);
for (const [channelUuid, sessionIds] of Object.entries(
claims.sessionIdsByChannel
)) {
const channel = Channel.records.get(channelUuid);
if (!channel) {
return res.end();
}
if (!channel.remoteAddress === remoteAddress) {
logger.warn(
`[${remoteAddress}] tried to disconnect sessions from channel ${channelUuid} but is not the owner`
);
return res.end();
}
for (const sessionId of sessionIds) {
const session = channel.sessions.get(sessionId);
session?.close(SESSION_CLOSE_CODE.KICKED, {
cause: `/disconnect by ${remoteAddress}`,
});
}
}
res.statusCode = 200;
} catch (error) {
logger.error(`[${remoteAddress}] failed to disconnect session: ${error.message}`);
res.statusCode = 422; // unprocessable entity
}
return res.end();
},
});
httpServer = http.createServer(routeListener.listen);
await new Promise((resolve) => {
httpServer.listen(port, httpInterface, resolve);
});
logger.info(`http listening at ${httpInterface}:${port}`);
await ws.start({ server: httpServer });
}
export function close() {
ws.close();
httpServer?.close();
}
class RouteListener {
/** @type {Map<string, { callback: routeCallback, cors: string, methods: string }>} */
GETs = new Map();
/** @type {Map<string, { callback: routeCallback, cors: string, methods: string }>} */
POSTs = new Map();
/** @type {Map<string, { cors: string, methods: string }>} */
OPTIONs = new Map();
constructor() {
this.listen = this.listen.bind(this);
}
/**
* @param {string} pattern
* @param {{ cors: string, callback: routeCallback }} options
*/
get(pattern, { cors, callback }) {
let methods = "GET";
if (cors) {
methods = "GET, OPTIONS";
this.OPTIONs.set(pattern, { cors, methods });
}
this.GETs.set(pattern, { cors, methods, callback });
}
/**
* @param {string} pattern
* @param {{ cors: string, callback: routeCallback }} options
*/
post(pattern, { cors, callback }) {
let methods = "POST";
if (cors) {
methods = "POST, OPTIONS";
this.OPTIONs.set(pattern, { cors, methods });
}
this.POSTs.set(pattern, { cors, methods, callback });
}
/**
* @param {http.IncomingMessage} req
* @param {http.ServerResponse} res
*/
async listen(req, res) {
const { host, protocol, remoteAddress, pathname, searchParams } = extractRequestInfo(req);
logger.verbose(`${remoteAddress} - ${req.method} - ${req.url}`);
res.statusCode = 404; // Not found
let registeredRoutes;
switch (req.method) {
case "OPTIONS":
registeredRoutes = this.OPTIONs.entries();
break;
case "GET":
registeredRoutes = this.GETs.entries();
break;
case "POST":
registeredRoutes = this.POSTs.entries();
break;
}
for (const [pattern, options] of registeredRoutes) {
if (pathname === pattern) {
if (options?.cors) {
res.setHeader("Access-Control-Allow-Origin", options.cors);
res.setHeader("Access-Control-Allow-Methods", options.methods);
res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
}
if (options?.callback) {
try {
return await options.callback(req, res, {
host,
protocol,
remoteAddress,
searchParams,
});
} catch (error) {
logger.error(
`[${remoteAddress}] ${error.message} when calling ${req.url}: ${error.message}`
);
res.statusCode = 500; // Internal server error
return res.end();
}
}
// if there is no callback, it is a preflight (OPTIONS) request
res.statusCode = 202; // Accepted
break;
}
}
return res.end();
}
}