#!/usr/bin/env node /** * 给弹匣加「DSH 接入」四处改动(本地源码)。 * * 1. GET /markmag/api/dsh/link 浏览器打开 → 显示一次性码 * 2. POST /api/dsh/link-exchange 桌面端拿码 → 换回成员 key + 工具凭证 * 3. POST /api/dsh/tool 桌面端调 4 个工具(凭证自验,不走会话门) * 4. do_POST 会话门放行 2、3 它们天生不能要求登录,靠码/凭证+限流自保 * * 幂等:已经改过就跳过。改之前备份。只改本地源码,线上要另跑部署。 */ import fs from 'node:fs' import { homedir } from 'node:os' import { join } from 'node:path' const HOME = homedir() const SRC = join(HOME, 'Documents/Essay_AI/Essay_system/dsh-markmag') const SRV = join(HOME, 'markmag/server') const MAIN = join(SRV, 'main.py') const MOD = join(SRV, 'sources/dsh_link.py') const STAMP = '20261107-dsh' const log = (k, v) => console.log(k.padEnd(16), v) const die = (m) => { console.error('✗ ' + m); process.exit(1) } if (!fs.existsSync(MAIN)) die('没找到 ' + MAIN) const bak = MAIN + '.bak-' + STAMP if (!fs.existsSync(bak)) fs.copyFileSync(MAIN, bak) log('backup:', fs.existsSync(bak) ? 'ok' : 'FAIL') fs.copyFileSync(join(SRC, 'deploy/markmag/dsh_link.py'), MOD) fs.copyFileSync(join(SRC, 'deploy/markmag/dsh_revoke.py'), join(SRV, 'sources/dsh_revoke.py')) log('module:', fs.existsSync(MOD) ? 'dsh_link.py + dsh_revoke.py' : 'FAIL') let src = fs.readFileSync(MAIN, 'utf8') const before = src // ── patch 1:GET 页面 ───────────────────────────────────── const GET_ANCHOR = ' # 默认拒绝(server 实例):API → 401 JSON;页面 → 302 引导登录页' const GET_BLOCK = [ ' if path == "api/dsh/link": # 11-07 DSH 接入码:浏览器打开显示一次性码', ' s = self._sess()', ' if not s:', ' self.send_response(302)', ' self.send_header("Location", "/markmag/auth/callback.html")', ' self.send_header("Content-Length", "0")', ' self.end_headers()', ' return', ' import sources.dsh_link as DL', ' r = DL.issue(s)', ' if not r.get("ok"):', ' self._send(400, r)', ' return', ' self._send(200, DL.page_html(r["code"], r["expires_in"]).encode("utf-8"),', ' "text/html; charset=utf-8")', ' return', '', '', ].join('\n') if (src.includes('if path == "api/dsh/link":')) { log('get route:', 'skip') } else { if (!src.includes(GET_ANCHOR)) die('GET 锚点没找到') src = src.replace(GET_ANCHOR, GET_BLOCK + GET_ANCHOR) log('get route:', 'added') } // ── patch 2:POST 兑换 ──────────────────────────────────── const POST_ANCHOR = ' elif path == "api/agent/token" and self.command == "POST":' const POST_BLOCK = [ ' elif path == "api/dsh/link-exchange" and self.command == "POST": # 11-07 桌面端拿码换成员 key', ' import sources.dsh_link as DL', ' r = DL.redeem(body.get("code", ""))', ' self._send(200 if r.get("ok") else 400, r)', ].join('\n') if (src.includes('elif path == "api/dsh/link-exchange"')) { log('post route:', 'skip') } else { if (!src.includes(POST_ANCHOR)) die('POST 锚点没找到') src = src.replace(POST_ANCHOR, POST_BLOCK + '\n' + POST_ANCHOR) log('post route:', 'added') } // ── patch 3:POST 工具面 ────────────────────────────────── const TOOL_BLOCK = [ ' elif path == "api/dsh/tool" and self.command == "POST": # 11-07 DSH 工具面:凭证自验,不走会话门', ' import sources.dsh_link as DL', ' r = DL.dispatch(self.headers.get("X-MarkMag-Token", ""), body.get("tool", ""), body.get("args") or {})', ' self._send(200 if r.get("ok") else 401, r)', ].join('\n') if (src.includes('elif path == "api/dsh/tool"')) { log('tool route:', 'skip') } else { if (!src.includes(POST_ANCHOR)) die('POST 锚点没找到(工具面)') src = src.replace(POST_ANCHOR, TOOL_BLOCK + '\n' + POST_ANCHOR) log('tool route:', 'added') } // ── patch 4:会话门放行两条自证路径 ─────────────────────── const GATE_RE = /^(\s+)if not config\.PRIVATE_ENABLED and path .*not self\._sess\(\):$/m const GATE_WANT = 'if not config.PRIVATE_ENABLED and path not in ("api/auth/dingtalk/login", "api/dsh/link-exchange", "api/dsh/tool") and not self._sess():' if (src.includes('"api/dsh/tool")')) { log('session gate:', 'skip') } else if (!GATE_RE.test(src)) { die('会话门那行没找到,main.py 结构和预期不符') } else { src = src.replace(GATE_RE, (m, indent) => indent + GATE_WANT) log('session gate:', 'opened for exchange + tool') } // ── patch 5:成员停用即吊销 key + 手动吊销端点 ──────────── const SETACTIVE_OLD = [ ' elif path == "api/admin/members/set-active" and self.command == "POST":', ' if not self._admin():', ' return self._send(403, {"err": "forbidden"})', ' import sources.members_store as M', ' ok = M.set_active(body.get("union_id"), bool(body.get("active")))', ' self._send(200 if ok else 404, {"ok": ok})', ].join('\n') const SETACTIVE_NEW = [ ' elif path == "api/admin/members/set-active" and self.command == "POST":', ' if not self._admin():', ' return self._send(403, {"err": "forbidden"})', ' import sources.members_store as M', ' uid_x = body.get("union_id")', ' want_on = bool(body.get("active"))', ' ok = M.set_active(uid_x, want_on)', ' extra = {}', ' if ok and not want_on: # 11-07 停用即吊销他名下全部 TDB key(离职即断)', ' import sources.dsh_revoke as RV', ' extra["keys"] = RV.revoke_member_keys(uid_x)', ' self._send(200 if ok else 404, {"ok": ok, **extra})', ' elif path == "api/admin/members/revoke-keys" and self.command == "POST": # 11-07 手动吊销(补漏用)', ' if not self._admin():', ' return self._send(403, {"err": "forbidden"})', ' import sources.dsh_revoke as RV', ' self._send(200, RV.revoke_member_keys(body.get("union_id")))', ' elif path == "api/admin/members/key-status" and self.command == "POST": # 11-07 只看现状,不动数据', ' if not self._admin():', ' return self._send(403, {"err": "forbidden"})', ' import sources.dsh_revoke as RV', ' self._send(200, RV.revoke_status(body.get("union_id")))', ].join('\n') if (src.includes('api/admin/members/revoke-keys')) { log('revoke hook:', 'skip') } else if (!src.includes(SETACTIVE_OLD)) { die('set-active 端点结构和预期不符,不敢改') } else { src = src.replace(SETACTIVE_OLD, SETACTIVE_NEW) log('revoke hook:', 'wired into set-active') } if (src !== before) fs.writeFileSync(MAIN, src) // ── 自检 ────────────────────────────────────────────────── const after = fs.readFileSync(MAIN, 'utf8') const checks = { getRoute: after.includes('if path == "api/dsh/link":'), exchangeRoute: after.includes('elif path == "api/dsh/link-exchange"'), toolRoute: after.includes('elif path == "api/dsh/tool"'), gateOpen: after.includes('"api/dsh/tool")'), moduleExists: fs.existsSync(MOD), toolTokenInModule: fs.readFileSync(MOD, 'utf8').includes('issue_tool_token'), revokeWired: after.includes('RV.revoke_member_keys(uid_x)'), revokeModule: fs.existsSync(join(SRV, 'sources/dsh_revoke.py')), } log('SELF_CHECK', JSON.stringify(checks)) if (!Object.values(checks).every(Boolean)) { console.log('\n✗ 自检没过,用备份回滚:' + bak) process.exit(1) }