Browse Source

feat-step3-auth-fix-gate-confirm

master
Serge Shutov 3 weeks ago
parent
commit
406ad1d120
  1. 4
      bridge/server.js
  2. 20
      content/bridge-client.js
  3. 21
      content/content.js

4
bridge/server.js

@ -24,7 +24,7 @@ try {
process.exit(1); process.exit(1);
} }
if (!config.token || String(config.token).length < 32) { 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); process.exit(1);
} }
@ -243,7 +243,7 @@ const server = http.createServer(async (req, res) => {
throw new ToolError('E_BUSY', '请求过于频繁(60/min)'); throw new ToolError('E_BUSY', '请求过于频繁(60/min)');
} }
if (!authOk(req)) { 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; let body = null;
if (req.method === 'POST') { if (req.method === 'POST') {

20
content/bridge-client.js

@ -4,6 +4,7 @@
}(typeof globalThis !== "undefined" ? globalThis : this, function () { }(typeof globalThis !== "undefined" ? globalThis : this, function () {
"use strict"; "use strict";
const TOKEN_KEY = "dshpro-bridge-token"; const TOKEN_KEY = "dshpro-bridge-token";
const ENABLED_KEY = "dsh-memory-enabled";
function available() { function available() {
return typeof chrome !== "undefined" && chrome.runtime && typeof chrome.runtime.sendMessage === "function"; 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
};
})); }));

21
content/content.js

@ -81,6 +81,17 @@
} }
pump(); 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() { function pump() {
if (pumping) return; if (pumping) return;
if (queue.length === 0) return; if (queue.length === 0) return;
@ -91,7 +102,7 @@
return; return;
} }
var call = queue.shift(); var call = queue.shift();
BRIDGE.sendCall(call).then(function (res) { executeOne(call).then(function (res) {
if (res) { if (res) {
if (res.ok) { if (res.ok) {
console.log("[dsh-memory] call ok", call.tool, res.durationMs); console.log("[dsh-memory] call ok", call.tool, res.durationMs);
@ -142,7 +153,12 @@
if (pending.size > 0) schedule(); 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) { if (!has) {
console.warn("[dsh-memory] no bridge token, idle"); console.warn("[dsh-memory] no bridge token, idle");
return; return;
@ -154,4 +170,5 @@
}); });
console.log("[dsh-memory] content.js active"); console.log("[dsh-memory] content.js active");
}); });
});
})(); })();

Loading…
Cancel
Save