2020-07-18 13:58:07 +00:00
|
|
|
import {
|
|
|
|
AudioMimeType,
|
|
|
|
DetectAudioExt,
|
|
|
|
GetArrayBuffer,
|
|
|
|
GetFileInfo,
|
|
|
|
GetMetaCoverURL,
|
|
|
|
GetWebImage,
|
|
|
|
IXAREA_API_ENDPOINT
|
|
|
|
} from "./util";
|
2020-03-01 15:36:16 +00:00
|
|
|
import {QmcMaskCreate58, QmcMaskDetectMflac, QmcMaskDetectMgg, QmcMaskGetDefault} from "./qmcMask";
|
2020-07-16 16:33:10 +00:00
|
|
|
import {fromByteArray as Base64Encode, toByteArray as Base64Decode} from 'base64-js'
|
2020-02-11 06:33:45 +00:00
|
|
|
|
2020-07-18 13:45:53 +00:00
|
|
|
const MetaFlac = require('metaflac-js');
|
|
|
|
|
2020-07-16 16:33:10 +00:00
|
|
|
const ID3Writer = require("browser-id3-writer");
|
2020-04-26 06:32:32 +00:00
|
|
|
|
2020-07-16 16:33:10 +00:00
|
|
|
const iconv = require('iconv-lite');
|
|
|
|
const decode = iconv.decode
|
2020-04-05 11:18:56 +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},
|
2020-04-04 17:01:59 +00:00
|
|
|
"qmc2": {handler: QmcMaskGetDefault, ext: "ogg", detect: false},
|
2020-02-11 06:48:27 +00:00
|
|
|
"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
|
|
|
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) {
|
2020-04-26 06:32:32 +00:00
|
|
|
const keyLen = new DataView(fileData.slice(fileData.length - 4).buffer).getUint32(0, true)
|
|
|
|
const keyPos = fileData.length - 4 - keyLen;
|
|
|
|
audioData = fileData.slice(0, keyPos);
|
2020-02-11 06:33:45 +00:00
|
|
|
seed = handler.handler(audioData);
|
2020-04-26 06:32:32 +00:00
|
|
|
keyData = fileData.slice(keyPos, keyPos + keyLen);
|
2020-02-11 06:33:45 +00:00
|
|
|
if (seed === undefined) seed = await queryKeyInfo(keyData, raw_filename, raw_ext);
|
2020-04-09 03:22:52 +00:00
|
|
|
if (seed === undefined) return {status: false, message: raw_ext + "格式仅提供实验性支持"};
|
2020-02-11 06:33:45 +00:00
|
|
|
} else {
|
|
|
|
audioData = fileData;
|
|
|
|
seed = handler.handler(audioData);
|
2019-07-05 07:05:11 +00:00
|
|
|
}
|
2020-04-05 09:32:19 +00:00
|
|
|
let musicDecoded = seed.Decrypt(audioData);
|
2020-02-11 06:33:45 +00:00
|
|
|
|
2020-04-05 09:32:19 +00:00
|
|
|
const ext = DetectAudioExt(musicDecoded, handler.ext);
|
2020-02-11 07:51:07 +00:00
|
|
|
const mime = AudioMimeType[ext];
|
|
|
|
|
2020-04-05 09:32:19 +00:00
|
|
|
let musicBlob = new Blob([musicDecoded], {type: mime});
|
|
|
|
|
|
|
|
const musicMeta = await musicMetadata.parseBlob(musicBlob);
|
2020-04-06 04:53:16 +00:00
|
|
|
for (let metaIdx in musicMeta.native) {
|
|
|
|
if (musicMeta.native[metaIdx].some(item => item.id === "TCON" && item.value === "(12)")) {
|
2020-08-03 06:03:10 +00:00
|
|
|
console.warn("The metadata is using gbk encoding")
|
2020-04-06 04:53:16 +00:00
|
|
|
musicMeta.common.artist = decode(musicMeta.common.artist, "gbk");
|
|
|
|
musicMeta.common.title = decode(musicMeta.common.title, "gbk");
|
|
|
|
musicMeta.common.album = decode(musicMeta.common.album, "gbk");
|
|
|
|
}
|
2020-04-05 11:18:56 +00:00
|
|
|
}
|
2020-04-06 04:53:16 +00:00
|
|
|
|
2020-04-05 09:32:19 +00:00
|
|
|
const info = GetFileInfo(musicMeta.common.artist, musicMeta.common.title, raw_filename);
|
2020-02-11 06:33:45 +00:00
|
|
|
if (handler.detect) reportKeyUsage(keyData, seed.Matrix128,
|
2020-04-05 09:32:19 +00:00
|
|
|
info.artist, info.title, musicMeta.common.album, raw_filename, raw_ext);
|
|
|
|
|
|
|
|
let imgUrl = GetMetaCoverURL(musicMeta);
|
|
|
|
if (imgUrl === "") {
|
|
|
|
imgUrl = await queryAlbumCoverImage(info.artist, info.title, musicMeta.common.album);
|
2020-07-16 16:33:10 +00:00
|
|
|
if (imgUrl !== "") {
|
|
|
|
const imageInfo = await GetWebImage(imgUrl);
|
|
|
|
if (imageInfo.url !== "") {
|
|
|
|
imgUrl = imageInfo.url
|
2020-07-31 17:10:27 +00:00
|
|
|
try {
|
|
|
|
if (ext === "mp3") {
|
|
|
|
let writer = new ID3Writer(musicDecoded)
|
|
|
|
writer.setFrame('APIC', {
|
|
|
|
type: 3,
|
|
|
|
data: imageInfo.buffer,
|
|
|
|
description: "Cover",
|
|
|
|
})
|
|
|
|
writer.addTag();
|
|
|
|
musicDecoded = writer.arrayBuffer
|
|
|
|
musicBlob = new Blob([musicDecoded], {type: mime});
|
|
|
|
} else if (ext === 'flac') {
|
|
|
|
const writer = new MetaFlac(Buffer.from(musicDecoded))
|
|
|
|
writer.importPictureFromBuffer(Buffer.from(imageInfo.buffer))
|
|
|
|
musicDecoded = writer.save()
|
|
|
|
musicBlob = new Blob([musicDecoded], {type: mime});
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.warn("Error while appending cover image to file " + e)
|
2020-07-16 16:33:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-05 09:32:19 +00:00
|
|
|
}
|
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 07:51:07 +00:00
|
|
|
ext: ext,
|
2020-04-05 09:32:19 +00:00
|
|
|
album: musicMeta.common.album,
|
|
|
|
picture: imgUrl,
|
|
|
|
file: URL.createObjectURL(musicBlob),
|
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) {
|
2020-07-18 13:58:07 +00:00
|
|
|
fetch(IXAREA_API_ENDPOINT + "/qmcmask/usage", {
|
2020-02-11 06:33:45 +00:00
|
|
|
method: "POST",
|
|
|
|
headers: {"Content-Type": "application/json"},
|
|
|
|
body: JSON.stringify({
|
2020-04-26 06:32:32 +00:00
|
|
|
Mask: Base64Encode(new Uint8Array(maskData)), Key: Base64Encode(keyData),
|
2020-02-11 06:33:45 +00:00
|
|
|
Artist: artist, Title: title, Album: album, Filename: filename, Format: format
|
|
|
|
}),
|
|
|
|
}).then().catch()
|
|
|
|
}
|
|
|
|
|
|
|
|
async function queryKeyInfo(keyData, filename, format) {
|
|
|
|
try {
|
2020-07-18 13:58:07 +00:00
|
|
|
const resp = await fetch(IXAREA_API_ENDPOINT + "/qmcmask/query", {
|
2020-02-11 06:33:45 +00:00
|
|
|
method: "POST",
|
|
|
|
headers: {"Content-Type": "application/json"},
|
2020-04-26 06:32:32 +00:00
|
|
|
body: JSON.stringify({Format: format, Key: Base64Encode(keyData), Filename: filename, Type: 44}),
|
2020-02-11 06:33:45 +00:00
|
|
|
});
|
|
|
|
let data = await resp.json();
|
2020-04-26 06:32:32 +00:00
|
|
|
return QmcMaskCreate58(Base64Decode(data.Matrix44));
|
2020-02-11 06:33:45 +00:00
|
|
|
} catch (e) {
|
2020-04-26 03:13:50 +00:00
|
|
|
console.log(e);
|
2020-02-11 06:33:45 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-05 09:32:19 +00:00
|
|
|
|
|
|
|
async function queryAlbumCoverImage(artist, title, album) {
|
2020-07-18 13:58:07 +00:00
|
|
|
const song_query_url = IXAREA_API_ENDPOINT + "/music/qq-cover"
|
2020-04-05 09:32:19 +00:00
|
|
|
try {
|
2020-07-18 14:43:25 +00:00
|
|
|
const params = {Artist: artist, Title: title, Album: album};
|
|
|
|
let _url = song_query_url + "?";
|
|
|
|
for (let pKey in params) {
|
|
|
|
_url += pKey + "=" + encodeURIComponent(params[pKey]) + "&"
|
|
|
|
}
|
|
|
|
const resp = await fetch(_url)
|
2020-07-18 13:45:53 +00:00
|
|
|
if (resp.ok) {
|
|
|
|
let data = await resp.json();
|
|
|
|
return song_query_url + "/" + data.Type + "/" + data.Id
|
2020-04-05 09:32:19 +00:00
|
|
|
}
|
2020-07-18 13:45:53 +00:00
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
2020-04-05 09:32:19 +00:00
|
|
|
}
|
2020-07-18 13:45:53 +00:00
|
|
|
return "";
|
2020-04-23 10:15:47 +00:00
|
|
|
}
|