2021-05-23 21:57:04 +00:00
|
|
|
import {QmcMask, QmcMaskDetectMflac, QmcMaskDetectMgg, QmcMaskGetDefault} from "./qmcMask";
|
2020-07-16 16:33:10 +00:00
|
|
|
import {fromByteArray as Base64Encode, toByteArray as Base64Decode} from 'base64-js'
|
2021-05-23 17:30:38 +00:00
|
|
|
import {
|
|
|
|
AudioMimeType,
|
|
|
|
GetArrayBuffer,
|
|
|
|
GetCoverFromFile,
|
|
|
|
GetImageFromURL,
|
2021-05-23 18:55:42 +00:00
|
|
|
GetMetaFromFile, IXAREA_API_ENDPOINT,
|
|
|
|
SniffAudioExt, WriteMetaToFlac, WriteMetaToMp3
|
2021-05-23 17:30:38 +00:00
|
|
|
} from "@/decrypt/utils.ts";
|
2021-05-23 15:47:01 +00:00
|
|
|
import {parseBlob as metaParseBlob} from "music-metadata-browser";
|
2020-02-11 06:33:45 +00:00
|
|
|
|
2020-04-26 06:32:32 +00:00
|
|
|
|
2021-05-23 22:50:20 +00:00
|
|
|
import iconv from "iconv-lite";
|
2021-05-24 07:05:14 +00:00
|
|
|
import {DecryptResult} from "@/decrypt/entity";
|
2020-04-05 11:18:56 +00:00
|
|
|
|
2021-05-23 22:50:20 +00:00
|
|
|
interface Handler {
|
|
|
|
ext: string
|
|
|
|
detect: boolean
|
|
|
|
|
|
|
|
handler(data?: Uint8Array): QmcMask | undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
const HandlerMap: { [key: string]: Handler } = {
|
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},
|
2020-09-19 04:03:02 +00:00
|
|
|
"tkm": {handler: QmcMaskGetDefault, ext: "m4a", detect: false},
|
|
|
|
"666c6163": {handler: QmcMaskGetDefault, ext: "flac", detect: false},
|
|
|
|
"6d7033": {handler: QmcMaskGetDefault, ext: "mp3", detect: false},
|
|
|
|
"6f6767": {handler: QmcMaskGetDefault, ext: "ogg", detect: false},
|
|
|
|
"6d3461": {handler: QmcMaskGetDefault, ext: "m4a", detect: false},
|
|
|
|
"776176": {handler: QmcMaskGetDefault, ext: "wav", detect: false}
|
2020-01-27 04:50:24 +00:00
|
|
|
};
|
2020-01-21 11:03:41 +00:00
|
|
|
|
2021-05-24 07:05:14 +00:00
|
|
|
export async function Decrypt(file: File, raw_filename: string, raw_ext: string): Promise<DecryptResult> {
|
2021-05-23 22:50:20 +00:00
|
|
|
if (!(raw_ext in HandlerMap)) throw "File type is incorrect!";
|
2020-02-11 06:33:45 +00:00
|
|
|
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);
|
2021-05-23 22:50:20 +00:00
|
|
|
if (!seed) seed = await queryKeyInfo(keyData, raw_filename, raw_ext);
|
|
|
|
if (!seed) throw raw_ext + "格式仅提供实验性支持";
|
2020-02-11 06:33:45 +00:00
|
|
|
} else {
|
|
|
|
audioData = fileData;
|
2021-05-23 22:50:20 +00:00
|
|
|
seed = handler.handler(audioData) as QmcMask;
|
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
|
|
|
|
2021-05-23 13:40:43 +00:00
|
|
|
const ext = SniffAudioExt(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});
|
|
|
|
|
2021-05-23 15:47:01 +00:00
|
|
|
const musicMeta = await metaParseBlob(musicBlob);
|
2020-04-06 04:53:16 +00:00
|
|
|
for (let metaIdx in musicMeta.native) {
|
2021-05-23 22:50:20 +00:00
|
|
|
if (!musicMeta.native.hasOwnProperty(metaIdx)) continue
|
2020-04-06 04:53:16 +00:00
|
|
|
if (musicMeta.native[metaIdx].some(item => item.id === "TCON" && item.value === "(12)")) {
|
2021-05-23 22:50:20 +00:00
|
|
|
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");
|
2020-04-06 04:53:16 +00:00
|
|
|
}
|
2020-04-05 11:18:56 +00:00
|
|
|
}
|
2020-04-06 04:53:16 +00:00
|
|
|
|
2021-05-23 15:06:21 +00:00
|
|
|
const info = GetMetaFromFile(raw_filename, musicMeta.common.title, musicMeta.common.artist)
|
2021-05-23 22:50:20 +00:00
|
|
|
if (keyData) reportKeyUsage(keyData, seed.getMatrix128(),
|
|
|
|
raw_filename, raw_ext, info.title, info.artist, musicMeta.common.album);
|
2020-04-05 09:32:19 +00:00
|
|
|
|
2021-05-23 15:06:21 +00:00
|
|
|
let imgUrl = GetCoverFromFile(musicMeta);
|
|
|
|
if (!imgUrl) {
|
2021-05-23 22:50:20 +00:00
|
|
|
imgUrl = await queryAlbumCoverImage(info.title, info.artist, musicMeta.common.album);
|
2020-07-16 16:33:10 +00:00
|
|
|
if (imgUrl !== "") {
|
2021-05-23 17:30:38 +00:00
|
|
|
const imageInfo = await GetImageFromURL(imgUrl);
|
|
|
|
if (imageInfo) {
|
2020-07-16 16:33:10 +00:00
|
|
|
imgUrl = imageInfo.url
|
2020-07-31 17:10:27 +00:00
|
|
|
try {
|
2021-05-23 22:50:20 +00:00
|
|
|
const newMeta = {picture: imageInfo.buffer, title: info.title, artists: info.artist?.split(" _ ")}
|
2020-07-31 17:10:27 +00:00
|
|
|
if (ext === "mp3") {
|
2021-05-23 22:50:20 +00:00
|
|
|
musicDecoded = WriteMetaToMp3(Buffer.from(musicDecoded), newMeta, musicMeta)
|
2020-07-31 17:10:27 +00:00
|
|
|
musicBlob = new Blob([musicDecoded], {type: mime});
|
|
|
|
} else if (ext === 'flac') {
|
2021-05-23 18:55:42 +00:00
|
|
|
musicDecoded = WriteMetaToFlac(Buffer.from(musicDecoded), newMeta, musicMeta)
|
2020-07-31 17:10:27 +00:00
|
|
|
musicBlob = new Blob([musicDecoded], {type: mime});
|
2020-12-05 18:32:57 +00:00
|
|
|
} else {
|
|
|
|
console.info("writing metadata for " + ext + " is not being supported for now")
|
2020-07-31 17:10:27 +00:00
|
|
|
}
|
|
|
|
} 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
|
|
|
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
|
|
|
|
2021-05-23 22:50:20 +00:00
|
|
|
|
|
|
|
function reportKeyUsage(keyData: Uint8Array, maskData: number[], filename: string, format: string, title: string, artist?: string, album?: string) {
|
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()
|
|
|
|
}
|
|
|
|
|
2021-05-23 22:50:20 +00:00
|
|
|
async function queryKeyInfo(keyData: Uint8Array, filename: string, format: string) {
|
2020-02-11 06:33:45 +00:00
|
|
|
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();
|
2021-05-23 21:57:04 +00:00
|
|
|
return new QmcMask(Base64Decode(data.Matrix44));
|
2020-02-11 06:33:45 +00:00
|
|
|
} catch (e) {
|
2021-05-24 14:19:37 +00:00
|
|
|
console.warn(e);
|
2020-02-11 06:33:45 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-05 09:32:19 +00:00
|
|
|
|
2021-05-23 22:50:20 +00:00
|
|
|
async function queryAlbumCoverImage(title: string, artist?: string, album?: string) {
|
|
|
|
const song_query_url = "https://stats.ixarea.com/apis" + "/music/qq-cover"
|
2020-04-05 09:32:19 +00:00
|
|
|
try {
|
2021-05-23 22:50:20 +00:00
|
|
|
const params = new URLSearchParams([["Title", title], ["Artist", artist ?? ""], ["Album", album ?? ""]])
|
|
|
|
const resp = await fetch(`${song_query_url}?${params.toString()}`)
|
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) {
|
2021-05-24 14:19:37 +00:00
|
|
|
console.warn(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
|
|
|
}
|