rewrite #2

Merged
tpd94 merged 11 commits from rewrite into main 2025-06-29 16:45:51 +00:00
Showing only changes of commit 388afaa171 - Show all commits

View File

@ -409,8 +409,6 @@ function arrayBufferToBase64(uint8array) {
// Challenge generator interceptor // Challenge generator interceptor
const originalGenerateRequest = MediaKeySession.prototype.generateRequest; const originalGenerateRequest = MediaKeySession.prototype.generateRequest;
MediaKeySession.prototype.generateRequest = function(initDataType, initData) { MediaKeySession.prototype.generateRequest = function(initDataType, initData) {
if (!generateRequestCalled) {
generateRequestCalled = true;
const session = this; const session = this;
let playReadyPssh = getPlayReadyPssh(initData); let playReadyPssh = getPlayReadyPssh(initData);
if (playReadyPssh) { if (playReadyPssh) {
@ -495,8 +493,7 @@ MediaKeySession.prototype.generateRequest = function(initDataType, initData) {
}) })
console.log("Message interceptor mounted."); console.log("Message interceptor mounted.");
} }
return originalGenerateRequest.call(session, initDataType, initData); return originalGenerateRequest.call(session, initDataType, initData);
}
}; };
// Message update interceptors // Message update interceptors
@ -591,7 +588,7 @@ MediaKeySession.prototype.update = function(response) {
if (body instanceof ArrayBuffer || body instanceof Uint8Array) { if (body instanceof ArrayBuffer || body instanceof Uint8Array) {
const buffer = body instanceof Uint8Array ? body : new Uint8Array(body); const buffer = body instanceof Uint8Array ? body : new Uint8Array(body);
const base64Body = window.btoa(String.fromCharCode(...buffer)); const base64Body = window.btoa(String.fromCharCode(...buffer));
if ((base64EncodedBody.startsWith("CAES") || base64EncodedBody.startsWith("PD94")) && (!remoteCDM || remoteCDM.challenge === null || base64Body !== remoteCDM.challenge) && interceptType === "EME") { if ((base64Body.startsWith("CAES") || base64Body.startsWith("PD94")) && (!remoteCDM || remoteCDM.challenge === null || base64Body !== remoteCDM.challenge) && interceptType === "EME") {
foundChallengeInBody = true; foundChallengeInBody = true;
window.postMessage({ type: "__LICENSE_URL__", data: this._url }, "*"); window.postMessage({ type: "__LICENSE_URL__", data: this._url }, "*");
// Block the request // Block the request
@ -601,7 +598,7 @@ MediaKeySession.prototype.update = function(response) {
foundChallengeInBody = true; foundChallengeInBody = true;
window.postMessage({ type: "__LICENSE_URL__", data: this._url }, "*"); window.postMessage({ type: "__LICENSE_URL__", data: this._url }, "*");
if (!remoteCDM) { if (!remoteCDM) {
if (base64EncodedBody.startsWith("CAES")) { if (base64Body.startsWith("CAES")) {
const { const {
device_type, system_id, security_level, host, secret, device_name device_type, system_id, security_level, host, secret, device_name
} = widevineDeviceInfo; } = widevineDeviceInfo;
@ -609,7 +606,7 @@ MediaKeySession.prototype.update = function(response) {
remoteCDM.openSession(); remoteCDM.openSession();
remoteCDM.getChallenge(foundWidevinePssh); remoteCDM.getChallenge(foundWidevinePssh);
} }
if (base64EncodedBody.startsWith("PD94")) { if (base64Body.startsWith("PD94")) {
const { const {
security_level, host, secret, device_name security_level, host, secret, device_name
} = playreadyDeviceInfo; } = playreadyDeviceInfo;
@ -618,12 +615,15 @@ MediaKeySession.prototype.update = function(response) {
remoteCDM.getChallenge(foundPlayreadyPssh); remoteCDM.getChallenge(foundPlayreadyPssh);
} }
} }
const injectedBody = atob(remoteCDM.challenge); if (remoteCDM) {
remoteCDM.getChallenge(foundWidevinePssh);
}
const injectedBody = base64ToUint8Array(remoteCDM.challenge);
return originalSend.call(this, injectedBody); return originalSend.call(this, injectedBody);
} }
} }
if (typeof body === 'string') { if (typeof body === 'string' && !isJson(body)) {
const base64EncodedBody = btoa(body); const base64EncodedBody = btoa(body);
if ((base64EncodedBody.startsWith("CAES") || base64EncodedBody.startsWith("PD94")) && (!remoteCDM || remoteCDM.challenge === null || base64EncodedBody !== remoteCDM.challenge) && interceptType === "EME") { if ((base64EncodedBody.startsWith("CAES") || base64EncodedBody.startsWith("PD94")) && (!remoteCDM || remoteCDM.challenge === null || base64EncodedBody !== remoteCDM.challenge) && interceptType === "EME") {
foundChallengeInBody = true; foundChallengeInBody = true;
@ -656,15 +656,6 @@ MediaKeySession.prototype.update = function(response) {
return originalSend.call(this, injectedBody); return originalSend.call(this, injectedBody);
} }
} }
if (isJson(body)) {
if (jsonContainsValue(body) && !jsonContainsValue(body, remoteCDM.challenge)) {
foundChallengeInBody = true;
window.postMessage({ type: "__LICENSE_URL__", data: this._url }, "*");
return;
}
}
} }
} }
return originalSend.apply(this, arguments); return originalSend.apply(this, arguments);