multi-downloader-nx/modules/cr_widevine.ts
2023-12-25 10:46:13 -08:00

38 lines
1.2 KiB
TypeScript

import { KeyContainer, Session } from './license';
import fs from 'fs';
import { console } from './log';
import got from 'got';
//read cdm files located in the same directory
const privateKey = fs.readFileSync('./widevine/device_private_key');
const identifierBlob = fs.readFileSync('./widevine/device_client_id_blob');
export default async function getKeys(pssh: string | undefined, licenseServer: string, authData: Record<string, string>): Promise<KeyContainer[]> {
if (!pssh) return [];
//pssh found in the mpd manifest
const psshBuffer = Buffer.from(
pssh,
'base64'
);
//Create a new widevine session
const session = new Session({ privateKey, identifierBlob }, psshBuffer);
//Generate license
const response = await got(licenseServer, {
method: 'POST',
body: session.createLicenseRequest(),
headers: authData,
responseType: 'text'
});
if (response.statusCode === 200) {
//Parse License and return keys
const json = JSON.parse(response.body);
const keys = session.parseLicense(Buffer.from(json['license'], 'base64'));
return keys;
} else {
console.info('License request failed:', response.statusMessage);
return [];
}
}