-
Notifications
You must be signed in to change notification settings - Fork 18
/
background.js
352 lines (301 loc) · 12.6 KB
/
background.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
import "./protobuf.min.js";
import "./license_protocol.js";
import "./forge.min.js";
import { Session } from "./license.js";
import {
DeviceManager,
base64toUint8Array,
uint8ArrayToBase64,
uint8ArrayToHex,
SettingsManager,
AsyncLocalStorage, RemoteCDMManager
} from "./util.js";
import { WidevineDevice } from "./device.js";
import { RemoteCdm } from "./remote_cdm.js";
const { LicenseType, SignedMessage, LicenseRequest, License } = protobuf.roots.default.license_protocol;
let manifests = new Map();
let requests = new Map();
let sessions = new Map();
let logs = [];
chrome.webRequest.onBeforeSendHeaders.addListener(
function(details) {
if (details.method === "GET") {
if (!requests.has(details.url)) {
const headers = details.requestHeaders
.filter(item => !(
item.name.startsWith('sec-ch-ua') ||
item.name.startsWith('Sec-Fetch') ||
item.name.startsWith('Accept-') ||
item.name.startsWith('Host') ||
item.name === "Connection"
)).reduce((acc, item) => {
acc[item.name] = item.value;
return acc;
}, {});
console.log(headers);
requests.set(details.url, headers);
}
}
},
{urls: ["<all_urls>"]},
['requestHeaders', chrome.webRequest.OnSendHeadersOptions.EXTRA_HEADERS].filter(Boolean)
);
async function parseClearKey(body, sendResponse, tab_url) {
const clearkey = JSON.parse(atob(body));
const formatted_keys = clearkey["keys"].map(key => ({
...key,
kid: uint8ArrayToHex(base64toUint8Array(key.kid.replace(/-/g, "+").replace(/_/g, "/") + "==")),
k: uint8ArrayToHex(base64toUint8Array(key.k.replace(/-/g, "+").replace(/_/g, "/") + "=="))
}));
const pssh_data = btoa(JSON.stringify({kids: clearkey["keys"].map(key => key.k)}));
if (logs.filter(log => log.pssh_data === pssh_data).length > 0) {
console.log("[WidevineProxy2]", `KEYS_ALREADY_RETRIEVED: ${pssh_data}`);
sendResponse();
return;
}
console.log("[WidevineProxy2]", "CLEARKEY KEYS", formatted_keys, tab_url);
const log = {
type: "CLEARKEY",
pssh_data: pssh_data,
keys: formatted_keys,
url: tab_url,
timestamp: Math.floor(Date.now() / 1000),
manifests: manifests.has(tab_url) ? manifests.get(tab_url) : []
}
logs.push(log);
await AsyncLocalStorage.setStorage({[pssh_data]: log});
sendResponse();
}
async function generateChallenge(body, sendResponse) {
const signed_message = SignedMessage.decode(base64toUint8Array(body));
const license_request = LicenseRequest.decode(signed_message.msg);
const pssh_data = license_request.contentId.widevinePsshData.psshData[0];
if (!pssh_data) {
console.log("[WidevineProxy2]", "NO_PSSH_DATA_IN_CHALLENGE");
sendResponse(body);
return;
}
if (logs.filter(log => log.pssh_data === Session.psshDataToPsshBoxB64(pssh_data)).length > 0) {
console.log("[WidevineProxy2]", `KEYS_ALREADY_RETRIEVED: ${uint8ArrayToBase64(pssh_data)}`);
sendResponse(body);
return;
}
const selected_device_name = await DeviceManager.getSelectedWidevineDevice();
if (!selected_device_name) {
sendResponse(body);
return;
}
const device_b64 = await DeviceManager.loadWidevineDevice(selected_device_name);
const widevine_device = new WidevineDevice(base64toUint8Array(device_b64).buffer);
const private_key = `-----BEGIN RSA PRIVATE KEY-----${uint8ArrayToBase64(widevine_device.private_key)}-----END RSA PRIVATE KEY-----`;
const session = new Session(
{
privateKey: private_key,
identifierBlob: widevine_device.client_id_bytes
},
pssh_data
);
const [challenge, request_id] = session.createLicenseRequest(LicenseType.STREAMING, widevine_device.type === 2);
sessions.set(uint8ArrayToBase64(request_id), session);
sendResponse(uint8ArrayToBase64(challenge));
}
async function parseLicense(body, sendResponse, tab_url) {
const license = base64toUint8Array(body);
const signed_license_message = SignedMessage.decode(license);
if (signed_license_message.type !== SignedMessage.MessageType.LICENSE) {
console.log("[WidevineProxy2]", "INVALID_MESSAGE_TYPE", signed_license_message.type.toString());
sendResponse();
return;
}
const license_obj = License.decode(signed_license_message.msg);
const loaded_request_id = uint8ArrayToBase64(license_obj.id.requestId);
if (!sessions.has(loaded_request_id)) {
sendResponse();
return;
}
const loadedSession = sessions.get(loaded_request_id);
const keys = await loadedSession.parseLicense(license);
const pssh = loadedSession.getPSSH();
console.log("[WidevineProxy2]", "KEYS", JSON.stringify(keys), tab_url);
const log = {
type: "WIDEVINE",
pssh_data: pssh,
keys: keys,
url: tab_url,
timestamp: Math.floor(Date.now() / 1000),
manifests: manifests.has(tab_url) ? manifests.get(tab_url) : []
}
logs.push(log);
await AsyncLocalStorage.setStorage({[pssh]: log});
sessions.delete(loaded_request_id);
sendResponse();
}
async function generateChallengeRemote(body, sendResponse) {
const signed_message = SignedMessage.decode(base64toUint8Array(body));
const license_request = LicenseRequest.decode(signed_message.msg);
const pssh_data = license_request.contentId.widevinePsshData.psshData[0];
if (!pssh_data) {
console.log("[WidevineProxy2]", "NO_PSSH_DATA_IN_CHALLENGE");
sendResponse(body);
return;
}
const pssh = Session.psshDataToPsshBoxB64(pssh_data);
if (logs.filter(log => log.pssh_data === pssh).length > 0) {
console.log("[WidevineProxy2]", `KEYS_ALREADY_RETRIEVED: ${uint8ArrayToBase64(pssh_data)}`);
sendResponse(body);
return;
}
const selected_remote_cdm_name = await RemoteCDMManager.getSelectedRemoteCDM();
if (!selected_remote_cdm_name) {
sendResponse(body);
return;
}
const selected_remote_cdm = JSON.parse(await RemoteCDMManager.loadRemoteCDM(selected_remote_cdm_name));
const remote_cdm = RemoteCdm.from_object(selected_remote_cdm);
const session_id = await remote_cdm.open();
const challenge_b64 = await remote_cdm.get_license_challenge(session_id, pssh, true);
const signed_challenge_message = SignedMessage.decode(base64toUint8Array(challenge_b64));
const challenge_message = LicenseRequest.decode(signed_challenge_message.msg);
sessions.set(uint8ArrayToBase64(challenge_message.contentId.widevinePsshData.requestId), {
id: session_id,
pssh: pssh
});
sendResponse(challenge_b64);
}
async function parseLicenseRemote(body, sendResponse, tab_url) {
const license = base64toUint8Array(body);
const signed_license_message = SignedMessage.decode(license);
if (signed_license_message.type !== SignedMessage.MessageType.LICENSE) {
console.log("[WidevineProxy2]", "INVALID_MESSAGE_TYPE", signed_license_message.type.toString());
sendResponse();
return;
}
const license_obj = License.decode(signed_license_message.msg);
const loaded_request_id = uint8ArrayToBase64(license_obj.id.requestId);
if (!sessions.has(loaded_request_id)) {
sendResponse();
return;
}
const session_id = sessions.get(loaded_request_id);
const selected_remote_cdm_name = await RemoteCDMManager.getSelectedRemoteCDM();
if (!selected_remote_cdm_name) {
sendResponse();
return;
}
const selected_remote_cdm = JSON.parse(await RemoteCDMManager.loadRemoteCDM(selected_remote_cdm_name));
const remote_cdm = RemoteCdm.from_object(selected_remote_cdm);
await remote_cdm.parse_license(session_id.id, body);
const returned_keys = await remote_cdm.get_keys(session_id.id, "CONTENT");
await remote_cdm.close(session_id.id);
if (returned_keys.length === 0) {
sendResponse();
return;
}
const keys = returned_keys.map(({ key, key_id }) => ({ k: key, kid: key_id }));
console.log("[WidevineProxy2]", "KEYS", JSON.stringify(keys), tab_url);
const log = {
type: "WIDEVINE",
pssh_data: session_id.pssh,
keys: keys,
url: tab_url,
timestamp: Math.floor(Date.now() / 1000),
manifests: manifests.has(tab_url) ? manifests.get(tab_url) : []
}
logs.push(log);
await AsyncLocalStorage.setStorage({[session_id.pssh]: log});
sessions.delete(loaded_request_id);
sendResponse();
}
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
(async () => {
const tab_url = sender.tab ? sender.tab.url : null;
switch (message.type) {
case "REQUEST":
if (!await SettingsManager.getEnabled()) {
sendResponse(message.body);
manifests.clear();
return;
}
try {
JSON.parse(atob(message.body));
sendResponse(message.body);
return;
} catch {
if (message.body) {
const device_type = await SettingsManager.getSelectedDeviceType();
switch (device_type) {
case "WVD":
await generateChallenge(message.body, sendResponse);
break;
case "REMOTE":
await generateChallengeRemote(message.body, sendResponse);
break;
}
}
}
break;
case "RESPONSE":
if (!await SettingsManager.getEnabled()) {
sendResponse(message.body);
manifests.clear();
return;
}
try {
await parseClearKey(message.body, sendResponse, tab_url);
return;
} catch (e) {
const device_type = await SettingsManager.getSelectedDeviceType();
switch (device_type) {
case "WVD":
await parseLicense(message.body, sendResponse, tab_url);
break;
case "REMOTE":
await parseLicenseRemote(message.body, sendResponse, tab_url);
break;
}
return;
}
case "GET_LOGS":
sendResponse(logs);
break;
case "OPEN_PICKER_WVD":
chrome.windows.create({
url: 'picker/wvd/filePicker.html',
type: 'popup',
width: 300,
height: 200,
});
break;
case "OPEN_PICKER_REMOTE":
chrome.windows.create({
url: 'picker/remote/filePicker.html',
type: 'popup',
width: 300,
height: 200,
});
break;
case "CLEAR":
logs = [];
manifests.clear()
break;
case "MANIFEST":
const parsed = JSON.parse(message.body);
const element = {
type: parsed.type,
url: parsed.url,
headers: requests.has(parsed.url) ? requests.get(parsed.url) : [],
};
if (!manifests.has(tab_url)) {
manifests.set(tab_url, [element]);
} else {
let elements = manifests.get(tab_url);
if (!elements.some(e => e.url === parsed.url)) {
elements.push(element);
manifests.set(tab_url, elements);
}
}
sendResponse();
}
})();
return true;
});