mirror of
https://github.com/Momoko-Ayase/Senbei.git
synced 2026-11-24 18:52:47 +08:00
feat(web): bring the Senbei workbench to the browser
Add folder scanning, Android library and package recovery, and the localized desktop-style workbench. Keep browser-refused outputs in a fallback ZIP and include logs with folder and ZIP exports. Align the desktop and web modal transitions and polish the desktop About and legal dialogs.
This commit is contained in:
1 parent
20a68b1c73
commit
21236e8b98
59 files changed
+11612
-3436
No files matched your search
+156
-79
@@ -1,97 +1,174 @@
|
||||
// One-shot worker: each job runs in a fresh worker with its own wasm instance.
|
||||
// Two reasons:
|
||||
// The recovery engine, off the main thread.
|
||||
//
|
||||
// 1. UI stays responsive — unpacking a 100 MB+ module (or rewriting a large
|
||||
// `global-metadata.dat`) blocks for seconds.
|
||||
// 2. Trap isolation — the senbei DLL-first routing probe relies on
|
||||
// catch_unwind to reject EXE-shell-layout DLLs, and panics cannot be
|
||||
// caught in WebAssembly: the probe traps the whole call. A trap kills
|
||||
// this worker's message handler, which the main thread observes and
|
||||
// retries with the forced-EXE pipeline in a NEW worker (the trapped
|
||||
// instance is never reused). That reproduces the CLI's
|
||||
// dll-first/exe-fallback routing without a catchable panic.
|
||||
// The page starts one of these for each job and ends it when the job is done.
|
||||
// A Rust panic is a trap in WebAssembly and a trapped instance cannot be used
|
||||
// again, so no instance outlives its job; ending the worker also returns the
|
||||
// instance's memory, which a recovery grows to a few times the input size.
|
||||
// Scanning uses a worker that answers several small requests and is replaced
|
||||
// after a trap.
|
||||
//
|
||||
// Every reply carries the recovery profile (see `profile_json`), including
|
||||
// failures: the page shows which route ran and why it was rejected.
|
||||
// Every request is `{ id, op, ... }` and every reply carries the same `id`.
|
||||
// The first request also carries `wasm`, the compiled WebAssembly module the
|
||||
// page shares between workers so that it is fetched and compiled once.
|
||||
|
||||
import init, {
|
||||
deobfuscate_metadata_reported,
|
||||
unpack_file_reported,
|
||||
InputBuffer,
|
||||
detect,
|
||||
inspect_profile_json,
|
||||
manifest_identity,
|
||||
probe_library,
|
||||
restore_library,
|
||||
restore_metadata,
|
||||
unpack_pe,
|
||||
unsafe_package_entry,
|
||||
} from './pkg/senbei_wasm.js';
|
||||
import { crc32, entryStream } from './zip.js';
|
||||
|
||||
let ready = null;
|
||||
|
||||
/** Parse the profile JSON into a plain object for structured cloning. */
|
||||
function profileOf(doc) {
|
||||
if (!doc) return null;
|
||||
/**
|
||||
* Copy one input into the instance.
|
||||
*
|
||||
* `source` is a `Blob`, or a package entry `{ blob, method, size, crc }` that
|
||||
* is inflated on the way in and checked against its declared size and CRC.
|
||||
* The input is never held as a whole on the JS side.
|
||||
*/
|
||||
async function load(source) {
|
||||
const entry = source instanceof Blob ? null : source;
|
||||
const size = entry ? entry.size : source.size;
|
||||
const buffer = new InputBuffer(size);
|
||||
const reader = (entry ? entryStream(entry) : source.stream()).getReader();
|
||||
let crc = 0;
|
||||
try {
|
||||
return JSON.parse(doc);
|
||||
} catch {
|
||||
return null;
|
||||
for (;;) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
// `append` refuses bytes past the declared size, so a malformed entry
|
||||
// cannot grow the buffer.
|
||||
buffer.append(value);
|
||||
if (entry) crc = crc32(value, crc);
|
||||
}
|
||||
} catch (error) {
|
||||
reader.cancel().catch(() => {});
|
||||
buffer.free();
|
||||
throw error;
|
||||
}
|
||||
if (buffer.length !== size) {
|
||||
buffer.free();
|
||||
throw new Error('input is shorter than its declared size');
|
||||
}
|
||||
if (entry && crc !== entry.crc) {
|
||||
buffer.free();
|
||||
throw new Error('package entry does not match its checksum');
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/** A copy of a view of the instance's memory, as a `Blob` the page can keep. */
|
||||
function blobOf(view) {
|
||||
return view && view.length > 0 ? new Blob([view]) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The SHA-256 of a blob in hexadecimal, or `null` where the browser offers no
|
||||
* digest: `crypto.subtle` exists only in a secure context.
|
||||
*/
|
||||
async function digestOf(blob) {
|
||||
if (!self.crypto?.subtle) return null;
|
||||
const digest = await self.crypto.subtle.digest('SHA-256', await blob.arrayBuffer());
|
||||
return Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, '0')).join('');
|
||||
}
|
||||
|
||||
async function recover(id, request) {
|
||||
const input = await load(request.input);
|
||||
let companion = null;
|
||||
let outcome = null;
|
||||
try {
|
||||
const onStep = (title, i, of, key) => self.postMessage({ id, type: 'step', title, i, of, key });
|
||||
if (request.op === 'pe') {
|
||||
// `unpack_pe` takes the companion buffer over and frees it.
|
||||
companion = request.companion ? await load(request.companion) : null;
|
||||
const taken = companion;
|
||||
companion = null;
|
||||
outcome = unpack_pe(input, taken ?? undefined, request.forceExe === true, request.lang, onStep);
|
||||
} else if (request.op === 'so') {
|
||||
outcome = restore_library(input, request.lang, onStep);
|
||||
} else {
|
||||
outcome = restore_metadata(
|
||||
input,
|
||||
request.moduleBytes ?? undefined,
|
||||
request.conflicting ?? 0,
|
||||
request.lang,
|
||||
);
|
||||
}
|
||||
const info = JSON.parse(outcome.info);
|
||||
// Each view is copied into a Blob before the next call into the module.
|
||||
const image = blobOf(outcome.image());
|
||||
const embedded = blobOf(outcome.embedded_metadata());
|
||||
const module = outcome.take_module() ?? null;
|
||||
self.postMessage({ id, type: 'result', info, image, embedded, module });
|
||||
if (info.status !== 'not_protected') {
|
||||
// Hashing reads every byte again, so it follows the result instead of
|
||||
// delaying it.
|
||||
self.postMessage({
|
||||
id,
|
||||
type: 'identity',
|
||||
input: input.sha256(),
|
||||
image: image ? outcome.image_sha256() : null,
|
||||
embedded: embedded ? outcome.embedded_metadata_sha256() ?? null : null,
|
||||
// The engine gave its copy of the companion back, so the file is read
|
||||
// once more for its digest, and only when it was the one spliced in.
|
||||
companion: info.companion && request.companion
|
||||
? await digestOf(request.companion).catch(() => null)
|
||||
: null,
|
||||
});
|
||||
}
|
||||
self.postMessage({ id, type: 'done' });
|
||||
} finally {
|
||||
outcome?.free();
|
||||
companion?.free();
|
||||
input.free();
|
||||
}
|
||||
}
|
||||
|
||||
/** Post a failure reply, keeping the trap flag the main thread retries on. */
|
||||
function postFailure(err, profile) {
|
||||
const trap = err instanceof WebAssembly.RuntimeError;
|
||||
self.postMessage({
|
||||
ok: false,
|
||||
trap,
|
||||
message: String(err?.message ?? err),
|
||||
profile,
|
||||
});
|
||||
async function handle(request) {
|
||||
const { id, op } = request;
|
||||
if (op === 'prefix') {
|
||||
const kind = detect(request.bytes) ?? null;
|
||||
const profile = JSON.parse(inspect_profile_json(request.bytes));
|
||||
self.postMessage({ id, type: 'done', kind, profile });
|
||||
} else if (op === 'probe-so') {
|
||||
const input = await load(request.input);
|
||||
try {
|
||||
self.postMessage({ id, type: 'done', profile: JSON.parse(probe_library(input)) });
|
||||
} finally {
|
||||
input.free();
|
||||
}
|
||||
} else if (op === 'manifest') {
|
||||
const identity = manifest_identity(request.bytes, request.truncated === true) ?? null;
|
||||
self.postMessage({ id, type: 'done', identity });
|
||||
} else if (op === 'unsafe-entry') {
|
||||
const message = JSON.parse(unsafe_package_entry(request.package, request.lang));
|
||||
self.postMessage({ id, type: 'done', message });
|
||||
} else {
|
||||
await recover(id, request);
|
||||
}
|
||||
}
|
||||
|
||||
self.onmessage = async (e) => {
|
||||
const { mode, input, companion, forceExe } = e.data;
|
||||
let profile = null;
|
||||
self.onmessage = async (event) => {
|
||||
const request = event.data;
|
||||
try {
|
||||
ready ??= init();
|
||||
ready ??= init({ module_or_path: request.wasm });
|
||||
await ready;
|
||||
|
||||
if (mode === 'metadata') {
|
||||
const r = deobfuscate_metadata_reported(input);
|
||||
profile = profileOf(r.profile_json);
|
||||
const bytes = r.bytes;
|
||||
self.postMessage(
|
||||
{
|
||||
ok: r.ok,
|
||||
mode: 'metadata',
|
||||
metadata: {
|
||||
version: r.version,
|
||||
methods: r.methods,
|
||||
remapped: r.remapped,
|
||||
modules: r.modules,
|
||||
},
|
||||
error: r.error ?? null,
|
||||
profile,
|
||||
bytes,
|
||||
},
|
||||
r.ok ? [bytes.buffer] : [],
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const r = unpack_file_reported(input, companion ?? undefined, forceExe === true);
|
||||
profile = profileOf(r.profile_json);
|
||||
const bytes = r.bytes;
|
||||
self.postMessage(
|
||||
{
|
||||
ok: r.ok,
|
||||
mode: 'unpack',
|
||||
kind: r.kind,
|
||||
suspect: r.suspect,
|
||||
issues: r.issues,
|
||||
severities: r.severities,
|
||||
fatal: r.fatal,
|
||||
companion: r.companion,
|
||||
error: r.error ?? null,
|
||||
profile,
|
||||
bytes,
|
||||
},
|
||||
r.ok ? [bytes.buffer] : [],
|
||||
);
|
||||
} catch (err) {
|
||||
postFailure(err, profile);
|
||||
await handle(request);
|
||||
} catch (error) {
|
||||
self.postMessage({
|
||||
id: request.id,
|
||||
type: 'error',
|
||||
// A trap leaves the instance unusable. The page ends this worker and,
|
||||
// for a DLL, tries the other route in a new one.
|
||||
trap: error instanceof WebAssembly.RuntimeError,
|
||||
message: String(error?.message ?? error),
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user