@orkestrel/sea 0.0.19 → 0.0.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/server/index.cjs +655 -626
- package/dist/src/server/index.cjs.map +1 -1
- package/dist/src/server/index.d.cts +6 -0
- package/dist/src/server/index.d.ts +6 -0
- package/dist/src/server/index.js +656 -627
- package/dist/src/server/index.js.map +1 -1
- package/package.json +16 -16
package/dist/src/server/index.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { isArrayBuffer, isError,
|
|
1
|
+
import { INTRINSICS, attempt, holds, isArrayBuffer, isError, isFunction, isObject, isString, literalOf } from "@orkestrel/contract";
|
|
2
2
|
import { appendFileSync, chmodSync, closeSync, copyFileSync, existsSync, fstatSync, fsyncSync, ftruncateSync, lstatSync, mkdirSync, openSync, readFileSync, readSync, readdirSync, realpathSync, renameSync, rmSync, statSync, writeFileSync, writeSync } from "node:fs";
|
|
3
3
|
import { brotliCompressSync, constants } from "node:zlib";
|
|
4
4
|
import { dirname, extname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
5
5
|
import { detach, executeSync } from "@orkestrel/process/server";
|
|
6
|
-
import { randomUUID } from "node:crypto";
|
|
7
6
|
import { Emitter } from "@orkestrel/emitter";
|
|
7
|
+
import { randomUUID } from "node:crypto";
|
|
8
8
|
import { getAssetKeys, getRawAsset, isSea } from "node:sea";
|
|
9
|
-
|
|
9
|
+
// src/server/constants.ts
|
|
10
10
|
/**
|
|
11
11
|
* Holds the SEA sentinel fuse value embedded in the Node.js binary,
|
|
12
12
|
* `NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2`.
|
|
@@ -147,8 +147,7 @@ var SEA_COMPRESSION_MODE_VALUES = Object.freeze({
|
|
|
147
147
|
text: 1,
|
|
148
148
|
font: 2
|
|
149
149
|
});
|
|
150
|
-
|
|
151
|
-
//#region src/server/errors.ts
|
|
150
|
+
// src/server/errors.ts
|
|
152
151
|
/**
|
|
153
152
|
* Represents the coded base error for every failure raised by the SEA build.
|
|
154
153
|
*
|
|
@@ -172,6 +171,7 @@ var SEAError = class extends Error {
|
|
|
172
171
|
context;
|
|
173
172
|
constructor(code, message, context) {
|
|
174
173
|
super(message);
|
|
174
|
+
INTRINSICS.define(this, Symbol.for("@orkestrel/sea.SEAError"), { value: this });
|
|
175
175
|
this.name = "SEAError";
|
|
176
176
|
this.code = code;
|
|
177
177
|
if (context !== void 0) this.context = context;
|
|
@@ -180,6 +180,9 @@ var SEAError = class extends Error {
|
|
|
180
180
|
/**
|
|
181
181
|
* Checks whether a value is a {@link SEAError}.
|
|
182
182
|
*
|
|
183
|
+
* @remarks
|
|
184
|
+
* Recognizes branded errors across package copies, including subclasses, and refuses transparent proxies.
|
|
185
|
+
*
|
|
183
186
|
* @param value - The value to test
|
|
184
187
|
* @returns True if `value` is a `SEAError`; false otherwise
|
|
185
188
|
*
|
|
@@ -191,7 +194,15 @@ var SEAError = class extends Error {
|
|
|
191
194
|
* ```
|
|
192
195
|
*/
|
|
193
196
|
function isSEAError(value) {
|
|
194
|
-
return
|
|
197
|
+
return holds(() => {
|
|
198
|
+
if (!isError(value) || INTRINSICS.prototype(value) === Error.prototype) return false;
|
|
199
|
+
const brand = INTRINSICS.describe(value, Symbol.for("@orkestrel/sea.SEAError"));
|
|
200
|
+
if (brand === void 0 || brand.value !== value) return false;
|
|
201
|
+
const code = INTRINSICS.reflect.read(value, "code");
|
|
202
|
+
if (code !== "PLATFORM" && code !== "ENTRY" && code !== "ASSET" && code !== "BLOB" && code !== "FORMAT" && code !== "INJECT" && code !== "ROOM" && code !== "FUSE" && code !== "SIGN" && code !== "SHELL" && code !== "TIMEOUT" && code !== "ABORT" && code !== "OUTPUT" && code !== "STATE" && code !== "BROWSER") return false;
|
|
203
|
+
const context = INTRINSICS.reflect.read(value, "context");
|
|
204
|
+
return context === void 0 || isObject(context) || isFunction(context);
|
|
205
|
+
});
|
|
195
206
|
}
|
|
196
207
|
/**
|
|
197
208
|
* Represents an error thrown when a shell command executed through `executeShell` exits non-zero.
|
|
@@ -217,6 +228,7 @@ var ShellError = class extends SEAError {
|
|
|
217
228
|
stderr;
|
|
218
229
|
constructor(message, stdout, stderr) {
|
|
219
230
|
super("SHELL", message);
|
|
231
|
+
INTRINSICS.define(this, Symbol.for("@orkestrel/sea.ShellError"), { value: this });
|
|
220
232
|
this.name = "ShellError";
|
|
221
233
|
this.stdout = stdout;
|
|
222
234
|
this.stderr = stderr;
|
|
@@ -225,6 +237,9 @@ var ShellError = class extends SEAError {
|
|
|
225
237
|
/**
|
|
226
238
|
* Checks whether a value is a {@link ShellError}.
|
|
227
239
|
*
|
|
240
|
+
* @remarks
|
|
241
|
+
* Recognizes branded errors across package copies, including subclasses, and refuses transparent proxies.
|
|
242
|
+
*
|
|
228
243
|
* @param value - The value to test
|
|
229
244
|
* @returns True if `value` is a `ShellError`; false otherwise
|
|
230
245
|
*
|
|
@@ -236,10 +251,13 @@ var ShellError = class extends SEAError {
|
|
|
236
251
|
* ```
|
|
237
252
|
*/
|
|
238
253
|
function isShellError(value) {
|
|
239
|
-
return
|
|
254
|
+
return holds(() => {
|
|
255
|
+
if (!isSEAError(value)) return false;
|
|
256
|
+
const brand = INTRINSICS.describe(value, Symbol.for("@orkestrel/sea.ShellError"));
|
|
257
|
+
return brand !== void 0 && brand.value === value && Buffer.isBuffer(INTRINSICS.reflect.read(value, "stdout")) && Buffer.isBuffer(INTRINSICS.reflect.read(value, "stderr"));
|
|
258
|
+
});
|
|
240
259
|
}
|
|
241
|
-
|
|
242
|
-
//#region src/server/validators.ts
|
|
260
|
+
// src/server/validators.ts
|
|
243
261
|
/**
|
|
244
262
|
* Checks whether a value is a valid {@link ExecutableFormat}.
|
|
245
263
|
*
|
|
@@ -255,8 +273,100 @@ function isShellError(value) {
|
|
|
255
273
|
function isExecutableFormat(value) {
|
|
256
274
|
return literalOf("pe", "elf", "macho")(value);
|
|
257
275
|
}
|
|
258
|
-
|
|
259
|
-
|
|
276
|
+
// src/server/FileAppender.ts
|
|
277
|
+
/** Owns the cursor of one appendFile operation. */
|
|
278
|
+
var FileAppender = class {
|
|
279
|
+
#offset = 0;
|
|
280
|
+
execute(target, source, chunk = 4194304) {
|
|
281
|
+
const fd = openSync(source, "r");
|
|
282
|
+
try {
|
|
283
|
+
const size = statSync(source).size;
|
|
284
|
+
const buffer = Buffer.alloc(Math.min(chunk, size));
|
|
285
|
+
this.#offset = 0;
|
|
286
|
+
while (this.#offset < size) {
|
|
287
|
+
const toRead = Math.min(chunk, size - this.#offset);
|
|
288
|
+
const bytesRead = readSync(fd, buffer, 0, toRead, this.#offset);
|
|
289
|
+
if (bytesRead === 0) break;
|
|
290
|
+
appendFileSync(target, buffer.subarray(0, bytesRead));
|
|
291
|
+
this.#offset += bytesRead;
|
|
292
|
+
}
|
|
293
|
+
} finally {
|
|
294
|
+
closeSync(fd);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
// src/server/FileCopier.ts
|
|
299
|
+
/** Owns the cursor of one copyRange operation. */
|
|
300
|
+
var FileCopier = class {
|
|
301
|
+
#position = 0;
|
|
302
|
+
#remaining = 0;
|
|
303
|
+
execute(source, target, start, length, chunk = 4194304) {
|
|
304
|
+
if (length <= 0) return;
|
|
305
|
+
const bufferSize = Math.min(chunk, length);
|
|
306
|
+
const buffer = Buffer.alloc(bufferSize);
|
|
307
|
+
this.#position = start;
|
|
308
|
+
this.#remaining = length;
|
|
309
|
+
while (this.#remaining > 0) {
|
|
310
|
+
const toRead = Math.min(chunk, this.#remaining);
|
|
311
|
+
const bytesRead = readSync(source, buffer, 0, toRead, this.#position);
|
|
312
|
+
if (bytesRead === 0) throw new SEAError("OUTPUT", "Unexpected end of file while copying byte range", {
|
|
313
|
+
start,
|
|
314
|
+
length,
|
|
315
|
+
position: this.#position,
|
|
316
|
+
remaining: this.#remaining
|
|
317
|
+
});
|
|
318
|
+
writeSync(target, buffer, 0, bytesRead, null);
|
|
319
|
+
this.#position += bytesRead;
|
|
320
|
+
this.#remaining -= bytesRead;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
};
|
|
324
|
+
// src/server/FusePatcher.ts
|
|
325
|
+
/** Owns the cursor of one patchSentinelFuse operation. */
|
|
326
|
+
var FusePatcher = class {
|
|
327
|
+
#offset = 0;
|
|
328
|
+
execute(executable, fuse) {
|
|
329
|
+
const fd = openSync(executable, "r+");
|
|
330
|
+
try {
|
|
331
|
+
const needle = Buffer.from(fuse, "utf-8");
|
|
332
|
+
const chunkSize = 67108864;
|
|
333
|
+
const readSize = chunkSize + needle.length;
|
|
334
|
+
const buffer = Buffer.alloc(readSize);
|
|
335
|
+
this.#offset = 0;
|
|
336
|
+
while (true) {
|
|
337
|
+
const bytesRead = readSync(fd, buffer, 0, readSize, this.#offset);
|
|
338
|
+
if (bytesRead === 0) break;
|
|
339
|
+
const idx = buffer.subarray(0, bytesRead).indexOf(needle);
|
|
340
|
+
if (idx !== -1) {
|
|
341
|
+
const fuseEnd = this.#offset + idx + needle.length;
|
|
342
|
+
const valueBuf = Buffer.alloc(2);
|
|
343
|
+
if (readSync(fd, valueBuf, 0, 2, fuseEnd) < 2) throw new SEAError("FUSE", "Could not read sentinel fuse value", {
|
|
344
|
+
executable,
|
|
345
|
+
fuse
|
|
346
|
+
});
|
|
347
|
+
const value = valueBuf.toString("utf-8");
|
|
348
|
+
if (value === ":0") {
|
|
349
|
+
writeSync(fd, Buffer.from("1", "utf-8"), 0, 1, fuseEnd + 1);
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
if (value === ":1") return;
|
|
353
|
+
throw new SEAError("FUSE", `Unexpected sentinel fuse value: ${value}`, {
|
|
354
|
+
executable,
|
|
355
|
+
fuse
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
this.#offset += chunkSize;
|
|
359
|
+
}
|
|
360
|
+
throw new SEAError("FUSE", `Sentinel fuse not found in executable: ${fuse}`, {
|
|
361
|
+
executable,
|
|
362
|
+
fuse
|
|
363
|
+
});
|
|
364
|
+
} finally {
|
|
365
|
+
closeSync(fd);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
};
|
|
369
|
+
// src/server/helpers.ts
|
|
260
370
|
/**
|
|
261
371
|
* Resolves the effective platform configuration.
|
|
262
372
|
*
|
|
@@ -399,13 +509,8 @@ function compressFile(input, output, options) {
|
|
|
399
509
|
[constants.BROTLI_PARAM_QUALITY]: quality,
|
|
400
510
|
[constants.BROTLI_PARAM_MODE]: mode
|
|
401
511
|
} });
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
outputStat = lstatSync(output);
|
|
405
|
-
} catch {
|
|
406
|
-
outputStat = void 0;
|
|
407
|
-
}
|
|
408
|
-
if (outputStat !== void 0 && outputStat.isSymbolicLink()) throw new SEAError("OUTPUT", "Refusing to write compressed output through a symlink", { output });
|
|
512
|
+
const outputStat = attempt(() => lstatSync(output));
|
|
513
|
+
if (outputStat.success && outputStat.value.isSymbolicLink()) throw new SEAError("OUTPUT", "Refusing to write compressed output through a symlink", { output });
|
|
409
514
|
writeFileSync(output, compressed);
|
|
410
515
|
return {
|
|
411
516
|
input,
|
|
@@ -424,20 +529,25 @@ function compressFile(input, output, options) {
|
|
|
424
529
|
function compressDirectory(directory, options, progress) {
|
|
425
530
|
const files = walkDirectory(directory);
|
|
426
531
|
const results = [];
|
|
427
|
-
|
|
428
|
-
let totalCompressed = 0;
|
|
429
|
-
for (const relativePath of files) {
|
|
532
|
+
const totals = files.reduce((total, relativePath) => {
|
|
430
533
|
const fullPath = resolve(directory, relativePath);
|
|
431
|
-
if (!isCompressible(fullPath))
|
|
534
|
+
if (!isCompressible(fullPath)) return total;
|
|
432
535
|
const result = compressFile(fullPath, fullPath + ".br", options);
|
|
433
536
|
results.push(result);
|
|
434
|
-
|
|
435
|
-
|
|
537
|
+
const original = total.original + result.size.original;
|
|
538
|
+
const compressed = total.compressed + result.size.compressed;
|
|
436
539
|
progress?.(result);
|
|
437
|
-
|
|
540
|
+
return {
|
|
541
|
+
original,
|
|
542
|
+
compressed
|
|
543
|
+
};
|
|
544
|
+
}, {
|
|
545
|
+
original: 0,
|
|
546
|
+
compressed: 0
|
|
547
|
+
});
|
|
438
548
|
return {
|
|
439
549
|
assets: results,
|
|
440
|
-
total: computeSize(
|
|
550
|
+
total: computeSize(totals.original, totals.compressed)
|
|
441
551
|
};
|
|
442
552
|
}
|
|
443
553
|
/**
|
|
@@ -571,21 +681,7 @@ function writeU64(fd, offset, value) {
|
|
|
571
681
|
* ```
|
|
572
682
|
*/
|
|
573
683
|
function appendFile(target, source, chunk = 4194304) {
|
|
574
|
-
|
|
575
|
-
try {
|
|
576
|
-
const size = statSync(source).size;
|
|
577
|
-
const buffer = Buffer.alloc(Math.min(chunk, size));
|
|
578
|
-
let offset = 0;
|
|
579
|
-
while (offset < size) {
|
|
580
|
-
const toRead = Math.min(chunk, size - offset);
|
|
581
|
-
const bytesRead = readSync(fd, buffer, 0, toRead, offset);
|
|
582
|
-
if (bytesRead === 0) break;
|
|
583
|
-
appendFileSync(target, buffer.subarray(0, bytesRead));
|
|
584
|
-
offset += bytesRead;
|
|
585
|
-
}
|
|
586
|
-
} finally {
|
|
587
|
-
closeSync(fd);
|
|
588
|
-
}
|
|
684
|
+
new FileAppender().execute(target, source, chunk);
|
|
589
685
|
}
|
|
590
686
|
/**
|
|
591
687
|
* Truncates a NUL-padded binary name field at its first NUL character.
|
|
@@ -670,9 +766,10 @@ function writeU16(fd, offset, value) {
|
|
|
670
766
|
* @returns True if the file has a valid PE signature; false otherwise
|
|
671
767
|
*/
|
|
672
768
|
function isPEExecutable(path) {
|
|
673
|
-
|
|
769
|
+
const opened = attempt(() => openSync(path, "r"));
|
|
770
|
+
if (!opened.success) return false;
|
|
771
|
+
const fd = opened.value;
|
|
674
772
|
try {
|
|
675
|
-
fd = openSync(path, "r");
|
|
676
773
|
const peOffset = readPEOffset(fd);
|
|
677
774
|
const sig = Buffer.alloc(4);
|
|
678
775
|
readSync(fd, sig, 0, 4, peOffset);
|
|
@@ -680,7 +777,7 @@ function isPEExecutable(path) {
|
|
|
680
777
|
} catch {
|
|
681
778
|
return false;
|
|
682
779
|
} finally {
|
|
683
|
-
|
|
780
|
+
closeSync(fd);
|
|
684
781
|
}
|
|
685
782
|
}
|
|
686
783
|
/**
|
|
@@ -765,13 +862,9 @@ function buildSignCommand(sign, target) {
|
|
|
765
862
|
if (!hasFile && !hasThumbprint) throw new SEAError("SIGN", "Windows signing requires exactly one of sign.file or sign.thumbprint");
|
|
766
863
|
if (hasFile && hasThumbprint) throw new SEAError("SIGN", "Windows signing accepts only one of sign.file or sign.thumbprint, not both");
|
|
767
864
|
if (sign.timestamp !== void 0) {
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
} catch {
|
|
772
|
-
throw new SEAError("SIGN", "Windows signing timestamp must be an http(s) URL", { timestamp: sign.timestamp });
|
|
773
|
-
}
|
|
774
|
-
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") throw new SEAError("SIGN", "Windows signing timestamp must be an http(s) URL", { timestamp: sign.timestamp });
|
|
865
|
+
const parsed = attempt(() => new URL(sign.timestamp ?? ""));
|
|
866
|
+
if (!parsed.success) throw new SEAError("SIGN", "Windows signing timestamp must be an http(s) URL", { timestamp: sign.timestamp });
|
|
867
|
+
if (parsed.value.protocol !== "http:" && parsed.value.protocol !== "https:") throw new SEAError("SIGN", "Windows signing timestamp must be an http(s) URL", { timestamp: sign.timestamp });
|
|
775
868
|
}
|
|
776
869
|
const digest = sign.digest ?? "sha256";
|
|
777
870
|
if (!(/* @__PURE__ */ new Set([
|
|
@@ -864,17 +957,18 @@ function ensureSafeKey(key) {
|
|
|
864
957
|
*/
|
|
865
958
|
function ensureContained(base, path) {
|
|
866
959
|
const resolved = resolve(base, path);
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
960
|
+
const paths = attempt(() => ({
|
|
961
|
+
real: realpathSync(resolved),
|
|
962
|
+
realBase: realpathSync(base)
|
|
963
|
+
}));
|
|
964
|
+
if (!paths.success) {
|
|
965
|
+
const thrown = paths.error;
|
|
873
966
|
throw new SEAError("ASSET", "Path not found or unresolvable", {
|
|
874
967
|
path,
|
|
875
968
|
cause: isError(thrown) ? thrown.message : String(thrown)
|
|
876
969
|
});
|
|
877
970
|
}
|
|
971
|
+
const { real, realBase } = paths.value;
|
|
878
972
|
if (real !== realBase && !real.startsWith(realBase + sep)) throw new SEAError("ASSET", "Path escapes the build root", {
|
|
879
973
|
base,
|
|
880
974
|
path
|
|
@@ -1099,23 +1193,7 @@ function buildELFNoteHeader(resource, blobSize) {
|
|
|
1099
1193
|
* ```
|
|
1100
1194
|
*/
|
|
1101
1195
|
function copyRange(source, target, start, length, chunk = 4194304) {
|
|
1102
|
-
|
|
1103
|
-
const bufferSize = Math.min(chunk, length);
|
|
1104
|
-
const buffer = Buffer.alloc(bufferSize);
|
|
1105
|
-
let position = start;
|
|
1106
|
-
let remaining = length;
|
|
1107
|
-
while (remaining > 0) {
|
|
1108
|
-
const bytesRead = readSync(source, buffer, 0, Math.min(chunk, remaining), position);
|
|
1109
|
-
if (bytesRead === 0) throw new SEAError("OUTPUT", "Unexpected end of file while copying byte range", {
|
|
1110
|
-
start,
|
|
1111
|
-
length,
|
|
1112
|
-
position,
|
|
1113
|
-
remaining
|
|
1114
|
-
});
|
|
1115
|
-
writeSync(target, buffer, 0, bytesRead, null);
|
|
1116
|
-
position += bytesRead;
|
|
1117
|
-
remaining -= bytesRead;
|
|
1118
|
-
}
|
|
1196
|
+
new FileCopier().execute(source, target, start, length, chunk);
|
|
1119
1197
|
}
|
|
1120
1198
|
/**
|
|
1121
1199
|
* Patches the sentinel fuse in a binary from `:0` to `:1`.
|
|
@@ -1133,44 +1211,7 @@ function copyRange(source, target, start, length, chunk = 4194304) {
|
|
|
1133
1211
|
* @throws SEAError with code `'FUSE'` when the fuse is not found in the executable
|
|
1134
1212
|
*/
|
|
1135
1213
|
function patchSentinelFuse(executable, fuse) {
|
|
1136
|
-
|
|
1137
|
-
try {
|
|
1138
|
-
const needle = Buffer.from(fuse, "utf-8");
|
|
1139
|
-
const chunkSize = 67108864;
|
|
1140
|
-
const readSize = chunkSize + needle.length;
|
|
1141
|
-
const buffer = Buffer.alloc(readSize);
|
|
1142
|
-
let offset = 0;
|
|
1143
|
-
while (true) {
|
|
1144
|
-
const bytesRead = readSync(fd, buffer, 0, readSize, offset);
|
|
1145
|
-
if (bytesRead === 0) break;
|
|
1146
|
-
const idx = buffer.subarray(0, bytesRead).indexOf(needle);
|
|
1147
|
-
if (idx !== -1) {
|
|
1148
|
-
const fuseEnd = offset + idx + needle.length;
|
|
1149
|
-
const valueBuf = Buffer.alloc(2);
|
|
1150
|
-
if (readSync(fd, valueBuf, 0, 2, fuseEnd) < 2) throw new SEAError("FUSE", "Could not read sentinel fuse value", {
|
|
1151
|
-
executable,
|
|
1152
|
-
fuse
|
|
1153
|
-
});
|
|
1154
|
-
const value = valueBuf.toString("utf-8");
|
|
1155
|
-
if (value === ":0") {
|
|
1156
|
-
writeSync(fd, Buffer.from("1", "utf-8"), 0, 1, fuseEnd + 1);
|
|
1157
|
-
return;
|
|
1158
|
-
}
|
|
1159
|
-
if (value === ":1") return;
|
|
1160
|
-
throw new SEAError("FUSE", `Unexpected sentinel fuse value: ${value}`, {
|
|
1161
|
-
executable,
|
|
1162
|
-
fuse
|
|
1163
|
-
});
|
|
1164
|
-
}
|
|
1165
|
-
offset += chunkSize;
|
|
1166
|
-
}
|
|
1167
|
-
throw new SEAError("FUSE", `Sentinel fuse not found in executable: ${fuse}`, {
|
|
1168
|
-
executable,
|
|
1169
|
-
fuse
|
|
1170
|
-
});
|
|
1171
|
-
} finally {
|
|
1172
|
-
closeSync(fd);
|
|
1173
|
-
}
|
|
1214
|
+
new FusePatcher().execute(executable, fuse);
|
|
1174
1215
|
}
|
|
1175
1216
|
/**
|
|
1176
1217
|
* Launches the system default browser at an http(s) URL.
|
|
@@ -1198,105 +1239,66 @@ function patchSentinelFuse(executable, fuse) {
|
|
|
1198
1239
|
* ```
|
|
1199
1240
|
*/
|
|
1200
1241
|
function openBrowser(url) {
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
} catch {
|
|
1205
|
-
throw new SEAError("BROWSER", "openBrowser requires a valid absolute URL", { url });
|
|
1206
|
-
}
|
|
1207
|
-
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") throw new SEAError("BROWSER", "openBrowser only supports http and https URLs", {
|
|
1242
|
+
const parsed = attempt(() => new URL(url));
|
|
1243
|
+
if (!parsed.success) throw new SEAError("BROWSER", "openBrowser requires a valid absolute URL", { url });
|
|
1244
|
+
if (parsed.value.protocol !== "http:" && parsed.value.protocol !== "https:") throw new SEAError("BROWSER", "openBrowser only supports http and https URLs", {
|
|
1208
1245
|
url,
|
|
1209
|
-
protocol: parsed.protocol
|
|
1246
|
+
protocol: parsed.value.protocol
|
|
1210
1247
|
});
|
|
1211
1248
|
const command = process.platform === "win32" ? {
|
|
1212
1249
|
file: "rundll32",
|
|
1213
|
-
arguments: ["url.dll,FileProtocolHandler", parsed.href]
|
|
1250
|
+
arguments: ["url.dll,FileProtocolHandler", parsed.value.href]
|
|
1214
1251
|
} : process.platform === "darwin" ? {
|
|
1215
1252
|
file: "open",
|
|
1216
|
-
arguments: [parsed.href]
|
|
1253
|
+
arguments: [parsed.value.href]
|
|
1217
1254
|
} : {
|
|
1218
1255
|
file: "xdg-open",
|
|
1219
|
-
arguments: [parsed.href]
|
|
1256
|
+
arguments: [parsed.value.href]
|
|
1220
1257
|
};
|
|
1221
1258
|
detach(command);
|
|
1222
1259
|
}
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
* fuse: 'NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2',
|
|
1246
|
-
* })
|
|
1247
|
-
* injector.format // 'pe' | 'elf' | 'macho'
|
|
1248
|
-
* injector.inject()
|
|
1249
|
-
* ```
|
|
1250
|
-
*/
|
|
1251
|
-
var Injector = class {
|
|
1260
|
+
// src/server/injectors/Injection.ts
|
|
1261
|
+
/** Owns the resources and layout cursors of one executable injection. */
|
|
1262
|
+
var Injection = class {
|
|
1263
|
+
#rsrcSectionIndex = 0;
|
|
1264
|
+
#dirTableSize = 0;
|
|
1265
|
+
#stringPoolSize = 0;
|
|
1266
|
+
#dataRegionOffset = 0;
|
|
1267
|
+
#dirPos = 0;
|
|
1268
|
+
#stringPos = 0;
|
|
1269
|
+
#dataEntryPos = 0;
|
|
1270
|
+
#accDirOffset = 0;
|
|
1271
|
+
#langSubdirCursor = 0;
|
|
1272
|
+
#checksumSum = 0;
|
|
1273
|
+
#checksumOffset = 0;
|
|
1274
|
+
#srcFd;
|
|
1275
|
+
#destFd;
|
|
1276
|
+
#blobFd;
|
|
1277
|
+
#injTemp;
|
|
1278
|
+
#cmdOffset = 0;
|
|
1279
|
+
#linkeditIndex = 0;
|
|
1280
|
+
#firstSectionOffset = 0;
|
|
1281
|
+
#verifyOffset = 0;
|
|
1252
1282
|
#options;
|
|
1253
1283
|
#format;
|
|
1254
|
-
constructor(options) {
|
|
1284
|
+
constructor(options, format) {
|
|
1255
1285
|
this.#options = options;
|
|
1256
|
-
this.#format =
|
|
1286
|
+
this.#format = format;
|
|
1257
1287
|
}
|
|
1258
|
-
|
|
1259
|
-
|
|
1288
|
+
execute() {
|
|
1289
|
+
const peOptionalOffset = this.#writeFormat();
|
|
1290
|
+
if (this.#options.fuse !== void 0) patchSentinelFuse(this.#options.executable, this.#options.fuse);
|
|
1291
|
+
if (peOptionalOffset !== void 0) this.#patchChecksum(peOptionalOffset);
|
|
1260
1292
|
}
|
|
1261
|
-
|
|
1262
|
-
let peOptionalOffset;
|
|
1293
|
+
#writeFormat() {
|
|
1263
1294
|
switch (this.#format) {
|
|
1264
|
-
case "pe":
|
|
1265
|
-
peOptionalOffset = this.#injectPE();
|
|
1266
|
-
break;
|
|
1295
|
+
case "pe": return this.#injectPE();
|
|
1267
1296
|
case "elf":
|
|
1268
1297
|
this.#injectELF();
|
|
1269
|
-
|
|
1270
|
-
case "macho":
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
if (peOptionalOffset !== void 0) this.#patchChecksum(peOptionalOffset);
|
|
1274
|
-
}
|
|
1275
|
-
#detect() {
|
|
1276
|
-
const fd = openSync(this.#options.executable, "r");
|
|
1277
|
-
try {
|
|
1278
|
-
const header = Buffer.alloc(4);
|
|
1279
|
-
readSync(fd, header, 0, 4, 0);
|
|
1280
|
-
const magic16 = header.readUInt16LE(0);
|
|
1281
|
-
const magic32 = header.readUInt32BE(0);
|
|
1282
|
-
const magic32le = header.readUInt32LE(0);
|
|
1283
|
-
if (magic16 === 23117) return "pe";
|
|
1284
|
-
if (magic32 === 2135247942) return "elf";
|
|
1285
|
-
if (magic32le === 4277009103) return "macho";
|
|
1286
|
-
if (magic32 === 3405691582 || magic32 === 3199925962) throw new SEAError("FORMAT", "Mach-O universal/fat binaries are not supported; provide a thin arm64 or x86_64 binary", {
|
|
1287
|
-
executable: this.#options.executable,
|
|
1288
|
-
magic: magic32
|
|
1289
|
-
});
|
|
1290
|
-
if (magic32le === 4277009102 || magic32le === 3472551422 || magic32 === 4277009102 || magic32 === 3472551422) throw new SEAError("FORMAT", "Only 64-bit little-endian Mach-O is supported", {
|
|
1291
|
-
executable: this.#options.executable,
|
|
1292
|
-
magic: magic32
|
|
1293
|
-
});
|
|
1294
|
-
throw new SEAError("FORMAT", `Unknown executable format (magic: 0x${magic32.toString(16).padStart(8, "0")})`, {
|
|
1295
|
-
executable: this.#options.executable,
|
|
1296
|
-
magic: magic32
|
|
1297
|
-
});
|
|
1298
|
-
} finally {
|
|
1299
|
-
closeSync(fd);
|
|
1298
|
+
return;
|
|
1299
|
+
case "macho":
|
|
1300
|
+
this.#injectMachO();
|
|
1301
|
+
return;
|
|
1300
1302
|
}
|
|
1301
1303
|
}
|
|
1302
1304
|
#injectPE() {
|
|
@@ -1327,8 +1329,8 @@ var Injector = class {
|
|
|
1327
1329
|
const existingResourceRVA = readU32(fd, resourceDirRvaOffset);
|
|
1328
1330
|
const sectionTableOffset = optionalOffset + optionalHeaderSize;
|
|
1329
1331
|
const sections = [];
|
|
1330
|
-
|
|
1331
|
-
for (
|
|
1332
|
+
this.#rsrcSectionIndex = -1;
|
|
1333
|
+
for (const i of Array.prototype.keys.call({ length: numberOfSections })) {
|
|
1332
1334
|
const off = sectionTableOffset + i * 40;
|
|
1333
1335
|
const nameBuf = Buffer.alloc(8);
|
|
1334
1336
|
readSync(fd, nameBuf, 0, 8, off);
|
|
@@ -1347,7 +1349,7 @@ var Injector = class {
|
|
|
1347
1349
|
characteristics,
|
|
1348
1350
|
headerOffset: off
|
|
1349
1351
|
});
|
|
1350
|
-
if (existingResourceRVA >= virtualAddress && existingResourceRVA < virtualAddress + Math.max(virtualSize, rawSize)) rsrcSectionIndex = i;
|
|
1352
|
+
if (existingResourceRVA >= virtualAddress && existingResourceRVA < virtualAddress + Math.max(virtualSize, rawSize)) this.#rsrcSectionIndex = i;
|
|
1351
1353
|
}
|
|
1352
1354
|
const sectionTableEnd = sectionTableOffset + numberOfSections * 40;
|
|
1353
1355
|
const availableHeaderSpace = sections.reduce((min, s) => s.rawOffset > 0 && s.rawOffset < min ? s.rawOffset : min, Infinity) - sectionTableEnd;
|
|
@@ -1357,8 +1359,8 @@ var Injector = class {
|
|
|
1357
1359
|
requiredHeaderSpace: 40
|
|
1358
1360
|
});
|
|
1359
1361
|
const existingLeaves = [];
|
|
1360
|
-
if (rsrcSectionIndex !== -1 && existingResourceRVA !== 0) {
|
|
1361
|
-
const rsrc = sections[rsrcSectionIndex];
|
|
1362
|
+
if (this.#rsrcSectionIndex !== -1 && existingResourceRVA !== 0) {
|
|
1363
|
+
const rsrc = sections[this.#rsrcSectionIndex];
|
|
1362
1364
|
if (rsrc !== void 0) this.#parseResourceDirectory(fd, rsrc.rawOffset, existingResourceRVA, 0, existingLeaves, 0, 0, void 0, 0, void 0);
|
|
1363
1365
|
}
|
|
1364
1366
|
const overwrite = this.#options.overwrite !== false;
|
|
@@ -1369,7 +1371,7 @@ var Injector = class {
|
|
|
1369
1371
|
return leaf.nameName?.toUpperCase() !== upperName;
|
|
1370
1372
|
});
|
|
1371
1373
|
const leafDataMap = /* @__PURE__ */ new Map();
|
|
1372
|
-
for (
|
|
1374
|
+
for (const i of Array.prototype.keys.call(filteredLeaves)) {
|
|
1373
1375
|
const leaf = filteredLeaves[i];
|
|
1374
1376
|
if (leaf === void 0) continue;
|
|
1375
1377
|
const fileOffset = this.#rvaToFileOffset(leaf.dataRVA, sections);
|
|
@@ -1384,12 +1386,10 @@ var Injector = class {
|
|
|
1384
1386
|
const { directoryBuffer, dataRegionSize } = this.#serializeResourceTree(tree, leafDataMap, blobSize);
|
|
1385
1387
|
const totalRawSize = directoryBuffer.length + dataRegionSize;
|
|
1386
1388
|
const alignedRawSize = alignTo(totalRawSize, fileAlignment);
|
|
1387
|
-
|
|
1388
|
-
for (const s of sections) {
|
|
1389
|
+
const newVa = alignTo(sections.reduce((highest, s) => {
|
|
1389
1390
|
const end = s.virtualAddress + Math.max(s.virtualSize, s.rawSize);
|
|
1390
|
-
|
|
1391
|
-
}
|
|
1392
|
-
const newVa = alignTo(highestVaEnd, sectionAlignment);
|
|
1391
|
+
return end > highest ? end : highest;
|
|
1392
|
+
}, 0), sectionAlignment);
|
|
1393
1393
|
const newVirtualSize = totalRawSize;
|
|
1394
1394
|
const fileSize = statSync(this.#options.executable).size;
|
|
1395
1395
|
const newRawOffset = alignTo(fileSize, fileAlignment);
|
|
@@ -1418,7 +1418,7 @@ var Injector = class {
|
|
|
1418
1418
|
}
|
|
1419
1419
|
this.#fixupDataEntries(directoryBuffer, 0, newVa);
|
|
1420
1420
|
appendFileSync(this.#options.executable, directoryBuffer);
|
|
1421
|
-
for (
|
|
1421
|
+
for (const i of Array.prototype.keys.call(filteredLeaves)) {
|
|
1422
1422
|
const data = leafDataMap.get(i);
|
|
1423
1423
|
if (data !== void 0) {
|
|
1424
1424
|
appendFileSync(this.#options.executable, data);
|
|
@@ -1440,27 +1440,17 @@ var Injector = class {
|
|
|
1440
1440
|
#parseResourceDirectory(fd, sectionFileOffset, sectionRva, dirOffset, leaves, depth, currentTypeId, currentTypeName, currentNameId, currentNameName) {
|
|
1441
1441
|
const absOffset = sectionFileOffset + dirOffset;
|
|
1442
1442
|
const totalEntries = readU16(fd, absOffset + 12) + readU16(fd, absOffset + 14);
|
|
1443
|
-
for (
|
|
1443
|
+
for (const i of Array.prototype.keys.call({ length: totalEntries })) {
|
|
1444
1444
|
const entryOffset = absOffset + 16 + i * 8;
|
|
1445
1445
|
const nameOrId = readU32(fd, entryOffset);
|
|
1446
1446
|
const offsetOrData = readU32(fd, entryOffset + 4);
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
let typeName = currentTypeName;
|
|
1455
|
-
let nameId = currentNameId;
|
|
1456
|
-
let nameName = currentNameName;
|
|
1457
|
-
if (depth === 0) {
|
|
1458
|
-
typeId = entryId;
|
|
1459
|
-
typeName = entryName;
|
|
1460
|
-
} else if (depth === 1) {
|
|
1461
|
-
nameId = entryId;
|
|
1462
|
-
nameName = entryName;
|
|
1463
|
-
}
|
|
1447
|
+
const named = (nameOrId & PE_RESOURCE_NAME_FLAG) !== 0;
|
|
1448
|
+
const entryId = named ? 0 : nameOrId;
|
|
1449
|
+
const entryName = named ? this.#readResourceString(fd, sectionFileOffset + (nameOrId & ~PE_RESOURCE_NAME_FLAG)) : void 0;
|
|
1450
|
+
const typeId = depth === 0 ? entryId : currentTypeId;
|
|
1451
|
+
const typeName = depth === 0 ? entryName : currentTypeName;
|
|
1452
|
+
const nameId = depth === 1 ? entryId : currentNameId;
|
|
1453
|
+
const nameName = depth === 1 ? entryName : currentNameName;
|
|
1464
1454
|
if ((offsetOrData & 2147483648) !== 0) {
|
|
1465
1455
|
const subDirOffset = offsetOrData & ~PE_RESOURCE_SUBDIR_FLAG;
|
|
1466
1456
|
this.#parseResourceDirectory(fd, sectionFileOffset, sectionRva, subDirOffset, leaves, depth + 1, typeId, typeName, nameId, nameName);
|
|
@@ -1501,21 +1491,15 @@ var Injector = class {
|
|
|
1501
1491
|
}
|
|
1502
1492
|
#buildResourceTree(existingLeaves, blobName, blobSize) {
|
|
1503
1493
|
const tree = /* @__PURE__ */ new Map();
|
|
1504
|
-
for (
|
|
1494
|
+
for (const i of Array.prototype.keys.call(existingLeaves)) {
|
|
1505
1495
|
const leaf = existingLeaves[i];
|
|
1506
1496
|
if (leaf === void 0) continue;
|
|
1507
1497
|
const typeKey = leaf.typeName !== void 0 ? `n:${leaf.typeName}` : `i:${String(leaf.typeId)}`;
|
|
1508
1498
|
const nameKey = leaf.nameName !== void 0 ? `n:${leaf.nameName}` : `i:${String(leaf.nameId)}`;
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
}
|
|
1514
|
-
let langArr = typeMap.get(nameKey);
|
|
1515
|
-
if (langArr === void 0) {
|
|
1516
|
-
langArr = [];
|
|
1517
|
-
typeMap.set(nameKey, langArr);
|
|
1518
|
-
}
|
|
1499
|
+
const typeMap = tree.get(typeKey) ?? /* @__PURE__ */ new Map();
|
|
1500
|
+
tree.set(typeKey, typeMap);
|
|
1501
|
+
const langArr = typeMap.get(nameKey) ?? [];
|
|
1502
|
+
typeMap.set(nameKey, langArr);
|
|
1519
1503
|
langArr.push({
|
|
1520
1504
|
language: leaf.language,
|
|
1521
1505
|
codePage: leaf.codePage,
|
|
@@ -1525,11 +1509,8 @@ var Injector = class {
|
|
|
1525
1509
|
}
|
|
1526
1510
|
const blobTypeKey = `i:${String(10)}`;
|
|
1527
1511
|
const blobNameKey = `n:${blobName}`;
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
typeMap = /* @__PURE__ */ new Map();
|
|
1531
|
-
tree.set(blobTypeKey, typeMap);
|
|
1532
|
-
}
|
|
1512
|
+
const typeMap = tree.get(blobTypeKey) ?? /* @__PURE__ */ new Map();
|
|
1513
|
+
tree.set(blobTypeKey, typeMap);
|
|
1533
1514
|
typeMap.set(blobNameKey, [{
|
|
1534
1515
|
language: 0,
|
|
1535
1516
|
codePage: 0,
|
|
@@ -1539,100 +1520,96 @@ var Injector = class {
|
|
|
1539
1520
|
return tree;
|
|
1540
1521
|
}
|
|
1541
1522
|
#serializeResourceTree(tree, leafDataMap, blobSize) {
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
dirTableSize += 16 + tree.size * 8;
|
|
1523
|
+
this.#dirTableSize = 0;
|
|
1524
|
+
this.#stringPoolSize = 0;
|
|
1525
|
+
this.#dirTableSize += 16 + tree.size * 8;
|
|
1546
1526
|
const allStrings = [];
|
|
1547
1527
|
const allLeafEntries = [];
|
|
1548
1528
|
for (const [typeKey, nameMap] of tree) {
|
|
1549
|
-
dirTableSize += 16 + nameMap.size * 8;
|
|
1529
|
+
this.#dirTableSize += 16 + nameMap.size * 8;
|
|
1550
1530
|
if (typeKey.startsWith("n:")) {
|
|
1551
1531
|
const str = typeKey.slice(2);
|
|
1552
1532
|
allStrings.push({
|
|
1553
1533
|
key: typeKey,
|
|
1554
1534
|
value: str
|
|
1555
1535
|
});
|
|
1556
|
-
stringPoolSize += 2 + str.length * 2;
|
|
1557
|
-
stringPoolSize = alignTo(stringPoolSize, 2);
|
|
1536
|
+
this.#stringPoolSize += 2 + str.length * 2;
|
|
1537
|
+
this.#stringPoolSize = alignTo(this.#stringPoolSize, 2);
|
|
1558
1538
|
}
|
|
1559
1539
|
for (const [nameKey, langEntries] of nameMap) {
|
|
1560
|
-
dirTableSize += 16 + langEntries.length * 8;
|
|
1540
|
+
this.#dirTableSize += 16 + langEntries.length * 8;
|
|
1561
1541
|
if (nameKey.startsWith("n:")) {
|
|
1562
1542
|
const str = nameKey.slice(2);
|
|
1563
1543
|
allStrings.push({
|
|
1564
1544
|
key: nameKey,
|
|
1565
1545
|
value: str
|
|
1566
1546
|
});
|
|
1567
|
-
stringPoolSize += 2 + str.length * 2;
|
|
1568
|
-
stringPoolSize = alignTo(stringPoolSize, 2);
|
|
1569
|
-
}
|
|
1570
|
-
for (const entry of langEntries) {
|
|
1571
|
-
allLeafEntries.push({
|
|
1572
|
-
leafIndex: entry.leafIndex,
|
|
1573
|
-
dataSize: entry.dataSize,
|
|
1574
|
-
codePage: entry.codePage
|
|
1575
|
-
});
|
|
1576
|
-
dataEntryCount++;
|
|
1547
|
+
this.#stringPoolSize += 2 + str.length * 2;
|
|
1548
|
+
this.#stringPoolSize = alignTo(this.#stringPoolSize, 2);
|
|
1577
1549
|
}
|
|
1550
|
+
for (const entry of langEntries) allLeafEntries.push({
|
|
1551
|
+
leafIndex: entry.leafIndex,
|
|
1552
|
+
dataSize: entry.dataSize,
|
|
1553
|
+
codePage: entry.codePage
|
|
1554
|
+
});
|
|
1578
1555
|
}
|
|
1579
1556
|
}
|
|
1580
|
-
const dataEntriesSize =
|
|
1581
|
-
const headerSize = dirTableSize + stringPoolSize + dataEntriesSize;
|
|
1582
|
-
|
|
1557
|
+
const dataEntriesSize = allLeafEntries.length * 16;
|
|
1558
|
+
const headerSize = this.#dirTableSize + this.#stringPoolSize + dataEntriesSize;
|
|
1559
|
+
this.#dataRegionOffset = 0;
|
|
1583
1560
|
const leafFileOffsets = /* @__PURE__ */ new Map();
|
|
1584
|
-
for (
|
|
1561
|
+
for (const i of Array.prototype.keys.call(allLeafEntries)) {
|
|
1585
1562
|
const entry = allLeafEntries[i];
|
|
1586
1563
|
if (entry === void 0) continue;
|
|
1587
1564
|
if (entry.leafIndex === -1) continue;
|
|
1588
1565
|
const data = leafDataMap.get(entry.leafIndex);
|
|
1589
1566
|
if (data !== void 0) {
|
|
1590
|
-
leafFileOffsets.set(entry.leafIndex, dataRegionOffset);
|
|
1591
|
-
dataRegionOffset += alignTo(data.length, 4);
|
|
1567
|
+
leafFileOffsets.set(entry.leafIndex, this.#dataRegionOffset);
|
|
1568
|
+
this.#dataRegionOffset += alignTo(data.length, 4);
|
|
1592
1569
|
}
|
|
1593
1570
|
}
|
|
1594
|
-
const blobDataOffset = dataRegionOffset;
|
|
1595
|
-
dataRegionOffset += blobSize;
|
|
1596
|
-
const dataRegionSize = dataRegionOffset;
|
|
1571
|
+
const blobDataOffset = this.#dataRegionOffset;
|
|
1572
|
+
this.#dataRegionOffset += blobSize;
|
|
1573
|
+
const dataRegionSize = this.#dataRegionOffset;
|
|
1597
1574
|
const buf = Buffer.alloc(headerSize);
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1575
|
+
this.#dirPos = 0;
|
|
1576
|
+
this.#stringPos = this.#dirTableSize;
|
|
1577
|
+
this.#dataEntryPos = this.#dirTableSize + this.#stringPoolSize;
|
|
1601
1578
|
const stringOffsets = /* @__PURE__ */ new Map();
|
|
1602
1579
|
for (const { key, value } of allStrings) {
|
|
1603
|
-
stringOffsets.set(key, stringPos);
|
|
1604
|
-
buf.writeUInt16LE(value.length, stringPos);
|
|
1605
|
-
buf.write(value, stringPos + 2, value.length * 2, "utf16le");
|
|
1606
|
-
stringPos += 2 + value.length * 2;
|
|
1607
|
-
stringPos = alignTo(stringPos, 2);
|
|
1580
|
+
stringOffsets.set(key, this.#stringPos);
|
|
1581
|
+
buf.writeUInt16LE(value.length, this.#stringPos);
|
|
1582
|
+
buf.write(value, this.#stringPos + 2, value.length * 2, "utf16le");
|
|
1583
|
+
this.#stringPos += 2 + value.length * 2;
|
|
1584
|
+
this.#stringPos = alignTo(this.#stringPos, 2);
|
|
1608
1585
|
}
|
|
1609
1586
|
const sortedTypeKeys = this.#sortResourceKeys([...tree.keys()]);
|
|
1610
1587
|
const nextDirOffset = 16 + tree.size * 8;
|
|
1611
1588
|
const namedTypes = sortedTypeKeys.filter((k) => k.startsWith("n:"));
|
|
1612
1589
|
const idTypes = sortedTypeKeys.filter((k) => k.startsWith("i:"));
|
|
1613
|
-
buf.writeUInt32LE(0, dirPos);
|
|
1614
|
-
buf.writeUInt32LE(0, dirPos + 4);
|
|
1615
|
-
buf.writeUInt16LE(0, dirPos + 8);
|
|
1616
|
-
buf.writeUInt16LE(0, dirPos + 10);
|
|
1617
|
-
buf.writeUInt16LE(namedTypes.length, dirPos + 12);
|
|
1618
|
-
buf.writeUInt16LE(idTypes.length, dirPos + 14);
|
|
1619
|
-
dirPos += 16;
|
|
1590
|
+
buf.writeUInt32LE(0, this.#dirPos);
|
|
1591
|
+
buf.writeUInt32LE(0, this.#dirPos + 4);
|
|
1592
|
+
buf.writeUInt16LE(0, this.#dirPos + 8);
|
|
1593
|
+
buf.writeUInt16LE(0, this.#dirPos + 10);
|
|
1594
|
+
buf.writeUInt16LE(namedTypes.length, this.#dirPos + 12);
|
|
1595
|
+
buf.writeUInt16LE(idTypes.length, this.#dirPos + 14);
|
|
1596
|
+
this.#dirPos += 16;
|
|
1620
1597
|
const typeDirOffsets = /* @__PURE__ */ new Map();
|
|
1621
|
-
|
|
1598
|
+
this.#accDirOffset = nextDirOffset;
|
|
1622
1599
|
for (const typeKey of sortedTypeKeys) {
|
|
1623
|
-
typeDirOffsets.set(typeKey, accDirOffset);
|
|
1600
|
+
typeDirOffsets.set(typeKey, this.#accDirOffset);
|
|
1624
1601
|
const nameMap = tree.get(typeKey);
|
|
1625
1602
|
if (nameMap === void 0) continue;
|
|
1626
1603
|
const typeEntryCount = nameMap.size;
|
|
1627
|
-
accDirOffset += 16 + typeEntryCount * 8;
|
|
1628
|
-
for (const [, langEntries] of nameMap) accDirOffset += 16 + langEntries.length * 8;
|
|
1604
|
+
this.#accDirOffset += 16 + typeEntryCount * 8;
|
|
1605
|
+
for (const [, langEntries] of nameMap) this.#accDirOffset += 16 + langEntries.length * 8;
|
|
1629
1606
|
}
|
|
1630
1607
|
for (const typeKey of sortedTypeKeys) {
|
|
1631
1608
|
const nameOrId = typeKey.startsWith("n:") ? (stringOffsets.get(typeKey) ?? 0) | PE_RESOURCE_NAME_FLAG : parseInt(typeKey.slice(2), 10);
|
|
1632
1609
|
const subdirOffset = (typeDirOffsets.get(typeKey) ?? 0) | PE_RESOURCE_SUBDIR_FLAG;
|
|
1633
|
-
buf.writeUInt32LE(nameOrId >>> 0, dirPos);
|
|
1634
|
-
buf.writeUInt32LE(subdirOffset >>> 0, dirPos + 4);
|
|
1635
|
-
dirPos += 8;
|
|
1610
|
+
buf.writeUInt32LE(nameOrId >>> 0, this.#dirPos);
|
|
1611
|
+
buf.writeUInt32LE(subdirOffset >>> 0, this.#dirPos + 4);
|
|
1612
|
+
this.#dirPos += 8;
|
|
1636
1613
|
}
|
|
1637
1614
|
for (const typeKey of sortedTypeKeys) {
|
|
1638
1615
|
const nameMap = tree.get(typeKey);
|
|
@@ -1640,52 +1617,48 @@ var Injector = class {
|
|
|
1640
1617
|
const sortedNameKeys = this.#sortResourceKeys([...nameMap.keys()]);
|
|
1641
1618
|
const namedNames = sortedNameKeys.filter((k) => k.startsWith("n:"));
|
|
1642
1619
|
const idNames = sortedNameKeys.filter((k) => k.startsWith("i:"));
|
|
1643
|
-
buf.writeUInt32LE(0, dirPos);
|
|
1644
|
-
buf.writeUInt32LE(0, dirPos + 4);
|
|
1645
|
-
buf.writeUInt16LE(0, dirPos + 8);
|
|
1646
|
-
buf.writeUInt16LE(0, dirPos + 10);
|
|
1647
|
-
buf.writeUInt16LE(namedNames.length, dirPos + 12);
|
|
1648
|
-
buf.writeUInt16LE(idNames.length, dirPos + 14);
|
|
1649
|
-
dirPos += 16;
|
|
1650
|
-
|
|
1620
|
+
buf.writeUInt32LE(0, this.#dirPos);
|
|
1621
|
+
buf.writeUInt32LE(0, this.#dirPos + 4);
|
|
1622
|
+
buf.writeUInt16LE(0, this.#dirPos + 8);
|
|
1623
|
+
buf.writeUInt16LE(0, this.#dirPos + 10);
|
|
1624
|
+
buf.writeUInt16LE(namedNames.length, this.#dirPos + 12);
|
|
1625
|
+
buf.writeUInt16LE(idNames.length, this.#dirPos + 14);
|
|
1626
|
+
this.#dirPos += 16;
|
|
1627
|
+
this.#langSubdirCursor = this.#dirPos + sortedNameKeys.length * 8;
|
|
1651
1628
|
for (const nameKey of sortedNameKeys) {
|
|
1652
1629
|
const nameOrId = nameKey.startsWith("n:") ? (stringOffsets.get(nameKey) ?? 0) | PE_RESOURCE_NAME_FLAG : parseInt(nameKey.slice(2), 10);
|
|
1653
|
-
const langSubDirRelOffset = langSubdirCursor | PE_RESOURCE_SUBDIR_FLAG;
|
|
1654
|
-
buf.writeUInt32LE(nameOrId >>> 0, dirPos);
|
|
1655
|
-
buf.writeUInt32LE(langSubDirRelOffset >>> 0, dirPos + 4);
|
|
1656
|
-
dirPos += 8;
|
|
1630
|
+
const langSubDirRelOffset = this.#langSubdirCursor | PE_RESOURCE_SUBDIR_FLAG;
|
|
1631
|
+
buf.writeUInt32LE(nameOrId >>> 0, this.#dirPos);
|
|
1632
|
+
buf.writeUInt32LE(langSubDirRelOffset >>> 0, this.#dirPos + 4);
|
|
1633
|
+
this.#dirPos += 8;
|
|
1657
1634
|
const langEntries = nameMap.get(nameKey);
|
|
1658
|
-
if (langEntries !== void 0) langSubdirCursor += 16 + langEntries.length * 8;
|
|
1635
|
+
if (langEntries !== void 0) this.#langSubdirCursor += 16 + langEntries.length * 8;
|
|
1659
1636
|
}
|
|
1660
1637
|
for (const nameKey of sortedNameKeys) {
|
|
1661
1638
|
const langEntries = nameMap.get(nameKey);
|
|
1662
1639
|
if (langEntries === void 0) continue;
|
|
1663
|
-
buf.writeUInt32LE(0, dirPos);
|
|
1664
|
-
buf.writeUInt32LE(0, dirPos + 4);
|
|
1665
|
-
buf.writeUInt16LE(0, dirPos + 8);
|
|
1666
|
-
buf.writeUInt16LE(0, dirPos + 10);
|
|
1667
|
-
buf.writeUInt16LE(0, dirPos + 12);
|
|
1668
|
-
buf.writeUInt16LE(langEntries.length, dirPos + 14);
|
|
1669
|
-
dirPos += 16;
|
|
1640
|
+
buf.writeUInt32LE(0, this.#dirPos);
|
|
1641
|
+
buf.writeUInt32LE(0, this.#dirPos + 4);
|
|
1642
|
+
buf.writeUInt16LE(0, this.#dirPos + 8);
|
|
1643
|
+
buf.writeUInt16LE(0, this.#dirPos + 10);
|
|
1644
|
+
buf.writeUInt16LE(0, this.#dirPos + 12);
|
|
1645
|
+
buf.writeUInt16LE(langEntries.length, this.#dirPos + 14);
|
|
1646
|
+
this.#dirPos += 16;
|
|
1670
1647
|
for (const entry of langEntries) {
|
|
1671
|
-
buf.writeUInt32LE(entry.language, dirPos);
|
|
1672
|
-
buf.writeUInt32LE(dataEntryPos, dirPos + 4);
|
|
1673
|
-
dirPos += 8;
|
|
1674
|
-
|
|
1675
|
-
if (
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
buf.writeUInt32LE(entry.dataSize, dataEntryPos + 4);
|
|
1686
|
-
buf.writeUInt32LE(entry.codePage, dataEntryPos + 8);
|
|
1687
|
-
buf.writeUInt32LE(0, dataEntryPos + 12);
|
|
1688
|
-
dataEntryPos += 16;
|
|
1648
|
+
buf.writeUInt32LE(entry.language, this.#dirPos);
|
|
1649
|
+
buf.writeUInt32LE(this.#dataEntryPos, this.#dirPos + 4);
|
|
1650
|
+
this.#dirPos += 8;
|
|
1651
|
+
const resolvedOffset = entry.leafIndex === -1 ? blobDataOffset : leafFileOffsets.get(entry.leafIndex);
|
|
1652
|
+
if (resolvedOffset === void 0) throw new SEAError("INJECT", "unresolvable resource RVA", {
|
|
1653
|
+
executable: this.#options.executable,
|
|
1654
|
+
leafIndex: entry.leafIndex
|
|
1655
|
+
});
|
|
1656
|
+
const dataOffset = headerSize + resolvedOffset;
|
|
1657
|
+
buf.writeUInt32LE(dataOffset, this.#dataEntryPos);
|
|
1658
|
+
buf.writeUInt32LE(entry.dataSize, this.#dataEntryPos + 4);
|
|
1659
|
+
buf.writeUInt32LE(entry.codePage, this.#dataEntryPos + 8);
|
|
1660
|
+
buf.writeUInt32LE(0, this.#dataEntryPos + 12);
|
|
1661
|
+
this.#dataEntryPos += 16;
|
|
1689
1662
|
}
|
|
1690
1663
|
}
|
|
1691
1664
|
}
|
|
@@ -1698,7 +1671,7 @@ var Injector = class {
|
|
|
1698
1671
|
#fixupDataEntries(buf, dirOffset, sectionVA) {
|
|
1699
1672
|
if (dirOffset + 16 > buf.length) return;
|
|
1700
1673
|
const totalEntries = buf.readUInt16LE(dirOffset + 12) + buf.readUInt16LE(dirOffset + 14);
|
|
1701
|
-
for (
|
|
1674
|
+
for (const i of Array.prototype.keys.call({ length: totalEntries })) {
|
|
1702
1675
|
const entryOffset = dirOffset + 16 + i * 8;
|
|
1703
1676
|
if (entryOffset + 8 > buf.length) return;
|
|
1704
1677
|
const offsetOrData = buf.readUInt32LE(entryOffset + 4);
|
|
@@ -1718,25 +1691,22 @@ var Injector = class {
|
|
|
1718
1691
|
const fileSize = statSync(this.#options.executable).size;
|
|
1719
1692
|
const chunkSize = 4194304;
|
|
1720
1693
|
const chunk = Buffer.alloc(Math.min(chunkSize, fileSize));
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
while (
|
|
1724
|
-
const toRead = Math.min(chunkSize, fileSize -
|
|
1725
|
-
const bytesRead = readSync(fd, chunk, 0, toRead,
|
|
1694
|
+
this.#checksumSum = 0;
|
|
1695
|
+
this.#checksumOffset = 0;
|
|
1696
|
+
while (this.#checksumOffset < fileSize) {
|
|
1697
|
+
const toRead = Math.min(chunkSize, fileSize - this.#checksumOffset);
|
|
1698
|
+
const bytesRead = readSync(fd, chunk, 0, toRead, this.#checksumOffset);
|
|
1726
1699
|
if (bytesRead === 0) break;
|
|
1727
|
-
for (
|
|
1728
|
-
const absolute =
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
else word = chunk.readUInt8(i);
|
|
1733
|
-
sum += word;
|
|
1734
|
-
sum = (sum & 65535) + (sum >>> 16);
|
|
1700
|
+
for (const i of Array.prototype.keys.call({ length: Math.ceil(bytesRead / 2) }).map((index) => index * 2)) {
|
|
1701
|
+
const absolute = this.#checksumOffset + i;
|
|
1702
|
+
const word = absolute === checksumOffset || absolute === checksumOffset + 2 ? 0 : i + 1 < bytesRead ? chunk.readUInt16LE(i) : chunk.readUInt8(i);
|
|
1703
|
+
this.#checksumSum += word;
|
|
1704
|
+
this.#checksumSum = (this.#checksumSum & 65535) + (this.#checksumSum >>> 16);
|
|
1735
1705
|
}
|
|
1736
|
-
|
|
1706
|
+
this.#checksumOffset += bytesRead;
|
|
1737
1707
|
}
|
|
1738
|
-
|
|
1739
|
-
writeU32(fd, checksumOffset,
|
|
1708
|
+
this.#checksumSum = (this.#checksumSum & 65535) + (this.#checksumSum >>> 16);
|
|
1709
|
+
writeU32(fd, checksumOffset, this.#checksumSum + fileSize >>> 0);
|
|
1740
1710
|
} finally {
|
|
1741
1711
|
closeSync(fd);
|
|
1742
1712
|
}
|
|
@@ -1752,54 +1722,12 @@ var Injector = class {
|
|
|
1752
1722
|
const exePath = this.#options.executable;
|
|
1753
1723
|
const resource = this.#options.resource;
|
|
1754
1724
|
const overwrite = this.#options.overwrite !== false;
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
let headers;
|
|
1759
|
-
const fd = openSync(exePath, "r");
|
|
1760
|
-
try {
|
|
1761
|
-
const identBuf = Buffer.alloc(16);
|
|
1762
|
-
readSync(fd, identBuf, 0, 16, 0);
|
|
1763
|
-
const elfClass = identBuf.readUInt8(4);
|
|
1764
|
-
const elfData = identBuf.readUInt8(5);
|
|
1765
|
-
if (elfClass !== 2) throw new SEAError("FORMAT", "Only 64-bit ELF executables are supported", {
|
|
1766
|
-
executable: exePath,
|
|
1767
|
-
elfClass
|
|
1768
|
-
});
|
|
1769
|
-
if (elfData !== 1) throw new SEAError("FORMAT", "Only little-endian ELF executables are supported", {
|
|
1770
|
-
executable: exePath,
|
|
1771
|
-
elfData
|
|
1772
|
-
});
|
|
1773
|
-
phdrOffset = Number(readU64(fd, 32));
|
|
1774
|
-
phdrEntrySize = readU16(fd, 54);
|
|
1775
|
-
phdrCount = readU16(fd, 56);
|
|
1776
|
-
headers = this.#readELFProgramHeaders(fd, phdrOffset, phdrEntrySize, phdrCount);
|
|
1777
|
-
const stale = /* @__PURE__ */ new Set();
|
|
1778
|
-
for (let i = 0; i < headers.length; i++) {
|
|
1779
|
-
const header = headers[i];
|
|
1780
|
-
if (header === void 0 || header.type !== 4) continue;
|
|
1781
|
-
const existingName = this.#readELFNoteName(fd, header.offset, header.filesz);
|
|
1782
|
-
if (existingName === void 0 || !existingName.startsWith("NODE_SEA")) continue;
|
|
1783
|
-
if (!overwrite) throw new SEAError("INJECT", `Note with name "${resource}" already exists`, {
|
|
1784
|
-
executable: exePath,
|
|
1785
|
-
resource
|
|
1786
|
-
});
|
|
1787
|
-
stale.add(i);
|
|
1788
|
-
}
|
|
1789
|
-
headers = headers.map((header, index) => stale.has(index) ? {
|
|
1790
|
-
...header,
|
|
1791
|
-
type: 0
|
|
1792
|
-
} : header);
|
|
1793
|
-
} finally {
|
|
1794
|
-
closeSync(fd);
|
|
1795
|
-
}
|
|
1796
|
-
let maxVaddrEnd = 0;
|
|
1797
|
-
for (const header of headers) {
|
|
1798
|
-
if (header.type !== 1) continue;
|
|
1725
|
+
const { phdrEntrySize, headers } = this.#prepareELF(exePath, resource, overwrite);
|
|
1726
|
+
const regionVaddr = alignTo(headers.reduce((highest, header) => {
|
|
1727
|
+
if (header.type !== 1) return highest;
|
|
1799
1728
|
const end = header.vaddr + header.memsz;
|
|
1800
|
-
|
|
1801
|
-
}
|
|
1802
|
-
const regionVaddr = alignTo(maxVaddrEnd, ELF_PAGE_SIZE);
|
|
1729
|
+
return end > highest ? end : highest;
|
|
1730
|
+
}, 0), ELF_PAGE_SIZE);
|
|
1803
1731
|
const blobSize = statSync(this.#options.blob).size;
|
|
1804
1732
|
const { header: noteHeader, total: noteTotal } = buildELFNoteHeader(resource, blobSize);
|
|
1805
1733
|
const noteAreaSize = alignTo(noteTotal, 8);
|
|
@@ -1843,7 +1771,7 @@ var Injector = class {
|
|
|
1843
1771
|
});
|
|
1844
1772
|
finalHeaders.push(newLoad, newNote);
|
|
1845
1773
|
const phtBuffer = Buffer.alloc(phtSize);
|
|
1846
|
-
for (
|
|
1774
|
+
for (const i of Array.prototype.keys.call(finalHeaders)) {
|
|
1847
1775
|
const entry = finalHeaders[i];
|
|
1848
1776
|
if (entry === void 0) continue;
|
|
1849
1777
|
this.#writeELFProgramHeaderEntry(phtBuffer, i * phdrEntrySize, entry);
|
|
@@ -1865,9 +1793,51 @@ var Injector = class {
|
|
|
1865
1793
|
closeSync(fd2);
|
|
1866
1794
|
}
|
|
1867
1795
|
}
|
|
1796
|
+
#prepareELF(exePath, resource, overwrite) {
|
|
1797
|
+
const fd = openSync(exePath, "r");
|
|
1798
|
+
try {
|
|
1799
|
+
const identBuf = Buffer.alloc(16);
|
|
1800
|
+
readSync(fd, identBuf, 0, 16, 0);
|
|
1801
|
+
const elfClass = identBuf.readUInt8(4);
|
|
1802
|
+
const elfData = identBuf.readUInt8(5);
|
|
1803
|
+
if (elfClass !== 2) throw new SEAError("FORMAT", "Only 64-bit ELF executables are supported", {
|
|
1804
|
+
executable: exePath,
|
|
1805
|
+
elfClass
|
|
1806
|
+
});
|
|
1807
|
+
if (elfData !== 1) throw new SEAError("FORMAT", "Only little-endian ELF executables are supported", {
|
|
1808
|
+
executable: exePath,
|
|
1809
|
+
elfData
|
|
1810
|
+
});
|
|
1811
|
+
const phdrOffset = Number(readU64(fd, 32));
|
|
1812
|
+
const phdrEntrySize = readU16(fd, 54);
|
|
1813
|
+
const phdrCount = readU16(fd, 56);
|
|
1814
|
+
const headers = this.#readELFProgramHeaders(fd, phdrOffset, phdrEntrySize, phdrCount);
|
|
1815
|
+
const stale = /* @__PURE__ */ new Set();
|
|
1816
|
+
for (const i of Array.prototype.keys.call(headers)) {
|
|
1817
|
+
const header = headers[i];
|
|
1818
|
+
if (header === void 0 || header.type !== 4) continue;
|
|
1819
|
+
const existingName = this.#readELFNoteName(fd, header.offset, header.filesz);
|
|
1820
|
+
if (existingName === void 0 || !existingName.startsWith("NODE_SEA")) continue;
|
|
1821
|
+
if (!overwrite) throw new SEAError("INJECT", `Note with name "${resource}" already exists`, {
|
|
1822
|
+
executable: exePath,
|
|
1823
|
+
resource
|
|
1824
|
+
});
|
|
1825
|
+
stale.add(i);
|
|
1826
|
+
}
|
|
1827
|
+
return {
|
|
1828
|
+
phdrEntrySize,
|
|
1829
|
+
headers: headers.map((header, index) => stale.has(index) ? {
|
|
1830
|
+
...header,
|
|
1831
|
+
type: 0
|
|
1832
|
+
} : header)
|
|
1833
|
+
};
|
|
1834
|
+
} finally {
|
|
1835
|
+
closeSync(fd);
|
|
1836
|
+
}
|
|
1837
|
+
}
|
|
1868
1838
|
#readELFProgramHeaders(fd, phdrOffset, entrySize, count) {
|
|
1869
1839
|
const headers = [];
|
|
1870
|
-
for (
|
|
1840
|
+
for (const i of Array.prototype.keys.call({ length: count })) {
|
|
1871
1841
|
const off = phdrOffset + i * entrySize;
|
|
1872
1842
|
headers.push({
|
|
1873
1843
|
type: readU32(fd, off),
|
|
@@ -1921,14 +1891,11 @@ var Injector = class {
|
|
|
1921
1891
|
#injectMachO() {
|
|
1922
1892
|
const exePath = this.#options.executable;
|
|
1923
1893
|
const fileSize = statSync(exePath).size;
|
|
1924
|
-
|
|
1925
|
-
let destFd;
|
|
1926
|
-
let blobFd;
|
|
1927
|
-
let injTemp;
|
|
1894
|
+
this.#srcFd = openSync(exePath, "r");
|
|
1928
1895
|
try {
|
|
1929
1896
|
const headerSize = 32;
|
|
1930
1897
|
const preHeader = Buffer.alloc(headerSize);
|
|
1931
|
-
readSync(srcFd, preHeader, 0, headerSize, 0);
|
|
1898
|
+
readSync(this.#srcFd, preHeader, 0, headerSize, 0);
|
|
1932
1899
|
const preSizeofcmds = preHeader.readUInt32LE(20);
|
|
1933
1900
|
if (headerSize + preSizeofcmds > fileSize) throw new SEAError("FORMAT", "Mach-O load command region exceeds file size", {
|
|
1934
1901
|
executable: exePath,
|
|
@@ -1937,7 +1904,7 @@ var Injector = class {
|
|
|
1937
1904
|
fileSize
|
|
1938
1905
|
});
|
|
1939
1906
|
const buf = Buffer.alloc(headerSize + preSizeofcmds);
|
|
1940
|
-
readSync(srcFd, buf, 0, headerSize + preSizeofcmds, 0);
|
|
1907
|
+
readSync(this.#srcFd, buf, 0, headerSize + preSizeofcmds, 0);
|
|
1941
1908
|
const magic = buf.readUInt32LE(0);
|
|
1942
1909
|
if (magic !== 4277009103) throw new SEAError("FORMAT", `Unsupported Mach-O magic: 0x${magic.toString(16)}`, {
|
|
1943
1910
|
executable: exePath,
|
|
@@ -1947,64 +1914,64 @@ var Injector = class {
|
|
|
1947
1914
|
const ncmds = buf.readUInt32LE(16);
|
|
1948
1915
|
const sizeofcmds = buf.readUInt32LE(20);
|
|
1949
1916
|
const segmentName = this.#options.macho?.segment ?? "NODE_SEA";
|
|
1950
|
-
|
|
1951
|
-
|
|
1917
|
+
const resource = this.#options.resource;
|
|
1918
|
+
const sectionName = resource.startsWith("__") ? resource : `__${resource}`;
|
|
1952
1919
|
const commands = [];
|
|
1953
|
-
|
|
1954
|
-
for (
|
|
1955
|
-
if (cmdOffset + 8 > buf.length) throw new SEAError("FORMAT", "Malformed Mach-O load command table", { executable: exePath });
|
|
1956
|
-
const cmdType = buf.readUInt32LE(cmdOffset);
|
|
1957
|
-
const cmdSize = buf.readUInt32LE(cmdOffset + 4);
|
|
1958
|
-
if (cmdSize < 8 || cmdOffset + cmdSize > buf.length) throw new SEAError("FORMAT", "Malformed Mach-O load command table", { executable: exePath });
|
|
1920
|
+
this.#cmdOffset = headerSize;
|
|
1921
|
+
for (const _ of Array.prototype.keys.call({ length: ncmds })) {
|
|
1922
|
+
if (this.#cmdOffset + 8 > buf.length) throw new SEAError("FORMAT", "Malformed Mach-O load command table", { executable: exePath });
|
|
1923
|
+
const cmdType = buf.readUInt32LE(this.#cmdOffset);
|
|
1924
|
+
const cmdSize = buf.readUInt32LE(this.#cmdOffset + 4);
|
|
1925
|
+
if (cmdSize < 8 || this.#cmdOffset + cmdSize > buf.length) throw new SEAError("FORMAT", "Malformed Mach-O load command table", { executable: exePath });
|
|
1959
1926
|
commands.push({
|
|
1960
1927
|
type: cmdType,
|
|
1961
1928
|
size: cmdSize,
|
|
1962
|
-
offset: cmdOffset
|
|
1929
|
+
offset: this.#cmdOffset
|
|
1963
1930
|
});
|
|
1964
|
-
cmdOffset += cmdSize;
|
|
1931
|
+
this.#cmdOffset += cmdSize;
|
|
1965
1932
|
}
|
|
1966
|
-
|
|
1933
|
+
this.#linkeditIndex = -1;
|
|
1967
1934
|
const existingSegmentIndices = [];
|
|
1968
|
-
for (
|
|
1935
|
+
for (const i of Array.prototype.keys.call(commands)) {
|
|
1969
1936
|
const cmd = commands[i];
|
|
1970
1937
|
if (cmd === void 0 || cmd.type !== 25) continue;
|
|
1971
1938
|
const segName = stripTrailingNulls(buf.subarray(cmd.offset + 8, cmd.offset + 24).toString("ascii"));
|
|
1972
|
-
if (segName === "__LINKEDIT") linkeditIndex = i;
|
|
1939
|
+
if (segName === "__LINKEDIT") this.#linkeditIndex = i;
|
|
1973
1940
|
if (segName === segmentName) existingSegmentIndices.push(i);
|
|
1974
1941
|
}
|
|
1975
1942
|
if (existingSegmentIndices.length > 0 && this.#options.overwrite === false) throw new SEAError("INJECT", `Segment "${segmentName}" already exists`, {
|
|
1976
1943
|
executable: exePath,
|
|
1977
1944
|
segmentName
|
|
1978
1945
|
});
|
|
1979
|
-
const linkeditCmd = linkeditIndex === -1 ? void 0 : commands[linkeditIndex];
|
|
1946
|
+
const linkeditCmd = this.#linkeditIndex === -1 ? void 0 : commands[this.#linkeditIndex];
|
|
1980
1947
|
if (linkeditCmd === void 0) throw new SEAError("ROOM", "Mach-O binary has no __LINKEDIT segment", { executable: exePath });
|
|
1981
1948
|
const linkeditOffset = Number(buf.readBigUInt64LE(linkeditCmd.offset + 40));
|
|
1982
1949
|
const linkeditSize = Number(buf.readBigUInt64LE(linkeditCmd.offset + 48));
|
|
1983
1950
|
const linkeditAddress = buf.readBigUInt64LE(linkeditCmd.offset + 24);
|
|
1984
1951
|
const blobSize = statSync(this.#options.blob).size;
|
|
1985
1952
|
const shift = alignTo(blobSize, cputype === 16777228 ? 16384 : 4096);
|
|
1986
|
-
|
|
1953
|
+
this.#firstSectionOffset = fileSize;
|
|
1987
1954
|
for (const cmd of commands) {
|
|
1988
1955
|
if (cmd.type !== 25) continue;
|
|
1989
1956
|
const nsects = buf.readUInt32LE(cmd.offset + 64);
|
|
1990
|
-
for (
|
|
1957
|
+
for (const s of Array.prototype.keys.call({ length: nsects })) {
|
|
1991
1958
|
const sectOff = cmd.offset + 72 + s * 80;
|
|
1992
1959
|
if (sectOff + 80 > buf.length) continue;
|
|
1993
1960
|
const size = Number(buf.readBigUInt64LE(sectOff + 40));
|
|
1994
1961
|
const offset = buf.readUInt32LE(sectOff + 48);
|
|
1995
|
-
if (size > 0 && offset > 0 && offset < firstSectionOffset) firstSectionOffset = offset;
|
|
1962
|
+
if (size > 0 && offset > 0 && offset < this.#firstSectionOffset) this.#firstSectionOffset = offset;
|
|
1996
1963
|
}
|
|
1997
1964
|
}
|
|
1998
1965
|
const newCmdSize = 152;
|
|
1999
1966
|
const removedSize = commands.filter((_c, i) => existingSegmentIndices.includes(i)).reduce((sum, c) => sum + c.size, 0);
|
|
2000
1967
|
const newNcmds = commands.length - existingSegmentIndices.length + 1;
|
|
2001
1968
|
const newSizeofcmds = sizeofcmds - removedSize + newCmdSize;
|
|
2002
|
-
if (headerSize + newSizeofcmds + 16 > firstSectionOffset) throw new SEAError("ROOM", `Not enough header space for new Mach-O load command (need offset ${String(headerSize + newSizeofcmds + 16)}, first section at ${String(firstSectionOffset)})`, {
|
|
1969
|
+
if (headerSize + newSizeofcmds + 16 > this.#firstSectionOffset) throw new SEAError("ROOM", `Not enough header space for new Mach-O load command (need offset ${String(headerSize + newSizeofcmds + 16)}, first section at ${String(this.#firstSectionOffset)})`, {
|
|
2003
1970
|
executable: exePath,
|
|
2004
|
-
firstSectionOffset,
|
|
1971
|
+
firstSectionOffset: this.#firstSectionOffset,
|
|
2005
1972
|
requiredOffset: headerSize + newSizeofcmds + 16
|
|
2006
1973
|
});
|
|
2007
|
-
for (
|
|
1974
|
+
for (const i of Array.prototype.keys.call(commands)) {
|
|
2008
1975
|
const cmd = commands[i];
|
|
2009
1976
|
if (cmd === void 0 || existingSegmentIndices.includes(i)) continue;
|
|
2010
1977
|
if (cmd.type === 25) {
|
|
@@ -2044,83 +2011,69 @@ var Injector = class {
|
|
|
2044
2011
|
cmd.writeUInt32LE(0, 144);
|
|
2045
2012
|
cmd.writeUInt32LE(0, 148);
|
|
2046
2013
|
const pieces = [];
|
|
2047
|
-
for (
|
|
2014
|
+
for (const i of Array.prototype.keys.call(commands)) {
|
|
2048
2015
|
if (existingSegmentIndices.includes(i)) continue;
|
|
2049
2016
|
const c = commands[i];
|
|
2050
2017
|
if (c === void 0) continue;
|
|
2051
|
-
if (i === linkeditIndex) pieces.push(cmd);
|
|
2018
|
+
if (i === this.#linkeditIndex) pieces.push(cmd);
|
|
2052
2019
|
pieces.push(Buffer.from(buf.subarray(c.offset, c.offset + c.size)));
|
|
2053
2020
|
}
|
|
2054
2021
|
const newLoadCmdsBuf = Buffer.concat(pieces);
|
|
2055
2022
|
if (newLoadCmdsBuf.length !== newSizeofcmds) throw new SEAError("INJECT", "Internal Mach-O load command size mismatch", { executable: exePath });
|
|
2056
|
-
const mode = fstatSync(srcFd).mode;
|
|
2057
|
-
injTemp = join(dirname(exePath), `.inject-${randomUUID()}.tmp`);
|
|
2058
|
-
destFd = openSync(injTemp, "w", mode);
|
|
2059
|
-
writeSync(destFd, buf.subarray(0, headerSize));
|
|
2060
|
-
writeSync(destFd, newLoadCmdsBuf);
|
|
2061
|
-
copyRange(srcFd, destFd, headerSize + newSizeofcmds, linkeditOffset - (headerSize + newSizeofcmds));
|
|
2062
|
-
blobFd = openSync(this.#options.blob, "r");
|
|
2063
|
-
copyRange(blobFd, destFd, 0, blobSize);
|
|
2064
|
-
closeSync(blobFd);
|
|
2065
|
-
blobFd = void 0;
|
|
2023
|
+
const mode = fstatSync(this.#srcFd).mode;
|
|
2024
|
+
this.#injTemp = join(dirname(exePath), `.inject-${randomUUID()}.tmp`);
|
|
2025
|
+
this.#destFd = openSync(this.#injTemp, "w", mode);
|
|
2026
|
+
writeSync(this.#destFd, buf.subarray(0, headerSize));
|
|
2027
|
+
writeSync(this.#destFd, newLoadCmdsBuf);
|
|
2028
|
+
copyRange(this.#srcFd, this.#destFd, headerSize + newSizeofcmds, linkeditOffset - (headerSize + newSizeofcmds));
|
|
2029
|
+
this.#blobFd = openSync(this.#options.blob, "r");
|
|
2030
|
+
copyRange(this.#blobFd, this.#destFd, 0, blobSize);
|
|
2031
|
+
closeSync(this.#blobFd);
|
|
2032
|
+
this.#blobFd = void 0;
|
|
2066
2033
|
const blobPadding = shift - blobSize;
|
|
2067
|
-
if (blobPadding > 0) writeSync(destFd, Buffer.alloc(blobPadding));
|
|
2068
|
-
copyRange(srcFd, destFd, linkeditOffset, linkeditSize);
|
|
2069
|
-
closeSync(destFd);
|
|
2070
|
-
destFd = void 0;
|
|
2071
|
-
closeSync(srcFd);
|
|
2072
|
-
srcFd = void 0;
|
|
2073
|
-
chmodSync(injTemp, mode & 4095);
|
|
2074
|
-
finalizeExecutable(injTemp, exePath);
|
|
2075
|
-
injTemp = void 0;
|
|
2034
|
+
if (blobPadding > 0) writeSync(this.#destFd, Buffer.alloc(blobPadding));
|
|
2035
|
+
copyRange(this.#srcFd, this.#destFd, linkeditOffset, linkeditSize);
|
|
2036
|
+
closeSync(this.#destFd);
|
|
2037
|
+
this.#destFd = void 0;
|
|
2038
|
+
closeSync(this.#srcFd);
|
|
2039
|
+
this.#srcFd = void 0;
|
|
2040
|
+
chmodSync(this.#injTemp, mode & 4095);
|
|
2041
|
+
finalizeExecutable(this.#injTemp, exePath);
|
|
2042
|
+
this.#injTemp = void 0;
|
|
2076
2043
|
this.#verifyMachOSection(exePath, segmentName, sectionName, linkeditOffset, blobSize);
|
|
2077
2044
|
} finally {
|
|
2078
2045
|
for (const fd of [
|
|
2079
|
-
blobFd,
|
|
2080
|
-
destFd,
|
|
2081
|
-
srcFd
|
|
2046
|
+
this.#blobFd,
|
|
2047
|
+
this.#destFd,
|
|
2048
|
+
this.#srcFd
|
|
2082
2049
|
]) if (fd !== void 0) try {
|
|
2083
2050
|
closeSync(fd);
|
|
2084
2051
|
} catch {}
|
|
2085
|
-
if (injTemp !== void 0) rmSync(injTemp, { force: true });
|
|
2052
|
+
if (this.#injTemp !== void 0) rmSync(this.#injTemp, { force: true });
|
|
2086
2053
|
}
|
|
2087
2054
|
}
|
|
2088
2055
|
#shiftMachOLinkeditOffsets(buf, cmd, threshold, shift) {
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
32,
|
|
2111
|
-
40
|
|
2112
|
-
];
|
|
2113
|
-
break;
|
|
2114
|
-
case 29:
|
|
2115
|
-
case 30:
|
|
2116
|
-
case 38:
|
|
2117
|
-
case 41:
|
|
2118
|
-
case 2147483699:
|
|
2119
|
-
case 2147483700:
|
|
2120
|
-
offsets = [8];
|
|
2121
|
-
break;
|
|
2122
|
-
default: return;
|
|
2123
|
-
}
|
|
2056
|
+
const offsets = cmd.type === 2 ? [8, 16] : cmd.type === 11 ? [
|
|
2057
|
+
32,
|
|
2058
|
+
40,
|
|
2059
|
+
48,
|
|
2060
|
+
56,
|
|
2061
|
+
64,
|
|
2062
|
+
72
|
|
2063
|
+
] : cmd.type === 34 || cmd.type === 2147483682 ? [
|
|
2064
|
+
8,
|
|
2065
|
+
16,
|
|
2066
|
+
24,
|
|
2067
|
+
32,
|
|
2068
|
+
40
|
|
2069
|
+
] : [
|
|
2070
|
+
29,
|
|
2071
|
+
30,
|
|
2072
|
+
38,
|
|
2073
|
+
41,
|
|
2074
|
+
2147483699,
|
|
2075
|
+
2147483700
|
|
2076
|
+
].includes(cmd.type) ? [8] : [];
|
|
2124
2077
|
for (const offset of offsets) {
|
|
2125
2078
|
const fieldOffset = cmd.offset + offset;
|
|
2126
2079
|
if (fieldOffset + 4 > buf.length) continue;
|
|
@@ -2134,18 +2087,18 @@ var Injector = class {
|
|
|
2134
2087
|
const header = Buffer.alloc(32);
|
|
2135
2088
|
readSync(fd, header, 0, 32, 0);
|
|
2136
2089
|
const ncmds = header.readUInt32LE(16);
|
|
2137
|
-
|
|
2138
|
-
for (
|
|
2090
|
+
this.#verifyOffset = 32;
|
|
2091
|
+
for (const _ of Array.prototype.keys.call({ length: ncmds })) {
|
|
2139
2092
|
const cmdHead = Buffer.alloc(8);
|
|
2140
|
-
readSync(fd, cmdHead, 0, 8,
|
|
2093
|
+
readSync(fd, cmdHead, 0, 8, this.#verifyOffset);
|
|
2141
2094
|
const cmdType = cmdHead.readUInt32LE(0);
|
|
2142
2095
|
const cmdSize = cmdHead.readUInt32LE(4);
|
|
2143
2096
|
if (cmdType === 25 && cmdSize >= 72) {
|
|
2144
2097
|
const segBuf = Buffer.alloc(cmdSize);
|
|
2145
|
-
readSync(fd, segBuf, 0, cmdSize,
|
|
2098
|
+
readSync(fd, segBuf, 0, cmdSize, this.#verifyOffset);
|
|
2146
2099
|
if (stripTrailingNulls(segBuf.subarray(8, 24).toString("ascii")) === segmentName) {
|
|
2147
2100
|
const nsects = segBuf.readUInt32LE(64);
|
|
2148
|
-
for (
|
|
2101
|
+
for (const s of Array.prototype.keys.call({ length: nsects })) {
|
|
2149
2102
|
const sectOff = 72 + s * 80;
|
|
2150
2103
|
if (sectOff + 80 > segBuf.length) continue;
|
|
2151
2104
|
if (stripTrailingNulls(segBuf.subarray(sectOff, sectOff + 16).toString("ascii")) !== sectionName) continue;
|
|
@@ -2154,7 +2107,7 @@ var Injector = class {
|
|
|
2154
2107
|
}
|
|
2155
2108
|
}
|
|
2156
2109
|
}
|
|
2157
|
-
|
|
2110
|
+
this.#verifyOffset += cmdSize;
|
|
2158
2111
|
}
|
|
2159
2112
|
} finally {
|
|
2160
2113
|
closeSync(fd);
|
|
@@ -2178,182 +2131,134 @@ var Injector = class {
|
|
|
2178
2131
|
});
|
|
2179
2132
|
}
|
|
2180
2133
|
};
|
|
2181
|
-
|
|
2182
|
-
//#region src/server/seas/SEA.ts
|
|
2134
|
+
// src/server/injectors/Injector.ts
|
|
2183
2135
|
/**
|
|
2184
|
-
*
|
|
2136
|
+
* Writes a named resource into a PE, ELF, or Mach-O executable in place.
|
|
2185
2137
|
*
|
|
2186
2138
|
* @remarks
|
|
2187
|
-
*
|
|
2188
|
-
*
|
|
2189
|
-
*
|
|
2190
|
-
*
|
|
2191
|
-
*
|
|
2139
|
+
* The format is detected from the file's magic bytes at construction, and each
|
|
2140
|
+
* format takes its own strategy: PE adds an `RT_RCDATA` resource by rebuilding the
|
|
2141
|
+
* resource directory, ELF appends a `PT_NOTE` segment carrying the blob as note
|
|
2142
|
+
* data, and Mach-O appends an `LC_SEGMENT_64` load command with one section. The
|
|
2143
|
+
* blob is streamed from disk in every case, so its size is bounded by the
|
|
2144
|
+
* filesystem rather than by memory.
|
|
2192
2145
|
*
|
|
2193
|
-
* @param options -
|
|
2194
|
-
*
|
|
2146
|
+
* @param options - Injector options: the target executable, the resource name, the
|
|
2147
|
+
* blob path, and the optional sentinel fuse and Mach-O segment
|
|
2195
2148
|
*
|
|
2196
2149
|
* @example
|
|
2197
2150
|
* ```ts
|
|
2198
|
-
* const
|
|
2199
|
-
*
|
|
2200
|
-
*
|
|
2201
|
-
*
|
|
2151
|
+
* const injector = new Injector({
|
|
2152
|
+
* executable: 'dist/sea/myapp.exe',
|
|
2153
|
+
* resource: 'NODE_SEA_BLOB',
|
|
2154
|
+
* blob: 'dist/sea/sea-prep.blob',
|
|
2155
|
+
* fuse: 'NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2',
|
|
2202
2156
|
* })
|
|
2203
|
-
*
|
|
2204
|
-
*
|
|
2157
|
+
* injector.format // 'pe' | 'elf' | 'macho'
|
|
2158
|
+
* injector.inject()
|
|
2205
2159
|
* ```
|
|
2206
2160
|
*/
|
|
2207
|
-
var
|
|
2208
|
-
#status = "idle";
|
|
2161
|
+
var Injector = class {
|
|
2209
2162
|
#options;
|
|
2210
|
-
#
|
|
2163
|
+
#format;
|
|
2211
2164
|
constructor(options) {
|
|
2212
2165
|
this.#options = options;
|
|
2213
|
-
this.#
|
|
2214
|
-
...options.on === void 0 ? {} : { on: options.on },
|
|
2215
|
-
...options.error === void 0 ? {} : { error: options.error }
|
|
2216
|
-
});
|
|
2166
|
+
this.#format = this.#detect();
|
|
2217
2167
|
}
|
|
2218
|
-
get
|
|
2219
|
-
return this.#
|
|
2168
|
+
get format() {
|
|
2169
|
+
return this.#format;
|
|
2220
2170
|
}
|
|
2221
|
-
|
|
2222
|
-
|
|
2171
|
+
inject() {
|
|
2172
|
+
new Injection(this.#options, this.#format).execute();
|
|
2223
2173
|
}
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
if (this.#status === "active") throw new SEAError("STATE", "SEA build already in progress");
|
|
2227
|
-
if (resolvePlatform() === void 0) {
|
|
2228
|
-
const error = new SEAError("PLATFORM", `Unsupported platform: ${process.platform}`, { platform: process.platform });
|
|
2229
|
-
this.#status = "error";
|
|
2230
|
-
this.#emitter.emit("error", error);
|
|
2231
|
-
throw error;
|
|
2232
|
-
}
|
|
2233
|
-
this.#status = "active";
|
|
2234
|
-
const start = Date.now();
|
|
2235
|
-
const root = this.#options.root ?? process.cwd();
|
|
2174
|
+
#detect() {
|
|
2175
|
+
const fd = openSync(this.#options.executable, "r");
|
|
2236
2176
|
try {
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
const
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
|
|
2255
|
-
|
|
2256
|
-
};
|
|
2257
|
-
|
|
2258
|
-
|
|
2259
|
-
return result;
|
|
2260
|
-
} catch (thrown) {
|
|
2261
|
-
this.#status = "error";
|
|
2262
|
-
this.#emitter.emit("error", thrown);
|
|
2263
|
-
throw thrown;
|
|
2177
|
+
const header = Buffer.alloc(4);
|
|
2178
|
+
readSync(fd, header, 0, 4, 0);
|
|
2179
|
+
const magic16 = header.readUInt16LE(0);
|
|
2180
|
+
const magic32 = header.readUInt32BE(0);
|
|
2181
|
+
const magic32le = header.readUInt32LE(0);
|
|
2182
|
+
if (magic16 === 23117) return "pe";
|
|
2183
|
+
if (magic32 === 2135247942) return "elf";
|
|
2184
|
+
if (magic32le === 4277009103) return "macho";
|
|
2185
|
+
if (magic32 === 3405691582 || magic32 === 3199925962) throw new SEAError("FORMAT", "Mach-O universal/fat binaries are not supported; provide a thin arm64 or x86_64 binary", {
|
|
2186
|
+
executable: this.#options.executable,
|
|
2187
|
+
magic: magic32
|
|
2188
|
+
});
|
|
2189
|
+
if (magic32le === 4277009102 || magic32le === 3472551422 || magic32 === 4277009102 || magic32 === 3472551422) throw new SEAError("FORMAT", "Only 64-bit little-endian Mach-O is supported", {
|
|
2190
|
+
executable: this.#options.executable,
|
|
2191
|
+
magic: magic32
|
|
2192
|
+
});
|
|
2193
|
+
throw new SEAError("FORMAT", `Unknown executable format (magic: 0x${magic32.toString(16).padStart(8, "0")})`, {
|
|
2194
|
+
executable: this.#options.executable,
|
|
2195
|
+
magic: magic32
|
|
2196
|
+
});
|
|
2197
|
+
} finally {
|
|
2198
|
+
closeSync(fd);
|
|
2264
2199
|
}
|
|
2265
2200
|
}
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
#
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
}
|
|
2201
|
+
};
|
|
2202
|
+
// src/server/seas/SEAConstruction.ts
|
|
2203
|
+
/** Owns the progress and assembly state of one SEA build. */
|
|
2204
|
+
var SEAConstruction = class {
|
|
2205
|
+
#options;
|
|
2206
|
+
#emitter;
|
|
2207
|
+
#check;
|
|
2208
|
+
#total = 0;
|
|
2209
|
+
#original = 0;
|
|
2210
|
+
#compressed = 0;
|
|
2211
|
+
#current = 0;
|
|
2212
|
+
#signed = false;
|
|
2213
|
+
#stripped = false;
|
|
2214
|
+
#terminal;
|
|
2215
|
+
constructor(options, emitter, check) {
|
|
2216
|
+
this.#options = options;
|
|
2217
|
+
this.#emitter = emitter;
|
|
2218
|
+
this.#check = check;
|
|
2285
2219
|
}
|
|
2286
|
-
|
|
2220
|
+
compress(root) {
|
|
2287
2221
|
const compression = this.#options.compression;
|
|
2288
2222
|
if (compression === void 0 || compression.paths.length === 0) {
|
|
2289
2223
|
this.#emitter.emit("compress", void 0);
|
|
2290
2224
|
return;
|
|
2291
2225
|
}
|
|
2292
2226
|
const directories = [];
|
|
2293
|
-
let total = 0;
|
|
2294
2227
|
for (const path of compression.paths) {
|
|
2295
2228
|
const candidate = resolve(root, path);
|
|
2296
2229
|
if (!existsSync(candidate)) continue;
|
|
2297
2230
|
this.#check();
|
|
2298
2231
|
const directory = ensureContained(root, path);
|
|
2299
2232
|
directories.push(directory);
|
|
2300
|
-
total += walkDirectory(directory).filter(isCompressible).length;
|
|
2233
|
+
this.#total += walkDirectory(directory).filter(isCompressible).length;
|
|
2301
2234
|
}
|
|
2302
2235
|
const assets = [];
|
|
2303
|
-
let original = 0;
|
|
2304
|
-
let compressed = 0;
|
|
2305
|
-
let current = 0;
|
|
2306
2236
|
for (const directory of directories) {
|
|
2307
2237
|
this.#check();
|
|
2308
2238
|
const manifest = compressDirectory(directory, compression, (result) => {
|
|
2309
|
-
current += 1;
|
|
2239
|
+
this.#current += 1;
|
|
2310
2240
|
this.#emitter.emit("progress", {
|
|
2311
2241
|
path: result.input,
|
|
2312
|
-
current,
|
|
2313
|
-
total
|
|
2242
|
+
current: this.#current,
|
|
2243
|
+
total: this.#total
|
|
2314
2244
|
});
|
|
2315
2245
|
});
|
|
2316
2246
|
assets.push(...manifest.assets);
|
|
2317
|
-
original += manifest.total.original;
|
|
2318
|
-
compressed += manifest.total.compressed;
|
|
2247
|
+
this.#original += manifest.total.original;
|
|
2248
|
+
this.#compressed += manifest.total.compressed;
|
|
2319
2249
|
}
|
|
2320
2250
|
const manifest = {
|
|
2321
2251
|
assets,
|
|
2322
2252
|
total: {
|
|
2323
|
-
original,
|
|
2324
|
-
compressed,
|
|
2325
|
-
ratio: original > 0 ? compressed / original : 0
|
|
2253
|
+
original: this.#original,
|
|
2254
|
+
compressed: this.#compressed,
|
|
2255
|
+
ratio: this.#original > 0 ? this.#compressed / this.#original : 0
|
|
2326
2256
|
}
|
|
2327
2257
|
};
|
|
2328
2258
|
this.#emitter.emit("compress", manifest);
|
|
2329
2259
|
return manifest;
|
|
2330
2260
|
}
|
|
2331
|
-
|
|
2332
|
-
const entry = resolve(root, this.#options.entry.path);
|
|
2333
|
-
ensureExists(entry, `Entry not found: ${this.#options.entry.path}`, "ENTRY");
|
|
2334
|
-
const output = resolve(root, this.#options.output);
|
|
2335
|
-
mkdirSync(output, { recursive: true });
|
|
2336
|
-
const configPath = join(output, "sea-config.json");
|
|
2337
|
-
const blob = join(output, "sea-prep.blob");
|
|
2338
|
-
const config = buildBlobConfig({
|
|
2339
|
-
path: entry,
|
|
2340
|
-
...this.#options.entry.format === void 0 ? {} : { format: this.#options.entry.format }
|
|
2341
|
-
}, blob, this.#resolveAssets(root, compression), this.#options.blob);
|
|
2342
|
-
writeFileSync(configPath, JSON.stringify(config, null, 2));
|
|
2343
|
-
executeShell([
|
|
2344
|
-
process.execPath,
|
|
2345
|
-
"--experimental-sea-config",
|
|
2346
|
-
configPath
|
|
2347
|
-
], {
|
|
2348
|
-
cwd: output,
|
|
2349
|
-
...this.#options.signal === void 0 ? {} : { signal: this.#options.signal },
|
|
2350
|
-
...this.#options.timeout === void 0 ? {} : { timeout: this.#options.timeout }
|
|
2351
|
-
});
|
|
2352
|
-
if (!existsSync(blob)) throw new SEAError("BLOB", "SEA blob generation failed — blob not found", { blob });
|
|
2353
|
-
this.#emitter.emit("blob", blob);
|
|
2354
|
-
return blob;
|
|
2355
|
-
}
|
|
2356
|
-
#assemble(root, blob) {
|
|
2261
|
+
assemble(root, blob) {
|
|
2357
2262
|
const platform = resolvePlatform();
|
|
2358
2263
|
if (platform === void 0) throw new SEAError("PLATFORM", `Unsupported platform: ${process.platform}`, { platform: process.platform });
|
|
2359
2264
|
const output = resolve(root, this.#options.output);
|
|
@@ -2365,9 +2270,6 @@ var SEA = class {
|
|
|
2365
2270
|
...this.#options.signal === void 0 ? {} : { signal: this.#options.signal },
|
|
2366
2271
|
...this.#options.timeout === void 0 ? {} : { timeout: this.#options.timeout }
|
|
2367
2272
|
};
|
|
2368
|
-
let signed = false;
|
|
2369
|
-
let stripped = false;
|
|
2370
|
-
let terminal;
|
|
2371
2273
|
try {
|
|
2372
2274
|
if (process.platform === "darwin") {
|
|
2373
2275
|
this.#check();
|
|
@@ -2380,7 +2282,7 @@ var SEA = class {
|
|
|
2380
2282
|
} catch (thrown) {
|
|
2381
2283
|
throw new SEAError("SIGN", "Failed to strip existing signature", { cause: isError(thrown) ? thrown.message : String(thrown) });
|
|
2382
2284
|
}
|
|
2383
|
-
stripped = true;
|
|
2285
|
+
this.#stripped = true;
|
|
2384
2286
|
}
|
|
2385
2287
|
this.#check();
|
|
2386
2288
|
new Injector({
|
|
@@ -2405,16 +2307,16 @@ var SEA = class {
|
|
|
2405
2307
|
throw new SEAError("SIGN", "Signature verification failed", { cause: isError(thrown) ? thrown.message : String(thrown) });
|
|
2406
2308
|
}
|
|
2407
2309
|
}
|
|
2408
|
-
signed = true;
|
|
2310
|
+
this.#signed = true;
|
|
2409
2311
|
}
|
|
2410
2312
|
} else if (process.platform === "win32") {
|
|
2411
2313
|
this.#check();
|
|
2412
2314
|
copyFileSync(process.execPath, temp);
|
|
2413
2315
|
stripPESignature(temp);
|
|
2414
|
-
stripped = true;
|
|
2316
|
+
this.#stripped = true;
|
|
2415
2317
|
if (isPEExecutable(temp)) {
|
|
2416
|
-
terminal = this.#options.windows?.terminal !== false;
|
|
2417
|
-
patchPESubsystem(temp, terminal ? 3 : 2);
|
|
2318
|
+
this.#terminal = this.#options.windows?.terminal !== false;
|
|
2319
|
+
patchPESubsystem(temp, this.#terminal ? 3 : 2);
|
|
2418
2320
|
}
|
|
2419
2321
|
this.#check();
|
|
2420
2322
|
new Injector({
|
|
@@ -2435,7 +2337,7 @@ var SEA = class {
|
|
|
2435
2337
|
} catch {
|
|
2436
2338
|
throw new SEAError("SIGN", "Windows signing failed", { executable: temp });
|
|
2437
2339
|
}
|
|
2438
|
-
signed = true;
|
|
2340
|
+
this.#signed = true;
|
|
2439
2341
|
if (platform.verify !== void 0) {
|
|
2440
2342
|
this.#check();
|
|
2441
2343
|
try {
|
|
@@ -2465,11 +2367,142 @@ var SEA = class {
|
|
|
2465
2367
|
this.#emitter.emit("assemble", finalOutput);
|
|
2466
2368
|
return {
|
|
2467
2369
|
executable: finalOutput,
|
|
2468
|
-
signed,
|
|
2469
|
-
stripped,
|
|
2470
|
-
...terminal === void 0 ? {} : { terminal }
|
|
2370
|
+
signed: this.#signed,
|
|
2371
|
+
stripped: this.#stripped,
|
|
2372
|
+
...this.#terminal === void 0 ? {} : { terminal: this.#terminal }
|
|
2471
2373
|
};
|
|
2472
2374
|
}
|
|
2375
|
+
};
|
|
2376
|
+
// src/server/seas/SEA.ts
|
|
2377
|
+
/**
|
|
2378
|
+
* Runs a Node.js single executable application build to completion.
|
|
2379
|
+
*
|
|
2380
|
+
* @remarks
|
|
2381
|
+
* `execute` compresses the configured directories, generates the SEA blob, copies
|
|
2382
|
+
* the host Node binary, injects the blob with an {@link Injector}, signs the
|
|
2383
|
+
* result where the platform supports it, and moves the finished executable into
|
|
2384
|
+
* place atomically. Each stage reports on `emitter`, and every failure throws a
|
|
2385
|
+
* coded `SEAError` after an `error` event carrying it.
|
|
2386
|
+
*
|
|
2387
|
+
* @param options - SEA build options: the output name, the entry point, the output
|
|
2388
|
+
* directory, and the optional assets, compression, Windows, and blob settings
|
|
2389
|
+
*
|
|
2390
|
+
* @example
|
|
2391
|
+
* ```ts
|
|
2392
|
+
* const sea = new SEA({
|
|
2393
|
+
* name: 'myapp',
|
|
2394
|
+
* entry: { path: 'dist/server/serve.cjs' },
|
|
2395
|
+
* output: 'dist/sea',
|
|
2396
|
+
* })
|
|
2397
|
+
* const result = await sea.execute()
|
|
2398
|
+
* sea.destroy()
|
|
2399
|
+
* ```
|
|
2400
|
+
*/
|
|
2401
|
+
var SEA = class {
|
|
2402
|
+
#status = "idle";
|
|
2403
|
+
#options;
|
|
2404
|
+
#emitter;
|
|
2405
|
+
constructor(options) {
|
|
2406
|
+
this.#options = options;
|
|
2407
|
+
this.#emitter = new Emitter({
|
|
2408
|
+
...options.on === void 0 ? {} : { on: options.on },
|
|
2409
|
+
...options.error === void 0 ? {} : { error: options.error }
|
|
2410
|
+
});
|
|
2411
|
+
}
|
|
2412
|
+
get emitter() {
|
|
2413
|
+
return this.#emitter;
|
|
2414
|
+
}
|
|
2415
|
+
get status() {
|
|
2416
|
+
return this.#status;
|
|
2417
|
+
}
|
|
2418
|
+
async execute() {
|
|
2419
|
+
if (this.#emitter.destroyed) throw new SEAError("STATE", "SEA is destroyed");
|
|
2420
|
+
if (this.#status === "active") throw new SEAError("STATE", "SEA build already in progress");
|
|
2421
|
+
if (resolvePlatform() === void 0) {
|
|
2422
|
+
const error = new SEAError("PLATFORM", `Unsupported platform: ${process.platform}`, { platform: process.platform });
|
|
2423
|
+
this.#status = "error";
|
|
2424
|
+
this.#emitter.emit("error", error);
|
|
2425
|
+
throw error;
|
|
2426
|
+
}
|
|
2427
|
+
this.#status = "active";
|
|
2428
|
+
const start = Date.now();
|
|
2429
|
+
const root = this.#options.root ?? process.cwd();
|
|
2430
|
+
try {
|
|
2431
|
+
this.#check();
|
|
2432
|
+
this.#validate(root);
|
|
2433
|
+
this.#check();
|
|
2434
|
+
const construction = new SEAConstruction(this.#options, this.#emitter, () => this.#check());
|
|
2435
|
+
const compression = construction.compress(root);
|
|
2436
|
+
this.#check();
|
|
2437
|
+
const blob = this.#buildBlob(root, compression);
|
|
2438
|
+
this.#check();
|
|
2439
|
+
const assembled = construction.assemble(root, blob);
|
|
2440
|
+
const size = statSync(assembled.executable).size;
|
|
2441
|
+
const duration = Date.now() - start;
|
|
2442
|
+
const result = {
|
|
2443
|
+
executable: assembled.executable,
|
|
2444
|
+
platform: process.platform,
|
|
2445
|
+
size,
|
|
2446
|
+
duration,
|
|
2447
|
+
signed: assembled.signed,
|
|
2448
|
+
stripped: assembled.stripped,
|
|
2449
|
+
...compression === void 0 ? {} : { compression },
|
|
2450
|
+
...assembled.terminal === void 0 ? {} : { terminal: assembled.terminal }
|
|
2451
|
+
};
|
|
2452
|
+
this.#status = "done";
|
|
2453
|
+
this.#emitter.emit("complete", result);
|
|
2454
|
+
return result;
|
|
2455
|
+
} catch (thrown) {
|
|
2456
|
+
this.#status = "error";
|
|
2457
|
+
this.#emitter.emit("error", thrown);
|
|
2458
|
+
throw thrown;
|
|
2459
|
+
}
|
|
2460
|
+
}
|
|
2461
|
+
destroy() {
|
|
2462
|
+
this.#emitter.destroy();
|
|
2463
|
+
}
|
|
2464
|
+
#check() {
|
|
2465
|
+
if (this.#options.signal?.aborted === true) throw new SEAError("ABORT", "SEA build aborted");
|
|
2466
|
+
}
|
|
2467
|
+
#validate(root) {
|
|
2468
|
+
const base = resolve(root);
|
|
2469
|
+
ensureSafeName(this.#options.name);
|
|
2470
|
+
for (const [key, path] of Object.entries(this.#options.assets ?? {})) {
|
|
2471
|
+
ensureSafeKey(key);
|
|
2472
|
+
ensureContained(base, path);
|
|
2473
|
+
}
|
|
2474
|
+
for (const path of this.#options.compression?.paths ?? []) if (existsSync(resolve(base, path))) ensureContained(base, path);
|
|
2475
|
+
const sign = this.#options.windows?.sign;
|
|
2476
|
+
if (sign !== void 0) {
|
|
2477
|
+
buildSignCommand(sign, "placeholder");
|
|
2478
|
+
if (sign.file !== void 0) ensureExists(resolve(base, sign.file), "Certificate file not found", "SIGN");
|
|
2479
|
+
}
|
|
2480
|
+
}
|
|
2481
|
+
#buildBlob(root, compression) {
|
|
2482
|
+
const entry = resolve(root, this.#options.entry.path);
|
|
2483
|
+
ensureExists(entry, `Entry not found: ${this.#options.entry.path}`, "ENTRY");
|
|
2484
|
+
const output = resolve(root, this.#options.output);
|
|
2485
|
+
mkdirSync(output, { recursive: true });
|
|
2486
|
+
const configPath = join(output, "sea-config.json");
|
|
2487
|
+
const blob = join(output, "sea-prep.blob");
|
|
2488
|
+
const config = buildBlobConfig({
|
|
2489
|
+
path: entry,
|
|
2490
|
+
...this.#options.entry.format === void 0 ? {} : { format: this.#options.entry.format }
|
|
2491
|
+
}, blob, this.#resolveAssets(root, compression), this.#options.blob);
|
|
2492
|
+
writeFileSync(configPath, JSON.stringify(config, null, 2));
|
|
2493
|
+
executeShell([
|
|
2494
|
+
process.execPath,
|
|
2495
|
+
"--experimental-sea-config",
|
|
2496
|
+
configPath
|
|
2497
|
+
], {
|
|
2498
|
+
cwd: output,
|
|
2499
|
+
...this.#options.signal === void 0 ? {} : { signal: this.#options.signal },
|
|
2500
|
+
...this.#options.timeout === void 0 ? {} : { timeout: this.#options.timeout }
|
|
2501
|
+
});
|
|
2502
|
+
if (!existsSync(blob)) throw new SEAError("BLOB", "SEA blob generation failed — blob not found", { blob });
|
|
2503
|
+
this.#emitter.emit("blob", blob);
|
|
2504
|
+
return blob;
|
|
2505
|
+
}
|
|
2473
2506
|
#resolveAssets(root, compression) {
|
|
2474
2507
|
const outputs = /* @__PURE__ */ new Map();
|
|
2475
2508
|
for (const result of compression?.assets ?? []) outputs.set(result.input, result.output);
|
|
@@ -2481,8 +2514,7 @@ var SEA = class {
|
|
|
2481
2514
|
return assets;
|
|
2482
2515
|
}
|
|
2483
2516
|
};
|
|
2484
|
-
|
|
2485
|
-
//#region src/server/assets/Asset.ts
|
|
2517
|
+
// src/server/assets/Asset.ts
|
|
2486
2518
|
/**
|
|
2487
2519
|
* Holds one named asset's key, bytes, and compression state.
|
|
2488
2520
|
*
|
|
@@ -2510,8 +2542,7 @@ var Asset = class {
|
|
|
2510
2542
|
this.compressed = input.compressed ?? input.key.endsWith(".br");
|
|
2511
2543
|
}
|
|
2512
2544
|
};
|
|
2513
|
-
|
|
2514
|
-
//#region src/server/assets/AssetManager.ts
|
|
2545
|
+
// src/server/assets/AssetManager.ts
|
|
2515
2546
|
/**
|
|
2516
2547
|
* Collects named assets from a SEA blob or from disk and serves them by key.
|
|
2517
2548
|
*
|
|
@@ -2624,8 +2655,7 @@ var AssetManager = class {
|
|
|
2624
2655
|
}
|
|
2625
2656
|
}
|
|
2626
2657
|
};
|
|
2627
|
-
|
|
2628
|
-
//#region src/server/factories.ts
|
|
2658
|
+
// src/server/factories.ts
|
|
2629
2659
|
/**
|
|
2630
2660
|
* Creates a SEA build orchestrator over the given options and returns it as a
|
|
2631
2661
|
* `SEAInterface`, the published contract a caller holds instead of the `SEA` class.
|
|
@@ -2714,7 +2744,6 @@ function createAsset(input) {
|
|
|
2714
2744
|
function createAssetManager(options) {
|
|
2715
2745
|
return new AssetManager(options);
|
|
2716
2746
|
}
|
|
2717
|
-
//#endregion
|
|
2718
2747
|
export { Asset, AssetManager, BROTLI_EXTENSION, DEFAULT_ENTRY_FORMAT, DEFAULT_SEA_COMPRESSION_QUALITY, ELF_CLASS_64, ELF_DATA_LSB, ELF_MAGIC, ELF_PAGE_SIZE, ELF_PF_R, ELF_PT_LOAD, ELF_PT_NOTE, ELF_PT_PHDR, Injector, MACHO_LC_SEGMENT_64, MACHO_MAGIC_64, PE32_MAGIC, PE32_PLUS_MAGIC, PE_MAGIC, PE_RESOURCE_DATA_ENTRY_SIZE, PE_RESOURCE_DIR_SIZE, PE_RESOURCE_ENTRY_SIZE, PE_RESOURCE_NAME_FLAG, PE_RESOURCE_SUBDIR_FLAG, PE_RT_RCDATA, PE_SCN_INITIALIZED_DATA, PE_SCN_MEM_READ, PE_SECTION_HEADER_SIZE, PE_SIGNATURE, SEA, SEAError, SEA_BLOB_RESOURCE, SEA_COMPRESSION_MODE_VALUES, SEA_PLATFORMS, SEA_SENTINEL_FUSE, SKIP_EXTENSIONS, ShellError, WINDOWS_SUBSYSTEM_CONSOLE, WINDOWS_SUBSYSTEM_GUI, alignELFNoteSize, alignTo, appendFile, buildBlobConfig, buildELFNoteHeader, buildSignCommand, compressDirectory, compressFile, computeSize, copyRange, createAsset, createAssetManager, createInjector, createSEA, ensureContained, ensureExists, ensureSafeKey, ensureSafeName, executeShell, finalizeExecutable, formatSize, isCompressible, isExecutableFormat, isPEExecutable, isPlatformSupported, isPowerOfTwo, isSEAError, isShellError, openBrowser, patchPESubsystem, patchSentinelFuse, readPEOffset, readU16, readU32, readU64, redactCommand, resolvePlatform, stripPESignature, stripTrailingNulls, syncDirectory, walkDirectory, writeU16, writeU32, writeU64 };
|
|
2719
2748
|
|
|
2720
2749
|
//# sourceMappingURL=index.js.map
|