consolidate DRM interception handling for fetch and XHR requests

This commit is contained in:
voldemort 2025-07-22 10:04:17 +07:00
parent a40a6abaf7
commit 8d4cd89a02

View File

@ -788,30 +788,17 @@ function handleLicenseMode({
} }
} }
// fetch POST interceptor function handleDRMInterception(drmInfo, body, url, setBodyCallback, continueRequestCallback) {
(function () {
const originalFetch = window.fetch;
window.fetch = async function (resource, config = {}) {
const method = (config.method || "GET").toUpperCase();
if (method === "POST") {
let body = config.body;
if (body) {
const drmInfo = detectDRMChallenge(body);
// EME mode: block the request if a DRM challenge is detected // EME mode: block the request if a DRM challenge is detected
if ( if (
drmInfo.type && drmInfo.type &&
(!remoteCDM || (!remoteCDM || remoteCDM.challenge === null || drmInfo.base64 !== remoteCDM.challenge) &&
remoteCDM.challenge === null ||
drmInfo.base64 !== remoteCDM.challenge) &&
interceptType === "EME" interceptType === "EME"
) { ) {
foundChallengeInBody = true; foundChallengeInBody = true;
window.postMessage({ type: "__LICENSE_URL__", data: resource }, "*"); window.postMessage({ type: "__LICENSE_URL__", data: url }, "*");
// Block the request // Block the request
return; return { shouldBlock: true };
} }
// LICENSE mode: replace the challenge in the request // LICENSE mode: replace the challenge in the request
@ -819,18 +806,41 @@ function handleLicenseMode({
handleLicenseMode({ handleLicenseMode({
drmInfo, drmInfo,
body, body,
setBody: (b) => { setBody: setBodyCallback,
config.body = b; urlOrResource: url,
},
urlOrResource: resource,
getWidevinePssh: () => foundWidevinePssh, getWidevinePssh: () => foundWidevinePssh,
getPlayreadyPssh: () => foundPlayreadyPssh, getPlayreadyPssh: () => foundPlayreadyPssh,
widevineDeviceInfo, widevineDeviceInfo,
playreadyDeviceInfo, playreadyDeviceInfo,
}); });
return originalFetch(resource, config); return { shouldIntercept: true, result: continueRequestCallback() };
} }
return { shouldContinue: true };
} }
// fetch POST interceptor
(function () {
const originalFetch = window.fetch;
window.fetch = async function (resource, config = {}) {
const method = (config.method || "GET").toUpperCase();
if (method === "POST" && config.body) {
const drmInfo = detectDRMChallenge(config.body);
const result = handleDRMInterception(
drmInfo,
config.body,
resource,
(b) => {
config.body = b;
},
() => originalFetch(resource, config)
);
if (result.shouldBlock) return;
if (result.shouldIntercept) return result.result;
} }
return originalFetch(resource, config); return originalFetch(resource, config);
@ -849,39 +859,21 @@ function handleLicenseMode({
}; };
XMLHttpRequest.prototype.send = function (body) { XMLHttpRequest.prototype.send = function (body) {
if (this._method && this._method.toUpperCase() === "POST") { if (this._method && this._method.toUpperCase() === "POST" && body) {
if (body) {
const drmInfo = detectDRMChallenge(body); const drmInfo = detectDRMChallenge(body);
// EME mode: block the request if a DRM challenge is detected const result = handleDRMInterception(
if (
drmInfo.type &&
(!remoteCDM ||
remoteCDM.challenge === null ||
drmInfo.base64 !== remoteCDM.challenge) &&
interceptType === "EME"
) {
foundChallengeInBody = true;
window.postMessage({ type: "__LICENSE_URL__", data: this._url }, "*");
// Block the request
return;
}
// LICENSE mode: replace the challenge in the request
if (drmInfo.type && interceptType === "LICENSE" && !foundChallengeInBody) {
return handleLicenseMode({
drmInfo, drmInfo,
body, body,
setBody: (b) => originalSend.call(this, b), this._url,
urlOrResource: this._url, (b) => originalSend.call(this, b),
getWidevinePssh: () => foundWidevinePssh, () => {} // XHR doesn't need continuation callback
getPlayreadyPssh: () => foundPlayreadyPssh, );
widevineDeviceInfo,
playreadyDeviceInfo, if (result.shouldBlock) return;
}); if (result.shouldIntercept) return result.result;
}
}
} }
return originalSend.apply(this, arguments); return originalSend.apply(this, arguments);
}; };
})(); })();