agentroam 0.2.0-preview.77 → 0.2.0-preview.79
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/dist/args.js +24 -4
- package/dist/cli.js +40 -2
- package/dist/codex-sharing.js +5 -3
- package/dist/device-pairing.js +35 -12
- package/dist/mesh/mesh-command.js +51 -0
- package/dist/mesh/mesh-config.js +95 -0
- package/dist/mesh/mesh-payload.js +20 -0
- package/dist/mesh/mesh-runtime.js +101 -0
- package/dist/opencode-runtime-manager.js +44 -23
- package/dist/platform-packages.js +2 -0
- package/dist/runtime-manager.js +10 -1
- package/dist/self-hosted-relay.json +1 -0
- package/dist/service/service-command.js +8 -1
- package/dist/service/service-controller.js +2 -0
- package/dist/tailscale/bundled-asset.js +43 -0
- package/dist/tailscale/daemon-manager.js +112 -0
- package/dist/tailscale/installer.js +131 -0
- package/dist/tailscale/manifest.js +15 -0
- package/dist/tailscale/tailnet-cli.js +101 -0
- package/dist/tunnel/relay-orchestrator.js +43 -37
- package/dist/tunnel/self-hosted-provider.js +147 -0
- package/dist/tunnel/tunnel-controller.js +10 -0
- package/dist/update/update-command.js +1 -0
- package/dist/update/update-worker.js +2 -0
- package/install/write-relay-default.mjs +6 -0
- package/package.json +10 -8
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { ensureCloudflared } from "../cloudflared/installer.js";
|
|
2
2
|
import { CloudflareTunnelProvider } from "./cloudflare-provider.js";
|
|
3
3
|
import { CustomCommandTunnelProvider } from "./custom-command-provider.js";
|
|
4
|
+
import { readDefaultRelayUrl, SelfHostedTunnelProvider } from "./self-hosted-provider.js";
|
|
4
5
|
import { PinggyTunnelProvider } from "./pinggy-provider.js";
|
|
5
6
|
import { waitForPublicReadiness } from "./public-readiness.js";
|
|
6
7
|
import { withAbortSignal } from "../abort.js";
|
|
@@ -9,52 +10,57 @@ export async function selectRelay(options) {
|
|
|
9
10
|
options.signal.throwIfAborted();
|
|
10
11
|
if (options.cli.localOnly)
|
|
11
12
|
return { tunnel: null, publicUrl: options.lanUrl, provider: "lan", failures: [] };
|
|
12
|
-
const
|
|
13
|
+
const { relay, relayUrl: configuredUrl } = options.cli;
|
|
14
|
+
const relayUrl = configuredUrl ?? (relay === "auto" || relay === "self-hosted" ? readDefaultRelayUrl() : null);
|
|
15
|
+
const providerName = relay === "auto" ? (relayUrl ? "self-hosted" : "cloudflare") : relay;
|
|
13
16
|
const failures = [];
|
|
14
17
|
const readiness = options.readiness ?? waitForPublicReadiness;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
18
|
+
options.onStage?.("connecting");
|
|
19
|
+
options.onAttempt?.(providerName);
|
|
20
|
+
const attempt = { tunnel: null };
|
|
21
|
+
const deadline = new AbortController();
|
|
22
|
+
const timeoutMs = options.timeoutMs ?? RELAY_START_TIMEOUT_MS;
|
|
23
|
+
const timer = setTimeout(() => deadline.abort(new Error(`relay connection timed out after ${timeoutMs}ms`)), timeoutMs);
|
|
24
|
+
const signal = AbortSignal.any([options.signal, deadline.signal]);
|
|
25
|
+
try {
|
|
26
|
+
return await withAbortSignal(async () => {
|
|
27
|
+
const provider = await createProvider(providerName, options, relayUrl);
|
|
28
|
+
signal.throwIfAborted();
|
|
29
|
+
const handle = await provider.start({ localUrl: options.localUrl, signal, log: options.log });
|
|
30
|
+
if (signal.aborted) {
|
|
31
|
+
void handle.close().catch(() => { });
|
|
26
32
|
signal.throwIfAborted();
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
options.onFailure?.(providerName, message);
|
|
47
|
-
}
|
|
48
|
-
finally {
|
|
49
|
-
clearTimeout(timer);
|
|
50
|
-
}
|
|
33
|
+
}
|
|
34
|
+
attempt.tunnel = handle;
|
|
35
|
+
options.onStage?.("checking");
|
|
36
|
+
await readiness(handle.publicUrl, { signal });
|
|
37
|
+
signal.throwIfAborted();
|
|
38
|
+
return { tunnel: handle, publicUrl: handle.publicUrl, provider: providerName, failures };
|
|
39
|
+
}, signal);
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
deadline.abort(error);
|
|
43
|
+
if (attempt.tunnel)
|
|
44
|
+
await withAbortSignal(() => attempt.tunnel.close(), AbortSignal.timeout(3_000)).catch(() => { });
|
|
45
|
+
options.signal.throwIfAborted();
|
|
46
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
47
|
+
failures.push({ provider: providerName, message });
|
|
48
|
+
options.onFailure?.(providerName, message);
|
|
49
|
+
}
|
|
50
|
+
finally {
|
|
51
|
+
clearTimeout(timer);
|
|
51
52
|
}
|
|
52
53
|
return { tunnel: null, publicUrl: options.lanUrl, provider: "lan", failures };
|
|
53
54
|
}
|
|
54
|
-
async function createProvider(name, options) {
|
|
55
|
+
async function createProvider(name, options, relayUrl) {
|
|
55
56
|
const override = options.providerFactories?.[name];
|
|
56
57
|
if (override)
|
|
57
58
|
return await override();
|
|
59
|
+
if (name === "self-hosted") {
|
|
60
|
+
if (!relayUrl)
|
|
61
|
+
throw new Error("自建 HTTPS 入口地址未配置");
|
|
62
|
+
return new SelfHostedTunnelProvider(relayUrl, options.cli.dataDir);
|
|
63
|
+
}
|
|
58
64
|
if (name === "cloudflare") {
|
|
59
65
|
const executable = await ensureCloudflared(options.target, options.cli.dataDir);
|
|
60
66
|
return new CloudflareTunnelProvider(executable);
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { link, readFile, unlink } from "node:fs/promises";
|
|
4
|
+
import { connect } from "node:net";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { WebSocket, createWebSocketStream } from "ws";
|
|
7
|
+
import { writePrivateText } from "../service/service-files.js";
|
|
8
|
+
const tokenPattern = /^[A-Za-z0-9_-]{43}$/;
|
|
9
|
+
export function relayOrigin(value) {
|
|
10
|
+
const url = new URL(value);
|
|
11
|
+
if (url.protocol !== "https:" || url.port || url.username || url.password || url.pathname !== "/" ||
|
|
12
|
+
url.search || url.hash || !/^(?:[a-z0-9-]+\.)+[a-z][a-z0-9-]*$/.test(url.hostname)) {
|
|
13
|
+
throw new Error("自建 HTTPS 入口需要一个 HTTPS 域名");
|
|
14
|
+
}
|
|
15
|
+
return url;
|
|
16
|
+
}
|
|
17
|
+
export function readDefaultRelayUrl() {
|
|
18
|
+
const configured = process.env.AGENTROAM_HTTPS_RELAY_URL?.trim();
|
|
19
|
+
if (configured)
|
|
20
|
+
return configured;
|
|
21
|
+
const path = new URL("../self-hosted-relay.json", import.meta.url);
|
|
22
|
+
try {
|
|
23
|
+
const url = JSON.parse(readFileSync(path, "utf8")).url;
|
|
24
|
+
return typeof url === "string" ? url : null;
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
if (error.code !== "ENOENT")
|
|
28
|
+
throw error;
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
async function installationToken(dataDir) {
|
|
33
|
+
const path = join(dataDir, "relay", "identity");
|
|
34
|
+
const existing = await readToken(path);
|
|
35
|
+
if (existing)
|
|
36
|
+
return existing;
|
|
37
|
+
const temporary = `${path}.${randomBytes(8).toString("hex")}.pending`;
|
|
38
|
+
await writePrivateText(temporary, randomBytes(32).toString("base64url"));
|
|
39
|
+
try {
|
|
40
|
+
await link(temporary, path);
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
if (error.code !== "EEXIST")
|
|
44
|
+
throw error;
|
|
45
|
+
}
|
|
46
|
+
finally {
|
|
47
|
+
await unlink(temporary);
|
|
48
|
+
}
|
|
49
|
+
const token = await readToken(path);
|
|
50
|
+
if (!token)
|
|
51
|
+
throw new Error("自建 HTTPS 入口的本机身份未能保存");
|
|
52
|
+
return token;
|
|
53
|
+
}
|
|
54
|
+
async function readToken(path) {
|
|
55
|
+
const token = await readFile(path, "utf8").catch((error) => {
|
|
56
|
+
if (error.code === "ENOENT")
|
|
57
|
+
return null;
|
|
58
|
+
throw error;
|
|
59
|
+
});
|
|
60
|
+
if (token !== null && !tokenPattern.test(token))
|
|
61
|
+
throw new Error("自建 HTTPS 入口的本机身份文件无效");
|
|
62
|
+
return token;
|
|
63
|
+
}
|
|
64
|
+
export class SelfHostedTunnelProvider {
|
|
65
|
+
url;
|
|
66
|
+
dataDir;
|
|
67
|
+
dial;
|
|
68
|
+
constructor(url, dataDir, dial = (url, options) => new WebSocket(url, options)) {
|
|
69
|
+
this.url = url;
|
|
70
|
+
this.dataDir = dataDir;
|
|
71
|
+
this.dial = dial;
|
|
72
|
+
}
|
|
73
|
+
async start({ localUrl, signal }) {
|
|
74
|
+
const root = relayOrigin(this.url);
|
|
75
|
+
const token = await installationToken(this.dataDir);
|
|
76
|
+
const local = new URL(localUrl);
|
|
77
|
+
if (local.protocol !== "http:" || local.hostname !== "127.0.0.1" || !local.port) {
|
|
78
|
+
throw new Error("自建 HTTPS 入口只能转发本机 AgentRoam 服务");
|
|
79
|
+
}
|
|
80
|
+
const name = createHash("sha256").update(token).digest("hex").slice(0, 32);
|
|
81
|
+
const publicUrl = `https://d-${name}.${root.host}`;
|
|
82
|
+
const options = { headers: { authorization: `Bearer ${token}` }, handshakeTimeout: 8_000, maxPayload: 1024 * 1024, perMessageDeflate: false };
|
|
83
|
+
const websocket = (path) => this.dial(new URL(`wss://${root.host}${path}`), options);
|
|
84
|
+
signal.throwIfAborted();
|
|
85
|
+
const control = websocket("/v1/connect");
|
|
86
|
+
const streams = new Set();
|
|
87
|
+
const close = () => {
|
|
88
|
+
control.terminate();
|
|
89
|
+
for (const socket of streams)
|
|
90
|
+
socket.terminate();
|
|
91
|
+
};
|
|
92
|
+
const exited = new Promise((resolve) => {
|
|
93
|
+
control.once("close", code => {
|
|
94
|
+
signal.removeEventListener("abort", close);
|
|
95
|
+
for (const socket of streams)
|
|
96
|
+
socket.terminate();
|
|
97
|
+
resolve({ code, signal: null });
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
signal.addEventListener("abort", close, { once: true });
|
|
101
|
+
try {
|
|
102
|
+
await new Promise((resolve, reject) => {
|
|
103
|
+
let ready = false;
|
|
104
|
+
control.on("error", reject);
|
|
105
|
+
void exited.then(() => reject(new Error("自建 HTTPS 入口连接已断开")));
|
|
106
|
+
control.on("message", chunk => {
|
|
107
|
+
try {
|
|
108
|
+
const message = JSON.parse(String(chunk));
|
|
109
|
+
if (!ready && message?.type === "ready" && message.publicUrl === publicUrl) {
|
|
110
|
+
ready = true;
|
|
111
|
+
resolve();
|
|
112
|
+
}
|
|
113
|
+
else if (ready && message?.type === "stream" && /^[a-f0-9]{32}$/.test(message.id) && streams.size < 64) {
|
|
114
|
+
const socket = websocket(`/v1/streams/${message.id}`);
|
|
115
|
+
streams.add(socket);
|
|
116
|
+
socket.once("close", () => streams.delete(socket));
|
|
117
|
+
connectToLoopback(socket, Number(local.port));
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
close();
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
reject(error);
|
|
125
|
+
close();
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
signal.throwIfAborted();
|
|
130
|
+
return { publicUrl, exited, close: async () => { close(); await exited; } };
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
close();
|
|
134
|
+
throw error;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
function connectToLoopback(socket, port) {
|
|
139
|
+
const stream = createWebSocketStream(socket);
|
|
140
|
+
// The remote can send immediately after the WebSocket handshake. Buffer on
|
|
141
|
+
// the stream until the local service accepts the socket.
|
|
142
|
+
const local = connect({ host: "127.0.0.1", port });
|
|
143
|
+
stream.on("error", () => local.destroy());
|
|
144
|
+
local.on("error", () => stream.destroy());
|
|
145
|
+
stream.once("close", () => local.destroy());
|
|
146
|
+
local.once("connect", () => stream.pipe(local).pipe(stream));
|
|
147
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
1
2
|
import { selectRelay } from "./relay-orchestrator.js";
|
|
2
3
|
/** Owns only the public route. Server, agents and terminals have a separate lifetime. */
|
|
3
4
|
export class TunnelController {
|
|
@@ -93,6 +94,15 @@ export class TunnelController {
|
|
|
93
94
|
if (this.stopped)
|
|
94
95
|
return;
|
|
95
96
|
await this.commit({ ...this.fallback(url), phase: "error", error: "公网连接已断开,请重试连接。" });
|
|
97
|
+
if (selection.provider !== "self-hosted")
|
|
98
|
+
return;
|
|
99
|
+
for (const wait of [1000, 2000, 5000]) {
|
|
100
|
+
await delay(wait, undefined, { signal: this.abort.signal }).catch(() => { });
|
|
101
|
+
if (this.stopped || this.state.phase === "ready")
|
|
102
|
+
return;
|
|
103
|
+
if ((await this.connect()).phase === "ready")
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
96
106
|
}
|
|
97
107
|
set(status) {
|
|
98
108
|
this.state = status;
|
|
@@ -52,6 +52,7 @@ export async function startUpdate(options) {
|
|
|
52
52
|
port: service?.port ?? null,
|
|
53
53
|
relay: service?.relay ?? "auto",
|
|
54
54
|
tunnelCommand: service?.tunnelCommand ?? null,
|
|
55
|
+
...(service?.relayUrl ? { relayUrl: service.relayUrl } : {}),
|
|
55
56
|
localOnly: service?.localOnly ?? false,
|
|
56
57
|
cliPath: resolve(options.cliPath),
|
|
57
58
|
updatedAt: Date.now(),
|
|
@@ -54,6 +54,8 @@ export async function runUpdateWorker(stateFile, fetchImpl = fetch) {
|
|
|
54
54
|
serviceArgs.push("--port", String(state.port));
|
|
55
55
|
if (state.tunnelCommand)
|
|
56
56
|
serviceArgs.push("--tunnel-command", state.tunnelCommand);
|
|
57
|
+
if (state.relayUrl)
|
|
58
|
+
serviceArgs.push("--relay-url", state.relayUrl);
|
|
57
59
|
if (state.localOnly)
|
|
58
60
|
serviceArgs.push("--local-only");
|
|
59
61
|
await update({ phase: "reconnecting" });
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { writeFile } from "node:fs/promises";
|
|
2
|
+
import { relayOrigin } from "../dist/tunnel/self-hosted-provider.js";
|
|
3
|
+
|
|
4
|
+
const configured = process.env.AGENTROAM_DEFAULT_HTTPS_RELAY?.trim();
|
|
5
|
+
const defaults = configured ? { url: relayOrigin(configured).origin } : {};
|
|
6
|
+
await writeFile(new URL("../dist/self-hosted-relay.json", import.meta.url), JSON.stringify(defaults) + "\n");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "agentroam",
|
|
3
|
-
"version": "0.2.0-preview.
|
|
3
|
+
"version": "0.2.0-preview.79",
|
|
4
4
|
"description": "Zero-install mobile remote terminal and file console",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"README.md"
|
|
18
18
|
],
|
|
19
19
|
"scripts": {
|
|
20
|
-
"build": "tsc -p tsconfig.build.json",
|
|
20
|
+
"build": "tsc -p tsconfig.build.json && node install/write-relay-default.mjs",
|
|
21
21
|
"test": "vitest run --root ../.. packages/cli/src",
|
|
22
22
|
"prepack": "npm run build"
|
|
23
23
|
},
|
|
@@ -27,12 +27,14 @@
|
|
|
27
27
|
"ws": "^8.21.3"
|
|
28
28
|
},
|
|
29
29
|
"optionalDependencies": {
|
|
30
|
-
"agentroam-runtime-darwin-arm64": "0.2.0-preview.
|
|
31
|
-
"agentroam-runtime-win32-x64": "0.2.0-preview.
|
|
32
|
-
"agentroam-cloudflared-darwin-arm64": "0.2.0-preview.
|
|
33
|
-
"agentroam-cloudflared-win32-x64": "0.2.0-preview.
|
|
34
|
-
"agentroam-
|
|
35
|
-
"
|
|
30
|
+
"agentroam-runtime-darwin-arm64": "0.2.0-preview.79",
|
|
31
|
+
"agentroam-runtime-win32-x64": "0.2.0-preview.79",
|
|
32
|
+
"agentroam-cloudflared-darwin-arm64": "0.2.0-preview.78",
|
|
33
|
+
"agentroam-cloudflared-win32-x64": "0.2.0-preview.78",
|
|
34
|
+
"agentroam-tailscale-darwin-arm64": "0.2.0-preview.77",
|
|
35
|
+
"agentroam-tailscale-win32-x64": "0.2.0-preview.77",
|
|
36
|
+
"agentroam-tui-darwin-arm64": "0.2.0-preview.79",
|
|
37
|
+
"@caoqu/agentroam-tui-win32-x64": "0.2.0-preview.79"
|
|
36
38
|
},
|
|
37
39
|
"devDependencies": {
|
|
38
40
|
"@types/node": "^22.0.0",
|