init
This commit is contained in:
222
ext/lib/signer.js
Normal file
222
ext/lib/signer.js
Normal file
@@ -0,0 +1,222 @@
|
||||
// Live OF request signer — port of ofsaver's ofrules.js for the offscreen
|
||||
// document (eval allowed via relaxed CSP). In-memory cache per revision.
|
||||
|
||||
const BUNDLE_ROOT = "https://static2.onlyfans.com/static/prod/f";
|
||||
const REV_ENDPOINT = "https://onlyfans.com/api2/v2/ip";
|
||||
const RC4_ALPHABET =
|
||||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/=";
|
||||
export const APP_TOKEN = "33d57ade8c02dbc5a333db99ff9ae26a";
|
||||
|
||||
// --- sync SHA-1 (hex) — the signer's dep is called synchronously ----------
|
||||
export function sha1hex(text) {
|
||||
const msg = new TextEncoder().encode(text);
|
||||
const ml = msg.length;
|
||||
const padded = new Uint8Array((((ml + 8) >> 6) + 1) << 6);
|
||||
padded.set(msg);
|
||||
padded[ml] = 0x80;
|
||||
const bitLen = ml * 8;
|
||||
const dv = new DataView(padded.buffer);
|
||||
dv.setUint32(padded.length - 4, bitLen >>> 0);
|
||||
dv.setUint32(padded.length - 8, Math.floor(bitLen / 2 ** 32));
|
||||
let h0 = 0x67452301, h1 = 0xefcdab89, h2 = 0x98badcfe, h3 = 0x10325476, h4 = 0xc3d2e1f0;
|
||||
const w = new Uint32Array(80);
|
||||
const rol = (x, n) => (x << n) | (x >>> (32 - n));
|
||||
for (let i = 0; i < padded.length; i += 64) {
|
||||
for (let j = 0; j < 16; j++) w[j] = dv.getUint32(i + j * 4);
|
||||
for (let j = 16; j < 80; j++) w[j] = rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);
|
||||
let a = h0, b = h1, c = h2, d = h3, e = h4;
|
||||
for (let j = 0; j < 80; j++) {
|
||||
let f, k;
|
||||
if (j < 20) { f = (b & c) | (~b & d); k = 0x5a827999; }
|
||||
else if (j < 40) { f = b ^ c ^ d; k = 0x6ed9eba1; }
|
||||
else if (j < 60) { f = (b & c) | (b & d) | (c & d); k = 0x8f1bbcdc; }
|
||||
else { f = b ^ c ^ d; k = 0xca62c1d6; }
|
||||
const t = (rol(a, 5) + f + e + k + w[j]) >>> 0;
|
||||
e = d; d = c; c = rol(b, 30); b = a; a = t;
|
||||
}
|
||||
h0 = (h0 + a) >>> 0; h1 = (h1 + b) >>> 0; h2 = (h2 + c) >>> 0; h3 = (h3 + d) >>> 0; h4 = (h4 + e) >>> 0;
|
||||
}
|
||||
return [h0, h1, h2, h3, h4].map((x) => x.toString(16).padStart(8, "0")).join("");
|
||||
}
|
||||
|
||||
export function pathGet(obj, path, fallback) {
|
||||
if (obj == null) return fallback;
|
||||
let cur = obj;
|
||||
for (const key of String(path).split(".")) {
|
||||
if (cur == null) return fallback;
|
||||
cur = cur[key];
|
||||
}
|
||||
return cur === undefined ? fallback : cur;
|
||||
}
|
||||
|
||||
export async function fetchRevision() {
|
||||
const res = await fetch(REV_ENDPOINT, { method: "HEAD", signal: AbortSignal.timeout(10_000) });
|
||||
const rev = res.headers.get("x-of-rev");
|
||||
if (!rev) throw new Error("no x-of-rev header on /api2/v2/ip response");
|
||||
return rev;
|
||||
}
|
||||
|
||||
export function extractChunkRenameMap(appJs) {
|
||||
const marker = '}[e]||e)+".js"';
|
||||
const idx = appJs.indexOf(marker);
|
||||
if (idx === -1) throw new Error("chunk map not found in app.js");
|
||||
const start = appJs.lastIndexOf("({", idx) + 1;
|
||||
const literal = appJs.slice(start, idx);
|
||||
const map = {};
|
||||
for (const m of literal.matchAll(/(\d+):"([^"]+)"/g)) map[m[1]] = m[2];
|
||||
return map;
|
||||
}
|
||||
|
||||
export function extractInitialChunkIds(appJs) {
|
||||
const m = appJs.match(/\.O\(void 0,\[([\d,]+)\]/);
|
||||
if (!m) throw new Error("initial chunk list not found in app.js");
|
||||
return m[1].split(",").filter(Boolean);
|
||||
}
|
||||
|
||||
export function extractModuleSource(chunkText, moduleId) {
|
||||
const key = `${moduleId}:function(`;
|
||||
const start = chunkText.indexOf(key);
|
||||
if (start === -1) return null;
|
||||
const fnStart = start + moduleId.length + 1;
|
||||
const bodyStart = chunkText.indexOf("{", fnStart);
|
||||
let depth = 0, inStr = null, esc = false;
|
||||
for (let i = bodyStart; i < chunkText.length; i++) {
|
||||
const ch = chunkText[i];
|
||||
if (esc) { esc = false; continue; }
|
||||
if (inStr) {
|
||||
if (ch === "\\") esc = true;
|
||||
else if (ch === inStr) inStr = null;
|
||||
continue;
|
||||
}
|
||||
if (ch === '"' || ch === "'" || ch === "`") { inStr = ch; continue; }
|
||||
if (ch === "{") depth++;
|
||||
else if (ch === "}") {
|
||||
depth--;
|
||||
if (depth === 0) return chunkText.slice(fnStart, i + 1);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function moduleIdBefore(chunkText, markerIndex) {
|
||||
const re = /(\d+):function\(/g;
|
||||
let last = null, m;
|
||||
while ((m = re.exec(chunkText)) && m.index < markerIndex) last = m;
|
||||
return last ? last[1] : null;
|
||||
}
|
||||
|
||||
function makeRequire(table) {
|
||||
const cache = {};
|
||||
const req = (id) => {
|
||||
if (cache[id]) return cache[id].exports;
|
||||
const fn = table[id];
|
||||
if (!fn) throw new Error("missing module " + id);
|
||||
const mod = { exports: {} };
|
||||
cache[id] = mod;
|
||||
fn.call(mod.exports, mod, mod.exports, req);
|
||||
return mod.exports;
|
||||
};
|
||||
req.d = (exports, definition) => {
|
||||
for (const key in definition)
|
||||
if (!Object.prototype.hasOwnProperty.call(exports, key) &&
|
||||
Object.prototype.hasOwnProperty.call(definition, key))
|
||||
Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
|
||||
};
|
||||
req.n = (module) => {
|
||||
const getter = module && module.__esModule ? () => module.default : () => module;
|
||||
req.d(getter, { a: getter });
|
||||
return getter;
|
||||
};
|
||||
req.o = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
|
||||
req.r = (exports) => {
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
};
|
||||
return req;
|
||||
}
|
||||
|
||||
function makeUniversalDep(userIdHolder) {
|
||||
const dep = function (a, b, c) {
|
||||
if (b === undefined) return sha1hex(String(a));
|
||||
return pathGet(a, b, c);
|
||||
};
|
||||
dep.A = {
|
||||
get getters() { return { "auth/authUserId": userIdHolder.value }; },
|
||||
};
|
||||
return dep;
|
||||
}
|
||||
|
||||
import signerFactoryDefault from "./signer-module.js";
|
||||
|
||||
async function registerSigner(table, signerModuleId, signerSource, userIdHolder) {
|
||||
const t = { ...table };
|
||||
const header = signerSource.slice(0, 800);
|
||||
for (const m of header.matchAll(/([A-Za-z_$][\w$]*)=t\((\d+)\)/g)) {
|
||||
const id = m[2];
|
||||
t[id] = (module) => { module.exports = makeUniversalDep(userIdHolder); };
|
||||
}
|
||||
t[signerModuleId] = signerFactoryDefault;
|
||||
return t;
|
||||
}
|
||||
|
||||
const bundleCache = new Map(); // rev -> {appJs, chunks: Map}
|
||||
async function cachedFetch(rev, name, url) {
|
||||
let entry = bundleCache.get(rev);
|
||||
if (!entry) { entry = { chunks: new Map() }; bundleCache.set(rev, entry); }
|
||||
if (name === "app.js") {
|
||||
if (!entry.appJs) entry.appJs = await (await fetch(url, { signal: AbortSignal.timeout(60_000) })).text();
|
||||
return entry.appJs;
|
||||
}
|
||||
if (!entry.chunks.has(name)) {
|
||||
entry.chunks.set(name, await (await fetch(url, { signal: AbortSignal.timeout(60_000) })).text());
|
||||
}
|
||||
return entry.chunks.get(name);
|
||||
}
|
||||
|
||||
/** Build the live signer: { revision, appToken, sign(urlPath, userId) }. */
|
||||
export async function loadLiveSigner(log = () => {}) {
|
||||
if (typeof globalThis.window === "undefined") globalThis.window = globalThis;
|
||||
const rev = await fetchRevision();
|
||||
log(`bundle revision: ${rev}`);
|
||||
|
||||
const appJs = await cachedFetch(rev, "app.js", `${BUNDLE_ROOT}/${rev}/app.js`);
|
||||
const renameMap = extractChunkRenameMap(appJs);
|
||||
const initialIds = extractInitialChunkIds(appJs);
|
||||
log(`initial chunks: ${initialIds.length}`);
|
||||
|
||||
const userIdHolder = { value: "0" };
|
||||
for (const id of initialIds) {
|
||||
const name = renameMap[id] ?? id;
|
||||
let text;
|
||||
try {
|
||||
text = await cachedFetch(rev, `${name}.js`, `${BUNDLE_ROOT}/${rev}/${name}.js`);
|
||||
} catch { continue; }
|
||||
const markerIdx = text.indexOf(RC4_ALPHABET);
|
||||
if (markerIdx === -1) continue;
|
||||
const moduleId = moduleIdBefore(text, markerIdx);
|
||||
if (!moduleId) continue;
|
||||
const source = extractModuleSource(text, moduleId);
|
||||
if (!source || !/\.A\s*=/.test(source)) continue;
|
||||
try {
|
||||
const table = await registerSigner({}, moduleId, source, userIdHolder);
|
||||
const signer = makeRequire(table)(moduleId);
|
||||
if (typeof signer.A !== "function") continue;
|
||||
userIdHolder.value = "0";
|
||||
const probe = signer.A({ url: "/api2/v2/users/me" });
|
||||
if (!probe || typeof probe.sign !== "string" || probe.sign.split(":").length < 3 || !probe.time) continue;
|
||||
log(`signer module ${moduleId} active from chunk ${name}.js`);
|
||||
return {
|
||||
mode: "bundle",
|
||||
revision: rev,
|
||||
appToken: APP_TOKEN,
|
||||
sign(urlPath, userId) {
|
||||
userIdHolder.value = String(userId ?? "0");
|
||||
const out = signer.A({ url: urlPath });
|
||||
return { sign: out.sign, time: String(out.time) };
|
||||
},
|
||||
};
|
||||
} catch (err) {
|
||||
log(`candidate chunk ${name}.js failed: ${err.message}`);
|
||||
}
|
||||
}
|
||||
throw new Error("signer module not found in any initial chunk");
|
||||
}
|
||||
Reference in New Issue
Block a user