From 0b22b4ec36f92a58064c9c15f0174e41bbb7fb3c Mon Sep 17 00:00:00 2001 From: AnimeDL Date: Tue, 19 Mar 2024 16:27:00 -0700 Subject: [PATCH] Improve CDM detection Automatically checks all files in the directory for a valid CDM --- modules/widevine.ts | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/modules/widevine.ts b/modules/widevine.ts index 3d7c88d..8e21e07 100644 --- a/modules/widevine.ts +++ b/modules/widevine.ts @@ -7,13 +7,35 @@ import path from 'path'; import { ReadError, Response } from 'got'; //read cdm files located in the same directory -let privateKey: Buffer, identifierBlob: Buffer; +let privateKey: Buffer = Buffer.from([]), identifierBlob: Buffer = Buffer.from([]); export let canDecrypt: boolean; try { - privateKey = fs.readFileSync(path.join(workingDir, 'widevine', 'device_private_key')); - identifierBlob = fs.readFileSync(path.join(workingDir, 'widevine', 'device_client_id_blob')); - canDecrypt = true; + const files = fs.readdirSync(path.join(workingDir, 'widevine')); + files.forEach(function(file) { + file = path.join(workingDir, 'widevine', file); + const stats = fs.statSync(file); + if (stats.size < 1024*8) { + const fileContents = fs.readFileSync(file, {'encoding': 'utf8'}); + if (fileContents.includes('-BEGIN RSA PRIVATE KEY-')) { + privateKey = fs.readFileSync(file); + } + if (fileContents.includes('widevine_cdm_version')) { + identifierBlob = fs.readFileSync(file); + } + } + }); + + if (privateKey.length !== 0 && identifierBlob.length !== 0) { + canDecrypt = true; + } else if (privateKey.length == 0) { + console.warn('Private key missing'); + canDecrypt = false; + } else if (identifierBlob.length == 0) { + console.warn('Identifier blob missing'); + canDecrypt = false; + } } catch (e) { + console.error(e); canDecrypt = false; }