refactor: anti-detection

This commit is contained in:
Xmader 2020-11-05 16:47:50 -05:00
parent f51e0742d7
commit 04c918d092
2 changed files with 34 additions and 21 deletions

View file

@ -7,7 +7,9 @@
export const makeNative = (() => { export const makeNative = (() => {
const l: Set<Function> = new Set() const l: Set<Function> = new Set()
const _toString = Function.prototype['toString'] const target = Function.prototype
const method = 'toString'
const _toString = target[method]
const toString = function () { const toString = function () {
if (l.has(this)) { if (l.has(this)) {
// "function () {\n [native code]\n}" // "function () {\n [native code]\n}"
@ -15,7 +17,7 @@ export const makeNative = (() => {
} }
return _toString.call(this) as string return _toString.call(this) as string
} }
Function.prototype.toString = toString target[method] = toString
// make `Function.prototype.toString` itself "native" // make `Function.prototype.toString` itself "native"
l.add(toString) l.add(toString)
@ -24,3 +26,22 @@ export const makeNative = (() => {
l.add(fn) l.add(fn)
} }
})() })()
export const hookNative = <T extends object, M extends (keyof T)> (
target: T,
method: M,
hook: (originalFn: T[M], detach: () => void) => T[M],
): void => {
// reserve for future hook update
const _fn = target[method]
const detach = () => {
target[method] = _fn // detach
}
// This script can run before anything on the page,
// so setting this function to be non-configurable and non-writable is no use.
const hookedFn = hook(_fn, detach)
target[method] = hookedFn
makeNative(hookedFn as any)
}

View file

@ -2,7 +2,7 @@
import scoreinfo from './scoreinfo' import scoreinfo from './scoreinfo'
import { webpackHook } from './webpack-hook' import { webpackHook } from './webpack-hook'
import { makeNative } from './anti-detection' import { hookNative } from './anti-detection'
const FILE_URL_MODULE_ID = 'iNJA' const FILE_URL_MODULE_ID = 'iNJA'
const MAGIC_REG = /^\d+(img|mp3|midi)\d(.+)$/ const MAGIC_REG = /^\d+(img|mp3|midi)\d(.+)$/
@ -18,25 +18,17 @@ const getApiUrl = (id: number, type: FileType, index: number): string => {
* I know this is super hacky. * I know this is super hacky.
*/ */
let magic: Promise<string> | string = new Promise((resolve) => { let magic: Promise<string> | string = new Promise((resolve) => {
// reserve for future hook update hookNative(String.prototype, 'charCodeAt', (_fn, detach) => {
const target = String.prototype return function (i: number) {
const method = 'charCodeAt' const m = this.match(MAGIC_REG)
const _fn = target[method] if (m) {
resolve(m[2])
// This script can run before anything on the page, magic = m[2]
// so setting this function to be non-configurable and non-writable is no use. detach()
const hookFn = function (i: number) { }
const m = this.match(MAGIC_REG) return _fn.call(this, i) as number
if (m) {
resolve(m[2])
magic = m[2]
target[method] = _fn // detach
} }
return _fn.call(this, i) as number })
}
target[method] = hookFn
makeNative(hookFn)
}) })
export const getFileUrl = async (type: FileType, index = 0): Promise<string> => { export const getFileUrl = async (type: FileType, index = 0): Promise<string> => {