mirror of
https://git.unlock-music.dev/um/um-react.git
synced 2024-11-23 22:42:16 +00:00
feat: added audio ext detection
This commit is contained in:
parent
c734a83854
commit
77a3eba036
@ -14,7 +14,7 @@
|
||||
"@chakra-ui/react": "^2.6.1",
|
||||
"@emotion/react": "^11.11.0",
|
||||
"@emotion/styled": "^11.11.0",
|
||||
"@jixun/libparakeet": "0.0.0-exp.14",
|
||||
"@jixun/libparakeet": "0.0.0-exp.15",
|
||||
"@reduxjs/toolkit": "^1.9.5",
|
||||
"framer-motion": "^10.12.8",
|
||||
"nanoid": "^4.0.2",
|
||||
|
@ -14,8 +14,8 @@ dependencies:
|
||||
specifier: ^11.11.0
|
||||
version: 11.11.0(@emotion/react@11.11.0)(@types/react@18.0.28)(react@18.2.0)
|
||||
'@jixun/libparakeet':
|
||||
specifier: 0.0.0-exp.14
|
||||
version: 0.0.0-exp.14
|
||||
specifier: 0.0.0-exp.15
|
||||
version: 0.0.0-exp.15
|
||||
'@reduxjs/toolkit':
|
||||
specifier: ^1.9.5
|
||||
version: 1.9.5(react-redux@8.0.5)(react@18.2.0)
|
||||
@ -1780,8 +1780,8 @@ packages:
|
||||
resolution: {integrity: sha512-ZnQMnLV4e7hDlUvw8H+U8ASL02SS2Gn6+9Ac3wGGLIe7+je2AeAOxPY+izIPJDfFDb7eDjev0Us8MO1iFRN8hA==}
|
||||
dev: true
|
||||
|
||||
/@jixun/libparakeet@0.0.0-exp.14:
|
||||
resolution: {integrity: sha512-PJCV+1v+6EM/TVomuKqxvm0GSZE58la7JeU690Jem+BGP4LXAE9aaDHvSljEbcokhar4NXI0j5Ggxrq/PaDEkw==}
|
||||
/@jixun/libparakeet@0.0.0-exp.15:
|
||||
resolution: {integrity: sha512-Le2Gl/2V6BsrK8NcFTtaelUkjqewCmDT3xZgUxOKjOo2lK1qvj1AtxarhfDtsOQVjvItiibuRPaRFPpLmWJe9g==}
|
||||
dev: false
|
||||
|
||||
/@jridgewell/gen-mapping@0.3.3:
|
||||
|
@ -1,4 +1,8 @@
|
||||
export interface CryptoBase {
|
||||
/**
|
||||
* When returning false, a successful decryption should be checked by its decrypted content instead.
|
||||
*/
|
||||
hasSignature(): boolean;
|
||||
isSupported(blob: Blob): Promise<boolean>;
|
||||
decrypt(blob: Blob): Promise<Blob>;
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { transformBlob } from '~/decrypt-worker/util/transformBlob';
|
||||
import type { CryptoBase } from '../CryptoBase';
|
||||
import { loadLibParakeet, factory, BlobSink, createArrayBufferReader, TransformResult } from '@jixun/libparakeet';
|
||||
|
||||
const key = new Uint8Array([
|
||||
0x77, 0x48, 0x32, 0x73, 0xde, 0xf2, 0xc0, 0xc8, 0x95, 0xec, 0x30, 0xb2, 0x51, 0xc3, 0xe1, 0xa0, 0x9e, 0xe6, 0x9d,
|
||||
@ -19,33 +19,15 @@ const key = new Uint8Array([
|
||||
]);
|
||||
|
||||
export class QMC1Crypto implements CryptoBase {
|
||||
async isSupported(_blob: Blob): Promise<boolean> {
|
||||
hasSignature(): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
async isSupported(): Promise<boolean> {
|
||||
return true;
|
||||
}
|
||||
|
||||
async decrypt(blob: Blob): Promise<Blob> {
|
||||
const cleanup: (() => void)[] = [];
|
||||
|
||||
try {
|
||||
const mod = await loadLibParakeet();
|
||||
const transformer = factory.CreateQMCv1Transformer(mod, key);
|
||||
cleanup.push(() => transformer.delete());
|
||||
|
||||
const reader = createArrayBufferReader(await blob.arrayBuffer(), mod);
|
||||
cleanup.push(() => reader.delete());
|
||||
|
||||
const sink = new BlobSink(mod);
|
||||
const writer = sink.getWriter();
|
||||
cleanup.push(() => writer.delete());
|
||||
|
||||
const result = transformer.Transform(writer, reader);
|
||||
if (result !== TransformResult.OK) {
|
||||
throw new Error(`transform failed with error: ${TransformResult[result]} (${result})`);
|
||||
}
|
||||
|
||||
return sink.collectBlob();
|
||||
} finally {
|
||||
cleanup.forEach((clean) => clean());
|
||||
}
|
||||
return transformBlob(blob, (p) => p.make.QMCv1(key));
|
||||
}
|
||||
}
|
||||
|
@ -23,6 +23,10 @@ const u8Sub = (a: number, b: number) => {
|
||||
};
|
||||
|
||||
export class XiamiCrypto implements CryptoBase {
|
||||
hasSignature(): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
async isSupported(blob: Blob): Promise<boolean> {
|
||||
const headerBuffer = await blob.slice(0, 0x10).arrayBuffer();
|
||||
const header = new Uint8Array(headerBuffer);
|
||||
|
31
src/decrypt-worker/util/transformBlob.ts
Normal file
31
src/decrypt-worker/util/transformBlob.ts
Normal file
@ -0,0 +1,31 @@
|
||||
import { Transformer, Parakeet, TransformResult, fetchParakeet } from '@jixun/libparakeet';
|
||||
|
||||
export async function transformBlob(
|
||||
blob: Blob,
|
||||
transformerFactory: (p: Parakeet) => Transformer | Promise<Transformer>,
|
||||
parakeet?: Parakeet
|
||||
) {
|
||||
const cleanup: (() => void)[] = [];
|
||||
|
||||
try {
|
||||
const mod = parakeet ?? (await fetchParakeet());
|
||||
const transformer = await transformerFactory(mod);
|
||||
cleanup.push(() => transformer.delete());
|
||||
|
||||
const reader = mod.make.Reader(await blob.arrayBuffer());
|
||||
cleanup.push(() => reader.delete());
|
||||
|
||||
const sink = mod.make.WriterSink();
|
||||
const writer = sink.getWriter();
|
||||
cleanup.push(() => writer.delete());
|
||||
|
||||
const result = transformer.Transform(writer, reader);
|
||||
if (result !== TransformResult.OK) {
|
||||
throw new Error(`transform failed with error: ${TransformResult[result]} (${result})`);
|
||||
}
|
||||
|
||||
return sink.collectBlob();
|
||||
} finally {
|
||||
cleanup.forEach((clean) => clean());
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ import { DECRYPTION_WORKER_ACTION_NAME } from './constants';
|
||||
import type { CryptoFactory } from './crypto/CryptoBase';
|
||||
import { XiamiCrypto } from './crypto/xiami/xiami';
|
||||
import { QMC1Crypto } from './crypto/qmc/qmc_v1';
|
||||
import { fetchParakeet } from '@jixun/libparakeet';
|
||||
|
||||
const bus = new WorkerServerBus();
|
||||
onmessage = bus.onmessage;
|
||||
@ -16,14 +17,27 @@ const decryptorFactories: CryptoFactory[] = [
|
||||
() => new QMC1Crypto(),
|
||||
];
|
||||
|
||||
// Use first 4MiB of the file to perform check.
|
||||
const TEST_FILE_HEADER_LEN = 1024 * 1024 * 4;
|
||||
|
||||
bus.addEventHandler(DECRYPTION_WORKER_ACTION_NAME.DECRYPT, async (blobURI) => {
|
||||
const blob = await fetch(blobURI).then((r) => r.blob());
|
||||
const parakeet = await fetchParakeet();
|
||||
|
||||
for (const factory of decryptorFactories) {
|
||||
const decryptor = factory();
|
||||
if (await decryptor.isSupported(blob)) {
|
||||
const decrypted = await decryptor.decrypt(blob);
|
||||
return { decrypted: URL.createObjectURL(decrypted) };
|
||||
const decryptedBlob = await decryptor.decrypt(blob);
|
||||
|
||||
// Check if we had a successful decryption
|
||||
const header = await decryptedBlob.slice(0, TEST_FILE_HEADER_LEN).arrayBuffer();
|
||||
const audioExt = parakeet.detectAudioExtension(header);
|
||||
if (!decryptor.hasSignature() && audioExt === 'bin') {
|
||||
// skip this decryptor result
|
||||
continue;
|
||||
}
|
||||
|
||||
return { decrypted: URL.createObjectURL(decryptedBlob), ext: audioExt };
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user