mirror of
https://git.unlock-music.dev/um/web.git
synced 2024-12-29 07:15:03 +08:00
refactor: move some utils to typescript
This commit is contained in:
parent
213ac35157
commit
f3f6f9ef40
@ -1,4 +1,5 @@
|
||||
import {AudioMimeType, DetectAudioExt, GetArrayBuffer, GetFileInfo, GetMetaCoverURL, IsBytesEqual} from "./util";
|
||||
import {AudioMimeType, GetArrayBuffer, GetFileInfo, GetMetaCoverURL} from "./util";
|
||||
import {BytesHasPrefix, SniffAudioExt} from "@/decrypt/utils.ts";
|
||||
|
||||
const musicMetadata = require("music-metadata-browser");
|
||||
const VprHeader = [
|
||||
@ -23,10 +24,10 @@ export async function Decrypt(file, raw_filename, raw_ext) {
|
||||
}
|
||||
const oriData = new Uint8Array(await GetArrayBuffer(file));
|
||||
if (raw_ext === "vpr") {
|
||||
if (!IsBytesEqual(VprHeader, oriData.slice(0, 0x10)))
|
||||
if (!BytesHasPrefix(oriData, VprHeader))
|
||||
return {status: false, message: "Not a valid vpr file!"}
|
||||
} else {
|
||||
if (!IsBytesEqual(KgmHeader, oriData.slice(0, 0x10)))
|
||||
if (!BytesHasPrefix(oriData, KgmHeader))
|
||||
return {status: false, message: "Not a valid kgm/kgma file!"}
|
||||
}
|
||||
let bHeaderLen = new DataView(oriData.slice(0x10, 0x14).buffer)
|
||||
@ -61,7 +62,7 @@ export async function Decrypt(file, raw_filename, raw_ext) {
|
||||
for (let i = 0; i < dataLen; i++) audioData[i] ^= VprMaskDiff[i % 17]
|
||||
}
|
||||
|
||||
const ext = DetectAudioExt(audioData, "mp3");
|
||||
const ext = SniffAudioExt(audioData);
|
||||
const mime = AudioMimeType[ext];
|
||||
let musicBlob = new Blob([audioData], {type: mime});
|
||||
const musicMeta = await musicMetadata.parseBlob(musicBlob);
|
||||
@ -71,11 +72,11 @@ export async function Decrypt(file, raw_filename, raw_ext) {
|
||||
status: true,
|
||||
title: info.title,
|
||||
artist: info.artist,
|
||||
ext: ext,
|
||||
album: musicMeta.common.album,
|
||||
picture: imgUrl,
|
||||
file: URL.createObjectURL(musicBlob),
|
||||
mime: mime
|
||||
ext,
|
||||
mime
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
import {AudioMimeType, DetectAudioExt, GetArrayBuffer, GetFileInfo, GetMetaCoverURL, IsBytesEqual} from "./util";
|
||||
import {AudioMimeType, GetArrayBuffer, GetFileInfo, GetMetaCoverURL} from "./util";
|
||||
import {BytesHasPrefix, SniffAudioExt} from "@/decrypt/utils.ts";
|
||||
|
||||
const musicMetadata = require("music-metadata-browser");
|
||||
const MagicHeader = [
|
||||
@ -7,9 +8,9 @@ const MagicHeader = [
|
||||
]
|
||||
const PreDefinedKey = "MoOtOiTvINGwd2E6n0E1i7L5t2IoOoNk"
|
||||
|
||||
export async function Decrypt(file, raw_filename, raw_ext) {
|
||||
export async function Decrypt(file, raw_filename, _) {
|
||||
const oriData = new Uint8Array(await GetArrayBuffer(file));
|
||||
if (!IsBytesEqual(MagicHeader, oriData.slice(0, 0x10)))
|
||||
if (!BytesHasPrefix(oriData, MagicHeader))
|
||||
return {status: false, message: "Not a valid kwm file!"}
|
||||
|
||||
let fileKey = oriData.slice(0x18, 0x20)
|
||||
@ -20,7 +21,7 @@ export async function Decrypt(file, raw_filename, raw_ext) {
|
||||
audioData[cur] ^= mask[cur % 0x20];
|
||||
|
||||
|
||||
const ext = DetectAudioExt(audioData, "mp3");
|
||||
const ext = SniffAudioExt(audioData);
|
||||
const mime = AudioMimeType[ext];
|
||||
let musicBlob = new Blob([audioData], {type: mime});
|
||||
|
||||
|
@ -1,3 +1,5 @@
|
||||
import {BytesHasPrefix, SniffAudioExt} from "@/decrypt/utils.ts";
|
||||
|
||||
const CryptoJS = require("crypto-js");
|
||||
const MetaFlac = require('metaflac-js');
|
||||
const CORE_KEY = CryptoJS.enc.Hex.parse("687a4852416d736f356b496e62617857");
|
||||
@ -8,19 +10,17 @@ import jimp from 'jimp';
|
||||
|
||||
import {
|
||||
AudioMimeType,
|
||||
DetectAudioExt,
|
||||
GetArrayBuffer,
|
||||
GetFileInfo,
|
||||
GetWebImage,
|
||||
IsBytesEqual,
|
||||
WriteMp3Meta
|
||||
} from "./util"
|
||||
|
||||
export async function Decrypt(file, raw_filename, raw_ext) {
|
||||
export async function Decrypt(file, raw_filename, _) {
|
||||
const fileBuffer = await GetArrayBuffer(file);
|
||||
const dataView = new DataView(fileBuffer);
|
||||
|
||||
if (!IsBytesEqual(MagicHeader, new Uint8Array(fileBuffer, 0, 8)))
|
||||
if (!BytesHasPrefix(new Uint8Array(fileBuffer, 0, 8), MagicHeader))
|
||||
return {status: false, message: "此ncm文件已损坏"};
|
||||
|
||||
const keyDataObj = getKeyData(dataView, fileBuffer, 10);
|
||||
@ -41,7 +41,7 @@ export async function Decrypt(file, raw_filename, raw_ext) {
|
||||
const info = GetFileInfo(artists.join("; "), musicMeta.musicName, raw_filename);
|
||||
if (artists.length === 0) artists.push(info.artist);
|
||||
|
||||
if (musicMeta.format === undefined) musicMeta.format = DetectAudioExt(audioData, "mp3");
|
||||
if (musicMeta.format === undefined) musicMeta.format = SniffAudioExt(audioData);
|
||||
console.log(musicMeta)
|
||||
|
||||
const imageInfo = await GetWebImage(musicMeta.albumPic);
|
||||
|
@ -1,6 +1,5 @@
|
||||
import {
|
||||
AudioMimeType,
|
||||
DetectAudioExt,
|
||||
GetArrayBuffer,
|
||||
GetFileInfo,
|
||||
GetMetaCoverURL,
|
||||
@ -10,6 +9,7 @@ import {
|
||||
} from "./util";
|
||||
import {QmcMaskCreate58, QmcMaskDetectMflac, QmcMaskDetectMgg, QmcMaskGetDefault} from "./qmcMask";
|
||||
import {fromByteArray as Base64Encode, toByteArray as Base64Decode} from 'base64-js'
|
||||
import {SniffAudioExt} from "@/decrypt/utils.ts";
|
||||
|
||||
const MetaFlac = require('metaflac-js');
|
||||
|
||||
@ -58,7 +58,7 @@ export async function Decrypt(file, raw_filename, raw_ext) {
|
||||
}
|
||||
let musicDecoded = seed.Decrypt(audioData);
|
||||
|
||||
const ext = DetectAudioExt(musicDecoded, handler.ext);
|
||||
const ext = SniffAudioExt(musicDecoded, handler.ext);
|
||||
const mime = AudioMimeType[ext];
|
||||
|
||||
let musicBlob = new Blob([musicDecoded], {type: mime});
|
||||
|
@ -1,4 +1,5 @@
|
||||
import {FLAC_HEADER, IsBytesEqual, OGG_HEADER} from "./util"
|
||||
import {FLAC_HEADER, OGG_HEADER} from "./util"
|
||||
import {BytesEquals, BytesHasPrefix} from "@/decrypt/utils.ts";
|
||||
|
||||
const QMOggPublicHeader1 = [
|
||||
0x4f, 0x67, 0x67, 0x53, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
|
||||
@ -84,7 +85,7 @@ class QmcMask {
|
||||
}
|
||||
let rowLeft = this.Matrix128.slice(lenStart + 1, lenStart + 8);
|
||||
let rowRight = this.Matrix128.slice(lenRightStart + 1, lenRightStart + 8).reverse();
|
||||
if (IsBytesEqual(rowLeft, rowRight)) {
|
||||
if (BytesEquals(rowLeft, rowRight)) {
|
||||
matrix58 = matrix58.concat(rowLeft);
|
||||
} else {
|
||||
throw "decode mask-128 to mask-58 failed"
|
||||
@ -151,7 +152,9 @@ export function QmcMaskDetectMflac(data) {
|
||||
for (let block_idx = 0; block_idx < search_len; block_idx += 128) {
|
||||
try {
|
||||
mask = new QmcMask(data.slice(block_idx, block_idx + 128));
|
||||
if (IsBytesEqual(FLAC_HEADER, mask.Decrypt(data.slice(0, FLAC_HEADER.length)))) break;
|
||||
if (BytesHasPrefix(mask.Decrypt(data.slice(0, FLAC_HEADER.length)), FLAC_HEADER)) {
|
||||
break;
|
||||
}
|
||||
} catch (e) {
|
||||
}
|
||||
}
|
||||
@ -186,8 +189,7 @@ export function QmcMaskDetectMgg(data) {
|
||||
return;
|
||||
}
|
||||
const mask = new QmcMask(matrix);
|
||||
let dx = mask.Decrypt(data.slice(0, OGG_HEADER.length));
|
||||
if (!IsBytesEqual(OGG_HEADER, dx)) {
|
||||
if (!BytesHasPrefix(mask.Decrypt(data.slice(0, OGG_HEADER.length)), OGG_HEADER)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -270,4 +272,4 @@ function QmcGenerateOggConf(page2) {
|
||||
for (let i = 2; i < page2; i++) specConf.push(4)
|
||||
specConf.push(0)
|
||||
return QMOggPublicConf1.concat(specConf, QMOggPublicConf2)
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,13 @@
|
||||
import {SniffAudioExt} from "@/decrypt/utils.ts";
|
||||
|
||||
const musicMetadata = require("music-metadata-browser");
|
||||
import {AudioMimeType, DetectAudioExt, GetArrayBuffer, GetMetaCoverURL, GetFileInfo} from "./util";
|
||||
import {AudioMimeType, GetArrayBuffer, GetMetaCoverURL, GetFileInfo} from "./util";
|
||||
|
||||
export async function Decrypt(file, raw_filename, raw_ext, detect = true) {
|
||||
let ext = raw_ext;
|
||||
if (detect) {
|
||||
const buffer = new Uint8Array(await GetArrayBuffer(file));
|
||||
ext = DetectAudioExt(buffer, raw_ext);
|
||||
ext = SniffAudioExt(buffer, raw_ext);
|
||||
if (ext !== raw_ext) file = new Blob([buffer], {type: AudioMimeType[ext]})
|
||||
}
|
||||
const tag = await musicMetadata.parseBlob(file);
|
||||
|
@ -1,5 +1,4 @@
|
||||
const ID3Writer = require("browser-id3-writer");
|
||||
const musicMetadata = require("music-metadata-browser");
|
||||
export const FLAC_HEADER = [0x66, 0x4C, 0x61, 0x43];
|
||||
export const MP3_HEADER = [0x49, 0x44, 0x33];
|
||||
export const OGG_HEADER = [0x4F, 0x67, 0x67, 0x53];
|
||||
@ -64,19 +63,6 @@ export function IsBytesEqual(first, second) {
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
export function DetectAudioExt(data, fallbackExt) {
|
||||
if (IsBytesEqual(MP3_HEADER, data.slice(0, MP3_HEADER.length))) return "mp3";
|
||||
if (IsBytesEqual(FLAC_HEADER, data.slice(0, FLAC_HEADER.length))) return "flac";
|
||||
if (IsBytesEqual(OGG_HEADER, data.slice(0, OGG_HEADER.length))) return "ogg";
|
||||
if (IsBytesEqual(M4A_HEADER, data.slice(4, 4 + M4A_HEADER.length))) return "m4a";
|
||||
if (IsBytesEqual(WMA_HEADER, data.slice(0, WMA_HEADER.length))) return "wma";
|
||||
if (IsBytesEqual(WAV_HEADER, data.slice(0, WAV_HEADER.length))) return "wav";
|
||||
return fallbackExt;
|
||||
}
|
||||
|
||||
|
||||
export async function GetWebImage(pic_url) {
|
||||
try {
|
||||
|
36
src/decrypt/utils.ts
Normal file
36
src/decrypt/utils.ts
Normal file
@ -0,0 +1,36 @@
|
||||
export const FLAC_HEADER = [0x66, 0x4C, 0x61, 0x43];
|
||||
export const MP3_HEADER = [0x49, 0x44, 0x33];
|
||||
export const OGG_HEADER = [0x4F, 0x67, 0x67, 0x53];
|
||||
export const M4A_HEADER = [0x66, 0x74, 0x79, 0x70];
|
||||
export const WMA_HEADER = [
|
||||
0x30, 0x26, 0xB2, 0x75, 0x8E, 0x66, 0xCF, 0x11,
|
||||
0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C,
|
||||
]
|
||||
export const WAV_HEADER = [0x52, 0x49, 0x46, 0x46]
|
||||
export const AAC_HEADER = [0xFF, 0xF1]
|
||||
|
||||
export function BytesHasPrefix(data: Uint8Array, prefix: number[]): boolean {
|
||||
if (prefix.length > data.length) return false
|
||||
return prefix.every((val, idx) => {
|
||||
return val === data[idx];
|
||||
})
|
||||
}
|
||||
|
||||
export function BytesEquals(data: Uint8Array, another: Uint8Array): boolean {
|
||||
if (another.length != data.length) return false
|
||||
return data.every((val, idx) => {
|
||||
return val === another[idx];
|
||||
})
|
||||
}
|
||||
|
||||
export function SniffAudioExt(data: Uint8Array, fallback_ext: string = "mp3"): string {
|
||||
if (BytesHasPrefix(data, MP3_HEADER)) return ".mp3"
|
||||
if (BytesHasPrefix(data, FLAC_HEADER)) return ".flac"
|
||||
if (BytesHasPrefix(data, OGG_HEADER)) return ".ogg"
|
||||
if (data.length >= 4 + M4A_HEADER.length &&
|
||||
BytesHasPrefix(data.slice(4), M4A_HEADER)) return ".m4a"
|
||||
if (BytesHasPrefix(data, WAV_HEADER)) return ".wav"
|
||||
if (BytesHasPrefix(data, WMA_HEADER)) return ".wma"
|
||||
if (BytesHasPrefix(data, AAC_HEADER)) return ".aac"
|
||||
return fallback_ext;
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
import {AudioMimeType, GetArrayBuffer, GetFileInfo, GetMetaCoverURL, IsBytesEqual} from "./util";
|
||||
import {AudioMimeType, GetArrayBuffer, GetFileInfo, GetMetaCoverURL} from "./util";
|
||||
|
||||
import {Decrypt as RawDecrypt} from "./raw";
|
||||
import {BytesHasPrefix} from "@/decrypt/utils.ts";
|
||||
|
||||
const musicMetadata = require("music-metadata-browser");
|
||||
const MagicHeader = [0x69, 0x66, 0x6D, 0x74]
|
||||
@ -14,8 +15,7 @@ const FileTypeMap = {
|
||||
|
||||
export async function Decrypt(file, raw_filename, raw_ext) {
|
||||
const oriData = new Uint8Array(await GetArrayBuffer(file));
|
||||
if (!IsBytesEqual(MagicHeader, oriData.slice(0, 4)) ||
|
||||
!IsBytesEqual(MagicHeader2, oriData.slice(8, 12))) {
|
||||
if (!BytesHasPrefix(oriData, MagicHeader) || !BytesHasPrefix(oriData.slice(8, 12), MagicHeader2)) {
|
||||
if (raw_ext === "xm") {
|
||||
return {status: false, message: "此xm文件已损坏"}
|
||||
} else {
|
||||
|
Loading…
Reference in New Issue
Block a user