discord-player-deezer 2.5.0 → 3.0.0-dev.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.yarn/install-state.gz +0 -0
- package/dist/index.d.ts +140 -6
- package/dist/index.js +369 -50
- package/package.json +1 -1
- package/readme.md +23 -1
- package/src/Cache/CacheTapStream.ts +81 -0
- package/src/Cache/DiskAudioCache.ts +232 -0
- package/src/Cache/IAudioCache.ts +30 -0
- package/src/Cache/index.ts +2 -0
- package/src/Crypto/JSDecryptor.ts +119 -18
- package/src/Crypto/NodeDecryptor.ts +70 -17
- package/src/Crypto/StripeDecryptStream.ts +73 -0
- package/src/Crypto/index.ts +2 -1
- package/src/DeezerExtractor.ts +5 -1
- package/src/index.ts +2 -1
- package/src/utils/crypto.ts +10 -0
- package/src/utils/util.ts +56 -47
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
2
|
+
import { createWriteStream } from "node:fs";
|
|
3
|
+
import {
|
|
4
|
+
mkdir,
|
|
5
|
+
open,
|
|
6
|
+
readdir,
|
|
7
|
+
rename,
|
|
8
|
+
stat,
|
|
9
|
+
unlink,
|
|
10
|
+
utimes,
|
|
11
|
+
} from "node:fs/promises";
|
|
12
|
+
import { tmpdir } from "node:os";
|
|
13
|
+
import { join } from "node:path";
|
|
14
|
+
import type { Readable, Writable } from "node:stream";
|
|
15
|
+
import type { IAudioCache } from "./IAudioCache";
|
|
16
|
+
|
|
17
|
+
const CACHE_EXTENSION = ".audio";
|
|
18
|
+
const TEMP_EXTENSION = ".part";
|
|
19
|
+
const TEMP_MAX_AGE = 24 * 60 * 60 * 1000;
|
|
20
|
+
|
|
21
|
+
const DEFAULT_MAX_SONGS = 100;
|
|
22
|
+
const DEFAULT_MAX_AGE = 7 * 24 * 60 * 60 * 1000;
|
|
23
|
+
const DEFAULT_CLEANUP_INTERVAL = 60 * 60 * 1000;
|
|
24
|
+
|
|
25
|
+
export interface DiskAudioCacheOptions {
|
|
26
|
+
/** Directory used to store decrypted tracks. */
|
|
27
|
+
directory?: string;
|
|
28
|
+
/**
|
|
29
|
+
* Maximum number of cached songs. Oldest entries are removed first.
|
|
30
|
+
* Set to `0` to disable the count limit.
|
|
31
|
+
*/
|
|
32
|
+
maxSongs?: number;
|
|
33
|
+
/**
|
|
34
|
+
* Remove entries older than this many milliseconds.
|
|
35
|
+
* Set to `0` to disable age-based expiry.
|
|
36
|
+
*/
|
|
37
|
+
maxAge?: number;
|
|
38
|
+
/**
|
|
39
|
+
* How often background cleanup runs, in milliseconds.
|
|
40
|
+
* Set to `0` to only clean up after writes.
|
|
41
|
+
*/
|
|
42
|
+
cleanupInterval?: number;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Disk-backed audio cache. Keys are hashed, so arbitrary strings can be used
|
|
47
|
+
* without risking path traversal. Audio is written to a temporary file and
|
|
48
|
+
* renamed into place once complete, so partially downloaded tracks are never
|
|
49
|
+
* served.
|
|
50
|
+
*/
|
|
51
|
+
export class DiskAudioCache implements IAudioCache {
|
|
52
|
+
readonly directory: string;
|
|
53
|
+
readonly maxSongs: number;
|
|
54
|
+
readonly maxAge: number;
|
|
55
|
+
readonly cleanupInterval: number;
|
|
56
|
+
|
|
57
|
+
private readonly ready: Promise<void>;
|
|
58
|
+
private cleaning?: Promise<void>;
|
|
59
|
+
private timer?: NodeJS.Timeout;
|
|
60
|
+
private closed = false;
|
|
61
|
+
|
|
62
|
+
constructor(options: DiskAudioCacheOptions = {}) {
|
|
63
|
+
this.directory =
|
|
64
|
+
options.directory ??
|
|
65
|
+
join(tmpdir(), "discord-player-deezer-cache");
|
|
66
|
+
this.maxSongs = options.maxSongs ?? DEFAULT_MAX_SONGS;
|
|
67
|
+
this.maxAge = options.maxAge ?? DEFAULT_MAX_AGE;
|
|
68
|
+
this.cleanupInterval =
|
|
69
|
+
options.cleanupInterval ?? DEFAULT_CLEANUP_INTERVAL;
|
|
70
|
+
|
|
71
|
+
this.ready = mkdir(this.directory, { recursive: true }).then(
|
|
72
|
+
() => undefined
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
if (this.cleanupInterval > 0) {
|
|
76
|
+
this.timer = setInterval(() => {
|
|
77
|
+
void this.cleanup().catch(() => undefined);
|
|
78
|
+
}, this.cleanupInterval);
|
|
79
|
+
this.timer.unref();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Run once on startup so stale entries are removed even if the cache
|
|
83
|
+
// is never written to.
|
|
84
|
+
void this.cleanup().catch(() => undefined);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async read(key: string): Promise<Readable | null> {
|
|
88
|
+
await this.ready;
|
|
89
|
+
|
|
90
|
+
const file = this.pathFor(key);
|
|
91
|
+
|
|
92
|
+
try {
|
|
93
|
+
const handle = await open(file, "r");
|
|
94
|
+
|
|
95
|
+
// Touch the entry so cleanup treats it as recently used.
|
|
96
|
+
const now = new Date();
|
|
97
|
+
await utimes(file, now, now).catch(() => undefined);
|
|
98
|
+
|
|
99
|
+
// `filehandle.createReadStream()` does not close the handle for
|
|
100
|
+
// us, so release it explicitly when the stream is done.
|
|
101
|
+
const stream = handle.createReadStream({ autoClose: false });
|
|
102
|
+
let released = false;
|
|
103
|
+
const release = () => {
|
|
104
|
+
if (released) return;
|
|
105
|
+
released = true;
|
|
106
|
+
void handle.close().catch(() => undefined);
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
stream.once("end", release);
|
|
110
|
+
stream.once("error", release);
|
|
111
|
+
stream.once("close", release);
|
|
112
|
+
|
|
113
|
+
return stream;
|
|
114
|
+
} catch (error) {
|
|
115
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return null;
|
|
116
|
+
throw error;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async write(key: string): Promise<Writable> {
|
|
121
|
+
if (this.closed) throw new Error("DiskAudioCache is closed");
|
|
122
|
+
|
|
123
|
+
await this.ready;
|
|
124
|
+
|
|
125
|
+
const target = this.pathFor(key);
|
|
126
|
+
const temp = `${target}.${randomBytes(8).toString("hex")}${TEMP_EXTENSION}`;
|
|
127
|
+
const out = createWriteStream(temp);
|
|
128
|
+
let committed = false;
|
|
129
|
+
|
|
130
|
+
// Always observe errors so a broken cache cannot crash the caller.
|
|
131
|
+
out.on("error", () => {
|
|
132
|
+
void unlink(temp).catch(() => undefined);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
out.once("finish", () => {
|
|
136
|
+
committed = true;
|
|
137
|
+
|
|
138
|
+
void rename(temp, target)
|
|
139
|
+
.then(() => this.cleanup())
|
|
140
|
+
.catch(async () => {
|
|
141
|
+
await unlink(temp).catch(() => undefined);
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
out.once("close", () => {
|
|
146
|
+
if (!committed) void unlink(temp).catch(() => undefined);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
return out;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async delete(key: string): Promise<void> {
|
|
153
|
+
await this.ready;
|
|
154
|
+
|
|
155
|
+
await unlink(this.pathFor(key)).catch((error) => {
|
|
156
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
cleanup(): Promise<void> {
|
|
161
|
+
if (!this.cleaning) {
|
|
162
|
+
this.cleaning = this.runCleanup().finally(() => {
|
|
163
|
+
this.cleaning = undefined;
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return this.cleaning;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
async close(): Promise<void> {
|
|
171
|
+
this.closed = true;
|
|
172
|
+
|
|
173
|
+
if (this.timer) {
|
|
174
|
+
clearInterval(this.timer);
|
|
175
|
+
this.timer = undefined;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (this.cleaning) await this.cleaning.catch(() => undefined);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
private pathFor(key: string): string {
|
|
182
|
+
const hash = createHash("sha256").update(key).digest("hex");
|
|
183
|
+
return join(this.directory, hash + CACHE_EXTENSION);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
private async runCleanup(): Promise<void> {
|
|
187
|
+
await this.ready;
|
|
188
|
+
|
|
189
|
+
const now = Date.now();
|
|
190
|
+
const entries = await readdir(this.directory, { withFileTypes: true });
|
|
191
|
+
const songs: { file: string; mtimeMs: number }[] = [];
|
|
192
|
+
|
|
193
|
+
for (const entry of entries) {
|
|
194
|
+
if (!entry.isFile()) continue;
|
|
195
|
+
|
|
196
|
+
const isSong = entry.name.endsWith(CACHE_EXTENSION);
|
|
197
|
+
const isTemp = entry.name.endsWith(TEMP_EXTENSION);
|
|
198
|
+
if (!isSong && !isTemp) continue;
|
|
199
|
+
|
|
200
|
+
const file = join(this.directory, entry.name);
|
|
201
|
+
|
|
202
|
+
let info;
|
|
203
|
+
try {
|
|
204
|
+
info = await stat(file);
|
|
205
|
+
} catch {
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (isTemp) {
|
|
210
|
+
if (now - info.mtimeMs > TEMP_MAX_AGE) {
|
|
211
|
+
await unlink(file).catch(() => undefined);
|
|
212
|
+
}
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (this.maxAge > 0 && now - info.mtimeMs > this.maxAge) {
|
|
217
|
+
await unlink(file).catch(() => undefined);
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
songs.push({ file, mtimeMs: info.mtimeMs });
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (this.maxSongs > 0 && songs.length > this.maxSongs) {
|
|
225
|
+
songs.sort((a, b) => b.mtimeMs - a.mtimeMs);
|
|
226
|
+
|
|
227
|
+
for (let i = this.maxSongs; i < songs.length; i++) {
|
|
228
|
+
await unlink(songs[i].file).catch(() => undefined);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Readable, Writable } from "node:stream";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Storage backend for decrypted audio.
|
|
5
|
+
*
|
|
6
|
+
* A cache is read before the track is fetched and written while the track is
|
|
7
|
+
* being streamed, so implementations should support concurrent streams.
|
|
8
|
+
*/
|
|
9
|
+
export interface IAudioCache {
|
|
10
|
+
/**
|
|
11
|
+
* Returns a readable stream for the cached track, or `null` when the key
|
|
12
|
+
* is not present.
|
|
13
|
+
*/
|
|
14
|
+
read(key: string): Promise<Readable | null>;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Opens a writable for the given key. The entry is committed when the
|
|
18
|
+
* writable finishes and discarded if it is destroyed first.
|
|
19
|
+
*/
|
|
20
|
+
write(key: string): Promise<Writable>;
|
|
21
|
+
|
|
22
|
+
/** Removes a single entry. Missing entries are ignored. */
|
|
23
|
+
delete(key: string): Promise<void>;
|
|
24
|
+
|
|
25
|
+
/** Applies the cache's age and size limits. */
|
|
26
|
+
cleanup(): Promise<void>;
|
|
27
|
+
|
|
28
|
+
/** Stops any background maintenance. Optional. */
|
|
29
|
+
close?(): void | Promise<void>;
|
|
30
|
+
}
|
|
@@ -1,18 +1,119 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
1
|
+
/* tslint:disable:no-bitwise */
|
|
2
|
+
import Blowfish, { MODE, PADDING } from "blowfish-node";
|
|
3
|
+
import { BaseDecryptor } from "./BaseDecryptor";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* `blowfish-node` does not expose the expanded key schedule in its typings,
|
|
7
|
+
* but the tables are available on the instance after construction.
|
|
8
|
+
*/
|
|
9
|
+
type BlowfishInternals = Blowfish & {
|
|
10
|
+
p: number[];
|
|
11
|
+
s: number[][];
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Blowfish-CBC decryptor for Deezer's BF_CBC_STRIPE streams.
|
|
16
|
+
*
|
|
17
|
+
* `blowfish-node`'s `decode()` allocates (and copies) several times per call
|
|
18
|
+
* and runs the block cipher through helper functions and array destructuring.
|
|
19
|
+
* Since this is the hot path for every encrypted 2048-byte stripe, we use the
|
|
20
|
+
* library only to run the key schedule once and then decrypt in place with a
|
|
21
|
+
* tight loop.
|
|
22
|
+
*/
|
|
23
|
+
export class JSDecryptor extends BaseDecryptor {
|
|
24
|
+
private readonly p: Uint32Array;
|
|
25
|
+
private readonly s0: Uint32Array;
|
|
26
|
+
private readonly s1: Uint32Array;
|
|
27
|
+
private readonly s2: Uint32Array;
|
|
28
|
+
private readonly s3: Uint32Array;
|
|
29
|
+
private readonly iv0: number;
|
|
30
|
+
private readonly iv1: number;
|
|
31
|
+
|
|
32
|
+
constructor(...args: ConstructorParameters<typeof BaseDecryptor>) {
|
|
33
|
+
super(...args);
|
|
34
|
+
|
|
35
|
+
const schedule = new Blowfish(
|
|
36
|
+
new Uint8Array(this.key),
|
|
37
|
+
MODE.CBC,
|
|
38
|
+
PADDING.NULL
|
|
39
|
+
) as BlowfishInternals;
|
|
40
|
+
|
|
41
|
+
this.p = Uint32Array.from(schedule.p);
|
|
42
|
+
this.s0 = Uint32Array.from(schedule.s[0]);
|
|
43
|
+
this.s1 = Uint32Array.from(schedule.s[1]);
|
|
44
|
+
this.s2 = Uint32Array.from(schedule.s[2]);
|
|
45
|
+
this.s3 = Uint32Array.from(schedule.s[3]);
|
|
46
|
+
|
|
47
|
+
this.iv0 =
|
|
48
|
+
(this.iv[0] << 24) |
|
|
49
|
+
(this.iv[1] << 16) |
|
|
50
|
+
(this.iv[2] << 8) |
|
|
51
|
+
this.iv[3];
|
|
52
|
+
this.iv1 =
|
|
53
|
+
(this.iv[4] << 24) |
|
|
54
|
+
(this.iv[5] << 16) |
|
|
55
|
+
(this.iv[6] << 8) |
|
|
56
|
+
this.iv[7];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async decrypt(chunk: Buffer): Promise<Buffer> {
|
|
60
|
+
if (chunk.length % 8 !== 0) {
|
|
61
|
+
throw new Error("Decoded data should be multiple of 8 bytes");
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Work on a copy so callers never observe a mutated input buffer.
|
|
65
|
+
const out = Buffer.from(chunk);
|
|
66
|
+
const view = new DataView(
|
|
67
|
+
out.buffer as ArrayBuffer,
|
|
68
|
+
out.byteOffset,
|
|
69
|
+
out.byteLength
|
|
70
|
+
);
|
|
71
|
+
const { p, s0, s1, s2, s3 } = this;
|
|
72
|
+
|
|
73
|
+
// Each Deezer stripe is an independent CBC stream starting from the
|
|
74
|
+
// same IV, so the chain restarts for every call.
|
|
75
|
+
let prevL = this.iv0;
|
|
76
|
+
let prevR = this.iv1;
|
|
77
|
+
|
|
78
|
+
for (let offset = 0; offset < out.length; offset += 8) {
|
|
79
|
+
let l = view.getUint32(offset, false);
|
|
80
|
+
let r = view.getUint32(offset + 4, false);
|
|
81
|
+
|
|
82
|
+
const cipherL = l;
|
|
83
|
+
const cipherR = r;
|
|
84
|
+
|
|
85
|
+
for (let round = 17; round > 1; round--) {
|
|
86
|
+
l ^= p[round];
|
|
87
|
+
|
|
88
|
+
let f =
|
|
89
|
+
(((s0[(l >>> 24) & 0xff] + s1[(l >>> 16) & 0xff]) | 0) ^
|
|
90
|
+
s2[(l >>> 8) & 0xff]);
|
|
91
|
+
f = (f + s3[l & 0xff]) | 0;
|
|
92
|
+
|
|
93
|
+
r ^= f;
|
|
94
|
+
|
|
95
|
+
const tmp = l;
|
|
96
|
+
l = r;
|
|
97
|
+
r = tmp;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const swap = l;
|
|
101
|
+
l = r;
|
|
102
|
+
r = swap;
|
|
103
|
+
|
|
104
|
+
r ^= p[1];
|
|
105
|
+
l ^= p[0];
|
|
106
|
+
|
|
107
|
+
l ^= prevL;
|
|
108
|
+
r ^= prevR;
|
|
109
|
+
|
|
110
|
+
prevL = cipherL;
|
|
111
|
+
prevR = cipherR;
|
|
112
|
+
|
|
113
|
+
view.setUint32(offset, l, false);
|
|
114
|
+
view.setUint32(offset + 4, r, false);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return out;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -1,17 +1,70 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
1
|
+
/* tslint:disable:no-bitwise */
|
|
2
|
+
import { getCrypto } from "../utils/crypto";
|
|
3
|
+
import { BaseDecryptor } from "./BaseDecryptor";
|
|
4
|
+
import type {
|
|
5
|
+
CipherGCMTypes,
|
|
6
|
+
CipherKey,
|
|
7
|
+
BinaryLike,
|
|
8
|
+
Decipher,
|
|
9
|
+
} from "node:crypto";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Native (`bf-cbc`) decryptor.
|
|
13
|
+
*
|
|
14
|
+
* A `Decipher` keeps its expanded key schedule for as long as it lives, but
|
|
15
|
+
* `createDecipheriv()` rebuilds that schedule on every call. Creating one per
|
|
16
|
+
* 2048-byte stripe is therefore slower than the pure-JS decryptor. Instead we
|
|
17
|
+
* keep a single cipher for the whole track and repair the CBC chain at stripe
|
|
18
|
+
* boundaries: Deezer restarts every stripe from the same IV, while the cipher
|
|
19
|
+
* carries the previous stripe's last ciphertext block forward. Only the first
|
|
20
|
+
* block of each stripe differs, and it can be fixed with one XOR.
|
|
21
|
+
*/
|
|
22
|
+
export class NodeDecryptor extends BaseDecryptor {
|
|
23
|
+
private decipher?: Decipher;
|
|
24
|
+
private previousCipherBlock?: Buffer;
|
|
25
|
+
private firstChunk = true;
|
|
26
|
+
|
|
27
|
+
async decrypt(chunk: Buffer): Promise<Buffer> {
|
|
28
|
+
if (chunk.length % 8 !== 0) {
|
|
29
|
+
throw new Error("Decoded data should be multiple of 8 bytes");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (chunk.length === 0) return Buffer.alloc(0);
|
|
33
|
+
|
|
34
|
+
const crypto = await getCrypto();
|
|
35
|
+
|
|
36
|
+
if (!this.decipher) {
|
|
37
|
+
try {
|
|
38
|
+
this.decipher = crypto
|
|
39
|
+
.createDecipheriv(
|
|
40
|
+
"bf-cbc" as unknown as CipherGCMTypes,
|
|
41
|
+
this.key as unknown as CipherKey,
|
|
42
|
+
this.iv as unknown as BinaryLike
|
|
43
|
+
)
|
|
44
|
+
.setAutoPadding(false) as unknown as Decipher;
|
|
45
|
+
} catch {
|
|
46
|
+
throw new Error(
|
|
47
|
+
"Cannot find bf-cbc. Start Node with --openssl-legacy-provider or use JSDecryptor."
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
this.previousCipherBlock = Buffer.alloc(8);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const out = this.decipher.update(chunk);
|
|
55
|
+
|
|
56
|
+
if (!this.firstChunk && this.previousCipherBlock && out.length >= 8) {
|
|
57
|
+
// Undo the CBC chaining left over from the previous stripe.
|
|
58
|
+
for (let i = 0; i < 8; i++) {
|
|
59
|
+
out[i] ^= this.previousCipherBlock[i] ^ this.iv[i];
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
this.firstChunk = false;
|
|
64
|
+
this.previousCipherBlock = Buffer.from(
|
|
65
|
+
chunk.subarray(chunk.length - 8)
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { Transform, type TransformCallback } from "node:stream";
|
|
2
|
+
import type { BaseDecryptor } from "./BaseDecryptor";
|
|
3
|
+
|
|
4
|
+
/** Deezer's BF_CBC_STRIPE cipher encrypts every 3rd 2048-byte stripe. */
|
|
5
|
+
const STRIPE_SIZE = 2048;
|
|
6
|
+
const ENCRYPTED_STRIPE_INTERVAL = 3;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Decrypts a Deezer stripe stream as it flows through.
|
|
10
|
+
*
|
|
11
|
+
* Using a `Transform` means the HTTP body is only pulled when the consumer
|
|
12
|
+
* actually reads: if playback/ffmpeg slows down, backpressure pauses the
|
|
13
|
+
* download instead of buffering the whole track in memory.
|
|
14
|
+
*/
|
|
15
|
+
export class StripeDecryptStream extends Transform {
|
|
16
|
+
private stripeIndex = 0;
|
|
17
|
+
private remainder: Buffer = Buffer.alloc(0);
|
|
18
|
+
private readonly decryptor: BaseDecryptor;
|
|
19
|
+
|
|
20
|
+
constructor(decryptor: BaseDecryptor) {
|
|
21
|
+
super();
|
|
22
|
+
this.decryptor = decryptor;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async _transform(
|
|
26
|
+
chunk: Buffer,
|
|
27
|
+
_encoding: BufferEncoding,
|
|
28
|
+
callback: TransformCallback
|
|
29
|
+
): Promise<void> {
|
|
30
|
+
try {
|
|
31
|
+
const data = this.remainder.length
|
|
32
|
+
? Buffer.concat([this.remainder, chunk])
|
|
33
|
+
: chunk;
|
|
34
|
+
|
|
35
|
+
let offset = 0;
|
|
36
|
+
|
|
37
|
+
while (data.length - offset >= STRIPE_SIZE) {
|
|
38
|
+
const stripe = data.subarray(offset, offset + STRIPE_SIZE);
|
|
39
|
+
offset += STRIPE_SIZE;
|
|
40
|
+
this.push(await this.decodeStripe(stripe));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Copy the tail: the incoming chunk may be reused by the source.
|
|
44
|
+
this.remainder = Buffer.from(data.subarray(offset));
|
|
45
|
+
|
|
46
|
+
callback();
|
|
47
|
+
} catch (error) {
|
|
48
|
+
callback(error as Error);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async _flush(callback: TransformCallback): Promise<void> {
|
|
53
|
+
try {
|
|
54
|
+
if (this.remainder.length) {
|
|
55
|
+
this.push(await this.decodeStripe(this.remainder));
|
|
56
|
+
this.remainder = Buffer.alloc(0);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
callback();
|
|
60
|
+
} catch (error) {
|
|
61
|
+
callback(error as Error);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
private async decodeStripe(stripe: Buffer): Promise<Buffer> {
|
|
66
|
+
const encrypted = this.stripeIndex++ % ENCRYPTED_STRIPE_INTERVAL === 0;
|
|
67
|
+
|
|
68
|
+
// A trailing partial stripe cannot be a valid CBC block.
|
|
69
|
+
if (!encrypted || stripe.length % 8 !== 0) return stripe;
|
|
70
|
+
|
|
71
|
+
return this.decryptor.decrypt(stripe);
|
|
72
|
+
}
|
|
73
|
+
}
|
package/src/Crypto/index.ts
CHANGED
package/src/DeezerExtractor.ts
CHANGED
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
Track,
|
|
15
15
|
} from "discord-player";
|
|
16
16
|
import { setTimeout } from "node:timers/promises";
|
|
17
|
+
import type { IAudioCache } from "./Cache";
|
|
17
18
|
import { BaseDecryptor } from "./Crypto/BaseDecryptor";
|
|
18
19
|
import {
|
|
19
20
|
buildTrackFromSearch,
|
|
@@ -46,6 +47,8 @@ export type DeezerExtractorOptions = {
|
|
|
46
47
|
priority?: number;
|
|
47
48
|
arl: string;
|
|
48
49
|
decryptor?: typeof BaseDecryptor;
|
|
50
|
+
/** Opt-in cache for decrypted audio. */
|
|
51
|
+
cache?: IAudioCache;
|
|
49
52
|
};
|
|
50
53
|
|
|
51
54
|
export type DeezerUserInfo = {
|
|
@@ -161,6 +164,7 @@ export class DeezerExtractor extends BaseExtractor<DeezerExtractorOptions> {
|
|
|
161
164
|
async deactivate() {
|
|
162
165
|
if (this.__interval) clearInterval(this.__interval);
|
|
163
166
|
this.protocols = [];
|
|
167
|
+
await this.options.cache?.close?.();
|
|
164
168
|
}
|
|
165
169
|
|
|
166
170
|
async validate(
|
|
@@ -231,7 +235,7 @@ export class DeezerExtractor extends BaseExtractor<DeezerExtractorOptions> {
|
|
|
231
235
|
url: track.url,
|
|
232
236
|
source: "arbitrary",
|
|
233
237
|
thumbnail: track.thumbnail[0].url,
|
|
234
|
-
duration: DPUtil.buildTimeCode(DPUtil.parseMS(track.duration)),
|
|
238
|
+
duration: DPUtil.buildTimeCode(DPUtil.parseMS(track.duration * 1000)),
|
|
235
239
|
requestedBy,
|
|
236
240
|
});
|
|
237
241
|
}
|
package/src/index.ts
CHANGED