From d037b7cfb215e3437a078ab249b32c3117af67e3 Mon Sep 17 00:00:00 2001 From: Luca Armaroli Date: Wed, 9 Sep 2020 04:50:24 +0200 Subject: [PATCH] renamed and finished decrypter --- src/Decrypter.ts | 36 +++++++++++++++++++ src/Descrypter.ts | 88 ----------------------------------------------- 2 files changed, 36 insertions(+), 88 deletions(-) create mode 100644 src/Decrypter.ts delete mode 100644 src/Descrypter.ts diff --git a/src/Decrypter.ts b/src/Decrypter.ts new file mode 100644 index 0000000..5ac50c7 --- /dev/null +++ b/src/Decrypter.ts @@ -0,0 +1,36 @@ +import { ApiClient } from './ApiClient'; +import { logger } from './Logger'; +import { Session } from './Types'; + +import crypto from 'crypto'; + + +export async function getDecrypter(playlistUrl: string, session: Session): Promise { + const apiClient = ApiClient.getInstance(session); + + const keyOption = await apiClient.callUrl(playlistUrl, 'get', null, 'text') + .then(res => (res?.data as string).split(/\r?\n/) + .find(line => line.startsWith('#EXT-X-KEY'))); + + if (keyOption) { + logger.debug('[Decrypter] CRIPTO LINE IN M3U8: ' + keyOption); + + const match = RegExp(/#EXT-X-KEY:METHOD=(.*?),URI="(.*?)",IV=0X(.*)/).exec(keyOption); + + if (!match) { + throw new Error('No match for regex'); + } + + const algorithm = match[1].toLowerCase().replace('-', ''); + + const key: Buffer = await apiClient.callUrl(match[2], 'post', null, 'arraybuffer') + .then(res => res?.data); + + const iv = Buffer.from(match[3], 'hex'); + + return crypto.createDecipheriv(algorithm, key, iv); + } + else { + process.exit(555); + } +} diff --git a/src/Descrypter.ts b/src/Descrypter.ts deleted file mode 100644 index 96483b2..0000000 --- a/src/Descrypter.ts +++ /dev/null @@ -1,88 +0,0 @@ -import { ApiClient } from './ApiClient'; -import { Session } from './Types'; - -import crypto from 'crypto'; -import { logger } from './Logger'; -// import Axios from 'axios'; - - -/* export async function getDecrypter(playlistUrl: string, session: Session): Promise { - - return new Promise(async (resolve, reject) => { - const apiClient = ApiClient.getInstance(session); - - const keyOption = await apiClient.callUrl(playlistUrl, 'get', null, 'text') - .then(res => (res?.data as string).split(/\r?\n/) - .find(line => line.startsWith('#EXT-X-KEY'))); - - if (keyOption) { - - const match = RegExp(/#EXT-X-KEY:METHOD=(.*?),URI="(.*?),IV=0X(.*)/).exec(keyOption); - - if (!match) { - throw new Error(); - } - - const algorithm = match[1].toLowerCase().replace('-', ''); - - const key: Buffer = await apiClient.callUrl(match[2], 'get', null, 'arraybuffer') - .then(res => res?.data); - - const iv = Buffer.from(match[3].substring(2), 'hex'); - - resolve(crypto.createDecipheriv(algorithm, key, iv)); - } - else { - reject(); - } - }); -} */ - - -export async function getDecrypter(playlistUrl: string, session: Session): Promise { - const apiClient = ApiClient.getInstance(session); - - /* Axios.get(playlistUrl, { - headers: { - 'Authorization': 'Bearer ' + session?.AccessToken, - 'User-Agent': 'destreamer/3.0 (beta)' - }, - responseType: 'text' - }) - .then(res => logger.warn(res.data.split(/\r?\n/)[0])) - .catch(err => logger.error(err)); */ - - const keyOption = await apiClient.callUrl(playlistUrl, 'get', null, 'text') - .catch(err => logger.debug(err)) - .then(res => (res?.data as string).split(/\r?\n/) - .find(line => line.startsWith('#EXT-X-KEY'))); - - if (keyOption) { - logger.debug('CRIPTO LINE IN M3U8: ' + keyOption); - - const match = RegExp(/#EXT-X-KEY:METHOD=(.*?),URI="(.*?)",IV=0X(.*)/).exec(keyOption); - - if (!match) { - throw new Error('No match for regex'); - } - - const algorithm = match[1].toLowerCase().replace('-', ''); - - const key: Buffer = await apiClient.callUrl(match[2], 'post', null, 'arraybuffer') - .then(res => res?.data); - - const iv = Buffer.from(match[3], 'hex'); - - return crypto.createDecipheriv(algorithm, key, iv); - } - else { - process.exit(555); - } -} - -/* -const input = fs.createReadStream('test.enc'); -const output = fs.createWriteStream('test.js'); - -input.pipe(decipher).pipe(output); -*/