@chenacc/dsh-agentbridge 0.3.0 → 0.3.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/lib/index.js CHANGED
@@ -4,7 +4,7 @@ import { createRequire } from "node:module";
4
4
  import { homedir } from "node:os";
5
5
  import path from "node:path";
6
6
  import { fileURLToPath } from "node:url";
7
- import { ensureAccessKey, findCloudflared, startQuickTunnel } from "./tunnel.js";
7
+ import { ensureAccessKey, ensureAutostartToken, findCloudflared, readAutostartToken, startQuickTunnel } from "./tunnel.js";
8
8
 
9
9
  const name = "agentbridge-dsh-phone";
10
10
  // `agents` is not optional: creating a conversation from the phone asks the agent
@@ -198,6 +198,9 @@ async function startBridge(origin, log = () => {}, extraEnv = {}) {
198
198
  // Marks this child as the plugin's own, so a later launch may stop it to pick
199
199
  // up a new address. A bridge the user started by hand never gets this flag.
200
200
  AGENTBRIDGE_AUTOSTART: "1",
201
+ // The bridge only accepts a restart request carrying this token, which is how
202
+ // the advertised address changes once a tunnel exists.
203
+ AGENTBRIDGE_AUTOSTART_TOKEN: ensureAutostartToken(dataDir),
201
204
  ...extraEnv,
202
205
  };
203
206
  // Under Electron `process.execPath` is DSH Desktop itself, not node: launching
@@ -326,11 +329,16 @@ async function resolveRemoteOrigin(log) {
326
329
  * endpoint reports whether it was autostarted, and only that flag authorises this.
327
330
  */
328
331
  async function stopBridge(origin, log) {
332
+ const token = readAutostartToken(bridgeDataDir());
333
+ if (!token) {
334
+ log("cannot restart the bridge: its autostart token is missing");
335
+ return false;
336
+ }
329
337
  let response;
330
338
  try {
331
- response = await fetch(`${origin}/api/local/shutdown`, {
339
+ response = await fetch(`${origin}/api/local/autostart/shutdown`, {
332
340
  method: "POST",
333
- headers: { "content-type": "application/json" },
341
+ headers: { "content-type": "application/json", "x-agentbridge-autostart": token },
334
342
  body: "{}",
335
343
  signal: AbortSignal.timeout(5_000),
336
344
  });
package/lib/tunnel.js CHANGED
@@ -74,6 +74,10 @@ export function accessKeyPath(dataDir) {
74
74
  return path.join(dataDir, "access-key");
75
75
  }
76
76
 
77
+ export function autostartTokenPath(dataDir) {
78
+ return path.join(dataDir, "autostart-token");
79
+ }
80
+
77
81
  /**
78
82
  * The key that gates a publicly exposed bridge.
79
83
  *
@@ -82,17 +86,40 @@ export function accessKeyPath(dataDir) {
82
86
  * out of its own bridge.
83
87
  */
84
88
  export function ensureAccessKey(dataDir, { readFile = readFileSync, writeFile = writeFileSync, mkdir = mkdirSync, random } = {}) {
85
- const target = accessKeyPath(dataDir);
89
+ return ensureSecret(accessKeyPath(dataDir), dataDir, { readFile, writeFile, mkdir, random });
90
+ }
91
+
92
+ /**
93
+ * The secret that lets this plugin ask its own bridge to restart.
94
+ *
95
+ * A restart is how the advertised address changes (the tunnel hostname only
96
+ * exists once cloudflared is up), and the bridge accepts that request only from
97
+ * loopback *with this token* — so a random local page cannot stop the bridge, and
98
+ * a second, unrelated DSH profile cannot either.
99
+ */
100
+ export function ensureAutostartToken(dataDir, options = {}) {
101
+ return ensureSecret(autostartTokenPath(dataDir), dataDir, options);
102
+ }
103
+
104
+ export function readAutostartToken(dataDir, { readFile = readFileSync } = {}) {
105
+ try {
106
+ return String(readFile(autostartTokenPath(dataDir), "utf8")).trim() || null;
107
+ } catch {
108
+ return null;
109
+ }
110
+ }
111
+
112
+ function ensureSecret(target, dataDir, { readFile = readFileSync, writeFile = writeFileSync, mkdir = mkdirSync, random } = {}) {
86
113
  try {
87
114
  const existing = String(readFile(target, "utf8")).trim();
88
115
  if (existing.length >= 16) return existing;
89
116
  } catch {
90
- // No usable key on disk yet.
117
+ // No usable secret on disk yet.
91
118
  }
92
- const key = (random ?? (() => randomBytes(24).toString("base64url")))();
119
+ const secret = (random ?? (() => randomBytes(24).toString("base64url")))();
93
120
  mkdir(dataDir, { recursive: true });
94
- writeFile(target, key, "utf8");
95
- return key;
121
+ writeFile(target, secret, "utf8");
122
+ return secret;
96
123
  }
97
124
 
98
125
  export function tunnelStatePath(dataDir) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chenacc/dsh-agentbridge",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "AgentBridge for DSH Desktop: run /phone in a conversation to pair a phone console with that exact live Agent.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -45,7 +45,7 @@
45
45
  }
46
46
  },
47
47
  "dependencies": {
48
- "@chenacc/agentbridge": "^0.1.2"
48
+ "@chenacc/agentbridge": "^0.1.3"
49
49
  },
50
50
  "peerDependencies": {
51
51
  "@deepseek-ai/dsh-commands": "^0.1.0-rc.7",