From dffd392d4d6a8e14c16ed990a2c3af0043c53eff Mon Sep 17 00:00:00 2001 From: Vitaly Gashkov Date: Sun, 21 Jul 2024 13:48:58 +0500 Subject: [PATCH] feat: log more info about DRM session, change notification layout & text --- manifest.json | 8 ++--- content-script.js => src/content.js | 3 +- eme-logger-mod.js => src/drm.js | 55 ++++++++++++++++------------- background.js => src/worker.js | 12 +++---- 4 files changed, 41 insertions(+), 37 deletions(-) rename content-script.js => src/content.js (89%) rename eme-logger-mod.js => src/drm.js (51%) rename background.js => src/worker.js (95%) diff --git a/manifest.json b/manifest.json index b4b2198..40e9cbe 100644 --- a/manifest.json +++ b/manifest.json @@ -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" } @@ -28,5 +28,5 @@ "webRequestBlocking", "notifications" ], - "web_accessible_resources": ["eme-logger-mod.js"] + "web_accessible_resources": ["src/drm.js"] } diff --git a/content-script.js b/src/content.js similarity index 89% rename from content-script.js rename to src/content.js index d41f41a..d121932 100644 --- a/content-script.js +++ b/src/content.js @@ -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); } @@ -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(); })(); diff --git a/eme-logger-mod.js b/src/drm.js similarity index 51% rename from eme-logger-mod.js rename to src/drm.js index aef18a5..26e8a08 100644 --- a/eme-logger-mod.js +++ b/src/drm.js @@ -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'); @@ -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); }); })(); diff --git a/background.js b/src/worker.js similarity index 95% rename from background.js rename to src/worker.js index 9013a01..64b63a4 100644 --- a/background.js +++ b/src/worker.js @@ -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, }); @@ -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: [], @@ -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); });