forked from tpd94/CDRM-Extension
consolidate DRM interception handling for fetch and XHR requests
This commit is contained in:
parent
a40a6abaf7
commit
8d4cd89a02
@ -788,30 +788,17 @@ function handleLicenseMode({
|
||||
}
|
||||
}
|
||||
|
||||
// fetch POST interceptor
|
||||
(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);
|
||||
|
||||
function handleDRMInterception(drmInfo, body, url, setBodyCallback, continueRequestCallback) {
|
||||
// EME mode: block the request if a DRM challenge is detected
|
||||
if (
|
||||
drmInfo.type &&
|
||||
(!remoteCDM ||
|
||||
remoteCDM.challenge === null ||
|
||||
drmInfo.base64 !== remoteCDM.challenge) &&
|
||||
(!remoteCDM || remoteCDM.challenge === null || drmInfo.base64 !== remoteCDM.challenge) &&
|
||||
interceptType === "EME"
|
||||
) {
|
||||
foundChallengeInBody = true;
|
||||
window.postMessage({ type: "__LICENSE_URL__", data: resource }, "*");
|
||||
window.postMessage({ type: "__LICENSE_URL__", data: url }, "*");
|
||||
// Block the request
|
||||
return;
|
||||
return { shouldBlock: true };
|
||||
}
|
||||
|
||||
// LICENSE mode: replace the challenge in the request
|
||||
@ -819,18 +806,41 @@ function handleLicenseMode({
|
||||
handleLicenseMode({
|
||||
drmInfo,
|
||||
body,
|
||||
setBody: (b) => {
|
||||
config.body = b;
|
||||
},
|
||||
urlOrResource: resource,
|
||||
setBody: setBodyCallback,
|
||||
urlOrResource: url,
|
||||
getWidevinePssh: () => foundWidevinePssh,
|
||||
getPlayreadyPssh: () => foundPlayreadyPssh,
|
||||
widevineDeviceInfo,
|
||||
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);
|
||||
@ -849,39 +859,21 @@ function handleLicenseMode({
|
||||
};
|
||||
|
||||
XMLHttpRequest.prototype.send = function (body) {
|
||||
if (this._method && this._method.toUpperCase() === "POST") {
|
||||
if (body) {
|
||||
if (this._method && this._method.toUpperCase() === "POST" && body) {
|
||||
const drmInfo = detectDRMChallenge(body);
|
||||
|
||||
// EME mode: block the request if a DRM challenge is detected
|
||||
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({
|
||||
const result = handleDRMInterception(
|
||||
drmInfo,
|
||||
body,
|
||||
setBody: (b) => originalSend.call(this, b),
|
||||
urlOrResource: this._url,
|
||||
getWidevinePssh: () => foundWidevinePssh,
|
||||
getPlayreadyPssh: () => foundPlayreadyPssh,
|
||||
widevineDeviceInfo,
|
||||
playreadyDeviceInfo,
|
||||
});
|
||||
}
|
||||
}
|
||||
this._url,
|
||||
(b) => originalSend.call(this, b),
|
||||
() => {} // XHR doesn't need continuation callback
|
||||
);
|
||||
|
||||
if (result.shouldBlock) return;
|
||||
if (result.shouldIntercept) return result.result;
|
||||
}
|
||||
|
||||
return originalSend.apply(this, arguments);
|
||||
};
|
||||
})();
|
||||
|
Loading…
x
Reference in New Issue
Block a user