anon-pi 0.4.0 → 0.5.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/Dockerfile.pi +21 -9
- package/README.md +199 -64
- package/dist/anon-pi.d.ts +849 -75
- package/dist/anon-pi.d.ts.map +1 -1
- package/dist/anon-pi.js +1222 -260
- package/dist/anon-pi.js.map +1 -1
- package/dist/cli.js +1243 -112
- package/dist/cli.js.map +1 -1
- package/examples/Dockerfile.pi-webveil +9 -3
- package/package.json +1 -1
- package/src/anon-pi.ts +1723 -312
- package/src/cli.ts +1470 -124
package/src/cli.ts
CHANGED
|
@@ -1,227 +1,1564 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
// anon-pi CLI.
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
//
|
|
8
|
-
//
|
|
9
|
-
//
|
|
2
|
+
// anon-pi CLI: the THIN impure launch path. Parses grammar A (pure
|
|
3
|
+
// parseLaunchArgs), reads config.json / machine.json + resolves the machine,
|
|
4
|
+
// composes the LaunchIntent, resolves the RunPlan (pure resolveRunPlan), decides
|
|
5
|
+
// run-vs-start against real netcage for `--keep`, and spawns netcage with
|
|
6
|
+
// inherited stdio (so -it is a real interactive TTY), propagating the exit code.
|
|
7
|
+
//
|
|
8
|
+
// All the DECISIONS live in the pure module (anon-pi.ts); this file only does
|
|
9
|
+
// I/O: fs reads/mkdirs, the netcage query, the spawn, and the TTY discipline.
|
|
10
|
+
// The forced-egress invariant is the RunPlan's guarantee: the composed argv
|
|
11
|
+
// ALWAYS carries --proxy + the one --allow-direct; the CLI never strips or adds
|
|
12
|
+
// egress.
|
|
10
13
|
|
|
11
14
|
import {
|
|
12
15
|
existsSync,
|
|
13
16
|
mkdirSync,
|
|
17
|
+
readdirSync,
|
|
14
18
|
readFileSync,
|
|
15
19
|
rmSync,
|
|
16
20
|
writeFileSync,
|
|
17
21
|
} from 'node:fs';
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
22
|
+
import {readSync} from 'node:fs';
|
|
23
|
+
import {spawnSync, execFileSync} from 'node:child_process';
|
|
24
|
+
import {join, dirname} from 'node:path';
|
|
20
25
|
import {
|
|
21
26
|
AnonPiError,
|
|
22
|
-
buildRunPlan,
|
|
23
|
-
envFromProcess,
|
|
24
27
|
HELP,
|
|
25
28
|
MODELS_FILE,
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
SEED_MARKER,
|
|
30
|
+
DEFAULT_MACHINE,
|
|
31
|
+
envFromProcess,
|
|
32
|
+
buildMenuChoiceList,
|
|
33
|
+
buildMenuEntries,
|
|
34
|
+
deriveProjectUsage,
|
|
35
|
+
machineDir,
|
|
36
|
+
machineHomeDir,
|
|
37
|
+
machineJsonPath,
|
|
38
|
+
machineSessionsDir,
|
|
39
|
+
validateName,
|
|
40
|
+
resolveDeleteHome,
|
|
41
|
+
resolveDeleteProject,
|
|
42
|
+
parseConfigJson,
|
|
43
|
+
parseLaunchArgs,
|
|
44
|
+
parseMachineArgs,
|
|
45
|
+
parseMachineJson,
|
|
46
|
+
projectHostDir,
|
|
47
|
+
resolveAnonPiHome,
|
|
48
|
+
resolveLlm,
|
|
49
|
+
resolveProjectsRoot,
|
|
50
|
+
resolveProxy,
|
|
51
|
+
resolveRunPlan,
|
|
52
|
+
resolveRunVsStart,
|
|
53
|
+
serializeMachineJson,
|
|
54
|
+
serializeConfigJson,
|
|
55
|
+
setImageWarning,
|
|
56
|
+
keptContainerKey,
|
|
57
|
+
DEFAULT_SOCKS_PROBE_PORTS,
|
|
58
|
+
SOCKS5_METHOD_SELECTOR,
|
|
59
|
+
formatProxyFindings,
|
|
60
|
+
interpretSocks5Handshake,
|
|
61
|
+
initImageMenu,
|
|
62
|
+
generateModelsJson,
|
|
63
|
+
parseVerifyExitIp,
|
|
64
|
+
processHint,
|
|
65
|
+
socks5hUrl,
|
|
66
|
+
hostPortKey,
|
|
67
|
+
shippedDockerfilePath,
|
|
68
|
+
shippedWebveilDockerfilePath,
|
|
69
|
+
type MenuEntry,
|
|
70
|
+
type SessionDirListing,
|
|
71
|
+
type ProxyFinding,
|
|
72
|
+
type SocksHandshake,
|
|
73
|
+
type InitImageChoice,
|
|
74
|
+
type AnonPiConfig,
|
|
75
|
+
type AnonPiEnv,
|
|
76
|
+
type KeptContainer,
|
|
77
|
+
type LaunchIntent,
|
|
78
|
+
type Machine,
|
|
79
|
+
type MachineConfig,
|
|
80
|
+
type MachineCommand,
|
|
81
|
+
type ParsedLaunch,
|
|
31
82
|
} from './anon-pi.js';
|
|
32
83
|
|
|
84
|
+
// The netcage label anon-pi stamps its launch-identity key onto (keptContainerKey)
|
|
85
|
+
// so a `--keep` re-entry can find and `netcage start` the same kept container.
|
|
86
|
+
// netcage's `netcage.managed` label marks it a managed container; this adds the
|
|
87
|
+
// anon-pi identity ON TOP (netcage's label IS the registry; anon-pi adds no file).
|
|
88
|
+
const ANON_PI_KEY_LABEL = 'anon-pi.key';
|
|
89
|
+
|
|
33
90
|
function main(argv: string[]): number {
|
|
34
91
|
const args = argv.slice(2);
|
|
35
92
|
|
|
36
|
-
|
|
93
|
+
// The global `--help`/`-h` prints the top-level HELP, EXCEPT when the first
|
|
94
|
+
// token is a subcommand that owns its own `--help` (so `anon-pi init --help`
|
|
95
|
+
// and `anon-pi machine --help` show THEIR help, not the global one). Those
|
|
96
|
+
// subcommands route to runInit / runMachine, which print INIT_HELP /
|
|
97
|
+
// MACHINE_HELP respectively.
|
|
98
|
+
const OWN_HELP_SUBCOMMANDS = new Set(['init', 'machine']);
|
|
99
|
+
if (
|
|
100
|
+
(args.includes('--help') || args.includes('-h')) &&
|
|
101
|
+
!OWN_HELP_SUBCOMMANDS.has(args[0] ?? '')
|
|
102
|
+
) {
|
|
37
103
|
process.stdout.write(HELP);
|
|
38
104
|
return 0;
|
|
39
105
|
}
|
|
40
106
|
|
|
41
|
-
//
|
|
42
|
-
|
|
43
|
-
|
|
107
|
+
// `machine …` is the machine-management surface (create/list/set-image/rm),
|
|
108
|
+
// dispatched BEFORE the launch grammar so a bare `machine` is never parsed as
|
|
109
|
+
// a project named "machine". Everything else is a launch.
|
|
110
|
+
if (args[0] === 'machine') {
|
|
111
|
+
return runMachine(args.slice(1));
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// The destructive cleanup verbs (replacing the old `--fresh`). Dispatched
|
|
115
|
+
// BEFORE the launch grammar: they are top-level data verbs, not launch flags,
|
|
116
|
+
// each with the confirm/`--yes`/non-TTY discipline. `--delete-home` takes an
|
|
117
|
+
// OPTIONAL machine (default machine when omitted); `--delete-project` REQUIRES
|
|
118
|
+
// a project.
|
|
119
|
+
if (args[0] === '--delete-home') {
|
|
120
|
+
return runDeleteHome(args.slice(1));
|
|
121
|
+
}
|
|
122
|
+
if (args[0] === '--delete-project') {
|
|
123
|
+
return runDeleteProject(args.slice(1));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// `init` onboards: verify the proxy, capture the llm endpoint, pick/build the
|
|
127
|
+
// default machine image, write config.json + the default machine. Re-runnable.
|
|
128
|
+
if (args[0] === 'init') {
|
|
129
|
+
return runInit(args.slice(1));
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
let parsed: ParsedLaunch;
|
|
133
|
+
try {
|
|
134
|
+
parsed = parseLaunchArgs(args);
|
|
135
|
+
} catch (e) {
|
|
136
|
+
return reportAnonPiError(e);
|
|
44
137
|
}
|
|
45
138
|
|
|
46
|
-
return runLaunch(
|
|
139
|
+
return runLaunch(parsed);
|
|
47
140
|
}
|
|
48
141
|
|
|
49
|
-
// ---
|
|
50
|
-
function runLaunch(
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
//
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
142
|
+
// --- the launch path --------------------------------------------------------
|
|
143
|
+
function runLaunch(parsed: ParsedLaunch): number {
|
|
144
|
+
const env = envFromProcess(process.env);
|
|
145
|
+
|
|
146
|
+
// config.json (the workspace default proxy/llm/defaultMachine/projects).
|
|
147
|
+
const config = readJsonConfig(env);
|
|
148
|
+
|
|
149
|
+
// Resolve the machine: an explicit -m wins, else config.defaultMachine, else
|
|
150
|
+
// the built-in DEFAULT_MACHINE (so an explicit `-m default` is honoured too).
|
|
151
|
+
const machineName = parsed.machineExplicit
|
|
152
|
+
? parsed.machine
|
|
153
|
+
: (config.defaultMachine ?? DEFAULT_MACHINE);
|
|
154
|
+
|
|
155
|
+
const machineConf = readMachineJson(env, machineName);
|
|
156
|
+
|
|
157
|
+
// Forced-egress inputs, resolved (env over config); the proxy is REQUIRED and
|
|
158
|
+
// fails closed with the verbatim guidance.
|
|
159
|
+
let proxy: string;
|
|
160
|
+
let llm: string | undefined;
|
|
161
|
+
let intent: LaunchIntent;
|
|
162
|
+
try {
|
|
163
|
+
proxy = resolveProxy({env, config});
|
|
164
|
+
llm = resolveLlm({env, config});
|
|
165
|
+
if (llm === undefined) {
|
|
166
|
+
throw new AnonPiError(
|
|
167
|
+
'anon-pi: set ANON_PI_LLM (or config.llm) to the RFC1918/link-local IP[:port]\n' +
|
|
168
|
+
'of your local model. It is the ONE direct hole; all other egress stays\n' +
|
|
169
|
+
'forced through the proxy.',
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// The machine's image: machine.json wins, ANON_PI_IMAGE is the fallback.
|
|
174
|
+
const image = machineConf.image ?? env.image ?? '';
|
|
175
|
+
|
|
176
|
+
// --mount re-roots at a HOST parent; otherwise the resolved projects root.
|
|
177
|
+
const mountParent = parsed.mountParent;
|
|
178
|
+
const projectsRoot = resolveProjectsRoot({
|
|
179
|
+
env,
|
|
180
|
+
config,
|
|
181
|
+
machine: machineConf,
|
|
182
|
+
mountParent,
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
const home = machineHomeDir(env, machineName);
|
|
186
|
+
const machine: Machine = {name: machineName, home, image};
|
|
187
|
+
|
|
188
|
+
// The generated models.json for this machine (mounted read-only for the
|
|
189
|
+
// first-launch seed) when present. Keyed per machine, not per import.
|
|
190
|
+
const modelsSeed = join(machineDir(env, machineName), MODELS_FILE);
|
|
191
|
+
|
|
192
|
+
intent = {
|
|
193
|
+
machine,
|
|
194
|
+
mode: parsed.mode,
|
|
195
|
+
projectsRoot,
|
|
196
|
+
project: parsed.project,
|
|
197
|
+
mountParent,
|
|
198
|
+
piArgs: parsed.piArgs,
|
|
199
|
+
keep: parsed.keep,
|
|
200
|
+
proxy,
|
|
201
|
+
llmDirect: llm,
|
|
202
|
+
modelsSeed: existsSync(modelsSeed) ? modelsSeed : undefined,
|
|
203
|
+
};
|
|
204
|
+
} catch (e) {
|
|
205
|
+
return reportAnonPiError(e);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// No-TTY discipline: the bare MENU and every INTERACTIVE launch (interactive
|
|
209
|
+
// pi, or a shell) need a TTY; a HEADLESS pi run (`<project> <pi-args…>`) does
|
|
210
|
+
// NOT. Check BEFORE we mutate anything or spawn.
|
|
211
|
+
const headless =
|
|
212
|
+
parsed.mode === 'pi' && !!parsed.piArgs && parsed.piArgs.length > 0;
|
|
213
|
+
if (!headless && !process.stdin.isTTY) {
|
|
214
|
+
if (parsed.mode === 'menu') {
|
|
215
|
+
process.stderr.write(
|
|
216
|
+
'anon-pi: no TTY. The menu needs an interactive terminal. Pick a project\n' +
|
|
217
|
+
'directly, e.g. `anon-pi <project>`, or run anon-pi in a terminal.\n',
|
|
218
|
+
);
|
|
219
|
+
} else {
|
|
220
|
+
process.stderr.write(
|
|
221
|
+
`anon-pi: no TTY. An interactive ${parsed.mode === 'shell' ? 'shell' : 'pi session'} needs a terminal.\n` +
|
|
222
|
+
'Forward a one-shot pi prompt instead, e.g. `anon-pi <project> -p "..."`.\n',
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
return 1;
|
|
64
226
|
}
|
|
65
|
-
|
|
227
|
+
|
|
228
|
+
// Fail loud if netcage is not installed, before we mutate anything.
|
|
229
|
+
if (!hasNetcage()) {
|
|
66
230
|
process.stderr.write(
|
|
67
|
-
'anon-pi:
|
|
231
|
+
'anon-pi: `netcage` not found on PATH. anon-pi is a launcher for netcage; install it first\n' +
|
|
232
|
+
'(https://github.com/wighawag/netcage). Linux only.\n',
|
|
68
233
|
);
|
|
69
|
-
return
|
|
234
|
+
return 1;
|
|
70
235
|
}
|
|
71
236
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
237
|
+
// Resolve the RunPlan (pure). homeFresh reads the real seed marker.
|
|
238
|
+
let plan;
|
|
239
|
+
try {
|
|
240
|
+
plan = resolveRunPlan(intent, homeFresh);
|
|
241
|
+
} catch (e) {
|
|
242
|
+
return reportAnonPiError(e);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// Bare launch: hand off to the interactive host-side menu, which re-resolves
|
|
246
|
+
// the user's pick into a concrete launch and executes it.
|
|
247
|
+
if (plan.kind === 'menu') {
|
|
248
|
+
return runMenu(intent, plan.machine);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
return executeLaunchPlan(intent, plan);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Execute a RESOLVED non-menu LaunchPlan: create the host dirs the mounts need,
|
|
256
|
+
* then run netcage (or `netcage start` a matching kept container under --keep).
|
|
257
|
+
* Shared by the direct launch path (runLaunch) and the menu dispatch (runMenu),
|
|
258
|
+
* so a menu-picked project/here/shell launches BYTE-FOR-BYTE identically to the
|
|
259
|
+
* same command typed directly.
|
|
260
|
+
*/
|
|
261
|
+
function executeLaunchPlan(
|
|
262
|
+
intent: LaunchIntent,
|
|
263
|
+
plan: Extract<ReturnType<typeof resolveRunPlan>, {kind: 'launch'}>,
|
|
264
|
+
): number {
|
|
265
|
+
// Create the host dirs the mounts need BEFORE spawn: the machine home and,
|
|
266
|
+
// for a named project (not the root token `.` / a bare shell), its folder
|
|
267
|
+
// under the active root (the --mount parent or the projects root).
|
|
268
|
+
mkdirSync(plan.machine.home, {recursive: true});
|
|
269
|
+
if (
|
|
270
|
+
intent.project !== undefined &&
|
|
271
|
+
intent.project !== '.' &&
|
|
272
|
+
intent.mode !== 'shell'
|
|
273
|
+
) {
|
|
274
|
+
const root = intent.mountParent ?? intent.projectsRoot;
|
|
275
|
+
mkdirSync(projectHostDir(root, intent.project), {recursive: true});
|
|
276
|
+
} else {
|
|
277
|
+
// still ensure the active root exists (so a shell/`.`/menu-picked launch
|
|
278
|
+
// has a real dir to cwd into).
|
|
279
|
+
mkdirSync(intent.mountParent ?? intent.projectsRoot, {recursive: true});
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// Run-vs-start: under --keep, ask netcage for its kept managed containers and
|
|
283
|
+
// resume a matching one via `netcage start`; else run the composed argv. A
|
|
284
|
+
// throwaway (`--rm`) launch is always a fresh run (the pure rule never
|
|
285
|
+
// consults the listing for it).
|
|
286
|
+
if (intent.keep) {
|
|
287
|
+
const decision = resolveRunVsStart(intent, queryKeptContainers());
|
|
288
|
+
if (decision.action === 'start') {
|
|
289
|
+
return spawnNetcage(['start', '-a', '-i', decision.ref]);
|
|
290
|
+
}
|
|
291
|
+
// A fresh `--keep` run: stamp the identity key so a later re-entry can
|
|
292
|
+
// find this container. The RunPlan already omits --rm under --keep.
|
|
293
|
+
return spawnNetcage(
|
|
294
|
+
withKeyLabel(plan.netcageArgs, keptContainerKey(intent)),
|
|
75
295
|
);
|
|
76
|
-
return 2;
|
|
77
296
|
}
|
|
78
297
|
|
|
298
|
+
return spawnNetcage(plan.netcageArgs);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// --- the interactive host-side menu (the ONLY untested I/O) -------------------
|
|
302
|
+
//
|
|
303
|
+
// Bare `anon-pi` (and bare `-m <machine>` / `--mount <parent>` with no project)
|
|
304
|
+
// dispatches here. The menu is a PURE host-side read: it lists the active root's
|
|
305
|
+
// projects (readdir) + each machine's pi session dirs (readdir) and feeds them
|
|
306
|
+
// to the pure buildMenuChoiceList / deriveProjectUsage / buildMenuEntries, which
|
|
307
|
+
// own ALL the logic (the entry order + the used-on / new-here annotation). This
|
|
308
|
+
// function does ONLY the I/O the pure seam cannot: the real dir reads, the raw-
|
|
309
|
+
// mode arrow-key render/select (select()), the new-project name prompt, and the
|
|
310
|
+
// dispatch of the pick back through resolveRunPlan + executeLaunchPlan (so a
|
|
311
|
+
// menu pick launches identically to the same command typed directly). No jail
|
|
312
|
+
// runs until the user chooses; the no-TTY case is handled BEFORE we reach here
|
|
313
|
+
// (runLaunch's discipline), so a TTY is guaranteed.
|
|
314
|
+
function runMenu(intent: LaunchIntent, machine: Machine): number {
|
|
79
315
|
const env = envFromProcess(process.env);
|
|
80
|
-
if (ephemeralFlag) env.ephemeral = true;
|
|
81
316
|
|
|
82
|
-
//
|
|
83
|
-
//
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
317
|
+
// The active root the menu lists projects from: a --mount parent re-roots, else
|
|
318
|
+
// the resolved projects root. (Named projects live under it; `.` is the root.)
|
|
319
|
+
const root = intent.mountParent ?? intent.projectsRoot;
|
|
320
|
+
const rawProjects = readDirNames(root);
|
|
321
|
+
const choiceList = buildMenuChoiceList({projects: rawProjects});
|
|
322
|
+
|
|
323
|
+
// Per-machine usage: the session-slug set present in each machine home's pi
|
|
324
|
+
// sessions dir, machine-invariant, so a shared project is credited on each.
|
|
325
|
+
const sessions: SessionDirListing = {};
|
|
326
|
+
for (const name of listMachineNames(env)) {
|
|
327
|
+
sessions[name] = readDirNames(machineSessionsDir(env, name));
|
|
328
|
+
}
|
|
329
|
+
// The current machine may be brand-new (no sessions dir yet); an absent entry
|
|
330
|
+
// reads as "new for it" in deriveProjectUsage.
|
|
331
|
+
if (sessions[machine.name] === undefined) sessions[machine.name] = [];
|
|
332
|
+
const usage = deriveProjectUsage({
|
|
333
|
+
projects: choiceList.projects,
|
|
334
|
+
currentMachine: machine.name,
|
|
335
|
+
sessions,
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
const entries = buildMenuEntries({choiceList, usage});
|
|
339
|
+
|
|
340
|
+
const picked = select(entries, {
|
|
341
|
+
header: `anon-pi: machine "${machine.name}" (\u2191/\u2193 move, Enter select, Ctrl-C quit)`,
|
|
342
|
+
});
|
|
343
|
+
if (picked === undefined) {
|
|
344
|
+
// Ctrl-C / EOF: a clean quit, nothing launched (the terminal is restored).
|
|
345
|
+
process.stderr.write('anon-pi: cancelled; nothing launched.\n');
|
|
346
|
+
return 130; // 128 + SIGINT, the conventional Ctrl-C exit code.
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// Turn the pick into a concrete launch intent, then re-resolve + execute it
|
|
350
|
+
// EXACTLY as the equivalent direct command would (same resolveRunPlan path).
|
|
351
|
+
let launchIntent: LaunchIntent;
|
|
352
|
+
switch (picked.kind) {
|
|
353
|
+
case 'project':
|
|
354
|
+
case 'here':
|
|
355
|
+
launchIntent = {...intent, mode: 'pi', project: picked.project};
|
|
356
|
+
break;
|
|
357
|
+
case 'shell':
|
|
358
|
+
launchIntent = {...intent, mode: 'shell', project: undefined};
|
|
359
|
+
break;
|
|
360
|
+
case 'new': {
|
|
361
|
+
const name = promptNewProject();
|
|
362
|
+
if (name === undefined) return 1;
|
|
363
|
+
launchIntent = {...intent, mode: 'pi', project: name};
|
|
364
|
+
break;
|
|
95
365
|
}
|
|
96
366
|
}
|
|
97
367
|
|
|
98
368
|
let plan;
|
|
99
369
|
try {
|
|
100
|
-
plan =
|
|
370
|
+
plan = resolveRunPlan(launchIntent, homeFresh);
|
|
101
371
|
} catch (e) {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
372
|
+
return reportAnonPiError(e);
|
|
373
|
+
}
|
|
374
|
+
// A menu pick is always a concrete launch (never the menu marker again).
|
|
375
|
+
if (plan.kind !== 'launch') {
|
|
376
|
+
process.stderr.write('anon-pi: internal error resolving the menu pick.\n');
|
|
377
|
+
return 1;
|
|
378
|
+
}
|
|
379
|
+
return executeLaunchPlan(launchIntent, plan);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Prompt for a NEW project name and validate it (validateName, the same guard a
|
|
384
|
+
* direct `anon-pi <name>` uses), so a menu-created project is a safe single
|
|
385
|
+
* folder segment. Returns the validated name, or undefined on an empty entry /
|
|
386
|
+
* EOF / a rejected name (with the error printed). TTY is guaranteed here.
|
|
387
|
+
*/
|
|
388
|
+
function promptNewProject(): string | undefined {
|
|
389
|
+
const ans = promptLine('New project name (a single folder segment): ');
|
|
390
|
+
if (ans === undefined || ans.trim() === '') {
|
|
391
|
+
process.stderr.write('anon-pi: no name given; nothing launched.\n');
|
|
392
|
+
return undefined;
|
|
393
|
+
}
|
|
394
|
+
try {
|
|
395
|
+
return validateName(ans.trim(), 'project');
|
|
396
|
+
} catch (e) {
|
|
397
|
+
reportAnonPiError(e);
|
|
398
|
+
return undefined;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Read the entry NAMES of a directory (best-effort): the plain names of its
|
|
404
|
+
* direct children, or [] if the dir is absent / unreadable. Used for both the
|
|
405
|
+
* projects-root listing (pure buildMenuChoiceList filters it to folder-safe
|
|
406
|
+
* project names) and each machine's sessions dir (the session slugs present).
|
|
407
|
+
*/
|
|
408
|
+
function readDirNames(dir: string): string[] {
|
|
409
|
+
if (!existsSync(dir)) return [];
|
|
410
|
+
try {
|
|
411
|
+
return readdirSync(dir, {withFileTypes: true}).map((d) => d.name);
|
|
412
|
+
} catch {
|
|
413
|
+
return [];
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
// --- the hand-rolled, zero-dependency raw-mode selector ----------------------
|
|
418
|
+
//
|
|
419
|
+
// A small supply-chain surface is on-brand for a security tool and the project
|
|
420
|
+
// list is short, so instead of a prompt library we drive stdin in raw mode
|
|
421
|
+
// ourselves: up/down (arrows or k/j) move a `>` cursor, Enter selects, Ctrl-C /
|
|
422
|
+
// q / Esc cancels. The active row is highlighted (reverse video). The terminal
|
|
423
|
+
// is ALWAYS restored (raw mode off, cursor shown) on every exit path, including
|
|
424
|
+
// Ctrl-C. Isolated here behind a tiny signature so a well-regarded prompt lib
|
|
425
|
+
// could swap in later as a localized change. This is the ONLY untested I/O in
|
|
426
|
+
// the menu; all logic (entries + labels) is the pure buildMenuEntries.
|
|
427
|
+
|
|
428
|
+
const ESC = '\u001b';
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* Present `entries` as an arrow-key list and return the chosen one, or undefined
|
|
432
|
+
* on cancel (Ctrl-C / q / Esc / EOF). Blocks on raw stdin; restores the terminal
|
|
433
|
+
* on every path. An empty entry list returns undefined immediately (nothing to
|
|
434
|
+
* pick), though the menu always offers at least the `.` here entry.
|
|
435
|
+
*/
|
|
436
|
+
function select(
|
|
437
|
+
entries: readonly MenuEntry[],
|
|
438
|
+
opts: {header?: string} = {},
|
|
439
|
+
): MenuEntry | undefined {
|
|
440
|
+
if (entries.length === 0) return undefined;
|
|
441
|
+
const out = process.stdout;
|
|
442
|
+
const stdin = process.stdin;
|
|
443
|
+
let active = 0;
|
|
444
|
+
|
|
445
|
+
const render = (first: boolean): void => {
|
|
446
|
+
if (!first) {
|
|
447
|
+
// move cursor up over the previously drawn rows to redraw in place.
|
|
448
|
+
out.write(`${ESC}[${entries.length}A`);
|
|
449
|
+
}
|
|
450
|
+
for (let i = 0; i < entries.length; i++) {
|
|
451
|
+
const selected = i === active;
|
|
452
|
+
const cursor = selected ? '>' : ' ';
|
|
453
|
+
const text = `${cursor} ${entries[i].label}`;
|
|
454
|
+
// clear the line, then draw; reverse-video the active row.
|
|
455
|
+
out.write(`${ESC}[2K`);
|
|
456
|
+
out.write(selected ? `${ESC}[7m${text}${ESC}[0m` : text);
|
|
457
|
+
out.write('\n');
|
|
105
458
|
}
|
|
106
|
-
|
|
459
|
+
};
|
|
460
|
+
|
|
461
|
+
const wasRaw = stdin.isRaw ?? false;
|
|
462
|
+
const restore = (): void => {
|
|
463
|
+
try {
|
|
464
|
+
if (stdin.setRawMode) stdin.setRawMode(wasRaw);
|
|
465
|
+
} catch {
|
|
466
|
+
/* best-effort */
|
|
467
|
+
}
|
|
468
|
+
out.write(`${ESC}[?25h`); // show the cursor again
|
|
469
|
+
};
|
|
470
|
+
|
|
471
|
+
if (opts.header) out.write(opts.header + '\n');
|
|
472
|
+
out.write(`${ESC}[?25l`); // hide the cursor while navigating
|
|
473
|
+
try {
|
|
474
|
+
if (stdin.setRawMode) stdin.setRawMode(true);
|
|
475
|
+
} catch {
|
|
476
|
+
/* if raw mode is unavailable we still render + read line-ish below */
|
|
107
477
|
}
|
|
478
|
+
render(true);
|
|
108
479
|
|
|
109
|
-
|
|
110
|
-
|
|
480
|
+
const buf = Buffer.alloc(3);
|
|
481
|
+
for (;;) {
|
|
482
|
+
let n: number;
|
|
483
|
+
try {
|
|
484
|
+
n = readSync(0, buf, 0, 3, null);
|
|
485
|
+
} catch (e) {
|
|
486
|
+
if ((e as NodeJS.ErrnoException).code === 'EAGAIN') continue;
|
|
487
|
+
restore();
|
|
488
|
+
return undefined;
|
|
489
|
+
}
|
|
490
|
+
if (n === 0) {
|
|
491
|
+
restore();
|
|
492
|
+
return undefined; // EOF
|
|
493
|
+
}
|
|
494
|
+
const s = buf.toString('utf8', 0, n);
|
|
495
|
+
// Ctrl-C (ETX \x03), q, or a bare Esc: cancel.
|
|
496
|
+
if (s === '\u0003' || s === 'q' || s === ESC) {
|
|
497
|
+
restore();
|
|
498
|
+
return undefined;
|
|
499
|
+
}
|
|
500
|
+
// Enter (CR or LF): select the active row.
|
|
501
|
+
if (s === '\r' || s === '\n') {
|
|
502
|
+
restore();
|
|
503
|
+
return entries[active];
|
|
504
|
+
}
|
|
505
|
+
// Up: arrow `Esc [ A` / `Esc O A`, or k. Down: `Esc [ B` / `Esc O B`, or j.
|
|
506
|
+
if (s === `${ESC}[A` || s === `${ESC}OA` || s === 'k') {
|
|
507
|
+
active = (active - 1 + entries.length) % entries.length;
|
|
508
|
+
render(false);
|
|
509
|
+
continue;
|
|
510
|
+
}
|
|
511
|
+
if (s === `${ESC}[B` || s === `${ESC}OB` || s === 'j') {
|
|
512
|
+
active = (active + 1) % entries.length;
|
|
513
|
+
render(false);
|
|
514
|
+
continue;
|
|
515
|
+
}
|
|
516
|
+
// any other key: ignore, keep waiting.
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
// --- the `machine` verbs (thin dispatch over the pure parts) -----------------
|
|
521
|
+
//
|
|
522
|
+
// Parse the `machine <verb> …` grammar (pure parseMachineArgs), then do only the
|
|
523
|
+
// I/O: mkdir/write the machine layout (create), read machines/*/machine.json
|
|
524
|
+
// (list), rewrite machine.json + WARN (set-image), and rm the machine dir with
|
|
525
|
+
// the confirm/`--yes`/non-TTY discipline (rm). All validation + the machine.json
|
|
526
|
+
// body + the warning wording live in the pure module.
|
|
527
|
+
function runMachine(machineArgs: string[]): number {
|
|
528
|
+
if (machineArgs.includes('--help') || machineArgs.includes('-h')) {
|
|
529
|
+
process.stdout.write(MACHINE_HELP);
|
|
530
|
+
return 0;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
const env = envFromProcess(process.env);
|
|
534
|
+
let cmd: MachineCommand;
|
|
535
|
+
try {
|
|
536
|
+
cmd = parseMachineArgs(machineArgs);
|
|
537
|
+
} catch (e) {
|
|
538
|
+
return reportAnonPiError(e);
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
try {
|
|
542
|
+
switch (cmd.verb) {
|
|
543
|
+
case 'create':
|
|
544
|
+
return machineCreate(env, cmd.name, cmd.image);
|
|
545
|
+
case 'list':
|
|
546
|
+
return machineList(env);
|
|
547
|
+
case 'set-image':
|
|
548
|
+
return machineSetImage(env, cmd.name, cmd.image);
|
|
549
|
+
case 'rm':
|
|
550
|
+
return machineRm(env, cmd.name, cmd.yes);
|
|
551
|
+
}
|
|
552
|
+
} catch (e) {
|
|
553
|
+
return reportAnonPiError(e);
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* `machine create <name> [--image <ref>]`: write machines/<name>/{machine.json,
|
|
559
|
+
* home/} and PIN the image (from --image or a TTY prompt). The home is only a
|
|
560
|
+
* dir here; it is SEEDED on first LAUNCH, not now. Refuses to clobber an
|
|
561
|
+
* existing machine.
|
|
562
|
+
*/
|
|
563
|
+
function machineCreate(
|
|
564
|
+
env: AnonPiEnv,
|
|
565
|
+
name: string,
|
|
566
|
+
image: string | undefined,
|
|
567
|
+
): number {
|
|
568
|
+
const dir = machineDir(env, name);
|
|
569
|
+
if (existsSync(dir)) {
|
|
111
570
|
process.stderr.write(
|
|
112
|
-
|
|
113
|
-
'
|
|
571
|
+
`anon-pi: machine ${JSON.stringify(name)} already exists (${dir}). ` +
|
|
572
|
+
'Use `anon-pi machine set-image` to re-pin its image, or `anon-pi machine rm` first.\n',
|
|
114
573
|
);
|
|
115
574
|
return 1;
|
|
116
575
|
}
|
|
117
576
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
process.
|
|
123
|
-
'anon-pi: ephemeral session (nothing persisted; no host state)\n',
|
|
124
|
-
);
|
|
125
|
-
} else {
|
|
126
|
-
// Persistent mode: create the per-workdir state home to mount.
|
|
127
|
-
mkdirSync(plan.stateDir, {recursive: true});
|
|
128
|
-
if (plan.fresh) {
|
|
577
|
+
// Pin the image: --image wins; else prompt on a TTY; else it is an error (a
|
|
578
|
+
// machine with no image cannot launch, so we refuse a headless imageless create).
|
|
579
|
+
let pinned = image;
|
|
580
|
+
if (pinned === undefined) {
|
|
581
|
+
if (!process.stdin.isTTY) {
|
|
129
582
|
process.stderr.write(
|
|
130
|
-
|
|
583
|
+
'anon-pi: no image and no TTY to prompt. Pass `--image <ref>` to pin the ' +
|
|
584
|
+
"machine's image (a container ref with `pi` on PATH).\n",
|
|
131
585
|
);
|
|
586
|
+
return 1;
|
|
587
|
+
}
|
|
588
|
+
pinned = promptLine(
|
|
589
|
+
`Image ref for machine ${JSON.stringify(name)} (a container with \`pi\` on PATH): `,
|
|
590
|
+
);
|
|
591
|
+
if (pinned === undefined || pinned.trim() === '') {
|
|
592
|
+
process.stderr.write('anon-pi: no image given; aborting create.\n');
|
|
593
|
+
return 1;
|
|
132
594
|
}
|
|
133
595
|
}
|
|
134
596
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
597
|
+
mkdirSync(machineHomeDir(env, name), {recursive: true});
|
|
598
|
+
writeFileSync(
|
|
599
|
+
machineJsonPath(env, name),
|
|
600
|
+
serializeMachineJson({image: pinned}),
|
|
601
|
+
);
|
|
602
|
+
process.stdout.write(
|
|
603
|
+
`anon-pi: created machine ${JSON.stringify(name)} (image ${pinned.trim()}) at ${dir}.\n` +
|
|
604
|
+
`Its home is seeded on first launch, e.g. \`anon-pi -m ${name} --shell\`.\n`,
|
|
605
|
+
);
|
|
606
|
+
return 0;
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
/**
|
|
610
|
+
* `machine list`: print each machine under machines/ with its pinned image
|
|
611
|
+
* (reading each machine's machine.json). An absent/garbage machine.json shows
|
|
612
|
+
* `(no image)` rather than erroring, so a hand-edited workspace still lists.
|
|
613
|
+
*/
|
|
614
|
+
function machineList(env: AnonPiEnv): number {
|
|
615
|
+
const root = join(resolveAnonPiHome(env), 'machines');
|
|
616
|
+
const names = existsSync(root)
|
|
617
|
+
? readdirSync(root, {withFileTypes: true})
|
|
618
|
+
.filter((d) => d.isDirectory())
|
|
619
|
+
.map((d) => d.name)
|
|
620
|
+
.sort((a, b) => (a < b ? -1 : a > b ? 1 : 0))
|
|
621
|
+
: [];
|
|
622
|
+
if (names.length === 0) {
|
|
623
|
+
process.stdout.write(
|
|
624
|
+
'anon-pi: no machines yet. Create one with `anon-pi machine create <name> --image <ref>`.\n',
|
|
625
|
+
);
|
|
626
|
+
return 0;
|
|
627
|
+
}
|
|
628
|
+
for (const name of names) {
|
|
629
|
+
const conf = readMachineJson(env, name);
|
|
630
|
+
const image = conf.image ?? '(no image)';
|
|
631
|
+
process.stdout.write(`${name}\t${image}\n`);
|
|
632
|
+
}
|
|
633
|
+
return 0;
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
/**
|
|
637
|
+
* `machine set-image <name> <ref>`: RE-PIN the image and WARN only. It does NOT
|
|
638
|
+
* reseed or touch the home (the home's extensions/bin were built for the OLD
|
|
639
|
+
* image). Preserves any per-machine projects override. The machine must exist.
|
|
640
|
+
*/
|
|
641
|
+
function machineSetImage(env: AnonPiEnv, name: string, image: string): number {
|
|
642
|
+
const dir = machineDir(env, name);
|
|
643
|
+
if (!existsSync(dir)) {
|
|
138
644
|
process.stderr.write(
|
|
139
|
-
`anon-pi:
|
|
645
|
+
`anon-pi: no machine ${JSON.stringify(name)} (${dir}). ` +
|
|
646
|
+
'Create it first with `anon-pi machine create`.\n',
|
|
140
647
|
);
|
|
141
648
|
return 1;
|
|
142
649
|
}
|
|
143
|
-
|
|
144
|
-
|
|
650
|
+
const prev = readMachineJson(env, name);
|
|
651
|
+
writeFileSync(
|
|
652
|
+
machineJsonPath(env, name),
|
|
653
|
+
serializeMachineJson({image, projects: prev.projects}),
|
|
654
|
+
);
|
|
655
|
+
process.stderr.write(setImageWarning(name, prev.image, image.trim()) + '\n');
|
|
656
|
+
return 0;
|
|
145
657
|
}
|
|
146
658
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
659
|
+
/**
|
|
660
|
+
* `machine rm <name> [--yes]`: delete the machine dir (its machine.json + home)
|
|
661
|
+
* after a confirm. Mirrors the destructive data-verb discipline: confirm on a
|
|
662
|
+
* TTY, `--yes` skips it, and a non-TTY WITHOUT `--yes` ABORTS (never deletes
|
|
663
|
+
* unprompted in a script). The machine must exist.
|
|
664
|
+
*/
|
|
665
|
+
function machineRm(env: AnonPiEnv, name: string, yes: boolean): number {
|
|
666
|
+
const dir = machineDir(env, name);
|
|
667
|
+
if (!existsSync(dir)) {
|
|
668
|
+
process.stderr.write(
|
|
669
|
+
`anon-pi: no machine ${JSON.stringify(name)} (${dir}); nothing to remove.\n`,
|
|
670
|
+
);
|
|
671
|
+
return 1;
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
if (!yes) {
|
|
675
|
+
if (!process.stdin.isTTY) {
|
|
676
|
+
process.stderr.write(
|
|
677
|
+
`anon-pi: refusing to delete machine ${JSON.stringify(name)} without a TTY to confirm. ` +
|
|
678
|
+
'Re-run with `--yes` to delete it (its home + conversations) non-interactively.\n',
|
|
679
|
+
);
|
|
680
|
+
return 1;
|
|
681
|
+
}
|
|
682
|
+
const answer = promptLine(
|
|
683
|
+
`Delete machine ${JSON.stringify(name)} and its home (conversations, config) at ${dir}? [y/N] `,
|
|
684
|
+
);
|
|
685
|
+
if (answer === undefined || !/^y(es)?$/i.test(answer.trim())) {
|
|
686
|
+
process.stderr.write('anon-pi: aborted; nothing deleted.\n');
|
|
687
|
+
return 1;
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
rmSync(dir, {recursive: true, force: true});
|
|
692
|
+
process.stdout.write(
|
|
693
|
+
`anon-pi: removed machine ${JSON.stringify(name)} (${dir}).\n`,
|
|
152
694
|
);
|
|
153
|
-
|
|
695
|
+
return 0;
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
// --- the destructive cleanup verbs (thin I/O over the pure resolvers) --------
|
|
699
|
+
//
|
|
700
|
+
// `--delete-home [<machine>]` and `--delete-project <project>` REPLACE the old
|
|
701
|
+
// `--fresh`. The pure module resolves the affected host paths (resolveDeleteHome
|
|
702
|
+
// / resolveDeleteProject); the CLI does ONLY the I/O: read config (for the
|
|
703
|
+
// default machine + the projects root), filter the resolved paths to those that
|
|
704
|
+
// exist, run the shared confirm/`--yes`/non-TTY discipline, then `rm`.
|
|
705
|
+
|
|
706
|
+
/**
|
|
707
|
+
* Parse the shared `[<positional>] [--yes|-y]` tail of a data verb. Returns the
|
|
708
|
+
* (optional) positional (a machine or project name) + the `--yes` flag, or an
|
|
709
|
+
* AnonPiError-style exit for an unknown flag / an extra positional.
|
|
710
|
+
*/
|
|
711
|
+
function parseDeleteArgs(
|
|
712
|
+
args: string[],
|
|
713
|
+
verb: string,
|
|
714
|
+
): {name?: string; yes: boolean} | number {
|
|
715
|
+
let name: string | undefined;
|
|
716
|
+
let yes = false;
|
|
717
|
+
for (const a of args) {
|
|
718
|
+
if (a === '--yes' || a === '-y') {
|
|
719
|
+
yes = true;
|
|
720
|
+
continue;
|
|
721
|
+
}
|
|
722
|
+
if (a.startsWith('-')) {
|
|
723
|
+
process.stderr.write(
|
|
724
|
+
`anon-pi: unknown option for ${verb}: ${a}. Run \`anon-pi --help\`.\n`,
|
|
725
|
+
);
|
|
726
|
+
return 1;
|
|
727
|
+
}
|
|
728
|
+
if (name !== undefined) {
|
|
729
|
+
process.stderr.write(
|
|
730
|
+
`anon-pi: ${verb} takes one name, got extra: ${a}. Run \`anon-pi --help\`.\n`,
|
|
731
|
+
);
|
|
732
|
+
return 1;
|
|
733
|
+
}
|
|
734
|
+
name = a;
|
|
735
|
+
}
|
|
736
|
+
return {name, yes};
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
/**
|
|
740
|
+
* Run the confirm/`--yes`/non-TTY discipline for a destructive delete: `--yes`
|
|
741
|
+
* skips the prompt; a non-TTY WITHOUT `--yes` ABORTS (never deletes unprompted
|
|
742
|
+
* in a script); a TTY prompts `[y/N]`. Returns true to PROCEED, false to abort
|
|
743
|
+
* (the caller has already printed nothing; this prints the abort/refusal note).
|
|
744
|
+
*/
|
|
745
|
+
function confirmDelete(what: string, yes: boolean): boolean {
|
|
746
|
+
if (yes) return true;
|
|
747
|
+
if (!process.stdin.isTTY) {
|
|
154
748
|
process.stderr.write(
|
|
155
|
-
`anon-pi
|
|
749
|
+
`anon-pi: refusing to delete ${what} without a TTY to confirm. ` +
|
|
750
|
+
'Re-run with `--yes` to delete it non-interactively.\n',
|
|
156
751
|
);
|
|
157
|
-
return
|
|
752
|
+
return false;
|
|
158
753
|
}
|
|
754
|
+
const answer = promptLine(`Delete ${what}? [y/N] `);
|
|
755
|
+
if (answer === undefined || !/^y(es)?$/i.test(answer.trim())) {
|
|
756
|
+
process.stderr.write('anon-pi: aborted; nothing deleted.\n');
|
|
757
|
+
return false;
|
|
758
|
+
}
|
|
759
|
+
return true;
|
|
760
|
+
}
|
|
159
761
|
|
|
762
|
+
/**
|
|
763
|
+
* `--delete-home [<machine>]`: delete ONE machine's HOME (config + convos + shell
|
|
764
|
+
* env), keeping its machine.json image pin (so it can be relaunched to reseed a
|
|
765
|
+
* fresh home) and ALL project files (they live under the projects root). Default
|
|
766
|
+
* machine (config.defaultMachine, else the built-in DEFAULT_MACHINE) when the
|
|
767
|
+
* name is omitted. Confirm / `--yes` / non-TTY abort.
|
|
768
|
+
*/
|
|
769
|
+
function runDeleteHome(args: string[]): number {
|
|
770
|
+
if (args.includes('--help') || args.includes('-h')) {
|
|
771
|
+
process.stdout.write(HELP);
|
|
772
|
+
return 0;
|
|
773
|
+
}
|
|
160
774
|
const env = envFromProcess(process.env);
|
|
775
|
+
const parsed = parseDeleteArgs(args, '--delete-home');
|
|
776
|
+
if (typeof parsed === 'number') return parsed;
|
|
777
|
+
|
|
778
|
+
const config = readJsonConfig(env);
|
|
779
|
+
const machine = parsed.name ?? config.defaultMachine ?? DEFAULT_MACHINE;
|
|
161
780
|
|
|
162
|
-
|
|
781
|
+
let plan;
|
|
782
|
+
try {
|
|
783
|
+
plan = resolveDeleteHome(env, machine);
|
|
784
|
+
} catch (e) {
|
|
785
|
+
return reportAnonPiError(e);
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
if (!existsSync(plan.home)) {
|
|
163
789
|
process.stderr.write(
|
|
164
|
-
|
|
165
|
-
'model whose provider should be imported (e.g. ANON_PI_LLM=192.168.1.150:8080).\n',
|
|
790
|
+
`anon-pi: no home for machine ${JSON.stringify(plan.machine)} (${plan.home}); nothing to delete.\n`,
|
|
166
791
|
);
|
|
167
792
|
return 1;
|
|
168
793
|
}
|
|
169
794
|
|
|
170
|
-
|
|
171
|
-
|
|
795
|
+
if (
|
|
796
|
+
!confirmDelete(
|
|
797
|
+
`machine ${JSON.stringify(plan.machine)} home (conversations + config) at ${plan.home}`,
|
|
798
|
+
parsed.yes,
|
|
799
|
+
)
|
|
800
|
+
) {
|
|
801
|
+
return 1;
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
rmSync(plan.home, {recursive: true, force: true});
|
|
805
|
+
process.stdout.write(
|
|
806
|
+
`anon-pi: deleted machine ${JSON.stringify(plan.machine)} home (${plan.home}). ` +
|
|
807
|
+
'Its image pin is kept; relaunch to seed a fresh home.\n',
|
|
808
|
+
);
|
|
809
|
+
return 0;
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
/**
|
|
813
|
+
* `--delete-project <project>`: delete the project's FILES (its folder under the
|
|
814
|
+
* resolved projects root) AND that project's per-machine session dir in EVERY
|
|
815
|
+
* machine home (the machine-invariant slug), keeping the homes otherwise intact.
|
|
816
|
+
* Confirm / `--yes` / non-TTY abort. The project name is REQUIRED.
|
|
817
|
+
*/
|
|
818
|
+
function runDeleteProject(args: string[]): number {
|
|
819
|
+
if (args.includes('--help') || args.includes('-h')) {
|
|
820
|
+
process.stdout.write(HELP);
|
|
821
|
+
return 0;
|
|
822
|
+
}
|
|
823
|
+
const env = envFromProcess(process.env);
|
|
824
|
+
const parsed = parseDeleteArgs(args, '--delete-project');
|
|
825
|
+
if (typeof parsed === 'number') return parsed;
|
|
826
|
+
if (parsed.name === undefined) {
|
|
172
827
|
process.stderr.write(
|
|
173
|
-
|
|
174
|
-
'Set ANON_PI_SOURCE_MODELS to your pi models.json, or run pi once to create it.\n',
|
|
828
|
+
'anon-pi: --delete-project needs a <project>. Run `anon-pi --help`.\n',
|
|
175
829
|
);
|
|
176
830
|
return 1;
|
|
177
831
|
}
|
|
178
832
|
|
|
179
|
-
|
|
833
|
+
const config = readJsonConfig(env);
|
|
834
|
+
// The RESOLVED projects root (config/env override, else the built-in). No
|
|
835
|
+
// --mount here: a data verb targets the durable projects root, not a per-run
|
|
836
|
+
// host parent.
|
|
837
|
+
const projectsRoot = resolveProjectsRoot({env, config});
|
|
838
|
+
const machines = listMachineNames(env);
|
|
839
|
+
|
|
840
|
+
let plan;
|
|
180
841
|
try {
|
|
181
|
-
|
|
842
|
+
plan = resolveDeleteProject({
|
|
843
|
+
env,
|
|
844
|
+
project: parsed.name,
|
|
845
|
+
projectsRoot,
|
|
846
|
+
machines,
|
|
847
|
+
});
|
|
182
848
|
} catch (e) {
|
|
849
|
+
return reportAnonPiError(e);
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
// Only the paths that actually exist: the folder (maybe absent) + whichever
|
|
853
|
+
// machine homes hold this project's session dir.
|
|
854
|
+
const targets = [plan.folder, ...plan.sessions].filter((p) => existsSync(p));
|
|
855
|
+
if (targets.length === 0) {
|
|
183
856
|
process.stderr.write(
|
|
184
|
-
`anon-pi
|
|
857
|
+
`anon-pi: no files or sessions found for project ${JSON.stringify(plan.project)} ` +
|
|
858
|
+
`(looked in ${plan.folder} and each machine home); nothing to delete.\n`,
|
|
185
859
|
);
|
|
186
860
|
return 1;
|
|
187
861
|
}
|
|
188
862
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
863
|
+
const sessionCount = targets.length - (existsSync(plan.folder) ? 1 : 0);
|
|
864
|
+
if (
|
|
865
|
+
!confirmDelete(
|
|
866
|
+
`project ${JSON.stringify(plan.project)}: its files (${plan.folder}) ` +
|
|
867
|
+
`and ${sessionCount} per-machine session dir(s)`,
|
|
868
|
+
parsed.yes,
|
|
869
|
+
)
|
|
870
|
+
) {
|
|
871
|
+
return 1;
|
|
198
872
|
}
|
|
199
873
|
|
|
200
|
-
const
|
|
201
|
-
|
|
202
|
-
|
|
874
|
+
for (const p of targets) rmSync(p, {recursive: true, force: true});
|
|
875
|
+
process.stdout.write(
|
|
876
|
+
`anon-pi: deleted project ${JSON.stringify(plan.project)} ` +
|
|
877
|
+
`(files + ${sessionCount} per-machine session dir(s)). The machine homes are kept.\n`,
|
|
878
|
+
);
|
|
879
|
+
return 0;
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
// --- `anon-pi init` onboarding (thin I/O over the pure detect/verify decisions) --
|
|
883
|
+
//
|
|
884
|
+
// init is the HONEST, re-runnable onboarding. It captures the socks5h PROXY (by
|
|
885
|
+
// evidence: open ports + a real SOCKS5 handshake + a real `netcage verify` exit
|
|
886
|
+
// IP, NEVER a provider label), the local-model ENDPOINT (generating models.json
|
|
887
|
+
// from it), and the default machine IMAGE (menu from shipped Dockerfiles / an
|
|
888
|
+
// existing ref / skip, building via `podman build`), then writes config.json +
|
|
889
|
+
// the `default` machine. It REPLACES the old `import`. All the DECISIONS are
|
|
890
|
+
// pure (anon-pi.ts); this does only the socket probes, the netcage/podman
|
|
891
|
+
// spawns, and the prompts. It NEVER destroys machines/homes: it pre-fills
|
|
892
|
+
// current values and only ADDS/updates config + a fresh default machine.
|
|
893
|
+
|
|
894
|
+
const INIT_HELP = `anon-pi init - onboard: verify your proxy, capture your local model, pick an image
|
|
895
|
+
|
|
896
|
+
USAGE
|
|
897
|
+
anon-pi init interactive onboarding (re-runnable reconfigure)
|
|
898
|
+
|
|
899
|
+
WHAT IT DOES
|
|
900
|
+
1. PROXY: probes common SOCKS ports, confirms SOCKS5 via a real handshake,
|
|
901
|
+
shows the findings (EVIDENCE only, never a provider label), then runs
|
|
902
|
+
\`netcage verify\` and shows the real EXIT IP as proof. You confirm.
|
|
903
|
+
2. LOCAL MODEL: captures host:port, probes reachability, generates models.json.
|
|
904
|
+
3. IMAGE: pick a shipped Dockerfile (built via podman), an existing ref, or skip.
|
|
905
|
+
Then writes ~/.anon-pi/config.json + the \`default\` machine. Never destroys homes.
|
|
906
|
+
`;
|
|
907
|
+
|
|
908
|
+
function runInit(args: string[]): number {
|
|
909
|
+
if (args.includes('--help') || args.includes('-h')) {
|
|
910
|
+
process.stdout.write(INIT_HELP);
|
|
911
|
+
return 0;
|
|
912
|
+
}
|
|
913
|
+
if (args.length > 0) {
|
|
203
914
|
process.stderr.write(
|
|
204
|
-
`anon-pi
|
|
915
|
+
`anon-pi: init takes no arguments, got: ${args.join(' ')}. Run \`anon-pi init --help\`.\n`,
|
|
205
916
|
);
|
|
206
917
|
return 1;
|
|
207
918
|
}
|
|
208
919
|
|
|
209
|
-
if (
|
|
920
|
+
if (!process.stdin.isTTY) {
|
|
210
921
|
process.stderr.write(
|
|
211
|
-
|
|
212
|
-
'
|
|
213
|
-
|
|
922
|
+
'anon-pi: init is interactive and needs a TTY. Run it in a terminal. To set\n' +
|
|
923
|
+
'values non-interactively, write ~/.anon-pi/config.json + a machine.json by hand,\n' +
|
|
924
|
+
'or export ANON_PI_PROXY / ANON_PI_LLM (they override config.json).\n',
|
|
925
|
+
);
|
|
926
|
+
return 1;
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
const env = envFromProcess(process.env);
|
|
930
|
+
// Pre-fill from the CURRENT config (re-runnable: init doubles as reconfigure).
|
|
931
|
+
const current = readJsonConfig(env);
|
|
932
|
+
|
|
933
|
+
process.stdout.write(
|
|
934
|
+
'anon-pi init: honest, evidence-based onboarding. Nothing is destroyed; your\n' +
|
|
935
|
+
'current values are pre-filled. Press Ctrl-C to abort at any prompt.\n\n',
|
|
936
|
+
);
|
|
937
|
+
|
|
938
|
+
// 1) PROXY: probe + handshake + findings + netcage verify + confirm.
|
|
939
|
+
const proxyHostPort = initProxyStep(current.proxy);
|
|
940
|
+
if (proxyHostPort === undefined) return 1;
|
|
941
|
+
const proxyUrl = socks5hUrl(proxyHostPort);
|
|
942
|
+
|
|
943
|
+
// 2) LOCAL MODEL endpoint: capture + probe + generate models.json.
|
|
944
|
+
const llm = initLlmStep(current.llm);
|
|
945
|
+
// llm may be undefined if the user skipped it (the launch path still errors
|
|
946
|
+
// without one, but init lets you set it later; we do not force it here).
|
|
947
|
+
|
|
948
|
+
// 3) DEFAULT MACHINE IMAGE: menu (shipped Dockerfiles / existing ref / skip).
|
|
949
|
+
const image = initImageStep();
|
|
950
|
+
if (image === ABORT) return 1;
|
|
951
|
+
|
|
952
|
+
// 4) WRITE config.json + the `default` machine (never destroying an existing
|
|
953
|
+
// home). The proxy is always present (we only reach here on a chosen proxy).
|
|
954
|
+
const anonHome = resolveAnonPiHome(env);
|
|
955
|
+
mkdirSync(anonHome, {recursive: true});
|
|
956
|
+
const configPath = join(anonHome, 'config.json');
|
|
957
|
+
const nextConfig: AnonPiConfig = {
|
|
958
|
+
proxy: proxyUrl,
|
|
959
|
+
llm: llm ?? current.llm,
|
|
960
|
+
defaultMachine: current.defaultMachine ?? DEFAULT_MACHINE,
|
|
961
|
+
projects: current.projects,
|
|
962
|
+
};
|
|
963
|
+
writeFileSync(configPath, serializeConfigJson(nextConfig));
|
|
964
|
+
process.stdout.write(`\nanon-pi: wrote ${configPath}.\n`);
|
|
965
|
+
|
|
966
|
+
// The `default` machine: create it if absent (NEVER wipe an existing home),
|
|
967
|
+
// pin/re-pin its image when one was chosen. Its home seeds on first launch.
|
|
968
|
+
initWriteDefaultMachine(env, image);
|
|
969
|
+
|
|
970
|
+
// The per-machine models.json seed for the default machine, generated from the
|
|
971
|
+
// captured endpoint (this is the `import` replacement). Only when we have an
|
|
972
|
+
// endpoint; written next to the machine so the first-launch seed mounts it.
|
|
973
|
+
if ((llm ?? current.llm) !== undefined) {
|
|
974
|
+
const mdir = machineDir(env, DEFAULT_MACHINE);
|
|
975
|
+
mkdirSync(mdir, {recursive: true});
|
|
976
|
+
const models = generateModelsJson((llm ?? current.llm) as string);
|
|
977
|
+
writeFileSync(
|
|
978
|
+
join(mdir, MODELS_FILE),
|
|
979
|
+
JSON.stringify(models, null, '\t') + '\n',
|
|
980
|
+
);
|
|
981
|
+
process.stdout.write(
|
|
982
|
+
`anon-pi: wrote the local-model models.json for machine "${DEFAULT_MACHINE}".\n`,
|
|
214
983
|
);
|
|
215
984
|
}
|
|
216
985
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
`anon-pi import: wrote ${dest} (provider "${result.name}"). Run \`anon-pi\` to launch.\n`,
|
|
986
|
+
process.stdout.write(
|
|
987
|
+
'\nanon-pi: onboarding complete. Launch with `anon-pi <project>` or ' +
|
|
988
|
+
'`anon-pi --shell`.\n',
|
|
221
989
|
);
|
|
222
990
|
return 0;
|
|
223
991
|
}
|
|
224
992
|
|
|
993
|
+
/** A sentinel a step returns when the user aborted (distinct from "skipped"). */
|
|
994
|
+
const ABORT = Symbol('abort');
|
|
995
|
+
|
|
996
|
+
/**
|
|
997
|
+
* The PROXY step: probe the default SOCKS ports, confirm SOCKS5 via a real
|
|
998
|
+
* handshake, show the EVIDENCE (never a provider label), let the user CHOOSE a
|
|
999
|
+
* SOCKS5-confirmed port or enter host:port, then run `netcage verify` and show
|
|
1000
|
+
* the real EXIT IP before confirming. Returns the chosen host:port, or undefined
|
|
1001
|
+
* on abort. The socket probes + the netcage spawn are the only I/O; the display
|
|
1002
|
+
* + the handshake verdict come from the pure module.
|
|
1003
|
+
*/
|
|
1004
|
+
function initProxyStep(currentProxy: string | undefined): string | undefined {
|
|
1005
|
+
process.stdout.write(
|
|
1006
|
+
'Step 1/3 - proxy (the socks5h endpoint that anonymizes egress)\n',
|
|
1007
|
+
);
|
|
1008
|
+
if (currentProxy) {
|
|
1009
|
+
process.stdout.write(` current: ${currentProxy}\n`);
|
|
1010
|
+
}
|
|
1011
|
+
process.stdout.write(' Probing common SOCKS ports (evidence only)...\n');
|
|
1012
|
+
|
|
1013
|
+
// Probe each default port: TCP-open + a real SOCKS5 handshake. The weak
|
|
1014
|
+
// process hint (a running `tor`/`wireproxy` LOCAL process, never the exit
|
|
1015
|
+
// provider) is HOST-WIDE, so gather it ONCE and pass it as the formatter's
|
|
1016
|
+
// single general note rather than gluing it onto every port line. The display
|
|
1017
|
+
// is the PURE formatter's job.
|
|
1018
|
+
const runningProcesses = observeRunningProcesses();
|
|
1019
|
+
const processNote = matchProcessHint(runningProcesses);
|
|
1020
|
+
const findings: ProxyFinding[] = DEFAULT_SOCKS_PROBE_PORTS.map(
|
|
1021
|
+
({port, hint}) => {
|
|
1022
|
+
const {open, handshake} = probeSocks5('127.0.0.1', port);
|
|
1023
|
+
return {
|
|
1024
|
+
host: '127.0.0.1',
|
|
1025
|
+
port,
|
|
1026
|
+
open,
|
|
1027
|
+
handshake,
|
|
1028
|
+
portHint: hint,
|
|
1029
|
+
};
|
|
1030
|
+
},
|
|
1031
|
+
);
|
|
1032
|
+
process.stdout.write(
|
|
1033
|
+
'\n' + formatProxyFindings(findings, processNote) + '\n\n',
|
|
1034
|
+
);
|
|
1035
|
+
|
|
1036
|
+
// Offer the SOCKS5-confirmed candidates as quick picks; always allow a manual
|
|
1037
|
+
// host:port entry (and pre-fill the current one).
|
|
1038
|
+
const confirmed = findings.filter((f) => f.open && f.handshake?.socks5);
|
|
1039
|
+
for (;;) {
|
|
1040
|
+
if (confirmed.length > 0) {
|
|
1041
|
+
process.stdout.write('SOCKS5-confirmed ports:\n');
|
|
1042
|
+
confirmed.forEach((f, i) => {
|
|
1043
|
+
process.stdout.write(` [${i + 1}] ${f.host}:${f.port}\n`);
|
|
1044
|
+
});
|
|
1045
|
+
}
|
|
1046
|
+
const prefill = currentProxy
|
|
1047
|
+
? ` (or Enter to keep ${hostPortKey(currentProxy)})`
|
|
1048
|
+
: '';
|
|
1049
|
+
const ans = promptLine(`Choose a number, or enter host:port${prefill}: `);
|
|
1050
|
+
if (ans === undefined) {
|
|
1051
|
+
process.stderr.write('anon-pi: aborted; nothing written.\n');
|
|
1052
|
+
return undefined;
|
|
1053
|
+
}
|
|
1054
|
+
const trimmed = ans.trim();
|
|
1055
|
+
let chosen: string | undefined;
|
|
1056
|
+
if (trimmed === '' && currentProxy) {
|
|
1057
|
+
chosen = hostPortKey(currentProxy);
|
|
1058
|
+
} else if (/^\d+$/.test(trimmed) && confirmed.length > 0) {
|
|
1059
|
+
const idx = Number(trimmed) - 1;
|
|
1060
|
+
if (idx >= 0 && idx < confirmed.length) {
|
|
1061
|
+
const f = confirmed[idx];
|
|
1062
|
+
chosen = `${f.host}:${f.port}`;
|
|
1063
|
+
}
|
|
1064
|
+
} else if (trimmed !== '') {
|
|
1065
|
+
chosen = hostPortKey(trimmed);
|
|
1066
|
+
}
|
|
1067
|
+
if (chosen === undefined || chosen === '') {
|
|
1068
|
+
process.stdout.write(
|
|
1069
|
+
' Please pick a listed number or enter a host:port.\n',
|
|
1070
|
+
);
|
|
1071
|
+
continue;
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
// VERIFY: run `netcage verify --proxy socks5h://<chosen>` and show the real
|
|
1075
|
+
// exit IP as evidence it is NOT the host IP. The user confirms ON that
|
|
1076
|
+
// evidence. netcage never announces the provider, so neither do we.
|
|
1077
|
+
const url = socks5hUrl(chosen);
|
|
1078
|
+
process.stdout.write(
|
|
1079
|
+
`\n Verifying via netcage: netcage verify --proxy ${url}\n`,
|
|
1080
|
+
);
|
|
1081
|
+
if (!hasNetcage()) {
|
|
1082
|
+
process.stderr.write(
|
|
1083
|
+
'anon-pi: `netcage` not found on PATH, cannot verify the exit IP. Install\n' +
|
|
1084
|
+
'it first (https://github.com/wighawag/netcage). Linux only.\n',
|
|
1085
|
+
);
|
|
1086
|
+
return undefined;
|
|
1087
|
+
}
|
|
1088
|
+
const verify = spawnSync('netcage', ['verify', '--proxy', url], {
|
|
1089
|
+
encoding: 'utf8',
|
|
1090
|
+
});
|
|
1091
|
+
const output = `${verify.stdout ?? ''}${verify.stderr ?? ''}`;
|
|
1092
|
+
if (verify.error || verify.status !== 0) {
|
|
1093
|
+
process.stdout.write(output.trimEnd() + '\n');
|
|
1094
|
+
process.stdout.write(
|
|
1095
|
+
` netcage verify FAILED for ${url} (exit ${verify.status ?? 'n/a'}). ` +
|
|
1096
|
+
'Pick another port or fix the proxy.\n\n',
|
|
1097
|
+
);
|
|
1098
|
+
continue;
|
|
1099
|
+
}
|
|
1100
|
+
const exitIp = parseVerifyExitIp(output);
|
|
1101
|
+
if (exitIp) {
|
|
1102
|
+
process.stdout.write(
|
|
1103
|
+
` Exit IP (via the proxy, NOT your host): ${exitIp}\n`,
|
|
1104
|
+
);
|
|
1105
|
+
} else {
|
|
1106
|
+
process.stdout.write(
|
|
1107
|
+
' netcage verify succeeded but no exit IP was parsed; raw output:\n' +
|
|
1108
|
+
output.trimEnd() +
|
|
1109
|
+
'\n',
|
|
1110
|
+
);
|
|
1111
|
+
}
|
|
1112
|
+
const ok = promptLine(` Use ${url} as your proxy? [Y/n] `);
|
|
1113
|
+
if (ok === undefined) {
|
|
1114
|
+
process.stderr.write('anon-pi: aborted; nothing written.\n');
|
|
1115
|
+
return undefined;
|
|
1116
|
+
}
|
|
1117
|
+
if (/^n(o)?$/i.test(ok.trim())) {
|
|
1118
|
+
process.stdout.write(' OK, pick another.\n\n');
|
|
1119
|
+
continue;
|
|
1120
|
+
}
|
|
1121
|
+
return chosen;
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
/**
|
|
1126
|
+
* The LOCAL MODEL step: capture host:port (pre-filled from config), probe TCP
|
|
1127
|
+
* reachability (evidence, not a gate), and return the endpoint (undefined if the
|
|
1128
|
+
* user skips it). models.json is generated from it by runInit. The one direct
|
|
1129
|
+
* hole; all other egress stays proxied.
|
|
1130
|
+
*/
|
|
1131
|
+
function initLlmStep(currentLlm: string | undefined): string | undefined {
|
|
1132
|
+
process.stdout.write(
|
|
1133
|
+
'\nStep 2/3 - local model endpoint (the ONE direct hole)\n',
|
|
1134
|
+
);
|
|
1135
|
+
if (currentLlm) process.stdout.write(` current: ${currentLlm}\n`);
|
|
1136
|
+
const prefill = currentLlm
|
|
1137
|
+
? ` (or Enter to keep ${currentLlm})`
|
|
1138
|
+
: ' (or Enter to skip)';
|
|
1139
|
+
const ans = promptLine(
|
|
1140
|
+
` Local model host:port, e.g. 192.168.1.150:8080${prefill}: `,
|
|
1141
|
+
);
|
|
1142
|
+
if (ans === undefined) return currentLlm;
|
|
1143
|
+
const trimmed = ans.trim();
|
|
1144
|
+
if (trimmed === '') return currentLlm;
|
|
1145
|
+
const endpoint = trimmed;
|
|
1146
|
+
|
|
1147
|
+
// Probe reachability: evidence only. A closed port is not fatal (the model may
|
|
1148
|
+
// start later); we just report it.
|
|
1149
|
+
const key = hostPortKey(endpoint);
|
|
1150
|
+
const colon = key.lastIndexOf(':');
|
|
1151
|
+
const host = colon > 0 ? key.slice(0, colon) : key;
|
|
1152
|
+
const port = colon > 0 ? Number(key.slice(colon + 1)) : 80;
|
|
1153
|
+
if (Number.isFinite(port)) {
|
|
1154
|
+
const reachable = probeTcp(host, port);
|
|
1155
|
+
process.stdout.write(
|
|
1156
|
+
reachable
|
|
1157
|
+
? ` reachable: ${host}:${port} accepted a TCP connection.\n`
|
|
1158
|
+
: ` note: ${host}:${port} did not accept a connection now (the model may not be up yet).\n`,
|
|
1159
|
+
);
|
|
1160
|
+
}
|
|
1161
|
+
return endpoint;
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
/**
|
|
1165
|
+
* The IMAGE step: the pure menu (shipped Dockerfiles / existing ref / skip), then
|
|
1166
|
+
* the impure action for the pick (build via `podman build`, take a ref, or
|
|
1167
|
+
* skip). Returns the resolved image ref, undefined for skip, or ABORT.
|
|
1168
|
+
*/
|
|
1169
|
+
function initImageStep(): string | undefined | typeof ABORT {
|
|
1170
|
+
process.stdout.write(
|
|
1171
|
+
'\nStep 3/3 - default machine image (an image with `pi` on PATH)\n',
|
|
1172
|
+
);
|
|
1173
|
+
const menu = initImageMenu();
|
|
1174
|
+
menu.forEach((e, i) => {
|
|
1175
|
+
process.stdout.write(` [${i + 1}] ${e.label}\n`);
|
|
1176
|
+
});
|
|
1177
|
+
for (;;) {
|
|
1178
|
+
const ans = promptLine(' Choose [1-4]: ');
|
|
1179
|
+
if (ans === undefined) return ABORT;
|
|
1180
|
+
const idx = Number(ans.trim()) - 1;
|
|
1181
|
+
if (!Number.isInteger(idx) || idx < 0 || idx >= menu.length) {
|
|
1182
|
+
process.stdout.write(' Please pick a number 1-4.\n');
|
|
1183
|
+
continue;
|
|
1184
|
+
}
|
|
1185
|
+
const choice: InitImageChoice = menu[idx].choice;
|
|
1186
|
+
if (choice === 'skip') {
|
|
1187
|
+
process.stdout.write(
|
|
1188
|
+
' Skipping the image; pin it later with `anon-pi machine set-image`.\n',
|
|
1189
|
+
);
|
|
1190
|
+
return undefined;
|
|
1191
|
+
}
|
|
1192
|
+
if (choice === 'existing') {
|
|
1193
|
+
const ref = promptLine(' Image ref (a container with `pi` on PATH): ');
|
|
1194
|
+
if (ref === undefined || ref.trim() === '') {
|
|
1195
|
+
process.stdout.write(' No ref given; pick again.\n');
|
|
1196
|
+
continue;
|
|
1197
|
+
}
|
|
1198
|
+
return ref.trim();
|
|
1199
|
+
}
|
|
1200
|
+
// basic | webveil: build the shipped Dockerfile via `podman build`.
|
|
1201
|
+
const dockerfile =
|
|
1202
|
+
choice === 'basic'
|
|
1203
|
+
? shippedDockerfilePath()
|
|
1204
|
+
: shippedWebveilDockerfilePath();
|
|
1205
|
+
if (dockerfile === undefined || !existsSync(dockerfile)) {
|
|
1206
|
+
process.stderr.write(
|
|
1207
|
+
` anon-pi: could not locate the shipped ${choice === 'basic' ? 'Dockerfile.pi' : 'examples/Dockerfile.pi-webveil'}. ` +
|
|
1208
|
+
'Pick an existing ref instead.\n',
|
|
1209
|
+
);
|
|
1210
|
+
continue;
|
|
1211
|
+
}
|
|
1212
|
+
const tag =
|
|
1213
|
+
choice === 'basic' ? 'anon-pi/pi:latest' : 'anon-pi/pi-webveil:latest';
|
|
1214
|
+
const built = buildImage(dockerfile, tag);
|
|
1215
|
+
if (!built) {
|
|
1216
|
+
process.stdout.write(' Build failed; pick another option.\n');
|
|
1217
|
+
continue;
|
|
1218
|
+
}
|
|
1219
|
+
return tag;
|
|
1220
|
+
}
|
|
1221
|
+
}
|
|
1222
|
+
|
|
1223
|
+
/**
|
|
1224
|
+
* Create or update the `default` machine: create it (dir + machine.json) if
|
|
1225
|
+
* absent, pinning the chosen image; if it already exists, re-pin the image only
|
|
1226
|
+
* when one was chosen (preserving any per-machine projects override), and NEVER
|
|
1227
|
+
* touch its home (init is non-destructive). A skipped image leaves an existing
|
|
1228
|
+
* machine's image as-is, or creates an imageless machine.
|
|
1229
|
+
*/
|
|
1230
|
+
function initWriteDefaultMachine(
|
|
1231
|
+
env: AnonPiEnv,
|
|
1232
|
+
image: string | undefined,
|
|
1233
|
+
): void {
|
|
1234
|
+
const name = DEFAULT_MACHINE;
|
|
1235
|
+
const dir = machineDir(env, name);
|
|
1236
|
+
const existed = existsSync(dir);
|
|
1237
|
+
mkdirSync(machineHomeDir(env, name), {recursive: true});
|
|
1238
|
+
if (!existed) {
|
|
1239
|
+
writeFileSync(machineJsonPath(env, name), serializeMachineJson({image}));
|
|
1240
|
+
process.stdout.write(
|
|
1241
|
+
`anon-pi: created machine "${name}"${image ? ` (image ${image})` : ' (imageless; pin it later)'} at ${dir}.\n`,
|
|
1242
|
+
);
|
|
1243
|
+
return;
|
|
1244
|
+
}
|
|
1245
|
+
// Existing machine: re-pin only if a new image was chosen; keep its projects
|
|
1246
|
+
// override and its home untouched.
|
|
1247
|
+
const prev = readMachineJson(env, name);
|
|
1248
|
+
if (image !== undefined) {
|
|
1249
|
+
writeFileSync(
|
|
1250
|
+
machineJsonPath(env, name),
|
|
1251
|
+
serializeMachineJson({image, projects: prev.projects}),
|
|
1252
|
+
);
|
|
1253
|
+
process.stdout.write(
|
|
1254
|
+
`anon-pi: re-pinned machine "${name}" image to ${image} (home kept intact).\n`,
|
|
1255
|
+
);
|
|
1256
|
+
} else {
|
|
1257
|
+
process.stdout.write(
|
|
1258
|
+
`anon-pi: machine "${name}" already exists; kept its image + home.\n`,
|
|
1259
|
+
);
|
|
1260
|
+
}
|
|
1261
|
+
}
|
|
1262
|
+
|
|
1263
|
+
// --- init's thin I/O primitives (socket probes, process observe, podman build) --
|
|
1264
|
+
|
|
1265
|
+
/**
|
|
1266
|
+
* Probe a TCP port for openness AND a SOCKS5 handshake: connect, send the
|
|
1267
|
+
* no-auth method-selection greeting, read the reply, and interpret it with the
|
|
1268
|
+
* PURE interpretSocks5Handshake. Fully synchronous + best-effort with a short
|
|
1269
|
+
* timeout so init stays a simple linear prompt flow. On any connect failure the
|
|
1270
|
+
* port is `open: false` with no handshake.
|
|
1271
|
+
*/
|
|
1272
|
+
function probeSocks5(
|
|
1273
|
+
host: string,
|
|
1274
|
+
port: number,
|
|
1275
|
+
): {open: boolean; handshake?: SocksHandshake} {
|
|
1276
|
+
// A synchronous SOCKS5 probe: node's net is async, so we drive a tiny state
|
|
1277
|
+
// machine over a blocking loop using a short deadline. To keep it simple and
|
|
1278
|
+
// dependency-free we use a child `bash`+`/dev/tcp`-style probe is unavailable
|
|
1279
|
+
// portably, so instead we do a promise-free spin with a hard cap via a
|
|
1280
|
+
// separate helper that returns synchronously.
|
|
1281
|
+
const reply = socks5Handshake(host, port, SOCKS5_METHOD_SELECTOR, 600);
|
|
1282
|
+
if (reply === undefined) return {open: false};
|
|
1283
|
+
return {open: true, handshake: interpretSocks5Handshake(reply)};
|
|
1284
|
+
}
|
|
1285
|
+
|
|
1286
|
+
/**
|
|
1287
|
+
* Best-effort synchronous SOCKS5 handshake: open a TCP connection, write the
|
|
1288
|
+
* greeting, and collect the reply bytes, blocking up to `timeoutMs`. Returns the
|
|
1289
|
+
* reply bytes when the connection opened (possibly empty if the server sent
|
|
1290
|
+
* nothing), or undefined when the connection could not be opened at all (port
|
|
1291
|
+
* closed / refused). Implemented with a nested event loop drained via a shared
|
|
1292
|
+
* flag, so the caller stays a simple linear script.
|
|
1293
|
+
*/
|
|
1294
|
+
function socks5Handshake(
|
|
1295
|
+
host: string,
|
|
1296
|
+
port: number,
|
|
1297
|
+
greeting: readonly number[],
|
|
1298
|
+
timeoutMs: number,
|
|
1299
|
+
): number[] | undefined {
|
|
1300
|
+
// node has no synchronous socket API; run a tiny worker via execFileSync on
|
|
1301
|
+
// the same node binary so the probe is fully synchronous and portable. The
|
|
1302
|
+
// worker connects, sends the greeting, reads up to 2 bytes, and prints them as
|
|
1303
|
+
// JSON (or prints "null" when the connection was refused).
|
|
1304
|
+
const script =
|
|
1305
|
+
`const net=require('net');` +
|
|
1306
|
+
`const s=net.connect({host:${JSON.stringify(host)},port:${port}});` +
|
|
1307
|
+
`let done=false;const bytes=[];` +
|
|
1308
|
+
`const fin=(v)=>{if(done)return;done=true;try{s.destroy()}catch(e){}` +
|
|
1309
|
+
`process.stdout.write(JSON.stringify(v));process.exit(0)};` +
|
|
1310
|
+
`s.setTimeout(${timeoutMs});` +
|
|
1311
|
+
`s.on('connect',()=>{s.write(Buffer.from(${JSON.stringify([...greeting])}))});` +
|
|
1312
|
+
`s.on('data',(d)=>{for(const b of d)bytes.push(b);if(bytes.length>=2)fin(bytes)});` +
|
|
1313
|
+
`s.on('timeout',()=>fin(bytes));` +
|
|
1314
|
+
`s.on('error',()=>fin(null));` +
|
|
1315
|
+
`s.on('close',()=>fin(bytes));`;
|
|
1316
|
+
try {
|
|
1317
|
+
const out = execFileSync(process.execPath, ['-e', script], {
|
|
1318
|
+
encoding: 'utf8',
|
|
1319
|
+
timeout: timeoutMs + 1500,
|
|
1320
|
+
});
|
|
1321
|
+
const parsed = JSON.parse(out) as number[] | null;
|
|
1322
|
+
return parsed === null ? undefined : parsed;
|
|
1323
|
+
} catch {
|
|
1324
|
+
return undefined;
|
|
1325
|
+
}
|
|
1326
|
+
}
|
|
1327
|
+
|
|
1328
|
+
/**
|
|
1329
|
+
* Best-effort synchronous TCP reachability probe (open a connection, succeed or
|
|
1330
|
+
* not) for the local-model endpoint. Reuses the socks5Handshake worker with an
|
|
1331
|
+
* empty greeting: a non-undefined return means the connection opened.
|
|
1332
|
+
*/
|
|
1333
|
+
function probeTcp(host: string, port: number): boolean {
|
|
1334
|
+
return socks5Handshake(host, port, [], 500) !== undefined;
|
|
1335
|
+
}
|
|
1336
|
+
|
|
1337
|
+
/**
|
|
1338
|
+
* Observe LOCAL process names (best-effort) so init can offer WEAK hints (a
|
|
1339
|
+
* running `tor` -> likely Tor). Returns the lowercased process names seen, or []
|
|
1340
|
+
* on any failure. This is a LOCAL observation only; it never claims the exit
|
|
1341
|
+
* provider.
|
|
1342
|
+
*/
|
|
1343
|
+
function observeRunningProcesses(): string[] {
|
|
1344
|
+
const res = spawnSync('ps', ['-eo', 'comm='], {encoding: 'utf8'});
|
|
1345
|
+
if (res.error || res.status !== 0 || !res.stdout) return [];
|
|
1346
|
+
return res.stdout
|
|
1347
|
+
.split('\n')
|
|
1348
|
+
.map((l) => l.trim().split('/').pop() ?? '')
|
|
1349
|
+
.filter((n) => n !== '')
|
|
1350
|
+
.map((n) => n.toLowerCase());
|
|
1351
|
+
}
|
|
1352
|
+
|
|
1353
|
+
/**
|
|
1354
|
+
* The weak process-hint text for the observed processes, if any maps (via the
|
|
1355
|
+
* PURE processHint). Returns the FIRST matching hint (tor before wireproxy), or
|
|
1356
|
+
* undefined. Never names the exit provider.
|
|
1357
|
+
*/
|
|
1358
|
+
function matchProcessHint(processes: readonly string[]): string | undefined {
|
|
1359
|
+
for (const p of processes) {
|
|
1360
|
+
const h = processHint(p);
|
|
1361
|
+
if (h) return h.hint;
|
|
1362
|
+
}
|
|
1363
|
+
return undefined;
|
|
1364
|
+
}
|
|
1365
|
+
|
|
1366
|
+
/**
|
|
1367
|
+
* Build a shipped Dockerfile into `tag` via `podman build`. Streams podman's
|
|
1368
|
+
* output (inherited stdio) so the user sees the build. Returns true on success.
|
|
1369
|
+
* The build CONTEXT is the Dockerfile's own directory (the shipped examples/
|
|
1370
|
+
* dir or the package root), which is where its COPY sources live.
|
|
1371
|
+
*/
|
|
1372
|
+
function buildImage(dockerfile: string, tag: string): boolean {
|
|
1373
|
+
const context = dirname(dockerfile);
|
|
1374
|
+
process.stdout.write(
|
|
1375
|
+
` Building ${tag} from ${dockerfile} (podman build)...\n`,
|
|
1376
|
+
);
|
|
1377
|
+
const res = spawnSync(
|
|
1378
|
+
'podman',
|
|
1379
|
+
['build', '-t', tag, '-f', dockerfile, context],
|
|
1380
|
+
{stdio: 'inherit'},
|
|
1381
|
+
);
|
|
1382
|
+
if (res.error) {
|
|
1383
|
+
process.stderr.write(
|
|
1384
|
+
` anon-pi: failed to run podman: ${res.error.message}. Is podman installed?\n`,
|
|
1385
|
+
);
|
|
1386
|
+
return false;
|
|
1387
|
+
}
|
|
1388
|
+
return res.status === 0;
|
|
1389
|
+
}
|
|
1390
|
+
|
|
1391
|
+
/** List machine names (readdir of machines/), or [] if the dir is absent. */
|
|
1392
|
+
function listMachineNames(env: AnonPiEnv): string[] {
|
|
1393
|
+
const root = join(resolveAnonPiHome(env), 'machines');
|
|
1394
|
+
if (!existsSync(root)) return [];
|
|
1395
|
+
return readdirSync(root, {withFileTypes: true})
|
|
1396
|
+
.filter((d) => d.isDirectory())
|
|
1397
|
+
.map((d) => d.name);
|
|
1398
|
+
}
|
|
1399
|
+
|
|
1400
|
+
/**
|
|
1401
|
+
* Read one line from stdin synchronously for a confirm/value prompt, writing the
|
|
1402
|
+
* prompt to stderr first. Returns undefined on EOF/error. Only called on a TTY
|
|
1403
|
+
* (the verbs enforce the non-TTY discipline before prompting), so a blocking
|
|
1404
|
+
* byte-at-a-time read from fd 0 is fine: the user types a line and hits enter.
|
|
1405
|
+
*/
|
|
1406
|
+
function promptLine(prompt: string): string | undefined {
|
|
1407
|
+
process.stderr.write(prompt);
|
|
1408
|
+
const byte = Buffer.alloc(1);
|
|
1409
|
+
let line = '';
|
|
1410
|
+
for (;;) {
|
|
1411
|
+
let n: number;
|
|
1412
|
+
try {
|
|
1413
|
+
n = readSync(0, byte, 0, 1, null);
|
|
1414
|
+
} catch (e) {
|
|
1415
|
+
// EAGAIN on a non-blocking TTY: retry; anything else ends the read.
|
|
1416
|
+
if ((e as NodeJS.ErrnoException).code === 'EAGAIN') continue;
|
|
1417
|
+
break;
|
|
1418
|
+
}
|
|
1419
|
+
if (n === 0) break; // EOF
|
|
1420
|
+
const ch = byte.toString('utf8', 0, 1);
|
|
1421
|
+
if (ch === '\n') return line;
|
|
1422
|
+
if (ch !== '\r') line += ch;
|
|
1423
|
+
}
|
|
1424
|
+
return line === '' ? undefined : line;
|
|
1425
|
+
}
|
|
1426
|
+
|
|
1427
|
+
/** The `machine` subcommand help. */
|
|
1428
|
+
const MACHINE_HELP = `anon-pi machine - manage machines (an image + a persistent host home)
|
|
1429
|
+
|
|
1430
|
+
USAGE
|
|
1431
|
+
anon-pi machine create <name> [--image <ref>] create a machine, pin its image
|
|
1432
|
+
anon-pi machine list list machines and their images
|
|
1433
|
+
anon-pi machine set-image <name> <ref> re-pin the image (WARNS; no reseed)
|
|
1434
|
+
anon-pi machine rm <name> [--yes] delete the machine + its home
|
|
1435
|
+
|
|
1436
|
+
A machine is an image + a persistent host home (machines/<name>/{machine.json,home/}).
|
|
1437
|
+
The home is seeded on FIRST LAUNCH, not at create. \`set-image\` re-pins only and
|
|
1438
|
+
warns (the home was built for the old image); \`rm\` confirms on a TTY, skips with
|
|
1439
|
+
\`--yes\`, and aborts non-interactively without it.
|
|
1440
|
+
`;
|
|
1441
|
+
|
|
1442
|
+
// --- impure helpers ---------------------------------------------------------
|
|
1443
|
+
|
|
1444
|
+
/** Read + parse <anon-pi-home>/config.json (tolerant: absent/garbage => {}). */
|
|
1445
|
+
function readJsonConfig(env: AnonPiEnv): AnonPiConfig {
|
|
1446
|
+
const path = join(resolveAnonPiHome(env), 'config.json');
|
|
1447
|
+
return parseConfigJson(readJsonFile(path));
|
|
1448
|
+
}
|
|
1449
|
+
|
|
1450
|
+
/** Read + parse a machine's machine.json (tolerant: absent/garbage => {}). */
|
|
1451
|
+
function readMachineJson(env: AnonPiEnv, name: string): MachineConfig {
|
|
1452
|
+
return parseMachineJson(readJsonFile(machineJsonPath(env, name)));
|
|
1453
|
+
}
|
|
1454
|
+
|
|
1455
|
+
/** Read + JSON.parse a file, returning undefined if absent or unparseable. */
|
|
1456
|
+
function readJsonFile(path: string): unknown {
|
|
1457
|
+
if (!existsSync(path)) return undefined;
|
|
1458
|
+
try {
|
|
1459
|
+
return JSON.parse(readFileSync(path, 'utf8'));
|
|
1460
|
+
} catch {
|
|
1461
|
+
return undefined;
|
|
1462
|
+
}
|
|
1463
|
+
}
|
|
1464
|
+
|
|
1465
|
+
/**
|
|
1466
|
+
* True iff a machine home is FRESH (no seed marker): the seed will run. The
|
|
1467
|
+
* marker lives under the mounted home at `.pi/agent/<SEED_MARKER>` (the host
|
|
1468
|
+
* side of the container's /root/.pi/agent = CONTAINER_AGENT_DIR).
|
|
1469
|
+
*/
|
|
1470
|
+
function homeFresh(machineHome: string): boolean {
|
|
1471
|
+
const marker = join(machineHome, '.pi', 'agent', SEED_MARKER);
|
|
1472
|
+
return !existsSync(marker);
|
|
1473
|
+
}
|
|
1474
|
+
|
|
1475
|
+
/**
|
|
1476
|
+
* Query netcage for its KEPT managed containers, surfacing each one's stamped
|
|
1477
|
+
* anon-pi identity key so the pure run-vs-start decision can match it. Thin,
|
|
1478
|
+
* best-effort I/O: on any failure (netcage missing the query, no containers, a
|
|
1479
|
+
* parse error) it returns an EMPTY listing, so the decision falls back to a
|
|
1480
|
+
* fresh `run` (safe: it never wrongly resumes, it just creates a new container).
|
|
1481
|
+
*/
|
|
1482
|
+
function queryKeptContainers(): KeptContainer[] {
|
|
1483
|
+
// Ask netcage for its managed containers as JSON, reading back the anon-pi
|
|
1484
|
+
// key label. netcage is a podman drop-in, so `ps` accepts the same
|
|
1485
|
+
// label-filter + Go-template/JSON format flags.
|
|
1486
|
+
const res = spawnSync(
|
|
1487
|
+
'netcage',
|
|
1488
|
+
[
|
|
1489
|
+
'ps',
|
|
1490
|
+
'-a',
|
|
1491
|
+
'--filter',
|
|
1492
|
+
'label=netcage.managed',
|
|
1493
|
+
'--format',
|
|
1494
|
+
'{{.ID}}\t{{.Labels}}',
|
|
1495
|
+
],
|
|
1496
|
+
{encoding: 'utf8'},
|
|
1497
|
+
);
|
|
1498
|
+
if (res.error || res.status !== 0 || !res.stdout) return [];
|
|
1499
|
+
|
|
1500
|
+
const out: KeptContainer[] = [];
|
|
1501
|
+
for (const line of res.stdout.split('\n')) {
|
|
1502
|
+
const trimmed = line.trim();
|
|
1503
|
+
if (trimmed === '') continue;
|
|
1504
|
+
const tab = trimmed.indexOf('\t');
|
|
1505
|
+
if (tab < 0) continue;
|
|
1506
|
+
const ref = trimmed.slice(0, tab).trim();
|
|
1507
|
+
const labels = trimmed.slice(tab + 1);
|
|
1508
|
+
const key = extractKeyLabel(labels);
|
|
1509
|
+
if (ref !== '' && key !== undefined) out.push({key, ref});
|
|
1510
|
+
}
|
|
1511
|
+
return out;
|
|
1512
|
+
}
|
|
1513
|
+
|
|
1514
|
+
/**
|
|
1515
|
+
* Pull the anon-pi key out of a podman `{{.Labels}}` rendering (a
|
|
1516
|
+
* comma-separated `k=v` list). The key is stamped as `anon-pi.key=<opaque>`;
|
|
1517
|
+
* because keptContainerKey embeds newlines, the CLI base64-encodes it when
|
|
1518
|
+
* stamping (withKeyLabel) and decodes it here, so a `\n` never breaks the label.
|
|
1519
|
+
*/
|
|
1520
|
+
function extractKeyLabel(labels: string): string | undefined {
|
|
1521
|
+
for (const pair of labels.split(',')) {
|
|
1522
|
+
const eq = pair.indexOf('=');
|
|
1523
|
+
if (eq < 0) continue;
|
|
1524
|
+
const k = pair.slice(0, eq).trim();
|
|
1525
|
+
if (k !== ANON_PI_KEY_LABEL) continue;
|
|
1526
|
+
const v = pair.slice(eq + 1).trim();
|
|
1527
|
+
try {
|
|
1528
|
+
return Buffer.from(v, 'base64').toString('utf8');
|
|
1529
|
+
} catch {
|
|
1530
|
+
return undefined;
|
|
1531
|
+
}
|
|
1532
|
+
}
|
|
1533
|
+
return undefined;
|
|
1534
|
+
}
|
|
1535
|
+
|
|
1536
|
+
/**
|
|
1537
|
+
* Insert the anon-pi identity label into a `netcage run` argv (right after
|
|
1538
|
+
* `run`), so a kept container can be found on re-entry. The key is base64'd
|
|
1539
|
+
* (keptContainerKey embeds newlines) to keep it a single safe label value. This
|
|
1540
|
+
* is ADDITIVE and touches NO egress flag (the RunPlan owns --proxy/--allow-direct).
|
|
1541
|
+
*/
|
|
1542
|
+
function withKeyLabel(netcageArgs: string[], key: string): string[] {
|
|
1543
|
+
const enc = Buffer.from(key, 'utf8').toString('base64');
|
|
1544
|
+
const out = netcageArgs.slice();
|
|
1545
|
+
// netcageArgs[0] is 'run'; splice the label right after it.
|
|
1546
|
+
out.splice(1, 0, '--label', `${ANON_PI_KEY_LABEL}=${enc}`);
|
|
1547
|
+
return out;
|
|
1548
|
+
}
|
|
1549
|
+
|
|
1550
|
+
/** Spawn netcage with inherited stdio; propagate its exit code. */
|
|
1551
|
+
function spawnNetcage(netcageArgs: string[]): number {
|
|
1552
|
+
const res = spawnSync('netcage', netcageArgs, {stdio: 'inherit'});
|
|
1553
|
+
if (res.error) {
|
|
1554
|
+
process.stderr.write(
|
|
1555
|
+
`anon-pi: failed to run netcage: ${res.error.message}\n`,
|
|
1556
|
+
);
|
|
1557
|
+
return 1;
|
|
1558
|
+
}
|
|
1559
|
+
return res.status ?? 1;
|
|
1560
|
+
}
|
|
1561
|
+
|
|
225
1562
|
function hasNetcage(): boolean {
|
|
226
1563
|
const which = spawnSync(
|
|
227
1564
|
process.platform === 'win32' ? 'where' : 'command',
|
|
@@ -237,4 +1574,13 @@ function hasNetcage(): boolean {
|
|
|
237
1574
|
return !probe.error;
|
|
238
1575
|
}
|
|
239
1576
|
|
|
1577
|
+
/** Print an AnonPiError's message verbatim (exit 1) or rethrow anything else. */
|
|
1578
|
+
function reportAnonPiError(e: unknown): number {
|
|
1579
|
+
if (e instanceof AnonPiError) {
|
|
1580
|
+
process.stderr.write(e.message + '\n');
|
|
1581
|
+
return 1;
|
|
1582
|
+
}
|
|
1583
|
+
throw e;
|
|
1584
|
+
}
|
|
1585
|
+
|
|
240
1586
|
process.exit(main(process.argv));
|