-
Notifications
You must be signed in to change notification settings - Fork 1
/
replay.js
73 lines (63 loc) · 2.2 KB
/
replay.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
/**
* Middleware for replaying requests from an HLS session
*/
let M3U8_MIME_TYPE = require('./mime-types').M3U8_MIME_TYPE;
var Decrypter = require('aes-decrypter').Decrypter;
/**
* Expects an `:index` parameter on the request object and an HLS
* session to work from as a local on the response object.
*/
function replay(request, response) {
var content, activeHlsSession;
activeHlsSession = response.locals.activeHlsSession;
if (!activeHlsSession ||
request.params.index < 0 ||
request.params.index >= activeHlsSession.length) {
return response.sendStatus(400);
}
content = activeHlsSession[request.params.index].response.content;
response.status(200);
if (content.encoding === 'base64') {
if (M3U8_MIME_TYPE.test(content.mimeType)) {
response.set('Content-Type', 'text/plain');
} else {
response.set('Content-Type', content.mimeType);
}
return response.send(new Buffer(content.text, 'base64'));
}
response.set('Content-Type', activeHlsSession[request.params.index].response.contentType);
if (activeHlsSession[request.params.index].response.contentType === 'application/octet-stream') {
return response.send(new Buffer(content.text, 'binary'));
}
response.send(content.text);
}
function decrypt(request, response, next) {
var entry, activeHlsSession;
console.log('decrypting:', request.url);
activeHlsSession = response.locals.activeHlsSession;
if (!activeHlsSession ||
request.params.index < 0 ||
request.params.index >= activeHlsSession.length) {
return response.sendStatus(400);
}
entry = activeHlsSession[request.params.index];
try {
new Decrypter(new Uint8Array(new Buffer(entry.response.content.text, 'base64').buffer),
entry.key,
entry.iv,
function(error, decrypted) {
if (error) {
return response.sendStatus(400);
}
response.set('Content-Type', 'video/mp2t');
return response.send(Buffer.from(decrypted.buffer));
});
} catch(e) {
response.status(400);
response.send(e);
}
}
module.exports = {
replay: replay,
decrypt: decrypt
};