From 3a33aded5efea61d58911af79202109770f722f1 Mon Sep 17 00:00:00 2001 From: FoxRefire <155989196+FoxRefire@users.noreply.github.com> Date: Wed, 8 May 2024 04:43:06 +0900 Subject: [PATCH] Clearkey support --- background.js | 6 ++++++ content.js | 10 +++++++++- inject.js | 31 ++++++++++++++++++++++++++++--- popup.html | 5 +++++ popup.js | 5 +++++ 5 files changed, 53 insertions(+), 4 deletions(-) diff --git a/background.js b/background.js index ec361d0..7d33914 100644 --- a/background.js +++ b/background.js @@ -2,6 +2,7 @@ window.psshs=[]; window.requests=[]; window.bodys=[]; window.pageURL=""; +window.clearkey=""; function convertHeaders(obj){ return JSON.stringify(Object.fromEntries(obj.map(header => [header.name, header.value]))) } @@ -43,11 +44,16 @@ chrome.runtime.onMessage.addListener( window.psshs=[]; window.requests=[]; window.bodys=[]; + window.clearkey=null; break; case "PSSH": window.psshs.push(request.text) window.pageURL=request.pageURL break; + case "CLEARKEY": + window.clearkey=request.text + window.pageURL=request.pageURL + break; } } ); diff --git a/content.js b/content.js index 720c8c9..c6586ec 100644 --- a/content.js +++ b/content.js @@ -10,7 +10,6 @@ chrome.runtime.sendMessage({type: "RESET"},null); //Send PSSH into background.js document.addEventListener('pssh', (e) => { - console.log("[PSSH]"+e.detail); chrome.runtime.sendMessage({ type: "PSSH", text: e.detail, @@ -18,6 +17,15 @@ document.addEventListener('pssh', (e) => { },null); }); +//Send Clearkey into background.js +document.addEventListener('clearkey', (e) => { + chrome.runtime.sendMessage({ + type: "CLEARKEY", + text: e.detail, + pageURL: document.URL + },null); +}); + //Fetch from original origin chrome.runtime.onMessage.addListener( function (request, sender, sendResponse) { diff --git a/inject.js b/inject.js index 4f8127d..c798e36 100644 --- a/inject.js +++ b/inject.js @@ -3,19 +3,30 @@ const fromHexString = hexString => Uint8Array.from(hexString.match(/.{1,2}/g).ma const toHexString = bytes => bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), ''); -// Refactored getPssh function +const b64ToHex = b64 => [...atob(b64)].map(c=> c.charCodeAt(0).toString(16).padStart(2,0)).join`` + +// initData to PSSH function getPssh(buffer) { const bytes = fromHexString(toHexString(new Uint8Array(buffer)).match(/000000..70737368.*/)[0]); return window.btoa(String.fromCharCode(...bytes)); } -// Refactored MediaKeySession.prototype.generateRequest -const originalGenerateRequest = MediaKeySession.prototype.generateRequest; +// Get Clearkey keys +function getClearkey(response) { + let obj=JSON.parse((new TextDecoder("utf-8")).decode(response)) + obj = obj["keys"].map(o => [o["kid"], o["k"]]); + obj = obj.map(o => o.map(a => a.replace(/-/g, '+').replace(/_/g, '/')+"==")) + return obj.map(o => `${b64ToHex(o[0])}:${b64ToHex(o[1])}`).join("\n") +} + +// Widevine PSSH extraction from init +const originalGenerateRequest = MediaKeySession.prototype.generateRequest; MediaKeySession.prototype.generateRequest = function(initDataType, initData) { const result = originalGenerateRequest.call(this, initDataType, initData); //Get PSSH and pass into content.js try { + console.log("[PSSH] " + getPssh(initData)) document.dispatchEvent(new CustomEvent('pssh', { detail: getPssh(initData) })); @@ -23,3 +34,17 @@ MediaKeySession.prototype.generateRequest = function(initDataType, initData) { return result; } }; + +//Clearkey Support +const originalUpdate = MediaKeySession.prototype.update; +MediaKeySession.prototype.update = function(response) { + const result = originalUpdate.call(this, response); + try { + console.log("[CLEARKEY] " + getClearkey(response)); + document.dispatchEvent(new CustomEvent('clearkey', { + detail: getClearkey(response) + })); + } finally { + return result; + } +}; diff --git a/popup.html b/popup.html index 8cb7e7e..2e9fca1 100644 --- a/popup.html +++ b/popup.html @@ -51,6 +51,11 @@ +