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