unplugin-https-reverse-proxy 4.1.0-beta.8 → 4.1.0
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/README.md +2 -2
- package/dist/{chunk-ICH7VY4H.js → chunk-U6J2RMIR.js} +211 -109
- package/dist/esbuild.cjs +223 -121
- package/dist/esbuild.js +1 -1
- package/dist/index.cjs +223 -121
- package/dist/index.js +1 -1
- package/dist/nuxt.cjs +221 -121
- package/dist/nuxt.js +1 -1
- package/dist/privileged-helper.cjs +154 -56
- package/dist/privileged-helper.js +144 -46
- package/dist/rollup.cjs +223 -121
- package/dist/rollup.js +1 -1
- package/dist/rspack.cjs +223 -121
- package/dist/rspack.js +1 -1
- package/dist/vite.cjs +223 -121
- package/dist/vite.js +1 -1
- package/dist/webpack.cjs +223 -121
- package/dist/webpack.js +1 -1
- package/package.json +1 -1
- package/src/caddy/caddyfile.ts +2 -0
- package/src/caddy/config.ts +57 -34
- package/src/caddy/constants.ts +30 -1
- package/src/caddy/index.ts +44 -49
- package/src/caddy/privileged.ts +3 -0
- package/src/index.ts +5 -0
- package/src/privileged/bootstrap.ts +27 -7
- package/src/privileged/client.ts +1 -1
- package/src/privileged/launch.ts +95 -11
- package/src/privileged/path-guard.ts +106 -0
- package/src/privileged/server.ts +36 -4
- package/src/privileged/session.ts +3 -0
- package/src/privileged/windows-elevation.ts +91 -33
- package/src/privileged-helper.ts +49 -9
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import type { Stats } from 'node:fs'
|
|
2
|
+
import { lstatSync } from 'node:fs'
|
|
3
|
+
import process from 'node:process'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Why a path is refused before the helper runs it.
|
|
7
|
+
*
|
|
8
|
+
* The helper runs as root while the paths come from a process running as the
|
|
9
|
+
* developer. Anything that process, or any other local user, can rewrite after
|
|
10
|
+
* the check would become root-executed code or root-loaded config, so the
|
|
11
|
+
* checks below run once at startup and the result is trusted for the session.
|
|
12
|
+
*/
|
|
13
|
+
export interface PathRejection {
|
|
14
|
+
path: string
|
|
15
|
+
reason: string
|
|
16
|
+
guidance: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface PathCheckOptions {
|
|
20
|
+
/**
|
|
21
|
+
* uid allowed to own the path besides root.
|
|
22
|
+
*
|
|
23
|
+
* The helper entry itself lives in the developer's `node_modules`, so the
|
|
24
|
+
* invoking user has to be accepted for the paths that came from there.
|
|
25
|
+
*/
|
|
26
|
+
ownerUid?: number
|
|
27
|
+
/** Human-readable role, used in the rejection message. */
|
|
28
|
+
label: string
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function describe(stats: Stats): string {
|
|
32
|
+
return `uid=${stats.uid} mode=${(stats.mode & 0o777).toString(8)}`
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Rejects a path the helper must not execute or load.
|
|
37
|
+
*
|
|
38
|
+
* Symbolic links are refused outright: the check has to describe the file that
|
|
39
|
+
* will actually be used, and a link can be repointed after the check. Group- or
|
|
40
|
+
* world-writable files are refused because any local user could replace them
|
|
41
|
+
* between the check and the exec.
|
|
42
|
+
*/
|
|
43
|
+
export function checkPrivilegedPath(
|
|
44
|
+
path: string,
|
|
45
|
+
options: PathCheckOptions,
|
|
46
|
+
): PathRejection | undefined {
|
|
47
|
+
let stats: Stats
|
|
48
|
+
try {
|
|
49
|
+
stats = lstatSync(path)
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
return {
|
|
53
|
+
path,
|
|
54
|
+
reason: `${options.label} is not readable: ${(error as Error).message}`,
|
|
55
|
+
guidance: 'Check that this path exists and that you can access its parent directories. Fix access manually; do not make the path group- or world-writable.',
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (stats.isSymbolicLink()) {
|
|
60
|
+
return {
|
|
61
|
+
path,
|
|
62
|
+
reason: `${options.label} is a symbolic link (${describe(stats)})`,
|
|
63
|
+
guidance: 'Replace this symlink with a regular file manually or reinstall the affected file; do not bypass the safety check.',
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (!stats.isFile() && !stats.isDirectory()) {
|
|
68
|
+
return {
|
|
69
|
+
path,
|
|
70
|
+
reason: `${options.label} is neither a file nor a directory`,
|
|
71
|
+
guidance: 'Provide a regular file or directory at this path manually.',
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Windows has no uid or mode bits: Node reports uid 0 and mode 666 for every
|
|
76
|
+
// writable path, so these checks would refuse everything. Access there is
|
|
77
|
+
// decided by ACLs, and the runtime directory is the per-user `%TEMP%`.
|
|
78
|
+
if (process.platform === 'win32')
|
|
79
|
+
return undefined
|
|
80
|
+
|
|
81
|
+
const allowedOwner = stats.uid === 0 || (options.ownerUid !== undefined && stats.uid === options.ownerUid)
|
|
82
|
+
if (!allowedOwner) {
|
|
83
|
+
return {
|
|
84
|
+
path,
|
|
85
|
+
reason: `${options.label} is owned by another user (${describe(stats)})`,
|
|
86
|
+
guidance: 'Check ownership manually. This path must be owned by root or the user running the dev server.',
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Root's own files are fine at 644/755; the point is that no one else can
|
|
91
|
+
// write them.
|
|
92
|
+
if (stats.mode & 0o022) {
|
|
93
|
+
return {
|
|
94
|
+
path,
|
|
95
|
+
reason: `${options.label} is group- or world-writable (${describe(stats)})`,
|
|
96
|
+
guidance: 'Remove group and world write access manually; do not use chmod 777.',
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return undefined
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Formats a rejection for the helper log; one place keeps the wording stable. */
|
|
104
|
+
export function describeRejection(rejection: PathRejection): string {
|
|
105
|
+
return `refusing to start: ${rejection.reason} (${rejection.path}). ${rejection.guidance}`
|
|
106
|
+
}
|
package/src/privileged/server.ts
CHANGED
|
@@ -104,6 +104,8 @@ export interface HelperRuntime {
|
|
|
104
104
|
dispose: () => Promise<void>
|
|
105
105
|
/** Removes hosts entries a previous crashed session left behind. */
|
|
106
106
|
sweepStaleEntries: () => Promise<boolean>
|
|
107
|
+
/** Number of attached dev servers; a signal must not tear down a live one. */
|
|
108
|
+
clientCount: () => number
|
|
107
109
|
}
|
|
108
110
|
|
|
109
111
|
/**
|
|
@@ -332,6 +334,7 @@ export function createHelperRuntime(deps: HelperDeps): HelperRuntime {
|
|
|
332
334
|
},
|
|
333
335
|
dispose,
|
|
334
336
|
sweepStaleEntries,
|
|
337
|
+
clientCount: () => clients.size,
|
|
335
338
|
}
|
|
336
339
|
}
|
|
337
340
|
|
|
@@ -339,6 +342,8 @@ export interface NodeHelperDepsOptions {
|
|
|
339
342
|
hostsPath?: string
|
|
340
343
|
caddyBin: string
|
|
341
344
|
caddyConfigPath: string
|
|
345
|
+
/** Admin API socket; `ca.trust` talks to it instead of the TCP default. */
|
|
346
|
+
caddyAdminSocket?: string
|
|
342
347
|
caddyCwd?: string
|
|
343
348
|
caddyEnv?: Record<string, string>
|
|
344
349
|
/** Caddy storage directory; handed back to the user when Caddy stops. */
|
|
@@ -388,13 +393,19 @@ async function reclaimTree(target: string, uid: number, gid: number): Promise<st
|
|
|
388
393
|
}
|
|
389
394
|
|
|
390
395
|
/** Installs the Caddy root CA using helper-owned commands. */
|
|
391
|
-
async function trustLocalCaWithSystemTools(
|
|
396
|
+
async function trustLocalCaWithSystemTools(
|
|
397
|
+
caddyBin: string,
|
|
398
|
+
adminSocket: string | undefined,
|
|
399
|
+
): Promise<{ trusted: boolean, output: string }> {
|
|
392
400
|
// `caddy trust` reads the CA through the admin API, which only exists while
|
|
393
|
-
// Caddy is serving; the plugin calls this after Caddy is up.
|
|
401
|
+
// Caddy is serving; the plugin calls this after Caddy is up. The admin API is
|
|
402
|
+
// a unix socket when the template enabled one, so the address follows it
|
|
403
|
+
// instead of the old TCP default.
|
|
404
|
+
const address = adminSocket ? `unix/${adminSocket}` : '127.0.0.1:2019'
|
|
394
405
|
try {
|
|
395
406
|
const { stdout, stderr } = await execFileAsync(
|
|
396
407
|
caddyBin,
|
|
397
|
-
['trust', '--ca', 'local', '--address',
|
|
408
|
+
['trust', '--ca', 'local', '--address', address],
|
|
398
409
|
{ timeout: 120_000 },
|
|
399
410
|
)
|
|
400
411
|
return { trusted: true, output: `${stdout}${stderr}`.trim() }
|
|
@@ -413,6 +424,19 @@ export function createNodeHelperDeps(options: NodeHelperDepsOptions): HelperDeps
|
|
|
413
424
|
const log = options.log ?? ((message: string) => process.stdout.write(`${message}\n`))
|
|
414
425
|
let caddy: CaddyChild | undefined
|
|
415
426
|
|
|
427
|
+
// Caddy runs in its own session, so it no longer dies with this process. The
|
|
428
|
+
// `exit` hook is the last chance to take it down; it must stay synchronous,
|
|
429
|
+
// which `kill` is. Nothing runs after SIGKILL, and that case is covered by
|
|
430
|
+
// the next start freeing the port.
|
|
431
|
+
process.on('exit', () => {
|
|
432
|
+
if (caddy?.pid === undefined)
|
|
433
|
+
return
|
|
434
|
+
try {
|
|
435
|
+
process.kill(caddy.pid, 'SIGTERM')
|
|
436
|
+
}
|
|
437
|
+
catch {}
|
|
438
|
+
})
|
|
439
|
+
|
|
416
440
|
return {
|
|
417
441
|
readHosts: () => readFileSync(hostsPath, 'utf8'),
|
|
418
442
|
writeHosts: (content) => {
|
|
@@ -459,16 +483,24 @@ export function createNodeHelperDeps(options: NodeHelperDepsOptions): HelperDeps
|
|
|
459
483
|
caddyConfigPath: options.caddyConfigPath,
|
|
460
484
|
caddyCwd: options.caddyCwd ?? process.cwd(),
|
|
461
485
|
caddyEnv: options.caddyEnv ?? {},
|
|
462
|
-
trustLocalCa: () => trustLocalCaWithSystemTools(options.caddyBin),
|
|
486
|
+
trustLocalCa: () => trustLocalCaWithSystemTools(options.caddyBin, options.caddyAdminSocket),
|
|
463
487
|
caddyStoragePath: options.caddyStoragePath,
|
|
464
488
|
storageOwner: options.storageOwner,
|
|
465
489
|
startCaddy: (emit) => {
|
|
466
490
|
// `--watch` makes Caddy reload when another project rewrites the shared
|
|
467
491
|
// Caddyfile. Without it the second project's route never takes effect.
|
|
492
|
+
//
|
|
493
|
+
// `detached` puts Caddy in its own session. It shares one Caddy between
|
|
494
|
+
// projects, so the Ctrl+C that stops one dev server must not reach it:
|
|
495
|
+
// the whole foreground process group gets SIGINT, and Caddy exits on
|
|
496
|
+
// SIGINT. `kill()` still targets the pid, and the exit event still fires.
|
|
497
|
+
// Windows has no process groups to escape and `detached` would open a
|
|
498
|
+
// console window there, so it stays off.
|
|
468
499
|
const child = spawn(options.caddyBin, ['run', '--adapter', 'caddyfile', '--config', options.caddyConfigPath, '--watch'], {
|
|
469
500
|
cwd: options.caddyCwd ?? process.cwd(),
|
|
470
501
|
env: { ...process.env, ...options.caddyEnv },
|
|
471
502
|
stdio: ['ignore', 'pipe', 'pipe'],
|
|
503
|
+
detached: platform() !== 'win32',
|
|
472
504
|
})
|
|
473
505
|
const handle: CaddyChild & { running?: boolean } = {
|
|
474
506
|
pid: child.pid,
|
|
@@ -25,6 +25,8 @@ export interface SessionScope {
|
|
|
25
25
|
caddyBin: string
|
|
26
26
|
/** Caddyfile the helper may load. */
|
|
27
27
|
caddyConfigPath: string
|
|
28
|
+
/** Admin API socket; used by `ca.trust` instead of the TCP default. */
|
|
29
|
+
caddyAdminSocket?: string
|
|
28
30
|
/** Shown before the password prompt so the request is not a surprise. */
|
|
29
31
|
reason?: { target: string, https: boolean }
|
|
30
32
|
log?: (message: string) => void
|
|
@@ -108,6 +110,7 @@ async function startNew(socketPath: string, scope: SessionScope): Promise<Opened
|
|
|
108
110
|
caddyStoragePath: scope.caddyStoragePath,
|
|
109
111
|
caddyBin: scope.caddyBin,
|
|
110
112
|
caddyConfigPath: scope.caddyConfigPath,
|
|
113
|
+
caddyAdminSocket: scope.caddyAdminSocket,
|
|
111
114
|
reason: scope.reason,
|
|
112
115
|
log: scope.log,
|
|
113
116
|
})
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Buffer } from 'node:buffer'
|
|
2
2
|
import { spawn } from 'node:child_process'
|
|
3
3
|
import process from 'node:process'
|
|
4
4
|
|
|
@@ -7,8 +7,8 @@ import process from 'node:process'
|
|
|
7
7
|
* is a new process that cannot inherit the parent's stdio. The helper talks
|
|
8
8
|
* over a named pipe, so it only needs to be started detached.
|
|
9
9
|
*
|
|
10
|
-
*
|
|
11
|
-
* without a helper binary or a dependency on `sudo-prompt`.
|
|
10
|
+
* ShellExecute with the `runas` verb is the documented way to trigger the UAC
|
|
11
|
+
* prompt without a helper binary or a dependency on `sudo-prompt`.
|
|
12
12
|
*/
|
|
13
13
|
export interface WindowsElevationOptions {
|
|
14
14
|
/** Executable to run elevated; normally the node runtime. */
|
|
@@ -19,58 +19,116 @@ export interface WindowsElevationOptions {
|
|
|
19
19
|
cwd?: string
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
export type WindowsElevationResult
|
|
23
|
+
= | { ok: true, pid?: number }
|
|
24
|
+
| { ok: false, denied: boolean, message: string }
|
|
25
|
+
|
|
26
|
+
/** ShellExecute reports a dismissed UAC prompt as `ERROR_CANCELLED`. */
|
|
27
|
+
const ERROR_CANCELLED = 1223
|
|
28
|
+
|
|
22
29
|
/** Quotes an argument for PowerShell's single-quoted string syntax. */
|
|
23
30
|
export function quotePowerShell(value: string): string {
|
|
24
31
|
return `'${value.replace(/'/g, '\'\'')}'`
|
|
25
32
|
}
|
|
26
33
|
|
|
27
34
|
/**
|
|
28
|
-
*
|
|
35
|
+
* Quotes one argument so the C runtime's command-line parser, which node uses
|
|
36
|
+
* for `process.argv`, reads it back unchanged. ShellExecute takes a single
|
|
37
|
+
* command line, so a path with a space would otherwise split in two.
|
|
38
|
+
*/
|
|
39
|
+
export function quoteWindowsArg(arg: string): string {
|
|
40
|
+
if (arg === '')
|
|
41
|
+
return '""'
|
|
42
|
+
if (!/[\s"]/.test(arg))
|
|
43
|
+
return arg
|
|
44
|
+
|
|
45
|
+
let quoted = '"'
|
|
46
|
+
let backslashes = 0
|
|
47
|
+
for (const char of arg) {
|
|
48
|
+
if (char === '\\') {
|
|
49
|
+
backslashes++
|
|
50
|
+
continue
|
|
51
|
+
}
|
|
52
|
+
// Backslashes are literal unless they precede a quote.
|
|
53
|
+
quoted += char === '"'
|
|
54
|
+
? `${'\\'.repeat(backslashes * 2 + 1)}"`
|
|
55
|
+
: `${'\\'.repeat(backslashes)}${char}`
|
|
56
|
+
backslashes = 0
|
|
57
|
+
}
|
|
58
|
+
return `${quoted}${'\\'.repeat(backslashes * 2)}"`
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Builds the PowerShell script that starts `file` elevated. Exported so the
|
|
29
63
|
* escaping can be unit-tested without touching a real UAC prompt.
|
|
64
|
+
*
|
|
65
|
+
* `Start-Process` is not used: it joins `-ArgumentList` with spaces without
|
|
66
|
+
* quoting, and it hides the Win32 error code that tells a dismissed prompt
|
|
67
|
+
* apart from a real failure.
|
|
30
68
|
*/
|
|
31
69
|
export function buildElevationCommand(options: WindowsElevationOptions): string {
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
'
|
|
35
|
-
'-
|
|
36
|
-
quotePowerShell(options.file)
|
|
70
|
+
const lines = [
|
|
71
|
+
// The message read back on failure is localized; keep it readable as UTF-8.
|
|
72
|
+
'[Console]::OutputEncoding = [System.Text.Encoding]::UTF8',
|
|
73
|
+
'$psi = New-Object System.Diagnostics.ProcessStartInfo',
|
|
74
|
+
`$psi.FileName = ${quotePowerShell(options.file)}`,
|
|
75
|
+
`$psi.Arguments = ${quotePowerShell(options.args.map(quoteWindowsArg).join(' '))}`,
|
|
37
76
|
]
|
|
38
77
|
|
|
39
|
-
if (argumentList)
|
|
40
|
-
parts.push('-ArgumentList', argumentList)
|
|
41
|
-
|
|
42
78
|
if (options.cwd)
|
|
43
|
-
|
|
79
|
+
lines.push(`$psi.WorkingDirectory = ${quotePowerShell(options.cwd)}`)
|
|
44
80
|
|
|
45
|
-
|
|
46
|
-
|
|
81
|
+
lines.push(
|
|
82
|
+
'$psi.Verb = \'runas\'',
|
|
83
|
+
'$psi.UseShellExecute = $true',
|
|
84
|
+
'$psi.WindowStyle = \'Hidden\'',
|
|
85
|
+
'try { $p = [System.Diagnostics.Process]::Start($psi); if ($p) { [Console]::Out.Write($p.Id) } }',
|
|
86
|
+
`catch { $e = $_.Exception; while ($e.InnerException) { $e = $e.InnerException }; if ($e.NativeErrorCode -eq ${ERROR_CANCELLED}) { exit ${ERROR_CANCELLED} }; [Console]::Error.Write($e.Message); exit 1 }`,
|
|
87
|
+
)
|
|
47
88
|
|
|
48
|
-
return
|
|
89
|
+
return lines.join('\n')
|
|
49
90
|
}
|
|
50
91
|
|
|
51
92
|
/**
|
|
52
|
-
* Starts the helper elevated on Windows. Resolves once
|
|
53
|
-
* the
|
|
93
|
+
* Starts the helper elevated on Windows. Resolves once UAC has been answered;
|
|
94
|
+
* the helper's own readiness is observed through the named pipe.
|
|
54
95
|
*/
|
|
55
|
-
export function spawnElevatedOnWindows(options: WindowsElevationOptions): Promise<
|
|
56
|
-
return new Promise((resolve
|
|
57
|
-
const script = buildElevationCommand(options)
|
|
96
|
+
export function spawnElevatedOnWindows(options: WindowsElevationOptions): Promise<WindowsElevationResult> {
|
|
97
|
+
return new Promise((resolve) => {
|
|
58
98
|
const child = spawn(
|
|
59
99
|
'powershell.exe',
|
|
60
|
-
['-NoProfile', '-NonInteractive', '-Command',
|
|
61
|
-
{ stdio: 'ignore', windowsHide: true },
|
|
100
|
+
['-NoProfile', '-NonInteractive', '-Command', buildElevationCommand(options)],
|
|
101
|
+
{ stdio: ['ignore', 'pipe', 'pipe'], windowsHide: true },
|
|
62
102
|
)
|
|
63
103
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
child.
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
104
|
+
let stdout = ''
|
|
105
|
+
let stderr = ''
|
|
106
|
+
child.stdout.on('data', (data: Buffer) => {
|
|
107
|
+
stdout += data.toString()
|
|
108
|
+
})
|
|
109
|
+
child.stderr.on('data', (data: Buffer) => {
|
|
110
|
+
stderr = (stderr + data.toString()).slice(-4096)
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
child.once('error', error => resolve({
|
|
114
|
+
ok: false,
|
|
115
|
+
denied: false,
|
|
116
|
+
message: `could not start PowerShell to request administrator permission: ${error.message}`,
|
|
117
|
+
}))
|
|
118
|
+
child.once('close', (code) => {
|
|
119
|
+
if (code === 0) {
|
|
120
|
+
const pid = Number(stdout.trim())
|
|
121
|
+
return resolve({ ok: true, pid: Number.isSafeInteger(pid) && pid > 0 ? pid : undefined })
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (code === ERROR_CANCELLED)
|
|
125
|
+
return resolve({ ok: false, denied: true, message: 'administrator permission was cancelled' })
|
|
126
|
+
|
|
127
|
+
resolve({
|
|
128
|
+
ok: false,
|
|
129
|
+
denied: false,
|
|
130
|
+
message: `could not start the privileged helper as administrator (PowerShell exited with ${code}).${stderr.trim() ? ` ${stderr.trim()}` : ''}`,
|
|
131
|
+
})
|
|
74
132
|
})
|
|
75
133
|
})
|
|
76
134
|
}
|
package/src/privileged-helper.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { writeFileSync } from 'node:fs'
|
|
2
|
+
import { tmpdir } from 'node:os'
|
|
3
|
+
import { basename, dirname, resolve } from 'node:path'
|
|
1
4
|
import process from 'node:process'
|
|
2
5
|
import { startHelper } from './privileged/bootstrap'
|
|
3
6
|
|
|
@@ -10,6 +13,7 @@ interface ParsedArgs {
|
|
|
10
13
|
ownerUid?: number
|
|
11
14
|
ownerGid?: number
|
|
12
15
|
caddyStorage?: string
|
|
16
|
+
caddyAdmin?: string
|
|
13
17
|
}
|
|
14
18
|
|
|
15
19
|
function parseArgs(argv: string[]): ParsedArgs {
|
|
@@ -21,6 +25,7 @@ function parseArgs(argv: string[]): ParsedArgs {
|
|
|
21
25
|
let ownerUid: number | undefined
|
|
22
26
|
let ownerGid: number | undefined
|
|
23
27
|
let caddyStorage: string | undefined
|
|
28
|
+
let caddyAdmin: string | undefined
|
|
24
29
|
|
|
25
30
|
for (let index = 0; index < argv.length; index++) {
|
|
26
31
|
const current = argv[index]
|
|
@@ -40,6 +45,8 @@ function parseArgs(argv: string[]): ParsedArgs {
|
|
|
40
45
|
ownerGid = Number(argv[++index])
|
|
41
46
|
else if (current === '--caddy-storage')
|
|
42
47
|
caddyStorage = argv[++index] ?? undefined
|
|
48
|
+
else if (current === '--caddy-admin')
|
|
49
|
+
caddyAdmin = argv[++index] ?? undefined
|
|
43
50
|
}
|
|
44
51
|
|
|
45
52
|
if (!socket || !token)
|
|
@@ -56,6 +63,7 @@ function parseArgs(argv: string[]): ParsedArgs {
|
|
|
56
63
|
ownerUid: Number.isInteger(ownerUid) ? ownerUid : undefined,
|
|
57
64
|
ownerGid: Number.isInteger(ownerGid) ? ownerGid : undefined,
|
|
58
65
|
caddyStorage,
|
|
66
|
+
caddyAdmin,
|
|
59
67
|
}
|
|
60
68
|
}
|
|
61
69
|
|
|
@@ -78,6 +86,7 @@ async function main(): Promise<void> {
|
|
|
78
86
|
ownerUid: args.ownerUid,
|
|
79
87
|
ownerGid: args.ownerGid,
|
|
80
88
|
caddyStoragePath: args.caddyStorage,
|
|
89
|
+
caddyAdminSocket: args.caddyAdmin,
|
|
81
90
|
caddyEnv: {
|
|
82
91
|
HOME: process.env.HOME ?? '',
|
|
83
92
|
// Keep Caddy's storage next to the invoking user's home even though the
|
|
@@ -86,17 +95,25 @@ async function main(): Promise<void> {
|
|
|
86
95
|
},
|
|
87
96
|
})
|
|
88
97
|
|
|
89
|
-
//
|
|
90
|
-
//
|
|
91
|
-
//
|
|
92
|
-
|
|
98
|
+
// Terminal signals reach the whole foreground process group, so they also hit
|
|
99
|
+
// this helper when one of several dev servers stops. Acting on them would tear
|
|
100
|
+
// down the shared Caddy and hosts entries the remaining projects still use.
|
|
101
|
+
// The socket closing is the only signal this helper acts on.
|
|
102
|
+
//
|
|
103
|
+
// Ctrl+C, Ctrl+\, Ctrl+Z and a terminal closing all arrive this way: SIGINT,
|
|
104
|
+
// SIGQUIT, SIGTSTP and SIGHUP. Ignoring SIGTSTP also keeps the helper from
|
|
105
|
+
// being suspended, which would stall every attached project's RPC.
|
|
106
|
+
for (const signal of ['SIGINT', 'SIGQUIT', 'SIGTSTP', 'SIGHUP'] as const)
|
|
107
|
+
process.on(signal, () => {})
|
|
93
108
|
|
|
94
|
-
|
|
109
|
+
// SIGTERM is different: IDE and process-tree runners send it to shut the tool
|
|
110
|
+
// down for real. It still must not drop a live session, so it only tears down
|
|
111
|
+
// when no dev server is attached.
|
|
112
|
+
process.on('SIGTERM', () => {
|
|
113
|
+
if (helper.runtime.clientCount() > 0)
|
|
114
|
+
return
|
|
95
115
|
void helper.runtime.dispose().finally(() => process.exit(0))
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
process.on('SIGTERM', shutdown)
|
|
99
|
-
process.on('SIGHUP', shutdown)
|
|
116
|
+
})
|
|
100
117
|
|
|
101
118
|
// A closed stdout (the parent died) must not interrupt the cleanup path.
|
|
102
119
|
process.stdout.on('error', () => {})
|
|
@@ -105,5 +122,28 @@ async function main(): Promise<void> {
|
|
|
105
122
|
|
|
106
123
|
main().catch((error: Error) => {
|
|
107
124
|
process.stderr.write(`[uhrp-helper] fatal: ${error.message}\n`)
|
|
125
|
+
const errorFile = errorFileFrom(process.argv.slice(2))
|
|
126
|
+
if (errorFile) {
|
|
127
|
+
try {
|
|
128
|
+
writeFileSync(errorFile, error.message, { flag: 'wx' })
|
|
129
|
+
}
|
|
130
|
+
catch {}
|
|
131
|
+
}
|
|
108
132
|
process.exit(1)
|
|
109
133
|
})
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Where to leave the fatal error for the client on Windows, where the elevated
|
|
137
|
+
* helper has no stdio the client can read.
|
|
138
|
+
*
|
|
139
|
+
* The path comes from an unprivileged process, so only a new file with the
|
|
140
|
+
* expected name directly in this user's temp directory is accepted; it must
|
|
141
|
+
* not become a way to write files elsewhere as administrator.
|
|
142
|
+
*/
|
|
143
|
+
function errorFileFrom(argv: string[]): string | undefined {
|
|
144
|
+
const index = argv.indexOf('--error-file')
|
|
145
|
+
const path = index >= 0 ? argv[index + 1] : undefined
|
|
146
|
+
if (!path || !/^uhrp-helper-[0-9a-f]+\.log$/.test(basename(path)))
|
|
147
|
+
return undefined
|
|
148
|
+
return resolve(dirname(path)) === resolve(tmpdir()) ? path : undefined
|
|
149
|
+
}
|