1
0
forked from um/web

refactor(decrypt/qmc): typescript

This commit is contained in:
Emmm Monster 2021-05-24 06:50:20 +08:00
parent aca1c11332
commit b3c6fe2f24
No known key found for this signature in database
GPG Key ID: C98279C83FB50DB9

View File

@ -11,10 +11,16 @@ import {
import {parseBlob as metaParseBlob} from "music-metadata-browser"; import {parseBlob as metaParseBlob} from "music-metadata-browser";
const iconv = require('iconv-lite'); import iconv from "iconv-lite";
const decode = iconv.decode
const HandlerMap = { interface Handler {
ext: string
detect: boolean
handler(data?: Uint8Array): QmcMask | undefined
}
const HandlerMap: { [key: string]: Handler } = {
"mgg": {handler: QmcMaskDetectMgg, ext: "ogg", detect: true}, "mgg": {handler: QmcMaskDetectMgg, ext: "ogg", detect: true},
"mflac": {handler: QmcMaskDetectMflac, ext: "flac", detect: true}, "mflac": {handler: QmcMaskDetectMflac, ext: "flac", detect: true},
"qmc0": {handler: QmcMaskGetDefault, ext: "mp3", detect: false}, "qmc0": {handler: QmcMaskGetDefault, ext: "mp3", detect: false},
@ -32,8 +38,8 @@ const HandlerMap = {
"776176": {handler: QmcMaskGetDefault, ext: "wav", detect: false} "776176": {handler: QmcMaskGetDefault, ext: "wav", detect: false}
}; };
export async function Decrypt(file, raw_filename, raw_ext) { export async function Decrypt(file: File, raw_filename: string, raw_ext: string) {
if (!(raw_ext in HandlerMap)) return {status: false, message: "File type is incorrect!"}; if (!(raw_ext in HandlerMap)) throw "File type is incorrect!";
const handler = HandlerMap[raw_ext]; const handler = HandlerMap[raw_ext];
const fileData = new Uint8Array(await GetArrayBuffer(file)); const fileData = new Uint8Array(await GetArrayBuffer(file));
@ -44,11 +50,11 @@ export async function Decrypt(file, raw_filename, raw_ext) {
audioData = fileData.slice(0, keyPos); audioData = fileData.slice(0, keyPos);
seed = handler.handler(audioData); seed = handler.handler(audioData);
keyData = fileData.slice(keyPos, keyPos + keyLen); keyData = fileData.slice(keyPos, keyPos + keyLen);
if (seed === undefined) seed = await queryKeyInfo(keyData, raw_filename, raw_ext); if (!seed) seed = await queryKeyInfo(keyData, raw_filename, raw_ext);
if (seed === undefined) return {status: false, message: raw_ext + "格式仅提供实验性支持"}; if (!seed) throw raw_ext + "格式仅提供实验性支持";
} else { } else {
audioData = fileData; audioData = fileData;
seed = handler.handler(audioData); seed = handler.handler(audioData) as QmcMask;
} }
let musicDecoded = seed.Decrypt(audioData); let musicDecoded = seed.Decrypt(audioData);
@ -59,29 +65,30 @@ export async function Decrypt(file, raw_filename, raw_ext) {
const musicMeta = await metaParseBlob(musicBlob); const musicMeta = await metaParseBlob(musicBlob);
for (let metaIdx in musicMeta.native) { for (let metaIdx in musicMeta.native) {
if (!musicMeta.native.hasOwnProperty(metaIdx)) continue
if (musicMeta.native[metaIdx].some(item => item.id === "TCON" && item.value === "(12)")) { if (musicMeta.native[metaIdx].some(item => item.id === "TCON" && item.value === "(12)")) {
console.warn("The metadata is using gbk encoding") console.warn("try using gbk encoding to decode meta")
musicMeta.common.artist = decode(musicMeta.common.artist, "gbk"); musicMeta.common.artist = iconv.decode(new Buffer(musicMeta.common.artist ?? ""), "gbk");
musicMeta.common.title = decode(musicMeta.common.title, "gbk"); musicMeta.common.title = iconv.decode(new Buffer(musicMeta.common.title ?? ""), "gbk");
musicMeta.common.album = decode(musicMeta.common.album, "gbk"); musicMeta.common.album = iconv.decode(new Buffer(musicMeta.common.album ?? ""), "gbk");
} }
} }
const info = GetMetaFromFile(raw_filename, musicMeta.common.title, musicMeta.common.artist) const info = GetMetaFromFile(raw_filename, musicMeta.common.title, musicMeta.common.artist)
if (handler.detect) reportKeyUsage(keyData, seed.getMatrix128(), if (keyData) reportKeyUsage(keyData, seed.getMatrix128(),
info.artist, info.title, musicMeta.common.album, raw_filename, raw_ext); raw_filename, raw_ext, info.title, info.artist, musicMeta.common.album);
let imgUrl = GetCoverFromFile(musicMeta); let imgUrl = GetCoverFromFile(musicMeta);
if (!imgUrl) { if (!imgUrl) {
imgUrl = await queryAlbumCoverImage(info.artist, info.title, musicMeta.common.album); imgUrl = await queryAlbumCoverImage(info.title, info.artist, musicMeta.common.album);
if (imgUrl !== "") { if (imgUrl !== "") {
const imageInfo = await GetImageFromURL(imgUrl); const imageInfo = await GetImageFromURL(imgUrl);
if (imageInfo) { if (imageInfo) {
imgUrl = imageInfo.url imgUrl = imageInfo.url
try { try {
const newMeta = {picture: imageInfo.buffer, title: info.title, artists: info.artist.split(" _ ")} const newMeta = {picture: imageInfo.buffer, title: info.title, artists: info.artist?.split(" _ ")}
if (ext === "mp3") { if (ext === "mp3") {
musicDecoded = WriteMetaToMp3(musicDecoded, newMeta, musicMeta) musicDecoded = WriteMetaToMp3(Buffer.from(musicDecoded), newMeta, musicMeta)
musicBlob = new Blob([musicDecoded], {type: mime}); musicBlob = new Blob([musicDecoded], {type: mime});
} else if (ext === 'flac') { } else if (ext === 'flac') {
musicDecoded = WriteMetaToFlac(Buffer.from(musicDecoded), newMeta, musicMeta) musicDecoded = WriteMetaToFlac(Buffer.from(musicDecoded), newMeta, musicMeta)
@ -107,7 +114,8 @@ export async function Decrypt(file, raw_filename, raw_ext) {
} }
} }
function reportKeyUsage(keyData, maskData, artist, title, album, filename, format) {
function reportKeyUsage(keyData: Uint8Array, maskData: number[], filename: string, format: string, title: string, artist?: string, album?: string) {
fetch(IXAREA_API_ENDPOINT + "/qmcmask/usage", { fetch(IXAREA_API_ENDPOINT + "/qmcmask/usage", {
method: "POST", method: "POST",
headers: {"Content-Type": "application/json"}, headers: {"Content-Type": "application/json"},
@ -118,7 +126,7 @@ function reportKeyUsage(keyData, maskData, artist, title, album, filename, forma
}).then().catch() }).then().catch()
} }
async function queryKeyInfo(keyData, filename, format) { async function queryKeyInfo(keyData: Uint8Array, filename: string, format: string) {
try { try {
const resp = await fetch(IXAREA_API_ENDPOINT + "/qmcmask/query", { const resp = await fetch(IXAREA_API_ENDPOINT + "/qmcmask/query", {
method: "POST", method: "POST",
@ -132,20 +140,15 @@ async function queryKeyInfo(keyData, filename, format) {
} }
} }
async function queryAlbumCoverImage(artist, title, album) { async function queryAlbumCoverImage(title: string, artist?: string, album?: string) {
const song_query_url = IXAREA_API_ENDPOINT + "/music/qq-cover" const song_query_url = "https://stats.ixarea.com/apis" + "/music/qq-cover"
try { try {
const params = {Artist: artist, Title: title, Album: album}; const params = new URLSearchParams([["Title", title], ["Artist", artist ?? ""], ["Album", album ?? ""]])
let _url = song_query_url + "?"; const resp = await fetch(`${song_query_url}?${params.toString()}`)
for (let pKey in params) {
_url += pKey + "=" + encodeURIComponent(params[pKey]) + "&"
}
const resp = await fetch(_url)
if (resp.ok) { if (resp.ok) {
let data = await resp.json(); let data = await resp.json();
return song_query_url + "/" + data.Type + "/" + data.Id return song_query_url + "/" + data.Type + "/" + data.Id
} }
} catch (e) { } catch (e) {
console.log(e); console.log(e);
} }