fix: get auth magics
using fetch hook
This commit is contained in:
parent
6b0a04b152
commit
c6eeb4d881
1 changed files with 57 additions and 38 deletions
95
src/file.ts
95
src/file.ts
|
@ -1,56 +1,51 @@
|
||||||
/* eslint-disable no-extend-native */
|
/* eslint-disable no-extend-native */
|
||||||
|
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
||||||
|
|
||||||
import scoreinfo from './scoreinfo'
|
import scoreinfo from './scoreinfo'
|
||||||
import { onPackLoad, getObfuscationCtx, OBFUSCATED_REG } from './webpack-hook'
|
import { ALL, webpackGlobalOverride } from './webpack-hook'
|
||||||
|
import { console } from './utils'
|
||||||
|
|
||||||
type FileType = 'img' | 'mp3' | 'midi'
|
type FileType = 'img' | 'mp3' | 'midi'
|
||||||
|
|
||||||
const AUTH_REG = `(\\+?${OBFUSCATED_REG.source}?)+`
|
const TYPE_REG = /id=(\d+)&type=(img|mp3|midi)/
|
||||||
const AUTH_CTX_REG = `,(${AUTH_REG})\\)(\\[|\\.then)`
|
|
||||||
|
|
||||||
enum PACK_HINT {
|
|
||||||
img = 'getImageRef',
|
|
||||||
midi = 'midi:',
|
|
||||||
mp3 = 'setVolume:',
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* I know this is super hacky.
|
* I know this is super hacky.
|
||||||
*/
|
*/
|
||||||
const magicHookConstr = async (type: FileType) => {
|
const magicHookConstr = (() => {
|
||||||
// request pack
|
const l = {}
|
||||||
// await loadAllPacks()
|
|
||||||
|
|
||||||
return new Promise<string>((resolve) => {
|
webpackGlobalOverride(ALL, (n, r, t) => { // override
|
||||||
onPackLoad((pack) => {
|
const e = n.exports
|
||||||
Object.values(pack[1]).forEach((mod) => {
|
if (typeof e === 'object' && e.fetch) {
|
||||||
const str = mod.toString()
|
const fn = e.fetch
|
||||||
if (!str.includes(PACK_HINT[type])) {
|
t.d(e, 'fetch', () => {
|
||||||
return
|
return function (...args) {
|
||||||
}
|
const [url, init] = args
|
||||||
|
const token = init?.headers?.Authorization
|
||||||
const m = str.match(AUTH_CTX_REG)
|
if (typeof url === 'string' && token) {
|
||||||
|
const m = url.match(TYPE_REG)
|
||||||
if (m) {
|
if (m) {
|
||||||
try {
|
const type = m[2]
|
||||||
const deObf = getObfuscationCtx(mod)
|
// eslint-disable-next-line no-unused-expressions
|
||||||
const authExp = m[1]
|
l[type]?.(token)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fn(...args)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
const reg = new RegExp(OBFUSCATED_REG)
|
return async (type: FileType) => {
|
||||||
let magic = ''
|
return new Promise<string>((resolve) => {
|
||||||
let r: RegExpMatchArray | null
|
l[type] = (token) => {
|
||||||
while ((r = reg.exec(authExp)) !== null) {
|
resolve(token)
|
||||||
magic += deObf(+r[2], r[3])
|
magics[type] = token
|
||||||
}
|
|
||||||
|
|
||||||
resolve(magic)
|
|
||||||
} catch (err) {
|
|
||||||
console.error(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
})()
|
||||||
|
|
||||||
const magics: Record<FileType, Promise<string>> = {
|
const magics: Record<FileType, Promise<string>> = {
|
||||||
img: magicHookConstr('img'),
|
img: magicHookConstr('img'),
|
||||||
|
@ -65,7 +60,31 @@ const getApiUrl = (type: FileType, index: number): string => {
|
||||||
const getApiAuth = async (type: FileType, index: number): Promise<string> => {
|
const getApiAuth = async (type: FileType, index: number): Promise<string> => {
|
||||||
// eslint-disable-next-line no-void
|
// eslint-disable-next-line no-void
|
||||||
void index
|
void index
|
||||||
return magics[type]
|
|
||||||
|
const magic = magics[type]
|
||||||
|
if (magic instanceof Promise) {
|
||||||
|
// force to retrieve the MAGIC
|
||||||
|
switch (type) {
|
||||||
|
case 'midi': {
|
||||||
|
const el = document.querySelectorAll('.SD7H- > button')[3] as HTMLButtonElement
|
||||||
|
el.click()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
case 'mp3': {
|
||||||
|
const el = document.querySelector('#playerBtnExprt') as HTMLButtonElement
|
||||||
|
el.click()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
case 'img': {
|
||||||
|
const imgE = document.querySelector('img[src*=score_]')
|
||||||
|
const nextE = imgE?.parentElement?.nextElementSibling
|
||||||
|
if (nextE) nextE.scrollIntoView()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return magic
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getFileUrl = async (type: FileType, index = 0): Promise<string> => {
|
export const getFileUrl = async (type: FileType, index = 0): Promise<string> => {
|
||||||
|
|
Loading…
Reference in a new issue