Skip to content

Commit

Permalink
feat: log more info about DRM session, change notification layout & text
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalygashkov committed Jul 21, 2024
1 parent 4374d16 commit dffd392
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 37 deletions.
8 changes: 4 additions & 4 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "Streamyx",
"version": "0.1.0",
"version": "0.2.0",
"manifest_version": 2,
"background": {
"scripts": ["background.js"]
"scripts": ["src/worker.js"]
},
"content_scripts": [
{
"matches": ["*://*/*"],
"js": ["content-script.js"],
"js": ["src/content.js"],
"all_frames": true,
"run_at": "document_start"
}
Expand All @@ -28,5 +28,5 @@
"webRequestBlocking",
"notifications"
],
"web_accessible_resources": ["eme-logger-mod.js"]
"web_accessible_resources": ["src/drm.js"]
}
3 changes: 1 addition & 2 deletions content-script.js → src/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
'message',
(event) => {
if (event.source != window) return;

if (event.data.type && event.data.type === '38405bbb-36ef-454d-8b32-346f9564c978') {
if (event.data.log) chrome.runtime.sendMessage(event.data.log);
}
Expand All @@ -17,7 +16,7 @@
script.type = 'text/javascript';
script.defer = false;
script.async = false;
script.src = chrome.extension.getURL('/eme-logger-mod.js');
script.src = chrome.extension.getURL('src/drm.js');
(document.head || document.documentElement).appendChild(script);
script.remove();
})();
55 changes: 31 additions & 24 deletions eme-logger-mod.js → src/drm.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,6 @@
encode: (b) => btoa(String.fromCharCode(...new Uint8Array(b))),
};

const fnproxy = (object, func) => new Proxy(object, { apply: func });

const proxy = (object, key, func) =>
Object.defineProperty(object, key, {
value: fnproxy(object[key], func),
});

proxy(MediaKeySession.prototype, 'generateRequest', async (_target, _this, _args) => {
const [initDataType, initData] = _args;
const pssh = b64.encode(initData);
console.groupCollapsed(`PSSH: ${pssh}`);
console.trace();
console.groupEnd();
if (pssh) window.postMessage({ type: '38405bbb-36ef-454d-8b32-346f9564c978', log: { pssh } }, '*');
return _target.apply(_this, _args);
});

function arrayBufferToHex(arrayBuffer) {
if (typeof arrayBuffer !== 'object' || arrayBuffer === null || typeof arrayBuffer.byteLength !== 'number') {
throw new TypeError('Expected input to be an ArrayBuffer');
Expand All @@ -35,15 +18,39 @@
return result;
}

const fnproxy = (object, func) => new Proxy(object, { apply: func });

const proxy = (object, key, func) => Object.defineProperty(object, key, { value: fnproxy(object[key], func) });

const POST_MESSAGE_TYPE = '38405bbb-36ef-454d-8b32-346f9564c978';

proxy(MediaKeySession.prototype, 'generateRequest', async (_target, _this, _args) => {
const [initDataType, initData] = _args;
const pssh = b64.encode(initData);
_this.initDataType = initDataType;
_this.initData = pssh;
if (pssh) window.postMessage({ type: POST_MESSAGE_TYPE, log: { pssh } }, '*');
return _target.apply(_this, _args);
});

proxy(MediaKeySession.prototype, 'update', async (_target, _this, _args) => {
setTimeout(() => {
const statuses = Array.from(_this.keyStatuses);
for (const [kidBuffer, state] of statuses) {
const kid = arrayBufferToHex(kidBuffer);
if (kid) window.postMessage({ type: '38405bbb-36ef-454d-8b32-346f9564c978', log: { kid } }, '*');
console.log(`KID:STATE -> ${kid}:${state}`);
setTimeout(async () => {
const [message] = _args;
_this.keyIds = [];
const keyStatuses = Array.from(_this.keyStatuses);
for (const [kid] of keyStatuses) _this.keyIds.push(arrayBufferToHex(kid));

console.groupCollapsed(`[STREAMYX] Session ${_this.sessionId} update`);
console.log(`Initialization Data Type: ${_this.initDataType}`);
console.log(`Initialization Data (PSSH): ${_this.initData}`);
console.log(`Key IDs: ${_this.keyIds}`);
console.log(`Message: ${b64.encode(message)}`);
console.groupEnd();

for (const kid of _this.keyIds) {
window.postMessage({ type: POST_MESSAGE_TYPE, log: { kid } }, '*');
}
}, 3000);
}, 2000);
return _target.apply(_this, _args);
});
})();
12 changes: 5 additions & 7 deletions background.js → src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ function requestToClipboard(tabId) {

const notificationId = `${kid || widevine_pssh}`;
const actions =
os === 'win' ? [{ title: 'PowerShell' }, { title: 'Command Prompt' }] : [{ title: 'Bash, zsh, etc.' }];
os === 'win'
? [{ title: 'Copy command for PowerShell' }, { title: 'Copy command for Command Prompt' }]
: [{ title: 'Copy command' }];
chrome.notifications.create(notificationId, {
type: 'basic',
title: `KID -> ${kid}`,
message: `Server: ${
new URL(lic_url).host
}\n\nCopy Streamyx command for your shell by clicking the button below`,
title: `Streamyx: ${new URL(lic_url).host}`,
message: `Key ID: ${kid}`,
iconUrl: 'icon128.png',
buttons: actions,
});
Expand Down Expand Up @@ -198,7 +198,6 @@ chrome.webRequest.onBeforeSendHeaders.addListener(

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (!request || !sender.tab) return;
console.log({ request, sender });
tabIDs[sender.tab.id] = tabIDs[sender.tab.id] || {
license_data: '',
license_request: [],
Expand All @@ -207,6 +206,5 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
};
if (request.pssh) tabIDs[sender.tab.id].pssh = request.pssh;
if (request.kid) tabIDs[sender.tab.id].kid = request.kid;
console.log(`PSSH: ${tabIDs[sender.tab.id].pssh}`);
requestToClipboard(sender.tab.id);
});

0 comments on commit dffd392

Please sign in to comment.