forked from truedread/netflix-1080p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsl_client.js
executable file
·435 lines (365 loc) · 12 KB
/
msl_client.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
431
432
433
434
435
function arrayBufferToBase64(buffer) {
var binary = "";
var bytes = new Uint8Array( buffer );
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return btoa(binary);
}
function base64ToArrayBuffer(b64) {
var byteString = atob(b64);
var byteArray = new Uint8Array(byteString.length);
for(var i=0; i < byteString.length; i++) {
byteArray[i] = byteString.charCodeAt(i);
}
return byteArray;
}
function arrayBufferToString(buffer){
var arr = new Uint8Array(buffer);
var str = String.fromCharCode.apply(String, arr);
if(/[\u0080-\uffff]/.test(str)){
throw new Error("this string seems to contain (still encoded) multibytes");
}
return str;
}
function padBase64(b64) {
var l = b64.length % 4;
if (l == 2) {
b64 += "==";
} else if (l == 3) {
b64 += "=";
}
return b64.replace(/-/g, "+").replace(/_/g, "/");
}
function generateEsn() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
for (var i = 0; i < 30; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
var manifestUrl = "https://www.netflix.com/nq/msl_v1/cadmium/pbo_manifests/^1.0.0/router";
var licenseUrl = "https://www.netflix.com/nq/msl_v1/cadmium/pbo_licenses/^1.0.0/router";
var shaktiMetadataUrl = "https://www.netflix.com/api/shakti/d7cab521/metadata?movieid=";
var defaultEsn = "NFCDIE-04-" + generateEsn();
var profiles = [
"playready-h264mpl30-dash",
"playready-h264mpl31-dash",
"playready-h264mpl40-dash",
"heaac-2-dash",
"simplesdh",
"nflx-cmisc",
"BIF240",
"BIF320"
];
if(use6Channels)
profiles.push("heaac-5.1-dash");
var messageid = Math.floor(Math.random() * 2**52);
var header = {
"sender": defaultEsn,
"renewable": true,
"capabilities": {
"languages": ["en-US"],
"compressionalgos": [""]
},
"messageid": messageid,
};
async function getViewableId(viewableIdPath) {
console.log("Getting video metadata for ID " + viewableIdPath);
var apiResp = await fetch(
shaktiMetadataUrl + viewableIdPath,
{
credentials: "same-origin",
method: "GET"
}
);
var apiJson = await apiResp.json();
console.log("Metadata response:");
console.log(apiJson);
var viewableId = apiJson.video.currentEpisode;
if (!viewableId) {
viewableId = parseInt(viewableIdPath);
}
return viewableId;
}
async function performKeyExchange() {
delete header.userauthdata;
var keyPair = await window.crypto.subtle.generateKey(
{
name: "RSA-OAEP",
modulusLength: 2048,
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
hash: {name: "SHA-1"},
},
false,
["encrypt", "decrypt"]
);
var publicKey = await window.crypto.subtle.exportKey(
"spki",
keyPair.publicKey
);
header.keyrequestdata = [
{
"scheme": "ASYMMETRIC_WRAPPED",
"keydata": {
"publickey": arrayBufferToBase64(publicKey),
"mechanism": "JWK_RSA",
"keypairid": "rsaKeypairId"
}
}
];
var headerenvelope = {
"entityauthdata": {
"scheme": "NONE",
"authdata": {
"identity": defaultEsn,
}
},
"signature": "",
};
headerenvelope.headerdata = btoa(JSON.stringify(header));
var payload = {
"signature": ""
};
payload.payload = btoa(JSON.stringify({
"sequencenumber": 1,
"messageid": messageid,
"endofmsg": true,
"data": ""
}));
var request = JSON.stringify(headerenvelope) + JSON.stringify(payload);
var handshakeResp = await fetch(
manifestUrl,
{
body: request,
method: "POST"
}
);
var handshakeJson = await handshakeResp.json();
if (!handshakeJson.headerdata) {
console.error(JSON.parse(atob(handshakeJson.errordata)));
throw("Error parsing key exchange response");
}
var headerdata = JSON.parse(atob(handshakeJson.headerdata));
var encryptionKeyData = await window.crypto.subtle.decrypt(
{
name: "RSA-OAEP",
},
keyPair.privateKey,
base64ToArrayBuffer(headerdata.keyresponsedata.keydata.encryptionkey)
);
encryptionKeyData = JSON.parse(arrayBufferToString(encryptionKeyData));
var signKeyData = await window.crypto.subtle.decrypt(
{
name: "RSA-OAEP",
},
keyPair.privateKey,
base64ToArrayBuffer(headerdata.keyresponsedata.keydata.hmackey)
);
signKeyData = JSON.parse(arrayBufferToString(signKeyData));
return {
"headerdata": headerdata,
"encryptionKeyData": encryptionKeyData,
"signKeyData": signKeyData
};
}
async function generateMslRequestData(data) {
var iv = window.crypto.getRandomValues(new Uint8Array(16));
var aesCbc = new aesjs.ModeOfOperation.cbc(
base64ToArrayBuffer(padBase64(encryptionKeyData.k)),
iv
);
var textBytes = aesjs.utils.utf8.toBytes(JSON.stringify(header));
var encrypted = aesCbc.encrypt(aesjs.padding.pkcs7.pad(textBytes));
var encryptionEnvelope = {
"keyid": defaultEsn + "_" + sequenceNumber,
"sha256": "AA==",
"iv": arrayBufferToBase64(iv),
"ciphertext": arrayBufferToBase64(encrypted)
};
var shaObj = new jsSHA("SHA-256", "TEXT");
shaObj.setHMACKey(padBase64(signKeyData.k), "B64");
shaObj.update(JSON.stringify(encryptionEnvelope));
var signature = shaObj.getHMAC("B64");
var encryptedHeader = {
"signature": signature,
"mastertoken": mastertoken
};
encryptedHeader.headerdata = btoa(JSON.stringify(encryptionEnvelope));
var firstPayload = {
"messageid": messageid,
"data": btoa(JSON.stringify(data)),
"sequencenumber": 1,
"endofmsg": true
};
iv = window.crypto.getRandomValues(new Uint8Array(16));
aesCbc = new aesjs.ModeOfOperation.cbc(
base64ToArrayBuffer(padBase64(encryptionKeyData.k)),
iv
);
textBytes = aesjs.utils.utf8.toBytes(JSON.stringify(firstPayload));
encrypted = aesCbc.encrypt(aesjs.padding.pkcs7.pad(textBytes));
encryptionEnvelope = {
"keyid": defaultEsn + "_" + sequenceNumber,
"sha256": "AA==",
"iv": arrayBufferToBase64(iv),
"ciphertext": arrayBufferToBase64(encrypted)
};
shaObj = new jsSHA("SHA-256", "TEXT");
shaObj.setHMACKey(padBase64(signKeyData.k), "B64");
shaObj.update(JSON.stringify(encryptionEnvelope));
signature = shaObj.getHMAC("B64");
var firstPayloadChunk = {
"signature": signature,
"payload": btoa(JSON.stringify(encryptionEnvelope))
};
return JSON.stringify(encryptedHeader) + JSON.stringify(firstPayloadChunk);
}
async function decryptMslResponse(data) {
try {
JSON.parse(data);
console.error(JSON.parse(atob(JSON.parse(data).errordata)));
throw("Error parsing data");
} catch (e) {}
var pattern = /,"signature":"[0-9A-Za-z/+=]+"}/;
var payloadsSplit = data.split("}}")[1].split(pattern);
payloadsSplit.pop();
var payloadChunks = [];
for (var i = 0; i < payloadsSplit.length; i++) {
payloadChunks.push(payloadsSplit[i] + "}");
}
var chunks = "";
for (i = 0; i < payloadChunks.length; i++) {
var payloadchunk = JSON.parse(payloadChunks[i]);
encryptionEnvelope = atob(payloadchunk.payload);
aesCbc = new aesjs.ModeOfOperation.cbc(
base64ToArrayBuffer(padBase64(encryptionKeyData.k)),
base64ToArrayBuffer(JSON.parse(encryptionEnvelope).iv)
);
var ciphertext = base64ToArrayBuffer(
JSON.parse(encryptionEnvelope).ciphertext
);
var plaintext = JSON.parse(
arrayBufferToString(
aesjs.padding.pkcs7.strip(
aesCbc.decrypt(ciphertext)
)
)
);
chunks += atob(plaintext.data);
}
var decrypted = JSON.parse(chunks);
if (!decrypted.result) {
console.error(decrypted);
throw("Error parsing decrypted data");
}
return decrypted.result;
}
async function getManifest(esn=defaultEsn) {
defaultEsn = esn;
console.log("Performing key exchange");
keyExchangeData = await performKeyExchange();
console.log("Key exchange data:");
console.log(keyExchangeData);
headerdata = keyExchangeData.headerdata;
encryptionKeyData = keyExchangeData.encryptionKeyData;
signKeyData = keyExchangeData.signKeyData;
mastertoken = headerdata.keyresponsedata.mastertoken;
sequenceNumber = JSON.parse(atob(mastertoken.tokendata)).sequencenumber;
viewableIdPath = window.location.pathname.substring(7, 15);
viewableId = await getViewableId(viewableIdPath);
localeId = "en-US";
try {
localeId = netflix.appContext.state.model.models.memberContext.data.geo.locale.id;
} catch (e) {}
var manifestRequestData = {
"version": 2,
"url": "/manifest",
"id": Date.now(),
"esn": defaultEsn,
"languages": [localeId],
"uiVersion": "shakti-v4bf615c3",
"clientVersion": "6.0015.328.011",
"params": {
"type": "standard",
"viewableId": viewableId,
"profiles": profiles,
"flavor": "STANDARD",
"drmType": "widevine",
"drmVersion": 25,
"usePsshBox": true,
"isBranching": false,
"useHttpsStreams": true,
"imageSubtitleHeight": 720,
"uiVersion": "shakti-v4bf615c3",
"clientVersion": "6.0015.328.011",
"supportsPreReleasePin": true,
"supportsWatermark": true,
"showAllSubDubTracks": false,
"videoOutputInfo": [
{
"type": "DigitalVideoOutputDescriptor",
"outputType": "unknown",
"supportedHdcpVersions": ['1.4'],
"isHdcpEngaged": true
}
],
"preferAssistiveAudio": false,
"isNonMember": false
}
};
header.userauthdata = {
"scheme": "NETFLIXID",
"authdata": {}
};
var encryptedManifestRequest = await generateMslRequestData(manifestRequestData);
var manifestResp = await fetch(
manifestUrl,
{
body: encryptedManifestRequest,
credentials: "same-origin",
method: "POST",
headers: {"Content-Type": "application/json"}
}
);
manifestResp = await manifestResp.text();
var manifest = await decryptMslResponse(manifestResp);
console.log("Manifest:");
console.log(manifest);
licensePath = manifest.links.license.href;
return manifest;
}
async function getLicense(challenge, sessionId) {
licenseRequestData = {
"version": 2,
"url": licensePath,
"id": Date.now(),
"esn": defaultEsn,
"languages": [localeId],
"uiVersion": "shakti-v4bf615c3",
"clientVersion": "6.0015.328.011",
"params": [{
"sessionId": sessionId,
"clientTime": Math.floor(Date.now() / 1000),
"challengeBase64": challenge,
"xid": Math.floor((Math.floor(Date.now() / 1000) + 0.1612) * 1000)
}],
"echo": "sessionId"
};
var encryptedLicenseRequest = await generateMslRequestData(licenseRequestData);
var licenseResp = await fetch(
licenseUrl,
{
body: encryptedLicenseRequest,
credentials: "same-origin",
method: "POST",
headers: {"Content-Type": "application/json"}
}
);
licenseResp = await licenseResp.text();
var license = await decryptMslResponse(licenseResp);
console.log("License:");
console.log(license);
return license;
}