Clearkey support

This commit is contained in:
FoxRefire 2024-05-08 04:43:06 +09:00
parent a5a33909ec
commit 3a33aded5e
5 changed files with 53 additions and 4 deletions

View File

@ -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;
}
}
);

View File

@ -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) {

View File

@ -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;
}
};

View File

@ -51,6 +51,11 @@
</form>
</div>
<div id="ckHome" class="hidden">
<h3>Clearkey detected</h3>
<label for="ckResult" disabled>Result:</label><br>
<textarea id="ckResult" rows="10" cols="50"></textarea>
</div>
<div id="selectPssh" class="hidden">
<input type="text" id="psshSearch" placeholder="Search">
<ul id="psshList"></ul>

View File

@ -1,6 +1,7 @@
let psshs=chrome.extension.getBackgroundPage().psshs;
let requests=chrome.extension.getBackgroundPage().requests;
let pageURL=chrome.extension.getBackgroundPage().pageURL;
let clearkey=chrome.extension.getBackgroundPage().clearkey;
async function guess(){
//Be patient!
@ -81,4 +82,8 @@ if(psshs.length!=0){
drawList(requests.map(r => r['url']),'requestSearch','requestList','license');
autoSelect();
});
} else if(clearkey) {
document.getElementById('noEME').style.display='none';
document.getElementById('ckHome').style.display='block';
document.getElementById('ckResult').value=clearkey;
}