forked from tomphttp/bare-server-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathV1.js
425 lines (355 loc) · 9.74 KB
/
V1.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
import http from 'node:http';
import https from 'node:https';
import { Response, Headers } from './AbstractMessage.js';
import { mapHeadersFromArray, rawHeaderNames } from './headerUtil.js';
import { decodeProtocol } from './encodeProtocol.js';
import { randomBytes } from 'node:crypto';
import { promisify } from 'node:util';
const valid_protocols = ['http:', 'https:', 'ws:', 'wss:'];
const randomBytesAsync = promisify(randomBytes);
// max of 4 concurrent sockets, rest is queued while busy? set max to 75
const http_agent = http.Agent({
keepAlive: true,
});
const https_agent = https.Agent({
keepAlive: true,
});
async function fetch(server, server_request, request_headers, url) {
const options = {
host: url.host,
port: url.port,
path: url.path,
method: server_request.method,
headers: request_headers,
setHost: false,
localAddress: server.local_address,
};
let outgoing;
if (url.protocol === 'https:') {
outgoing = https.request({ ...options, agent: https_agent });
} else if (url.protocol === 'http:') {
outgoing = http.request({ ...options, agent: http_agent });
} else {
throw new RangeError(`Unsupported protocol: '${url.protocol}'`);
}
server_request.body.pipe(outgoing);
return await new Promise((resolve, reject) => {
outgoing.on('response', resolve);
outgoing.on('error', reject);
});
}
async function upgradeFetch(server, server_request, request_headers, url) {
const options = {
host: url.host,
port: url.port,
path: url.path,
headers: request_headers,
method: server_request.method,
setHost: false,
localAddress: server.local_address,
};
let outgoing;
if (url.protocol === 'wss:') {
outgoing = https.request({ ...options, agent: https_agent });
} else if (url.protocol === 'ws:') {
outgoing = http.request({ ...options, agent: http_agent });
} else {
throw new RangeError(`Unsupported protocol: '${url.protocol}'`);
}
outgoing.end();
return await new Promise((resolve, reject) => {
outgoing.on('response', () => {
reject('Remote upgraded the WebSocket');
});
outgoing.on('upgrade', (...args) => {
resolve(args);
});
outgoing.on('error', error => {
reject(error);
});
});
}
function load_forwarded_headers(forward, target, client_request) {
for (let header of forward) {
if (client_request.headers.has(header)) {
target[header] = client_request.headers.get(header);
}
}
}
function read_headers(server_request) {
const remote = Object.setPrototypeOf({}, null);
const headers = Object.setPrototypeOf({}, null);
for (let remote_prop of ['host', 'port', 'protocol', 'path']) {
const header = `x-bare-${remote_prop}`;
if (server_request.headers.has(header)) {
let value = server_request.headers.get(header);
switch (remote_prop) {
case 'port':
value = parseInt(value);
if (isNaN(value)) {
return {
error: {
code: 'INVALID_BARE_HEADER',
id: `request.headers.${header}`,
message: `Header was not a valid integer.`,
},
};
}
break;
case 'protocol':
if (!valid_protocols.includes(value)) {
return {
error: {
code: 'INVALID_BARE_HEADER',
id: `request.headers.${header}`,
message: `Header was invalid`,
},
};
}
break;
}
remote[remote_prop] = value;
} else {
return {
error: {
code: 'MISSING_BARE_HEADER',
id: `request.headers.${header}`,
message: `Header was not specified.`,
},
};
}
}
if (server_request.headers.has('x-bare-headers')) {
let json;
try {
json = JSON.parse(server_request.headers.get('x-bare-headers'));
for (let header in json) {
if (typeof json[header] !== 'string' && !Array.isArray(json[header])) {
return {
error: {
code: 'INVALID_BARE_HEADER',
id: `bare.headers.${header}`,
message: `Header was not a String or Array.`,
},
};
}
}
} catch (err) {
return {
error: {
code: 'INVALID_BARE_HEADER',
id: `request.headers.${header}`,
message: `Header contained invalid JSON. (${err.message})`,
},
};
}
Object.assign(headers, json);
} else {
return {
error: {
code: 'MISSING_BARE_HEADER',
id: `request.headers.x-bare-headers`,
message: `Header was not specified.`,
},
};
}
if (server_request.headers.has('x-bare-forward-headers')) {
let json;
try {
json = JSON.parse(server_request.headers.get('x-bare-forward-headers'));
} catch (err) {
return {
error: {
code: 'INVALID_BARE_HEADER',
id: `request.headers.x-bare-forward-headers`,
message: `Header contained invalid JSON. (${err.message})`,
},
};
}
load_forwarded_headers(json, headers, server_request);
} else {
return {
error: {
code: 'MISSING_BARE_HEADER',
id: `request.headers.x-bare-forward-headers`,
message: `Header was not specified.`,
},
};
}
return { remote, headers };
}
async function v1(server, server_request) {
const { error, remote, headers } = read_headers(server_request);
if (error) {
// sent by browser, not client
if (server_request.method === 'OPTIONS') {
return new Response(undefined, 200);
} else {
return server.json(400, error);
}
}
let response;
try {
response = await fetch(server, server_request, headers, remote);
} catch (err) {
if (err instanceof Error) {
switch (err.code) {
case 'ENOTFOUND':
return server.json(500, {
code: 'HOST_NOT_FOUND',
id: 'request',
message: 'The specified host could not be resolved.',
});
case 'ECONNREFUSED':
return server.json(500, {
code: 'CONNECTION_REFUSED',
id: 'response',
message: 'The remote rejected the request.',
});
case 'ECONNRESET':
return server.json(500, {
code: 'CONNECTION_RESET',
id: 'response',
message: 'The request was forcibly closed.',
});
case 'ETIMEOUT':
return server.json(500, {
code: 'CONNECTION_TIMEOUT',
id: 'response',
message: 'The response timed out.',
});
}
}
throw err;
}
const response_headers = new Headers();
for (let header in response.headers) {
if (header === 'content-encoding' || header === 'x-content-encoding') {
response_headers.set('content-encoding', response.headers[header]);
} else if (header === 'content-length') {
response_headers.set('content-length', response.headers[header]);
}
}
response_headers.set(
'x-bare-headers',
JSON.stringify(
mapHeadersFromArray(rawHeaderNames(response.rawHeaders), {
...response.headers,
})
)
);
response_headers.set('x-bare-status', response.statusCode);
response_headers.set('x-bare-status-text', response.statusMessage);
return new Response(response, 200, response_headers);
}
// prevent users from specifying id=__proto__ or id=constructor
const temp_meta = Object.setPrototypeOf({}, null);
setInterval(() => {
for (let id in temp_meta) {
if (temp_meta[id].expires < Date.now()) {
delete temp_meta[id];
}
}
}, 1e3);
async function v1wsmeta(server, server_request) {
if (server_request.method === 'OPTIONS') {
return new Response(undefined, 200);
}
if (!server_request.headers.has('x-bare-id')) {
return server.json(400, {
code: 'MISSING_BARE_HEADER',
id: 'request.headers.x-bare-id',
message: 'Header was not specified',
});
}
const id = server_request.headers.get('x-bare-id');
if (!(id in temp_meta)) {
return server.json(400, {
code: 'INVALID_BARE_HEADER',
id: 'request.headers.x-bare-id',
message: 'Unregistered ID',
});
}
const { meta } = temp_meta[id];
if (typeof meta === 'undefined') {
return server.json(200, null);
}
delete temp_meta[id];
return server.json(200, meta);
}
async function v1wsnewmeta(server, server_request) {
const id = (await randomBytesAsync(32)).toString('hex');
temp_meta[id] = {
expires: Date.now() + 30e3,
};
return new Response(Buffer.from(id.toString('hex')));
}
async function v1socket(server, server_request, server_socket, server_head) {
if (!server_request.headers.has('sec-websocket-protocol')) {
server_socket.end();
return;
}
const [first_protocol, data] = server_request.headers
.get('sec-websocket-protocol')
.split(/,\s*/g);
if (first_protocol !== 'bare') {
server_socket.end();
return;
}
const { remote, headers, forward_headers, id } = JSON.parse(
decodeProtocol(data)
);
load_forwarded_headers(forward_headers, headers, server_request);
const [response, socket, head] = await upgradeFetch(
server,
server_request,
headers,
remote
);
if (id in temp_meta) {
const meta = {
headers: mapHeadersFromArray(rawHeaderNames(response.rawHeaders), {
...response.headers,
}),
};
temp_meta[id].meta = meta;
}
const response_headers = [
`HTTP/1.1 101 Switching Protocols`,
`Upgrade: websocket`,
`Connection: Upgrade`,
`Sec-WebSocket-Protocol: bare`,
`Sec-WebSocket-Accept: ${response.headers['sec-websocket-accept']}`,
];
if ('sec-websocket-extensions' in response.headers) {
response_headers.push(
`Sec-WebSocket-Extensions: ${response.headers['sec-websocket-extensions']}`
);
}
server_socket.write(response_headers.concat('', '').join('\r\n'));
server_socket.write(head);
socket.on('close', () => {
// console.log('Remote closed');
server_socket.end();
});
server_socket.on('close', () => {
// console.log('Serving closed');
socket.end();
});
socket.on('error', err => {
server.error('Remote socket error:', err);
server_socket.end();
});
server_socket.on('error', err => {
server.error('Serving socket error:', err);
socket.end();
});
socket.pipe(server_socket);
server_socket.pipe(socket);
}
export default function register(server) {
server.routes.set('/v1/', v1);
server.routes.set('/v1/ws-new-meta', v1wsnewmeta);
server.routes.set('/v1/ws-meta', v1wsmeta);
server.socket_routes.set('/v1/', v1socket);
}