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