2021-12-18 13:55:31 +00:00
|
|
|
import { QmcMapCipher, QmcRC4Cipher, QmcStaticCipher, QmcStreamCipher } from './qmc_cipher';
|
2021-05-23 17:30:38 +00:00
|
|
|
import {
|
2021-12-15 23:07:51 +00:00
|
|
|
AudioMimeType,
|
|
|
|
GetArrayBuffer,
|
|
|
|
GetCoverFromFile,
|
|
|
|
GetImageFromURL,
|
|
|
|
GetMetaFromFile,
|
|
|
|
SniffAudioExt,
|
|
|
|
WriteMetaToFlac,
|
2021-12-18 13:55:31 +00:00
|
|
|
WriteMetaToMp3,
|
|
|
|
} from '@/decrypt/utils';
|
|
|
|
import { parseBlob as metaParseBlob } from 'music-metadata-browser';
|
|
|
|
import { DecryptQMCWasm } from './qmc_wasm';
|
2020-02-11 06:33:45 +00:00
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
import iconv from 'iconv-lite';
|
|
|
|
import { DecryptResult } from '@/decrypt/entity';
|
|
|
|
import { queryAlbumCover } from '@/utils/api';
|
|
|
|
import { QmcDeriveKey } from '@/decrypt/qmc_key';
|
2020-04-05 11:18:56 +00:00
|
|
|
|
2021-05-23 22:50:20 +00:00
|
|
|
interface Handler {
|
2021-12-18 13:55:31 +00:00
|
|
|
ext: string;
|
|
|
|
version: number;
|
2021-05-23 22:50:20 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 04:47:06 +00:00
|
|
|
export const HandlerMap: { [key: string]: Handler } = {
|
2021-12-18 13:55:31 +00:00
|
|
|
mgg: { ext: 'ogg', version: 2 },
|
|
|
|
mgg1: { ext: 'ogg', version: 2 },
|
|
|
|
mflac: { ext: 'flac', version: 2 },
|
|
|
|
mflac0: { ext: 'flac', version: 2 },
|
2021-12-15 23:07:51 +00:00
|
|
|
|
|
|
|
// qmcflac / qmcogg:
|
|
|
|
// 有可能是 v2 加密但混用同一个后缀名。
|
2021-12-18 13:55:31 +00:00
|
|
|
qmcflac: { ext: 'flac', version: 2 },
|
|
|
|
qmcogg: { ext: 'ogg', version: 2 },
|
|
|
|
|
|
|
|
qmc0: { ext: 'mp3', version: 1 },
|
|
|
|
qmc2: { ext: 'ogg', version: 1 },
|
|
|
|
qmc3: { ext: 'mp3', version: 1 },
|
|
|
|
bkcmp3: { ext: 'mp3', version: 1 },
|
|
|
|
bkcflac: { ext: 'flac', version: 1 },
|
|
|
|
tkm: { ext: 'm4a', version: 1 },
|
|
|
|
'666c6163': { ext: 'flac', version: 1 },
|
|
|
|
'6d7033': { ext: 'mp3', version: 1 },
|
|
|
|
'6f6767': { ext: 'ogg', version: 1 },
|
|
|
|
'6d3461': { ext: 'm4a', version: 1 },
|
|
|
|
'776176': { ext: 'wav', version: 1 },
|
2021-12-15 19:59:06 +00:00
|
|
|
};
|
2021-12-15 13:53:50 +00:00
|
|
|
|
2021-06-03 04:47:06 +00:00
|
|
|
export async function Decrypt(file: Blob, raw_filename: string, raw_ext: string): Promise<DecryptResult> {
|
2021-12-15 23:07:51 +00:00
|
|
|
if (!(raw_ext in HandlerMap)) throw `Qmc cannot handle type: ${raw_ext}`;
|
|
|
|
const handler = HandlerMap[raw_ext];
|
2021-12-18 13:55:31 +00:00
|
|
|
let { version } = handler;
|
2021-12-15 23:07:51 +00:00
|
|
|
|
|
|
|
const fileBuffer = await GetArrayBuffer(file);
|
|
|
|
let musicDecoded: Uint8Array | undefined;
|
|
|
|
|
2021-12-16 22:24:21 +00:00
|
|
|
if (version === 2 && globalThis.WebAssembly) {
|
2021-12-18 13:55:31 +00:00
|
|
|
console.log('qmc: using wasm decoder');
|
2021-12-16 22:24:21 +00:00
|
|
|
const v2Decrypted = await DecryptQMCWasm(fileBuffer);
|
2021-12-15 23:07:51 +00:00
|
|
|
// 如果 v2 检测失败,降级到 v1 再尝试一次
|
|
|
|
if (v2Decrypted) {
|
|
|
|
musicDecoded = v2Decrypted;
|
2021-12-15 22:07:05 +00:00
|
|
|
}
|
2021-12-15 23:07:51 +00:00
|
|
|
}
|
2021-12-16 22:24:21 +00:00
|
|
|
if (!musicDecoded) {
|
|
|
|
// may throw error
|
2021-12-18 13:55:31 +00:00
|
|
|
console.log('qmc: using js decoder');
|
|
|
|
const d = new QmcDecoder(new Uint8Array(fileBuffer));
|
|
|
|
musicDecoded = d.decrypt();
|
2021-12-15 23:07:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const ext = SniffAudioExt(musicDecoded, handler.ext);
|
|
|
|
const mime = AudioMimeType[ext];
|
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
let musicBlob = new Blob([musicDecoded], { type: mime });
|
2021-12-15 23:07:51 +00:00
|
|
|
|
|
|
|
const musicMeta = await metaParseBlob(musicBlob);
|
|
|
|
for (let metaIdx in musicMeta.native) {
|
2021-12-18 13:55:31 +00:00
|
|
|
if (!musicMeta.native.hasOwnProperty(metaIdx)) continue;
|
|
|
|
if (musicMeta.native[metaIdx].some((item) => item.id === 'TCON' && item.value === '(12)')) {
|
|
|
|
console.warn('try using gbk encoding to decode meta');
|
|
|
|
musicMeta.common.artist = iconv.decode(new Buffer(musicMeta.common.artist ?? ''), 'gbk');
|
|
|
|
musicMeta.common.title = iconv.decode(new Buffer(musicMeta.common.title ?? ''), 'gbk');
|
|
|
|
musicMeta.common.album = iconv.decode(new Buffer(musicMeta.common.album ?? ''), 'gbk');
|
2021-12-15 19:59:06 +00:00
|
|
|
}
|
2021-12-15 23:07:51 +00:00
|
|
|
}
|
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
const info = GetMetaFromFile(raw_filename, musicMeta.common.title, musicMeta.common.artist);
|
2021-12-15 23:07:51 +00:00
|
|
|
|
|
|
|
let imgUrl = GetCoverFromFile(musicMeta);
|
|
|
|
if (!imgUrl) {
|
|
|
|
imgUrl = await getCoverImage(info.title, info.artist, musicMeta.common.album);
|
|
|
|
if (imgUrl) {
|
|
|
|
const imageInfo = await GetImageFromURL(imgUrl);
|
|
|
|
if (imageInfo) {
|
2021-12-18 13:55:31 +00:00
|
|
|
imgUrl = imageInfo.url;
|
2021-12-15 23:07:51 +00:00
|
|
|
try {
|
2021-12-18 13:55:31 +00:00
|
|
|
const newMeta = { picture: imageInfo.buffer, title: info.title, artists: info.artist?.split(' _ ') };
|
|
|
|
if (ext === 'mp3') {
|
|
|
|
musicDecoded = WriteMetaToMp3(Buffer.from(musicDecoded), newMeta, musicMeta);
|
|
|
|
musicBlob = new Blob([musicDecoded], { type: mime });
|
2021-12-15 23:07:51 +00:00
|
|
|
} else if (ext === 'flac') {
|
2021-12-18 13:55:31 +00:00
|
|
|
musicDecoded = WriteMetaToFlac(Buffer.from(musicDecoded), newMeta, musicMeta);
|
|
|
|
musicBlob = new Blob([musicDecoded], { type: mime });
|
2021-12-15 23:07:51 +00:00
|
|
|
} else {
|
2021-12-18 13:55:31 +00:00
|
|
|
console.info('writing metadata for ' + ext + ' is not being supported for now');
|
2021-12-15 23:07:51 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2021-12-18 13:55:31 +00:00
|
|
|
console.warn('Error while appending cover image to file ' + e);
|
2020-04-06 04:53:16 +00:00
|
|
|
}
|
2021-12-15 23:07:51 +00:00
|
|
|
}
|
2020-04-05 11:18:56 +00:00
|
|
|
}
|
2021-12-15 23:07:51 +00:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
title: info.title,
|
|
|
|
artist: info.artist,
|
|
|
|
ext: ext,
|
|
|
|
album: musicMeta.common.album,
|
|
|
|
picture: imgUrl,
|
|
|
|
file: URL.createObjectURL(musicBlob),
|
|
|
|
blob: musicBlob,
|
2021-12-18 13:55:31 +00:00
|
|
|
mime: mime,
|
|
|
|
};
|
2019-07-05 07:05:11 +00:00
|
|
|
}
|
2020-02-11 06:33:45 +00:00
|
|
|
|
2021-05-24 15:48:52 +00:00
|
|
|
async function getCoverImage(title: string, artist?: string, album?: string): Promise<string> {
|
2021-12-18 13:55:31 +00:00
|
|
|
const song_query_url = 'https://stats.ixarea.com/apis' + '/music/qq-cover';
|
2021-12-15 23:07:51 +00:00
|
|
|
try {
|
2021-12-18 13:55:31 +00:00
|
|
|
const data = await queryAlbumCover(title, artist, album);
|
|
|
|
return `${song_query_url}/${data.Type}/${data.Id}`;
|
2021-12-15 23:07:51 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.warn(e);
|
|
|
|
}
|
2021-12-18 13:55:31 +00:00
|
|
|
return '';
|
2020-04-23 10:15:47 +00:00
|
|
|
}
|
2021-12-16 22:07:43 +00:00
|
|
|
|
|
|
|
export class QmcDecoder {
|
2021-12-18 13:55:31 +00:00
|
|
|
private static readonly BYTE_COMMA = ','.charCodeAt(0);
|
|
|
|
file: Uint8Array;
|
|
|
|
size: number;
|
|
|
|
decoded: boolean = false;
|
|
|
|
audioSize?: number;
|
|
|
|
cipher?: QmcStreamCipher;
|
2021-12-16 22:07:43 +00:00
|
|
|
|
|
|
|
constructor(file: Uint8Array) {
|
2021-12-18 13:55:31 +00:00
|
|
|
this.file = file;
|
|
|
|
this.size = file.length;
|
|
|
|
this.searchKey();
|
2021-12-16 22:07:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
decrypt(): Uint8Array {
|
|
|
|
if (!this.cipher) {
|
2021-12-18 13:55:31 +00:00
|
|
|
throw new Error('no cipher found');
|
2021-12-16 22:07:43 +00:00
|
|
|
}
|
|
|
|
if (!this.audioSize || this.audioSize <= 0) {
|
2021-12-18 13:55:31 +00:00
|
|
|
throw new Error('invalid audio size');
|
2021-12-16 22:07:43 +00:00
|
|
|
}
|
2021-12-18 13:55:31 +00:00
|
|
|
const audioBuf = this.file.subarray(0, this.audioSize);
|
2021-12-16 22:07:43 +00:00
|
|
|
|
|
|
|
if (!this.decoded) {
|
2021-12-18 13:55:31 +00:00
|
|
|
this.cipher.decrypt(audioBuf, 0);
|
|
|
|
this.decoded = true;
|
2021-12-16 22:07:43 +00:00
|
|
|
}
|
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
return audioBuf;
|
2021-12-16 22:07:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private searchKey() {
|
2021-12-17 00:41:44 +00:00
|
|
|
const last4Byte = this.file.slice(-4);
|
2021-12-18 13:55:31 +00:00
|
|
|
const textEnc = new TextDecoder();
|
2021-12-16 22:07:43 +00:00
|
|
|
if (textEnc.decode(last4Byte) === 'QTag') {
|
2021-12-18 13:55:31 +00:00
|
|
|
const sizeBuf = this.file.slice(-8, -4);
|
|
|
|
const sizeView = new DataView(sizeBuf.buffer, sizeBuf.byteOffset);
|
|
|
|
const keySize = sizeView.getUint32(0, false);
|
|
|
|
this.audioSize = this.size - keySize - 8;
|
|
|
|
const rawKey = this.file.subarray(this.audioSize, this.size - 8);
|
|
|
|
const keyEnd = rawKey.findIndex((v) => v == QmcDecoder.BYTE_COMMA);
|
|
|
|
this.setCipher(rawKey.subarray(0, keyEnd));
|
2021-12-16 22:07:43 +00:00
|
|
|
} else {
|
|
|
|
const sizeView = new DataView(last4Byte.buffer, last4Byte.byteOffset);
|
2021-12-18 13:55:31 +00:00
|
|
|
const keySize = sizeView.getUint32(0, true);
|
2021-12-16 22:07:43 +00:00
|
|
|
if (keySize < 0x300) {
|
2021-12-18 13:55:31 +00:00
|
|
|
this.audioSize = this.size - keySize - 4;
|
|
|
|
const rawKey = this.file.subarray(this.audioSize, this.size - 4);
|
|
|
|
this.setCipher(rawKey);
|
2021-12-16 22:07:43 +00:00
|
|
|
} else {
|
2021-12-18 13:55:31 +00:00
|
|
|
this.audioSize = this.size;
|
|
|
|
this.cipher = new QmcStaticCipher();
|
2021-12-16 22:07:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-18 13:35:22 +00:00
|
|
|
private setCipher(keyRaw: Uint8Array) {
|
2021-12-18 13:55:31 +00:00
|
|
|
const keyDec = QmcDeriveKey(keyRaw);
|
2021-12-18 13:35:22 +00:00
|
|
|
if (keyDec.length > 300) {
|
2021-12-18 13:55:31 +00:00
|
|
|
this.cipher = new QmcRC4Cipher(keyDec);
|
2021-12-18 13:35:22 +00:00
|
|
|
} else {
|
2021-12-18 13:55:31 +00:00
|
|
|
this.cipher = new QmcMapCipher(keyDec);
|
2021-12-18 13:35:22 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-16 22:07:43 +00:00
|
|
|
}
|