aicomputer 0.1.18 → 0.1.20
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 +28 -0
- package/dist/{chunk-3ZUTAUUD.js → chunk-36BZXAAX.js} +129 -258
- package/dist/chunk-3ZF7JRBW.js +270 -0
- package/dist/chunk-4TE5XTYE.js +242 -0
- package/dist/chunk-5Y2NWK5I.js +14 -0
- package/dist/chunk-D3SAFNSI.js +75 -0
- package/dist/chunk-GD42GHW3.js +183 -0
- package/dist/chunk-GGBVVRLL.js +32 -0
- package/dist/chunk-LOGK7YYJ.js +255 -0
- package/dist/{chunk-F2U4SFJ4.js → chunk-TPFE3CC6.js} +24 -174
- package/dist/index.js +678 -430
- package/dist/lib/autossh-runtime.d.ts +21 -0
- package/dist/lib/autossh-runtime.js +23 -0
- package/dist/lib/mount-config.d.ts +5 -1
- package/dist/lib/mount-mutagen.d.ts +2 -0
- package/dist/lib/mount-mutagen.js +3 -1
- package/dist/lib/mount-reconcile.js +5 -2
- package/dist/lib/mutagen-runtime.d.ts +20 -0
- package/dist/lib/mutagen-runtime.js +19 -0
- package/dist/lib/ssh-access.d.ts +74 -0
- package/dist/lib/ssh-access.js +25 -0
- package/dist/lib/ssh-config.d.ts +14 -0
- package/dist/lib/ssh-config.js +10 -0
- package/dist/lib/upgrade-version.d.ts +10 -0
- package/dist/lib/upgrade-version.js +8 -0
- package/package.json +3 -3
- package/scripts/postinstall.mjs +36 -23
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import {
|
|
2
|
+
resolveSystemCommandPath
|
|
3
|
+
} from "./chunk-5Y2NWK5I.js";
|
|
4
|
+
|
|
5
|
+
// src/lib/autossh-runtime.ts
|
|
6
|
+
import { spawnSync } from "child_process";
|
|
7
|
+
import { createHash } from "crypto";
|
|
8
|
+
import {
|
|
9
|
+
chmodSync,
|
|
10
|
+
existsSync,
|
|
11
|
+
mkdirSync,
|
|
12
|
+
readdirSync,
|
|
13
|
+
renameSync,
|
|
14
|
+
rmSync
|
|
15
|
+
} from "fs";
|
|
16
|
+
import { writeFile } from "fs/promises";
|
|
17
|
+
import { homedir } from "os";
|
|
18
|
+
import { dirname, join } from "path";
|
|
19
|
+
var BUNDLED_AUTOSSH_VERSION = "1.4f";
|
|
20
|
+
var BUNDLED_AUTOSSH_DOWNLOAD_BASE = `https://github.com/Autossh/autossh/archive/refs/tags/v${BUNDLED_AUTOSSH_VERSION}.tar.gz`;
|
|
21
|
+
var BUNDLED_AUTOSSH_ARCHIVE_SHA256 = "fce67ee6e8e032eb41eca984f7e16a6ea74f85c513d94f78e56656815c473604";
|
|
22
|
+
var AGENTCOMPUTER_AUTOSSH_PATH_ENV = "AGENTCOMPUTER_AUTOSSH_PATH";
|
|
23
|
+
var AUTOSSH_SSH_PATH_ENV = "AUTOSSH_PATH";
|
|
24
|
+
function getBundledAutosshAsset(platform = process.platform, homeDirectory = homedir()) {
|
|
25
|
+
if (!isSupportedPlatform(platform)) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
const installDir = join(
|
|
29
|
+
homeDirectory,
|
|
30
|
+
".agentcomputer",
|
|
31
|
+
"tools",
|
|
32
|
+
"autossh",
|
|
33
|
+
`v${BUNDLED_AUTOSSH_VERSION}`,
|
|
34
|
+
platform
|
|
35
|
+
);
|
|
36
|
+
return {
|
|
37
|
+
platform,
|
|
38
|
+
version: BUNDLED_AUTOSSH_VERSION,
|
|
39
|
+
archiveName: `autossh-v${BUNDLED_AUTOSSH_VERSION}.tar.gz`,
|
|
40
|
+
downloadUrl: BUNDLED_AUTOSSH_DOWNLOAD_BASE,
|
|
41
|
+
archiveSha256: BUNDLED_AUTOSSH_ARCHIVE_SHA256,
|
|
42
|
+
installDir,
|
|
43
|
+
executablePath: join(installDir, "bin", "autossh")
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function hasBundledAutossh() {
|
|
47
|
+
const asset = getBundledAutosshAsset();
|
|
48
|
+
return asset ? existsSync(asset.executablePath) : false;
|
|
49
|
+
}
|
|
50
|
+
async function ensureAutosshCommandPath(env = process.env) {
|
|
51
|
+
const resolution = lookupAutosshCommandPath(env);
|
|
52
|
+
if (resolution.commandPath) {
|
|
53
|
+
return resolution.commandPath;
|
|
54
|
+
}
|
|
55
|
+
if (!resolution.asset) {
|
|
56
|
+
throw new Error(
|
|
57
|
+
`Agent Computer does not ship bundled autossh for ${process.platform}. Install autossh manually and rerun \`computer ssh\`.`
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
try {
|
|
61
|
+
return await installBundledAutossh(resolution.asset);
|
|
62
|
+
} catch (error) {
|
|
63
|
+
const reason = error instanceof Error ? error.message : "unknown autossh install failure";
|
|
64
|
+
throw new Error(
|
|
65
|
+
`Failed to install Agent Computer's bundled autossh (${reason}). Check your network connection and local build tools, then rerun \`computer ssh\`.`
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
async function ensureBundledAutosshInstalled() {
|
|
70
|
+
const asset = getBundledAutosshAsset();
|
|
71
|
+
if (!asset) {
|
|
72
|
+
throw new Error(
|
|
73
|
+
`Agent Computer does not ship bundled autossh for ${process.platform}.`
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
return installBundledAutossh(asset);
|
|
77
|
+
}
|
|
78
|
+
function resolveAutosshCommandPath(env = process.env) {
|
|
79
|
+
const resolution = lookupAutosshCommandPath(env);
|
|
80
|
+
if (resolution.commandPath) {
|
|
81
|
+
return resolution.commandPath;
|
|
82
|
+
}
|
|
83
|
+
if (!resolution.asset) {
|
|
84
|
+
throw new Error(
|
|
85
|
+
`Agent Computer does not ship bundled autossh for ${process.platform}. Install autossh manually and rerun \`computer ssh\`.`
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
throw new Error(
|
|
89
|
+
"autossh is not installed yet. Re-run `computer ssh` and Agent Computer will install its bundled copy."
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
function resolveAutosshSSHCommandPath(env = process.env) {
|
|
93
|
+
const overridden = env[AUTOSSH_SSH_PATH_ENV]?.trim();
|
|
94
|
+
if (overridden) {
|
|
95
|
+
return overridden;
|
|
96
|
+
}
|
|
97
|
+
const systemPath = resolveSystemCommandPath("ssh", env);
|
|
98
|
+
if (systemPath) {
|
|
99
|
+
return systemPath;
|
|
100
|
+
}
|
|
101
|
+
throw new Error(getMissingOpenSSHClientMessage());
|
|
102
|
+
}
|
|
103
|
+
function getMissingOpenSSHClientMessage(platform = process.platform) {
|
|
104
|
+
switch (platform) {
|
|
105
|
+
case "darwin":
|
|
106
|
+
return "OpenSSH client tools are required for `computer ssh`. Install the macOS command line tools or make sure `ssh` is available on PATH, then rerun the command.";
|
|
107
|
+
case "linux":
|
|
108
|
+
return "OpenSSH client tools are required for `computer ssh`. Install `ssh` and `scp` with your package manager, then rerun the command.";
|
|
109
|
+
default:
|
|
110
|
+
return "OpenSSH client tools are required for `computer ssh`. Install `ssh` and `scp`, then rerun the command.";
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
async function installBundledAutossh(asset) {
|
|
114
|
+
if (existsSync(asset.executablePath)) {
|
|
115
|
+
return asset.executablePath;
|
|
116
|
+
}
|
|
117
|
+
const sshPath = resolveSystemCommandPath("ssh");
|
|
118
|
+
if (!sshPath) {
|
|
119
|
+
throw new Error("OpenSSH client (`ssh`) is required before autossh can be provisioned");
|
|
120
|
+
}
|
|
121
|
+
if (!resolveSystemCommandPath("make")) {
|
|
122
|
+
throw new Error("`make` is required to build bundled autossh");
|
|
123
|
+
}
|
|
124
|
+
const ccPath = resolveSystemCommandPath("cc") ?? resolveSystemCommandPath("clang");
|
|
125
|
+
if (!ccPath) {
|
|
126
|
+
throw new Error("a C compiler is required to build bundled autossh");
|
|
127
|
+
}
|
|
128
|
+
if (!resolveSystemCommandPath("tar")) {
|
|
129
|
+
throw new Error("`tar` is required to extract bundled autossh");
|
|
130
|
+
}
|
|
131
|
+
mkdirSync(dirname(asset.installDir), { recursive: true });
|
|
132
|
+
const stagingDir = `${asset.installDir}.staging-${process.pid}-${Date.now()}`;
|
|
133
|
+
const archivePath = join(stagingDir, asset.archiveName);
|
|
134
|
+
const prefixDir = join(stagingDir, "prefix");
|
|
135
|
+
rmSync(stagingDir, { recursive: true, force: true });
|
|
136
|
+
if (existsSync(asset.installDir) && !existsSync(asset.executablePath)) {
|
|
137
|
+
rmSync(asset.installDir, { recursive: true, force: true });
|
|
138
|
+
}
|
|
139
|
+
mkdirSync(stagingDir, { recursive: true });
|
|
140
|
+
try {
|
|
141
|
+
const response = await fetch(asset.downloadUrl, {
|
|
142
|
+
headers: {
|
|
143
|
+
"User-Agent": "aicomputer-cli"
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
if (!response.ok || !response.body) {
|
|
147
|
+
throw new Error(`download failed with status ${response.status}`);
|
|
148
|
+
}
|
|
149
|
+
const archiveBuffer = Buffer.from(await response.arrayBuffer());
|
|
150
|
+
verifyAutosshArchiveChecksum(asset, archiveBuffer);
|
|
151
|
+
await writeFile(archivePath, archiveBuffer, {
|
|
152
|
+
mode: 384
|
|
153
|
+
});
|
|
154
|
+
const extract = spawnSync("tar", ["-xzf", archivePath, "-C", stagingDir], {
|
|
155
|
+
encoding: "utf8"
|
|
156
|
+
});
|
|
157
|
+
if (extract.status !== 0) {
|
|
158
|
+
throw new Error(
|
|
159
|
+
extract.stderr.trim() || extract.stdout.trim() || `failed to extract ${asset.archiveName}`
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
const sourceDir = findExtractedSourceDir(stagingDir);
|
|
163
|
+
if (!sourceDir) {
|
|
164
|
+
throw new Error("downloaded autossh archive did not contain source files");
|
|
165
|
+
}
|
|
166
|
+
const configure = spawnSync(
|
|
167
|
+
"./configure",
|
|
168
|
+
[`--prefix=${prefixDir}`, `--with-ssh=${sshPath}`],
|
|
169
|
+
{
|
|
170
|
+
cwd: sourceDir,
|
|
171
|
+
encoding: "utf8",
|
|
172
|
+
env: {
|
|
173
|
+
...process.env,
|
|
174
|
+
CC: ccPath
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
);
|
|
178
|
+
if (configure.status !== 0) {
|
|
179
|
+
throw new Error(
|
|
180
|
+
configure.stderr.trim() || configure.stdout.trim() || "autossh configure failed"
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
const make = spawnSync("make", [], {
|
|
184
|
+
cwd: sourceDir,
|
|
185
|
+
encoding: "utf8",
|
|
186
|
+
env: {
|
|
187
|
+
...process.env,
|
|
188
|
+
CC: ccPath
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
if (make.status !== 0) {
|
|
192
|
+
throw new Error(make.stderr.trim() || make.stdout.trim() || "autossh build failed");
|
|
193
|
+
}
|
|
194
|
+
const install = spawnSync("make", ["install"], {
|
|
195
|
+
cwd: sourceDir,
|
|
196
|
+
encoding: "utf8",
|
|
197
|
+
env: {
|
|
198
|
+
...process.env,
|
|
199
|
+
CC: ccPath
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
if (install.status !== 0) {
|
|
203
|
+
throw new Error(
|
|
204
|
+
install.stderr.trim() || install.stdout.trim() || "autossh install failed"
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
if (!existsSync(join(prefixDir, "bin", "autossh"))) {
|
|
208
|
+
throw new Error("autossh install did not produce the executable");
|
|
209
|
+
}
|
|
210
|
+
chmodSync(join(prefixDir, "bin", "autossh"), 493);
|
|
211
|
+
rmSync(archivePath, { force: true });
|
|
212
|
+
try {
|
|
213
|
+
renameSync(prefixDir, asset.installDir);
|
|
214
|
+
} catch (error) {
|
|
215
|
+
if (!existsSync(asset.executablePath)) {
|
|
216
|
+
throw error;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
rmSync(stagingDir, { recursive: true, force: true });
|
|
220
|
+
return asset.executablePath;
|
|
221
|
+
} catch (error) {
|
|
222
|
+
rmSync(stagingDir, { recursive: true, force: true });
|
|
223
|
+
throw error;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
function lookupAutosshCommandPath(env) {
|
|
227
|
+
const overridden = env[AGENTCOMPUTER_AUTOSSH_PATH_ENV]?.trim();
|
|
228
|
+
const asset = getBundledAutosshAsset();
|
|
229
|
+
if (overridden) {
|
|
230
|
+
return { asset, commandPath: overridden };
|
|
231
|
+
}
|
|
232
|
+
if (asset && existsSync(asset.executablePath)) {
|
|
233
|
+
return { asset, commandPath: asset.executablePath };
|
|
234
|
+
}
|
|
235
|
+
return {
|
|
236
|
+
asset,
|
|
237
|
+
commandPath: resolveSystemCommandPath("autossh", env)
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
function verifyAutosshArchiveChecksum(asset, archiveBuffer) {
|
|
241
|
+
const digest = createHash("sha256").update(archiveBuffer).digest("hex");
|
|
242
|
+
if (digest !== asset.archiveSha256) {
|
|
243
|
+
throw new Error(
|
|
244
|
+
`autossh archive checksum mismatch (expected ${asset.archiveSha256}, got ${digest})`
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
function findExtractedSourceDir(stagingDir) {
|
|
249
|
+
for (const entry of readdirSync(stagingDir, { withFileTypes: true })) {
|
|
250
|
+
if (entry.isDirectory() && entry.name !== "prefix") {
|
|
251
|
+
return join(stagingDir, entry.name);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return null;
|
|
255
|
+
}
|
|
256
|
+
function isSupportedPlatform(platform) {
|
|
257
|
+
return platform === "darwin" || platform === "linux";
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export {
|
|
261
|
+
AGENTCOMPUTER_AUTOSSH_PATH_ENV,
|
|
262
|
+
AUTOSSH_SSH_PATH_ENV,
|
|
263
|
+
getBundledAutosshAsset,
|
|
264
|
+
hasBundledAutossh,
|
|
265
|
+
ensureAutosshCommandPath,
|
|
266
|
+
ensureBundledAutosshInstalled,
|
|
267
|
+
resolveAutosshCommandPath,
|
|
268
|
+
resolveAutosshSSHCommandPath,
|
|
269
|
+
getMissingOpenSSHClientMessage
|
|
270
|
+
};
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AUTOSSH_SSH_PATH_ENV,
|
|
3
|
+
ensureAutosshCommandPath,
|
|
4
|
+
resolveAutosshSSHCommandPath
|
|
5
|
+
} from "./chunk-3ZF7JRBW.js";
|
|
6
|
+
import {
|
|
7
|
+
api,
|
|
8
|
+
getConnectionInfo,
|
|
9
|
+
resolveComputer
|
|
10
|
+
} from "./chunk-LOGK7YYJ.js";
|
|
11
|
+
|
|
12
|
+
// src/lib/ssh-access.ts
|
|
13
|
+
import { spawn } from "child_process";
|
|
14
|
+
import { basename as basename2 } from "path";
|
|
15
|
+
|
|
16
|
+
// src/lib/ssh-keys.ts
|
|
17
|
+
import { basename } from "path";
|
|
18
|
+
import { homedir } from "os";
|
|
19
|
+
import { readFile, mkdir } from "fs/promises";
|
|
20
|
+
import { execFileSync } from "child_process";
|
|
21
|
+
import { existsSync } from "fs";
|
|
22
|
+
var DEFAULT_PUBLIC_KEY_PATHS = [
|
|
23
|
+
`${homedir()}/.ssh/id_ed25519.pub`,
|
|
24
|
+
`${homedir()}/.ssh/id_ecdsa.pub`,
|
|
25
|
+
`${homedir()}/.ssh/id_rsa.pub`
|
|
26
|
+
];
|
|
27
|
+
async function ensureDefaultSSHKeyRegistered() {
|
|
28
|
+
for (const path of DEFAULT_PUBLIC_KEY_PATHS) {
|
|
29
|
+
try {
|
|
30
|
+
const publicKey2 = (await readFile(path, "utf8")).trim();
|
|
31
|
+
if (!publicKey2) {
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
const key2 = await api("/v1/ssh-keys", {
|
|
35
|
+
method: "POST",
|
|
36
|
+
body: JSON.stringify({
|
|
37
|
+
name: basename(path),
|
|
38
|
+
public_key: publicKey2
|
|
39
|
+
})
|
|
40
|
+
});
|
|
41
|
+
return {
|
|
42
|
+
key: key2,
|
|
43
|
+
publicKeyPath: path,
|
|
44
|
+
privateKeyPath: path.replace(/\.pub$/, "")
|
|
45
|
+
};
|
|
46
|
+
} catch (error) {
|
|
47
|
+
if (error?.code === "ENOENT") {
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
throw error;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const generated = await generateSSHKey();
|
|
54
|
+
const publicKey = (await readFile(generated.publicKeyPath, "utf8")).trim();
|
|
55
|
+
const key = await api("/v1/ssh-keys", {
|
|
56
|
+
method: "POST",
|
|
57
|
+
body: JSON.stringify({
|
|
58
|
+
name: basename(generated.publicKeyPath),
|
|
59
|
+
public_key: publicKey
|
|
60
|
+
})
|
|
61
|
+
});
|
|
62
|
+
return {
|
|
63
|
+
key,
|
|
64
|
+
publicKeyPath: generated.publicKeyPath,
|
|
65
|
+
privateKeyPath: generated.privateKeyPath
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
async function generateSSHKey() {
|
|
69
|
+
const sshDir = `${homedir()}/.ssh`;
|
|
70
|
+
if (!existsSync(sshDir)) {
|
|
71
|
+
await mkdir(sshDir, { mode: 448 });
|
|
72
|
+
}
|
|
73
|
+
const privateKeyPath = `${sshDir}/id_ed25519`;
|
|
74
|
+
const publicKeyPath = `${privateKeyPath}.pub`;
|
|
75
|
+
console.log("No SSH key found \u2014 generating one at", publicKeyPath);
|
|
76
|
+
execFileSync("ssh-keygen", ["-t", "ed25519", "-f", privateKeyPath, "-N", ""], {
|
|
77
|
+
stdio: "inherit"
|
|
78
|
+
});
|
|
79
|
+
return { publicKeyPath, privateKeyPath };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// src/lib/ssh-access.ts
|
|
83
|
+
var AUTOSSH_BINARY = "autossh";
|
|
84
|
+
var AUTOSSH_MONITOR_PORT = "0";
|
|
85
|
+
var TMUX_SESSION_NAME = "agentcomputer";
|
|
86
|
+
var TMUX_INCOMPATIBLE_SSH_ARGS = /* @__PURE__ */ new Set(["-N", "-T", "-f"]);
|
|
87
|
+
var SSH_SERVER_ALIVE_INTERVAL_SECONDS = 30;
|
|
88
|
+
var SSH_SERVER_ALIVE_COUNT_MAX = 3;
|
|
89
|
+
async function prepareSSHConnection(computer, options = {}) {
|
|
90
|
+
const registered = await ensureDefaultSSHKeyRegistered();
|
|
91
|
+
const info = await getConnectionInfo(computer.id);
|
|
92
|
+
if (!info.connection.ssh_available) {
|
|
93
|
+
throw new Error("SSH is not available for this computer");
|
|
94
|
+
}
|
|
95
|
+
const launchPlan = buildSSHLaunchPlan({
|
|
96
|
+
user: info.connection.ssh_user,
|
|
97
|
+
host: info.connection.ssh_host,
|
|
98
|
+
port: info.connection.ssh_port,
|
|
99
|
+
identityFilePath: registered.privateKeyPath,
|
|
100
|
+
commandPath: await ensureAutosshCommandPath(),
|
|
101
|
+
sshCommandPath: resolveAutosshSSHCommandPath(),
|
|
102
|
+
extraArgs: options.extraArgs,
|
|
103
|
+
tmux: options.tmux
|
|
104
|
+
});
|
|
105
|
+
return {
|
|
106
|
+
computer,
|
|
107
|
+
...launchPlan
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
async function prepareSSHConnectionByIdentifier(identifier, options = {}) {
|
|
111
|
+
const computer = await resolveComputer(identifier);
|
|
112
|
+
return prepareSSHConnection(computer, options);
|
|
113
|
+
}
|
|
114
|
+
async function openSSHConnection(connection) {
|
|
115
|
+
await new Promise((resolve, reject) => {
|
|
116
|
+
const child = spawn(connection.commandPath, connection.args, {
|
|
117
|
+
env: {
|
|
118
|
+
...process.env,
|
|
119
|
+
...connection.env
|
|
120
|
+
},
|
|
121
|
+
stdio: "inherit"
|
|
122
|
+
});
|
|
123
|
+
child.on("error", reject);
|
|
124
|
+
child.on("exit", (code) => {
|
|
125
|
+
if (code === 0) {
|
|
126
|
+
resolve();
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
reject(
|
|
130
|
+
new Error(
|
|
131
|
+
`${basename2(connection.commandPath)} exited with code ${code ?? 1}`
|
|
132
|
+
)
|
|
133
|
+
);
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
function buildSSHLaunchPlan(input) {
|
|
138
|
+
const user = input.user.trim();
|
|
139
|
+
const host = input.host.trim();
|
|
140
|
+
const port = input.port;
|
|
141
|
+
const identityFilePath = input.identityFilePath.trim();
|
|
142
|
+
const extraArgs = [...input.extraArgs ?? []];
|
|
143
|
+
const tmux = input.tmux === true;
|
|
144
|
+
if (!user || !host) {
|
|
145
|
+
throw new Error("ssh is unavailable");
|
|
146
|
+
}
|
|
147
|
+
if (!identityFilePath) {
|
|
148
|
+
throw new Error("ssh identity file is required");
|
|
149
|
+
}
|
|
150
|
+
if (tmux) {
|
|
151
|
+
validateTmuxSSHArgs(extraArgs);
|
|
152
|
+
}
|
|
153
|
+
const commandPath = input.commandPath?.trim() || AUTOSSH_BINARY;
|
|
154
|
+
const env = {};
|
|
155
|
+
if (input.sshCommandPath?.trim()) {
|
|
156
|
+
env[AUTOSSH_SSH_PATH_ENV] = input.sshCommandPath.trim();
|
|
157
|
+
}
|
|
158
|
+
return {
|
|
159
|
+
commandPath,
|
|
160
|
+
command: formatSSHCommand(user, host, port, extraArgs, { tmux }),
|
|
161
|
+
args: [
|
|
162
|
+
"-M",
|
|
163
|
+
AUTOSSH_MONITOR_PORT,
|
|
164
|
+
...getSSHResilienceOptionArgs(),
|
|
165
|
+
"-i",
|
|
166
|
+
identityFilePath,
|
|
167
|
+
"-p",
|
|
168
|
+
String(port),
|
|
169
|
+
...tmux ? ["-t"] : [],
|
|
170
|
+
...extraArgs,
|
|
171
|
+
`${user}@${host}`,
|
|
172
|
+
...tmux ? getTmuxRemoteCommandArgs() : []
|
|
173
|
+
],
|
|
174
|
+
env
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
function getSSHResilienceOptionArgs() {
|
|
178
|
+
return [
|
|
179
|
+
"-o",
|
|
180
|
+
`ServerAliveInterval=${SSH_SERVER_ALIVE_INTERVAL_SECONDS}`,
|
|
181
|
+
"-o",
|
|
182
|
+
`ServerAliveCountMax=${SSH_SERVER_ALIVE_COUNT_MAX}`,
|
|
183
|
+
"-o",
|
|
184
|
+
"ExitOnForwardFailure=yes"
|
|
185
|
+
];
|
|
186
|
+
}
|
|
187
|
+
function getSSHResilienceConfigLines() {
|
|
188
|
+
return [
|
|
189
|
+
`ServerAliveInterval ${SSH_SERVER_ALIVE_INTERVAL_SECONDS}`,
|
|
190
|
+
`ServerAliveCountMax ${SSH_SERVER_ALIVE_COUNT_MAX}`,
|
|
191
|
+
"ExitOnForwardFailure yes"
|
|
192
|
+
];
|
|
193
|
+
}
|
|
194
|
+
function formatSSHCommand(user, host, port, extraArgs = [], options = {}) {
|
|
195
|
+
if (!user.trim() || !host.trim()) {
|
|
196
|
+
return "ssh unavailable";
|
|
197
|
+
}
|
|
198
|
+
const parts = [AUTOSSH_BINARY, "-M", AUTOSSH_MONITOR_PORT];
|
|
199
|
+
if (port > 0 && port !== 22) {
|
|
200
|
+
parts.push("-p", String(port));
|
|
201
|
+
}
|
|
202
|
+
if (options.tmux) {
|
|
203
|
+
parts.push("-t");
|
|
204
|
+
}
|
|
205
|
+
parts.push(...extraArgs, `${user}@${host}`);
|
|
206
|
+
if (options.tmux) {
|
|
207
|
+
parts.push(...getTmuxRemoteCommandArgs());
|
|
208
|
+
}
|
|
209
|
+
return parts.map(formatShellDisplayArg).join(" ");
|
|
210
|
+
}
|
|
211
|
+
function getTmuxRemoteCommandArgs() {
|
|
212
|
+
return ["tmux", "new-session", "-A", "-s", TMUX_SESSION_NAME];
|
|
213
|
+
}
|
|
214
|
+
function validateTmuxSSHArgs(extraArgs) {
|
|
215
|
+
const incompatibleArg = extraArgs.find(
|
|
216
|
+
(arg) => TMUX_INCOMPATIBLE_SSH_ARGS.has(arg)
|
|
217
|
+
);
|
|
218
|
+
if (incompatibleArg) {
|
|
219
|
+
throw new Error(
|
|
220
|
+
`--tmux cannot be combined with ${incompatibleArg} because tmux attach mode requires an interactive shell.`
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
function formatShellDisplayArg(value) {
|
|
225
|
+
if (/^[A-Za-z0-9_@%+=:,./-]+$/.test(value)) {
|
|
226
|
+
return value;
|
|
227
|
+
}
|
|
228
|
+
return JSON.stringify(value);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export {
|
|
232
|
+
ensureDefaultSSHKeyRegistered,
|
|
233
|
+
SSH_SERVER_ALIVE_INTERVAL_SECONDS,
|
|
234
|
+
SSH_SERVER_ALIVE_COUNT_MAX,
|
|
235
|
+
prepareSSHConnection,
|
|
236
|
+
prepareSSHConnectionByIdentifier,
|
|
237
|
+
openSSHConnection,
|
|
238
|
+
buildSSHLaunchPlan,
|
|
239
|
+
getSSHResilienceOptionArgs,
|
|
240
|
+
getSSHResilienceConfigLines,
|
|
241
|
+
formatSSHCommand
|
|
242
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// src/lib/system-tools.ts
|
|
2
|
+
import { spawnSync } from "child_process";
|
|
3
|
+
function resolveSystemCommandPath(command, env = process.env) {
|
|
4
|
+
const result = spawnSync("which", [command], { encoding: "utf8", env });
|
|
5
|
+
if (result.status !== 0) {
|
|
6
|
+
return null;
|
|
7
|
+
}
|
|
8
|
+
const resolved = result.stdout.trim();
|
|
9
|
+
return resolved.length > 0 ? resolved : null;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export {
|
|
13
|
+
resolveSystemCommandPath
|
|
14
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getSSHResilienceConfigLines
|
|
3
|
+
} from "./chunk-4TE5XTYE.js";
|
|
4
|
+
|
|
5
|
+
// src/lib/ssh-config.ts
|
|
6
|
+
import { homedir } from "os";
|
|
7
|
+
import { join } from "path";
|
|
8
|
+
import { mkdir, readFile, writeFile } from "fs/promises";
|
|
9
|
+
var MANAGED_BLOCK_START = "# >>> agentcomputer ssh setup >>>";
|
|
10
|
+
var MANAGED_BLOCK_END = "# <<< agentcomputer ssh setup <<<";
|
|
11
|
+
async function ensureSSHAliasConfig(options) {
|
|
12
|
+
const sshDir = join(homedir(), ".ssh");
|
|
13
|
+
const configPath = join(sshDir, "config");
|
|
14
|
+
await mkdir(sshDir, { recursive: true, mode: 448 });
|
|
15
|
+
let existing = "";
|
|
16
|
+
try {
|
|
17
|
+
existing = await readFile(configPath, "utf8");
|
|
18
|
+
} catch (error) {
|
|
19
|
+
if (error.code !== "ENOENT") {
|
|
20
|
+
throw error;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
const nextBlock = renderManagedBlock(options);
|
|
24
|
+
const managedBlockPattern = new RegExp(
|
|
25
|
+
`${escapeRegex(MANAGED_BLOCK_START)}[\\s\\S]*?${escapeRegex(MANAGED_BLOCK_END)}\\n?`,
|
|
26
|
+
"m"
|
|
27
|
+
);
|
|
28
|
+
let nextContents;
|
|
29
|
+
if (managedBlockPattern.test(existing)) {
|
|
30
|
+
nextContents = existing.replace(managedBlockPattern, nextBlock);
|
|
31
|
+
} else {
|
|
32
|
+
const normalized = existing.length === 0 ? "" : existing.endsWith("\n") ? existing : `${existing}
|
|
33
|
+
`;
|
|
34
|
+
nextContents = normalized.length === 0 ? nextBlock : `${normalized}
|
|
35
|
+
${nextBlock}`;
|
|
36
|
+
}
|
|
37
|
+
const changed = nextContents !== existing;
|
|
38
|
+
if (changed) {
|
|
39
|
+
await writeFile(configPath, nextContents, { mode: 384 });
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
configPath,
|
|
43
|
+
changed
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function renderManagedBlock(options) {
|
|
47
|
+
const user = options.user?.trim() || "agentcomputer";
|
|
48
|
+
const identityFile = formatIdentityFilePath(options.identityFilePath);
|
|
49
|
+
const resilienceLines = getSSHResilienceConfigLines().map((line) => ` ${line}`).join("\n");
|
|
50
|
+
return `${MANAGED_BLOCK_START}
|
|
51
|
+
Host ${options.alias}
|
|
52
|
+
HostName ${options.host}
|
|
53
|
+
Port ${options.port}
|
|
54
|
+
User ${user}
|
|
55
|
+
IdentityFile ${identityFile}
|
|
56
|
+
IdentitiesOnly yes
|
|
57
|
+
${resilienceLines}
|
|
58
|
+
${MANAGED_BLOCK_END}
|
|
59
|
+
`;
|
|
60
|
+
}
|
|
61
|
+
function formatIdentityFilePath(path) {
|
|
62
|
+
const normalized = path.trim();
|
|
63
|
+
const homePath = `${homedir()}/`;
|
|
64
|
+
if (normalized.startsWith(homePath)) {
|
|
65
|
+
return `~/${normalized.slice(homePath.length)}`;
|
|
66
|
+
}
|
|
67
|
+
return normalized.replaceAll(" ", "\\ ");
|
|
68
|
+
}
|
|
69
|
+
function escapeRegex(value) {
|
|
70
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export {
|
|
74
|
+
ensureSSHAliasConfig
|
|
75
|
+
};
|