2023-05-09 00:22:00 +00:00
|
|
|
import { WorkerServerBus } from '~/util/WorkerEventBus';
|
2023-05-08 16:36:10 +00:00
|
|
|
import { DECRYPTION_WORKER_ACTION_NAME } from './constants';
|
|
|
|
|
2023-05-09 00:23:35 +00:00
|
|
|
import type { CryptoFactory } from './crypto/CryptoBase';
|
|
|
|
import { XiamiCrypto } from './crypto/xiami/xiami';
|
2023-05-13 14:55:02 +00:00
|
|
|
import { QMC1Crypto } from './crypto/qmc/qmc_v1';
|
2023-05-14 20:57:18 +00:00
|
|
|
import { fetchParakeet } from '@jixun/libparakeet';
|
2023-05-09 00:23:35 +00:00
|
|
|
|
2023-05-08 16:36:10 +00:00
|
|
|
const bus = new WorkerServerBus();
|
|
|
|
onmessage = bus.onmessage;
|
|
|
|
|
2023-05-09 00:23:35 +00:00
|
|
|
const decryptorFactories: CryptoFactory[] = [
|
|
|
|
// Xiami (*.xm)
|
|
|
|
() => new XiamiCrypto(),
|
2023-05-13 19:04:55 +00:00
|
|
|
|
|
|
|
// QMCv1 (*.qmcflac)
|
2023-05-13 14:55:02 +00:00
|
|
|
() => new QMC1Crypto(),
|
2023-05-09 00:23:35 +00:00
|
|
|
];
|
|
|
|
|
2023-05-14 20:57:18 +00:00
|
|
|
// Use first 4MiB of the file to perform check.
|
|
|
|
const TEST_FILE_HEADER_LEN = 1024 * 1024 * 4;
|
|
|
|
|
2023-05-08 16:36:10 +00:00
|
|
|
bus.addEventHandler(DECRYPTION_WORKER_ACTION_NAME.DECRYPT, async (blobURI) => {
|
2023-05-09 00:23:35 +00:00
|
|
|
const blob = await fetch(blobURI).then((r) => r.blob());
|
2023-05-14 20:57:18 +00:00
|
|
|
const parakeet = await fetchParakeet();
|
2023-05-09 00:23:35 +00:00
|
|
|
|
|
|
|
for (const factory of decryptorFactories) {
|
|
|
|
const decryptor = factory();
|
|
|
|
if (await decryptor.isSupported(blob)) {
|
2023-05-14 20:57:18 +00:00
|
|
|
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 };
|
2023-05-09 00:23:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error('could not decrypt file: no working decryptor found');
|
2023-05-08 16:36:10 +00:00
|
|
|
});
|