fix: auth magic

This commit is contained in:
Xmader 2020-11-17 12:09:49 -05:00
parent 6bc841f132
commit 4764e0931b
No known key found for this signature in database
GPG Key ID: A20B97FB9EB730E4
1 changed files with 32 additions and 34 deletions

View File

@ -1,53 +1,51 @@
/* eslint-disable no-extend-native */ /* eslint-disable no-extend-native */
import scoreinfo from './scoreinfo' import scoreinfo from './scoreinfo'
import { webpackHook, webpackGlobalOverride, ALL } from './webpack-hook' import { onPackLoad, webpackContext } from './webpack-hook'
let authModuleId: string
const AUTH_FN = '+3],22,-1044525330)'
const MAGIC_ARG_INDEX = 1
type FileType = 'img' | 'mp3' | 'midi' type FileType = 'img' | 'mp3' | 'midi'
const AUTH_REG = /[0-9a-f]{40}/
enum PACK_ID {
img = 9,
midi = 118,
mp3 = 74,
}
/** /**
* I know this is super hacky. * I know this is super hacky.
*/ */
let magic: Promise<string> | string = new Promise((resolve) => { const magicHookConstr = (type: FileType) => {
// todo: hook module by what it does, not what it is called // request pack
webpackGlobalOverride(ALL, (n, r, t) => { // override // eslint-disable-next-line no-void, @typescript-eslint/no-unsafe-return
const fn = n.exports void webpackContext.then((ctx) => ctx.e(PACK_ID[type])).then(console.log)
if (typeof fn === 'function' && fn.toString().includes(AUTH_FN)) {
if (!authModuleId && n.i) { return new Promise<string>((resolve) => {
authModuleId = n.i onPackLoad((pack) => {
n.exports = (...args) => { if (pack[0].includes(PACK_ID[type])) {
if (magic instanceof Promise) { Object.values(pack[1]).forEach((mod) => {
magic = args[MAGIC_ARG_INDEX] const m = mod.toString().match(AUTH_REG)
resolve(magic) if (m && m[0]) resolve(m[0])
} })
return fn(...args) as string
}
} }
} })
}) })
}) }
const magics: Record<FileType, Promise<string>> = {
img: magicHookConstr('img'),
midi: magicHookConstr('midi'),
mp3: magicHookConstr('mp3'),
}
const getApiUrl = (type: FileType, index: number): string => { const getApiUrl = (type: FileType, index: number): string => {
return `/api/jmuse?id=${scoreinfo.id}&type=${type}&index=${index}` return `/api/jmuse?id=${scoreinfo.id}&type=${type}&index=${index}&v2=1`
} }
const getApiAuth = async (type: FileType, index: number): Promise<string> => { const getApiAuth = async (type: FileType, index: number): Promise<string> => {
if (magic instanceof Promise) { // eslint-disable-next-line no-void
// force to retrieve the MAGIC void index
const el = document.querySelectorAll('.SD7H- > button')[3] as HTMLButtonElement return magics[type]
el.click()
magic = await magic
}
const str = String(scoreinfo.id) + type + String(index)
const fn: (str: string, magic: string) => string = webpackHook(authModuleId)
return fn(str, magic)
} }
export const getFileUrl = async (type: FileType, index = 0): Promise<string> => { export const getFileUrl = async (type: FileType, index = 0): Promise<string> => {