2020-02-11 06:33:45 +00:00
|
|
|
import {AudioMimeType, GetArrayBuffer, GetCoverURL, GetFileInfo} from "./util";
|
2020-02-11 06:48:27 +00:00
|
|
|
import {QmcMaskCreate58, QmcMaskGetDefault, QmcMaskDetectMgg, QmcMaskDetectMflac} from "./qmcMask";
|
2020-02-11 06:33:45 +00:00
|
|
|
|
2019-09-07 17:50:04 +00:00
|
|
|
const musicMetadata = require("music-metadata-browser");
|
2020-02-11 06:33:45 +00:00
|
|
|
|
|
|
|
const HandlerMap = {
|
2020-02-11 06:48:27 +00:00
|
|
|
"mgg": {handler: QmcMaskDetectMgg, ext: "ogg", detect: true},
|
|
|
|
"mflac": {handler: QmcMaskDetectMflac, ext: "flac", detect: true},
|
|
|
|
"qmc0": {handler: QmcMaskGetDefault, ext: "mp3", detect: false},
|
|
|
|
"qmc3": {handler: QmcMaskGetDefault, ext: "mp3", detect: false},
|
|
|
|
"qmcogg": {handler: QmcMaskGetDefault, ext: "ogg", detect: false},
|
|
|
|
"qmcflac": {handler: QmcMaskGetDefault, ext: "flac", detect: false},
|
|
|
|
"bkcmp3": {handler: QmcMaskGetDefault, ext: "mp3", detect: false},
|
|
|
|
"bkcflac": {handler: QmcMaskGetDefault, ext: "flac", detect: false},
|
|
|
|
"tkm": {handler: QmcMaskGetDefault, ext: "m4a", detect: false}
|
2020-01-27 04:50:24 +00:00
|
|
|
};
|
2020-01-21 11:03:41 +00:00
|
|
|
|
2020-02-10 16:34:26 +00:00
|
|
|
//todo: use header to detect media type
|
|
|
|
export async function Decrypt(file, raw_filename, raw_ext) {
|
2020-02-11 06:33:45 +00:00
|
|
|
if (!(raw_ext in HandlerMap)) return {status: false, message: "File type is incorrect!"};
|
|
|
|
const handler = HandlerMap[raw_ext];
|
|
|
|
|
|
|
|
const fileData = new Uint8Array(await GetArrayBuffer(file));
|
|
|
|
let audioData, seed, keyData;
|
|
|
|
if (handler.detect) {
|
|
|
|
audioData = fileData.slice(0, -0x170);
|
|
|
|
seed = handler.handler(audioData);
|
|
|
|
keyData = fileData.slice(-0x170);
|
|
|
|
if (seed === undefined) seed = await queryKeyInfo(keyData, raw_filename, raw_ext);
|
|
|
|
if (seed === undefined) return {status: false, message: "此格式仅提供实验性支持!"};
|
|
|
|
} else {
|
|
|
|
audioData = fileData;
|
|
|
|
seed = handler.handler(audioData);
|
2019-07-05 07:05:11 +00:00
|
|
|
}
|
2020-02-10 16:34:26 +00:00
|
|
|
const dec = seed.Decrypt(audioData);
|
2020-02-11 06:33:45 +00:00
|
|
|
|
|
|
|
const mime = AudioMimeType[handler.ext];
|
2020-02-10 16:34:26 +00:00
|
|
|
const musicData = new Blob([dec], {type: mime});
|
2019-07-05 07:05:11 +00:00
|
|
|
|
2020-02-11 06:33:45 +00:00
|
|
|
const tag = await musicMetadata.parseBlob(musicData);
|
|
|
|
const info = GetFileInfo(tag.common.artist, tag.common.title, raw_filename);
|
|
|
|
if (handler.detect) reportKeyUsage(keyData, seed.Matrix128,
|
|
|
|
info.artist, info.title, tag.common.album, raw_filename, raw_ext);
|
2019-07-05 07:05:11 +00:00
|
|
|
return {
|
2020-01-21 11:03:41 +00:00
|
|
|
status: true,
|
|
|
|
title: info.title,
|
|
|
|
artist: info.artist,
|
2020-02-11 06:33:45 +00:00
|
|
|
ext: handler.ext,
|
2019-09-08 05:40:32 +00:00
|
|
|
album: tag.common.album,
|
2020-02-11 06:33:45 +00:00
|
|
|
picture: GetCoverURL(tag),
|
2020-02-06 08:18:40 +00:00
|
|
|
file: URL.createObjectURL(musicData),
|
2019-07-05 07:05:11 +00:00
|
|
|
mime: mime
|
|
|
|
}
|
|
|
|
}
|
2020-02-11 06:33:45 +00:00
|
|
|
|
|
|
|
function reportKeyUsage(keyData, maskData, artist, title, album, filename, format) {
|
|
|
|
fetch("https://stats.ixarea.com/collect/qmcmask/usage", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {"Content-Type": "application/json"},
|
|
|
|
body: JSON.stringify({
|
|
|
|
Mask: Array.from(maskData), Key: Array.from(keyData),
|
|
|
|
Artist: artist, Title: title, Album: album, Filename: filename, Format: format
|
|
|
|
}),
|
|
|
|
}).then().catch()
|
|
|
|
}
|
|
|
|
|
|
|
|
async function queryKeyInfo(keyData, filename, format) {
|
|
|
|
try {
|
|
|
|
const resp = await fetch("https://stats.ixarea.com/collect/qmcmask/query", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {"Content-Type": "application/json"},
|
|
|
|
body: JSON.stringify({Format: format, Key: Array.from(keyData), Filename: filename}),
|
|
|
|
});
|
|
|
|
let data = await resp.json();
|
2020-02-11 06:48:27 +00:00
|
|
|
return QmcMaskCreate58(data.Matrix58, data.Super58A, data.Super58B);
|
2020-02-11 06:33:45 +00:00
|
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
}
|