Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore/remove legacy code #4

Merged
merged 7 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
.github/*
src/*
.github/*
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"license": "MIT",
"private": false,
"dependencies": {
"@mithron/deezer-music-metadata": "^1.0.3"
"@mithron/deezer-music-metadata": "^1.0.3",
"blowfish-node": "^1.1.4"
},
"devDependencies": {
"@discord-player/extractor": "^4.5.1",
Expand All @@ -21,7 +22,8 @@
"scripts": {
"build": "yarn lint && tsup",
"lint": "tslint -p tsconfig.json",
"npm:publish": "yarn build && npm publish --access public"
"npm:publish": "yarn build && npm publish --access public",
"prepare": "tsup"
},
"repository": {
"type": "git",
Expand Down
13 changes: 11 additions & 2 deletions src/DeezerExtractor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BaseExtractor, ExtractorSearchContext, ExtractorStreamable, Track, Playlist, Util as DPUtil, ExtractorInfo } from "discord-player"
import { Playlist as DeezerPlaylist, Track as DeezerTrack, getData } from "@mithron/deezer-music-metadata";
import { buildTrackFromSearch, getCrypto, searchOneTrack, streamTrack, validate } from "./utils/util";
import { buildTrackFromSearch, deezerRegex, getCrypto, searchOneTrack, streamTrack, validate } from "./utils/util";

/**
* -------------------------------NOTICE-------------------------------------
Expand All @@ -23,12 +23,17 @@ export type DeezerUserInfo = {
mediaUrl: string;
}

export const Warnings = {
MissingDecryption: "Decryption Key missing! This is needed for extracting streams."
} as const
export type Warnings = (typeof Warnings)[keyof typeof Warnings]

export class DeezerExtractor extends BaseExtractor<DeezerExtractorOptions> {
static identifier: string = "com.retrouser955.discord-player.deezr-ext"
userInfo!: DeezerUserInfo

async activate(): Promise<void> {
if(!this.options.decryptionKey) process.emitWarning("Decryption Key missing! This is needed for extracting streams.")
if(!this.options.decryptionKey) process.emitWarning(Warnings.MissingDecryption)
else {
// extract deezer username
// dynamically load crypto because some might not want streaming
Expand Down Expand Up @@ -81,6 +86,10 @@ export class DeezerExtractor extends BaseExtractor<DeezerExtractorOptions> {
}

async handle(query: string, context: ExtractorSearchContext): Promise<ExtractorInfo> {
if (deezerRegex.share.test(query)) {
const redirect = await fetch(query);
query = redirect.url; // follow the redirect of deezer page links
}
const metadata = await getData(query)

if(metadata?.type === "song") return this.createResponse(null, [this.buildTrack(metadata, context)])
Expand Down
15 changes: 6 additions & 9 deletions src/utils/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { type Player, Track, Util } from "discord-player";
import type { DeezerExtractor } from "../DeezerExtractor";
import { Readable, PassThrough } from 'stream'
import type { BinaryLike, CipherGCMTypes, CipherKey, Decipher } from "crypto";
import Blowfish from "blowfish-node";

const IV = Buffer.from(Array.from({ length: 8 }, (_, x) => x))

Expand Down Expand Up @@ -158,20 +159,16 @@ export async function streamTrack(track: Track, ext: DeezerExtractor) {
} else {
buffer = Buffer.concat([buffer, chunk])
}

const blowfishDecrypter = new Blowfish(new Uint8Array(trackKey), Blowfish.MODE.CBC, Blowfish.PADDING.NULL)
blowfishDecrypter.setIv(new Uint8Array(IV))

while (buffer.length >= bufferSize) {
const bufferSized = buffer.subarray(0, bufferSize)

if (i % 3 === 0) {
const decipher = crypto.createDecipheriv(
'bf-cbc' as unknown as CipherGCMTypes,
trackKey as unknown as CipherKey,
IV as unknown as BinaryLike
).setAutoPadding(false) as unknown as Decipher

// @ts-ignore
passThrough.write(decipher.update(bufferSized as unknown as ArrayBufferView))
passThrough.write(decipher.final())
const decipher = Buffer.from(blowfishDecrypter.decode(new Uint8Array(bufferSized), Blowfish.TYPE.UINT8_ARRAY))
passThrough.write(decipher)
} else {
passThrough.write(bufferSized)
}
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,11 @@ balanced-match@^1.0.0:
resolved "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee"
integrity sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==

blowfish-node@^1.1.4:
version "1.1.4"
resolved "https://registry.npmjs.org/blowfish-node/-/blowfish-node-1.1.4.tgz#37bfe62a08f6d6026407399ad214c44d5bb4e01f"
integrity sha512-Iahpxc/cutT0M0tgwV5goklB+EzDuiYLgwJg050AmUG2jSIOpViWMLdnRgBxzZuNfswAgHSUiIdvmNdgL2v6DA==

boolbase@^1.0.0:
version "1.0.0"
resolved "https://registry.npmjs.org/boolbase/-/boolbase-1.0.0.tgz#68dff5fbe60c51eb37725ea9e3ed310dcc1e776e"
Expand Down