localgate 0.5.0 → 0.6.1
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 +95 -17
- package/logic/cli/localgateBanner.ts +4 -4
- package/logic/cli/localgateCli.ts +36 -10
- package/logic/client/localgateProxyClient.ts +7 -1
- package/logic/config/localgateAliasStore.ts +58 -18
- package/logic/config/localgateNames.ts +12 -0
- package/logic/proxy/localgateHeaderRewrite.ts +6 -0
- package/logic/proxy/localgateHealth.ts +7 -0
- package/logic/proxy/localgatePortProbe.ts +42 -0
- package/logic/proxy/localgateProxy.ts +25 -1
- package/logic/proxy/localgateProxyHost.ts +39 -5
- package/logic/proxy/localgateRegistry.ts +17 -7
- package/logic/proxy/localgateStartPage.ts +491 -0
- package/logic/proxy/localgateUrl.ts +43 -0
- package/logic/run/localgateEnvRewrite.ts +29 -10
- package/logic/run/localgateNodeBootstrap.mjs +29 -0
- package/logic/run/localgateNodeOptions.ts +15 -0
- package/logic/run/localgateProcessTree.ts +122 -31
- package/logic/run/localgateRunner.ts +215 -124
- package/logic/run/localgateRunnerControl.ts +151 -0
- package/logic/run/localgateShellCommand.ts +51 -0
- package/package.json +1 -1
|
@@ -1,11 +1,20 @@
|
|
|
1
1
|
import { execFile } from "node:child_process";
|
|
2
2
|
import { connect } from "node:net";
|
|
3
|
+
import { win32 } from "node:path";
|
|
3
4
|
import { promisify } from "node:util";
|
|
4
5
|
|
|
5
6
|
export type LocalgateKillSpec =
|
|
6
7
|
| { via: "command"; file: string; args: string[] }
|
|
7
8
|
| { via: "signal"; target: number };
|
|
8
9
|
|
|
10
|
+
// What a kill actually achieved. `released` is the fact a caller may act on - the port is free - and the
|
|
11
|
+
// errors are the reason it is not, which the platform prints once and nowhere else.
|
|
12
|
+
export type LocalgateKillOutcome =
|
|
13
|
+
{
|
|
14
|
+
released: boolean;
|
|
15
|
+
errors: string[];
|
|
16
|
+
};
|
|
17
|
+
|
|
9
18
|
// Killing a dev server is not one call on either platform, and for the same reason: `npm run dev` spawns
|
|
10
19
|
// a shell that spawns the real server, so the process that holds the port is not the one whose pid we
|
|
11
20
|
// recorded. Windows walks parent links with `taskkill /T`, POSIX signals the process group the child was
|
|
@@ -15,57 +24,85 @@ export class LocalgateProcessTree
|
|
|
15
24
|
{
|
|
16
25
|
private static readonly pollIntervalMsConst = 100;
|
|
17
26
|
private static readonly connectTimeoutMsConst = 500;
|
|
27
|
+
private static readonly commandTimeoutMsConst = 5_000;
|
|
18
28
|
|
|
19
29
|
// Pure so both branches are testable from either OS: what runs is a decision, and only the execution
|
|
20
30
|
// below is platform-bound.
|
|
21
|
-
static killSpec(pid: number, platform: NodeJS.Platform = process.platform
|
|
31
|
+
static killSpec(pid: number, platform: NodeJS.Platform = process.platform,
|
|
32
|
+
systemRoot = process.env.SystemRoot): LocalgateKillSpec
|
|
22
33
|
{
|
|
23
|
-
if (platform == "win32") return {
|
|
34
|
+
if (platform == "win32") return {
|
|
35
|
+
via: "command", file: LocalgateProcessTree.windowsTool("taskkill.exe", systemRoot),
|
|
36
|
+
args: ["/T", "/F", "/PID", String(pid)]
|
|
37
|
+
};
|
|
24
38
|
|
|
25
39
|
// The negative pid is the process group. The child is spawned detached precisely so that it leads
|
|
26
40
|
// one, which is what reaches the shell's children instead of only the shell.
|
|
27
41
|
return { via: "signal", target: -pid };
|
|
28
42
|
}
|
|
29
43
|
|
|
30
|
-
static portHolderCommand(port: number, platform: NodeJS.Platform = process.platform
|
|
44
|
+
static portHolderCommand(port: number, platform: NodeJS.Platform = process.platform,
|
|
45
|
+
systemRoot = process.env.SystemRoot): { file: string; args: string[] }
|
|
31
46
|
{
|
|
32
47
|
if (platform == "win32")
|
|
33
48
|
return {
|
|
34
|
-
file: "powershell",
|
|
49
|
+
file: LocalgateProcessTree.windowsTool("WindowsPowerShell\\v1.0\\powershell.exe", systemRoot),
|
|
35
50
|
args: [
|
|
36
51
|
"-NoProfile",
|
|
52
|
+
"-NonInteractive",
|
|
37
53
|
"-Command",
|
|
38
|
-
|
|
54
|
+
`$ErrorActionPreference = 'Stop'; (Get-NetTCPConnection -ErrorAction Stop | `
|
|
55
|
+
+ `Where-Object { $_.LocalPort -eq ${port} -and $_.State -eq 'Listen' } | Select-Object -First 1).OwningProcess`
|
|
39
56
|
]
|
|
40
57
|
};
|
|
41
58
|
|
|
42
59
|
return { file: "lsof", args: ["-ti", `tcp:${port}`, "-sTCP:LISTEN"] };
|
|
43
60
|
}
|
|
44
61
|
|
|
45
|
-
static async killTree(pid: number, port: number, timeoutMs = 10_000): Promise<
|
|
62
|
+
static async killTree(pid: number, port: number, timeoutMs = 10_000): Promise<LocalgateKillOutcome>
|
|
46
63
|
{
|
|
47
|
-
|
|
64
|
+
const errors: string[] = [];
|
|
65
|
+
const killed = await LocalgateProcessTree.killPid(pid, timeoutMs);
|
|
66
|
+
if (killed !== null) errors.push(killed);
|
|
48
67
|
|
|
49
|
-
if (await LocalgateProcessTree.waitForPortRelease(port, timeoutMs)) return;
|
|
68
|
+
if (await LocalgateProcessTree.waitForPortRelease(port, timeoutMs)) return { released: true, errors };
|
|
50
69
|
|
|
51
|
-
const holder = await LocalgateProcessTree.findPortHolder(port);
|
|
70
|
+
const holder = await LocalgateProcessTree.findPortHolder(port, errors, timeoutMs);
|
|
52
71
|
if (holder !== null && holder != pid)
|
|
53
72
|
{
|
|
54
|
-
await LocalgateProcessTree.killPid(holder);
|
|
55
|
-
|
|
73
|
+
const holderKilled = await LocalgateProcessTree.killPid(holder, timeoutMs);
|
|
74
|
+
if (holderKilled !== null) errors.push(holderKilled);
|
|
75
|
+
if (await LocalgateProcessTree.waitForPortRelease(port, timeoutMs)) return { released: true, errors };
|
|
56
76
|
}
|
|
77
|
+
|
|
78
|
+
// The wait ran out rather than the port being sampled once, so this last look only catches a release
|
|
79
|
+
// that happened between the deadline and now. Everything else is a process that is still serving.
|
|
80
|
+
return { released: !await LocalgateProcessTree.isPortListening(port), errors };
|
|
57
81
|
}
|
|
58
82
|
|
|
59
83
|
// For a process nobody owns any more. The recorded pid belongs to something that already exited and
|
|
60
84
|
// both platforms reuse pid numbers, so killing it blind can take out an unrelated tree; whoever
|
|
61
85
|
// actually holds the port is the only target that is still known to be the right one.
|
|
62
|
-
static async killPortHolder(port: number, timeoutMs = 10_000): Promise<
|
|
86
|
+
static async killPortHolder(port: number, timeoutMs = 10_000): Promise<LocalgateKillOutcome>
|
|
63
87
|
{
|
|
64
|
-
const
|
|
65
|
-
|
|
88
|
+
const errors: string[] = [];
|
|
89
|
+
const holder = await LocalgateProcessTree.findPortHolder(port, errors, timeoutMs);
|
|
90
|
+
if (holder === null) return { released: !await LocalgateProcessTree.isPortListening(port), errors };
|
|
91
|
+
|
|
92
|
+
const killed = await LocalgateProcessTree.killPid(holder, timeoutMs);
|
|
93
|
+
if (killed !== null) errors.push(killed);
|
|
94
|
+
return {
|
|
95
|
+
released: await LocalgateProcessTree.waitForPortRelease(port, timeoutMs),
|
|
96
|
+
errors
|
|
97
|
+
};
|
|
98
|
+
}
|
|
66
99
|
|
|
67
|
-
|
|
68
|
-
|
|
100
|
+
// Appended to a caller's own "still held" line, which on its own cannot say which kill was refused or
|
|
101
|
+
// what it printed. Empty when nothing failed, which is the case where the process outlived a kill that
|
|
102
|
+
// the platform accepted.
|
|
103
|
+
static describeErrors(outcome: LocalgateKillOutcome): string
|
|
104
|
+
{
|
|
105
|
+
return outcome.errors.length > 0 ? ` (${outcome.errors.join("; ")})` : "";
|
|
69
106
|
}
|
|
70
107
|
|
|
71
108
|
static async waitForPortRelease(port: number, timeoutMs: number): Promise<boolean>
|
|
@@ -97,45 +134,57 @@ export class LocalgateProcessTree
|
|
|
97
134
|
});
|
|
98
135
|
}
|
|
99
136
|
|
|
100
|
-
static async findPortHolder(port: number
|
|
137
|
+
static async findPortHolder(port: number, errors: string[] = [],
|
|
138
|
+
timeoutMs = LocalgateProcessTree.commandTimeoutMsConst): Promise<number | null>
|
|
101
139
|
{
|
|
102
|
-
const { file, args } = LocalgateProcessTree.portHolderCommand(port);
|
|
103
|
-
|
|
104
140
|
try
|
|
105
141
|
{
|
|
106
|
-
const {
|
|
142
|
+
const { file, args } = LocalgateProcessTree.portHolderCommand(port);
|
|
143
|
+
const { stdout } = await LocalgateProcessTree.runCommand(file, args, timeoutMs);
|
|
107
144
|
// lsof prints one pid per line and can name several; the listener is the first.
|
|
108
145
|
const pid = Number.parseInt(stdout.trim().split(/\r?\n/)[0] ?? "", 10);
|
|
109
146
|
return Number.isFinite(pid) && pid > 0 ? pid : null;
|
|
110
147
|
}
|
|
111
|
-
catch
|
|
148
|
+
catch (error)
|
|
112
149
|
{
|
|
113
|
-
|
|
150
|
+
errors.push(`port lookup ${port}: ${LocalgateProcessTree.describeFailure(error)}`);
|
|
114
151
|
return null;
|
|
115
152
|
}
|
|
116
153
|
}
|
|
117
154
|
|
|
118
|
-
|
|
155
|
+
// Returns what the kill said when it failed, and null when it was accepted. A process that was already
|
|
156
|
+
// gone and a `taskkill` that was refused look the same from here, so the decision is left to the port:
|
|
157
|
+
// this only makes sure the reason survives as far as the caller that has to report it.
|
|
158
|
+
private static async killPid(pid: number, timeoutMs: number): Promise<string | null>
|
|
119
159
|
{
|
|
120
|
-
|
|
160
|
+
let spec: LocalgateKillSpec;
|
|
161
|
+
try
|
|
162
|
+
{
|
|
163
|
+
spec = LocalgateProcessTree.killSpec(pid);
|
|
164
|
+
}
|
|
165
|
+
catch (error)
|
|
166
|
+
{
|
|
167
|
+
return `kill ${pid}: ${LocalgateProcessTree.describeFailure(error)}`;
|
|
168
|
+
}
|
|
121
169
|
|
|
122
170
|
if (spec.via == "command")
|
|
123
171
|
{
|
|
124
172
|
try
|
|
125
173
|
{
|
|
126
|
-
await
|
|
174
|
+
await LocalgateProcessTree.runCommand(spec.file, spec.args, timeoutMs);
|
|
175
|
+
return null;
|
|
127
176
|
}
|
|
128
|
-
catch
|
|
177
|
+
catch (error)
|
|
129
178
|
{
|
|
130
|
-
|
|
179
|
+
return LocalgateProcessTree.describeFailure(error);
|
|
131
180
|
}
|
|
132
|
-
return;
|
|
133
181
|
}
|
|
134
182
|
else if (spec.via == "signal")
|
|
135
183
|
{
|
|
136
184
|
try
|
|
137
185
|
{
|
|
138
186
|
process.kill(spec.target, "SIGKILL");
|
|
187
|
+
return null;
|
|
139
188
|
}
|
|
140
189
|
catch
|
|
141
190
|
{
|
|
@@ -144,15 +193,57 @@ export class LocalgateProcessTree
|
|
|
144
193
|
try
|
|
145
194
|
{
|
|
146
195
|
process.kill(Math.abs(spec.target), "SIGKILL");
|
|
196
|
+
return null;
|
|
147
197
|
}
|
|
148
|
-
catch
|
|
198
|
+
catch (error)
|
|
149
199
|
{
|
|
150
|
-
|
|
200
|
+
return `kill ${Math.abs(spec.target)}: ${LocalgateProcessTree.describeFailure(error)}`;
|
|
151
201
|
}
|
|
152
202
|
}
|
|
153
|
-
return;
|
|
154
203
|
}
|
|
155
204
|
else
|
|
156
205
|
throw new Error(`Unknown kill spec: ${JSON.stringify(spec)}`);
|
|
157
206
|
}
|
|
207
|
+
|
|
208
|
+
// A runner can inherit a project-local PATH. Use the system tools without a shell, and include the
|
|
209
|
+
// selected path in failures instead of dumping an environment that can contain credentials.
|
|
210
|
+
private static windowsTool(relativePath: string, systemRoot: string | undefined): string
|
|
211
|
+
{
|
|
212
|
+
if (!systemRoot || !win32.isAbsolute(systemRoot))
|
|
213
|
+
throw new Error("SystemRoot must be an absolute Windows directory to resolve process tools");
|
|
214
|
+
return win32.join(systemRoot, "System32", relativePath);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
private static async runCommand(file: string, args: string[], timeoutMs: number): Promise<{ stdout: string; stderr: string }>
|
|
218
|
+
{
|
|
219
|
+
const timeout = Math.max(1, Math.min(timeoutMs, LocalgateProcessTree.commandTimeoutMsConst));
|
|
220
|
+
try
|
|
221
|
+
{
|
|
222
|
+
return await promisify(execFile)(file, args, { timeout, killSignal: "SIGKILL", windowsHide: true, shell: false });
|
|
223
|
+
}
|
|
224
|
+
catch (error)
|
|
225
|
+
{
|
|
226
|
+
throw new Error(`${file} ${JSON.stringify(args)} [shell=false timeout=${timeout}ms]: `
|
|
227
|
+
+ LocalgateProcessTree.describeFailure(error));
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
private static describeFailure(error: unknown): string
|
|
232
|
+
{
|
|
233
|
+
if (typeof error != "object" || error === null) return String(error);
|
|
234
|
+
|
|
235
|
+
const detail: { stdout?: unknown; stderr?: unknown; message?: unknown;
|
|
236
|
+
code?: unknown; signal?: unknown; killed?: unknown } = error;
|
|
237
|
+
const stdout = typeof detail.stdout == "string" ? detail.stdout.trim() : "";
|
|
238
|
+
const stderr = typeof detail.stderr == "string" ? detail.stderr.trim() : "";
|
|
239
|
+
const message = typeof detail.message == "string" ? detail.message.trim() : "";
|
|
240
|
+
const parts: string[] = [];
|
|
241
|
+
if (detail.code !== undefined) parts.push(`code=${String(detail.code)}`);
|
|
242
|
+
if (detail.signal !== undefined) parts.push(`signal=${String(detail.signal)}`);
|
|
243
|
+
if (detail.killed !== undefined) parts.push(`killed=${String(detail.killed)}`);
|
|
244
|
+
if (stdout) parts.push(`stdout=${stdout}`);
|
|
245
|
+
if (stderr) parts.push(`stderr=${stderr}`);
|
|
246
|
+
if (!stdout && !stderr) parts.push(message || String(error));
|
|
247
|
+
return parts.join("; ").replace(/\s+/g, " ");
|
|
248
|
+
}
|
|
158
249
|
}
|