@infosupport/huddle-cli 0.0.0-experiment-160.131.1 → 0.0.0-experiment-44.133.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -14
- package/dist/api.js +5 -11
- package/dist/config.js +2 -5
- package/dist/index.js +28 -53
- package/dist/indexfolder.js +167 -0
- package/dist/init.js +5 -50
- package/dist/runtime.js +19 -38
- package/dist/start.js +8 -10
- package/package.json +3 -7
- package/dist/extension.js +0 -170
- package/dist/host-logs.js +0 -60
- package/dist/pipe/command.js +0 -186
- package/dist/pipe/fs-list.js +0 -181
- package/dist/pipe/protocol.js +0 -99
- package/dist/pipe/sbx-kit.js +0 -142
- package/dist/pipe/sbx-links.js +0 -147
- package/dist/pipe/serve.js +0 -223
- package/dist/pipe/ssh-hosts.js +0 -155
- package/dist/pipe/tools.js +0 -227
- package/dist/zip.js +0 -74
package/dist/pipe/protocol.js
DELETED
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// ── Host pipe: shared protocol bits ───────────────────────────────────────────
|
|
3
|
-
// The host pipe is a folder (~/.huddle/pipe on the host, /huddle-home/pipe in the
|
|
4
|
-
// gateway) through which the gateway asks the host for things it cannot do from
|
|
5
|
-
// inside its container. See docs/host-pipe.md — that file is the contract; the
|
|
6
|
-
// names and numbers below mirror it and must not drift.
|
|
7
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
8
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
9
|
-
};
|
|
10
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
-
exports.PipeError = exports.MAX_TOOL_TIMEOUT_MS = exports.DEFAULT_TOOL_TIMEOUT_MS = exports.MAX_OUTPUT_BYTES = exports.MAX_FOLDER_ENTRIES = exports.STALE_FILE_MS = exports.HEARTBEAT_STALE_MS = exports.HEARTBEAT_MS = exports.POLL_MS = exports.LOG_FILE = exports.HEARTBEAT_FILE = exports.PIPE_DIR = exports.PROTOCOL_VERSION = void 0;
|
|
12
|
-
exports.ensurePrivateDir = ensurePrivateDir;
|
|
13
|
-
exports.writeJsonAtomic = writeJsonAtomic;
|
|
14
|
-
exports.readHeartbeat = readHeartbeat;
|
|
15
|
-
exports.heartbeatFresh = heartbeatFresh;
|
|
16
|
-
const fs_1 = __importDefault(require("fs"));
|
|
17
|
-
const path_1 = __importDefault(require("path"));
|
|
18
|
-
const config_1 = require("../config");
|
|
19
|
-
exports.PROTOCOL_VERSION = 1;
|
|
20
|
-
/** Where the agent serves by default: inside the ~/.huddle mount the gateway already has. */
|
|
21
|
-
exports.PIPE_DIR = path_1.default.join(config_1.CONFIG_DIR, 'pipe');
|
|
22
|
-
exports.HEARTBEAT_FILE = 'host.json';
|
|
23
|
-
exports.LOG_FILE = 'host.log';
|
|
24
|
-
exports.POLL_MS = 100;
|
|
25
|
-
exports.HEARTBEAT_MS = 2000;
|
|
26
|
-
/** The gateway calls the agent offline when the heartbeat is older than this. */
|
|
27
|
-
exports.HEARTBEAT_STALE_MS = 10_000;
|
|
28
|
-
/** Leftover request/reply files older than this are deleted on start. */
|
|
29
|
-
exports.STALE_FILE_MS = 60 * 60 * 1000;
|
|
30
|
-
exports.MAX_FOLDER_ENTRIES = 2000;
|
|
31
|
-
exports.MAX_OUTPUT_BYTES = 16 * 1024 * 1024;
|
|
32
|
-
exports.DEFAULT_TOOL_TIMEOUT_MS = 30_000;
|
|
33
|
-
exports.MAX_TOOL_TIMEOUT_MS = 15 * 60 * 1000;
|
|
34
|
-
/** Thrown by an op handler to answer with `ok:false` and a specific code. */
|
|
35
|
-
class PipeError extends Error {
|
|
36
|
-
code;
|
|
37
|
-
constructor(code, message) {
|
|
38
|
-
super(message);
|
|
39
|
-
this.code = code;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
exports.PipeError = PipeError;
|
|
43
|
-
/**
|
|
44
|
-
* Writes JSON to `<file>.tmp` and renames it over `<file>`, so the other side
|
|
45
|
-
* never reads a half-written file. Rename within one folder is atomic on every
|
|
46
|
-
* platform, including across the Docker bind mount.
|
|
47
|
-
*/
|
|
48
|
-
/**
|
|
49
|
-
* Creates the pipe folder owner-only, or makes an existing one so. Whoever can
|
|
50
|
-
* write a request into it can run the allowlisted host tools, so on POSIX it
|
|
51
|
-
* must be ours (not a symlink) and closed to everyone else, and its parent must
|
|
52
|
-
* not let anyone else swap it out. Throws when that cannot be made true. On
|
|
53
|
-
* Windows a folder under the user profile is private already.
|
|
54
|
-
*/
|
|
55
|
-
function ensurePrivateDir(dir) {
|
|
56
|
-
fs_1.default.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
57
|
-
if (process.platform === 'win32' || typeof process.getuid !== 'function')
|
|
58
|
-
return;
|
|
59
|
-
const uid = process.getuid();
|
|
60
|
-
const st = fs_1.default.lstatSync(dir);
|
|
61
|
-
if (!st.isDirectory())
|
|
62
|
-
throw new Error(`${dir} is not a directory (a symlink is not accepted)`);
|
|
63
|
-
if (st.uid !== uid)
|
|
64
|
-
throw new Error(`${dir} belongs to uid ${st.uid}, not to you: refusing to use it as the host pipe`);
|
|
65
|
-
// The parent: ours (then closed to writes by others), or root's and either not
|
|
66
|
-
// writable by others or sticky (like /tmp), where nobody else can rename ours.
|
|
67
|
-
const parent = path_1.default.dirname(dir);
|
|
68
|
-
const ps = fs_1.default.statSync(parent);
|
|
69
|
-
const pmode = ps.mode & 0o7777;
|
|
70
|
-
if (ps.uid === uid) {
|
|
71
|
-
if (pmode & 0o022)
|
|
72
|
-
fs_1.default.chmodSync(parent, pmode & ~0o022);
|
|
73
|
-
}
|
|
74
|
-
else if (ps.uid !== 0 || ((pmode & 0o022) && !(pmode & 0o1000))) {
|
|
75
|
-
throw new Error(`${parent} can be changed by other users: refusing to serve the host pipe below it`);
|
|
76
|
-
}
|
|
77
|
-
if ((fs_1.default.statSync(dir).mode & 0o777) !== 0o700)
|
|
78
|
-
fs_1.default.chmodSync(dir, 0o700);
|
|
79
|
-
}
|
|
80
|
-
function writeJsonAtomic(file, data) {
|
|
81
|
-
const tmp = `${file}.tmp`;
|
|
82
|
-
fs_1.default.writeFileSync(tmp, JSON.stringify(data), { mode: 0o600 });
|
|
83
|
-
fs_1.default.renameSync(tmp, file);
|
|
84
|
-
}
|
|
85
|
-
/** The heartbeat in `dir`, or null when there is none (or it is unreadable). */
|
|
86
|
-
function readHeartbeat(dir = exports.PIPE_DIR) {
|
|
87
|
-
try {
|
|
88
|
-
const hb = JSON.parse(fs_1.default.readFileSync(path_1.default.join(dir, exports.HEARTBEAT_FILE), 'utf8'));
|
|
89
|
-
return typeof hb === 'object' && hb !== null && typeof hb.pid === 'number' ? hb : null;
|
|
90
|
-
}
|
|
91
|
-
catch {
|
|
92
|
-
return null;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
/** Same rule as the gateway: online only while the heartbeat is younger than 10 s. */
|
|
96
|
-
function heartbeatFresh(hb, now = Date.now()) {
|
|
97
|
-
const updated = Date.parse(hb.updatedAt);
|
|
98
|
-
return Number.isFinite(updated) && now - updated < exports.HEARTBEAT_STALE_MS;
|
|
99
|
-
}
|
package/dist/pipe/sbx-kit.js
DELETED
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// ── Host pipe op `sbx.kit`: the Huddle CA for a Docker Sandbox ──
|
|
3
|
-
// Huddle inspects TLS (it terminates it with its own CA) for a devcontainer, and
|
|
4
|
-
// the devcontainer is set up to trust that CA. A Docker Sandbox gets the same
|
|
5
|
-
// through an sbx kit (a "mixin": `sbx create --kit <dir>`), which sbx applies to
|
|
6
|
-
// the sandbox at create:
|
|
7
|
-
// - the CA in the system trust store (install, as root);
|
|
8
|
-
// - NODE_EXTRA_CA_CERTS / SSL_CERT_FILE / REQUESTS_CA_BUNDLE, like a devcontainer,
|
|
9
|
-
// but all on the system bundle: that keeps sbx's own proxy CA, which sbx's
|
|
10
|
-
// credential proxy needs (a tool trusting only the Huddle CA breaks there);
|
|
11
|
-
// - the CA in the keystore of a JetBrains backend (the JBR does not read the
|
|
12
|
-
// system store), also for a backend Toolbox installs later.
|
|
13
|
-
// sbx only combines kits of one format, so the kit comes in the agent's: v2
|
|
14
|
-
// (spec.yaml plus files/) for sbx before 0.51, v3 (a descriptor sbx builds into
|
|
15
|
-
// an image) from 0.51 on, where the built-in agents are v3 kits.
|
|
16
|
-
// The op writes the kit to ~/.huddle/sbx/kits/huddle-ca and returns its host path.
|
|
17
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
18
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
19
|
-
};
|
|
20
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
-
exports.KIT_NAME = exports.SBX_KITS_DIR = void 0;
|
|
22
|
-
exports.sbxKit = sbxKit;
|
|
23
|
-
const fs_1 = __importDefault(require("fs"));
|
|
24
|
-
const path_1 = __importDefault(require("path"));
|
|
25
|
-
const config_1 = require("../config");
|
|
26
|
-
const protocol_1 = require("./protocol");
|
|
27
|
-
const fs_list_1 = require("./fs-list");
|
|
28
|
-
exports.SBX_KITS_DIR = path_1.default.join(config_1.CONFIG_DIR, 'sbx', 'kits');
|
|
29
|
-
exports.KIT_NAME = 'huddle-ca';
|
|
30
|
-
const PEM_RE = /^-----BEGIN CERTIFICATE-----\r?\n[A-Za-z0-9+/=\r\n]+-----END CERTIFICATE-----\r?\n?$/;
|
|
31
|
-
const MAX_PEM = 16 * 1024;
|
|
32
|
-
const BUNDLE = '/etc/ssl/certs/ca-certificates.crt';
|
|
33
|
-
// v2: in the sandbox, files/home/ is /home/agent/.
|
|
34
|
-
const V2_IN_BOX = '/home/agent/.huddle-ca';
|
|
35
|
-
const V2_SPEC = `schemaVersion: "2"
|
|
36
|
-
kind: mixin
|
|
37
|
-
name: ${exports.KIT_NAME}
|
|
38
|
-
description: Trust the Huddle CA, so Huddle can inspect this sandbox's TLS
|
|
39
|
-
environment:
|
|
40
|
-
variables:
|
|
41
|
-
NODE_EXTRA_CA_CERTS: ${BUNDLE}
|
|
42
|
-
SSL_CERT_FILE: ${BUNDLE}
|
|
43
|
-
REQUESTS_CA_BUNDLE: ${BUNDLE}
|
|
44
|
-
setup:
|
|
45
|
-
install:
|
|
46
|
-
- command: "sh ${V2_IN_BOX}/install.sh"
|
|
47
|
-
user: "0"
|
|
48
|
-
# startup takes the command as argv, install as one string (sbx's schema).
|
|
49
|
-
startup:
|
|
50
|
-
- command: ["sh", "${V2_IN_BOX}/jbr-watch.sh"]
|
|
51
|
-
`;
|
|
52
|
-
// v3: the kit is an image layer over the agent's filesystem, so it stays out of
|
|
53
|
-
// /home/agent (a layer that has that folder would take over its owner).
|
|
54
|
-
const V3_IN_BOX = '/usr/local/share/huddle-ca';
|
|
55
|
-
// Install hooks only see the variables they name.
|
|
56
|
-
const V3_SPEC = `# syntax=docker/sandbox-kit:3
|
|
57
|
-
schemaVersion: "3"
|
|
58
|
-
kind: mixin
|
|
59
|
-
displayName: Huddle CA
|
|
60
|
-
description: Trust the Huddle CA, so Huddle can inspect this sandbox's TLS
|
|
61
|
-
build: |
|
|
62
|
-
FROM scratch
|
|
63
|
-
COPY share/ ${V3_IN_BOX}/
|
|
64
|
-
ENV NODE_EXTRA_CA_CERTS=${BUNDLE} SSL_CERT_FILE=${BUNDLE} REQUESTS_CA_BUNDLE=${BUNDLE}
|
|
65
|
-
capabilities:
|
|
66
|
-
- type: com.docker.sandbox/lifecycle@1
|
|
67
|
-
config:
|
|
68
|
-
install:
|
|
69
|
-
- command: "sh ${V3_IN_BOX}/install.sh"
|
|
70
|
-
user: "0"
|
|
71
|
-
env: [PROXY_CA_CERT_B64]
|
|
72
|
-
startup:
|
|
73
|
-
- command: ["sh", "${V3_IN_BOX}/jbr-watch.sh"]
|
|
74
|
-
`;
|
|
75
|
-
const install = (inBox) => `#!/bin/sh
|
|
76
|
-
# Generated by Huddle. Runs as root when the sandbox is created.
|
|
77
|
-
set -e
|
|
78
|
-
mkdir -p /usr/local/share/ca-certificates
|
|
79
|
-
install -m 0644 ${inBox}/huddle-ca.crt /usr/local/share/ca-certificates/huddle-ca.crt
|
|
80
|
-
# sbx's own proxy CA into the same store, so the bundle the variables point at
|
|
81
|
-
# trusts both (sbx's credential proxy signs with that one).
|
|
82
|
-
if [ -n "\${PROXY_CA_CERT_B64:-}" ]; then
|
|
83
|
-
echo "$PROXY_CA_CERT_B64" | base64 -d > /usr/local/share/ca-certificates/sbx-proxy-ca.crt 2>/dev/null \\
|
|
84
|
-
&& chmod 644 /usr/local/share/ca-certificates/sbx-proxy-ca.crt \\
|
|
85
|
-
|| rm -f /usr/local/share/ca-certificates/sbx-proxy-ca.crt
|
|
86
|
-
fi
|
|
87
|
-
update-ca-certificates >/dev/null 2>&1 || true
|
|
88
|
-
echo "[huddle] CA installed"
|
|
89
|
-
`;
|
|
90
|
-
// A JetBrains backend runs on its own JVM (the JBR), which only trusts its own
|
|
91
|
-
// keystore. Toolbox installs it under ~/.cache/JetBrains/RemoteDev/dist on the
|
|
92
|
-
// first connect, so this keeps watching (in the background) and imports the CA
|
|
93
|
-
// into every backend it finds, once.
|
|
94
|
-
const jbrWatch = (inBox) => `#!/bin/sh
|
|
95
|
-
# Generated by Huddle. Runs as the agent user on every sandbox start.
|
|
96
|
-
CA=/usr/local/share/ca-certificates/huddle-ca.crt
|
|
97
|
-
[ -f "$CA" ] || CA=${inBox}/huddle-ca.crt
|
|
98
|
-
import_all() {
|
|
99
|
-
for d in "$HOME"/.cache/JetBrains/RemoteDev/dist/*/; do
|
|
100
|
-
kt="$d/jbr/bin/keytool"; ks="$d/jbr/lib/security/cacerts"
|
|
101
|
-
[ -x "$kt" ] && [ -f "$ks" ] && [ ! -f "$d/.huddle-ca" ] || continue
|
|
102
|
-
"$kt" -delete -alias huddle-ca -keystore "$ks" -storepass changeit >/dev/null 2>&1 || true
|
|
103
|
-
"$kt" -importcert -noprompt -trustcacerts -alias huddle-ca -file "$CA" -keystore "$ks" -storepass changeit >/dev/null 2>&1 \\
|
|
104
|
-
&& touch "$d/.huddle-ca"
|
|
105
|
-
done
|
|
106
|
-
}
|
|
107
|
-
import_all
|
|
108
|
-
( while sleep 30; do import_all; done ) >/dev/null 2>&1 </dev/null &
|
|
109
|
-
exit 0
|
|
110
|
-
`;
|
|
111
|
-
/** The `sbx.kit` op: `{ caPem, format? }` → `{ dir, format }`, the kit's folder as a host path. */
|
|
112
|
-
function sbxKit(args, root = exports.SBX_KITS_DIR, platform = process.platform) {
|
|
113
|
-
const { caPem, format = 2 } = args;
|
|
114
|
-
if (typeof caPem !== 'string' || caPem.length > MAX_PEM || !PEM_RE.test(caPem)) {
|
|
115
|
-
throw new protocol_1.PipeError('bad_request', 'caPem must be one PEM certificate');
|
|
116
|
-
}
|
|
117
|
-
if (format !== 2 && format !== 3)
|
|
118
|
-
throw new protocol_1.PipeError('bad_request', 'format must be 2 or 3');
|
|
119
|
-
const dir = path_1.default.join(root, exports.KIT_NAME);
|
|
120
|
-
try {
|
|
121
|
-
(0, protocol_1.ensurePrivateDir)(root);
|
|
122
|
-
// Rewritten whole: a kit left in the other format would confuse sbx.
|
|
123
|
-
fs_1.default.rmSync(dir, { recursive: true, force: true });
|
|
124
|
-
(0, protocol_1.ensurePrivateDir)(dir);
|
|
125
|
-
const inBox = format === 3 ? V3_IN_BOX : V2_IN_BOX;
|
|
126
|
-
const files = format === 3 ? path_1.default.join(dir, 'share') : path_1.default.join(dir, 'files', 'home', '.huddle-ca');
|
|
127
|
-
fs_1.default.mkdirSync(files, { recursive: true, mode: 0o700 });
|
|
128
|
-
// \n only: these are read inside the Linux sandbox, also when written on Windows.
|
|
129
|
-
const write = (file, text) => fs_1.default.writeFileSync(file, text.replace(/\r\n/g, '\n'), { mode: 0o644 });
|
|
130
|
-
if (format === 3)
|
|
131
|
-
write(path_1.default.join(dir, `${exports.KIT_NAME}.yaml`), V3_SPEC);
|
|
132
|
-
else
|
|
133
|
-
write(path_1.default.join(dir, 'spec.yaml'), V2_SPEC);
|
|
134
|
-
write(path_1.default.join(files, 'huddle-ca.crt'), caPem.endsWith('\n') ? caPem : `${caPem}\n`);
|
|
135
|
-
write(path_1.default.join(files, 'install.sh'), install(inBox));
|
|
136
|
-
write(path_1.default.join(files, 'jbr-watch.sh'), jbrWatch(inBox));
|
|
137
|
-
}
|
|
138
|
-
catch (err) {
|
|
139
|
-
throw new protocol_1.PipeError('internal', `could not write the sbx kit: ${err.message ?? err}`);
|
|
140
|
-
}
|
|
141
|
-
return { dir: (0, fs_list_1.toHuddlePath)(dir, platform), format };
|
|
142
|
-
}
|
package/dist/pipe/sbx-links.js
DELETED
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// ── Host pipe op `sbx.links`: Settings → Folder mappings for a Docker Sandbox ──
|
|
3
|
-
// sbx mounts only the folders it is given at create, at their host path. So each
|
|
4
|
-
// sandbox gets two extra folders, ~/.huddle/sbx/links/<name>/rw and /ro, with one
|
|
5
|
-
// link per folder mapping in them (a junction on Windows, which needs no admin
|
|
6
|
-
// rights or developer mode; a symlink elsewhere). The gateway passes them to
|
|
7
|
-
// `sbx create`, ro with `:ro`, and links each one to its container path inside.
|
|
8
|
-
//
|
|
9
|
-
// Tested against sbx v0.39.0 on Windows (sbx-folder-links-poc.mjs): sbx's file
|
|
10
|
-
// share follows these links on the host side, `:ro` on the ro folder holds for
|
|
11
|
-
// what is behind them too, and a link the sandbox makes in rw itself stays inside
|
|
12
|
-
// the sandbox (it never appears on the host, so it cannot reach other folders).
|
|
13
|
-
//
|
|
14
|
-
// The op only (re)builds those folders. An empty list removes the sandbox's
|
|
15
|
-
// folder. Links in it are removed, never followed.
|
|
16
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
-
};
|
|
19
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
-
exports.SBX_LINKS_DIR = void 0;
|
|
21
|
-
exports.removeNoFollow = removeNoFollow;
|
|
22
|
-
exports.sbxLinks = sbxLinks;
|
|
23
|
-
const fs_1 = __importDefault(require("fs"));
|
|
24
|
-
const path_1 = __importDefault(require("path"));
|
|
25
|
-
const config_1 = require("../config");
|
|
26
|
-
const protocol_1 = require("./protocol");
|
|
27
|
-
const fs_list_1 = require("./fs-list");
|
|
28
|
-
exports.SBX_LINKS_DIR = path_1.default.join(config_1.CONFIG_DIR, 'sbx', 'links');
|
|
29
|
-
// Same rules as the gateway (environments/sbx.ts): the name is a folder name here.
|
|
30
|
-
const SANDBOX_NAME_RE = /^[a-z0-9][a-z0-9-]{0,62}$/;
|
|
31
|
-
const LINK_ID_RE = /^[A-Za-z0-9_-]{1,64}$/;
|
|
32
|
-
const MAX_LINKS = 64;
|
|
33
|
-
/** Removes `p` without following any link in it: a link is removed itself. */
|
|
34
|
-
function removeNoFollow(p) {
|
|
35
|
-
let st;
|
|
36
|
-
try {
|
|
37
|
-
st = fs_1.default.lstatSync(p);
|
|
38
|
-
}
|
|
39
|
-
catch {
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
if (st.isSymbolicLink()) {
|
|
43
|
-
// A junction is a directory to unlink() on some Node versions.
|
|
44
|
-
try {
|
|
45
|
-
fs_1.default.unlinkSync(p);
|
|
46
|
-
}
|
|
47
|
-
catch {
|
|
48
|
-
fs_1.default.rmdirSync(p);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
else if (st.isDirectory()) {
|
|
52
|
-
for (const e of fs_1.default.readdirSync(p))
|
|
53
|
-
removeNoFollow(path_1.default.join(p, e));
|
|
54
|
-
fs_1.default.rmdirSync(p);
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
fs_1.default.unlinkSync(p);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
function inside(child, parent, platform) {
|
|
61
|
-
const norm = (s) => {
|
|
62
|
-
const h = (0, fs_list_1.toHuddlePath)(s, platform);
|
|
63
|
-
return platform === 'win32' ? h.toLowerCase() : h;
|
|
64
|
-
};
|
|
65
|
-
const c = norm(child);
|
|
66
|
-
const p = norm(parent);
|
|
67
|
-
return c === p || c.startsWith(p.endsWith('/') ? p : `${p}/`);
|
|
68
|
-
}
|
|
69
|
-
function parseLinks(raw, platform) {
|
|
70
|
-
if (!Array.isArray(raw))
|
|
71
|
-
throw new protocol_1.PipeError('bad_request', 'links must be an array');
|
|
72
|
-
if (raw.length > MAX_LINKS)
|
|
73
|
-
throw new protocol_1.PipeError('bad_request', `at most ${MAX_LINKS} links`);
|
|
74
|
-
const seen = new Set();
|
|
75
|
-
return raw.map((l) => {
|
|
76
|
-
const { id, hostPath, readOnly } = (l ?? {});
|
|
77
|
-
if (typeof id !== 'string' || !LINK_ID_RE.test(id))
|
|
78
|
-
throw new protocol_1.PipeError('bad_request', 'link id must be 1-64 letters, digits, _ or -');
|
|
79
|
-
if (seen.has(id))
|
|
80
|
-
throw new protocol_1.PipeError('bad_request', `duplicate link id ${id}`);
|
|
81
|
-
seen.add(id);
|
|
82
|
-
if (typeof hostPath !== 'string' || hostPath.includes('\0') || !(0, fs_list_1.isAbsoluteHostPath)(hostPath, platform)) {
|
|
83
|
-
throw new protocol_1.PipeError('bad_request', `link ${id}: hostPath must be an absolute path`);
|
|
84
|
-
}
|
|
85
|
-
return { id, hostPath, readOnly: readOnly === true };
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
/** The `sbx.links` op. `root` and `huddleHome` are only moved in tests. */
|
|
89
|
-
function sbxLinks(args, root = exports.SBX_LINKS_DIR, huddleHome = config_1.CONFIG_DIR, platform = process.platform) {
|
|
90
|
-
const { name } = args;
|
|
91
|
-
if (typeof name !== 'string' || !SANDBOX_NAME_RE.test(name))
|
|
92
|
-
throw new protocol_1.PipeError('bad_request', 'invalid sandbox name');
|
|
93
|
-
const links = parseLinks(args.links, platform);
|
|
94
|
-
// Resolved first: a mapping that is itself a link counts as where it points.
|
|
95
|
-
const usable = [];
|
|
96
|
-
const skipped = [];
|
|
97
|
-
for (const l of links) {
|
|
98
|
-
let target;
|
|
99
|
-
try {
|
|
100
|
-
target = fs_1.default.realpathSync(l.hostPath);
|
|
101
|
-
if (!fs_1.default.statSync(target).isDirectory()) {
|
|
102
|
-
skipped.push({ id: l.id, reason: 'not a folder' });
|
|
103
|
-
continue;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
catch {
|
|
107
|
-
skipped.push({ id: l.id, reason: 'does not exist' });
|
|
108
|
-
continue;
|
|
109
|
-
}
|
|
110
|
-
// Same rule as every mount Huddle makes (gateway host-path.ts): never
|
|
111
|
-
// ~/.huddle or inside it, and a folder around it only read-only.
|
|
112
|
-
if (inside(target, huddleHome, platform)) {
|
|
113
|
-
skipped.push({ id: l.id, reason: "is Huddle's own folder or inside it" });
|
|
114
|
-
continue;
|
|
115
|
-
}
|
|
116
|
-
if (!l.readOnly && inside(huddleHome, target, platform)) {
|
|
117
|
-
skipped.push({ id: l.id, reason: "contains Huddle's own folder, so only read-only" });
|
|
118
|
-
continue;
|
|
119
|
-
}
|
|
120
|
-
usable.push({ ...l, target });
|
|
121
|
-
}
|
|
122
|
-
const boxDir = path_1.default.join(root, name);
|
|
123
|
-
try {
|
|
124
|
-
removeNoFollow(boxDir);
|
|
125
|
-
if (usable.length === 0)
|
|
126
|
-
return { rw: null, ro: null, skipped };
|
|
127
|
-
(0, protocol_1.ensurePrivateDir)(path_1.default.dirname(root));
|
|
128
|
-
(0, protocol_1.ensurePrivateDir)(root);
|
|
129
|
-
(0, protocol_1.ensurePrivateDir)(boxDir);
|
|
130
|
-
const made = { rw: null, ro: null };
|
|
131
|
-
for (const l of usable) {
|
|
132
|
-
const kind = l.readOnly ? 'ro' : 'rw';
|
|
133
|
-
const dir = path_1.default.join(boxDir, kind);
|
|
134
|
-
if (!made[kind]) {
|
|
135
|
-
fs_1.default.mkdirSync(dir, { mode: 0o700 });
|
|
136
|
-
made[kind] = (0, fs_list_1.toHuddlePath)(dir, platform);
|
|
137
|
-
}
|
|
138
|
-
fs_1.default.symlinkSync(l.target, path_1.default.join(dir, l.id), platform === 'win32' ? 'junction' : 'dir');
|
|
139
|
-
}
|
|
140
|
-
return { ...made, skipped };
|
|
141
|
-
}
|
|
142
|
-
catch (err) {
|
|
143
|
-
if (err instanceof protocol_1.PipeError)
|
|
144
|
-
throw err;
|
|
145
|
-
throw new protocol_1.PipeError('internal', `could not prepare the folder links: ${err.message ?? err}`);
|
|
146
|
-
}
|
|
147
|
-
}
|
package/dist/pipe/serve.js
DELETED
|
@@ -1,223 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// ── Host pipe: the agent's serve loop ─────────────────────────────────────────
|
|
3
|
-
// Polls the pipe folder for `<id>.in`, claims each by renaming it to
|
|
4
|
-
// `<id>.busy` (so it is handled exactly once, even if two agents race), runs it
|
|
5
|
-
// without blocking the loop (a 10-minute `sbx create` must not hold up a folder
|
|
6
|
-
// listing), writes `<id>.out` atomically and deletes the `.busy`. Every request
|
|
7
|
-
// gets a reply, a malformed one included; nothing a request contains can crash
|
|
8
|
-
// the loop. See docs/host-pipe.md for the protocol.
|
|
9
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
10
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
11
|
-
};
|
|
12
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
-
exports.cleanStale = cleanStale;
|
|
14
|
-
exports.servePipe = servePipe;
|
|
15
|
-
const fs_1 = __importDefault(require("fs"));
|
|
16
|
-
const os_1 = __importDefault(require("os"));
|
|
17
|
-
const path_1 = __importDefault(require("path"));
|
|
18
|
-
const self_update_1 = require("../self-update");
|
|
19
|
-
const protocol_1 = require("./protocol");
|
|
20
|
-
const fs_list_1 = require("./fs-list");
|
|
21
|
-
const tools_1 = require("./tools");
|
|
22
|
-
const sbx_kit_1 = require("./sbx-kit");
|
|
23
|
-
const sbx_links_1 = require("./sbx-links");
|
|
24
|
-
const ssh_hosts_1 = require("./ssh-hosts");
|
|
25
|
-
// Ids are generated by the gateway (32 hex); anything that does not look like
|
|
26
|
-
// an id is not ours and is left alone rather than turned into a reply path.
|
|
27
|
-
const REQUEST_RE = /^([A-Za-z0-9_-]{1,128})\.in$/;
|
|
28
|
-
const LEFTOVER_RE = /\.(in|busy|out|tmp)$/;
|
|
29
|
-
async function dispatch(req, maxEntries) {
|
|
30
|
-
const args = req.args ?? {};
|
|
31
|
-
switch (req.op) {
|
|
32
|
-
case 'host.info':
|
|
33
|
-
return {
|
|
34
|
-
platform: process.platform,
|
|
35
|
-
version: (0, self_update_1.cliVersion)(),
|
|
36
|
-
home: (0, fs_list_1.toHuddlePath)(os_1.default.homedir()),
|
|
37
|
-
tools: await (0, tools_1.toolsInfo)(),
|
|
38
|
-
};
|
|
39
|
-
case 'fs.list':
|
|
40
|
-
return (0, fs_list_1.fsList)(args, maxEntries);
|
|
41
|
-
case 'fs.resolve':
|
|
42
|
-
return (0, fs_list_1.fsResolve)(args);
|
|
43
|
-
case 'tool.run':
|
|
44
|
-
return (0, tools_1.toolRun)(args);
|
|
45
|
-
case 'sbx.links':
|
|
46
|
-
return (0, sbx_links_1.sbxLinks)(args);
|
|
47
|
-
case 'sbx.kit':
|
|
48
|
-
return (0, sbx_kit_1.sbxKit)(args);
|
|
49
|
-
case 'ssh.key':
|
|
50
|
-
return (0, ssh_hosts_1.sshKey)(args);
|
|
51
|
-
case 'ssh.host':
|
|
52
|
-
return (0, ssh_hosts_1.sshHost)(args);
|
|
53
|
-
default:
|
|
54
|
-
throw new protocol_1.PipeError('unknown_op', `unknown op '${req.op}'`);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
function parseRequest(text) {
|
|
58
|
-
let req;
|
|
59
|
-
try {
|
|
60
|
-
req = JSON.parse(text);
|
|
61
|
-
}
|
|
62
|
-
catch {
|
|
63
|
-
throw new protocol_1.PipeError('bad_request', 'request is not valid JSON');
|
|
64
|
-
}
|
|
65
|
-
if (typeof req !== 'object' || req === null || Array.isArray(req))
|
|
66
|
-
throw new protocol_1.PipeError('bad_request', 'request must be a JSON object');
|
|
67
|
-
const r = req;
|
|
68
|
-
if (r.v !== protocol_1.PROTOCOL_VERSION)
|
|
69
|
-
throw new protocol_1.PipeError('bad_request', `unsupported protocol version ${JSON.stringify(r.v)} (agent speaks ${protocol_1.PROTOCOL_VERSION})`);
|
|
70
|
-
if (typeof r.op !== 'string' || !r.op)
|
|
71
|
-
throw new protocol_1.PipeError('bad_request', 'op must be a non-empty string');
|
|
72
|
-
if (r.args !== undefined && (typeof r.args !== 'object' || r.args === null || Array.isArray(r.args))) {
|
|
73
|
-
throw new protocol_1.PipeError('bad_request', 'args must be an object');
|
|
74
|
-
}
|
|
75
|
-
return r;
|
|
76
|
-
}
|
|
77
|
-
/** Deletes request/reply leftovers older than STALE_FILE_MS, skipping ids still in flight. */
|
|
78
|
-
function cleanStale(dir, inFlight = new Set(), now = Date.now()) {
|
|
79
|
-
const removed = [];
|
|
80
|
-
let names;
|
|
81
|
-
try {
|
|
82
|
-
names = fs_1.default.readdirSync(dir);
|
|
83
|
-
}
|
|
84
|
-
catch {
|
|
85
|
-
return removed;
|
|
86
|
-
}
|
|
87
|
-
for (const name of names) {
|
|
88
|
-
if (!LEFTOVER_RE.test(name) || inFlight.has(name.split('.')[0]))
|
|
89
|
-
continue;
|
|
90
|
-
const file = path_1.default.join(dir, name);
|
|
91
|
-
try {
|
|
92
|
-
if (now - fs_1.default.statSync(file).mtimeMs > protocol_1.STALE_FILE_MS) {
|
|
93
|
-
fs_1.default.unlinkSync(file);
|
|
94
|
-
removed.push(name);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
catch { /* gone already, or not ours to delete */ }
|
|
98
|
-
}
|
|
99
|
-
return removed;
|
|
100
|
-
}
|
|
101
|
-
function servePipe(opts) {
|
|
102
|
-
const { dir } = opts;
|
|
103
|
-
const maxEntries = opts.maxEntries ?? protocol_1.MAX_FOLDER_ENTRIES;
|
|
104
|
-
const log = opts.log ?? (() => { });
|
|
105
|
-
(0, protocol_1.ensurePrivateDir)(dir);
|
|
106
|
-
const startedAt = new Date().toISOString();
|
|
107
|
-
const version = (0, self_update_1.cliVersion)();
|
|
108
|
-
const inFlight = new Map();
|
|
109
|
-
let stopped = false;
|
|
110
|
-
let pollTimer;
|
|
111
|
-
let heartbeatTimer;
|
|
112
|
-
let cleanupTimer;
|
|
113
|
-
const removed = cleanStale(dir);
|
|
114
|
-
if (removed.length)
|
|
115
|
-
log(`removed ${removed.length} stale file(s)`);
|
|
116
|
-
const handle = async (id) => {
|
|
117
|
-
const busy = path_1.default.join(dir, `${id}.busy`);
|
|
118
|
-
let reply;
|
|
119
|
-
let op = '?';
|
|
120
|
-
try {
|
|
121
|
-
const req = parseRequest(await fs_1.default.promises.readFile(busy, 'utf8'));
|
|
122
|
-
op = req.op;
|
|
123
|
-
reply = { ok: true, result: await dispatch(req, maxEntries) };
|
|
124
|
-
}
|
|
125
|
-
catch (err) {
|
|
126
|
-
reply = err instanceof protocol_1.PipeError
|
|
127
|
-
? { ok: false, error: { code: err.code, message: err.message } }
|
|
128
|
-
: { ok: false, error: { code: 'internal', message: err instanceof Error ? err.message : String(err) } };
|
|
129
|
-
}
|
|
130
|
-
try {
|
|
131
|
-
(0, protocol_1.writeJsonAtomic)(path_1.default.join(dir, `${id}.out`), reply);
|
|
132
|
-
}
|
|
133
|
-
catch (err) {
|
|
134
|
-
log(`${id} ${op}: could not write reply: ${err instanceof Error ? err.message : err}`);
|
|
135
|
-
}
|
|
136
|
-
try {
|
|
137
|
-
fs_1.default.unlinkSync(busy);
|
|
138
|
-
}
|
|
139
|
-
catch { /* already gone */ }
|
|
140
|
-
log(`${id} ${op} -> ${reply.ok ? 'ok' : reply.error.code}`);
|
|
141
|
-
};
|
|
142
|
-
const poll = async () => {
|
|
143
|
-
let names = [];
|
|
144
|
-
try {
|
|
145
|
-
names = await fs_1.default.promises.readdir(dir);
|
|
146
|
-
}
|
|
147
|
-
catch {
|
|
148
|
-
// The folder went away (e.g. ~/.huddle was removed); recreate and carry on.
|
|
149
|
-
try {
|
|
150
|
-
(0, protocol_1.ensurePrivateDir)(dir);
|
|
151
|
-
}
|
|
152
|
-
catch { /* retry next tick */ }
|
|
153
|
-
}
|
|
154
|
-
for (const name of names) {
|
|
155
|
-
const m = REQUEST_RE.exec(name);
|
|
156
|
-
if (!m || stopped)
|
|
157
|
-
continue;
|
|
158
|
-
const id = m[1];
|
|
159
|
-
// Claiming is the rename: whoever renames first owns the request. A
|
|
160
|
-
// failure means someone else got it (or the gateway gave up on it).
|
|
161
|
-
try {
|
|
162
|
-
fs_1.default.renameSync(path_1.default.join(dir, name), path_1.default.join(dir, `${id}.busy`));
|
|
163
|
-
}
|
|
164
|
-
catch {
|
|
165
|
-
continue;
|
|
166
|
-
}
|
|
167
|
-
const p = handle(id).finally(() => inFlight.delete(id));
|
|
168
|
-
inFlight.set(id, p);
|
|
169
|
-
}
|
|
170
|
-
if (!stopped)
|
|
171
|
-
pollTimer = setTimeout(() => void poll(), opts.pollMs ?? protocol_1.POLL_MS);
|
|
172
|
-
};
|
|
173
|
-
const heartbeat = () => {
|
|
174
|
-
const file = path_1.default.join(dir, protocol_1.HEARTBEAT_FILE);
|
|
175
|
-
// Another agent (a newer CLI started by `huddle init`) took over: step aside
|
|
176
|
-
// rather than fight over host.json every two seconds.
|
|
177
|
-
const current = (0, protocol_1.readHeartbeat)(dir);
|
|
178
|
-
if (current && current.pid !== process.pid && current.startedAt > startedAt) {
|
|
179
|
-
log(`replaced by pid ${current.pid}; stopping`);
|
|
180
|
-
void stop().then(() => opts.onReplaced?.());
|
|
181
|
-
return;
|
|
182
|
-
}
|
|
183
|
-
const hb = {
|
|
184
|
-
v: protocol_1.PROTOCOL_VERSION,
|
|
185
|
-
pid: process.pid,
|
|
186
|
-
version,
|
|
187
|
-
platform: process.platform,
|
|
188
|
-
tools: (0, tools_1.availableTools)(),
|
|
189
|
-
startedAt,
|
|
190
|
-
updatedAt: new Date().toISOString(),
|
|
191
|
-
};
|
|
192
|
-
try {
|
|
193
|
-
(0, protocol_1.writeJsonAtomic)(file, hb);
|
|
194
|
-
}
|
|
195
|
-
catch (err) {
|
|
196
|
-
log(`could not write heartbeat: ${err instanceof Error ? err.message : err}`);
|
|
197
|
-
}
|
|
198
|
-
};
|
|
199
|
-
heartbeatTimer = setInterval(heartbeat, opts.heartbeatMs ?? protocol_1.HEARTBEAT_MS);
|
|
200
|
-
heartbeat();
|
|
201
|
-
// Orphaned replies (the gateway timed out and never collected them) would
|
|
202
|
-
// otherwise pile up for as long as the agent runs.
|
|
203
|
-
cleanupTimer = setInterval(() => cleanStale(dir, new Set(inFlight.keys())), protocol_1.STALE_FILE_MS);
|
|
204
|
-
cleanupTimer.unref();
|
|
205
|
-
void poll();
|
|
206
|
-
async function stop() {
|
|
207
|
-
if (!stopped) {
|
|
208
|
-
stopped = true;
|
|
209
|
-
clearTimeout(pollTimer);
|
|
210
|
-
clearInterval(heartbeatTimer);
|
|
211
|
-
clearInterval(cleanupTimer);
|
|
212
|
-
const current = (0, protocol_1.readHeartbeat)(dir);
|
|
213
|
-
if (current && current.pid === process.pid && current.startedAt === startedAt) {
|
|
214
|
-
try {
|
|
215
|
-
fs_1.default.unlinkSync(path_1.default.join(dir, protocol_1.HEARTBEAT_FILE));
|
|
216
|
-
}
|
|
217
|
-
catch { /* already gone */ }
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
await Promise.allSettled([...inFlight.values()]);
|
|
221
|
-
}
|
|
222
|
-
return { stop };
|
|
223
|
-
}
|