From 406ad1d120607545a068203bcc76164487d4b20d Mon Sep 17 00:00:00 2001 From: Serge Alex Date: Fri, 30 Oct 2026 03:14:37 +0300 Subject: [PATCH] feat-step3-auth-fix-gate-confirm --- bridge/server.js | 4 ++-- content/bridge-client.js | 20 +++++++++++++++++++- content/content.js | 21 +++++++++++++++++++-- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/bridge/server.js b/bridge/server.js index a9fdda8..c29e243 100644 --- a/bridge/server.js +++ b/bridge/server.js @@ -24,7 +24,7 @@ try { process.exit(1); } if (!config.token || String(config.token).length < 32) { - console.error('[DSH bridge] config.json token is empty. Fill bridge/config.json token first.'); + console.error('[DSH bridge] STARTUP: token missing or short, exiting'); process.exit(1); } @@ -243,7 +243,7 @@ const server = http.createServer(async (req, res) => { throw new ToolError('E_BUSY', '请求过于频繁(60/min)'); } if (!authOk(req)) { - console.error('[DSH bridge] config.json token is empty. Fill bridge/config.json token first.'); + throw new ToolError('E_UNAUTHORIZED', 'invalid or missing bearer token'); } let body = null; if (req.method === 'POST') { diff --git a/content/bridge-client.js b/content/bridge-client.js index 6f8d9f9..f063c3e 100644 --- a/content/bridge-client.js +++ b/content/bridge-client.js @@ -4,6 +4,7 @@ }(typeof globalThis !== "undefined" ? globalThis : this, function () { "use strict"; const TOKEN_KEY = "dshpro-bridge-token"; + const ENABLED_KEY = "dsh-memory-enabled"; function available() { return typeof chrome !== "undefined" && chrome.runtime && typeof chrome.runtime.sendMessage === "function"; @@ -54,5 +55,22 @@ }); } - return { sendCall: sendCall, health: health, hasToken: hasToken, available: available, TOKEN_KEY: TOKEN_KEY }; + function isEnabled() { + return new Promise(function (resolve) { + if (typeof chrome === "undefined" || !chrome.storage) { resolve(false); return; } + chrome.storage.local.get(ENABLED_KEY, function (v) { + resolve(Boolean(v && v[ENABLED_KEY])); + }); + }); + } + + return { + sendCall: sendCall, + health: health, + hasToken: hasToken, + isEnabled: isEnabled, + available: available, + TOKEN_KEY: TOKEN_KEY, + ENABLED_KEY: ENABLED_KEY + }; })); diff --git a/content/content.js b/content/content.js index abbc014..cbd8a64 100644 --- a/content/content.js +++ b/content/content.js @@ -81,6 +81,17 @@ } pump(); } + function executeOne(call) { + return BRIDGE.sendCall(call).then(function (res) { + var e = res && !res.ok && res.error; + var needConfirm = e && e.code === "E_NEEDS_CONFIRM" && e.extra && e.extra.confirmToken; + if (needConfirm) { + console.log("[dsh-memory] needs confirm, retrying", call.tool); + return BRIDGE.sendCall({ tool: call.tool, args: call.args, confirmToken: e.extra.confirmToken }); + } + return res; + }); + } function pump() { if (pumping) return; if (queue.length === 0) return; @@ -91,7 +102,7 @@ return; } var call = queue.shift(); - BRIDGE.sendCall(call).then(function (res) { + executeOne(call).then(function (res) { if (res) { if (res.ok) { console.log("[dsh-memory] call ok", call.tool, res.durationMs); @@ -142,7 +153,12 @@ if (pending.size > 0) schedule(); }); - BRIDGE.hasToken().then(function (has) { + BRIDGE.isEnabled().then(function (enabled) { + if (!enabled) { + console.warn("[dsh-memory] disabled (dsh-memory-enabled=false), idle"); + return; + } + return BRIDGE.hasToken().then(function (has) { if (!has) { console.warn("[dsh-memory] no bridge token, idle"); return; @@ -154,4 +170,5 @@ }); console.log("[dsh-memory] content.js active"); }); + }); })();