pop-agent 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +620 -2
- package/README.zh.md +622 -0
- package/dist/server/approval/read-only.js +107 -0
- package/dist/server/approval/rules.js +73 -0
- package/dist/server/approval/scope.js +164 -0
- package/dist/server/approval/unattended.js +25 -0
- package/dist/server/asr/bridge.js +244 -0
- package/dist/server/asr/install.js +188 -0
- package/dist/server/asr/paths.js +101 -0
- package/dist/server/asr/routes.js +62 -0
- package/dist/server/brand.js +2 -0
- package/dist/server/cli-config.js +65 -0
- package/dist/server/cli-info.js +169 -0
- package/dist/server/cli.js +602 -0
- package/dist/server/computer/apps.js +152 -0
- package/dist/server/computer/cli.js +322 -0
- package/dist/server/computer/framing.js +36 -0
- package/dist/server/computer/helper-client.js +268 -0
- package/dist/server/computer/paths.js +114 -0
- package/dist/server/computer/routes.js +118 -0
- package/dist/server/computer/settings.js +169 -0
- package/dist/server/computer/skill.js +131 -0
- package/dist/server/computer/status.js +134 -0
- package/dist/server/cron/agent-run.js +121 -0
- package/dist/server/cron/index.js +539 -0
- package/dist/server/cron/tool.js +155 -0
- package/dist/server/engine/builtin.js +180 -0
- package/dist/server/engine/catalog-probe.js +34 -0
- package/dist/server/engine/catalog.js +8 -0
- package/dist/server/engine/claude-map.js +297 -0
- package/dist/server/engine/claude-sessions.js +581 -0
- package/dist/server/engine/claude-transport.js +75 -0
- package/dist/server/engine/claude.js +414 -0
- package/dist/server/engine/cli-version.js +119 -0
- package/dist/server/engine/codex-approvals.js +45 -0
- package/dist/server/engine/codex-map.js +298 -0
- package/dist/server/engine/codex-sessions.js +159 -0
- package/dist/server/engine/codex-subagents.js +349 -0
- package/dist/server/engine/codex-transport.js +49 -0
- package/dist/server/engine/codex.js +530 -0
- package/dist/server/engine/dsh-history.js +195 -0
- package/dist/server/engine/dsh-log.js +256 -0
- package/dist/server/engine/dsh-map.js +223 -0
- package/dist/server/engine/dsh-prompt-patch.js +48 -0
- package/dist/server/engine/dsh-sessions.js +206 -0
- package/dist/server/engine/dsh-transport.js +37 -0
- package/dist/server/engine/dsh.js +329 -0
- package/dist/server/engine/engine-install.js +182 -0
- package/dist/server/engine/engine-routes.js +54 -0
- package/dist/server/engine/engine-specs.js +65 -0
- package/dist/server/engine/engine-versions.js +41 -0
- package/dist/server/engine/export-html-tree-patch.js +89 -0
- package/dist/server/engine/fake.js +263 -0
- package/dist/server/engine/jsonrpc-stdio.js +275 -0
- package/dist/server/engine/model-registry-reload.js +115 -0
- package/dist/server/engine/narrate-guidance.js +25 -0
- package/dist/server/engine/opencode-map.js +500 -0
- package/dist/server/engine/opencode-projects.js +61 -0
- package/dist/server/engine/opencode-sessions.js +201 -0
- package/dist/server/engine/opencode-transport.js +48 -0
- package/dist/server/engine/opencode.js +434 -0
- package/dist/server/engine/pi-appearance-tool.js +101 -0
- package/dist/server/engine/pi-canvas-tool.js +181 -0
- package/dist/server/engine/pi-cron-tool.js +196 -0
- package/dist/server/engine/pi-events.js +153 -0
- package/dist/server/engine/pi-plugins.js +312 -0
- package/dist/server/engine/pi-rename.js +14 -0
- package/dist/server/engine/pi-retry.js +124 -0
- package/dist/server/engine/pi-sdk.js +1272 -0
- package/dist/server/engine/pi-session-model.js +23 -0
- package/dist/server/engine/pi-stop.js +28 -0
- package/dist/server/engine/pi-subagent-tool.js +188 -0
- package/dist/server/engine/pi-subagents.js +445 -0
- package/dist/server/engine/pi-thinking.js +34 -0
- package/dist/server/engine/pi-transcript.js +247 -0
- package/dist/server/engine/pi-turn-outcome.js +25 -0
- package/dist/server/engine/pi-version.js +11 -0
- package/dist/server/engine/port.js +1 -0
- package/dist/server/engine/registry.js +61 -0
- package/dist/server/engine/resource-signature.js +55 -0
- package/dist/server/engine/scratch.js +13 -0
- package/dist/server/engine/subagent-guidance.js +22 -0
- package/dist/server/engine/suggestion-guidance.js +24 -0
- package/dist/server/engine/switchable.js +22 -0
- package/dist/server/engine/wiring.js +225 -0
- package/dist/server/files/content-search.js +187 -0
- package/dist/server/files/dir-routes.js +104 -0
- package/dist/server/files/file-search.js +64 -0
- package/dist/server/files/file-service.js +286 -0
- package/dist/server/files/files-routes.js +278 -0
- package/dist/server/files/git-info.js +428 -0
- package/dist/server/files/path-resolve.js +176 -0
- package/dist/server/files/pick-path-routes.js +162 -0
- package/dist/server/http/auth-routes.js +454 -0
- package/dist/server/http/auth.js +306 -0
- package/dist/server/http/aux-routes.js +98 -0
- package/dist/server/http/client-log-routes.js +49 -0
- package/dist/server/http/compress.js +102 -0
- package/dist/server/http/file-headers.js +75 -0
- package/dist/server/http/health-and-static.js +46 -0
- package/dist/server/http/live-routes.js +116 -0
- package/dist/server/http/login-activity.js +76 -0
- package/dist/server/http/login-guard.js +185 -0
- package/dist/server/http/login-page.js +347 -0
- package/dist/server/http/password-store.js +88 -0
- package/dist/server/http/voice-settings-routes.js +30 -0
- package/dist/server/http/ws-routes.js +141 -0
- package/dist/server/index.js +1085 -0
- package/dist/server/markers/actions.js +281 -0
- package/dist/server/mcp/codex-mcp-store.js +190 -0
- package/dist/server/mcp/json-mcp-store.js +276 -0
- package/dist/server/mcp/mcp-auth.js +194 -0
- package/dist/server/mcp/mcp-command.js +137 -0
- package/dist/server/mcp/mcp-engine-shared.js +112 -0
- package/dist/server/mcp/mcp-import.js +289 -0
- package/dist/server/mcp/mcp-market-sources.js +192 -0
- package/dist/server/mcp/mcp-market-types.js +5 -0
- package/dist/server/mcp/mcp-market.js +174 -0
- package/dist/server/mcp/mcp-probe.js +170 -0
- package/dist/server/mcp/mcp-routes.js +71 -0
- package/dist/server/mcp/mcp-store.js +286 -0
- package/dist/server/mcp/none-mcp-store.js +27 -0
- package/dist/server/mcp/pi-mcp-config.js +308 -0
- package/dist/server/pi-gate.js +106 -0
- package/dist/server/plugins/engine-plugin-stores.js +265 -0
- package/dist/server/plugins/plugin-routes.js +22 -0
- package/dist/server/protocol-parse.js +533 -0
- package/dist/server/protocol-settings.js +1 -0
- package/dist/server/protocol.js +8 -0
- package/dist/server/providers/catalog-store.js +89 -0
- package/dist/server/providers/catalog-writes.js +199 -0
- package/dist/server/providers/catalog.js +77 -0
- package/dist/server/providers/codex-models.js +96 -0
- package/dist/server/providers/codex-test.js +124 -0
- package/dist/server/providers/enabled-models.js +110 -0
- package/dist/server/providers/import-models.js +69 -0
- package/dist/server/providers/import-routes.js +266 -0
- package/dist/server/providers/model-kind.js +35 -0
- package/dist/server/providers/model-test.js +113 -0
- package/dist/server/providers/one-shot.js +218 -0
- package/dist/server/providers/probe-routes.js +26 -0
- package/dist/server/providers/probe.js +84 -0
- package/dist/server/providers/provider-handlers.js +240 -0
- package/dist/server/providers/provider-pool.js +63 -0
- package/dist/server/providers/provider-store.js +502 -0
- package/dist/server/providers/request-headers.js +7 -0
- package/dist/server/providers/subscription-routes.js +183 -0
- package/dist/server/providers/validate.js +249 -0
- package/dist/server/push/push-routes.js +171 -0
- package/dist/server/push/push.js +239 -0
- package/dist/server/push/vapid-config.js +179 -0
- package/dist/server/session/appearance.js +79 -0
- package/dist/server/session/approval-bridge.js +119 -0
- package/dist/server/session/attachment-resolve.js +21 -0
- package/dist/server/session/canvas-artifacts.js +108 -0
- package/dist/server/session/canvas-store.js +137 -0
- package/dist/server/session/canvas-suggest.js +70 -0
- package/dist/server/session/canvas.js +323 -0
- package/dist/server/session/client-view.js +82 -0
- package/dist/server/session/close-ephemeral.js +23 -0
- package/dist/server/session/conversation-create.js +87 -0
- package/dist/server/session/cron-prompt-routes.js +25 -0
- package/dist/server/session/cron-prompt.js +59 -0
- package/dist/server/session/delete-session.js +125 -0
- package/dist/server/session/engine-switch.js +118 -0
- package/dist/server/session/fork-session.js +38 -0
- package/dist/server/session/lock.js +82 -0
- package/dist/server/session/model-controller.js +149 -0
- package/dist/server/session/move-session.js +40 -0
- package/dist/server/session/notices.js +33 -0
- package/dist/server/session/one-shot.js +95 -0
- package/dist/server/session/persist-ephemeral.js +48 -0
- package/dist/server/session/pi-opener.js +163 -0
- package/dist/server/session/pi-refresh.js +51 -0
- package/dist/server/session/pi-sessions-provider.js +41 -0
- package/dist/server/session/pi-sessions.js +431 -0
- package/dist/server/session/refresh-actions.js +58 -0
- package/dist/server/session/rename-session.js +23 -0
- package/dist/server/session/runtime-services.js +37 -0
- package/dist/server/session/runtime-types.js +1 -0
- package/dist/server/session/runtime.js +844 -0
- package/dist/server/session/session-stats.js +91 -0
- package/dist/server/session/settings.js +526 -0
- package/dist/server/session/snapshot-for.js +9 -0
- package/dist/server/session/stats-refresher.js +55 -0
- package/dist/server/session/subagents.js +119 -0
- package/dist/server/session/suggest-title.js +33 -0
- package/dist/server/session/switch-conversation.js +111 -0
- package/dist/server/session/template-prompt-routes.js +26 -0
- package/dist/server/session/template-prompt.js +110 -0
- package/dist/server/session/throttle.js +14 -0
- package/dist/server/session/title-prompt.js +128 -0
- package/dist/server/session/turn-events.js +198 -0
- package/dist/server/session/turn-finish.js +97 -0
- package/dist/server/session/turn-markers.js +31 -0
- package/dist/server/session/turn-queue.js +264 -0
- package/dist/server/session/turn-send.js +138 -0
- package/dist/server/session/turn-watchdog.js +77 -0
- package/dist/server/session/voice-settings.js +39 -0
- package/dist/server/sessions/session-routes.js +97 -0
- package/dist/server/skills/skill-install.js +184 -0
- package/dist/server/skills/skill-routes.js +134 -0
- package/dist/server/skills/skill-store.js +163 -0
- package/dist/server/skills/skillhub.js +213 -0
- package/dist/server/skills/zip.js +121 -0
- package/dist/server/snapshot/fan-out.js +12 -0
- package/dist/server/snapshot/messages.js +117 -0
- package/dist/server/snapshot/sender.js +53 -0
- package/dist/server/snapshot/state.js +274 -0
- package/dist/server/subagents/profile-store.js +305 -0
- package/dist/server/terminal/terminal-routes.js +223 -0
- package/dist/server/terminal/terminal-session.js +197 -0
- package/dist/server/tts/edge-worker-source.js +48 -0
- package/dist/server/tts/edge.js +143 -0
- package/dist/server/tts/local-voices.js +35 -0
- package/dist/server/tts/local.js +192 -0
- package/dist/server/tts/routes.js +56 -0
- package/dist/server/tts/service.js +97 -0
- package/dist/server/tts/types.js +7 -0
- package/dist/server/tts/voices.js +21 -0
- package/dist/server/update-check.js +147 -0
- package/dist/server/update-prompt.js +131 -0
- package/dist/server/uploads/upload-routes.js +67 -0
- package/dist/server/uploads/upload-store.js +113 -0
- package/dist/server/util/atomic-write.js +18 -0
- package/dist/server/util/data-dir.js +36 -0
- package/dist/server/util/desktop.js +216 -0
- package/dist/server/util/env.js +202 -0
- package/dist/server/util/file-mode.js +57 -0
- package/dist/server/util/ids.js +28 -0
- package/dist/server/util/json-file.js +38 -0
- package/dist/server/util/log-file.js +67 -0
- package/dist/server/util/log.js +52 -0
- package/dist/server/util/origin.js +117 -0
- package/dist/server/util/paths.js +187 -0
- package/dist/server/util/pricing-index.js +117 -0
- package/dist/server/util/pricing.js +214 -0
- package/dist/server/util/prune-pty.js +72 -0
- package/dist/server/util/spawn-cli.js +147 -0
- package/dist/server/util/startup.js +34 -0
- package/dist/server/util/upstream-version.js +177 -0
- package/dist/server/util/usage.js +19 -0
- package/dist/server/util/version.js +59 -0
- package/dist/server/web-proxy/picker-script.js +495 -0
- package/dist/server/web-proxy/proxy-routes.js +205 -0
- package/dist/server/web-proxy/proxy-url.js +76 -0
- package/dist/server/web-proxy/rewrite.js +98 -0
- package/dist/server/web-proxy/wrapper.js +92 -0
- package/dist/server/ws/handle-message.js +182 -0
- package/dist/server/ws/reply-frame.js +5 -0
- package/helper/asr/Info.plist +24 -0
- package/helper/asr/build.sh +59 -0
- package/helper/asr/main.swift +279 -0
- package/helper/asr/release/ASRBridge.zip +0 -0
- package/package.json +120 -5
- package/web/dist/apple-touch-icon-precomposed.png +0 -0
- package/web/dist/apple-touch-icon.png +0 -0
- package/web/dist/assets/_pop-agent-mermaid-pruned_chunk_abnfDiagram-WEYOIA3C-Cxf7yoVn.js +1 -0
- package/web/dist/assets/_pop-agent-mermaid-pruned_chunk_architectureDiagram-ZHVYKT53-BtmjGlGq.js +1 -0
- package/web/dist/assets/_pop-agent-mermaid-pruned_chunk_c4Diagram-XDPPPE54-CMVGVtIk.js +1 -0
- package/web/dist/assets/_pop-agent-mermaid-pruned_chunk_cynefinDiagram-WF24UDRP-7i0BPrM8.js +1 -0
- package/web/dist/assets/_pop-agent-mermaid-pruned_chunk_diagram-CBK3MWTC-G70wUrpC.js +1 -0
- package/web/dist/assets/_pop-agent-mermaid-pruned_chunk_diagram-ODIL2UCW-DG5xxpj4.js +1 -0
- package/web/dist/assets/_pop-agent-mermaid-pruned_chunk_diagram-PFBEJUXE-TVSrX2lz.js +1 -0
- package/web/dist/assets/_pop-agent-mermaid-pruned_chunk_diagram-TYUNFSNR-B5kj_W4w.js +1 -0
- package/web/dist/assets/_pop-agent-mermaid-pruned_chunk_ebnfDiagram-V4P3TTPT-CYDlMnIy.js +1 -0
- package/web/dist/assets/_pop-agent-mermaid-pruned_chunk_ishikawaDiagram-HPEHV2GE-Bwle1LQ4.js +1 -0
- package/web/dist/assets/_pop-agent-mermaid-pruned_chunk_mindmap-definition-6OY57UG3-Dp_r4po3.js +1 -0
- package/web/dist/assets/_pop-agent-mermaid-pruned_chunk_pegDiagram-ECIUJNBX-D0XH8M0y.js +1 -0
- package/web/dist/assets/_pop-agent-mermaid-pruned_chunk_railroadDiagram-Y72NRBL7-BYBIBrHp.js +1 -0
- package/web/dist/assets/_pop-agent-mermaid-pruned_chunk_swimlanes-B3ENRKFQ-Bj4-4hK8.js +1 -0
- package/web/dist/assets/_pop-agent-mermaid-pruned_chunk_swimlanesDiagram-AYQRV4AK-Cr0JbDyV.js +1 -0
- package/web/dist/assets/_pop-agent-mermaid-pruned_chunk_usecaseDiagram-WPOLBNNX-BN92OiEn.js +1 -0
- package/web/dist/assets/_pop-agent-mermaid-pruned_chunk_vennDiagram-Q3Y3IDCW--PhbzdU_.js +1 -0
- package/web/dist/assets/_pop-agent-mermaid-pruned_chunk_wardleyDiagram-IVAIDKWA-nObiqZ0S.js +1 -0
- package/web/dist/assets/_pop-agent-mermaid-pruned_katex_katex-Zojedrce.js +1 -0
- package/web/dist/assets/approval.en-BFhMPNWP.mp3 +0 -0
- package/web/dist/assets/approval.zh-DWqP6Pcu.mp3 +0 -0
- package/web/dist/assets/arc-Dn7mfTUL.js +1 -0
- package/web/dist/assets/architecture-WOLXFQ4H-DwHKr8GA.js +1 -0
- package/web/dist/assets/array-BifhSqXX.js +1 -0
- package/web/dist/assets/blockDiagram-RJAE7QA3-CA2AOM6-.js +140 -0
- package/web/dist/assets/channel-PVcvMEH-.js +1 -0
- package/web/dist/assets/chunk-2Q5K7J3B-C1jixKkw.js +1 -0
- package/web/dist/assets/chunk-55XCU76K-DlITu1ZH.js +1 -0
- package/web/dist/assets/chunk-5VM5RSS4-ZNzvKenW.js +15 -0
- package/web/dist/assets/chunk-7S6XSTFO-DzX1XF3_.js +2 -0
- package/web/dist/assets/chunk-AQDTSKZB-BdEDejS-.js +72 -0
- package/web/dist/assets/chunk-DSNLSHJZ-Chj2PD0m.js +129 -0
- package/web/dist/assets/chunk-DUW6YSOI-Dgx8z5s3.js +1 -0
- package/web/dist/assets/chunk-HNE4Q36P-V3jylrEv.js +1 -0
- package/web/dist/assets/chunk-J5ZVWO5B-oDCd-gWO.js +1 -0
- package/web/dist/assets/chunk-JWPE2WC7-DVXcaiue.js +1 -0
- package/web/dist/assets/chunk-NGNAAXSQ-DuHRe0y2.js +136 -0
- package/web/dist/assets/chunk-VQ5W62QI-BiWZxVe5.js +1 -0
- package/web/dist/assets/chunk-WQQL4CDK-BtyaHLI7.js +10 -0
- package/web/dist/assets/chunk-Y2CYZVJY-DsF7k-Jl.js +1 -0
- package/web/dist/assets/chunk-ZAUX2LTX-DS3wpSTy.js +62 -0
- package/web/dist/assets/chunk-ZCHMQSOR-at82jgqF.js +2 -0
- package/web/dist/assets/classDiagram-v2-4MYWR3UA-CTgR1e3M.js +217 -0
- package/web/dist/assets/cose-bilkent-JH36ORCC-DW-hnOsq.js +1 -0
- package/web/dist/assets/cynefin-EF2NZ3EQ-Pg-c-yVE.js +1 -0
- package/web/dist/assets/dagre-NK4GIZVX-B0Ivt5Pn.js +4 -0
- package/web/dist/assets/diagram-I5XIIV2D-jE6SNlbX.js +24 -0
- package/web/dist/assets/diagram-RJA3I4VV-DQJNgW0u.js +41 -0
- package/web/dist/assets/done.en-s1tZOg6A.mp3 +0 -0
- package/web/dist/assets/done.zh-Dq-Os4NS.mp3 +0 -0
- package/web/dist/assets/elk-QONTBCTQ-KOJVoDyP.js +27 -0
- package/web/dist/assets/erDiagram-KYQIGQAA-DNUqfe2J.js +99 -0
- package/web/dist/assets/error.en-BGPHdZVi.mp3 +0 -0
- package/web/dist/assets/error.zh-DdFp64-u.mp3 +0 -0
- package/web/dist/assets/eventmodeling-K75KTNOO-DnxXCUaf.js +1 -0
- package/web/dist/assets/flowDiagram-IUGLLWBU-CRLc8ozS.js +213 -0
- package/web/dist/assets/ganttDiagram-R7GPNTJY-DDLhSPGT.js +292 -0
- package/web/dist/assets/gitGraph-VSP46ZUC-BjfoZeXP.js +1 -0
- package/web/dist/assets/gitGraphDiagram-PDYL7XHY-6uZGw3lU.js +106 -0
- package/web/dist/assets/graphlib-DS17s2tU.js +1 -0
- package/web/dist/assets/highlight-DMv0JmTA.js +5 -0
- package/web/dist/assets/i18n-en-H5OrSHR5.js +1 -0
- package/web/dist/assets/index-CQ1VhLwx.js +79 -0
- package/web/dist/assets/index-DB6jJFLf.css +1 -0
- package/web/dist/assets/info-OHQRW6UA-BMDHcIvy.js +1 -0
- package/web/dist/assets/infoDiagram-MVDRKAX5-C2d1Uwhn.js +2 -0
- package/web/dist/assets/init-C-OQMol4.js +1 -0
- package/web/dist/assets/journeyDiagram-BOSHW7O7-BAE_nPCd.js +139 -0
- package/web/dist/assets/kanban-definition-FNSA4HC5-C2Z3ejvx.js +89 -0
- package/web/dist/assets/line-DHlXL-f4.js +1 -0
- package/web/dist/assets/linear-B363vzIx.js +1 -0
- package/web/dist/assets/markdown-Bcx45PNe.js +31 -0
- package/web/dist/assets/math-Dh0H2mym.js +1 -0
- package/web/dist/assets/mermaid-parser.core-CxD9DMnU.js +7 -0
- package/web/dist/assets/mermaid.core-CdVzG6_M.js +44 -0
- package/web/dist/assets/ordinal-BDEzSJ7C.js +1 -0
- package/web/dist/assets/packet-JDAUHWVQ-Cx23kBS5.js +1 -0
- package/web/dist/assets/path-fybaL0A-.js +1 -0
- package/web/dist/assets/pie-XZMESJXO-BDdtcaza.js +1 -0
- package/web/dist/assets/pieDiagram-GNMPY74T-DRqt_o8d.js +39 -0
- package/web/dist/assets/quadrantDiagram-D57K4MC3-Cq3-_E3T.js +7 -0
- package/web/dist/assets/radar-ABXABTNO-yJ_gp7Uq.js +1 -0
- package/web/dist/assets/railroad-I3PHUGI6-BFVVDyfL.js +1 -0
- package/web/dist/assets/railroad-abnf-LNEFI6M7-BhhpD6rl.js +1 -0
- package/web/dist/assets/railroad-ebnf-DWYD2UWJ-os_iV6TA.js +1 -0
- package/web/dist/assets/railroad-peg-JR7BVNR7-kex9mDBE.js +1 -0
- package/web/dist/assets/react-TO3cgmuO.js +9 -0
- package/web/dist/assets/requirementDiagram-EQYSW5RA-BnDM54-F.js +84 -0
- package/web/dist/assets/rolldown-runtime-COiEfhsA.js +1 -0
- package/web/dist/assets/sankeyDiagram-SY24JHO5-CMQmL2Tu.js +40 -0
- package/web/dist/assets/sequenceDiagram-BLEHNHQU-Dqhxl46T.js +169 -0
- package/web/dist/assets/sizeCapture-INFHLROL-3uHXrFqA.js +1 -0
- package/web/dist/assets/src-BWF7jMDv.js +1 -0
- package/web/dist/assets/stateDiagram-v2-A6IESKWZ-D1u1qdD7.js +291 -0
- package/web/dist/assets/steer.en-1RIP4K_0.mp3 +0 -0
- package/web/dist/assets/steer.zh-BCINYQs7.mp3 +0 -0
- package/web/dist/assets/stopped.en-CFPMi6oy.mp3 +0 -0
- package/web/dist/assets/stopped.zh-D2CUs-KZ.mp3 +0 -0
- package/web/dist/assets/timeline-definition-JXYKGLGB-DEjsSeWV.js +120 -0
- package/web/dist/assets/treeView-D4JQ5SDB-BXYISxgj.js +1 -0
- package/web/dist/assets/treemap-SAJKECNS-B6BJK2c0.js +1 -0
- package/web/dist/assets/wardley-7MLQ67FV-DsjBYeKs.js +1 -0
- package/web/dist/assets/xterm-BrP-ENHg.css +1 -0
- package/web/dist/assets/xterm-C3-Bm0Yw.js +16 -0
- package/web/dist/assets/xychartDiagram-4URNHDU4-CBbRqTE2.js +7 -0
- package/web/dist/favicon-32.png +0 -0
- package/web/dist/favicon.ico +0 -0
- package/web/dist/favicon.svg +25 -0
- package/web/dist/icons/apple-touch-icon-180.png +0 -0
- package/web/dist/icons/icon-1024.png +0 -0
- package/web/dist/icons/icon-192.png +0 -0
- package/web/dist/icons/icon-512.png +0 -0
- package/web/dist/icons/icon-maskable-512.png +0 -0
- package/web/dist/index.html +62 -0
- package/web/dist/manifest.webmanifest +17 -0
- package/web/dist/maskable.svg +25 -0
- package/web/dist/sw.js +40 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP 的「非 API」部分(从 index.ts 抽出:400 行护栏):健康检查 + 前端静态资源。
|
|
3
|
+
*/
|
|
4
|
+
import express, {} from "express";
|
|
5
|
+
import { PROTOCOL_VERSION } from "../protocol.js";
|
|
6
|
+
import { webVersion } from "../util/version.js";
|
|
7
|
+
export function registerHealthAndStatic(app, options) {
|
|
8
|
+
// 压缩 + 缓存头由 index.ts 在**注册 API 路由之前**挂(见那里的注释):
|
|
9
|
+
// 在这里挂只会覆盖它之后注册的路由,`/api/rpc` 那些大包就永远压不到。
|
|
10
|
+
app.get("/api/health", (_request, response) => {
|
|
11
|
+
const counts = options.counts();
|
|
12
|
+
response.json({
|
|
13
|
+
ok: true,
|
|
14
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
15
|
+
// 跑着的这个进程的版本(pop status 用它和磁盘上的 package.json 对比)
|
|
16
|
+
version: webVersion(),
|
|
17
|
+
workspace: options.workspace,
|
|
18
|
+
engine: options.engine,
|
|
19
|
+
// 会话事实源是 pi 的会话存储(ADR-0021 §2)→ 报它的数量
|
|
20
|
+
sessions: counts.sessions,
|
|
21
|
+
conversations: counts.conversations,
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
if (options.webDist) {
|
|
25
|
+
/**
|
|
26
|
+
* `/index.html` 与 `/` 一律 `no-store`。
|
|
27
|
+
*
|
|
28
|
+
* 为什么:`index.html` 里写着带哈希的资源名,**它自己不能缓存** —— 否则改完前端、
|
|
29
|
+
* 用户刷新一次仍拿到旧 HTML,进而加载旧 JS/CSS(实测踩过:界面上还显示已经删掉的浮动
|
|
30
|
+
* 麦克风按钮,看起来像「改了没生效」)。带哈希的 assets 仍然长缓存(见 immutableAssets)。
|
|
31
|
+
*/
|
|
32
|
+
app.use((request, response, next) => {
|
|
33
|
+
if (request.path === "/" || request.path === "/index.html") {
|
|
34
|
+
response.setHeader("Cache-Control", "no-store, must-revalidate");
|
|
35
|
+
}
|
|
36
|
+
next();
|
|
37
|
+
});
|
|
38
|
+
app.use(express.static(options.webDist));
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
app.get("/", (_request, response) => {
|
|
42
|
+
response
|
|
43
|
+
.type("text/plain")
|
|
44
|
+
.send(`pop-agent 服务已启动\n\n前端开发态请用:npm run dev(vite 在 http://127.0.0.1:${options.devWebPort})\n`);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 纯 HTTP 的实时通道(SSE + RPC)。ADR-0032 定通道,ADR-0033 定「每客户端一份会话视图」。
|
|
3
|
+
*
|
|
4
|
+
* 两个端点:
|
|
5
|
+
* - `GET /api/events` SSE 长连接:服务端 → 客户端(快照 / 事件)
|
|
6
|
+
* - `POST /api/rpc` 一问一答:客户端 → 服务端(协议不变,还是 ClientRequest)
|
|
7
|
+
*
|
|
8
|
+
* 客户端身份:`pop-agent-client` cookie。有了它,快照才能按「这个客户端在看哪个会话」生成
|
|
9
|
+
* (否则两个标签页会互相拽着切换)。cookie 不存在时由 `/api/events` 发一个。
|
|
10
|
+
*/
|
|
11
|
+
import { randomUUID } from "node:crypto";
|
|
12
|
+
import { constants as zlibConstants, createGzip } from "node:zlib";
|
|
13
|
+
const CLIENT_COOKIE = "pop-agent-client";
|
|
14
|
+
/** 每个标签页自己的标识(cookie 是同浏览器共享的,不能用来区分标签页) */
|
|
15
|
+
const CLIENT_HEADER = "x-agent-client";
|
|
16
|
+
/** 客户端标识:优先请求头 / 查询参数(每个标签页不一样),最后才回落到 cookie */
|
|
17
|
+
function clientIdOf(input) {
|
|
18
|
+
const fromHeader = Array.isArray(input.header) ? input.header[0] : input.header;
|
|
19
|
+
if (typeof fromHeader === "string" && fromHeader.trim() !== "")
|
|
20
|
+
return fromHeader.trim();
|
|
21
|
+
if (typeof input.query === "string" && input.query.trim() !== "")
|
|
22
|
+
return input.query.trim();
|
|
23
|
+
return clientIdFromCookie(input.cookie);
|
|
24
|
+
}
|
|
25
|
+
/** 从 Cookie 头里取客户端标识 */
|
|
26
|
+
function clientIdFromCookie(header) {
|
|
27
|
+
if (!header)
|
|
28
|
+
return null;
|
|
29
|
+
for (const part of header.split(";")) {
|
|
30
|
+
const [name, ...rest] = part.trim().split("=");
|
|
31
|
+
if (name === CLIENT_COOKIE) {
|
|
32
|
+
const value = rest.join("=").trim();
|
|
33
|
+
return value === "" ? null : value;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
export function registerLiveRoutes(app, deps) {
|
|
39
|
+
app.get("/api/events", (request, response) => {
|
|
40
|
+
const existing = clientIdOf({
|
|
41
|
+
header: request.headers[CLIENT_HEADER],
|
|
42
|
+
query: request.query?.client,
|
|
43
|
+
cookie: request.headers.cookie,
|
|
44
|
+
});
|
|
45
|
+
const clientId = existing ?? randomUUID();
|
|
46
|
+
if (!existing) {
|
|
47
|
+
response.setHeader("Set-Cookie", `${CLIENT_COOKIE}=${clientId}; Path=/; HttpOnly; SameSite=Lax; Max-Age=31536000`);
|
|
48
|
+
}
|
|
49
|
+
// SSE 首帧快照 ~136KB,wifi/外网下很慢;能 gzip 就流式压(不能攒,必须边压边发)
|
|
50
|
+
const gzip = /gzip/i.test(String(request.headers["accept-encoding"] ?? ""));
|
|
51
|
+
response.writeHead(200, {
|
|
52
|
+
"content-type": "text/event-stream; charset=utf-8",
|
|
53
|
+
// no-store:**不能让任何代理/缓存重放这条响应**。曾经怀疑过反向代理把
|
|
54
|
+
// 首个客户端的 `Set-Cookie` 或首帧快照缓存后重放给别人(那会让两个设备拿到同一个
|
|
55
|
+
// 客户端标识 → 互相拽着切会话)。no-transform 顺带禁掉压缩二次加工。
|
|
56
|
+
"cache-control": "no-store, no-transform",
|
|
57
|
+
connection: "keep-alive",
|
|
58
|
+
// nginx 默认会缓冲响应体,SSE 必须显式关掉,否则事件全卡在缓冲里
|
|
59
|
+
"x-accel-buffering": "no",
|
|
60
|
+
...(gzip ? { "content-encoding": "gzip", vary: "accept-encoding" } : {}),
|
|
61
|
+
});
|
|
62
|
+
response.flushHeaders?.();
|
|
63
|
+
// `flush: Z_SYNC_FLUSH` 是必需的,不是调优项:SSE 每一帧都得**立刻**落地。
|
|
64
|
+
// 默认模式(Z_NO_FLUSH)下 zlib 会把小帧攒在内部缓冲区里,几 KB~几十 KB 的快照
|
|
65
|
+
// 要等下一大块数据才被推出去 —— 表现就是「切会话 / 新建会话页面不跳、快照像停了」。
|
|
66
|
+
const compressor = gzip ? createGzip({ level: 6, flush: zlibConstants.Z_SYNC_FLUSH }) : null;
|
|
67
|
+
compressor?.on("data", (chunk) => response.write(chunk));
|
|
68
|
+
const send = (text) => (compressor ? compressor.write(text) : response.write(text));
|
|
69
|
+
const sink = (payload) => send(`data: ${payload}\n\n`);
|
|
70
|
+
deps.sinks.set(clientId, sink);
|
|
71
|
+
// 能力协商:带 slim=1 的客户端由服务端决定「会话清单只在变化时带」
|
|
72
|
+
deps.clientTracker?.setSlim(clientId, request.query?.slim === "1");
|
|
73
|
+
// 带上客户端标识前缀:排查「两个设备互相拽着切会话」时,一眼能看出它们是不是同一个 id
|
|
74
|
+
//
|
|
75
|
+
// 故意用 debug:每个标签页 / 每次重连都会写一条,正常运行时纯噪声(日志文件里一天几十行)。
|
|
76
|
+
// 真要查「谁把谁拽走了」时用 `POP_AGENT_LOG=debug pop` 打开。
|
|
77
|
+
deps.logger.debug("SSE 已连接", {
|
|
78
|
+
clients: deps.sinks.size,
|
|
79
|
+
slim: request.query?.slim === "1",
|
|
80
|
+
client: clientId.slice(0, 8),
|
|
81
|
+
from: existing ? (request.query?.client ? "query" : "cookie") : "new",
|
|
82
|
+
});
|
|
83
|
+
// 首屏:立刻给这个客户端一份「属于它自己」的快照
|
|
84
|
+
sink(JSON.stringify({ type: "event", event: "snapshot", snapshot: deps.snapshotFor(clientId) }));
|
|
85
|
+
// 心跳:中间代理一般 60s 掐空闲连接,25s 发一次注释行足够保活
|
|
86
|
+
const beat = setInterval(() => send(": ping\n\n"), 25_000);
|
|
87
|
+
request.on("close", () => {
|
|
88
|
+
clearInterval(beat);
|
|
89
|
+
compressor?.end();
|
|
90
|
+
// 只删**自己**那条:同一个 clientId 重连时,新连接会覆盖 map 里的条目,
|
|
91
|
+
// 旧连接的 close 若照删不误,就会把新连接一起干掉(心跳还在、但再也收不到快照/事件)。
|
|
92
|
+
if (deps.sinks.get(clientId) === sink) {
|
|
93
|
+
deps.sinks.delete(clientId);
|
|
94
|
+
deps.clientTracker?.drop(clientId);
|
|
95
|
+
}
|
|
96
|
+
deps.logger.debug("SSE 已断开", { clients: deps.sinks.size });
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
app.post("/api/rpc", async (request, response) => {
|
|
100
|
+
// 同上:RPC 的结果里带快照,绝不能被缓存重放
|
|
101
|
+
response.setHeader("cache-control", "no-store");
|
|
102
|
+
const clientId = clientIdOf({ header: request.headers[CLIENT_HEADER], cookie: request.headers.cookie }) ?? "anonymous";
|
|
103
|
+
const frames = [];
|
|
104
|
+
const shim = { send: (payload) => frames.push(payload) };
|
|
105
|
+
try {
|
|
106
|
+
await deps.handleMessage(clientId, shim, JSON.stringify(request.body));
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
deps.logger.info("RPC 处理失败", { message: error instanceof Error ? error.message : String(error) });
|
|
110
|
+
response.status(500).json({ frames, error: "handler_failed" });
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
// 同步产生的帧(reply / notice)随响应带回;异步事件走 SSE
|
|
114
|
+
response.json({ frames });
|
|
115
|
+
});
|
|
116
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 登录活动记录(设置 → 远程 里那一列):谁从哪个 IP 试过、成没成、错了几次、什么时候被锁。
|
|
3
|
+
*
|
|
4
|
+
* 用户 2026-11-02:「远程里可以显示哪个 ip 登陆过、登错几次、登陆成功等信息,按时间最新的在最前面」。
|
|
5
|
+
*
|
|
6
|
+
* 几条刻意的取舍:
|
|
7
|
+
* 1. **只记「真的比对过密码」的那两次结果**(成功 / 失败)和「刚被锁」这一件事。
|
|
8
|
+
* 锁住之后的 429 不再入账 —— 否则爆破时每来一个请求就写一条,日志立刻变成噪音;
|
|
9
|
+
* 2. 落盘(`<dataDir>/login-activity.json`,保留最近 N 条):重启后还能看见「昨晚谁在试我的密码」;
|
|
10
|
+
* 3. 写盘是同步的:这些事件的数量被登录锁**天然限制**(每个来源一个窗口最多 N 次),
|
|
11
|
+
* 不会出现洪水式写盘 —— 换来的好处是不用调试时还要等一个 debounce 定时器。
|
|
12
|
+
*/
|
|
13
|
+
import { existsSync } from "node:fs";
|
|
14
|
+
import { readJsonObject, writeJsonObject } from "../util/json-file.js";
|
|
15
|
+
const STORE_VERSION = 1;
|
|
16
|
+
/** 最多留多少条:一屏能看十几条,200 条够回溯好几天了 */
|
|
17
|
+
export const MAX_LOGIN_ACTIVITY = 200;
|
|
18
|
+
function readEntries(raw, limit) {
|
|
19
|
+
if (!Array.isArray(raw))
|
|
20
|
+
return [];
|
|
21
|
+
const out = [];
|
|
22
|
+
for (const item of raw) {
|
|
23
|
+
if (item === null || typeof item !== "object")
|
|
24
|
+
continue;
|
|
25
|
+
const record = item;
|
|
26
|
+
const at = typeof record.at === "number" && Number.isFinite(record.at) ? record.at : 0;
|
|
27
|
+
const address = typeof record.address === "string" ? record.address : "";
|
|
28
|
+
const kind = record.kind;
|
|
29
|
+
if (at <= 0 || address === "")
|
|
30
|
+
continue;
|
|
31
|
+
if (kind !== "success" && kind !== "failure" && kind !== "locked")
|
|
32
|
+
continue;
|
|
33
|
+
out.push({
|
|
34
|
+
at,
|
|
35
|
+
address,
|
|
36
|
+
kind,
|
|
37
|
+
...(typeof record.attempts === "number" ? { attempts: record.attempts } : {}),
|
|
38
|
+
...(typeof record.lockMinutes === "number" ? { lockMinutes: record.lockMinutes } : {}),
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
// 文件里是「新 → 旧」存的,读回来也按时间倒序,别信写入顺序
|
|
42
|
+
return out.sort((a, b) => b.at - a.at).slice(0, limit);
|
|
43
|
+
}
|
|
44
|
+
export function createLoginActivity(options = {}) {
|
|
45
|
+
const clock = options.now ?? (() => Date.now());
|
|
46
|
+
const limit = options.limit ?? MAX_LOGIN_ACTIVITY;
|
|
47
|
+
/** 内存里始终是「新 → 旧」 */
|
|
48
|
+
let entries = [];
|
|
49
|
+
if (options.file && existsSync(options.file)) {
|
|
50
|
+
const loaded = readJsonObject(options.file);
|
|
51
|
+
if ("json" in loaded)
|
|
52
|
+
entries = readEntries(loaded.json.entries, limit);
|
|
53
|
+
// 坏文件不硬修(`json-file` 的规矩):这次当空表用,下次记录时才覆盖
|
|
54
|
+
}
|
|
55
|
+
return {
|
|
56
|
+
record(entry) {
|
|
57
|
+
const next = {
|
|
58
|
+
at: entry.at ?? clock(),
|
|
59
|
+
address: entry.address,
|
|
60
|
+
kind: entry.kind,
|
|
61
|
+
...(entry.attempts !== undefined ? { attempts: entry.attempts } : {}),
|
|
62
|
+
...(entry.lockMinutes !== undefined ? { lockMinutes: entry.lockMinutes } : {}),
|
|
63
|
+
};
|
|
64
|
+
entries = [next, ...entries].slice(0, limit);
|
|
65
|
+
if (!options.file)
|
|
66
|
+
return;
|
|
67
|
+
try {
|
|
68
|
+
writeJsonObject(options.file, { version: STORE_VERSION, entries });
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
// 写不进去不该影响登录本身:内存里这份照样能在界面上看到
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
list: () => entries.map((entry) => ({ ...entry })),
|
|
75
|
+
};
|
|
76
|
+
}
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 登录失败节流(挡在线爆破):**同一来源连续输错 N 次 → 暂停 M 分钟**。
|
|
3
|
+
*
|
|
4
|
+
* 用户 2026-11-02 的要求:默认「3 次 / 30 分钟」,次数与时长可以在 设置 → 远程 里改。
|
|
5
|
+
*
|
|
6
|
+
* 为什么单独成一个模块(而不是继续写在 `auth-routes.ts` 里):
|
|
7
|
+
* 1. 这块逻辑要能单测 —— 时间注入进来,不用真的等 30 分钟;
|
|
8
|
+
* 2. 它要**落盘**(重启之后锁还在),而路由文件里已经混着密码、cookie、HTML 登录页三件事;
|
|
9
|
+
* 3. 语义要说清:以前的实现是「10 分钟内失败 20 次就拒收」,等于**不会锁**,只是把门槛抬高;
|
|
10
|
+
* 现在是真的锁一段时间。
|
|
11
|
+
*
|
|
12
|
+
* 规则细节(都是刻意的):
|
|
13
|
+
* - **按来源(IP)分开记**:一个人输错不该把别人挡在外面;反过来,隧道/反代后面所有请求
|
|
14
|
+
* 可能是同一个 IP,那就一起锁 —— 这是 `auth.ts` 早就有的取舍(它就是这么算来源的);
|
|
15
|
+
* - 计数窗口 = 锁定时长:所谓「连续」就是这段时间里累计的次数,过期自动归零;
|
|
16
|
+
* - **到达上限的那一次就锁**(第 3 次输错返回的不再是 401,而是「已暂停 30 分钟」);
|
|
17
|
+
* - 登录成功、改密码 → 清掉这个来源的计数与锁;
|
|
18
|
+
* - 失败计数只在内存(重启归零可以接受),但**锁落盘** —— 否则被爆破时把进程搞挂一次就解锁了。
|
|
19
|
+
*/
|
|
20
|
+
import { existsSync } from "node:fs";
|
|
21
|
+
import { readJsonObject, writeJsonObject } from "../util/json-file.js";
|
|
22
|
+
/** 默认:连续错 3 次,暂停 30 分钟(用户定的默认值) */
|
|
23
|
+
export const DEFAULT_LOGIN_GUARD = { maxFailures: 3, lockMinutes: 30 };
|
|
24
|
+
/** 可填范围(挡住「0 次」「暂停 100 年」这类把服务锁死的输入) */
|
|
25
|
+
export const LOGIN_GUARD_RANGE = {
|
|
26
|
+
maxFailures: { min: 1, max: 20 },
|
|
27
|
+
lockMinutes: { min: 1, max: 1440 },
|
|
28
|
+
};
|
|
29
|
+
/** 同时跟踪的来源上限:只挡脏 IP 把 Map 撑大,不清会把内存吃光 */
|
|
30
|
+
const MAX_TRACKED_SOURCES = 512;
|
|
31
|
+
const STORE_VERSION = 1;
|
|
32
|
+
function clampInt(value, range) {
|
|
33
|
+
if (typeof value !== "number" || !Number.isFinite(value))
|
|
34
|
+
return null;
|
|
35
|
+
const rounded = Math.round(value);
|
|
36
|
+
return rounded < range.min || rounded > range.max ? null : rounded;
|
|
37
|
+
}
|
|
38
|
+
/** 把任意输入收敛成合法策略(读坏文件 / 老版本文件时用;**不**给用户看这个宽松行为) */
|
|
39
|
+
export function normalizeLoginGuardPolicy(input, fallback = DEFAULT_LOGIN_GUARD) {
|
|
40
|
+
const source = input !== null && typeof input === "object" ? input : {};
|
|
41
|
+
return {
|
|
42
|
+
maxFailures: clampInt(source.maxFailures, LOGIN_GUARD_RANGE.maxFailures) ?? fallback.maxFailures,
|
|
43
|
+
lockMinutes: clampInt(source.lockMinutes, LOGIN_GUARD_RANGE.lockMinutes) ?? fallback.lockMinutes,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
export function createLoginGuard(options = {}) {
|
|
47
|
+
const clock = options.now ?? (() => Date.now());
|
|
48
|
+
let policy = { ...DEFAULT_LOGIN_GUARD };
|
|
49
|
+
const failures = new Map();
|
|
50
|
+
let locks = new Map();
|
|
51
|
+
/** 读一次盘:策略 + 仍在生效的锁(过期锁顺手丢掉) */
|
|
52
|
+
if (options.file && existsSync(options.file)) {
|
|
53
|
+
const loaded = readJsonObject(options.file);
|
|
54
|
+
if ("json" in loaded) {
|
|
55
|
+
policy = normalizeLoginGuardPolicy(loaded.json);
|
|
56
|
+
const now = clock();
|
|
57
|
+
const raw = loaded.json.locks;
|
|
58
|
+
if (Array.isArray(raw)) {
|
|
59
|
+
for (const entry of raw) {
|
|
60
|
+
if (entry === null || typeof entry !== "object")
|
|
61
|
+
continue;
|
|
62
|
+
const record = entry;
|
|
63
|
+
const address = typeof record.address === "string" ? record.address : "";
|
|
64
|
+
const until = typeof record.until === "number" && Number.isFinite(record.until) ? record.until : 0;
|
|
65
|
+
if (address !== "" && until > now)
|
|
66
|
+
locks.set(address, until);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
// 坏文件不硬修(`json-file` 的规矩):这次就当默认值用,等真正保存策略时才覆盖它
|
|
71
|
+
}
|
|
72
|
+
const persist = () => {
|
|
73
|
+
if (!options.file)
|
|
74
|
+
return;
|
|
75
|
+
try {
|
|
76
|
+
writeJsonObject(options.file, {
|
|
77
|
+
version: STORE_VERSION,
|
|
78
|
+
maxFailures: policy.maxFailures,
|
|
79
|
+
lockMinutes: policy.lockMinutes,
|
|
80
|
+
locks: [...locks].map(([address, until]) => ({ address, until })),
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
// 落盘失败不该让登录流程挂掉:内存里的策略与锁照常生效,只是「重启后还记得」这条丢了
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
const windowMs = () => policy.lockMinutes * 60_000;
|
|
88
|
+
/** 过期锁顺手清掉(Map 只增不减的话,长时间运行会越积越多) */
|
|
89
|
+
const prune = (now) => {
|
|
90
|
+
for (const [address, until] of locks)
|
|
91
|
+
if (until <= now)
|
|
92
|
+
locks.delete(address);
|
|
93
|
+
};
|
|
94
|
+
const status = (address) => {
|
|
95
|
+
const now = clock();
|
|
96
|
+
prune(now);
|
|
97
|
+
const until = locks.get(address);
|
|
98
|
+
if (until !== undefined && until > now) {
|
|
99
|
+
return {
|
|
100
|
+
locked: true,
|
|
101
|
+
retryAfterSeconds: Math.ceil((until - now) / 1000),
|
|
102
|
+
failures: policy.maxFailures,
|
|
103
|
+
remainingAttempts: 0,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
const record = failures.get(address);
|
|
107
|
+
if (!record || now > record.resetAt) {
|
|
108
|
+
failures.delete(address);
|
|
109
|
+
return { locked: false, retryAfterSeconds: 0, failures: 0, remainingAttempts: policy.maxFailures };
|
|
110
|
+
}
|
|
111
|
+
return {
|
|
112
|
+
locked: false,
|
|
113
|
+
retryAfterSeconds: 0,
|
|
114
|
+
failures: record.count,
|
|
115
|
+
remainingAttempts: Math.max(0, policy.maxFailures - record.count),
|
|
116
|
+
};
|
|
117
|
+
};
|
|
118
|
+
return {
|
|
119
|
+
policy: () => ({ ...policy }),
|
|
120
|
+
status,
|
|
121
|
+
setPolicy(patch) {
|
|
122
|
+
const source = patch !== null && typeof patch === "object" ? patch : {};
|
|
123
|
+
const next = { ...policy };
|
|
124
|
+
if (source.maxFailures !== undefined) {
|
|
125
|
+
const value = clampInt(source.maxFailures, LOGIN_GUARD_RANGE.maxFailures);
|
|
126
|
+
if (value === null) {
|
|
127
|
+
const { min, max } = LOGIN_GUARD_RANGE.maxFailures;
|
|
128
|
+
return { ok: false, error: `失败次数要在 ${min}–${max} 之间` };
|
|
129
|
+
}
|
|
130
|
+
next.maxFailures = value;
|
|
131
|
+
}
|
|
132
|
+
if (source.lockMinutes !== undefined) {
|
|
133
|
+
const value = clampInt(source.lockMinutes, LOGIN_GUARD_RANGE.lockMinutes);
|
|
134
|
+
if (value === null) {
|
|
135
|
+
const { min, max } = LOGIN_GUARD_RANGE.lockMinutes;
|
|
136
|
+
return { ok: false, error: `暂停时长要在 ${min}–${max} 分钟之间` };
|
|
137
|
+
}
|
|
138
|
+
next.lockMinutes = value;
|
|
139
|
+
}
|
|
140
|
+
policy = next;
|
|
141
|
+
// 策略变了,已经数到一半的计数按新窗口重算没有意义:直接清空,避免「改完更严了还留着旧次数」
|
|
142
|
+
failures.clear();
|
|
143
|
+
persist();
|
|
144
|
+
return { ok: true, policy: { ...policy } };
|
|
145
|
+
},
|
|
146
|
+
noteFailure(address) {
|
|
147
|
+
const now = clock();
|
|
148
|
+
const current = failures.get(address);
|
|
149
|
+
const record = !current || now > current.resetAt
|
|
150
|
+
? { count: 1, resetAt: now + windowMs() }
|
|
151
|
+
: { count: current.count + 1, resetAt: current.resetAt };
|
|
152
|
+
failures.set(address, record);
|
|
153
|
+
if (failures.size > MAX_TRACKED_SOURCES) {
|
|
154
|
+
for (const [key, value] of failures)
|
|
155
|
+
if (now > value.resetAt)
|
|
156
|
+
failures.delete(key);
|
|
157
|
+
}
|
|
158
|
+
if (record.count >= policy.maxFailures) {
|
|
159
|
+
failures.delete(address);
|
|
160
|
+
locks.set(address, now + windowMs());
|
|
161
|
+
persist();
|
|
162
|
+
}
|
|
163
|
+
return status(address);
|
|
164
|
+
},
|
|
165
|
+
clear(address) {
|
|
166
|
+
const hadLock = locks.delete(address);
|
|
167
|
+
failures.delete(address);
|
|
168
|
+
if (hadLock)
|
|
169
|
+
persist();
|
|
170
|
+
},
|
|
171
|
+
clearAll() {
|
|
172
|
+
const hadLock = locks.size > 0;
|
|
173
|
+
locks = new Map();
|
|
174
|
+
failures.clear();
|
|
175
|
+
if (hadLock)
|
|
176
|
+
persist();
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
/** 「还要等多久」给人看的写法(登录页 / 429 文案共用) */
|
|
181
|
+
export function describeLockout(status) {
|
|
182
|
+
const minutes = Math.ceil(status.retryAfterSeconds / 60);
|
|
183
|
+
const unit = minutes >= 60 ? `${Math.ceil(minutes / 60)} 小时` : `${minutes} 分钟`;
|
|
184
|
+
return `密码错误次数过多,请 ${unit}后再试`;
|
|
185
|
+
}
|