@linkshell/host 0.1.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/LICENSE +21 -0
- package/README.md +11 -0
- package/dist/host/src/drivers/acp/connection.d.ts +61 -0
- package/dist/host/src/drivers/acp/connection.js +121 -0
- package/dist/host/src/drivers/acp/connection.js.map +1 -0
- package/dist/host/src/drivers/acp/driver.d.ts +110 -0
- package/dist/host/src/drivers/acp/driver.js +438 -0
- package/dist/host/src/drivers/acp/driver.js.map +1 -0
- package/dist/host/src/drivers/acp/mapper.d.ts +49 -0
- package/dist/host/src/drivers/acp/mapper.js +411 -0
- package/dist/host/src/drivers/acp/mapper.js.map +1 -0
- package/dist/host/src/drivers/auth.d.ts +7 -0
- package/dist/host/src/drivers/auth.js +57 -0
- package/dist/host/src/drivers/auth.js.map +1 -0
- package/dist/host/src/drivers/claude/driver.d.ts +76 -0
- package/dist/host/src/drivers/claude/driver.js +331 -0
- package/dist/host/src/drivers/claude/driver.js.map +1 -0
- package/dist/host/src/drivers/claude/transcript.d.ts +335 -0
- package/dist/host/src/drivers/claude/transcript.js +516 -0
- package/dist/host/src/drivers/claude/transcript.js.map +1 -0
- package/dist/host/src/drivers/codex/app-server.d.ts +36 -0
- package/dist/host/src/drivers/codex/app-server.js +144 -0
- package/dist/host/src/drivers/codex/app-server.js.map +1 -0
- package/dist/host/src/drivers/codex/driver.d.ts +89 -0
- package/dist/host/src/drivers/codex/driver.js +452 -0
- package/dist/host/src/drivers/codex/driver.js.map +1 -0
- package/dist/host/src/drivers/codex/mapper.d.ts +61 -0
- package/dist/host/src/drivers/codex/mapper.js +623 -0
- package/dist/host/src/drivers/codex/mapper.js.map +1 -0
- package/dist/host/src/drivers/codex/settings.d.ts +51 -0
- package/dist/host/src/drivers/codex/settings.js +107 -0
- package/dist/host/src/drivers/codex/settings.js.map +1 -0
- package/dist/host/src/drivers/images.d.ts +11 -0
- package/dist/host/src/drivers/images.js +34 -0
- package/dist/host/src/drivers/images.js.map +1 -0
- package/dist/host/src/drivers/nesting.d.ts +6 -0
- package/dist/host/src/drivers/nesting.js +25 -0
- package/dist/host/src/drivers/nesting.js.map +1 -0
- package/dist/host/src/drivers/registry.d.ts +21 -0
- package/dist/host/src/drivers/registry.js +30 -0
- package/dist/host/src/drivers/registry.js.map +1 -0
- package/dist/host/src/drivers/types.d.ts +105 -0
- package/dist/host/src/drivers/types.js +2 -0
- package/dist/host/src/drivers/types.js.map +1 -0
- package/dist/host/src/fs.d.ts +69 -0
- package/dist/host/src/fs.js +198 -0
- package/dist/host/src/fs.js.map +1 -0
- package/dist/host/src/gateway.d.ts +55 -0
- package/dist/host/src/gateway.js +263 -0
- package/dist/host/src/gateway.js.map +1 -0
- package/dist/host/src/host.d.ts +51 -0
- package/dist/host/src/host.js +133 -0
- package/dist/host/src/host.js.map +1 -0
- package/dist/host/src/hub.d.ts +89 -0
- package/dist/host/src/hub.js +592 -0
- package/dist/host/src/hub.js.map +1 -0
- package/dist/host/src/index.d.ts +10 -0
- package/dist/host/src/index.js +10 -0
- package/dist/host/src/index.js.map +1 -0
- package/dist/host/src/ports.d.ts +24 -0
- package/dist/host/src/ports.js +201 -0
- package/dist/host/src/ports.js.map +1 -0
- package/dist/host/src/rpc/client.d.ts +9 -0
- package/dist/host/src/rpc/client.js +42 -0
- package/dist/host/src/rpc/client.js.map +1 -0
- package/dist/host/src/rpc/server.d.ts +52 -0
- package/dist/host/src/rpc/server.js +293 -0
- package/dist/host/src/rpc/server.js.map +1 -0
- package/dist/host/src/screen.d.ts +30 -0
- package/dist/host/src/screen.js +230 -0
- package/dist/host/src/screen.js.map +1 -0
- package/dist/host/src/shell-env.d.ts +8 -0
- package/dist/host/src/shell-env.js +39 -0
- package/dist/host/src/shell-env.js.map +1 -0
- package/dist/host/src/store.d.ts +75 -0
- package/dist/host/src/store.js +265 -0
- package/dist/host/src/store.js.map +1 -0
- package/dist/host/src/terminals.d.ts +55 -0
- package/dist/host/src/terminals.js +367 -0
- package/dist/host/src/terminals.js.map +1 -0
- package/dist/host/tsconfig.tsbuildinfo +1 -0
- package/dist/wire/src/crypto.d.ts +65 -0
- package/dist/wire/src/crypto.js +253 -0
- package/dist/wire/src/crypto.js.map +1 -0
- package/dist/wire/src/index.d.ts +8 -0
- package/dist/wire/src/index.js +9 -0
- package/dist/wire/src/index.js.map +1 -0
- package/dist/wire/src/model.d.ts +704 -0
- package/dist/wire/src/model.js +163 -0
- package/dist/wire/src/model.js.map +1 -0
- package/dist/wire/src/peer.d.ts +42 -0
- package/dist/wire/src/peer.js +143 -0
- package/dist/wire/src/peer.js.map +1 -0
- package/dist/wire/src/relay-client.d.ts +82 -0
- package/dist/wire/src/relay-client.js +259 -0
- package/dist/wire/src/relay-client.js.map +1 -0
- package/dist/wire/src/relay.d.ts +481 -0
- package/dist/wire/src/relay.js +129 -0
- package/dist/wire/src/relay.js.map +1 -0
- package/dist/wire/src/rpc.d.ts +4170 -0
- package/dist/wire/src/rpc.js +332 -0
- package/dist/wire/src/rpc.js.map +1 -0
- package/dist/wire/src/shell.d.ts +2 -0
- package/dist/wire/src/shell.js +62 -0
- package/dist/wire/src/shell.js.map +1 -0
- package/dist/wire/src/updates.d.ts +3325 -0
- package/dist/wire/src/updates.js +210 -0
- package/dist/wire/src/updates.js.map +1 -0
- package/package.json +53 -0
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { mkdir, open, readdir, stat, writeFile } from "node:fs/promises";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { basename, dirname, extname, join, resolve } from "node:path";
|
|
5
|
+
import { RpcError } from "@linkshell/wire";
|
|
6
|
+
// Browsing and searching the host's directories, to pick where a session runs.
|
|
7
|
+
const PROJECT_MARKERS = [".git", "package.json", "Cargo.toml", "go.mod", "pyproject.toml", "Package.swift", "pom.xml", "build.gradle", "Gemfile", "composer.json", "deno.json", "CMakeLists.txt"];
|
|
8
|
+
/** Directories never worth descending into when searching. */
|
|
9
|
+
const SKIP = new Set([
|
|
10
|
+
"node_modules", ".git", "Library", "Pods", "DerivedData", "build", "dist", ".next", ".expo", "target", "vendor",
|
|
11
|
+
".Trash", "Applications", "Movies", "Music", "Pictures", ".cache", ".npm", ".pnpm-store", "venv", ".venv", "__pycache__",
|
|
12
|
+
]);
|
|
13
|
+
function isProject(names) {
|
|
14
|
+
return PROJECT_MARKERS.some((marker) => names.has(marker));
|
|
15
|
+
}
|
|
16
|
+
function expand(path) {
|
|
17
|
+
if (!path || path === "~")
|
|
18
|
+
return homedir();
|
|
19
|
+
if (path.startsWith("~/"))
|
|
20
|
+
return join(homedir(), path.slice(2));
|
|
21
|
+
return resolve(path);
|
|
22
|
+
}
|
|
23
|
+
async function subdirectories(path, hidden) {
|
|
24
|
+
const dirents = await readdir(path, { withFileTypes: true });
|
|
25
|
+
const names = new Set(dirents.map((d) => d.name));
|
|
26
|
+
const dirs = dirents.filter((d) => (d.isDirectory() || d.isSymbolicLink()) && (hidden || !d.name.startsWith(".")));
|
|
27
|
+
const entries = await Promise.all(dirs.map(async (d) => {
|
|
28
|
+
const full = join(path, d.name);
|
|
29
|
+
try {
|
|
30
|
+
if (d.isSymbolicLink() && !(await stat(full)).isDirectory())
|
|
31
|
+
return undefined;
|
|
32
|
+
const children = new Set(await readdir(full).catch(() => []));
|
|
33
|
+
return { name: d.name, path: full, project: isProject(children) || undefined };
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
}));
|
|
39
|
+
return {
|
|
40
|
+
entries: entries
|
|
41
|
+
.filter((e) => Boolean(e))
|
|
42
|
+
.sort((a, b) => Number(Boolean(b.project)) - Number(Boolean(a.project)) || a.name.localeCompare(b.name, undefined, { sensitivity: "base" })),
|
|
43
|
+
names,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
export async function listDirectory(input) {
|
|
47
|
+
const path = expand(input.path);
|
|
48
|
+
if (!existsSync(path) || !(await stat(path)).isDirectory())
|
|
49
|
+
throw RpcError.app("not_found", `no such directory: ${path}`);
|
|
50
|
+
const { entries } = await subdirectories(path, input.hidden).catch(() => {
|
|
51
|
+
throw RpcError.app("not_found", `can't read ${path}`);
|
|
52
|
+
});
|
|
53
|
+
const parent = dirname(path);
|
|
54
|
+
return { path, parent: parent === path ? undefined : parent, home: homedir(), entries };
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Breadth-first under the home directory, a few levels deep, within a time
|
|
58
|
+
* budget: good enough to find "claude-remote" or "rtranslate" by typing.
|
|
59
|
+
*/
|
|
60
|
+
export async function searchDirectories(input, budgetMs = 1500) {
|
|
61
|
+
const query = input.query.toLowerCase().trim();
|
|
62
|
+
const deadline = Date.now() + budgetMs;
|
|
63
|
+
const found = [];
|
|
64
|
+
let queue = [{ path: homedir(), depth: 0 }];
|
|
65
|
+
let truncated = false;
|
|
66
|
+
while (queue.length > 0) {
|
|
67
|
+
if (Date.now() > deadline) {
|
|
68
|
+
truncated = true;
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
const next = [];
|
|
72
|
+
for (const { path, depth } of queue) {
|
|
73
|
+
if (Date.now() > deadline) {
|
|
74
|
+
truncated = true;
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
let dirents;
|
|
78
|
+
try {
|
|
79
|
+
dirents = await readdir(path, { withFileTypes: true });
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
for (const d of dirents) {
|
|
85
|
+
if (!d.isDirectory() || d.name.startsWith(".") || SKIP.has(d.name))
|
|
86
|
+
continue;
|
|
87
|
+
const full = join(path, d.name);
|
|
88
|
+
if (d.name.toLowerCase().includes(query)) {
|
|
89
|
+
const children = new Set(await readdir(full).catch(() => []));
|
|
90
|
+
found.push({ name: d.name, path: full, project: isProject(children) || undefined });
|
|
91
|
+
}
|
|
92
|
+
if (depth < 4)
|
|
93
|
+
next.push({ path: full, depth: depth + 1 });
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
queue = next;
|
|
97
|
+
}
|
|
98
|
+
// Code projects first (that's what a session runs in), then how well the name matches.
|
|
99
|
+
const score = (e) => (e.project ? 0 : 10) + (e.name.toLowerCase() === query ? 0 : e.name.toLowerCase().startsWith(query) ? 1 : 2);
|
|
100
|
+
found.sort((a, b) => score(a) - score(b) || a.path.split("/").length - b.path.split("/").length);
|
|
101
|
+
if (found.length > input.limit)
|
|
102
|
+
truncated = true;
|
|
103
|
+
return { entries: found.slice(0, input.limit), truncated };
|
|
104
|
+
}
|
|
105
|
+
const IMAGE_TYPES = {
|
|
106
|
+
png: "image/png",
|
|
107
|
+
jpg: "image/jpeg",
|
|
108
|
+
jpeg: "image/jpeg",
|
|
109
|
+
gif: "image/gif",
|
|
110
|
+
webp: "image/webp",
|
|
111
|
+
svg: "image/svg+xml",
|
|
112
|
+
heic: "image/heic",
|
|
113
|
+
};
|
|
114
|
+
/** Reads a file for the phone's viewer. Text is detected by content, not extension. */
|
|
115
|
+
export async function readFile(input) {
|
|
116
|
+
const path = expand(input.path);
|
|
117
|
+
let info;
|
|
118
|
+
try {
|
|
119
|
+
info = await stat(path);
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
throw RpcError.app("not_found", `no such file: ${path}`);
|
|
123
|
+
}
|
|
124
|
+
if (!info.isFile())
|
|
125
|
+
throw RpcError.app("invalid_params", `not a file: ${path}`);
|
|
126
|
+
const extension = path.split(".").pop()?.toLowerCase() ?? "";
|
|
127
|
+
const image = IMAGE_TYPES[extension];
|
|
128
|
+
const limit = image ? Math.min(input.maxBytes * 3, 8_000_000) : input.maxBytes;
|
|
129
|
+
const length = Math.min(info.size, limit);
|
|
130
|
+
const handle = await open(path, "r");
|
|
131
|
+
const buffer = Buffer.alloc(length);
|
|
132
|
+
try {
|
|
133
|
+
await handle.read(buffer, 0, length, 0);
|
|
134
|
+
}
|
|
135
|
+
finally {
|
|
136
|
+
await handle.close();
|
|
137
|
+
}
|
|
138
|
+
const base = { path, size: info.size, modifiedAt: info.mtimeMs, truncated: info.size > length };
|
|
139
|
+
if (image && !base.truncated)
|
|
140
|
+
return { ...base, kind: "image", data: buffer.toString("base64"), mimeType: image };
|
|
141
|
+
// Text: no NUL bytes in the first 8KB.
|
|
142
|
+
if (buffer.subarray(0, 8192).includes(0))
|
|
143
|
+
return { ...base, kind: "binary" };
|
|
144
|
+
let text = buffer.toString("utf8");
|
|
145
|
+
// Don't end on half a multi-byte character.
|
|
146
|
+
if (base.truncated)
|
|
147
|
+
text = text.replace(/\uFFFD+$/, "");
|
|
148
|
+
return { ...base, kind: "text", text };
|
|
149
|
+
}
|
|
150
|
+
/** A single path component: no separators, no `.`/`..`, nothing a shell or filesystem would choke on. */
|
|
151
|
+
function safeName(name) {
|
|
152
|
+
const cleaned = name.replace(/[/\\\0]/g, "_").trim();
|
|
153
|
+
if (!cleaned || cleaned === "." || cleaned === "..")
|
|
154
|
+
throw RpcError.app("invalid_params", "invalid name");
|
|
155
|
+
return cleaned;
|
|
156
|
+
}
|
|
157
|
+
async function existingDirectory(path) {
|
|
158
|
+
const dir = resolve(path.replace(/^~(?=$|\/)/, homedir()));
|
|
159
|
+
const info = await stat(dir).catch(() => undefined);
|
|
160
|
+
if (!info?.isDirectory())
|
|
161
|
+
throw RpcError.app("not_found", `no such directory: ${path}`);
|
|
162
|
+
return dir;
|
|
163
|
+
}
|
|
164
|
+
/** Writes an uploaded file into `dir`, never over an existing one. */
|
|
165
|
+
export async function uploadFile(input) {
|
|
166
|
+
const dir = await existingDirectory(input.dir);
|
|
167
|
+
const name = safeName(input.name);
|
|
168
|
+
const bytes = Buffer.from(input.data, "base64");
|
|
169
|
+
const ext = extname(name);
|
|
170
|
+
const stem = basename(name, ext);
|
|
171
|
+
for (let attempt = 1; attempt < 1000; attempt++) {
|
|
172
|
+
const path = join(dir, attempt === 1 ? name : `${stem} ${attempt}${ext}`);
|
|
173
|
+
try {
|
|
174
|
+
// "wx": fails if the file exists, so a race can't overwrite either.
|
|
175
|
+
await writeFile(path, bytes, { flag: "wx" });
|
|
176
|
+
return { path, size: bytes.length };
|
|
177
|
+
}
|
|
178
|
+
catch (error) {
|
|
179
|
+
if (error.code !== "EEXIST")
|
|
180
|
+
throw error;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
throw RpcError.app("busy", "too many files with this name");
|
|
184
|
+
}
|
|
185
|
+
export async function makeDirectory(input) {
|
|
186
|
+
const parent = await existingDirectory(input.parent);
|
|
187
|
+
const path = join(parent, safeName(input.name));
|
|
188
|
+
try {
|
|
189
|
+
await mkdir(path);
|
|
190
|
+
}
|
|
191
|
+
catch (error) {
|
|
192
|
+
if (error.code === "EEXIST")
|
|
193
|
+
throw RpcError.app("invalid_params", "a file or folder with this name already exists");
|
|
194
|
+
throw error;
|
|
195
|
+
}
|
|
196
|
+
return { path };
|
|
197
|
+
}
|
|
198
|
+
//# sourceMappingURL=fs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fs.js","sourceRoot":"","sources":["../../../src/fs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACzE,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACtE,OAAO,EAAE,QAAQ,EAAuB,MAAM,iBAAiB,CAAC;AAEhE,+EAA+E;AAE/E,MAAM,eAAe,GAAG,CAAC,MAAM,EAAE,cAAc,EAAE,YAAY,EAAE,QAAQ,EAAE,gBAAgB,EAAE,eAAe,EAAE,SAAS,EAAE,cAAc,EAAE,SAAS,EAAE,eAAe,EAA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E,KAAK,CAAC,MAAM,EAAE,CAAC;QACtC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAK,KAA+B,CAAC,IAAI,KAAK,QAAQ;gBAAE,MAAM,KAAK,CAAC;QACtE,CAAC;IACH,CAAC;IACD,MAAM,QAAQ,CAAC,GAAG,CAAC,MAAM,EAAE,+BAA+B,CAAC,CAAC;AAC9D,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,KAAuC;IACzE,MAAM,MAAM,GAAG,MAAM,iBAAiB,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IACrD,MAAM,IAAI,GAAG,IAAI,CAAC,MAAM,EAAE,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC;IAChD,IAAI,CAAC;QACH,MAAM,KAAK,CAAC,IAAI,CAAC,CAAC;IACpB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAK,KAA+B,CAAC,IAAI,KAAK,QAAQ;YAAE,MAAM,QAAQ,CAAC,GAAG,CAAC,gBAAgB,EAAE,gDAAgD,CAAC,CAAC;QAC/I,MAAM,KAAK,CAAC;IACd,CAAC;IACD,OAAO,EAAE,IAAI,EAAE,CAAC;AAClB,CAAC"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { type GatewayStatus, type Identity } from "@linkshell/wire";
|
|
2
|
+
import type { RpcTransport } from "./rpc/server.js";
|
|
3
|
+
export interface GatewayLinkOptions {
|
|
4
|
+
/** Gateway base URL (ws:// or wss://, or http(s):// which is converted). */
|
|
5
|
+
url: string;
|
|
6
|
+
home: string;
|
|
7
|
+
/** Account token (from `linkshell login`); the machine then belongs to that account. */
|
|
8
|
+
token?: () => Promise<string | undefined> | string | undefined;
|
|
9
|
+
name?: string;
|
|
10
|
+
/** Serves a decrypted channel as a client connection. */
|
|
11
|
+
serve: (transport: RpcTransport) => void;
|
|
12
|
+
onChange?: (status: GatewayStatus) => void;
|
|
13
|
+
onPaired?: (device: {
|
|
14
|
+
id: string;
|
|
15
|
+
name: string;
|
|
16
|
+
}) => void;
|
|
17
|
+
log: (message: string) => void;
|
|
18
|
+
}
|
|
19
|
+
export declare function relayUrl(base: string): string;
|
|
20
|
+
export declare class GatewayLink {
|
|
21
|
+
private readonly options;
|
|
22
|
+
readonly identity: Identity;
|
|
23
|
+
private readonly relay;
|
|
24
|
+
private readonly devicesPath;
|
|
25
|
+
private devices;
|
|
26
|
+
private pairing?;
|
|
27
|
+
private readonly tunnels;
|
|
28
|
+
private readonly online;
|
|
29
|
+
private readonly name;
|
|
30
|
+
constructor(options: GatewayLinkOptions);
|
|
31
|
+
start(): void;
|
|
32
|
+
stop(): void;
|
|
33
|
+
status(): GatewayStatus;
|
|
34
|
+
/** Opens a pairing window; the returned link goes in the QR code. */
|
|
35
|
+
startPairing(): Promise<{
|
|
36
|
+
link: string;
|
|
37
|
+
code: string;
|
|
38
|
+
expiresAt: number;
|
|
39
|
+
gateway: string;
|
|
40
|
+
}>;
|
|
41
|
+
revoke(deviceId: string): Promise<void>;
|
|
42
|
+
private onPairRequest;
|
|
43
|
+
private onFrame;
|
|
44
|
+
/**
|
|
45
|
+
* Whose hello to accept: devices paired here (with the keys stored at
|
|
46
|
+
* pairing, not whatever the gateway says now), or, when this machine is
|
|
47
|
+
* signed in, devices the gateway says share its account.
|
|
48
|
+
*/
|
|
49
|
+
private authorize;
|
|
50
|
+
private closeTunnel;
|
|
51
|
+
private refreshPresence;
|
|
52
|
+
private changed;
|
|
53
|
+
private loadDevices;
|
|
54
|
+
private saveDevices;
|
|
55
|
+
}
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { chmodSync, existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { hostname, platform } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { randomBytes } from "node:crypto";
|
|
5
|
+
import WebSocket from "ws";
|
|
6
|
+
import { answerTunnel, checkPairingProof, codeSecret, createIdentity, encodePairingLink, RELAY_PATH, RelayClient, RpcError, toBase64, } from "@linkshell/wire";
|
|
7
|
+
export function relayUrl(base) {
|
|
8
|
+
const url = new URL(base.replace(/^http/, "ws"));
|
|
9
|
+
if (!url.pathname.endsWith(RELAY_PATH))
|
|
10
|
+
url.pathname = url.pathname.replace(/\/$/, "") + RELAY_PATH;
|
|
11
|
+
return url.toString();
|
|
12
|
+
}
|
|
13
|
+
function loadIdentity(home) {
|
|
14
|
+
const path = join(home, "identity.json");
|
|
15
|
+
try {
|
|
16
|
+
const stored = JSON.parse(readFileSync(path, "utf8"));
|
|
17
|
+
if (stored.id && stored.signSecret && stored.boxSecret)
|
|
18
|
+
return stored;
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
// First run: create below.
|
|
22
|
+
}
|
|
23
|
+
const identity = createIdentity();
|
|
24
|
+
writeFileSync(path, `${JSON.stringify(identity, null, 2)}\n`, { mode: 0o600 });
|
|
25
|
+
chmodSync(path, 0o600);
|
|
26
|
+
return identity;
|
|
27
|
+
}
|
|
28
|
+
export class GatewayLink {
|
|
29
|
+
options;
|
|
30
|
+
identity;
|
|
31
|
+
relay;
|
|
32
|
+
devicesPath;
|
|
33
|
+
devices;
|
|
34
|
+
pairing;
|
|
35
|
+
tunnels = new Map();
|
|
36
|
+
online = new Set();
|
|
37
|
+
name;
|
|
38
|
+
constructor(options) {
|
|
39
|
+
this.options = options;
|
|
40
|
+
this.identity = loadIdentity(options.home);
|
|
41
|
+
this.devicesPath = join(options.home, "paired-devices.json");
|
|
42
|
+
this.devices = this.loadDevices();
|
|
43
|
+
this.name = options.name ?? hostname().replace(/\.local$/, "");
|
|
44
|
+
this.relay = new RelayClient({
|
|
45
|
+
url: relayUrl(options.url),
|
|
46
|
+
identity: this.identity,
|
|
47
|
+
role: "machine",
|
|
48
|
+
name: this.name,
|
|
49
|
+
platform: platform(),
|
|
50
|
+
token: options.token,
|
|
51
|
+
createSocket: (url) => new WebSocket(url, { perMessageDeflate: false }),
|
|
52
|
+
});
|
|
53
|
+
this.relay.onStatus((status, error) => {
|
|
54
|
+
if (status === "online") {
|
|
55
|
+
this.options.log(`[gateway] online as ${this.identity.id.slice(0, 8)}${this.relay.account ? ` (${this.relay.account.email ?? "account"})` : ""}`);
|
|
56
|
+
void this.refreshPresence();
|
|
57
|
+
}
|
|
58
|
+
else if (error && status === "offline") {
|
|
59
|
+
this.options.log(`[gateway] ${error.code}: ${error.message}`);
|
|
60
|
+
for (const tunnel of this.tunnels.values())
|
|
61
|
+
this.closeTunnel(tunnel, "gateway disconnected");
|
|
62
|
+
}
|
|
63
|
+
this.changed();
|
|
64
|
+
});
|
|
65
|
+
this.relay.onFrame((from, via, frame) => this.onFrame(from, via, frame));
|
|
66
|
+
this.relay.on("pair.request", (request) => void this.onPairRequest(request));
|
|
67
|
+
this.relay.on("presence", ({ id, online }) => {
|
|
68
|
+
if (online)
|
|
69
|
+
this.online.add(id);
|
|
70
|
+
else {
|
|
71
|
+
this.online.delete(id);
|
|
72
|
+
for (const tunnel of this.tunnels.values())
|
|
73
|
+
if (tunnel.deviceId === id)
|
|
74
|
+
this.closeTunnel(tunnel, "device went offline");
|
|
75
|
+
}
|
|
76
|
+
this.changed();
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
start() {
|
|
80
|
+
this.relay.start();
|
|
81
|
+
}
|
|
82
|
+
stop() {
|
|
83
|
+
for (const tunnel of this.tunnels.values())
|
|
84
|
+
this.closeTunnel(tunnel, "host stopping");
|
|
85
|
+
this.relay.stop();
|
|
86
|
+
}
|
|
87
|
+
status() {
|
|
88
|
+
const status = this.relay.status;
|
|
89
|
+
return {
|
|
90
|
+
url: this.options.url,
|
|
91
|
+
status: status === "stopped" ? "off" : status,
|
|
92
|
+
error: status === "online" ? undefined : this.relay.lastError,
|
|
93
|
+
machine: { id: this.identity.id, name: this.name },
|
|
94
|
+
account: this.relay.account,
|
|
95
|
+
devices: this.devices.map((device) => ({
|
|
96
|
+
id: device.id,
|
|
97
|
+
name: device.name,
|
|
98
|
+
pairedAt: device.pairedAt,
|
|
99
|
+
online: this.online.has(device.id),
|
|
100
|
+
})),
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
/** Opens a pairing window; the returned link goes in the QR code. */
|
|
104
|
+
async startPairing() {
|
|
105
|
+
if (!(await this.relay.waitOnline(8000))) {
|
|
106
|
+
throw RpcError.app("offline", `can't reach the gateway (${this.options.url})${this.relay.lastError ? `: ${this.relay.lastError.message}` : ""}`);
|
|
107
|
+
}
|
|
108
|
+
const offer = await this.relay.request("pair.offer", {});
|
|
109
|
+
const secret = toBase64(randomBytes(16));
|
|
110
|
+
this.pairing = { secret, code: offer.code, expiresAt: offer.expiresAt };
|
|
111
|
+
const link = encodePairingLink({ gateway: this.options.url, signKey: this.identity.signKey, secret, code: offer.code });
|
|
112
|
+
return { link, code: offer.code, expiresAt: offer.expiresAt, gateway: this.options.url };
|
|
113
|
+
}
|
|
114
|
+
async revoke(deviceId) {
|
|
115
|
+
this.devices = this.devices.filter((device) => device.id !== deviceId);
|
|
116
|
+
this.saveDevices();
|
|
117
|
+
for (const tunnel of this.tunnels.values())
|
|
118
|
+
if (tunnel.deviceId === deviceId)
|
|
119
|
+
this.closeTunnel(tunnel, "unpaired");
|
|
120
|
+
await this.relay.request("devices.revoke", { deviceId }).catch(() => { });
|
|
121
|
+
this.changed();
|
|
122
|
+
}
|
|
123
|
+
// ── pairing ───────────────────────────────────────────────────────
|
|
124
|
+
async onPairRequest(request) {
|
|
125
|
+
const pairing = this.pairing;
|
|
126
|
+
const valid = !!pairing &&
|
|
127
|
+
pairing.expiresAt > Date.now() &&
|
|
128
|
+
request.code === pairing.code &&
|
|
129
|
+
(checkPairingProof(pairing.secret, request.device, request.proof) || checkPairingProof(codeSecret(pairing.code), request.device, request.proof));
|
|
130
|
+
if (valid) {
|
|
131
|
+
this.devices = [
|
|
132
|
+
...this.devices.filter((device) => device.id !== request.device.id),
|
|
133
|
+
{ ...request.device, pairedAt: Date.now() },
|
|
134
|
+
];
|
|
135
|
+
this.saveDevices();
|
|
136
|
+
this.pairing = undefined;
|
|
137
|
+
this.online.add(request.device.id);
|
|
138
|
+
this.options.log(`[gateway] paired with "${request.device.name}"`);
|
|
139
|
+
this.options.onPaired?.({ id: request.device.id, name: request.device.name });
|
|
140
|
+
this.changed();
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
this.options.log(`[gateway] refused a pairing attempt from "${request.device.name}"`);
|
|
144
|
+
}
|
|
145
|
+
await this.relay.request("pair.decide", { requestId: request.requestId, accept: valid }).catch(() => { });
|
|
146
|
+
}
|
|
147
|
+
// ── channels ──────────────────────────────────────────────────────
|
|
148
|
+
onFrame(from, via, frame) {
|
|
149
|
+
const key = `${from.id}:${frame.ch}`;
|
|
150
|
+
switch (frame.k) {
|
|
151
|
+
case "hello": {
|
|
152
|
+
const device = this.authorize(from, via);
|
|
153
|
+
if (!device) {
|
|
154
|
+
this.relay.send(from.id, { k: "refuse", ch: frame.ch, code: "not_paired", message: "this device isn't paired with this computer" });
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
let answer;
|
|
158
|
+
try {
|
|
159
|
+
answer = answerTunnel(this.identity, device, frame);
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
this.relay.send(from.id, { k: "refuse", ch: frame.ch, code: "bad_hello", message: "handshake failed" });
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
const previous = this.tunnels.get(key);
|
|
166
|
+
if (previous)
|
|
167
|
+
this.closeTunnel(previous, "reopened");
|
|
168
|
+
const tunnel = { channel: answer.channel, deviceId: from.id, ch: frame.ch, closed: false };
|
|
169
|
+
this.tunnels.set(key, tunnel);
|
|
170
|
+
this.relay.send(from.id, answer.welcome);
|
|
171
|
+
this.options.serve({
|
|
172
|
+
local: false,
|
|
173
|
+
send: (text) => {
|
|
174
|
+
if (!tunnel.closed)
|
|
175
|
+
this.relay.send(from.id, { k: "data", ch: frame.ch, box: tunnel.channel.seal(text) });
|
|
176
|
+
},
|
|
177
|
+
close: () => this.closeTunnel(tunnel, "closed by host", true),
|
|
178
|
+
onMessage: (listener) => (tunnel.message = listener),
|
|
179
|
+
onClose: (listener) => (tunnel.close = listener),
|
|
180
|
+
});
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
case "data": {
|
|
184
|
+
const tunnel = this.tunnels.get(key);
|
|
185
|
+
if (!tunnel)
|
|
186
|
+
return;
|
|
187
|
+
let text;
|
|
188
|
+
try {
|
|
189
|
+
text = tunnel.channel.open(frame.box);
|
|
190
|
+
}
|
|
191
|
+
catch {
|
|
192
|
+
// Tampered, replayed or out of order: the channel can't be trusted any more.
|
|
193
|
+
this.closeTunnel(tunnel, "bad frame", true);
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
tunnel.message?.(text);
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
case "close": {
|
|
200
|
+
const tunnel = this.tunnels.get(key);
|
|
201
|
+
if (tunnel)
|
|
202
|
+
this.closeTunnel(tunnel, frame.reason ?? "closed by device");
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
default:
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Whose hello to accept: devices paired here (with the keys stored at
|
|
211
|
+
* pairing, not whatever the gateway says now), or, when this machine is
|
|
212
|
+
* signed in, devices the gateway says share its account.
|
|
213
|
+
*/
|
|
214
|
+
authorize(from, via) {
|
|
215
|
+
const paired = this.devices.find((device) => device.id === from.id);
|
|
216
|
+
if (paired)
|
|
217
|
+
return paired.signKey === from.signKey && paired.boxKey === from.boxKey ? paired : undefined;
|
|
218
|
+
if (via === "account" && this.relay.account)
|
|
219
|
+
return from;
|
|
220
|
+
return undefined;
|
|
221
|
+
}
|
|
222
|
+
closeTunnel(tunnel, reason, notify = false) {
|
|
223
|
+
if (tunnel.closed)
|
|
224
|
+
return;
|
|
225
|
+
tunnel.closed = true;
|
|
226
|
+
this.tunnels.delete(`${tunnel.deviceId}:${tunnel.ch}`);
|
|
227
|
+
if (notify)
|
|
228
|
+
this.relay.send(tunnel.deviceId, { k: "close", ch: tunnel.ch, reason });
|
|
229
|
+
tunnel.close?.();
|
|
230
|
+
}
|
|
231
|
+
async refreshPresence() {
|
|
232
|
+
try {
|
|
233
|
+
const { devices } = await this.relay.request("devices.list", {});
|
|
234
|
+
this.online.clear();
|
|
235
|
+
for (const device of devices)
|
|
236
|
+
if (device.online)
|
|
237
|
+
this.online.add(device.id);
|
|
238
|
+
this.changed();
|
|
239
|
+
}
|
|
240
|
+
catch {
|
|
241
|
+
// Presence events will correct it.
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
changed() {
|
|
245
|
+
this.options.onChange?.(this.status());
|
|
246
|
+
}
|
|
247
|
+
loadDevices() {
|
|
248
|
+
if (!existsSync(this.devicesPath))
|
|
249
|
+
return [];
|
|
250
|
+
try {
|
|
251
|
+
const parsed = JSON.parse(readFileSync(this.devicesPath, "utf8"));
|
|
252
|
+
return Array.isArray(parsed) ? parsed : [];
|
|
253
|
+
}
|
|
254
|
+
catch {
|
|
255
|
+
return [];
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
saveDevices() {
|
|
259
|
+
writeFileSync(this.devicesPath, `${JSON.stringify(this.devices, null, 2)}\n`, { mode: 0o600 });
|
|
260
|
+
chmodSync(this.devicesPath, 0o600);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
//# sourceMappingURL=gateway.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gateway.js","sourceRoot":"","sources":["../../../src/gateway.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC7E,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAC7C,OAAO,EAAE,IAAI,EAAE,MAA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AM,CAAC,KAAK,EAAE,CAAC;YACpB,KAAK,MAAM,MAAM,IAAI,OAAO;gBAAE,IAAI,MAAM,CAAC,MAAM;oBAAE,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;YAC5E,IAAI,CAAC,OAAO,EAAE,CAAC;QACjB,CAAC;QAAC,MAAM,CAAC;YACP,mCAAmC;QACrC,CAAC;IACH,CAAC;IAEO,OAAO;QACb,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;IACzC,CAAC;IAEO,WAAW;QACjB,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,WAAW,CAAC;YAAE,OAAO,EAAE,CAAC;QAC7C,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,IAAI,CAAC,WAAW,EAAE,MAAM,CAAC,CAAmB,CAAC;YACpF,OAAO,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC;QAC7C,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,EAAE,CAAC;QACZ,CAAC;IACH,CAAC;IAEO,WAAW;QACjB,aAAa,CAAC,IAAI,CAAC,WAAW,EAAE,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;QAC/F,SAAS,CAAC,IAAI,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC;IACrC,CAAC;CACF"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { MachineInfo } from "@linkshell/wire";
|
|
2
|
+
import type { AgentDriver } from "./drivers/types.js";
|
|
3
|
+
import { SessionHub } from "./hub.js";
|
|
4
|
+
import { HostRpcServer } from "./rpc/server.js";
|
|
5
|
+
import { GatewayLink } from "./gateway.js";
|
|
6
|
+
export interface HostPaths {
|
|
7
|
+
home: string;
|
|
8
|
+
runDir: string;
|
|
9
|
+
hostSocket: string;
|
|
10
|
+
codexSocket: string;
|
|
11
|
+
database: string;
|
|
12
|
+
}
|
|
13
|
+
/** `~/.linkshell`, or `$LINKSHELL_HOME` (tests, multiple instances). */
|
|
14
|
+
export declare function defaultHome(): string;
|
|
15
|
+
export declare function hostPaths(home?: string): HostPaths;
|
|
16
|
+
export interface HostOptions {
|
|
17
|
+
home?: string;
|
|
18
|
+
version: string;
|
|
19
|
+
/** Environment agents run with — should be the user's login-shell environment. */
|
|
20
|
+
env?: NodeJS.ProcessEnv;
|
|
21
|
+
codexCommand?: string;
|
|
22
|
+
claudeCommand?: string;
|
|
23
|
+
/** Overrides the bundled Claude ACP adapter. */
|
|
24
|
+
claudeAdapter?: {
|
|
25
|
+
command: string;
|
|
26
|
+
args: string[];
|
|
27
|
+
};
|
|
28
|
+
/** Extra drivers (tests) or a full replacement of the default set. */
|
|
29
|
+
drivers?: (paths: HostPaths) => AgentDriver[];
|
|
30
|
+
tcpPort?: number;
|
|
31
|
+
log?: (message: string) => void;
|
|
32
|
+
/** A v2 gateway to reach this machine through, from anywhere. */
|
|
33
|
+
gateway?: {
|
|
34
|
+
url: string;
|
|
35
|
+
token?: () => Promise<string | undefined> | string | undefined;
|
|
36
|
+
name?: string;
|
|
37
|
+
};
|
|
38
|
+
/** How often to re-list sessions started outside LinkShell; 0 disables. */
|
|
39
|
+
discoveryIntervalMs?: number;
|
|
40
|
+
}
|
|
41
|
+
export interface RunningHost {
|
|
42
|
+
paths: HostPaths;
|
|
43
|
+
hub: SessionHub;
|
|
44
|
+
server: HostRpcServer;
|
|
45
|
+
gateway?: GatewayLink;
|
|
46
|
+
machineInfo(): MachineInfo;
|
|
47
|
+
stop(): Promise<void>;
|
|
48
|
+
}
|
|
49
|
+
/** True when another host already answers on the socket. */
|
|
50
|
+
export declare function isHostRunning(socketPath: string): Promise<boolean>;
|
|
51
|
+
export declare function startHost(options: HostOptions): Promise<RunningHost>;
|