localgate 0.4.0 → 0.6.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 +91 -19
- package/logic/cli/localgateBanner.ts +9 -8
- package/logic/cli/localgateCli.ts +158 -23
- package/logic/client/localgateProxyClient.ts +9 -1
- package/logic/config/localgateAliasStore.ts +187 -0
- package/logic/config/localgateConfigReport.ts +45 -0
- package/logic/config/localgateMachineConfig.ts +7 -0
- package/logic/config/localgateNames.ts +19 -0
- package/logic/proxy/localgateAliasRoute.ts +46 -0
- package/logic/proxy/localgateControlApi.ts +18 -0
- package/logic/proxy/localgateHealth.ts +53 -15
- package/logic/proxy/localgatePortProbe.ts +42 -0
- package/logic/proxy/localgateProxy.ts +34 -1
- package/logic/proxy/localgateProxyHost.ts +52 -6
- package/logic/proxy/localgateRegistry.ts +20 -8
- package/logic/proxy/localgateStartPage.ts +491 -0
- package/logic/proxy/localgateUrl.ts +43 -0
- package/logic/run/localgateEnvRewrite.ts +28 -6
- 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
package/README.md
CHANGED
|
@@ -49,7 +49,7 @@ request. Localgate only needs the public prefix to register the exact public nam
|
|
|
49
49
|
The table lives in the proxy's memory. There is no config file listing apps, no port assignment to keep
|
|
50
50
|
in sync, and nothing to clean up after a crash.
|
|
51
51
|
|
|
52
|
-
## How restart
|
|
52
|
+
## How restart and stop work
|
|
53
53
|
|
|
54
54
|
The runner sits in your editor's debug terminal and owns the dev server as its child. That is what lets
|
|
55
55
|
a restart keep the terminal and debugger while swapping the process underneath. The restart also
|
|
@@ -70,6 +70,18 @@ sequenceDiagram
|
|
|
70
70
|
Note over Cli,Dev: same route, same port, the browser keeps its URL
|
|
71
71
|
```
|
|
72
72
|
|
|
73
|
+
A restart reports success only once the runner has seen both halves of it: the old tree gone with its
|
|
74
|
+
port free, and a replacement child running. If the kill is refused or the port stays bound, the runner
|
|
75
|
+
answers `409` with what the kill said, `localgate restart` prints that and exits non-zero, and the old
|
|
76
|
+
process is left named rather than reported as replaced. See
|
|
77
|
+
[docs/architecture/restart-verification.md](docs/architecture/restart-verification.md).
|
|
78
|
+
|
|
79
|
+
`localgate stop` is decided the same way, on the same fact: the runner kills the tree first and answers
|
|
80
|
+
with what that achieved, so a stop the system refused prints the reason and exits non-zero instead of
|
|
81
|
+
`stopped`. A refused stop keeps its route and its runner, because the process behind that route is still
|
|
82
|
+
the one serving it. Ctrl+C is the other way round and ends the runner whatever the kill said. See
|
|
83
|
+
[docs/architecture/stop-verification.md](docs/architecture/stop-verification.md).
|
|
84
|
+
|
|
73
85
|
While the dev server is down the proxy answers with a readable page instead of a connection error.
|
|
74
86
|
Websocket upgrades are proxied, and on Next.js dev endpoints (`/_next/*`, `/__nextjs*`) the `Origin`
|
|
75
87
|
and `Referer` headers are mapped back to the route's `.localhost` name. The rewrite requires the header
|
|
@@ -119,7 +131,9 @@ The debugger attaches to the child as usual, and the terminal stays where it is
|
|
|
119
131
|
Press the editor's restart button and you start a *second* `localgate run` for the same project, which
|
|
120
132
|
used to fail twice over: the name moved to the new runner and left the old one unreachable, and the
|
|
121
133
|
dev server then refused the port because of its own lock. So the collision is settled before anything
|
|
122
|
-
is claimed - the new run finds its predecessor
|
|
134
|
+
is claimed - the new run finds its predecessor, prints what it is about to stop in the debug terminal
|
|
135
|
+
it was started from, and takes over without asking. A compound launch of several projects therefore
|
|
136
|
+
restarts all of them, with no terminal left waiting for an answer:
|
|
123
137
|
|
|
124
138
|
```
|
|
125
139
|
localgate myapp.localhost is already running
|
|
@@ -130,32 +144,79 @@ is claimed - the new run finds its predecessor and asks, in the debug terminal i
|
|
|
130
144
|
pids runner 45568, child 4544
|
|
131
145
|
debugger attached
|
|
132
146
|
|
|
133
|
-
|
|
147
|
+
localgate: stopping myapp.localhost (runner 45568)
|
|
134
148
|
```
|
|
135
149
|
|
|
136
|
-
|
|
150
|
+
The old one is stopped the good way: its own runner kills the child tree, releases the port and
|
|
137
151
|
deregisters itself, so its terminal ends cleanly and the new run takes the name. Killing by pid is
|
|
138
152
|
only the fallback for a runner that no longer answers.
|
|
139
153
|
|
|
140
|
-
A route whose runner is already gone is
|
|
141
|
-
holds the port - that is the orphan a framework lock trips over - so it is
|
|
142
|
-
|
|
154
|
+
A route whose runner is already gone is cleared too. Its dev server usually outlived it and still
|
|
155
|
+
holds the port - that is the orphan a framework lock trips over - so it is killed and the name reused.
|
|
156
|
+
|
|
157
|
+
The same takeover happens if the name is taken between finding it free and claiming it. The route
|
|
158
|
+
table lives in the proxy, so a proxy that has just started has an empty one, and the runners that were
|
|
159
|
+
already running refill it from their heartbeats a few seconds later - a window in which the check sees
|
|
160
|
+
a free name that is not free.
|
|
161
|
+
|
|
162
|
+
An alias is never taken over: `run` refuses the name and exits 1.
|
|
163
|
+
|
|
164
|
+
## The start page
|
|
165
|
+
|
|
166
|
+
`http://start.localhost` is every route in a browser. One card per app: click it anywhere and it opens.
|
|
167
|
+
It needs no setup - the proxy serves the page from itself, and the name is reserved, so no project or
|
|
168
|
+
alias can take it.
|
|
169
|
+
|
|
170
|
+

|
|
171
|
+
|
|
172
|
+
The card opens the address that works from where you opened the page, and the route's other addresses -
|
|
173
|
+
the shared name, the public one - are chips next to it. The line at the bottom is the upstream port, the
|
|
174
|
+
command and the directory. The page refreshes itself every few seconds, so a dev server that has just
|
|
175
|
+
started appears without a reload, and a state changes while you watch.
|
|
176
|
+
|
|
177
|
+
The badge is measured, not remembered: the page opens a connection to each route's port as it renders,
|
|
178
|
+
so a route nobody has visited says **running** rather than sitting at "starting", and a route with
|
|
179
|
+
nothing behind it says **not running** and dims. A wedged app - one that accepts connections but answers
|
|
180
|
+
nothing - still reads **not responding**, because only forwarded traffic can tell you that.
|
|
181
|
+
|
|
182
|
+
A `stop` chip ends a dev server: the first click turns that card into a question, the second asks the
|
|
183
|
+
runner to kill the child tree and exit, exactly as `localgate stop` does. It is on the loopback page
|
|
184
|
+
only, and it takes a same-origin POST, so no other site the browser has open can reach it.
|
|
185
|
+
|
|
186
|
+
An alias has no runner, so it gets that button only once you say how to stop what it points at:
|
|
187
|
+
|
|
188
|
+
```bash
|
|
189
|
+
localgate alias wagtail 8003 --stop "docker stop wagtail"
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
The command lives in `~/.localgate/aliases.json`, which is machine-local and never committed, and the
|
|
193
|
+
question names it before it runs. Without one, an alias card has no stop button - localgate will not
|
|
194
|
+
guess how to end a process it did not start.
|
|
195
|
+
|
|
196
|
+
With a machine config it also answers on `start.<label>.<your-domain>`, which is the address to send a
|
|
197
|
+
colleague - and that answer is narrower than the local one. A route in mode `local` is absent from it,
|
|
198
|
+
its name included, and the routes that remain show only the addresses that work off this machine,
|
|
199
|
+
without the upstream port, the command or the directory. Details in
|
|
200
|
+
[docs/architecture/start-page.md](docs/architecture/start-page.md).
|
|
143
201
|
|
|
144
202
|
## Commands
|
|
145
203
|
|
|
146
204
|
| Command | What it does |
|
|
147
205
|
|---|---|
|
|
148
|
-
| `localgate run [--force] <cmd...>` | Run a dev server behind its name.
|
|
206
|
+
| `localgate run [--force] <cmd...>` | Run a dev server behind its name. One already running for the project is stopped first; without a terminal, only with `--force`. |
|
|
149
207
|
| `localgate list [--json]` | Show active routes. |
|
|
150
208
|
| `localgate status [app] [--json]` | State of one route, with a tail of its output in `--json`. |
|
|
151
209
|
| `localgate restart [app]` | Restart a route's dev server. |
|
|
152
210
|
| `localgate stop [app]` | Stop a route's dev server. |
|
|
153
211
|
| `localgate logs [app] [--lines N]` | Tail a route's captured output. |
|
|
154
|
-
| `localgate alias <name> <port
|
|
212
|
+
| `localgate alias <name> <port> [--stop "<cmd>"]` | Register a static route, e.g. a docker service. Remembered across proxy restarts. `--stop` records what ends it, which gives it a stop button on the start page. |
|
|
213
|
+
| `localgate alias --remove <name>` | Drop a static route and forget it. |
|
|
214
|
+
| `localgate config [dir] [--json]` | Machine and project config, the names it implies, and the alias file. |
|
|
155
215
|
| `localgate cloudflare-info` | Print the DNS and ingress entries to paste. |
|
|
156
216
|
| `localgate prune` | Drop routes whose runner is gone. |
|
|
157
217
|
|
|
158
|
-
With no `[app]` argument, a command acts on the route owning the current directory.
|
|
218
|
+
With no `[app]` argument, a command acts on the route owning the current directory. `start` is not a
|
|
219
|
+
name a project or an alias can use: it belongs to the start page above.
|
|
159
220
|
|
|
160
221
|
## Working with a coding agent
|
|
161
222
|
|
|
@@ -171,9 +232,10 @@ localgate list --json # everything running on the machine
|
|
|
171
232
|
`status --json` includes the last lines the dev server printed, so an agent can check whether its
|
|
172
233
|
change compiled without owning the terminal it compiled in.
|
|
173
234
|
|
|
174
|
-
The takeover
|
|
175
|
-
|
|
176
|
-
|
|
235
|
+
The takeover above is the one place this matters for safety. **With no terminal - an agent, a CI job -
|
|
236
|
+
`localgate run` prints what is already running and exits 1 instead of taking it over.** It never ends
|
|
237
|
+
the developer's session by accident; `localgate run --force` takes over deliberately. To pick up a
|
|
238
|
+
change, use `localgate restart`, which keeps the runner, the terminal and the debugger.
|
|
177
239
|
|
|
178
240
|
## Names, and who can reach them
|
|
179
241
|
|
|
@@ -219,6 +281,8 @@ With that file present, `myapp` in mode `lan` also answers on `myapp.dev.example
|
|
|
219
281
|
environment variables (`NEXT_PUBLIC_*`, `*_PUBLIC_URL`, `AUTH_URL`, `NEXTAUTH_URL`) are rewritten from
|
|
220
282
|
`.localhost` to the name selected by the current mode on the way into the dev server. Public URLs use
|
|
221
283
|
HTTPS and never carry the proxy's internal LAN port. Server-side variables stay on `.localhost`.
|
|
284
|
+
Node children get a lookup hook for that reserved suffix, so Fetch and the standard HTTP clients reach
|
|
285
|
+
the local proxy even when the operating system resolver does not expand nested localhost names.
|
|
222
286
|
|
|
223
287
|
For reaching an app from outside the LAN, `localgate cloudflare-info` prints the exact prefixed DNS
|
|
224
288
|
record and tunnel ingress entry. It prints them and stops: localgate holds no API token and sends
|
|
@@ -246,7 +310,8 @@ externally indistinguishable from a wedged one, and killing it would destroy the
|
|
|
246
310
|
standing in. Reporting always happens; only the restart is behind the switch.
|
|
247
311
|
|
|
248
312
|
If the proxy itself dies, every running app notices within ten seconds and re-registers its route, so
|
|
249
|
-
the names come back without touching a single dev server.
|
|
313
|
+
the names come back without touching a single dev server. An alias has no process to notice, so the
|
|
314
|
+
starting proxy replays the aliases from `~/.localgate/aliases.json` instead.
|
|
250
315
|
|
|
251
316
|
## Design rules
|
|
252
317
|
|
|
@@ -254,8 +319,11 @@ the names come back without touching a single dev server.
|
|
|
254
319
|
`~/.localgate/config.json`; a repository carries at most a `package.json` key holding a mode
|
|
255
320
|
(`local` / `lan` / `internet`) and an optional name. Absent key means `local`, which is names on
|
|
256
321
|
`.localhost` only.
|
|
257
|
-
- **Nothing is persisted.** The route table is live state; after a reboot nothing
|
|
258
|
-
command.
|
|
322
|
+
- **Nothing is persisted except alias intent.** The route table is live state; after a reboot nothing
|
|
323
|
+
runs until the first command. The one exception is `localgate alias`: it records the name and the port
|
|
324
|
+
in `~/.localgate/aliases.json`, and a starting proxy serves those names again. An alias has no process
|
|
325
|
+
of its own, so a file is the only thing that can bring it back. Drop it with
|
|
326
|
+
`localgate alias --remove <name>`.
|
|
259
327
|
- **The proxy is not a service.** The first `run` or `alias` starts it, and it exits once the last route
|
|
260
328
|
disappears.
|
|
261
329
|
- **No TLS, no tunnels of its own.** Plain http; put Cloudflare in front when something must be reachable
|
|
@@ -272,6 +340,10 @@ machine that resolved it", so the LAN listener never answers one, whatever `Host
|
|
|
272
340
|
carries and whether it is a plain request or a websocket upgrade. An app in the default `local` mode
|
|
273
341
|
has no other name, which is what keeps it on this machine.
|
|
274
342
|
|
|
343
|
+
The start page follows the same rule rather than working around it: on the LAN it lists only what that
|
|
344
|
+
listener would serve. An app in mode `local` does not appear on it at all - not its name, its port, its
|
|
345
|
+
command or its directory.
|
|
346
|
+
|
|
275
347
|
Those dev servers are the real exposure: in `lan` mode anyone on the network reaches them with no
|
|
276
348
|
authentication, and in `internet` mode anyone holding the URL does. Use `lan` on networks you trust, and
|
|
277
349
|
switch back from `internet` when you are done. There is no TLS - it is plain http by design.
|
|
@@ -291,9 +363,9 @@ build step.
|
|
|
291
363
|
Early. Windows, Linux and macOS: the kill that makes `restart` reliable goes through `taskkill /T` on
|
|
292
364
|
Windows and the child's process group elsewhere, and the port-holder lookup is PowerShell or `lsof`.
|
|
293
365
|
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
366
|
+
Browsers and Node processes started through Localgate resolve `*.localhost` to loopback. Other
|
|
367
|
+
command-line programs still use the operating system resolver, so on a system that does not expand
|
|
368
|
+
nested localhost names they need an explicit resolver option or a hosts entry.
|
|
297
369
|
|
|
298
370
|
Bug reports and ideas are welcome - see [CONTRIBUTING.md](CONTRIBUTING.md), and
|
|
299
371
|
[SECURITY.md](SECURITY.md) for anything security-related. Licensed under
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { LocalgateNames } from "../config/localgateNames.ts";
|
|
1
2
|
import type { LocalgateRoute } from "../proxy/localgateRegistry.ts";
|
|
2
3
|
import { LocalgateUrl } from "../proxy/localgateUrl.ts";
|
|
3
4
|
|
|
@@ -14,14 +15,14 @@ export class LocalgateBanner
|
|
|
14
15
|
|
|
15
16
|
static render(route: LocalgateRoute, proxyPort: number): string
|
|
16
17
|
{
|
|
17
|
-
const [local, network, internet] = route.names;
|
|
18
18
|
const [nextLabel, nextCommand] = LocalgateBanner.next(route);
|
|
19
19
|
|
|
20
20
|
const lines = ["", ` localgate ${LocalgateBanner.appName(route)} · ${LocalgateBanner.subtitle(route)}`, ""];
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
// Widest reach first, because the address a person came here for is the one that leaves the machine.
|
|
23
|
+
for (const address of LocalgateUrl.routeAddresses(route, proxyPort).reverse())
|
|
24
|
+
lines.push(LocalgateBanner.row(address.label, address.url));
|
|
25
|
+
|
|
25
26
|
lines.push("", LocalgateBanner.row("upstream", `127.0.0.1:${route.port}`), LocalgateBanner.row(nextLabel, nextCommand), "");
|
|
26
27
|
|
|
27
28
|
return `${lines.join("\n")}\n`;
|
|
@@ -30,7 +31,7 @@ export class LocalgateBanner
|
|
|
30
31
|
// The short name a person types, which is the first label of the local name.
|
|
31
32
|
static appName(route: LocalgateRoute): string
|
|
32
33
|
{
|
|
33
|
-
return route.names[0]
|
|
34
|
+
return LocalgateNames.shortName(route.names[0]);
|
|
34
35
|
}
|
|
35
36
|
|
|
36
37
|
private static subtitle(route: LocalgateRoute): string
|
|
@@ -41,12 +42,12 @@ export class LocalgateBanner
|
|
|
41
42
|
throw new Error(`Unknown route kind: ${JSON.stringify(route.kind)}`);
|
|
42
43
|
}
|
|
43
44
|
|
|
44
|
-
// An alias has no process, so there is nothing to restart
|
|
45
|
-
//
|
|
45
|
+
// An alias has no process, so there is nothing to restart. It used to need the line that put it back
|
|
46
|
+
// after a proxy restart; now that its intent is remembered, the line it needs is the way out.
|
|
46
47
|
private static next(route: LocalgateRoute): [string, string]
|
|
47
48
|
{
|
|
48
49
|
if (route.kind == "app") return ["restart", "localgate restart"];
|
|
49
|
-
else if (route.kind == "alias") return ["
|
|
50
|
+
else if (route.kind == "alias") return ["remove", `localgate alias --remove ${LocalgateBanner.appName(route)}`];
|
|
50
51
|
else
|
|
51
52
|
throw new Error(`Unknown route kind: ${JSON.stringify(route.kind)}`);
|
|
52
53
|
}
|
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
import { LocalgateProxyClient } from "../client/localgateProxyClient.ts";
|
|
2
2
|
import { LocalgateBanner } from "./localgateBanner.ts";
|
|
3
3
|
import { LocalgateCloudflareInfo } from "../cloudflare/localgateCloudflareInfo.ts";
|
|
4
|
+
import { LocalgateAliasStore } from "../config/localgateAliasStore.ts";
|
|
4
5
|
import { LocalgateMachineConfig } from "../config/localgateMachineConfig.ts";
|
|
5
6
|
import { LocalgateNames } from "../config/localgateNames.ts";
|
|
6
7
|
import { LocalgateProjectConfig } from "../config/localgateProjectConfig.ts";
|
|
8
|
+
import { LocalgateConfigReporter } from "../config/localgateConfigReport.ts";
|
|
9
|
+
import { LocalgateAliasRoute } from "../proxy/localgateAliasRoute.ts";
|
|
7
10
|
import { LocalgateProxyHost } from "../proxy/localgateProxyHost.ts";
|
|
11
|
+
import { LocalgateStartPage } from "../proxy/localgateStartPage.ts";
|
|
8
12
|
import { LocalgateUrl } from "../proxy/localgateUrl.ts";
|
|
9
13
|
import { LocalgateRouteConflictError } from "../proxy/localgateRegistry.ts";
|
|
10
14
|
import type { LocalgateRoute } from "../proxy/localgateRegistry.ts";
|
|
11
15
|
import { LocalgateRunner } from "../run/localgateRunner.ts";
|
|
16
|
+
import { LocalgateRunnerControl } from "../run/localgateRunnerControl.ts";
|
|
12
17
|
|
|
13
18
|
export class LocalgateCli
|
|
14
19
|
{
|
|
@@ -36,6 +41,7 @@ export class LocalgateCli
|
|
|
36
41
|
else if (command == "stop") return LocalgateCli.control(rest, "stop");
|
|
37
42
|
else if (command == "logs") return LocalgateCli.logs(rest);
|
|
38
43
|
else if (command == "alias") return LocalgateCli.alias(rest);
|
|
44
|
+
else if (command == "config") return LocalgateCli.config(rest);
|
|
39
45
|
else if (command == "cloudflare-info") return LocalgateCli.cloudflareInfo();
|
|
40
46
|
else if (command == "prune") return LocalgateCli.prune();
|
|
41
47
|
else
|
|
@@ -59,6 +65,11 @@ export class LocalgateCli
|
|
|
59
65
|
if (routes.length == 0)
|
|
60
66
|
{
|
|
61
67
|
process.stdout.write("No routes. Nothing is running, and the proxy is not needed until something is.\n");
|
|
68
|
+
|
|
69
|
+
const dormant = LocalgateAliasStore.load().length;
|
|
70
|
+
if (dormant > 0)
|
|
71
|
+
process.stdout.write(`${dormant} alias(es) will answer again when the proxy next starts.\n`);
|
|
72
|
+
|
|
62
73
|
return 0;
|
|
63
74
|
}
|
|
64
75
|
|
|
@@ -73,6 +84,41 @@ export class LocalgateCli
|
|
|
73
84
|
process.stdout.write("\n");
|
|
74
85
|
}
|
|
75
86
|
|
|
87
|
+
// Only with a route in hand, because the page is served by the proxy and the proxy is not running
|
|
88
|
+
// when nothing is registered - naming it above would send the reader to a refused connection.
|
|
89
|
+
process.stdout.write(`All of this in a browser: ${LocalgateStartPage.localUrl(proxyPort)}\n`);
|
|
90
|
+
return 0;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Read-only: what this machine and this project are configured as, and the names that follow from it.
|
|
94
|
+
// Other tools ask for this instead of parsing ~/.localgate/config.json and re-deriving the names, which
|
|
95
|
+
// is how two copies of the same rules start to drift.
|
|
96
|
+
private static config(args: string[]): number
|
|
97
|
+
{
|
|
98
|
+
const asJson = args.includes("--json");
|
|
99
|
+
const directory = args.find(argument => !argument.startsWith("--")) ?? process.cwd();
|
|
100
|
+
const report = LocalgateConfigReporter.build(directory);
|
|
101
|
+
|
|
102
|
+
if (asJson)
|
|
103
|
+
{
|
|
104
|
+
process.stdout.write(`${JSON.stringify(report, null, 2)}\n`);
|
|
105
|
+
return 0;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
process.stdout.write([
|
|
109
|
+
`machine config ${report.machineFilePath}${report.machine ? "" : " (missing - this machine serves .localhost only)"}`,
|
|
110
|
+
`label ${report.machine?.label ?? "-"}`,
|
|
111
|
+
`base domain ${report.machine?.baseDomain ?? "-"}`,
|
|
112
|
+
`lan ip ${report.machine?.lanIp ?? "-"}`,
|
|
113
|
+
`public prefix ${report.machine?.publicPrefix ?? "-"}`,
|
|
114
|
+
`proxy port ${report.proxyPort}`,
|
|
115
|
+
`project ${report.project ? `${report.project.name} (${report.project.mode})` : "-"}`,
|
|
116
|
+
`local name ${report.names?.local ?? "-"}`,
|
|
117
|
+
`lan name ${report.names?.lan ?? "-"}`,
|
|
118
|
+
`public name ${report.names?.public ?? "-"}`,
|
|
119
|
+
`aliases ${report.aliases.filePath} (${report.aliases.count})`,
|
|
120
|
+
""
|
|
121
|
+
].join("\n"));
|
|
76
122
|
return 0;
|
|
77
123
|
}
|
|
78
124
|
|
|
@@ -125,7 +171,8 @@ export class LocalgateCli
|
|
|
125
171
|
const response = await fetch(`${route.controlUrl}/${action}`, { method: "POST" });
|
|
126
172
|
if (!response.ok)
|
|
127
173
|
{
|
|
128
|
-
|
|
174
|
+
const detail = LocalgateRunnerControl.refusalDetail(await response.text().catch(() => ""));
|
|
175
|
+
process.stderr.write(`localgate: ${action} failed, the runner answered ${response.status}${detail}\n`);
|
|
129
176
|
return 1;
|
|
130
177
|
}
|
|
131
178
|
|
|
@@ -149,35 +196,59 @@ export class LocalgateCli
|
|
|
149
196
|
|
|
150
197
|
private static async alias(args: string[]): Promise<number>
|
|
151
198
|
{
|
|
152
|
-
|
|
199
|
+
if (args[0] == "--remove") return LocalgateCli.aliasRemove(args[1]);
|
|
200
|
+
|
|
201
|
+
// Everything after --stop is the command, kept out of the positional pair so a name and a port can
|
|
202
|
+
// still be read the way they always were.
|
|
203
|
+
const stopIndex = args.indexOf("--stop");
|
|
204
|
+
const stop = stopIndex >= 0 ? args.slice(stopIndex + 1).join(" ").trim() : null;
|
|
205
|
+
const [name, portText] = stopIndex >= 0 ? args.slice(0, stopIndex) : args;
|
|
153
206
|
const port = Number.parseInt(portText ?? "", 10);
|
|
154
207
|
|
|
155
208
|
if (!name || !Number.isFinite(port))
|
|
156
209
|
{
|
|
157
|
-
process.stderr.write("localgate: usage is localgate alias <name> <port>\n");
|
|
210
|
+
process.stderr.write("localgate: usage is localgate alias <name> <port> [--stop \"<command>\"]\n");
|
|
211
|
+
return 1;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (stop !== null && stop.length == 0)
|
|
215
|
+
{
|
|
216
|
+
process.stderr.write("localgate: --stop needs the command that stops the service, "
|
|
217
|
+
+ "for example --stop \"docker stop myservice\"\n");
|
|
218
|
+
return 1;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (LocalgateNames.isReserved(name))
|
|
222
|
+
{
|
|
223
|
+
process.stderr.write(`localgate: "${LocalgateNames.startNameConst}" is reserved for the start page, `
|
|
224
|
+
+ "which lists every route. Pick another name for the alias.\n");
|
|
225
|
+
return 1;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// The store applies exactly these two rules when it reads the file back, so anything refused here
|
|
229
|
+
// would otherwise register, print a banner, and be dropped without a word at the next proxy boot.
|
|
230
|
+
if (!LocalgateMachineConfig.isLabel(name) || !LocalgateAliasStore.isPort(port))
|
|
231
|
+
{
|
|
232
|
+
process.stderr.write(`localgate: ${name} ${portText} cannot be an alias. The name must be a DNS `
|
|
233
|
+
+ "label (lowercase letters, digits, hyphens) and the port a number from 1 to 65535.\n");
|
|
158
234
|
return 1;
|
|
159
235
|
}
|
|
160
236
|
|
|
161
237
|
const machine = LocalgateMachineConfig.load();
|
|
162
|
-
|
|
238
|
+
|
|
239
|
+
if (port == LocalgateUrl.proxyPort(machine))
|
|
240
|
+
{
|
|
241
|
+
process.stderr.write(`localgate: port ${port} is localgate's own port, so the alias would only `
|
|
242
|
+
+ "send requests back to the proxy. Point it at the port the service listens on.\n");
|
|
243
|
+
return 1;
|
|
244
|
+
}
|
|
163
245
|
|
|
164
246
|
await LocalgateProxyClient.ensureRunning();
|
|
165
247
|
|
|
166
248
|
let route: LocalgateRoute;
|
|
167
249
|
try
|
|
168
250
|
{
|
|
169
|
-
route = await LocalgateProxyClient.register(
|
|
170
|
-
names,
|
|
171
|
-
port,
|
|
172
|
-
kind: "alias",
|
|
173
|
-
mode: machine ? "lan" : "local",
|
|
174
|
-
cwd: null,
|
|
175
|
-
command: null,
|
|
176
|
-
controlUrl: null,
|
|
177
|
-
runnerPid: null,
|
|
178
|
-
childPid: null,
|
|
179
|
-
debuggerAttached: false
|
|
180
|
-
});
|
|
251
|
+
route = await LocalgateProxyClient.register(LocalgateAliasRoute.registration(name, port, machine));
|
|
181
252
|
}
|
|
182
253
|
catch (error)
|
|
183
254
|
{
|
|
@@ -188,10 +259,69 @@ export class LocalgateCli
|
|
|
188
259
|
return 1;
|
|
189
260
|
}
|
|
190
261
|
|
|
262
|
+
// Only once the proxy took the name: a refused registration must not leave an entry that the next
|
|
263
|
+
// proxy boot would restore. A disk failure here is worth saying out loud rather than throwing over
|
|
264
|
+
// the banner, because the alias is already live and useful - it just will not come back.
|
|
265
|
+
try
|
|
266
|
+
{
|
|
267
|
+
LocalgateAliasStore.add({ name, port, stop });
|
|
268
|
+
}
|
|
269
|
+
catch (error)
|
|
270
|
+
{
|
|
271
|
+
process.stderr.write(`localgate: ${name} is live, but ${LocalgateAliasStore.filePath()} could not `
|
|
272
|
+
+ `be written, so it will not survive a proxy restart: ${String(error)}\n`);
|
|
273
|
+
}
|
|
274
|
+
|
|
191
275
|
process.stdout.write(LocalgateBanner.render(route, LocalgateProxyClient.proxyPort()));
|
|
192
276
|
return 0;
|
|
193
277
|
}
|
|
194
278
|
|
|
279
|
+
// Removal has to work with the proxy down, because an alias outliving the proxy is the whole point of
|
|
280
|
+
// persisting it. With nothing listening, dropping the file entry IS the removal: the only other reader
|
|
281
|
+
// of the file is the next proxy boot.
|
|
282
|
+
private static async aliasRemove(argument: string | undefined): Promise<number>
|
|
283
|
+
{
|
|
284
|
+
if (!argument)
|
|
285
|
+
{
|
|
286
|
+
process.stderr.write("localgate: usage is localgate alias --remove <name>\n");
|
|
287
|
+
return 1;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// The store knows an alias by the short name it was registered under, while a route answers to its
|
|
291
|
+
// full hostnames too - and those are the names a person reads in `localgate list`. Reduce once, so
|
|
292
|
+
// the route and the file cannot end up disagreeing about what was removed.
|
|
293
|
+
const name = LocalgateNames.shortName(argument);
|
|
294
|
+
const route = await LocalgateProxyClient.resolve({ name });
|
|
295
|
+
|
|
296
|
+
// An app that took an alias's name evicts the route but not the intent, so the entry has to go even
|
|
297
|
+
// here - otherwise the alias comes back at the next proxy boot and nothing can stop it.
|
|
298
|
+
if (route && route.kind != "alias")
|
|
299
|
+
{
|
|
300
|
+
const forgotten = LocalgateAliasStore.remove(name);
|
|
301
|
+
if (forgotten)
|
|
302
|
+
{
|
|
303
|
+
process.stdout.write(`localgate: ${name} is a running dev server, so its route stays. `
|
|
304
|
+
+ "The persisted alias of that name was removed.\n");
|
|
305
|
+
return 0;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
process.stderr.write(`localgate: ${name} is a running dev server, not an alias\n`);
|
|
309
|
+
return 1;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
if (route) await LocalgateProxyClient.deregister(route.id);
|
|
313
|
+
const dropped = LocalgateAliasStore.remove(name);
|
|
314
|
+
|
|
315
|
+
if (!route && !dropped)
|
|
316
|
+
{
|
|
317
|
+
process.stderr.write(`localgate: no alias named ${name}\n`);
|
|
318
|
+
return 1;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
process.stdout.write(`localgate: alias ${name} removed\n`);
|
|
322
|
+
return 0;
|
|
323
|
+
}
|
|
324
|
+
|
|
195
325
|
private static cloudflareInfo(): number
|
|
196
326
|
{
|
|
197
327
|
const machine = LocalgateMachineConfig.load();
|
|
@@ -273,26 +403,31 @@ export class LocalgateCli
|
|
|
273
403
|
|
|
274
404
|
private static routeUrls(route: LocalgateRoute, proxyPort: number): string[]
|
|
275
405
|
{
|
|
276
|
-
return
|
|
277
|
-
? `https://${name}`
|
|
278
|
-
: LocalgateUrl.forName(name, proxyPort));
|
|
406
|
+
return LocalgateUrl.routeAddresses(route, proxyPort).map(address => address.url);
|
|
279
407
|
}
|
|
280
408
|
|
|
281
409
|
private static printUsage(): void
|
|
282
410
|
{
|
|
283
411
|
process.stdout.write(
|
|
284
412
|
"localgate - stable names for local dev servers\n\n" +
|
|
285
|
-
" localgate run [--force] <cmd...> run a dev server behind its name\n" +
|
|
286
|
-
"
|
|
413
|
+
" localgate run [--force] <cmd...> run a dev server behind its name; one already\n" +
|
|
414
|
+
" running for the project is stopped first\n" +
|
|
415
|
+
" --force is needed for that without a terminal\n" +
|
|
287
416
|
" localgate list [--json] show active routes\n" +
|
|
288
417
|
" localgate status [app] [--json] state of one route (defaults to this directory)\n" +
|
|
289
418
|
" localgate restart [app] restart a route's dev server\n" +
|
|
290
419
|
" localgate stop [app] stop a route's dev server\n" +
|
|
291
420
|
" localgate logs [app] [--lines N] tail a route's captured output\n" +
|
|
292
|
-
" localgate alias <name> <port> register a static route (e.g. a docker service)\n" +
|
|
421
|
+
" localgate alias <name> <port> register a static route (e.g. a docker service); it is\n" +
|
|
422
|
+
" remembered and comes back when the proxy restarts\n" +
|
|
423
|
+
" [--stop \"<command>\"] what stops that service, e.g. \"docker stop myservice\";\n" +
|
|
424
|
+
" it gives the alias a stop button on the start page\n" +
|
|
425
|
+
" localgate alias --remove <name> drop a static route and forget it\n" +
|
|
426
|
+
" localgate config [dir] [--json] machine + project config and the names it implies\n" +
|
|
293
427
|
" localgate cloudflare-info print the DNS and ingress entries to paste\n" +
|
|
294
428
|
" localgate prune drop routes whose runner is gone\n\n" +
|
|
295
|
-
"[app] is optional: with no argument a command acts on the route owning the current directory.\n"
|
|
429
|
+
"[app] is optional: with no argument a command acts on the route owning the current directory.\n" +
|
|
430
|
+
`Every route, with its addresses, is also a page: ${LocalgateStartPage.localUrl(LocalgateProxyClient.proxyPort())}\n`
|
|
296
431
|
);
|
|
297
432
|
}
|
|
298
433
|
}
|
|
@@ -8,6 +8,8 @@ import type { LocalgateRoute, LocalgateRouteRegistration } from "../proxy/localg
|
|
|
8
8
|
|
|
9
9
|
// Every process other than the proxy reaches the route table through here. The table has no file, so
|
|
10
10
|
// this client is the only way in - and it is loopback-only, which is what keeps the LAN out of it.
|
|
11
|
+
// Alias intent is the one exception: `localgate alias --remove` edits ~/.localgate/aliases.json itself
|
|
12
|
+
// when the proxy is down, because there is nothing to ask.
|
|
11
13
|
export class LocalgateProxyClient
|
|
12
14
|
{
|
|
13
15
|
private static readonly startTimeoutMsConst = 15_000;
|
|
@@ -132,8 +134,14 @@ export class LocalgateProxyClient
|
|
|
132
134
|
throw new LocalgateRouteConflictError(payload.existing);
|
|
133
135
|
}
|
|
134
136
|
|
|
137
|
+
// The proxy refuses a registration with a sentence saying why - a reserved name, a port outside the
|
|
138
|
+
// dialable range - and a bare status code throws that sentence away in the one place a person reads
|
|
139
|
+
// it, which is the terminal the run was started from.
|
|
135
140
|
if (!response.ok)
|
|
136
|
-
|
|
141
|
+
{
|
|
142
|
+
const reason = await response.json().then(payload => (payload as { error?: string }).error).catch(() => undefined);
|
|
143
|
+
throw new Error(reason ?? `localgate proxy answered ${response.status} for ${method} ${path}`);
|
|
144
|
+
}
|
|
137
145
|
|
|
138
146
|
return response.json();
|
|
139
147
|
}
|