@hjd-hitsz/claudex-web 0.0.0-stage → 4.0.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/.env.example +27 -0
- package/CHANGELOG.md +449 -0
- package/LICENSE +674 -0
- package/README.md +128 -2
- package/bin/claudex-web.mjs +129 -0
- package/build-info.json +8 -0
- package/client/dist/_baseMerge.js +1 -0
- package/client/dist/abnfDiagram-WEYOIA3C.js +1 -0
- package/client/dist/apl.js +1 -0
- package/client/dist/app.js +393 -0
- package/client/dist/apple-touch-icon.png +0 -0
- package/client/dist/arc.js +1 -0
- package/client/dist/architectureDiagram-ZHVYKT53.js +36 -0
- package/client/dist/asciiarmor.js +1 -0
- package/client/dist/asn1.js +1 -0
- package/client/dist/asterisk.js +1 -0
- package/client/dist/blockDiagram-RJAE7QA3.js +140 -0
- package/client/dist/brainfuck.js +1 -0
- package/client/dist/c4Diagram-XDPPPE54.js +38 -0
- package/client/dist/channel.js +1 -0
- package/client/dist/chunk-2E7WHVMD.js +88 -0
- package/client/dist/chunk-2Q5K7J3B.js +1 -0
- package/client/dist/chunk-4EMNKR2T.js +1 -0
- package/client/dist/chunk-55XCU76K.js +1 -0
- package/client/dist/chunk-5VM5RSS4.js +15 -0
- package/client/dist/chunk-JWPE2WC7.js +1 -0
- package/client/dist/chunk-VQ5W62QI.js +1 -0
- package/client/dist/chunk-XXDRQBXY.js +1 -0
- package/client/dist/chunk-ZCHMQSOR.js +2 -0
- package/client/dist/classDiagram-v2-4MYWR3UA.js +217 -0
- package/client/dist/clike.js +1 -0
- package/client/dist/clojure.js +1 -0
- package/client/dist/cmake.js +1 -0
- package/client/dist/cobol.js +1 -0
- package/client/dist/code-highlight-style.js +13 -0
- package/client/dist/coffeescript.js +1 -0
- package/client/dist/commonlisp.js +1 -0
- package/client/dist/cose-bilkent-JH36ORCC.js +1 -0
- package/client/dist/crystal.js +1 -0
- package/client/dist/css.js +1 -0
- package/client/dist/cynefin-EF2NZ3EQ.js +141 -0
- package/client/dist/cynefinDiagram-WF24UDRP.js +62 -0
- package/client/dist/cypher.js +1 -0
- package/client/dist/cytoscape.esm.js +321 -0
- package/client/dist/d.js +1 -0
- package/client/dist/dagre-NK4GIZVX.js +4 -0
- package/client/dist/defaultLocale.js +1 -0
- package/client/dist/diagram-CBK3MWTC.js +24 -0
- package/client/dist/diagram-I5XIIV2D.js +24 -0
- package/client/dist/diagram-ODIL2UCW.js +30 -0
- package/client/dist/diagram-PFBEJUXE.js +3 -0
- package/client/dist/diagram-RJA3I4VV.js +41 -0
- package/client/dist/diagram-TYUNFSNR.js +200 -0
- package/client/dist/diff.js +1 -0
- package/client/dist/dockerfile.js +1 -0
- package/client/dist/document-editor.js +1040 -0
- package/client/dist/dtd.js +1 -0
- package/client/dist/dylan.js +1 -0
- package/client/dist/ebnf.js +1 -0
- package/client/dist/ebnfDiagram-V4P3TTPT.js +1 -0
- package/client/dist/ecl.js +1 -0
- package/client/dist/eiffel.js +1 -0
- package/client/dist/elk-QONTBCTQ.js +27 -0
- package/client/dist/elm.js +1 -0
- package/client/dist/erDiagram-KYQIGQAA.js +99 -0
- package/client/dist/erlang.js +1 -0
- package/client/dist/factor.js +1 -0
- package/client/dist/favicon.ico +0 -0
- package/client/dist/favicon.png +0 -0
- package/client/dist/fcl.js +1 -0
- package/client/dist/file-panel.js +1 -0
- package/client/dist/flowDiagram-IUGLLWBU.js +213 -0
- package/client/dist/forth.js +1 -0
- package/client/dist/fortran.js +1 -0
- package/client/dist/ganttDiagram-R7GPNTJY.js +292 -0
- package/client/dist/gas.js +1 -0
- package/client/dist/gherkin.js +1 -0
- package/client/dist/gitGraphDiagram-PDYL7XHY.js +106 -0
- package/client/dist/graph.js +1 -0
- package/client/dist/groovy.js +1 -0
- package/client/dist/haskell.js +1 -0
- package/client/dist/haxe.js +1 -0
- package/client/dist/http.js +1 -0
- package/client/dist/idl.js +1 -0
- package/client/dist/index.html +25 -0
- package/client/dist/index.js +2 -0
- package/client/dist/index10.js +1 -0
- package/client/dist/index11.js +1 -0
- package/client/dist/index12.js +1 -0
- package/client/dist/index13.js +7 -0
- package/client/dist/index14.js +1 -0
- package/client/dist/index15.js +1 -0
- package/client/dist/index16.js +6 -0
- package/client/dist/index17.js +1 -0
- package/client/dist/index18.js +3 -0
- package/client/dist/index19.js +1 -0
- package/client/dist/index2.js +1 -0
- package/client/dist/index20.js +1 -0
- package/client/dist/index21.js +1 -0
- package/client/dist/index22.js +1 -0
- package/client/dist/index3.js +1 -0
- package/client/dist/index4.js +1 -0
- package/client/dist/index5.js +1 -0
- package/client/dist/index6.js +13 -0
- package/client/dist/index7.js +1 -0
- package/client/dist/index8.js +1 -0
- package/client/dist/index9.js +1 -0
- package/client/dist/infoDiagram-MVDRKAX5.js +2 -0
- package/client/dist/init.js +1 -0
- package/client/dist/isArrayLikeObject.js +1 -0
- package/client/dist/ishikawaDiagram-HPEHV2GE.js +70 -0
- package/client/dist/javascript.js +1 -0
- package/client/dist/journeyDiagram-BOSHW7O7.js +139 -0
- package/client/dist/julia.js +1 -0
- package/client/dist/kanban-definition-FNSA4HC5.js +89 -0
- package/client/dist/katex.js +257 -0
- package/client/dist/linear.js +1 -0
- package/client/dist/livescript.js +1 -0
- package/client/dist/lua.js +1 -0
- package/client/dist/mathematica.js +1 -0
- package/client/dist/mbox.js +1 -0
- package/client/dist/mermaid.core.js +310 -0
- package/client/dist/mindmap-definition-6OY57UG3.js +98 -0
- package/client/dist/mirc.js +1 -0
- package/client/dist/mllike.js +1 -0
- package/client/dist/modelica.js +1 -0
- package/client/dist/mscgen.js +1 -0
- package/client/dist/mumps.js +1 -0
- package/client/dist/nginx.js +1 -0
- package/client/dist/notification-worker.js +45 -0
- package/client/dist/nsis.js +1 -0
- package/client/dist/ntriples.js +1 -0
- package/client/dist/octave.js +1 -0
- package/client/dist/ordinal.js +1 -0
- package/client/dist/oz.js +1 -0
- package/client/dist/pascal.js +1 -0
- package/client/dist/pegDiagram-ECIUJNBX.js +1 -0
- package/client/dist/perl.js +1 -0
- package/client/dist/pieDiagram-GNMPY74T.js +39 -0
- package/client/dist/pig.js +1 -0
- package/client/dist/powershell.js +1 -0
- package/client/dist/properties.js +1 -0
- package/client/dist/protobuf.js +1 -0
- package/client/dist/pug.js +1 -0
- package/client/dist/puppet.js +1 -0
- package/client/dist/purify.es.js +3 -0
- package/client/dist/python.js +1 -0
- package/client/dist/q.js +1 -0
- package/client/dist/quadrantDiagram-D57K4MC3.js +7 -0
- package/client/dist/r.js +1 -0
- package/client/dist/railroadDiagram-Y72NRBL7.js +1 -0
- package/client/dist/requirementDiagram-EQYSW5RA.js +84 -0
- package/client/dist/rpm.js +1 -0
- package/client/dist/ruby.js +1 -0
- package/client/dist/sankeyDiagram-SY24JHO5.js +40 -0
- package/client/dist/sas.js +1 -0
- package/client/dist/scheme.js +1 -0
- package/client/dist/sequenceDiagram-BLEHNHQU.js +169 -0
- package/client/dist/shell.js +1 -0
- package/client/dist/sieve.js +1 -0
- package/client/dist/simple-mode.js +1 -0
- package/client/dist/sizeCapture-INFHLROL.js +1 -0
- package/client/dist/smalltalk.js +1 -0
- package/client/dist/solr.js +1 -0
- package/client/dist/sparql.js +1 -0
- package/client/dist/spreadsheet.js +1 -0
- package/client/dist/sql.js +1 -0
- package/client/dist/stateDiagram-v2-A6IESKWZ.js +291 -0
- package/client/dist/stex.js +1 -0
- package/client/dist/style.css +1 -0
- package/client/dist/stylus.js +1 -0
- package/client/dist/swift.js +1 -0
- package/client/dist/swimlanes-B3ENRKFQ.js +1 -0
- package/client/dist/swimlanesDiagram-AYQRV4AK.js +8 -0
- package/client/dist/tcl.js +1 -0
- package/client/dist/terminal-panel.js +18 -0
- package/client/dist/textile.js +1 -0
- package/client/dist/tiddlywiki.js +1 -0
- package/client/dist/tiki.js +1 -0
- package/client/dist/timeline-definition-JXYKGLGB.js +120 -0
- package/client/dist/toml.js +1 -0
- package/client/dist/troff.js +1 -0
- package/client/dist/ttcn-cfg.js +1 -0
- package/client/dist/ttcn.js +1 -0
- package/client/dist/turtle.js +1 -0
- package/client/dist/usecaseDiagram-WPOLBNNX.js +370 -0
- package/client/dist/vb.js +1 -0
- package/client/dist/vbscript.js +1 -0
- package/client/dist/velocity.js +1 -0
- package/client/dist/vennDiagram-Q3Y3IDCW.js +34 -0
- package/client/dist/verilog.js +1 -0
- package/client/dist/vhdl.js +1 -0
- package/client/dist/wardleyDiagram-IVAIDKWA.js +78 -0
- package/client/dist/webidl.js +1 -0
- package/client/dist/xquery.js +1 -0
- package/client/dist/xychartDiagram-4URNHDU4.js +7 -0
- package/client/dist/yacas.js +1 -0
- package/client/dist/z80.js +1 -0
- package/docs/architecture/backend-regressions.md +53 -0
- package/docs/architecture/backends.md +113 -0
- package/docs/architecture/behavior.md +206 -0
- package/docs/architecture.md +306 -0
- package/docs/background-tasks.md +39 -0
- package/docs/cli-command-coverage.md +60 -0
- package/docs/deployment.md +269 -0
- package/docs/development.md +385 -0
- package/docs/images/desktop-dark.png +0 -0
- package/docs/images/desktop-light.png +0 -0
- package/docs/images/mobile-sidebar.png +0 -0
- package/docs/images/mobile.png +0 -0
- package/docs/releases/v2.0.0.md +5 -0
- package/docs/releases/v2.1.0.md +6 -0
- package/docs/releases/v2.1.1.md +6 -0
- package/docs/releases/v2.1.2.md +8 -0
- package/docs/releases/v2.2.0.md +11 -0
- package/docs/releases/v2.3.0.md +12 -0
- package/docs/releases/v2.3.1.md +8 -0
- package/docs/releases/v2.4.0.md +8 -0
- package/docs/releases/v2.4.1.md +8 -0
- package/docs/releases/v2.4.2.md +8 -0
- package/docs/releases/v3.0.0.md +9 -0
- package/docs/releases/v3.1.0.md +10 -0
- package/docs/releases/v3.1.1.md +7 -0
- package/docs/releases/v4.0.0-beta.1.md +10 -0
- package/docs/releases/v4.0.0.md +20 -0
- package/docs/releases/v4.0.1.md +10 -0
- package/docs/versioning.md +119 -0
- package/package.json +70 -4
- package/scripts/install-services.mjs +134 -0
- package/scripts/restart-when-idle.mjs +125 -0
- package/scripts/start-backend.mjs +29 -0
- package/server/src/agent-messages.mjs +124 -0
- package/server/src/application.mjs +133 -0
- package/server/src/auth.mjs +328 -0
- package/server/src/backends/claude/account.mjs +171 -0
- package/server/src/backends/claude/adapter.mjs +366 -0
- package/server/src/backends/claude/agent-history.mjs +70 -0
- package/server/src/backends/claude/background-tasks.mjs +168 -0
- package/server/src/backends/claude/catalogue.mjs +266 -0
- package/server/src/backends/claude/execution-errors.mjs +44 -0
- package/server/src/backends/claude/execution-session.mjs +390 -0
- package/server/src/backends/claude/git-tool-changes.mjs +77 -0
- package/server/src/backends/claude/goals.mjs +319 -0
- package/server/src/backends/claude/history-actions.mjs +170 -0
- package/server/src/backends/claude/history.mjs +65 -0
- package/server/src/backends/claude/hooks.mjs +232 -0
- package/server/src/backends/claude/input-stream.mjs +24 -0
- package/server/src/backends/claude/messages-request.mjs +34 -0
- package/server/src/backends/claude/messages.mjs +300 -0
- package/server/src/backends/claude/model-options.mjs +18 -0
- package/server/src/backends/claude/native-commands.mjs +48 -0
- package/server/src/backends/claude/native-records.mjs +76 -0
- package/server/src/backends/claude/process-ownership.mjs +82 -0
- package/server/src/backends/claude/project-instructions.mjs +40 -0
- package/server/src/backends/claude/provider-balance.mjs +40 -0
- package/server/src/backends/claude/provider-environment.mjs +93 -0
- package/server/src/backends/claude/provider-transport.mjs +70 -0
- package/server/src/backends/claude/recovery.mjs +54 -0
- package/server/src/backends/claude/runtime.mjs +463 -0
- package/server/src/backends/claude/session-worker.mjs +32 -0
- package/server/src/backends/claude/sessions.mjs +347 -0
- package/server/src/backends/claude/token-usage.mjs +98 -0
- package/server/src/backends/claude/worker-client.mjs +344 -0
- package/server/src/backends/claude/worker-launcher.mjs +119 -0
- package/server/src/backends/claude/worker-location.mjs +31 -0
- package/server/src/backends/claude/worker-revision.mjs +45 -0
- package/server/src/backends/claude/worker-server.mjs +290 -0
- package/server/src/backends/cli-installation.mjs +140 -0
- package/server/src/backends/codex/adapter.mjs +153 -0
- package/server/src/backends/codex/compaction-recovery.mjs +78 -0
- package/server/src/backends/codex/events.mjs +125 -0
- package/server/src/backends/codex/methods.mjs +17 -0
- package/server/src/backends/codex/reference-mcp.mjs +97 -0
- package/server/src/backends/codex/reference-tools.mjs +71 -0
- package/server/src/backends/codex/requests.mjs +170 -0
- package/server/src/backends/codex/subscriptions.mjs +91 -0
- package/server/src/backends/codex/upstream.mjs +148 -0
- package/server/src/backends/configuration.mjs +122 -0
- package/server/src/backends/identity-mapping.mjs +87 -0
- package/server/src/backends/manager.mjs +412 -0
- package/server/src/backends/network-proxy.mjs +55 -0
- package/server/src/backends/provider-presets.mjs +45 -0
- package/server/src/backends/providers.mjs +132 -0
- package/server/src/backends/storage.mjs +35 -0
- package/server/src/bridge/conversation-policy.mjs +29 -0
- package/server/src/bridge/file-operations.mjs +47 -0
- package/server/src/bridge/product-methods.mjs +66 -0
- package/server/src/compaction.mjs +82 -0
- package/server/src/config.mjs +57 -0
- package/server/src/file-content.mjs +99 -0
- package/server/src/file-undo.mjs +142 -0
- package/server/src/http/router.mjs +80 -0
- package/server/src/http/static.mjs +62 -0
- package/server/src/product-config.mjs +80 -0
- package/server/src/project-models.mjs +59 -0
- package/server/src/rpc-transport.mjs +21 -0
- package/server/src/server.mjs +20 -0
- package/server/src/summary-translation.mjs +82 -0
- package/server/src/system-auth.mjs +45 -0
- package/server/src/thread-folders.mjs +76 -0
- package/server/src/thread-names.mjs +264 -0
- package/server/src/thread-projects.mjs +36 -0
- package/server/src/tls.mjs +87 -0
- package/server/src/version.mjs +55 -0
- package/server/src/web-config-migration.mjs +48 -0
- package/server/src/workspace/commands.mjs +33 -0
- package/server/src/workspace/files.mjs +44 -0
- package/server/src/workspace/service.mjs +27 -0
- package/server/src/workspace/terminals.mjs +122 -0
- package/shared/backend-activity.mjs +13 -0
- package/shared/backend-capabilities.mjs +33 -0
- package/shared/backend-identity.mjs +17 -0
- package/shared/compaction-state.mjs +49 -0
- package/shared/file-types.mjs +82 -0
- package/shared/product-contract.mjs +26 -0
- package/shared/rpc-limits.mjs +2 -0
- package/shared/terminal-workspace.mjs +26 -0
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { createReadStream } from "node:fs";
|
|
2
|
+
import { createInterface } from "node:readline";
|
|
3
|
+
|
|
4
|
+
// 接收记录可能晚于发送调用落盘;完整 Fernet 封包按布局识别,避免暴露排队中的密文。
|
|
5
|
+
function isEncryptedEnvelope(text) {
|
|
6
|
+
if (!/^[A-Za-z0-9_-]+={0,2}$/.test(text)) return false;
|
|
7
|
+
const packet = Buffer.from(text, "base64url");
|
|
8
|
+
const encoded = packet.toString("base64url");
|
|
9
|
+
// 版本 1 + 时间戳 8 + IV 16 + HMAC 32 字节;正文至少包含一个 16 字节块。
|
|
10
|
+
return (
|
|
11
|
+
packet[0] === 0x80 &&
|
|
12
|
+
packet.length >= 73 &&
|
|
13
|
+
(packet.length - 57) % 16 === 0 &&
|
|
14
|
+
(text === encoded ||
|
|
15
|
+
text === encoded.padEnd(Math.ceil(encoded.length / 4) * 4, "="))
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Rollout 中的调用正文补齐 app-server 子代理活动记录省略的发送内容。
|
|
20
|
+
async function scan(path, visit) {
|
|
21
|
+
if (!path) throw new Error("对话记录路径暂不可用,请刷新重试。");
|
|
22
|
+
const lines = createInterface({
|
|
23
|
+
input: createReadStream(path),
|
|
24
|
+
crlfDelay: Infinity,
|
|
25
|
+
});
|
|
26
|
+
for await (const line of lines) {
|
|
27
|
+
let record;
|
|
28
|
+
try {
|
|
29
|
+
record = JSON.parse(line);
|
|
30
|
+
} catch {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
visit(record);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export async function readAgentMessages(call, threadId) {
|
|
38
|
+
const { thread } = await call("thread/read", {
|
|
39
|
+
threadId,
|
|
40
|
+
includeTurns: false,
|
|
41
|
+
});
|
|
42
|
+
const source = thread.source?.subAgent?.thread_spawn;
|
|
43
|
+
const parentId = thread.parentThreadId || source?.parent_thread_id;
|
|
44
|
+
if (!parentId) return { messages: [] };
|
|
45
|
+
const { thread: parent } = await call("thread/read", {
|
|
46
|
+
threadId: parentId,
|
|
47
|
+
includeTurns: false,
|
|
48
|
+
});
|
|
49
|
+
const calls = new Map();
|
|
50
|
+
const accepted = new Set();
|
|
51
|
+
await scan(parent.path, ({ type, payload }) => {
|
|
52
|
+
if (
|
|
53
|
+
type === "response_item" &&
|
|
54
|
+
payload.type === "function_call" &&
|
|
55
|
+
payload.namespace === "collaboration" &&
|
|
56
|
+
["spawn_agent", "send_message", "followup_task"].includes(payload.name)
|
|
57
|
+
) {
|
|
58
|
+
try {
|
|
59
|
+
const args = JSON.parse(payload.arguments);
|
|
60
|
+
if (typeof args.message === "string" && args.message.trim())
|
|
61
|
+
calls.set(payload.call_id, args.message);
|
|
62
|
+
} catch {
|
|
63
|
+
/* 写入中的不完整调用在下一次读取时恢复。 */
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const item = payload?.item;
|
|
67
|
+
if (
|
|
68
|
+
type === "event_msg" &&
|
|
69
|
+
item?.type === "SubAgentActivity" &&
|
|
70
|
+
item.agent_thread_id === threadId &&
|
|
71
|
+
["started", "interacted"].includes(item.kind)
|
|
72
|
+
)
|
|
73
|
+
accepted.add(item.id);
|
|
74
|
+
});
|
|
75
|
+
const incoming = new Map();
|
|
76
|
+
const encrypted = new Set();
|
|
77
|
+
let current;
|
|
78
|
+
const agentPath = source?.agent_path;
|
|
79
|
+
const parentPath = agentPath?.slice(0, agentPath.lastIndexOf("/"));
|
|
80
|
+
await scan(thread.path, ({ type, payload }) => {
|
|
81
|
+
if (
|
|
82
|
+
type === "response_item" &&
|
|
83
|
+
payload.type === "agent_message" &&
|
|
84
|
+
payload.recipient === agentPath &&
|
|
85
|
+
payload.author === parentPath
|
|
86
|
+
) {
|
|
87
|
+
for (const part of payload.content || [])
|
|
88
|
+
if (part.type === "encrypted_content")
|
|
89
|
+
encrypted.add(part.encrypted_content);
|
|
90
|
+
current = incoming.get(payload.id) || {
|
|
91
|
+
turnId: payload.internal_chat_message_metadata_passthrough?.turn_id,
|
|
92
|
+
};
|
|
93
|
+
incoming.set(payload.id, current);
|
|
94
|
+
}
|
|
95
|
+
if (
|
|
96
|
+
current &&
|
|
97
|
+
!current.beforeItemId &&
|
|
98
|
+
type === "event_msg" &&
|
|
99
|
+
payload.type === "item_completed" &&
|
|
100
|
+
payload.turn_id === current.turnId
|
|
101
|
+
)
|
|
102
|
+
current.beforeItemId = payload.item.id;
|
|
103
|
+
});
|
|
104
|
+
const deliveries = [...incoming.values()];
|
|
105
|
+
return {
|
|
106
|
+
messages: [...calls]
|
|
107
|
+
.filter(([id]) => accepted.has(id))
|
|
108
|
+
.map(([id, text], index) => ({
|
|
109
|
+
id: "parent-input-" + id,
|
|
110
|
+
type: "userMessage",
|
|
111
|
+
content: [
|
|
112
|
+
{
|
|
113
|
+
type: "text",
|
|
114
|
+
text:
|
|
115
|
+
encrypted.has(text) || isEncryptedEnvelope(text)
|
|
116
|
+
? "消息内容已加密"
|
|
117
|
+
: text,
|
|
118
|
+
},
|
|
119
|
+
],
|
|
120
|
+
authorLabel: "主代理",
|
|
121
|
+
...deliveries[index],
|
|
122
|
+
})),
|
|
123
|
+
};
|
|
124
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import https from "node:https";
|
|
2
|
+
import { WebSocketServer } from "ws";
|
|
3
|
+
import { createHttpHandler } from "./http/router.mjs";
|
|
4
|
+
import { createBackendManager } from "./backends/manager.mjs";
|
|
5
|
+
import { sendRpcText } from "./rpc-transport.mjs";
|
|
6
|
+
export function createWebApplication({ config, tls, authentication, version }) {
|
|
7
|
+
const clients = new WebSocketServer({ noServer: true });
|
|
8
|
+
const monitors = new WebSocketServer({ noServer: true });
|
|
9
|
+
function send(client, message) {
|
|
10
|
+
if (client.authSessionId && !authentication.valid(client.authSessionId))
|
|
11
|
+
return;
|
|
12
|
+
sendRpcText(client, JSON.stringify(message));
|
|
13
|
+
}
|
|
14
|
+
function broadcast(message) {
|
|
15
|
+
for (const client of clients.clients) send(client, message);
|
|
16
|
+
if (
|
|
17
|
+
[
|
|
18
|
+
"lan/state",
|
|
19
|
+
"lan/compaction",
|
|
20
|
+
"turn/started",
|
|
21
|
+
"turn/completed",
|
|
22
|
+
].includes(message.method)
|
|
23
|
+
)
|
|
24
|
+
for (const monitor of monitors.clients)
|
|
25
|
+
send(monitor, bridge.maintenanceState());
|
|
26
|
+
}
|
|
27
|
+
const bridge = createBackendManager({ config, version, send, broadcast });
|
|
28
|
+
const { state, maintenanceState, releaseUnusedThreads } = bridge;
|
|
29
|
+
const server = https.createServer(
|
|
30
|
+
tls,
|
|
31
|
+
createHttpHandler({
|
|
32
|
+
config,
|
|
33
|
+
tls,
|
|
34
|
+
authentication,
|
|
35
|
+
version,
|
|
36
|
+
isReady: () => bridge.ready,
|
|
37
|
+
}),
|
|
38
|
+
);
|
|
39
|
+
server.on("upgrade", (request, socket, head) => {
|
|
40
|
+
const path = new URL(request.url, "https://localhost").pathname;
|
|
41
|
+
if (path === "/maintenance" && authentication.maintenance(request)) {
|
|
42
|
+
monitors.handleUpgrade(request, socket, head, (monitor) => {
|
|
43
|
+
monitor.on("error", () => {});
|
|
44
|
+
monitor.on("message", () => monitor.close(1008, "只读维护连接"));
|
|
45
|
+
send(monitor, maintenanceState());
|
|
46
|
+
});
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (path !== "/rpc") {
|
|
50
|
+
socket.destroy();
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const id = authentication.sessionId(request);
|
|
54
|
+
if (!authentication.sameOrigin(request) || !authentication.valid(id)) {
|
|
55
|
+
socket.end("HTTP/1.1 401 Unauthorized\r\nConnection: close\r\n\r\n");
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
clients.handleUpgrade(request, socket, head, (client) => {
|
|
59
|
+
authentication.attach(client, id);
|
|
60
|
+
clients.emit("connection", client);
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
clients.on("connection", (client) => {
|
|
65
|
+
client.alive = true;
|
|
66
|
+
client.on("pong", () => {
|
|
67
|
+
client.alive = true;
|
|
68
|
+
});
|
|
69
|
+
client.on("error", (error) =>
|
|
70
|
+
console.error("Browser connection:", error.message),
|
|
71
|
+
);
|
|
72
|
+
client.on("close", () => {
|
|
73
|
+
bridge.releaseClient(client);
|
|
74
|
+
releaseUnusedThreads();
|
|
75
|
+
});
|
|
76
|
+
send(client, state());
|
|
77
|
+
|
|
78
|
+
client.on("message", (bytes) => {
|
|
79
|
+
if (!authentication.valid(client.authSessionId)) return;
|
|
80
|
+
let message;
|
|
81
|
+
try {
|
|
82
|
+
message = JSON.parse(bytes.toString());
|
|
83
|
+
} catch {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
bridge.receive(client, message).catch((error) =>
|
|
87
|
+
send(client, {
|
|
88
|
+
id: message.id,
|
|
89
|
+
error: { code: -32000, message: error.message },
|
|
90
|
+
}),
|
|
91
|
+
);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
const heartbeat = setInterval(() => {
|
|
95
|
+
for (const client of clients.clients) {
|
|
96
|
+
if (!client.alive) client.terminate();
|
|
97
|
+
else {
|
|
98
|
+
client.alive = false;
|
|
99
|
+
client.ping();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
releaseUnusedThreads();
|
|
103
|
+
}, 30000);
|
|
104
|
+
heartbeat.unref();
|
|
105
|
+
|
|
106
|
+
server.requestTimeout = 20000;
|
|
107
|
+
server.headersTimeout = 10000;
|
|
108
|
+
return {
|
|
109
|
+
server,
|
|
110
|
+
async start() {
|
|
111
|
+
await new Promise((resolve, reject) => {
|
|
112
|
+
server.once("error", reject);
|
|
113
|
+
server.listen(config.port, config.host, () => {
|
|
114
|
+
server.off("error", reject);
|
|
115
|
+
resolve();
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
await bridge.connect();
|
|
119
|
+
},
|
|
120
|
+
async close() {
|
|
121
|
+
clearInterval(heartbeat);
|
|
122
|
+
bridge.close();
|
|
123
|
+
for (const client of [...clients.clients, ...monitors.clients])
|
|
124
|
+
client.terminate();
|
|
125
|
+
clients.close();
|
|
126
|
+
monitors.close();
|
|
127
|
+
if (server.listening)
|
|
128
|
+
await new Promise((resolve, reject) =>
|
|
129
|
+
server.close((error) => (error ? reject(error) : resolve())),
|
|
130
|
+
);
|
|
131
|
+
},
|
|
132
|
+
};
|
|
133
|
+
}
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
import { migrateWebDirectory } from "./web-config-migration.mjs";
|
|
2
|
+
import { createHash, randomBytes, timingSafeEqual } from "node:crypto";
|
|
3
|
+
import { mkdir, readFile, writeFile, rename } from "node:fs/promises";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { userInfo } from "node:os";
|
|
6
|
+
import { authenticateSystemUser } from "./system-auth.mjs";
|
|
7
|
+
|
|
8
|
+
const cookieName = "__Host-claudex-web-session";
|
|
9
|
+
const digest = (value) => createHash("sha256").update(value).digest("hex");
|
|
10
|
+
const cookie = (value, seconds) =>
|
|
11
|
+
`${cookieName}=${value}; Path=/; HttpOnly; Secure; SameSite=Strict; Max-Age=${seconds}`;
|
|
12
|
+
export function isLoopback(address) {
|
|
13
|
+
return ["127.0.0.1", "::1", "::ffff:127.0.0.1"].includes(address);
|
|
14
|
+
}
|
|
15
|
+
function json(response, status, body, headers = {}) {
|
|
16
|
+
response.writeHead(status, {
|
|
17
|
+
"Content-Type": "application/json",
|
|
18
|
+
"Cache-Control": "no-store",
|
|
19
|
+
...headers,
|
|
20
|
+
});
|
|
21
|
+
response.end(JSON.stringify(body));
|
|
22
|
+
}
|
|
23
|
+
async function credentials(request) {
|
|
24
|
+
if (!/^application\/json(?:;|$)/i.test(request.headers["content-type"] || ""))
|
|
25
|
+
throw new Error("请使用 JSON 提交登录信息。", { cause: 415 });
|
|
26
|
+
let size = 0;
|
|
27
|
+
const chunks = [];
|
|
28
|
+
for await (const chunk of request.iterator({ destroyOnReturn: false })) {
|
|
29
|
+
size += chunk.length;
|
|
30
|
+
if (size > 8192) throw new Error("登录信息过长。", { cause: 413 });
|
|
31
|
+
chunks.push(chunk);
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
return JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
35
|
+
} catch {
|
|
36
|
+
throw new Error("登录信息格式错误。", { cause: 400 });
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
export async function createAuthentication(config) {
|
|
40
|
+
await migrateWebDirectory(config, "auth", [
|
|
41
|
+
"sessions.json",
|
|
42
|
+
"maintenance-token",
|
|
43
|
+
]);
|
|
44
|
+
const directory = join(config.configDir, "auth");
|
|
45
|
+
await mkdir(directory, { recursive: true, mode: 0o700 });
|
|
46
|
+
const path = join(directory, "sessions.json");
|
|
47
|
+
const username = userInfo().username;
|
|
48
|
+
let entries = [];
|
|
49
|
+
try {
|
|
50
|
+
entries = JSON.parse(await readFile(path, "utf8"));
|
|
51
|
+
} catch (error) {
|
|
52
|
+
if (error.code !== "ENOENT") throw error;
|
|
53
|
+
}
|
|
54
|
+
const sessions = new Map(
|
|
55
|
+
entries.filter(
|
|
56
|
+
([, s]) => s.username === username && s.expiresAt > Date.now(),
|
|
57
|
+
),
|
|
58
|
+
);
|
|
59
|
+
const sockets = new Map();
|
|
60
|
+
const responses = new Map();
|
|
61
|
+
let writing = Promise.resolve();
|
|
62
|
+
const persist = () => {
|
|
63
|
+
const snapshot = JSON.stringify([...sessions]);
|
|
64
|
+
const result = writing
|
|
65
|
+
.catch(() => {})
|
|
66
|
+
.then(async () => {
|
|
67
|
+
await writeFile(path + ".tmp", snapshot, { mode: 0o600 });
|
|
68
|
+
await rename(path + ".tmp", path);
|
|
69
|
+
});
|
|
70
|
+
writing = result;
|
|
71
|
+
return result;
|
|
72
|
+
};
|
|
73
|
+
const secretPath = join(directory, "maintenance-token");
|
|
74
|
+
let maintenanceToken;
|
|
75
|
+
try {
|
|
76
|
+
maintenanceToken = await readFile(secretPath, "utf8");
|
|
77
|
+
} catch (error) {
|
|
78
|
+
if (error.code !== "ENOENT") throw error;
|
|
79
|
+
maintenanceToken = randomBytes(32).toString("hex");
|
|
80
|
+
await writeFile(secretPath, maintenanceToken, { mode: 0o600, flag: "wx" });
|
|
81
|
+
}
|
|
82
|
+
const revoke = (id) => {
|
|
83
|
+
sessions.delete(id);
|
|
84
|
+
for (const socket of sockets.get(id) || [])
|
|
85
|
+
socket.close(4401, "登录已失效,请重新登录");
|
|
86
|
+
sockets.delete(id);
|
|
87
|
+
for (const response of responses.get(id) || []) response.destroy();
|
|
88
|
+
responses.delete(id);
|
|
89
|
+
};
|
|
90
|
+
const valid = (id) => {
|
|
91
|
+
const session = sessions.get(id);
|
|
92
|
+
if (!session || session.expiresAt <= Date.now()) {
|
|
93
|
+
revoke(id);
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
return session;
|
|
97
|
+
};
|
|
98
|
+
const sessionId = (request) => {
|
|
99
|
+
const entries = (request.headers.cookie || "")
|
|
100
|
+
.split(";")
|
|
101
|
+
.map((s) => s.trim());
|
|
102
|
+
const name = entries.some((s) => s.startsWith(cookieName + "="))
|
|
103
|
+
? cookieName
|
|
104
|
+
: "__Host-codex-web-session";
|
|
105
|
+
const tokens = entries.filter((s) => s.startsWith(name + "="));
|
|
106
|
+
if (tokens.length !== 1) return null;
|
|
107
|
+
const token = tokens[0].slice(name.length + 1);
|
|
108
|
+
return /^[a-zA-Z0-9_-]{43}$/.test(token) ? digest(token) : null;
|
|
109
|
+
};
|
|
110
|
+
const sameOrigin = (request) => {
|
|
111
|
+
const origin = request.headers.origin;
|
|
112
|
+
return (
|
|
113
|
+
request.headers["sec-fetch-site"] !== "cross-site" &&
|
|
114
|
+
origin === `https://${request.headers.host}`
|
|
115
|
+
);
|
|
116
|
+
};
|
|
117
|
+
const attempts = new Map();
|
|
118
|
+
let active = 0,
|
|
119
|
+
globalAttempts = [];
|
|
120
|
+
function limited(address) {
|
|
121
|
+
const now = Date.now();
|
|
122
|
+
for (const [ip, record] of attempts)
|
|
123
|
+
if (record.until <= now) attempts.delete(ip);
|
|
124
|
+
globalAttempts = globalAttempts.filter((time) => time > now - 60000);
|
|
125
|
+
const record = attempts.get(address) || {
|
|
126
|
+
count: 0,
|
|
127
|
+
until: now + 15 * 60000,
|
|
128
|
+
};
|
|
129
|
+
if (
|
|
130
|
+
active >= 2 ||
|
|
131
|
+
record.count >= 10 ||
|
|
132
|
+
globalAttempts.length >= 30 ||
|
|
133
|
+
attempts.size >= 1024
|
|
134
|
+
)
|
|
135
|
+
return true;
|
|
136
|
+
record.count++;
|
|
137
|
+
attempts.set(address, record);
|
|
138
|
+
globalAttempts.push(now);
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
return {
|
|
142
|
+
sameOrigin,
|
|
143
|
+
valid,
|
|
144
|
+
sessionId,
|
|
145
|
+
authorize(request, response) {
|
|
146
|
+
const id = sessionId(request);
|
|
147
|
+
const session = valid(id);
|
|
148
|
+
if (!session) {
|
|
149
|
+
json(response, 401, { error: "请先登录。" });
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
if (
|
|
153
|
+
((request.headers.origin ||
|
|
154
|
+
!["GET", "HEAD"].includes(request.method)) &&
|
|
155
|
+
!sameOrigin(request)) ||
|
|
156
|
+
request.headers["sec-fetch-site"] === "cross-site"
|
|
157
|
+
) {
|
|
158
|
+
json(response, 403, { error: "请求来源不匹配。" });
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
if (!responses.has(id)) responses.set(id, new Set());
|
|
162
|
+
responses.get(id).add(response);
|
|
163
|
+
let timer;
|
|
164
|
+
const expire = () => {
|
|
165
|
+
if (!valid(id)) return;
|
|
166
|
+
timer = setTimeout(
|
|
167
|
+
expire,
|
|
168
|
+
Math.min(session.expiresAt - Date.now(), 2147483647),
|
|
169
|
+
);
|
|
170
|
+
timer.unref();
|
|
171
|
+
};
|
|
172
|
+
expire();
|
|
173
|
+
response.once("close", () => {
|
|
174
|
+
clearTimeout(timer);
|
|
175
|
+
responses.get(id)?.delete(response);
|
|
176
|
+
if (!responses.get(id)?.size) responses.delete(id);
|
|
177
|
+
});
|
|
178
|
+
return true;
|
|
179
|
+
},
|
|
180
|
+
maintenance(request) {
|
|
181
|
+
const supplied = Buffer.from(request.headers.authorization || "");
|
|
182
|
+
const expected = Buffer.from(`Bearer ${maintenanceToken}`);
|
|
183
|
+
return (
|
|
184
|
+
!request.headers.origin &&
|
|
185
|
+
isLoopback(request.socket.remoteAddress) &&
|
|
186
|
+
supplied.length === expected.length &&
|
|
187
|
+
timingSafeEqual(supplied, expected)
|
|
188
|
+
);
|
|
189
|
+
},
|
|
190
|
+
attach(socket, id) {
|
|
191
|
+
const session = valid(id);
|
|
192
|
+
if (!session) {
|
|
193
|
+
socket.close(4401, "登录已失效,请重新登录");
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
socket.authSessionId = id;
|
|
197
|
+
if (!sockets.has(id)) sockets.set(id, new Set());
|
|
198
|
+
sockets.get(id).add(socket);
|
|
199
|
+
let timer;
|
|
200
|
+
const expire = () => {
|
|
201
|
+
if (!valid(id)) return;
|
|
202
|
+
timer = setTimeout(
|
|
203
|
+
expire,
|
|
204
|
+
Math.min(session.expiresAt - Date.now(), 2147483647),
|
|
205
|
+
);
|
|
206
|
+
timer.unref();
|
|
207
|
+
};
|
|
208
|
+
expire();
|
|
209
|
+
socket.once("close", () => {
|
|
210
|
+
clearTimeout(timer);
|
|
211
|
+
sockets.get(id)?.delete(socket);
|
|
212
|
+
});
|
|
213
|
+
},
|
|
214
|
+
async handle(request, response, route) {
|
|
215
|
+
if (!route.startsWith("/api/auth/")) return false;
|
|
216
|
+
try {
|
|
217
|
+
const id = sessionId(request);
|
|
218
|
+
if (route === "/api/auth/session" && request.method === "GET") {
|
|
219
|
+
const session = valid(id);
|
|
220
|
+
json(
|
|
221
|
+
response,
|
|
222
|
+
200,
|
|
223
|
+
session
|
|
224
|
+
? { authenticated: true, ...session }
|
|
225
|
+
: { authenticated: false },
|
|
226
|
+
);
|
|
227
|
+
return true;
|
|
228
|
+
}
|
|
229
|
+
if (
|
|
230
|
+
!["/api/auth/login", "/api/auth/logout"].includes(route) ||
|
|
231
|
+
request.method !== "POST"
|
|
232
|
+
) {
|
|
233
|
+
json(response, 405, { error: "不支持此登录操作。" });
|
|
234
|
+
return true;
|
|
235
|
+
}
|
|
236
|
+
if (
|
|
237
|
+
!sameOrigin(request) ||
|
|
238
|
+
request.headers["x-claudex-web-request"] !== "1"
|
|
239
|
+
) {
|
|
240
|
+
json(response, 403, { error: "请求来源不匹配,请重新打开本站。" });
|
|
241
|
+
return true;
|
|
242
|
+
}
|
|
243
|
+
if (route === "/api/auth/logout") {
|
|
244
|
+
revoke(id);
|
|
245
|
+
await persist();
|
|
246
|
+
json(
|
|
247
|
+
response,
|
|
248
|
+
200,
|
|
249
|
+
{ authenticated: false },
|
|
250
|
+
{ "Set-Cookie": cookie("", 0) },
|
|
251
|
+
);
|
|
252
|
+
return true;
|
|
253
|
+
}
|
|
254
|
+
if (limited(request.socket.remoteAddress)) {
|
|
255
|
+
json(
|
|
256
|
+
response,
|
|
257
|
+
429,
|
|
258
|
+
{ error: "登录尝试过于频繁,请稍后重试。" },
|
|
259
|
+
{ "Retry-After": "900" },
|
|
260
|
+
);
|
|
261
|
+
return true;
|
|
262
|
+
}
|
|
263
|
+
active++;
|
|
264
|
+
try {
|
|
265
|
+
const input = await credentials(request);
|
|
266
|
+
const { username: submitted, password } = input || {};
|
|
267
|
+
if (
|
|
268
|
+
typeof submitted !== "string" ||
|
|
269
|
+
typeof password !== "string" ||
|
|
270
|
+
!submitted ||
|
|
271
|
+
!password ||
|
|
272
|
+
submitted.length > 256 ||
|
|
273
|
+
password.length > 1024 ||
|
|
274
|
+
/\0/.test(submitted + password)
|
|
275
|
+
) {
|
|
276
|
+
json(response, 400, { error: "请输入有效的系统用户名和密码。" });
|
|
277
|
+
return true;
|
|
278
|
+
}
|
|
279
|
+
if (
|
|
280
|
+
submitted !== username ||
|
|
281
|
+
!(await authenticateSystemUser(submitted, password, config))
|
|
282
|
+
) {
|
|
283
|
+
json(response, 401, {
|
|
284
|
+
error:
|
|
285
|
+
"用户名或密码错误,或系统账户不可用。请使用运行服务的系统账户。",
|
|
286
|
+
});
|
|
287
|
+
return true;
|
|
288
|
+
}
|
|
289
|
+
for (const [key, session] of sessions)
|
|
290
|
+
if (session.expiresAt <= Date.now()) revoke(key);
|
|
291
|
+
if (sessions.size >= 64) revoke(sessions.keys().next().value);
|
|
292
|
+
revoke(id);
|
|
293
|
+
const token = randomBytes(32).toString("base64url");
|
|
294
|
+
const session = {
|
|
295
|
+
username,
|
|
296
|
+
expiresAt: Date.now() + config.sessionDays * 86400000,
|
|
297
|
+
};
|
|
298
|
+
const key = digest(token);
|
|
299
|
+
sessions.set(key, session);
|
|
300
|
+
try {
|
|
301
|
+
await persist();
|
|
302
|
+
} catch (error) {
|
|
303
|
+
revoke(key);
|
|
304
|
+
throw error;
|
|
305
|
+
}
|
|
306
|
+
json(
|
|
307
|
+
response,
|
|
308
|
+
200,
|
|
309
|
+
{ authenticated: true, ...session },
|
|
310
|
+
{ "Set-Cookie": cookie(token, config.sessionDays * 86400) },
|
|
311
|
+
);
|
|
312
|
+
} finally {
|
|
313
|
+
active--;
|
|
314
|
+
}
|
|
315
|
+
} catch (error) {
|
|
316
|
+
request.resume();
|
|
317
|
+
if (!response.headersSent && !response.destroyed)
|
|
318
|
+
json(response, typeof error.cause === "number" ? error.cause : 503, {
|
|
319
|
+
error:
|
|
320
|
+
typeof error.cause === "number"
|
|
321
|
+
? error.message
|
|
322
|
+
: "登录暂不可用,请稍后重试或联系管理员。",
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
return true;
|
|
326
|
+
},
|
|
327
|
+
};
|
|
328
|
+
}
|