Skip to content

Instantly share code, notes, and snippets.

@Ap0dexMe0
Last active October 11, 2024 18:47
Show Gist options
  • Select an option

  • Save Ap0dexMe0/2d39ee61ca09ee22b4e5b5085696fe20 to your computer and use it in GitHub Desktop.

Select an option

Save Ap0dexMe0/2d39ee61ca09ee22b4e5b5085696fe20 to your computer and use it in GitHub Desktop.
Here's my sample snippet to get you started. It is designed to simplify the Chrome CDM mirroring process. Have fun, and feel free to share your thoughts! You are welcome to recode the code according to the key and value according to your possession
// Create a WeakMap to track whether a message has been modified
const modifiedEventsMap = new WeakMap();
let savedPSSH = null; // Global variable to store PSSH
let savedSessionId = null;
// Save the original reference of generateRequest
const originalGenerateRequest = MediaKeySession.prototype.generateRequest;
// Override generateRequest with a custom function
MediaKeySession.prototype.generateRequest = function(initDataType, initData) {
if (initData) {
savedPSSH = arrayBufferToBase64(initData); // Save initData (pssh)
console.log('Saved PSSH:', savedPSSH);
}
return originalGenerateRequest.call(this, initDataType, initData);
};
// Save the original reference of createSession
const originalCreateSession = MediaKeys.prototype.createSession;
// Override createSession with a custom function
MediaKeys.prototype.createSession = function(sessionType) {
console.log('Custom createSession function called');
// Call the original createSession
const session = originalCreateSession.call(this, sessionType);
// Add a listener to capture messages from the session
session.addEventListener('message', (event) => {
// Check if the event has been modified using the WeakMap
if (modifiedEventsMap.has(event)) {
console.log('Modified message, skipping to avoid repeat.');
return; // Skip events that have already been modified
}
console.log('Original message received:', event.message);
if (event.messageType == "license-request") {
// Perform license fetching and create a fake event
fetch("API URL HERE", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ cert: "", pssh: savedPSSH })
})
.then(response => response.json())
.then(data => {
const license_b64 = data["responseData"]["challenge_b64"];
savedSessionId = data["responseData"]["session_id"];
// Create a "fake event" by copying the message and messageType properties
const fakeEvent = new MediaKeyMessageEvent('message', {
message: base64ToArrayBuffer(license_b64), // Replace `message` with the modified message
messageType: event.messageType // Add `messageType` since it's required
});
console.log("Done!");
console.log(savedSessionId);
// Mark that this fakeEvent has been modified using the WeakMap
modifiedEventsMap.set(fakeEvent, true);
// Dispatch the "fake event" for processing
session.dispatchEvent(fakeEvent);
});
} else {
console.log(event.messageType);
}
});
// Override the update function to capture updated responses
const originalUpdate = session.update.bind(session);
session.update = function(response) {
const updatedData = arrayBufferToBase64(response); // Save the response to a variable
if (updatedData.startsWith("CAIS") && savedSessionId) {
fetch("API URL HERE", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ license_b64: updatedData, session_id: savedSessionId })
})
.then(response => response.json())
.then(data => {
console.log(data);
});
}
// Call the original update function
return originalUpdate(response);
};
return session;
};
function base64ToArrayBuffer(base64) {
const binaryString = atob(base64); // Decode Base64
const len = binaryString.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binaryString.charCodeAt(i); // Convert binary character to byte
}
return bytes.buffer; // Return as ArrayBuffer
}
function arrayBufferToBase64(buffer) {
const binary = String.fromCharCode.apply(null, new Uint8Array(buffer));
return btoa(binary);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment