feat: added audio ext detection

This commit is contained in:
鲁树人 2023-05-14 21:57:18 +01:00
parent c89be532a8
commit 175d6d0645
7 changed files with 67 additions and 32 deletions

View File

@ -14,7 +14,7 @@
"@chakra-ui/react": "^2.6.1", "@chakra-ui/react": "^2.6.1",
"@emotion/react": "^11.11.0", "@emotion/react": "^11.11.0",
"@emotion/styled": "^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", "@reduxjs/toolkit": "^1.9.5",
"framer-motion": "^10.12.8", "framer-motion": "^10.12.8",
"nanoid": "^4.0.2", "nanoid": "^4.0.2",

View File

@ -14,8 +14,8 @@ dependencies:
specifier: ^11.11.0 specifier: ^11.11.0
version: 11.11.0(@emotion/react@11.11.0)(@types/react@18.0.28)(react@18.2.0) version: 11.11.0(@emotion/react@11.11.0)(@types/react@18.0.28)(react@18.2.0)
'@jixun/libparakeet': '@jixun/libparakeet':
specifier: 0.0.0-exp.14 specifier: 0.0.0-exp.15
version: 0.0.0-exp.14 version: 0.0.0-exp.15
'@reduxjs/toolkit': '@reduxjs/toolkit':
specifier: ^1.9.5 specifier: ^1.9.5
version: 1.9.5(react-redux@8.0.5)(react@18.2.0) 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==} resolution: {integrity: sha512-ZnQMnLV4e7hDlUvw8H+U8ASL02SS2Gn6+9Ac3wGGLIe7+je2AeAOxPY+izIPJDfFDb7eDjev0Us8MO1iFRN8hA==}
dev: true dev: true
/@jixun/libparakeet@0.0.0-exp.14: /@jixun/libparakeet@0.0.0-exp.15:
resolution: {integrity: sha512-PJCV+1v+6EM/TVomuKqxvm0GSZE58la7JeU690Jem+BGP4LXAE9aaDHvSljEbcokhar4NXI0j5Ggxrq/PaDEkw==} resolution: {integrity: sha512-Le2Gl/2V6BsrK8NcFTtaelUkjqewCmDT3xZgUxOKjOo2lK1qvj1AtxarhfDtsOQVjvItiibuRPaRFPpLmWJe9g==}
dev: false dev: false
/@jridgewell/gen-mapping@0.3.3: /@jridgewell/gen-mapping@0.3.3:

View File

@ -1,4 +1,8 @@
export interface CryptoBase { export interface CryptoBase {
/**
* When returning false, a successful decryption should be checked by its decrypted content instead.
*/
hasSignature(): boolean;
isSupported(blob: Blob): Promise<boolean>; isSupported(blob: Blob): Promise<boolean>;
decrypt(blob: Blob): Promise<Blob>; decrypt(blob: Blob): Promise<Blob>;
} }

View File

@ -1,5 +1,5 @@
import { transformBlob } from '~/decrypt-worker/util/transformBlob';
import type { CryptoBase } from '../CryptoBase'; import type { CryptoBase } from '../CryptoBase';
import { loadLibParakeet, factory, BlobSink, createArrayBufferReader, TransformResult } from '@jixun/libparakeet';
const key = new Uint8Array([ const key = new Uint8Array([
0x77, 0x48, 0x32, 0x73, 0xde, 0xf2, 0xc0, 0xc8, 0x95, 0xec, 0x30, 0xb2, 0x51, 0xc3, 0xe1, 0xa0, 0x9e, 0xe6, 0x9d, 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 { export class QMC1Crypto implements CryptoBase {
async isSupported(_blob: Blob): Promise<boolean> { hasSignature(): boolean {
return false;
}
async isSupported(): Promise<boolean> {
return true; return true;
} }
async decrypt(blob: Blob): Promise<Blob> { async decrypt(blob: Blob): Promise<Blob> {
const cleanup: (() => void)[] = []; return transformBlob(blob, (p) => p.make.QMCv1(key));
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());
}
} }
} }

View File

@ -23,6 +23,10 @@ const u8Sub = (a: number, b: number) => {
}; };
export class XiamiCrypto implements CryptoBase { export class XiamiCrypto implements CryptoBase {
hasSignature(): boolean {
return true;
}
async isSupported(blob: Blob): Promise<boolean> { async isSupported(blob: Blob): Promise<boolean> {
const headerBuffer = await blob.slice(0, 0x10).arrayBuffer(); const headerBuffer = await blob.slice(0, 0x10).arrayBuffer();
const header = new Uint8Array(headerBuffer); const header = new Uint8Array(headerBuffer);

View 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());
}
}

View File

@ -4,6 +4,7 @@ import { DECRYPTION_WORKER_ACTION_NAME } from './constants';
import type { CryptoFactory } from './crypto/CryptoBase'; import type { CryptoFactory } from './crypto/CryptoBase';
import { XiamiCrypto } from './crypto/xiami/xiami'; import { XiamiCrypto } from './crypto/xiami/xiami';
import { QMC1Crypto } from './crypto/qmc/qmc_v1'; import { QMC1Crypto } from './crypto/qmc/qmc_v1';
import { fetchParakeet } from '@jixun/libparakeet';
const bus = new WorkerServerBus(); const bus = new WorkerServerBus();
onmessage = bus.onmessage; onmessage = bus.onmessage;
@ -16,14 +17,27 @@ const decryptorFactories: CryptoFactory[] = [
() => new QMC1Crypto(), () => 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) => { bus.addEventHandler(DECRYPTION_WORKER_ACTION_NAME.DECRYPT, async (blobURI) => {
const blob = await fetch(blobURI).then((r) => r.blob()); const blob = await fetch(blobURI).then((r) => r.blob());
const parakeet = await fetchParakeet();
for (const factory of decryptorFactories) { for (const factory of decryptorFactories) {
const decryptor = factory(); const decryptor = factory();
if (await decryptor.isSupported(blob)) { if (await decryptor.isSupported(blob)) {
const decrypted = await decryptor.decrypt(blob); const decryptedBlob = await decryptor.decrypt(blob);
return { decrypted: URL.createObjectURL(decrypted) };
// 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 };
} }
} }