@waq666/pneuma 0.0.0-stage → 0.7.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/LICENSE +21 -0
- package/README.md +206 -3
- package/bin/dsc.js +91 -0
- package/bin/headless.cjs +40 -0
- package/lib/adapter/core-runtime.d.ts +11 -0
- package/lib/adapter/core-runtime.js +259 -0
- package/lib/adapter/host.d.ts +18 -0
- package/lib/adapter/host.js +14 -0
- package/lib/adapter/runtime.d.ts +15 -0
- package/lib/adapter/runtime.js +290 -0
- package/lib/adapter/transcript.d.ts +184 -0
- package/lib/adapter/transcript.js +605 -0
- package/lib/app/AgentsOverlay.d.ts +18 -0
- package/lib/app/AgentsOverlay.js +63 -0
- package/lib/app/App.d.ts +15 -0
- package/lib/app/App.js +1650 -0
- package/lib/app/App.js.nv-backup +1650 -0
- package/lib/app/ApprovalCard.d.ts +12 -0
- package/lib/app/ApprovalCard.js +76 -0
- package/lib/app/AskCard.d.ts +20 -0
- package/lib/app/AskCard.js +45 -0
- package/lib/app/ChatView.d.ts +63 -0
- package/lib/app/ChatView.js +310 -0
- package/lib/app/Composer.d.ts +50 -0
- package/lib/app/Composer.js +282 -0
- package/lib/app/DiffBlock.d.ts +6 -0
- package/lib/app/DiffBlock.js +37 -0
- package/lib/app/MarkdownView.d.ts +6 -0
- package/lib/app/MarkdownView.js +99 -0
- package/lib/app/ModelPicker.d.ts +7 -0
- package/lib/app/ModelPicker.js +18 -0
- package/lib/app/PlanReviewCard.d.ts +13 -0
- package/lib/app/PlanReviewCard.js +54 -0
- package/lib/app/PresetPicker.d.ts +11 -0
- package/lib/app/PresetPicker.js +19 -0
- package/lib/app/PreviewOverlay.d.ts +14 -0
- package/lib/app/PreviewOverlay.js +22 -0
- package/lib/app/SessionPicker.d.ts +43 -0
- package/lib/app/SessionPicker.js +56 -0
- package/lib/app/SettingsOverlay.d.ts +32 -0
- package/lib/app/SettingsOverlay.js +157 -0
- package/lib/app/SkillsPicker.d.ts +7 -0
- package/lib/app/SkillsPicker.js +19 -0
- package/lib/app/StatusBar.d.ts +22 -0
- package/lib/app/StatusBar.js +201 -0
- package/lib/app/TaskStrips.d.ts +13 -0
- package/lib/app/TaskStrips.js +40 -0
- package/lib/app/TimelineRail.d.ts +22 -0
- package/lib/app/TimelineRail.js +160 -0
- package/lib/app/TimelineRail.js.negbak +98 -0
- package/lib/app/TimelineRail.js.nv-backup +160 -0
- package/lib/app/ToolCard.d.ts +11 -0
- package/lib/app/ToolCard.js +64 -0
- package/lib/app/ToolCard.js.nv-backup +64 -0
- package/lib/app/TranscriptOverlay.d.ts +27 -0
- package/lib/app/TranscriptOverlay.js +36 -0
- package/lib/app/TranscriptViewport.d.ts +78 -0
- package/lib/app/TranscriptViewport.js +218 -0
- package/lib/app/Welcome.d.ts +6 -0
- package/lib/app/Welcome.js +71 -0
- package/lib/app/attach.d.ts +16 -0
- package/lib/app/attach.js +58 -0
- package/lib/app/click.d.ts +40 -0
- package/lib/app/click.js +103 -0
- package/lib/app/clipboard-image.d.ts +11 -0
- package/lib/app/clipboard-image.js +66 -0
- package/lib/app/cursor.d.ts +4 -0
- package/lib/app/cursor.js +24 -0
- package/lib/app/hello.d.ts +11 -0
- package/lib/app/hello.js +8 -0
- package/lib/app/history-store.d.ts +4 -0
- package/lib/app/history-store.js +41 -0
- package/lib/app/image-blocks.d.ts +12 -0
- package/lib/app/image-blocks.js +125 -0
- package/lib/app/keymap.d.ts +56 -0
- package/lib/app/keymap.js +56 -0
- package/lib/app/markdown.d.ts +54 -0
- package/lib/app/markdown.js +189 -0
- package/lib/app/mouse.d.ts +33 -0
- package/lib/app/mouse.js +35 -0
- package/lib/app/pointer.d.ts +16 -0
- package/lib/app/pointer.js +60 -0
- package/lib/app/scroll-model.d.ts +240 -0
- package/lib/app/scroll-model.js +347 -0
- package/lib/app/settings-model.d.ts +69 -0
- package/lib/app/settings-model.js +114 -0
- package/lib/app/snapshot-subscribe.d.ts +34 -0
- package/lib/app/snapshot-subscribe.js +110 -0
- package/lib/app/theme.d.ts +194 -0
- package/lib/app/theme.js +186 -0
- package/lib/app/ticker.d.ts +7 -0
- package/lib/app/ticker.js +53 -0
- package/lib/boot.d.ts +1 -0
- package/lib/boot.js +34 -0
- package/lib/commands.d.ts +49 -0
- package/lib/commands.js +115 -0
- package/lib/contract.d.ts +1973 -0
- package/lib/contract.js +36 -0
- package/lib/core/agent-roles.d.ts +52 -0
- package/lib/core/agent-roles.js +274 -0
- package/lib/core/approval-floor.d.ts +183 -0
- package/lib/core/approval-floor.js +679 -0
- package/lib/core/approval.d.ts +42 -0
- package/lib/core/approval.js +8 -0
- package/lib/core/audit.d.ts +61 -0
- package/lib/core/audit.js +59 -0
- package/lib/core/boot-notices.d.ts +6 -0
- package/lib/core/boot-notices.js +61 -0
- package/lib/core/cdp/actions.d.ts +182 -0
- package/lib/core/cdp/actions.js +649 -0
- package/lib/core/cdp/launch.d.ts +192 -0
- package/lib/core/cdp/launch.js +491 -0
- package/lib/core/cdp/snapshot.d.ts +139 -0
- package/lib/core/cdp/snapshot.js +316 -0
- package/lib/core/cdp/transport.d.ts +160 -0
- package/lib/core/cdp/transport.js +359 -0
- package/lib/core/command-policy.d.ts +108 -0
- package/lib/core/command-policy.js +580 -0
- package/lib/core/commands-completion.d.ts +78 -0
- package/lib/core/commands-completion.js +167 -0
- package/lib/core/compact-anchors.d.ts +76 -0
- package/lib/core/compact-anchors.js +284 -0
- package/lib/core/compact.d.ts +110 -0
- package/lib/core/compact.js +210 -0
- package/lib/core/config-store.d.ts +38 -0
- package/lib/core/config-store.js +307 -0
- package/lib/core/config.d.ts +36 -0
- package/lib/core/config.js +124 -0
- package/lib/core/connection-test.d.ts +60 -0
- package/lib/core/connection-test.js +84 -0
- package/lib/core/desktop-control.d.ts +298 -0
- package/lib/core/desktop-control.js +1311 -0
- package/lib/core/diff-text.d.ts +74 -0
- package/lib/core/diff-text.js +257 -0
- package/lib/core/dock-browser.d.ts +137 -0
- package/lib/core/dock-browser.js +146 -0
- package/lib/core/dock-ops.d.ts +59 -0
- package/lib/core/dock-ops.js +104 -0
- package/lib/core/dock-session.d.ts +39 -0
- package/lib/core/dock-session.js +28 -0
- package/lib/core/dsh-compat/hook.d.ts +18 -0
- package/lib/core/dsh-compat/hook.js +52 -0
- package/lib/core/dsh-compat/resolve-hook.d.ts +5 -0
- package/lib/core/dsh-compat/resolve-hook.js +59 -0
- package/lib/core/dsh-compat/tools-facade.d.ts +47 -0
- package/lib/core/dsh-compat/tools-facade.js +108 -0
- package/lib/core/err-text.d.ts +13 -0
- package/lib/core/err-text.js +15 -0
- package/lib/core/events.d.ts +149 -0
- package/lib/core/events.js +1 -0
- package/lib/core/git-exec.d.ts +23 -0
- package/lib/core/git-exec.js +72 -0
- package/lib/core/git-info.d.ts +47 -0
- package/lib/core/git-info.js +157 -0
- package/lib/core/goal.d.ts +89 -0
- package/lib/core/goal.js +189 -0
- package/lib/core/hooks.d.ts +272 -0
- package/lib/core/hooks.js +818 -0
- package/lib/core/host-browser-link.d.ts +59 -0
- package/lib/core/host-browser-link.js +9 -0
- package/lib/core/host-methods.d.ts +25 -0
- package/lib/core/host-methods.js +103 -0
- package/lib/core/image-diet.d.ts +77 -0
- package/lib/core/image-diet.js +127 -0
- package/lib/core/learnings/ledger.d.ts +73 -0
- package/lib/core/learnings/ledger.js +273 -0
- package/lib/core/learnings/skill-write.d.ts +250 -0
- package/lib/core/learnings/skill-write.js +854 -0
- package/lib/core/learnings/store.d.ts +102 -0
- package/lib/core/learnings/store.js +266 -0
- package/lib/core/lifecycle-hooks.d.ts +189 -0
- package/lib/core/lifecycle-hooks.js +576 -0
- package/lib/core/llm.d.ts +283 -0
- package/lib/core/llm.js +565 -0
- package/lib/core/lockfile.d.ts +17 -0
- package/lib/core/lockfile.js +110 -0
- package/lib/core/loop.d.ts +185 -0
- package/lib/core/loop.js +587 -0
- package/lib/core/lsp/client.d.ts +189 -0
- package/lib/core/lsp/client.js +690 -0
- package/lib/core/lsp/framing.d.ts +84 -0
- package/lib/core/lsp/framing.js +181 -0
- package/lib/core/lsp/line-shift.d.ts +28 -0
- package/lib/core/lsp/line-shift.js +127 -0
- package/lib/core/lsp/manager.d.ts +93 -0
- package/lib/core/lsp/manager.js +418 -0
- package/lib/core/lsp/normalize.d.ts +32 -0
- package/lib/core/lsp/normalize.js +139 -0
- package/lib/core/lsp/servers.d.ts +106 -0
- package/lib/core/lsp/servers.js +352 -0
- package/lib/core/lsp/uri.d.ts +76 -0
- package/lib/core/lsp/uri.js +179 -0
- package/lib/core/market.d.ts +51 -0
- package/lib/core/market.js +358 -0
- package/lib/core/mcp.d.ts +201 -0
- package/lib/core/mcp.js +836 -0
- package/lib/core/memory-eval.d.ts +158 -0
- package/lib/core/memory-eval.js +215 -0
- package/lib/core/memory.d.ts +266 -0
- package/lib/core/memory.js +703 -0
- package/lib/core/mention.d.ts +57 -0
- package/lib/core/mention.js +129 -0
- package/lib/core/microcompact.d.ts +48 -0
- package/lib/core/microcompact.js +79 -0
- package/lib/core/migrate.d.ts +56 -0
- package/lib/core/migrate.js +134 -0
- package/lib/core/model-caps.d.ts +83 -0
- package/lib/core/model-caps.js +150 -0
- package/lib/core/model-discovery.d.ts +19 -0
- package/lib/core/model-discovery.js +69 -0
- package/lib/core/modes.d.ts +72 -0
- package/lib/core/modes.js +152 -0
- package/lib/core/native-bridge.d.ts +135 -0
- package/lib/core/native-bridge.js +563 -0
- package/lib/core/path-policy.d.ts +32 -0
- package/lib/core/path-policy.js +248 -0
- package/lib/core/permission-presets.d.ts +67 -0
- package/lib/core/permission-presets.js +75 -0
- package/lib/core/plugin-loader.d.ts +36 -0
- package/lib/core/plugin-loader.js +294 -0
- package/lib/core/plugin-registry.d.ts +113 -0
- package/lib/core/plugin-registry.js +256 -0
- package/lib/core/prefs.d.ts +82 -0
- package/lib/core/prefs.js +372 -0
- package/lib/core/presets.d.ts +80 -0
- package/lib/core/presets.js +373 -0
- package/lib/core/pricing.d.ts +90 -0
- package/lib/core/pricing.js +126 -0
- package/lib/core/prompt.d.ts +92 -0
- package/lib/core/prompt.js +339 -0
- package/lib/core/ptc.d.ts +66 -0
- package/lib/core/ptc.js +70 -0
- package/lib/core/remote/frames.d.ts +91 -0
- package/lib/core/remote/frames.js +174 -0
- package/lib/core/remote/notify.d.ts +54 -0
- package/lib/core/remote/notify.js +77 -0
- package/lib/core/remote/owner.d.ts +32 -0
- package/lib/core/remote/owner.js +75 -0
- package/lib/core/remote/pairing.d.ts +104 -0
- package/lib/core/remote/pairing.js +261 -0
- package/lib/core/remote/push.d.ts +80 -0
- package/lib/core/remote/push.js +222 -0
- package/lib/core/remote/store.d.ts +18 -0
- package/lib/core/remote/store.js +59 -0
- package/lib/core/remote/tickets.d.ts +20 -0
- package/lib/core/remote/tickets.js +49 -0
- package/lib/core/remote/uploads.d.ts +63 -0
- package/lib/core/remote/uploads.js +161 -0
- package/lib/core/request-attribution.d.ts +26 -0
- package/lib/core/request-attribution.js +86 -0
- package/lib/core/request-invariant.d.ts +61 -0
- package/lib/core/request-invariant.js +199 -0
- package/lib/core/sandbox/backends.d.ts +64 -0
- package/lib/core/sandbox/backends.js +194 -0
- package/lib/core/sandbox/execpolicy.d.ts +93 -0
- package/lib/core/sandbox/execpolicy.js +452 -0
- package/lib/core/sandbox/net-proxy.d.ts +31 -0
- package/lib/core/sandbox/net-proxy.js +162 -0
- package/lib/core/sandbox/policy.d.ts +211 -0
- package/lib/core/sandbox/policy.js +559 -0
- package/lib/core/sandbox/win/abi.d.ts +161 -0
- package/lib/core/sandbox/win/abi.js +169 -0
- package/lib/core/sandbox/win/account.d.ts +71 -0
- package/lib/core/sandbox/win/account.js +206 -0
- package/lib/core/sandbox/win/acl.d.ts +54 -0
- package/lib/core/sandbox/win/acl.js +339 -0
- package/lib/core/sandbox/win/backend.d.ts +61 -0
- package/lib/core/sandbox/win/backend.js +249 -0
- package/lib/core/sandbox/win/doctor.d.ts +24 -0
- package/lib/core/sandbox/win/doctor.js +272 -0
- package/lib/core/sandbox/win/ffi.d.ts +107 -0
- package/lib/core/sandbox/win/ffi.js +166 -0
- package/lib/core/sandbox/win/net-abi.d.ts +154 -0
- package/lib/core/sandbox/win/net-abi.js +316 -0
- package/lib/core/sandbox/win/runner-entry.d.ts +35 -0
- package/lib/core/sandbox/win/runner-entry.js +391 -0
- package/lib/core/sandbox/win/setup.d.ts +60 -0
- package/lib/core/sandbox/win/setup.js +986 -0
- package/lib/core/sandbox/win/token.d.ts +35 -0
- package/lib/core/sandbox/win/token.js +238 -0
- package/lib/core/sandbox/win/wfp.d.ts +41 -0
- package/lib/core/sandbox/win/wfp.js +200 -0
- package/lib/core/sandbox/win/workspace-sid.d.ts +9 -0
- package/lib/core/sandbox/win/workspace-sid.js +45 -0
- package/lib/core/schedule/rule.d.ts +184 -0
- package/lib/core/schedule/rule.js +745 -0
- package/lib/core/schedule/runner.d.ts +185 -0
- package/lib/core/schedule/runner.js +678 -0
- package/lib/core/schedule/store.d.ts +182 -0
- package/lib/core/schedule/store.js +455 -0
- package/lib/core/secrets.d.ts +41 -0
- package/lib/core/secrets.js +150 -0
- package/lib/core/session-export.d.ts +18 -0
- package/lib/core/session-export.js +73 -0
- package/lib/core/session-index.d.ts +159 -0
- package/lib/core/session-index.js +683 -0
- package/lib/core/session-meta.d.ts +41 -0
- package/lib/core/session-meta.js +97 -0
- package/lib/core/session.d.ts +356 -0
- package/lib/core/session.js +998 -0
- package/lib/core/skills.d.ts +60 -0
- package/lib/core/skills.js +171 -0
- package/lib/core/spill.d.ts +159 -0
- package/lib/core/spill.js +329 -0
- package/lib/core/surface-registry.d.ts +45 -0
- package/lib/core/surface-registry.js +67 -0
- package/lib/core/team-board.d.ts +95 -0
- package/lib/core/team-board.js +316 -0
- package/lib/core/throughput.d.ts +46 -0
- package/lib/core/throughput.js +40 -0
- package/lib/core/todo-reminder.d.ts +44 -0
- package/lib/core/todo-reminder.js +55 -0
- package/lib/core/todo.d.ts +81 -0
- package/lib/core/todo.js +222 -0
- package/lib/core/token-estimate.d.ts +55 -0
- package/lib/core/token-estimate.js +82 -0
- package/lib/core/tool-guards.d.ts +103 -0
- package/lib/core/tool-guards.js +77 -0
- package/lib/core/tool-search.d.ts +152 -0
- package/lib/core/tool-search.js +311 -0
- package/lib/core/tools/bash.d.ts +281 -0
- package/lib/core/tools/bash.js +871 -0
- package/lib/core/tools/command-runner.d.ts +84 -0
- package/lib/core/tools/command-runner.js +68 -0
- package/lib/core/tools/fs-tools.d.ts +23 -0
- package/lib/core/tools/fs-tools.js +247 -0
- package/lib/core/tools/index.d.ts +8 -0
- package/lib/core/tools/index.js +4 -0
- package/lib/core/tools/sandbox-args.d.ts +51 -0
- package/lib/core/tools/sandbox-args.js +90 -0
- package/lib/core/tools/search-tools.d.ts +3 -0
- package/lib/core/tools/search-tools.js +153 -0
- package/lib/core/tools.d.ts +96 -0
- package/lib/core/tools.js +53 -0
- package/lib/core/ui-boot-source.d.ts +71 -0
- package/lib/core/ui-boot-source.js +121 -0
- package/lib/core/ui-bundles.d.ts +249 -0
- package/lib/core/ui-bundles.js +830 -0
- package/lib/core/ui-combo.d.ts +49 -0
- package/lib/core/ui-combo.js +55 -0
- package/lib/core/ui-events.d.ts +81 -0
- package/lib/core/ui-events.js +364 -0
- package/lib/core/ui-hmr.d.ts +36 -0
- package/lib/core/ui-hmr.js +128 -0
- package/lib/core/ui-methods.d.ts +69 -0
- package/lib/core/ui-methods.js +341 -0
- package/lib/core/ui-route.d.ts +69 -0
- package/lib/core/ui-route.js +122 -0
- package/lib/core/ui-server.d.ts +75 -0
- package/lib/core/ui-server.js +369 -0
- package/lib/core/ui-trust.d.ts +47 -0
- package/lib/core/ui-trust.js +164 -0
- package/lib/core/untrusted.d.ts +30 -0
- package/lib/core/untrusted.js +48 -0
- package/lib/core/update-check.d.ts +81 -0
- package/lib/core/update-check.js +177 -0
- package/lib/core/usage-log.d.ts +82 -0
- package/lib/core/usage-log.js +306 -0
- package/lib/core/version.d.ts +1 -0
- package/lib/core/version.js +37 -0
- package/lib/core/web-http.d.ts +57 -0
- package/lib/core/web-http.js +137 -0
- package/lib/core/web-static.d.ts +31 -0
- package/lib/core/web-static.js +75 -0
- package/lib/headless.d.ts +1 -0
- package/lib/headless.js +31 -0
- package/lib/host/kernel.d.ts +33 -0
- package/lib/host/kernel.js +451 -0
- package/lib/index.d.ts +41 -0
- package/lib/index.js +35 -0
- package/lib/plugins/agent.d.ts +2 -0
- package/lib/plugins/agent.js +274 -0
- package/lib/plugins/approval-floor.d.ts +2 -0
- package/lib/plugins/approval-floor.js +223 -0
- package/lib/plugins/approval.d.ts +16 -0
- package/lib/plugins/approval.js +674 -0
- package/lib/plugins/ask.d.ts +2 -0
- package/lib/plugins/ask.js +245 -0
- package/lib/plugins/browser.d.ts +68 -0
- package/lib/plugins/browser.js +1341 -0
- package/lib/plugins/commands.d.ts +23 -0
- package/lib/plugins/commands.js +498 -0
- package/lib/plugins/compact.d.ts +21 -0
- package/lib/plugins/compact.js +274 -0
- package/lib/plugins/computer-use.d.ts +47 -0
- package/lib/plugins/computer-use.js +539 -0
- package/lib/plugins/desktop-dock.d.ts +2 -0
- package/lib/plugins/desktop-dock.js +626 -0
- package/lib/plugins/dock-browser.d.ts +34 -0
- package/lib/plugins/dock-browser.js +765 -0
- package/lib/plugins/dsh-compat.d.ts +25 -0
- package/lib/plugins/dsh-compat.js +37 -0
- package/lib/plugins/file-review.d.ts +108 -0
- package/lib/plugins/file-review.js +393 -0
- package/lib/plugins/git.d.ts +26 -0
- package/lib/plugins/git.js +132 -0
- package/lib/plugins/goal.d.ts +20 -0
- package/lib/plugins/goal.js +245 -0
- package/lib/plugins/guards.d.ts +14 -0
- package/lib/plugins/guards.js +18 -0
- package/lib/plugins/hooks.d.ts +19 -0
- package/lib/plugins/hooks.js +583 -0
- package/lib/plugins/host-stdio.d.ts +88 -0
- package/lib/plugins/host-stdio.js +317 -0
- package/lib/plugins/image-diet.d.ts +17 -0
- package/lib/plugins/image-diet.js +85 -0
- package/lib/plugins/lifecycle-hooks.d.ts +26 -0
- package/lib/plugins/lifecycle-hooks.js +338 -0
- package/lib/plugins/llm.d.ts +13 -0
- package/lib/plugins/llm.js +215 -0
- package/lib/plugins/lsp.d.ts +54 -0
- package/lib/plugins/lsp.js +608 -0
- package/lib/plugins/mcp.d.ts +16 -0
- package/lib/plugins/mcp.js +673 -0
- package/lib/plugins/memory.d.ts +17 -0
- package/lib/plugins/memory.js +858 -0
- package/lib/plugins/microcompact.d.ts +18 -0
- package/lib/plugins/microcompact.js +119 -0
- package/lib/plugins/mode.d.ts +18 -0
- package/lib/plugins/mode.js +115 -0
- package/lib/plugins/permission-presets.d.ts +18 -0
- package/lib/plugins/permission-presets.js +135 -0
- package/lib/plugins/plan.d.ts +2 -0
- package/lib/plugins/plan.js +192 -0
- package/lib/plugins/plugin-manager.d.ts +2 -0
- package/lib/plugins/plugin-manager.js +165 -0
- package/lib/plugins/presets.d.ts +2 -0
- package/lib/plugins/presets.js +182 -0
- package/lib/plugins/prompt.d.ts +2 -0
- package/lib/plugins/prompt.js +264 -0
- package/lib/plugins/ptc.d.ts +2 -0
- package/lib/plugins/ptc.js +272 -0
- package/lib/plugins/remote/http.d.ts +11 -0
- package/lib/plugins/remote/http.js +11 -0
- package/lib/plugins/remote/routes.d.ts +24 -0
- package/lib/plugins/remote/routes.js +384 -0
- package/lib/plugins/remote/types.d.ts +135 -0
- package/lib/plugins/remote/types.js +87 -0
- package/lib/plugins/remote.d.ts +17 -0
- package/lib/plugins/remote.js +1028 -0
- package/lib/plugins/runtime-api.d.ts +16 -0
- package/lib/plugins/runtime-api.js +99 -0
- package/lib/plugins/runtime.d.ts +10 -0
- package/lib/plugins/runtime.js +451 -0
- package/lib/plugins/sandbox.d.ts +2 -0
- package/lib/plugins/sandbox.js +761 -0
- package/lib/plugins/schedule.d.ts +26 -0
- package/lib/plugins/schedule.js +538 -0
- package/lib/plugins/self-improve.d.ts +61 -0
- package/lib/plugins/self-improve.js +953 -0
- package/lib/plugins/session-search.d.ts +16 -0
- package/lib/plugins/session-search.js +308 -0
- package/lib/plugins/session-title.d.ts +19 -0
- package/lib/plugins/session-title.js +109 -0
- package/lib/plugins/session.d.ts +7 -0
- package/lib/plugins/session.js +296 -0
- package/lib/plugins/settings.d.ts +3 -0
- package/lib/plugins/settings.js +705 -0
- package/lib/plugins/skills.d.ts +2 -0
- package/lib/plugins/skills.js +360 -0
- package/lib/plugins/spill.d.ts +16 -0
- package/lib/plugins/spill.js +200 -0
- package/lib/plugins/subagent.d.ts +2 -0
- package/lib/plugins/subagent.js +896 -0
- package/lib/plugins/surfaces.d.ts +11 -0
- package/lib/plugins/surfaces.js +20 -0
- package/lib/plugins/team.d.ts +16 -0
- package/lib/plugins/team.js +149 -0
- package/lib/plugins/todo.d.ts +19 -0
- package/lib/plugins/todo.js +188 -0
- package/lib/plugins/tool-search.d.ts +2 -0
- package/lib/plugins/tool-search.js +434 -0
- package/lib/plugins/tool-search.js.nv-backup +434 -0
- package/lib/plugins/tools-default.d.ts +12 -0
- package/lib/plugins/tools-default.js +275 -0
- package/lib/plugins/tools.d.ts +16 -0
- package/lib/plugins/tools.js +62 -0
- package/lib/plugins/transcript.d.ts +15 -0
- package/lib/plugins/transcript.js +311 -0
- package/lib/plugins/tui.d.ts +2 -0
- package/lib/plugins/tui.js +98 -0
- package/lib/plugins/waiting.d.ts +12 -0
- package/lib/plugins/waiting.js +34 -0
- package/lib/plugins/web-search.d.ts +14 -0
- package/lib/plugins/web-search.js +263 -0
- package/lib/plugins/webserver.d.ts +48 -0
- package/lib/plugins/webserver.js +228 -0
- package/lib/remote/assets/app-DAcUCOEn.js +14 -0
- package/lib/remote/assets/icon-192.png +0 -0
- package/lib/remote/assets/icon-512.png +0 -0
- package/lib/remote/assets/index.html +34 -0
- package/lib/remote/assets/manifest.json +27 -0
- package/lib/remote/assets/style-DIB6729x.css +1 -0
- package/lib/remote/assets/sw.js +66 -0
- package/lib/services/types.d.ts +1228 -0
- package/lib/services/types.js +1 -0
- package/lib/tools/config-cli.d.ts +1 -0
- package/lib/tools/config-cli.js +89 -0
- package/lib/ui/arrival.d.ts +72 -0
- package/lib/ui/arrival.js +105 -0
- package/lib/ui/assets/app-B70nVIKd.js +69 -0
- package/lib/ui/assets/chunk-2UxHyX5q.js +1 -0
- package/lib/ui/assets/chunk-B1dDrJ26.js +1 -0
- package/lib/ui/assets/chunk-B6aJPvgy.js +1 -0
- package/lib/ui/assets/chunk-BEwlwnbL.js +1 -0
- package/lib/ui/assets/chunk-BGcgwlCI.js +12 -0
- package/lib/ui/assets/chunk-BIGW1oBm.js +1 -0
- package/lib/ui/assets/chunk-BPQ3VLAy.js +1 -0
- package/lib/ui/assets/chunk-BRH0KZbk.js +141 -0
- package/lib/ui/assets/chunk-BX6H_ySx.js +1 -0
- package/lib/ui/assets/chunk-B_P3UbPa.js +1 -0
- package/lib/ui/assets/chunk-B_TYUkTI.js +1 -0
- package/lib/ui/assets/chunk-B_vNuMnf.js +1 -0
- package/lib/ui/assets/chunk-BajyYBss.js +10 -0
- package/lib/ui/assets/chunk-BdnUsdx6.js +1 -0
- package/lib/ui/assets/chunk-Buea-lGh.js +1 -0
- package/lib/ui/assets/chunk-C0PvZCCE.js +1 -0
- package/lib/ui/assets/chunk-C0m74zRD.js +1 -0
- package/lib/ui/assets/chunk-C27-OAKa.js +1 -0
- package/lib/ui/assets/chunk-C2oV4EkX.js +1 -0
- package/lib/ui/assets/chunk-C7zT0LnQ.js +1 -0
- package/lib/ui/assets/chunk-C9GSunVT.js +1 -0
- package/lib/ui/assets/chunk-C9tS-k6U.js +1 -0
- package/lib/ui/assets/chunk-CHLpvVh8.js +1 -0
- package/lib/ui/assets/chunk-CLxacb5B.js +1 -0
- package/lib/ui/assets/chunk-COt5Ahok.js +1 -0
- package/lib/ui/assets/chunk-CPTIWCL2.js +1 -0
- package/lib/ui/assets/chunk-CQLl-L-p.js +1 -0
- package/lib/ui/assets/chunk-CUWRgxuq.js +55 -0
- package/lib/ui/assets/chunk-CWanVSXC.js +1 -0
- package/lib/ui/assets/chunk-Cf5RLm7j.js +1 -0
- package/lib/ui/assets/chunk-CickPsom.js +1 -0
- package/lib/ui/assets/chunk-Cj5Yp3dK.js +1 -0
- package/lib/ui/assets/chunk-Cmh6b_Ma.js +1 -0
- package/lib/ui/assets/chunk-Cp-IABpG.js +1 -0
- package/lib/ui/assets/chunk-CqE71os6.js +1 -0
- package/lib/ui/assets/chunk-CtKP3W-j.js +1 -0
- package/lib/ui/assets/chunk-Cvjx9yec.js +1 -0
- package/lib/ui/assets/chunk-CxGP3dtY.js +1 -0
- package/lib/ui/assets/chunk-CylS5w8V.js +1 -0
- package/lib/ui/assets/chunk-D38fbIBN.js +1 -0
- package/lib/ui/assets/chunk-D97Zzqfu.js +1 -0
- package/lib/ui/assets/chunk-DAi9KRSo.js +1 -0
- package/lib/ui/assets/chunk-DHK4yRfg.js +1 -0
- package/lib/ui/assets/chunk-DJeabLx-.js +1 -0
- package/lib/ui/assets/chunk-DNRxmY5a.js +1 -0
- package/lib/ui/assets/chunk-DNSN1z5r.js +1 -0
- package/lib/ui/assets/chunk-DNasK_ZG.js +1 -0
- package/lib/ui/assets/chunk-DPvrY3IQ.js +1 -0
- package/lib/ui/assets/chunk-DXmwc3jG.js +1 -0
- package/lib/ui/assets/chunk-Dd0kTxKP.js +1 -0
- package/lib/ui/assets/chunk-De0Btta0.js +1 -0
- package/lib/ui/assets/chunk-Des-eS-w.js +1 -0
- package/lib/ui/assets/chunk-Df6bDoY_.js +1 -0
- package/lib/ui/assets/chunk-DijEV5ha.js +1 -0
- package/lib/ui/assets/chunk-DkvoVjww.js +1 -0
- package/lib/ui/assets/chunk-DlC-OlHO.js +1 -0
- package/lib/ui/assets/chunk-DnzrXTVv.js +1 -0
- package/lib/ui/assets/chunk-Dualf0xi.js +1 -0
- package/lib/ui/assets/chunk-EXT84bkm.js +1 -0
- package/lib/ui/assets/chunk-GwR3hkPK.js +1 -0
- package/lib/ui/assets/chunk-P80f7IUj.js +1 -0
- package/lib/ui/assets/chunk-VOosw3JB.js +1 -0
- package/lib/ui/assets/chunk-YQ4lDkt_.js +140 -0
- package/lib/ui/assets/chunk-Yzrsuije.js +1 -0
- package/lib/ui/assets/chunk-Z72CF80r.js +1 -0
- package/lib/ui/assets/chunk-g9-lgVsj.js +1 -0
- package/lib/ui/assets/chunk-vGWfd6FD.js +1 -0
- package/lib/ui/assets/chunk-wDzz0qaB.js +1 -0
- package/lib/ui/assets/chunk-ySF7HWmS.js +1 -0
- package/lib/ui/assets/icon.png +0 -0
- package/lib/ui/assets/index.html +14 -0
- package/lib/ui/assets/pdf.worker.min-CjEcRF4W.mjs +28 -0
- package/lib/ui/assets/style-Bq01xH_b.css +1 -0
- package/lib/ui/frames.d.ts +67 -0
- package/lib/ui/frames.js +146 -0
- package/lib/ui/index.d.ts +59 -0
- package/lib/ui/index.js +50 -0
- package/lib/ui/manifest.d.ts +172 -0
- package/lib/ui/manifest.js +158 -0
- package/lib/ui/plugin.d.ts +161 -0
- package/lib/ui/plugin.js +7 -0
- package/lib/ui/plugins/browser/client.js +290 -0
- package/lib/ui/plugins/browser/icon.png +0 -0
- package/lib/ui/plugins/composer/client.js +1843 -0
- package/lib/ui/plugins/composer/icon.png +0 -0
- package/lib/ui/plugins/files/client.js +646 -0
- package/lib/ui/plugins/files/icon.png +0 -0
- package/lib/ui/plugins/manifest.json +107 -0
- package/lib/ui/plugins/plugins/client.js +682 -0
- package/lib/ui/plugins/plugins/icon.png +0 -0
- package/lib/ui/plugins/preview/client.js +197 -0
- package/lib/ui/plugins/preview/icon.png +0 -0
- package/lib/ui/plugins/prompt/client.js +653 -0
- package/lib/ui/plugins/prompt/icon.png +0 -0
- package/lib/ui/plugins/sessions/client.js +1577 -0
- package/lib/ui/plugins/sessions/icon.png +0 -0
- package/lib/ui/plugins/settings/client.js +5470 -0
- package/lib/ui/plugins/settings/icon.png +0 -0
- package/lib/ui/plugins/skills/client.js +485 -0
- package/lib/ui/plugins/skills/icon.png +0 -0
- package/lib/ui/plugins/status/client.js +1420 -0
- package/lib/ui/plugins/status/icon.png +0 -0
- package/lib/ui/plugins/statusbar/client.js +504 -0
- package/lib/ui/plugins/statusbar/icon.png +0 -0
- package/lib/ui/plugins/tasks/client.js +52 -0
- package/lib/ui/plugins/tasks/icon.png +0 -0
- package/lib/ui/plugins/team/client.js +527 -0
- package/lib/ui/plugins/team/icon.png +0 -0
- package/lib/ui/plugins/terminal/client.js +232 -0
- package/lib/ui/plugins/terminal/icon.png +0 -0
- package/lib/ui/plugins/trace/client.js +1376 -0
- package/lib/ui/plugins/trace/icon.png +0 -0
- package/lib/ui/plugins/transcript/client.js +2661 -0
- package/lib/ui/plugins/transcript/icon.png +0 -0
- package/lib/ui/scoped-slots.d.ts +20 -0
- package/lib/ui/scoped-slots.js +21 -0
- package/lib/ui/sdk/@dsc/ui/index.d.ts +242 -0
- package/lib/ui/sdk/@dsc/ui/package.json +16 -0
- package/lib/ui/sdk/@dsc/ui-core/contract.d.ts +1973 -0
- package/lib/ui/sdk/@dsc/ui-core/core/diff-text.d.ts +74 -0
- package/lib/ui/sdk/@dsc/ui-core/core/dock-browser.d.ts +137 -0
- package/lib/ui/sdk/@dsc/ui-core/core/dock-ops.d.ts +59 -0
- package/lib/ui/sdk/@dsc/ui-core/core/model-caps.d.ts +83 -0
- package/lib/ui/sdk/@dsc/ui-core/core/native-bridge.d.ts +135 -0
- package/lib/ui/sdk/@dsc/ui-core/core/token-estimate.d.ts +55 -0
- package/lib/ui/sdk/@dsc/ui-core/package.json +16 -0
- package/lib/ui/sdk/@dsc/ui-core/ui/arrival.d.ts +72 -0
- package/lib/ui/sdk/@dsc/ui-core/ui/frames.d.ts +67 -0
- package/lib/ui/sdk/@dsc/ui-core/ui/index.d.ts +59 -0
- package/lib/ui/sdk/@dsc/ui-core/ui/manifest.d.ts +172 -0
- package/lib/ui/sdk/@dsc/ui-core/ui/plugin.d.ts +161 -0
- package/lib/ui/sdk/@dsc/ui-core/ui/scoped-slots.d.ts +20 -0
- package/lib/ui/sdk/@dsc/ui-core/ui/services-react.d.ts +27 -0
- package/lib/ui/sdk/@dsc/ui-core/ui/services.d.ts +63 -0
- package/lib/ui/sdk/@dsc/ui-core/ui/slots-react.d.ts +28 -0
- package/lib/ui/sdk/@dsc/ui-core/ui/slots.d.ts +481 -0
- package/lib/ui/sdk/@dsc/ui-core/ui/store.d.ts +89 -0
- package/lib/ui/sdk/@dsc/ui-dockkit/dock-model.d.ts +238 -0
- package/lib/ui/sdk/@dsc/ui-dockkit/dock-state.d.ts +163 -0
- package/lib/ui/sdk/@dsc/ui-dockkit/dock.d.ts +24 -0
- package/lib/ui/sdk/@dsc/ui-dockkit/index.d.ts +15 -0
- package/lib/ui/sdk/@dsc/ui-dockkit/package.json +16 -0
- package/lib/ui/sdk/@dsc/ui-layout/app.d.ts +74 -0
- package/lib/ui/sdk/@dsc/ui-layout/appearance.d.ts +70 -0
- package/lib/ui/sdk/@dsc/ui-layout/host-channel.d.ts +112 -0
- package/lib/ui/sdk/@dsc/ui-layout/index.d.ts +34 -0
- package/lib/ui/sdk/@dsc/ui-layout/package.json +16 -0
- package/lib/ui/sdk/@dsc/ui-layout/page-state.d.ts +14 -0
- package/lib/ui/sdk/@dsc/ui-layout/review-pane.d.ts +21 -0
- package/lib/ui/sdk/@dsc/ui-layout/runtime-context.d.ts +44 -0
- package/lib/ui/sdk/@dsc/ui-layout/thread-view.d.ts +28 -0
- package/lib/ui/sdk/@dsc/ui-layout/update-button.d.ts +21 -0
- package/lib/ui/sdk/@dsc/ui-primitives/code-file-icon-artwork.d.ts +53 -0
- package/lib/ui/sdk/@dsc/ui-primitives/code-view.d.ts +15 -0
- package/lib/ui/sdk/@dsc/ui-primitives/compat.d.ts +27 -0
- package/lib/ui/sdk/@dsc/ui-primitives/completion-sound.d.ts +24 -0
- package/lib/ui/sdk/@dsc/ui-primitives/confirm.d.ts +33 -0
- package/lib/ui/sdk/@dsc/ui-primitives/css.d.ts +47 -0
- package/lib/ui/sdk/@dsc/ui-primitives/diff-rows.d.ts +20 -0
- package/lib/ui/sdk/@dsc/ui-primitives/file-icons.d.ts +38 -0
- package/lib/ui/sdk/@dsc/ui-primitives/file-type-classify.d.ts +66 -0
- package/lib/ui/sdk/@dsc/ui-primitives/file-type-icon.d.ts +57 -0
- package/lib/ui/sdk/@dsc/ui-primitives/highlight.d.ts +24 -0
- package/lib/ui/sdk/@dsc/ui-primitives/hover-delay.d.ts +17 -0
- package/lib/ui/sdk/@dsc/ui-primitives/icons.d.ts +109 -0
- package/lib/ui/sdk/@dsc/ui-primitives/index.d.ts +55 -0
- package/lib/ui/sdk/@dsc/ui-primitives/lang-table.d.ts +10 -0
- package/lib/ui/sdk/@dsc/ui-primitives/markdown.d.ts +20 -0
- package/lib/ui/sdk/@dsc/ui-primitives/native.d.ts +41 -0
- package/lib/ui/sdk/@dsc/ui-primitives/package.json +16 -0
- package/lib/ui/sdk/@dsc/ui-primitives/panels.d.ts +86 -0
- package/lib/ui/sdk/@dsc/ui-primitives/paths.d.ts +11 -0
- package/lib/ui/sdk/@dsc/ui-primitives/pdf-view.d.ts +13 -0
- package/lib/ui/sdk/@dsc/ui-primitives/plugin-text.d.ts +15 -0
- package/lib/ui/sdk/@dsc/ui-primitives/select.d.ts +35 -0
- package/lib/ui/sdk/@dsc/ui-primitives/table-view.d.ts +23 -0
- package/lib/ui/sdk/@dsc/ui-primitives/thread-resizer.d.ts +22 -0
- package/lib/ui/sdk/@dsc/ui-primitives/time.d.ts +33 -0
- package/lib/ui/sdk/@dsc/ui-primitives/toast.d.ts +16 -0
- package/lib/ui/sdk/@dsc/ui-primitives/tokens.d.ts +14 -0
- package/lib/ui/sdk/@dsc/ui-primitives/tool-args.d.ts +49 -0
- package/lib/ui/sdk/@dsc/ui-primitives/tool-summary.d.ts +64 -0
- package/lib/ui/sdk/@dsc/ui-renderer/boot.d.ts +121 -0
- package/lib/ui/sdk/@dsc/ui-renderer/hmr.d.ts +43 -0
- package/lib/ui/sdk/@dsc/ui-renderer/index.d.ts +20 -0
- package/lib/ui/sdk/@dsc/ui-renderer/package.json +16 -0
- package/lib/ui/sdk/@dsc/ui-slots/index.d.ts +19 -0
- package/lib/ui/sdk/@dsc/ui-slots/package.json +16 -0
- package/lib/ui/sdk/@dsc/ui-slots/slots-react.d.ts +48 -0
- package/lib/ui/sdk/@dsc/ui-slots/slots.d.ts +102 -0
- package/lib/ui/services-react.d.ts +27 -0
- package/lib/ui/services-react.js +32 -0
- package/lib/ui/services.d.ts +63 -0
- package/lib/ui/services.js +121 -0
- package/lib/ui/slots-ink.d.ts +26 -0
- package/lib/ui/slots-ink.js +78 -0
- package/lib/ui/slots-react.d.ts +28 -0
- package/lib/ui/slots-react.js +88 -0
- package/lib/ui/slots.d.ts +481 -0
- package/lib/ui/slots.js +550 -0
- package/lib/ui/store.d.ts +89 -0
- package/lib/ui/store.js +62 -0
- package/lib/ui/tui-plugin-api.d.ts +23 -0
- package/lib/ui/tui-plugin-api.js +23 -0
- package/lib/ui/tui-plugins.d.ts +47 -0
- package/lib/ui/tui-plugins.js +220 -0
- package/package.json +58 -6
|
@@ -0,0 +1,986 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 二级网络管控的**一次性提权 setup**:生成 PowerShell 5.1 脚本(内嵌 C# 做 WFP 安装)+ 提权执行。
|
|
3
|
+
*
|
|
4
|
+
* 分工:TS 这边只负责「把要做的事写成一个可审计的 .ps1」(`buildSetupScript` 是纯函数,不碰盘),
|
|
5
|
+
* 真正的提权动作全在那个脚本里(建账号、布防火墙、装 WFP filter)。`runSetupElevated` 负责落盘 + 拉起 UAC。
|
|
6
|
+
*
|
|
7
|
+
* 顺序即安全纪律(中断也不留「有账号没布防」的窗口):
|
|
8
|
+
* 建组 → 生成密码 → **-Disabled 建账号** → 进组 → 隐藏账号 → DPAPI 存密码 →
|
|
9
|
+
* 5 条防火墙规则 → 12 条 WFP filter(单事务持久)→ 写 setup-state.json → **最后才 Enable-LocalUser**。
|
|
10
|
+
* 任何一步抛错都 exit 1、账号停在禁用态;重跑脚本幂等。
|
|
11
|
+
*
|
|
12
|
+
* 与 codex 的两处刻意不同:
|
|
13
|
+
* 1. 环回 TCP 的端口补集一步到位(不做「先全堵再收窄」两段式)——因为本脚本全程把账号钉在禁用态,
|
|
14
|
+
* 不存在 codex 那种「账号已启用、规则还在装」的敞口;
|
|
15
|
+
* 2. 账本(setup-state.json)写在 Enable-LocalUser **之前**:这份账本也是「布防」的一部分,
|
|
16
|
+
* 这样「账号已启用」就一定意味着「账本已在」,doctor 不会读到半截状态。
|
|
17
|
+
*
|
|
18
|
+
* @module dsc/core/sandbox/win/setup
|
|
19
|
+
*/
|
|
20
|
+
import { spawn } from 'node:child_process';
|
|
21
|
+
import { mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
22
|
+
import { join } from 'node:path';
|
|
23
|
+
import { DSC_WFP_FILTERS, DSC_WFP_PROVIDER_GUID, DSC_WFP_SUBLAYER_GUID, firewallRuleSpecs, } from './net-abi.js';
|
|
24
|
+
import { sandboxDir, setupErrorPath, setupScriptPath, readSetupState } from './account.js';
|
|
25
|
+
/** 固定的离线沙箱账号名(普通本地账号,不是服务账号)。 */
|
|
26
|
+
export const DSC_SANDBOX_ACCOUNT = 'dsc-sandbox-offline';
|
|
27
|
+
/** 沙箱账号组的名字(便于以后整体清理/识别)。 */
|
|
28
|
+
export const DSC_SANDBOX_GROUP = 'DscSandboxUsers';
|
|
29
|
+
/** 默认代理端口:沙箱里唯一被放行的环回 TCP 出口。 */
|
|
30
|
+
export const DSC_SANDBOX_DEFAULT_PROXY_PORT = 3128;
|
|
31
|
+
/** 账本版本号(改了脚本语义就加一,doctor 可以据此判新旧)。 */
|
|
32
|
+
export const DSC_SETUP_STATE_VERSION = 1;
|
|
33
|
+
/**
|
|
34
|
+
* 生成完整的提权脚本(纯函数:同样的入参给同样的文本)。
|
|
35
|
+
*
|
|
36
|
+
* 输入有一处会被写进 PowerShell 单引号字符串(dscHome 与几个名字),所以这里先校一遍形状,
|
|
37
|
+
* 不合法就直接抛——把「路径里带单引号导致脚本被改写」这种事挡在生成阶段。
|
|
38
|
+
*/
|
|
39
|
+
export function buildSetupScript(options) {
|
|
40
|
+
const dscHome = options.dscHome;
|
|
41
|
+
if (typeof dscHome !== 'string' || dscHome === '') {
|
|
42
|
+
throw new Error('buildSetupScript:dscHome 必须是非空字符串');
|
|
43
|
+
}
|
|
44
|
+
if (dscHome.includes("'") || /[\r\n]/.test(dscHome)) {
|
|
45
|
+
throw new Error(`buildSetupScript:dscHome 不能含单引号或换行(会破坏生成的 PowerShell 文本):${JSON.stringify(dscHome)}`);
|
|
46
|
+
}
|
|
47
|
+
const proxyPort = options.proxyPort ?? DSC_SANDBOX_DEFAULT_PROXY_PORT;
|
|
48
|
+
const guidProvider = options.guidProvider ?? DSC_WFP_PROVIDER_GUID;
|
|
49
|
+
const guidSublayer = options.guidSublayer ?? DSC_WFP_SUBLAYER_GUID;
|
|
50
|
+
assertGuid(guidProvider, 'guidProvider');
|
|
51
|
+
assertGuid(guidSublayer, 'guidSublayer');
|
|
52
|
+
const rules = firewallRuleSpecs(proxyPort); // 顺带校验 proxyPort 合法(不合法会抛 RangeError)
|
|
53
|
+
const ruleNameList = rules.map((rule) => `'${rule.name}'`).join(', ');
|
|
54
|
+
const firewallBlock = rules
|
|
55
|
+
.map((rule) => [` Remove-NetFirewallRule -Name '${rule.name}' -ErrorAction SilentlyContinue`, ` ${renderFirewallRule(rule)}`].join('\n'))
|
|
56
|
+
.join('\n');
|
|
57
|
+
const filterBlock = DSC_WFP_FILTERS.map((spec) => ` '${spec.key}|${spec.name}|${spec.layer}|${spec.protocol === null ? '' : String(spec.protocol)}|${spec.remotePort === null ? '' : String(spec.remotePort)}'`).join('\n');
|
|
58
|
+
return `# dsc 沙箱「网络管控第二级」一次性提权 setup。
|
|
59
|
+
# 由 src/core/sandbox/win/setup.ts 的 buildSetupScript() 生成 —— 要看改了什么就看那个函数,别手改本文件。
|
|
60
|
+
#
|
|
61
|
+
# 纪律:账号先以 -Disabled 建出来 → 防火墙 5 条 + WFP 12 条全部布防成功 → 写账本 → 最后才 Enable-LocalUser。
|
|
62
|
+
# 任何一步失败都会写 ~/.dsc/sandbox/setup-error.txt 并 exit 1,账号停在禁用态;重跑本脚本即幂等重做。
|
|
63
|
+
\$ErrorActionPreference = 'Stop'
|
|
64
|
+
\$DscHome = '${dscHome}'
|
|
65
|
+
\$DscSandboxDir = Join-Path \$DscHome 'sandbox'
|
|
66
|
+
\$DscAccount = '${DSC_SANDBOX_ACCOUNT}'
|
|
67
|
+
\$DscGroup = '${DSC_SANDBOX_GROUP}'
|
|
68
|
+
\$DscProxyPort = ${String(proxyPort)}
|
|
69
|
+
\$DscAccountDescription = 'dsc 沙箱离线账号(网络管控第二级用,非交互登录)'
|
|
70
|
+
\$DscProviderGuid = '${guidProvider}'
|
|
71
|
+
\$DscSublayerGuid = '${guidSublayer}'
|
|
72
|
+
\$DscStatePath = Join-Path \$DscSandboxDir 'setup-state.json'
|
|
73
|
+
\$DscSecretPath = Join-Path \$DscSandboxDir 'account.bin'
|
|
74
|
+
\$DscErrorPath = Join-Path \$DscSandboxDir 'setup-error.txt'
|
|
75
|
+
\$DscRuleNames = @(${ruleNameList})
|
|
76
|
+
\$DscFilters = @(
|
|
77
|
+
${filterBlock}
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
function Write-DscSetupError([string]\$Message) {
|
|
81
|
+
# 失败原因单独落一个文件:提权窗口一闪而过,控制台输出经常来不及看
|
|
82
|
+
try {
|
|
83
|
+
New-Item -ItemType Directory -Force -Path \$DscSandboxDir | Out-Null
|
|
84
|
+
\$text = ('[{0}] {1}' -f (Get-Date).ToUniversalTime().ToString('o'), \$Message)
|
|
85
|
+
[System.IO.File]::WriteAllText(\$DscErrorPath, \$text, (New-Object System.Text.UTF8Encoding(\$false)))
|
|
86
|
+
} catch {
|
|
87
|
+
# 连错误文件都写不进去就只能靠控制台了
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function Get-DscRandomIndex([int]\$Max) {
|
|
92
|
+
# 用密码学 RNG,不用 Get-Random(后者是给测试用的可预测源)
|
|
93
|
+
\$bytes = New-Object byte[] 4
|
|
94
|
+
\$DscRng.GetBytes(\$bytes)
|
|
95
|
+
return [int]([System.BitConverter]::ToUInt32(\$bytes, 0) % [uint32]\$Max)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function Get-DscRandomChar([char[]]\$Set) {
|
|
99
|
+
return \$Set[(Get-DscRandomIndex \$Set.Length)]
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
try {
|
|
103
|
+
New-Item -ItemType Directory -Force -Path \$DscSandboxDir | Out-Null
|
|
104
|
+
|
|
105
|
+
# ── 1. 组:存在就跳过 ──
|
|
106
|
+
if (\$null -eq (Get-LocalGroup -Name \$DscGroup -ErrorAction SilentlyContinue)) {
|
|
107
|
+
New-LocalGroup -Name \$DscGroup -Description 'dsc 沙箱账号组(二级网络管控用)' | Out-Null
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
# ── 2. 随机 24 位密码:四类字符各至少 2 个,再洗牌 ──
|
|
111
|
+
# 只用 RandomNumberGenerator + 自己拼的字符集,不依赖 .NET Framework 里那套已废弃的成员解析类
|
|
112
|
+
\$DscRng = [System.Security.Cryptography.RandomNumberGenerator]::Create()
|
|
113
|
+
\$DscUpper = 'ABCDEFGHJKLMNPQRSTUVWXYZ'.ToCharArray()
|
|
114
|
+
\$DscLower = 'abcdefghijkmnopqrstuvwxyz'.ToCharArray()
|
|
115
|
+
\$DscDigit = '23456789'.ToCharArray()
|
|
116
|
+
\$DscSymbol = '!@#%^&*()-_=+'.ToCharArray()
|
|
117
|
+
\$DscAll = \$DscUpper + \$DscLower + \$DscDigit + \$DscSymbol
|
|
118
|
+
\$DscChars = New-Object System.Collections.Generic.List[char]
|
|
119
|
+
foreach (\$set in @(\$DscUpper, \$DscLower, \$DscDigit, \$DscSymbol)) {
|
|
120
|
+
1..2 | ForEach-Object { \$DscChars.Add((Get-DscRandomChar \$set)) }
|
|
121
|
+
}
|
|
122
|
+
1..16 | ForEach-Object { \$DscChars.Add((Get-DscRandomChar \$DscAll)) }
|
|
123
|
+
for (\$i = \$DscChars.Count - 1; \$i -gt 0; \$i--) {
|
|
124
|
+
\$j = Get-DscRandomIndex (\$i + 1)
|
|
125
|
+
\$tmp = \$DscChars[\$i]; \$DscChars[\$i] = \$DscChars[\$j]; \$DscChars[\$j] = \$tmp
|
|
126
|
+
}
|
|
127
|
+
\$DscPassword = -join \$DscChars
|
|
128
|
+
\$DscSecure = ConvertTo-SecureString -String \$DscPassword -AsPlainText -Force
|
|
129
|
+
|
|
130
|
+
# ── 3. 账号:新建立刻是禁用态;已存在则重置密码并强制回禁用(中断后重跑的关键)──
|
|
131
|
+
\$DscExisting = Get-LocalUser -Name \$DscAccount -ErrorAction SilentlyContinue
|
|
132
|
+
if (\$null -eq \$DscExisting) {
|
|
133
|
+
New-LocalUser -Name \$DscAccount -Password \$DscSecure -Description \$DscAccountDescription -PasswordNeverExpires -UserMayNotChangePassword -Disabled | Out-Null
|
|
134
|
+
} else {
|
|
135
|
+
Set-LocalUser -Name \$DscAccount -Password \$DscSecure -PasswordNeverExpires \$true
|
|
136
|
+
Disable-LocalUser -Name \$DscAccount
|
|
137
|
+
}
|
|
138
|
+
\$DscSid = (Get-LocalUser -Name \$DscAccount).Sid.Value
|
|
139
|
+
if (\$DscSid -notlike 'S-1-5-21-*') {
|
|
140
|
+
throw "沙箱账号 SID 形状不对(本地账号应以 S-1-5-21- 开头):\$DscSid"
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
# ── 4. 进组:Users(按 SID 找,免得撞上本地化组名)+ dsc 自己的组;按 SID 比对成员,已在组里就跳过 ──
|
|
144
|
+
\$DscGroupTargets = @(\$DscGroup)
|
|
145
|
+
\$DscUsersGroup = Get-LocalGroup -SID 'S-1-5-32-545' -ErrorAction SilentlyContinue
|
|
146
|
+
if (\$null -ne \$DscUsersGroup) { \$DscGroupTargets += \$DscUsersGroup.Name }
|
|
147
|
+
foreach (\$g in \$DscGroupTargets) {
|
|
148
|
+
\$DscMemberSids = @(Get-LocalGroupMember -Group \$g -ErrorAction SilentlyContinue | ForEach-Object { \$_.SID.Value })
|
|
149
|
+
if (\$DscMemberSids -notcontains \$DscSid) {
|
|
150
|
+
Add-LocalGroupMember -Group \$g -Member \$DscAccount | Out-Null
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
# ── 5. 从登录界面藏起来(SpecialAccounts\\UserList;只影响登录界面列表,不禁用账号)──
|
|
155
|
+
\$DscWinlogonKey = 'HKLM:\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\SpecialAccounts\\UserList'
|
|
156
|
+
if (-not (Test-Path \$DscWinlogonKey)) { New-Item -Path \$DscWinlogonKey -Force | Out-Null }
|
|
157
|
+
New-ItemProperty -Path \$DscWinlogonKey -Name \$DscAccount -PropertyType DWord -Value 0 -Force | Out-Null
|
|
158
|
+
|
|
159
|
+
# ── 6. 密码用 DPAPI(CurrentUser)加密后落盘:提权只是提升完整性,还是同一个用户的 DPAPI 密钥,
|
|
160
|
+
# 所以之后免提权也能解开 ──
|
|
161
|
+
try {
|
|
162
|
+
Add-Type -AssemblyName System.Security -ErrorAction Stop
|
|
163
|
+
} catch {
|
|
164
|
+
Add-Type -AssemblyName System.Security.Cryptography.ProtectedData -ErrorAction Stop
|
|
165
|
+
}
|
|
166
|
+
\$DscPlain = [System.Text.Encoding]::UTF8.GetBytes(\$DscPassword)
|
|
167
|
+
\$DscBlob = [System.Security.Cryptography.ProtectedData]::Protect(\$DscPlain, \$null, [System.Security.Cryptography.DataProtectionScope]::CurrentUser)
|
|
168
|
+
[System.IO.File]::WriteAllBytes(\$DscSecretPath, \$DscBlob)
|
|
169
|
+
\$DscPassword = \$null
|
|
170
|
+
|
|
171
|
+
# ── 7. 防火墙 5 条:全部按账号 SID 限定作用域(LocalUser 的 SDDL 掩码 CC 就是 0x1 = FWP_ACTRL_MATCH_FILTER 同款语义)──
|
|
172
|
+
# 代理端口的放行靠「block 规则的端口补集」,不靠 allow 规则——Windows 防火墙里显式 block 优先于 allow。
|
|
173
|
+
${firewallBlock}
|
|
174
|
+
\$DscRuleCount = @(Get-NetFirewallRule -Name \$DscRuleNames -ErrorAction SilentlyContinue).Count
|
|
175
|
+
if (\$DscRuleCount -ne \$DscRuleNames.Count) {
|
|
176
|
+
throw "防火墙规则只装上 \$DscRuleCount / \$(\$DscRuleNames.Count) 条"
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
# ── 8. WFP:持久 provider + sublayer + 12 条 BLOCK filter,单事务提交(内嵌 C#,见下面的 P/Invoke)──
|
|
180
|
+
\$DscWfpSource = @'
|
|
181
|
+
${WFP_CSHARP_SOURCE}
|
|
182
|
+
'@
|
|
183
|
+
Add-Type -TypeDefinition \$DscWfpSource -Language CSharp
|
|
184
|
+
\$DscLayout = [DscWfp]::SelfTest()
|
|
185
|
+
if (-not \$DscLayout.StartsWith('OK')) { throw "WFP 结构体布局自检没通过:\$DscLayout" }
|
|
186
|
+
\$DscInstalled = [DscWfp]::Install(\$DscAccount, \$DscSid, \$DscProviderGuid, \$DscSublayerGuid, \$DscFilters)
|
|
187
|
+
if (\$DscInstalled -ne \$DscFilters.Count) {
|
|
188
|
+
throw "WFP filter 只装上 \$DscInstalled / \$(\$DscFilters.Count) 条"
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
# ── 9. 账本:写在这里而不是最后,是为了让「账号已启用」必然蕴含「账本已在」──
|
|
192
|
+
\$DscState = [ordered]@{
|
|
193
|
+
version = ${String(DSC_SETUP_STATE_VERSION)}
|
|
194
|
+
account = \$DscAccount
|
|
195
|
+
sid = \$DscSid
|
|
196
|
+
proxyPort = \$DscProxyPort
|
|
197
|
+
firewallRules = \$DscRuleNames
|
|
198
|
+
wfpProviderGuid = \$DscProviderGuid
|
|
199
|
+
wfpSublayerGuid = \$DscSublayerGuid
|
|
200
|
+
wfpFilterCount = \$DscInstalled
|
|
201
|
+
at = (Get-Date).ToUniversalTime().ToString('o')
|
|
202
|
+
}
|
|
203
|
+
\$DscJson = \$DscState | ConvertTo-Json -Depth 5
|
|
204
|
+
\$DscStateTmp = (\$DscStatePath + '.tmp')
|
|
205
|
+
# 不带 BOM 的 UTF-8:PowerShell 5.1 的 -Encoding UTF8 会写 BOM,node 侧 JSON.parse 会直接报错
|
|
206
|
+
[System.IO.File]::WriteAllText(\$DscStateTmp, \$DscJson, (New-Object System.Text.UTF8Encoding(\$false)))
|
|
207
|
+
Move-Item -Path \$DscStateTmp -Destination \$DscStatePath -Force
|
|
208
|
+
|
|
209
|
+
# ── 10. 最后一步:解禁。走到这里说明布防全部成功了 ──
|
|
210
|
+
Enable-LocalUser -Name \$DscAccount
|
|
211
|
+
|
|
212
|
+
Write-Output ('dsc 沙箱二级网络管控布防完成:账号 {0}({1}),防火墙 {2} 条,WFP filter {3} 条,代理端口 {4},账本 {5}' -f \$DscAccount, \$DscSid, \$DscRuleCount, \$DscInstalled, \$DscProxyPort, \$DscStatePath)
|
|
213
|
+
exit 0
|
|
214
|
+
} catch {
|
|
215
|
+
\$message = ('setup 失败:{0} @ {1}' -f \$_.Exception.Message, \$_.InvocationInfo.PositionMessage)
|
|
216
|
+
Write-DscSetupError \$message
|
|
217
|
+
# 用 [Console]::Error 而不是 Write-Error:$ErrorActionPreference='Stop' 下 Write-Error 自己也会抛,
|
|
218
|
+
# 那样就走不到 exit 1 了(虽然 PowerShell 最终也会以非零码退出,但把语义写明确更好)
|
|
219
|
+
[Console]::Error.WriteLine(\$message)
|
|
220
|
+
exit 1
|
|
221
|
+
}
|
|
222
|
+
`;
|
|
223
|
+
}
|
|
224
|
+
/** 生成一条 New-NetFirewallRule 调用(单行,避免 PowerShell 续行符那点麻烦)。 */
|
|
225
|
+
function renderFirewallRule(rule) {
|
|
226
|
+
const parts = [
|
|
227
|
+
'New-NetFirewallRule',
|
|
228
|
+
`-Name '${rule.name}'`,
|
|
229
|
+
`-DisplayName '${rule.displayName}'`,
|
|
230
|
+
`-Description 'dsc 沙箱账号网络围栏(二级网络管控,由 dsc setup 生成)'`,
|
|
231
|
+
`-Direction ${rule.direction}`,
|
|
232
|
+
'-Action Block',
|
|
233
|
+
'-Enabled True',
|
|
234
|
+
'-Profile Any',
|
|
235
|
+
`-Protocol ${rule.protocol}`,
|
|
236
|
+
'-LocalUser "O:LSD:(A;;CC;;;$DscSid)"',
|
|
237
|
+
`-RemoteAddresses '${rule.remoteAddresses}'`,
|
|
238
|
+
];
|
|
239
|
+
if (rule.remotePorts !== null)
|
|
240
|
+
parts.push(`-RemotePorts '${rule.remotePorts}'`);
|
|
241
|
+
return parts.join(' ');
|
|
242
|
+
}
|
|
243
|
+
/** GUID 字面量校验(生成脚本前就挡掉,别让 PowerShell 那边报看不懂的错)。 */
|
|
244
|
+
function assertGuid(guid, label) {
|
|
245
|
+
if (!/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test(guid)) {
|
|
246
|
+
throw new Error(`buildSetupScript:${label} 不是合法 GUID 字面量:${JSON.stringify(guid)}`);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* 落盘脚本并提权执行(会弹一次 UAC:`Start-Process -Verb RunAs`)。
|
|
251
|
+
*
|
|
252
|
+
* 结论取自三处:拉起进程的 exit code、`setup-state.json`(成功账本)、`setup-error.txt`(失败原因)。
|
|
253
|
+
* 三处对不上时按「失败」报,并把能看到的信息都写进 detail —— 提权子进程崩了也不能只回一句「失败」。
|
|
254
|
+
*
|
|
255
|
+
* 注意:本任务里**不调用**它(会弹 UAC,且当前 shell 非管理员);这里保证的是代码正确。
|
|
256
|
+
*/
|
|
257
|
+
export async function runSetupElevated(dscHome, options = {}) {
|
|
258
|
+
const scriptPath = setupScriptPath(dscHome);
|
|
259
|
+
let script;
|
|
260
|
+
try {
|
|
261
|
+
script = buildSetupScript({
|
|
262
|
+
dscHome,
|
|
263
|
+
guidProvider: DSC_WFP_PROVIDER_GUID,
|
|
264
|
+
guidSublayer: DSC_WFP_SUBLAYER_GUID,
|
|
265
|
+
...(options.proxyPort === undefined ? {} : { proxyPort: options.proxyPort }),
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
catch (error) {
|
|
269
|
+
return { ok: false, detail: `生成 setup 脚本失败:${errorText(error)}` };
|
|
270
|
+
}
|
|
271
|
+
try {
|
|
272
|
+
mkdirSync(sandboxDir(dscHome), { recursive: true });
|
|
273
|
+
// 必须带 BOM:Windows PowerShell 5.1 只有在看到 BOM 时才按 UTF-8 读 .ps1,
|
|
274
|
+
// 否则会按系统 ANSI 代码页解释,脚本里的中文(显示名、注释)会变乱码甚至解析失败。
|
|
275
|
+
writeFileSync(scriptPath, `\uFEFF${script}`, { encoding: 'utf8' });
|
|
276
|
+
}
|
|
277
|
+
catch (error) {
|
|
278
|
+
return { ok: false, detail: `写 setup 脚本失败(${scriptPath}):${errorText(error)}` };
|
|
279
|
+
}
|
|
280
|
+
// 上一轮的失败记录必须先清掉,否则这一轮的结论会被旧文件带偏
|
|
281
|
+
try {
|
|
282
|
+
rmSync(setupErrorPath(dscHome), { force: true });
|
|
283
|
+
}
|
|
284
|
+
catch (error) {
|
|
285
|
+
return { ok: false, detail: `清理旧 setup-error.txt 失败(${setupErrorPath(dscHome)}):${errorText(error)}` };
|
|
286
|
+
}
|
|
287
|
+
let launcher;
|
|
288
|
+
try {
|
|
289
|
+
launcher = buildElevationLauncher(scriptPath);
|
|
290
|
+
}
|
|
291
|
+
catch (error) {
|
|
292
|
+
return { ok: false, detail: `拼提权命令失败:${errorText(error)}` };
|
|
293
|
+
}
|
|
294
|
+
const outcome = await runPowerShell(launcher, options.timeoutMs ?? 15 * 60 * 1000);
|
|
295
|
+
const state = readSetupState(dscHome);
|
|
296
|
+
const failureText = readSetupErrorText(dscHome);
|
|
297
|
+
if (outcome.timedOut) {
|
|
298
|
+
return { ok: false, detail: `提权 setup 超时未结束(可能在等 UAC 授权,或脚本卡住):${outcome.stderr.trim()}` };
|
|
299
|
+
}
|
|
300
|
+
if (outcome.code === 0 && state !== null && state.ok) {
|
|
301
|
+
return {
|
|
302
|
+
ok: true,
|
|
303
|
+
detail: `提权 setup 成功:账号 ${state.state.account}(${state.state.sid})已启用,防火墙 ${String(state.state.firewallRules.length)} 条,WFP filter ${String(state.state.wfpFilterCount ?? -1)} 条`,
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
const pieces = [`提权 setup 退出码 ${String(outcome.code)}`];
|
|
307
|
+
if (failureText !== '')
|
|
308
|
+
pieces.push(`脚本报错:${failureText}`);
|
|
309
|
+
if (outcome.stderr.trim() !== '')
|
|
310
|
+
pieces.push(`stderr:${outcome.stderr.trim().slice(0, 500)}`);
|
|
311
|
+
if (state === null) {
|
|
312
|
+
pieces.push('没有 setup-state.json(布防没走完)');
|
|
313
|
+
}
|
|
314
|
+
else if (!state.ok) {
|
|
315
|
+
pieces.push(`账本读不动:${state.error}`);
|
|
316
|
+
}
|
|
317
|
+
else {
|
|
318
|
+
pieces.push(`账本在(${state.state.at})但退出码非 0,属半成品,请重跑 setup`);
|
|
319
|
+
}
|
|
320
|
+
return { ok: false, detail: pieces.join(';') };
|
|
321
|
+
}
|
|
322
|
+
/** 拼「非提权 PowerShell 拉起提权 PowerShell」的那一行(-Wait -PassThru 后把退出码透传出来)。 */
|
|
323
|
+
function buildElevationLauncher(scriptPath) {
|
|
324
|
+
if (/[\r\n]/.test(scriptPath))
|
|
325
|
+
throw new Error(`setup 脚本路径不能含换行:${JSON.stringify(scriptPath)}`);
|
|
326
|
+
const quoted = `'${scriptPath.replace(/'/g, "''")}'`;
|
|
327
|
+
return [
|
|
328
|
+
'$ErrorActionPreference = "Stop"',
|
|
329
|
+
`$p = Start-Process -FilePath 'powershell.exe' -Verb RunAs -Wait -PassThru -ArgumentList @('-NoProfile','-ExecutionPolicy','Bypass','-File',${quoted})`,
|
|
330
|
+
'exit $p.ExitCode',
|
|
331
|
+
].join('; ');
|
|
332
|
+
}
|
|
333
|
+
/** 系统自带的 Windows PowerShell 5.1(Get-LocalUser / Get-NetFirewallRule / Add-Type 都在它这儿)。 */
|
|
334
|
+
function systemPowerShellPath() {
|
|
335
|
+
const systemRoot = process.env.SystemRoot ?? 'C:\\Windows';
|
|
336
|
+
return join(systemRoot, 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe');
|
|
337
|
+
}
|
|
338
|
+
/** 跑一次 powershell -Command,收退出码与输出(带超时,超时就杀)。 */
|
|
339
|
+
function runPowerShell(command, timeoutMs) {
|
|
340
|
+
return new Promise((resolve) => {
|
|
341
|
+
let stdout = '';
|
|
342
|
+
let stderr = '';
|
|
343
|
+
let settled = false;
|
|
344
|
+
const child = spawn(systemPowerShellPath(), ['-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-Command', command], { windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] });
|
|
345
|
+
const timer = setTimeout(() => {
|
|
346
|
+
if (!settled) {
|
|
347
|
+
child.kill();
|
|
348
|
+
settled = true;
|
|
349
|
+
resolve({ code: -1, stdout, stderr, timedOut: true });
|
|
350
|
+
}
|
|
351
|
+
}, timeoutMs);
|
|
352
|
+
child.stdout?.on('data', (chunk) => {
|
|
353
|
+
stdout += chunk.toString('utf8');
|
|
354
|
+
});
|
|
355
|
+
child.stderr?.on('data', (chunk) => {
|
|
356
|
+
stderr += chunk.toString('utf8');
|
|
357
|
+
});
|
|
358
|
+
child.on('error', (error) => {
|
|
359
|
+
if (settled)
|
|
360
|
+
return;
|
|
361
|
+
settled = true;
|
|
362
|
+
clearTimeout(timer);
|
|
363
|
+
resolve({ code: -1, stdout, stderr: `${stderr}${errorText(error)}`, timedOut: false });
|
|
364
|
+
});
|
|
365
|
+
child.on('close', (code) => {
|
|
366
|
+
if (settled)
|
|
367
|
+
return;
|
|
368
|
+
settled = true;
|
|
369
|
+
clearTimeout(timer);
|
|
370
|
+
resolve({ code: code ?? -1, stdout, stderr, timedOut: false });
|
|
371
|
+
});
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
/** 读 setup-error.txt(没有就空串;顺手剥掉 BOM)。 */
|
|
375
|
+
function readSetupErrorText(dscHome) {
|
|
376
|
+
try {
|
|
377
|
+
const text = readFileSync(setupErrorPath(dscHome), 'utf8');
|
|
378
|
+
return (text.charCodeAt(0) === 0xfeff ? text.slice(1) : text).trim();
|
|
379
|
+
}
|
|
380
|
+
catch {
|
|
381
|
+
return '';
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
/** 异常转一句话。 */
|
|
385
|
+
function errorText(error) {
|
|
386
|
+
return error instanceof Error ? error.message : String(error);
|
|
387
|
+
}
|
|
388
|
+
// ── 内嵌 C#:WFP 安装器 ──────────────────────────────────────────────────────
|
|
389
|
+
//
|
|
390
|
+
// 为什么用 C#(而不是 koffi 直接装):装持久 filter 要管理员 + 单事务 + 一堆手写结构体,
|
|
391
|
+
// 这些在提权 PowerShell 里跑最省事(Add-Type 自带 C# 编译器,不用额外分发原生模块),
|
|
392
|
+
// 而 koffi 只留着做免提权读(见 wfp.ts)。
|
|
393
|
+
//
|
|
394
|
+
// **结构体布局是本文件最难的部分**,全部按 Windows SDK 头(fwpmtypes.h/fwptypes.h)逐字段写,
|
|
395
|
+
// 并逐条核对过 windows-sys 0.61.2 的 Rust 定义(同一份 Win32 元数据生成,字段序与 C 头一致)。
|
|
396
|
+
// x64 下的 sizeof 推算如下(MSVC 默认 Pack=8;含指针/uint64 的 union 对齐 8):
|
|
397
|
+
//
|
|
398
|
+
// FWP_BYTE_BLOB = 4(cbData) + 4(补) + 8(pbData) = 16
|
|
399
|
+
// FWPM_DISPLAY_DATA0 = 8(name) + 8(description) = 16
|
|
400
|
+
// FWP_VALUE0 = 4(type) + 4(补,union 里含 UINT64* 故对齐 8) + 8(union) = 16
|
|
401
|
+
// FWPM_FILTER_CONDITION0 = 16(fieldKey) + 4(matchType) + 4(补) + 16(conditionValue)= 40
|
|
402
|
+
// FWPM_ACTION0 = 4(type) + 4(补) + 16(union{GUID|UINT64}) = 24
|
|
403
|
+
// FWPM_FILTER0 = 见下面逐行偏移注释 = 200
|
|
404
|
+
// FWPM_PROVIDER0 = 16+16(displayData)+4+4(补)+16(providerData)+8(service) = 64
|
|
405
|
+
// FWPM_SUBLAYER0 = 16+16+4+4(补)+8(providerKey)+16(providerData)+2(weight)
|
|
406
|
+
// + 6(补到 8 的倍数) = 72
|
|
407
|
+
// EXPLICIT_ACCESS_W = 4+4+4 + 4(补) + 32(TRUSTEE_W) = 48
|
|
408
|
+
// (与仓库已有的 abi.ts 里 EA_OFFSETS 完全一致:ptstrName@40、TrusteeForm@28)
|
|
409
|
+
//
|
|
410
|
+
// 这些数字不是「算得差不多」就行的:C# 里 union 用 Explicit 布局单独声明,外层结构体用
|
|
411
|
+
// Sequential + Pack=8 并把「C 里靠对齐自然出现」的 4 字节补洞写成显式的 pad 字段,
|
|
412
|
+
// 这样 CLR 的打点不会因为自己对齐规则不同而跑偏。SelfTest() 会在装之前把
|
|
413
|
+
// 每个 sizeof 与关键字段偏移都验一遍,对不上就抛(宁可不装,也不装一份条件错位的 filter)。
|
|
414
|
+
const WFP_CSHARP_SOURCE = `using System;
|
|
415
|
+
using System.Collections.Generic;
|
|
416
|
+
using System.Globalization;
|
|
417
|
+
using System.Runtime.InteropServices;
|
|
418
|
+
using System.Text;
|
|
419
|
+
|
|
420
|
+
// dsc 沙箱网络管控第二级:WFP 持久 provider + sublayer + 12 条 BLOCK filter 的安装器。
|
|
421
|
+
// 全部条件都以 ALE_USER_ID(SD 主体 = 沙箱账号 SID)限定,只影响沙箱账号。
|
|
422
|
+
public static class DscWfp
|
|
423
|
+
{
|
|
424
|
+
// ── 常量(与 net-abi.ts 同源;值取自 Windows SDK,已与 windows-sys 0.61.2 逐条核对)──
|
|
425
|
+
// FWP_ACTION_BLOCK 是 0x1001 而不是 0x1:0x1000 是 FWP_ACTION_FLAG_TERMINATING。
|
|
426
|
+
private const uint FWP_ACTION_BLOCK = 0x1001;
|
|
427
|
+
private const int FWP_MATCH_EQUAL = 0;
|
|
428
|
+
private const int FWP_EMPTY = 0;
|
|
429
|
+
private const int FWP_UINT8 = 1;
|
|
430
|
+
private const int FWP_UINT16 = 2;
|
|
431
|
+
private const int FWP_SECURITY_DESCRIPTOR_TYPE = 14;
|
|
432
|
+
private const uint FWPM_FILTER_FLAG_PERSISTENT = 0x1;
|
|
433
|
+
private const uint FWPM_PROVIDER_FLAG_PERSISTENT = 0x1;
|
|
434
|
+
private const uint FWPM_SUBLAYER_FLAG_PERSISTENT = 0x1;
|
|
435
|
+
private const uint FWP_ACTRL_MATCH_FILTER = 0x1;
|
|
436
|
+
private const uint GRANT_ACCESS = 2;
|
|
437
|
+
private const uint RPC_C_AUTHN_WINNT = 10;
|
|
438
|
+
private const ushort DSC_SUBLAYER_WEIGHT = 0x8000;
|
|
439
|
+
private const uint FWP_E_FILTER_NOT_FOUND = 0x80320003;
|
|
440
|
+
private const uint FWP_E_NOT_FOUND = 0x80320008;
|
|
441
|
+
private const uint FWP_E_ALREADY_EXISTS = 0x80320009;
|
|
442
|
+
private const int AclSizeInformation = 2;
|
|
443
|
+
private const string ProviderName = "dsc Windows Sandbox WFP";
|
|
444
|
+
private const string SublayerName = "dsc Windows Sandbox WFP";
|
|
445
|
+
private const string ProviderDescription = "dsc 沙箱网络管控的持久 WFP provider";
|
|
446
|
+
private const string SublayerDescription = "dsc 沙箱网络管控的持久 WFP sublayer(12 条 BLOCK filter 挂这里)";
|
|
447
|
+
|
|
448
|
+
// 条件 GUID(Windows 官方常量,fwpmu.h)
|
|
449
|
+
private static readonly Guid CondAleUserId = new Guid("af043a0a-b34d-4f86-979c-c90371af6e66");
|
|
450
|
+
private static readonly Guid CondIpProtocol = new Guid("3971ef2b-623e-4f9a-8cb1-6e79b806b9a7");
|
|
451
|
+
private static readonly Guid CondIpRemotePort = new Guid("c35a604d-d22b-4e1a-91b4-68f674ee674b");
|
|
452
|
+
|
|
453
|
+
// ── 结构体(x64,Pack=8;sizeof 推算见 setup.ts 顶部注释)──
|
|
454
|
+
|
|
455
|
+
[StructLayout(LayoutKind.Sequential, Pack = 8)]
|
|
456
|
+
internal struct FWP_BYTE_BLOB // 16:size@0,data@8
|
|
457
|
+
{
|
|
458
|
+
public uint size;
|
|
459
|
+
public IntPtr data;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
[StructLayout(LayoutKind.Sequential, Pack = 8)]
|
|
463
|
+
internal struct FWPM_DISPLAY_DATA0 // 16:name@0,description@8
|
|
464
|
+
{
|
|
465
|
+
public IntPtr name;
|
|
466
|
+
public IntPtr description;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
// FWP_VALUE0 / FWP_CONDITION_VALUE0 的匿名 union:x64 上取最大成员(指针)= 8 字节。
|
|
470
|
+
// 数值型条件(UINT8/UINT16)也走这里:小端下低地址就是最低字节,所以写 sd 这个指针槽
|
|
471
|
+
// 等于把协议号/端口号写进了 union 的低位,读法一致。
|
|
472
|
+
[StructLayout(LayoutKind.Explicit, Pack = 8)]
|
|
473
|
+
internal struct FWP_VALUE0_UNION // 8
|
|
474
|
+
{
|
|
475
|
+
[FieldOffset(0)] public byte uint8;
|
|
476
|
+
[FieldOffset(0)] public ushort uint16;
|
|
477
|
+
[FieldOffset(0)] public uint uint32;
|
|
478
|
+
[FieldOffset(0)] public IntPtr sd;
|
|
479
|
+
[FieldOffset(0)] public IntPtr uint64;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
[StructLayout(LayoutKind.Sequential, Pack = 8)]
|
|
483
|
+
internal struct FWP_VALUE0 // 16:type@0,补 4,union@8
|
|
484
|
+
{
|
|
485
|
+
public int type;
|
|
486
|
+
private int pad0;
|
|
487
|
+
public FWP_VALUE0_UNION value;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
[StructLayout(LayoutKind.Sequential, Pack = 8)]
|
|
491
|
+
internal struct FWPM_FILTER_CONDITION0 // 40:fieldKey@0,matchType@16,补 4,conditionValue@24
|
|
492
|
+
{
|
|
493
|
+
public Guid fieldKey;
|
|
494
|
+
public int matchType;
|
|
495
|
+
private int pad0;
|
|
496
|
+
public FWP_VALUE0 conditionValue; // FWP_CONDITION_VALUE0 与 FWP_VALUE0 同布局
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
[StructLayout(LayoutKind.Explicit, Pack = 8)]
|
|
500
|
+
internal struct FWPM_ACTION0_UNION // 16:filterType(GUID) / calloutKey(UINT64) 叠在一起
|
|
501
|
+
{
|
|
502
|
+
[FieldOffset(0)] public Guid filterType;
|
|
503
|
+
[FieldOffset(0)] public ulong calloutKey;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
[StructLayout(LayoutKind.Sequential, Pack = 8)]
|
|
507
|
+
internal struct FWPM_ACTION0 // 24:type@0,补 4,union@8
|
|
508
|
+
{
|
|
509
|
+
public uint type;
|
|
510
|
+
private uint pad0;
|
|
511
|
+
public FWPM_ACTION0_UNION action;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
[StructLayout(LayoutKind.Explicit, Pack = 8)]
|
|
515
|
+
internal struct FWPM_FILTER0_UNION // 16:rawContext(UINT64) / providerContextKey(GUID)
|
|
516
|
+
{
|
|
517
|
+
[FieldOffset(0)] public ulong rawContext;
|
|
518
|
+
[FieldOffset(0)] public Guid providerContextKey;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
[StructLayout(LayoutKind.Sequential, Pack = 8)]
|
|
522
|
+
internal struct FWPM_FILTER0 // 200
|
|
523
|
+
{
|
|
524
|
+
public Guid filterKey; // 0 .. 16
|
|
525
|
+
public FWPM_DISPLAY_DATA0 displayData; // 16 .. 32
|
|
526
|
+
public uint flags; // 32 .. 36
|
|
527
|
+
private uint pad0; // 36 .. 40(providerKey 是指针,C 里自然补 4)
|
|
528
|
+
public IntPtr providerKey; // 40 .. 48
|
|
529
|
+
public FWP_BYTE_BLOB providerData; // 48 .. 64
|
|
530
|
+
public Guid layerKey; // 64 .. 80
|
|
531
|
+
public Guid subLayerKey; // 80 .. 96
|
|
532
|
+
public FWP_VALUE0 weight; // 96 .. 112(对齐 8)
|
|
533
|
+
public uint numFilterConditions; // 112 .. 116
|
|
534
|
+
private uint pad1; // 116 .. 120(filterCondition 是指针)
|
|
535
|
+
public IntPtr filterCondition; // 120 .. 128
|
|
536
|
+
public FWPM_ACTION0 action; // 128 .. 152
|
|
537
|
+
public FWPM_FILTER0_UNION anon; // 152 .. 168
|
|
538
|
+
public IntPtr reserved; // 168 .. 176
|
|
539
|
+
public ulong filterId; // 176 .. 184
|
|
540
|
+
public FWP_VALUE0 effectiveWeight; // 184 .. 200
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
[StructLayout(LayoutKind.Sequential, Pack = 8)]
|
|
544
|
+
internal struct FWPM_PROVIDER0 // 64
|
|
545
|
+
{
|
|
546
|
+
public Guid providerKey; // 0 .. 16
|
|
547
|
+
public FWPM_DISPLAY_DATA0 displayData; // 16 .. 32
|
|
548
|
+
public uint flags; // 32 .. 36
|
|
549
|
+
private uint pad0; // 36 .. 40
|
|
550
|
+
public FWP_BYTE_BLOB providerData; // 40 .. 56
|
|
551
|
+
public IntPtr serviceName; // 56 .. 64
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
[StructLayout(LayoutKind.Sequential, Pack = 8)]
|
|
555
|
+
internal struct FWPM_SUBLAYER0 // 72(weight 之后补 6 字节到 8 的倍数,CLR 自动补)
|
|
556
|
+
{
|
|
557
|
+
public Guid subLayerKey; // 0 .. 16
|
|
558
|
+
public FWPM_DISPLAY_DATA0 displayData; // 16 .. 32
|
|
559
|
+
public uint flags; // 32 .. 36
|
|
560
|
+
private uint pad0; // 36 .. 40
|
|
561
|
+
public IntPtr providerKey; // 40 .. 48
|
|
562
|
+
public FWP_BYTE_BLOB providerData; // 48 .. 64
|
|
563
|
+
public ushort weight; // 64 .. 66
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
[StructLayout(LayoutKind.Sequential, Pack = 8)]
|
|
567
|
+
internal struct TRUSTEE_W // 32
|
|
568
|
+
{
|
|
569
|
+
public IntPtr pMultipleTrustee; // 0 .. 8
|
|
570
|
+
public int MultipleTrusteeOperation; // 8 .. 12
|
|
571
|
+
public int TrusteeForm; // 12 .. 16
|
|
572
|
+
public int TrusteeType; // 16 .. 20
|
|
573
|
+
private int pad0; // 20 .. 24
|
|
574
|
+
public IntPtr ptstrName; // 24 .. 32
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
[StructLayout(LayoutKind.Sequential, Pack = 8)]
|
|
578
|
+
internal struct EXPLICIT_ACCESS_W // 48(与 abi.ts 的 EA_OFFSETS 一致)
|
|
579
|
+
{
|
|
580
|
+
public uint grfAccessPermissions; // 0 .. 4
|
|
581
|
+
public uint grfAccessMode; // 4 .. 8
|
|
582
|
+
public uint grfInheritance; // 8 .. 12
|
|
583
|
+
private uint pad0; // 12 .. 16
|
|
584
|
+
public TRUSTEE_W Trustee; // 16 .. 48
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
[StructLayout(LayoutKind.Sequential, Pack = 8)]
|
|
588
|
+
internal struct ACL_SIZE_INFORMATION // 12
|
|
589
|
+
{
|
|
590
|
+
public uint AceCount;
|
|
591
|
+
public uint AclBytesInUse;
|
|
592
|
+
public uint AclBytesFree;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
// ── P/Invoke ──
|
|
596
|
+
//
|
|
597
|
+
// 全部写 ExactSpelling = true:带 W 后缀的入口名已经写全了,不要让运行时再去猜
|
|
598
|
+
// 「名字 + W」「名字 + A」那套探测(探测不到才回落,容易埋下莫名其妙的 EntryPointNotFound)。
|
|
599
|
+
|
|
600
|
+
[DllImport("fwpuclnt.dll", ExactSpelling = true)]
|
|
601
|
+
private static extern uint FwpmEngineOpen0(IntPtr serverName, uint authnService, IntPtr authIdentity, IntPtr session, out IntPtr engineHandle);
|
|
602
|
+
|
|
603
|
+
[DllImport("fwpuclnt.dll", ExactSpelling = true)]
|
|
604
|
+
private static extern uint FwpmEngineClose0(IntPtr engineHandle);
|
|
605
|
+
|
|
606
|
+
[DllImport("fwpuclnt.dll", ExactSpelling = true)]
|
|
607
|
+
private static extern uint FwpmTransactionBegin0(IntPtr engineHandle, uint flags);
|
|
608
|
+
|
|
609
|
+
[DllImport("fwpuclnt.dll", ExactSpelling = true)]
|
|
610
|
+
private static extern uint FwpmTransactionCommit0(IntPtr engineHandle);
|
|
611
|
+
|
|
612
|
+
[DllImport("fwpuclnt.dll", ExactSpelling = true)]
|
|
613
|
+
private static extern uint FwpmTransactionAbort0(IntPtr engineHandle);
|
|
614
|
+
|
|
615
|
+
[DllImport("fwpuclnt.dll", ExactSpelling = true)]
|
|
616
|
+
private static extern uint FwpmProviderAdd0(IntPtr engineHandle, ref FWPM_PROVIDER0 provider, IntPtr sd);
|
|
617
|
+
|
|
618
|
+
[DllImport("fwpuclnt.dll", ExactSpelling = true)]
|
|
619
|
+
private static extern uint FwpmSubLayerAdd0(IntPtr engineHandle, ref FWPM_SUBLAYER0 subLayer, IntPtr sd);
|
|
620
|
+
|
|
621
|
+
[DllImport("fwpuclnt.dll", ExactSpelling = true)]
|
|
622
|
+
private static extern uint FwpmFilterAdd0(IntPtr engineHandle, ref FWPM_FILTER0 filter, IntPtr sd, out ulong id);
|
|
623
|
+
|
|
624
|
+
[DllImport("fwpuclnt.dll", ExactSpelling = true)]
|
|
625
|
+
private static extern uint FwpmFilterDeleteByKey0(IntPtr engineHandle, ref Guid key);
|
|
626
|
+
|
|
627
|
+
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
|
|
628
|
+
private static extern void BuildExplicitAccessWithNameW(ref EXPLICIT_ACCESS_W pExplicitAccess, IntPtr pTrusteeName, uint accessPermissions, uint accessMode, uint inheritance);
|
|
629
|
+
|
|
630
|
+
[DllImport("advapi32.dll", ExactSpelling = true)]
|
|
631
|
+
private static extern uint BuildSecurityDescriptorW(IntPtr pOwner, IntPtr pGroup, uint cCountOfAccessEntries, ref EXPLICIT_ACCESS_W pListOfAccessEntries, uint cCountOfAuditEntries, IntPtr pListOfAuditEntries, IntPtr pOldSD, out uint pSizeNewSD, out IntPtr pNewSD);
|
|
632
|
+
|
|
633
|
+
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
|
|
634
|
+
private static extern bool ConvertStringSidToSidW(string stringSid, out IntPtr sid);
|
|
635
|
+
|
|
636
|
+
// LocalFree 在 kernel32(winbase.h),不在 advapi32:写错 DLL 名要到运行时才炸
|
|
637
|
+
// (EntryPointNotFoundException),而且是炸在 finally 的释放路径上,最难查。
|
|
638
|
+
[DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true)]
|
|
639
|
+
private static extern IntPtr LocalFree(IntPtr hMem);
|
|
640
|
+
|
|
641
|
+
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
|
|
642
|
+
private static extern bool GetSecurityDescriptorDacl(IntPtr pSecurityDescriptor, out bool bDaclPresent, out IntPtr pDacl, out bool bDaclDefaulted);
|
|
643
|
+
|
|
644
|
+
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
|
|
645
|
+
private static extern bool GetAclInformation(IntPtr pAcl, out ACL_SIZE_INFORMATION pAclInformation, uint nAclInformationLength, int dwAclInformationClass);
|
|
646
|
+
|
|
647
|
+
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
|
|
648
|
+
private static extern bool GetAce(IntPtr pAcl, uint dwAceIndex, out IntPtr pAce);
|
|
649
|
+
|
|
650
|
+
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
|
|
651
|
+
private static extern bool EqualSid(IntPtr pSid1, IntPtr pSid2);
|
|
652
|
+
|
|
653
|
+
// ── 布局自检:装之前先把「我对 C 布局的理解」验一遍 ──
|
|
654
|
+
|
|
655
|
+
private static void ExpectSize(StringBuilder report, string name, int actual, int expected)
|
|
656
|
+
{
|
|
657
|
+
report.Append(name).Append('=').Append(actual.ToString(CultureInfo.InvariantCulture)).Append(' ');
|
|
658
|
+
if (actual != expected)
|
|
659
|
+
{
|
|
660
|
+
throw new InvalidOperationException("结构体布局与 C 头不一致:" + name + " CLR=" + actual.ToString(CultureInfo.InvariantCulture) + " 期望=" + expected.ToString(CultureInfo.InvariantCulture));
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
private static void ExpectOffset(StringBuilder report, string label, int actual, int expected)
|
|
665
|
+
{
|
|
666
|
+
report.Append(label).Append(actual.ToString(CultureInfo.InvariantCulture)).Append(' ');
|
|
667
|
+
if (actual != expected)
|
|
668
|
+
{
|
|
669
|
+
throw new InvalidOperationException("结构体字段偏移与 C 头不一致:" + label + " CLR=" + actual.ToString(CultureInfo.InvariantCulture) + " 期望=" + expected.ToString(CultureInfo.InvariantCulture));
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
public static string SelfTest()
|
|
674
|
+
{
|
|
675
|
+
StringBuilder report = new StringBuilder();
|
|
676
|
+
ExpectSize(report, "FWP_BYTE_BLOB", Marshal.SizeOf(typeof(FWP_BYTE_BLOB)), 16);
|
|
677
|
+
ExpectSize(report, "FWPM_DISPLAY_DATA0", Marshal.SizeOf(typeof(FWPM_DISPLAY_DATA0)), 16);
|
|
678
|
+
ExpectSize(report, "FWP_VALUE0", Marshal.SizeOf(typeof(FWP_VALUE0)), 16);
|
|
679
|
+
ExpectSize(report, "FWPM_FILTER_CONDITION0", Marshal.SizeOf(typeof(FWPM_FILTER_CONDITION0)), 40);
|
|
680
|
+
ExpectSize(report, "FWPM_ACTION0", Marshal.SizeOf(typeof(FWPM_ACTION0)), 24);
|
|
681
|
+
ExpectSize(report, "FWPM_FILTER0", Marshal.SizeOf(typeof(FWPM_FILTER0)), 200);
|
|
682
|
+
ExpectSize(report, "FWPM_PROVIDER0", Marshal.SizeOf(typeof(FWPM_PROVIDER0)), 64);
|
|
683
|
+
ExpectSize(report, "FWPM_SUBLAYER0", Marshal.SizeOf(typeof(FWPM_SUBLAYER0)), 72);
|
|
684
|
+
ExpectSize(report, "EXPLICIT_ACCESS_W", Marshal.SizeOf(typeof(EXPLICIT_ACCESS_W)), 48);
|
|
685
|
+
// sizeof 对得上也可能是把两个字段的位置互换了,所以关键偏移也钉住
|
|
686
|
+
ExpectOffset(report, "FWPM_FILTER0.action@", (int)Marshal.OffsetOf(typeof(FWPM_FILTER0), "action"), 128);
|
|
687
|
+
ExpectOffset(report, "FWPM_FILTER0.effectiveWeight@", (int)Marshal.OffsetOf(typeof(FWPM_FILTER0), "effectiveWeight"), 184);
|
|
688
|
+
ExpectOffset(report, "FWP_VALUE0.value@", (int)Marshal.OffsetOf(typeof(FWP_VALUE0), "value"), 8);
|
|
689
|
+
ExpectOffset(report, "FWPM_ACTION0.action@", (int)Marshal.OffsetOf(typeof(FWPM_ACTION0), "action"), 8);
|
|
690
|
+
ExpectOffset(report, "FWPM_FILTER_CONDITION0.conditionValue@", (int)Marshal.OffsetOf(typeof(FWPM_FILTER_CONDITION0), "conditionValue"), 24);
|
|
691
|
+
ExpectOffset(report, "EXPLICIT_ACCESS_W.Trustee@", (int)Marshal.OffsetOf(typeof(EXPLICIT_ACCESS_W), "Trustee"), 16);
|
|
692
|
+
return "OK " + report.ToString().Trim();
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
// ── 安装 ──
|
|
696
|
+
|
|
697
|
+
/// <summary>
|
|
698
|
+
/// 装持久 provider + sublayer + 每条 filter(单事务,persistent)。
|
|
699
|
+
/// filters 每行形如 filterKey|name|layerKey|protocol|remotePort(后两个字段可空)。
|
|
700
|
+
/// </summary>
|
|
701
|
+
public static int Install(string account, string expectedSid, string providerGuid, string sublayerGuid, string[] filters)
|
|
702
|
+
{
|
|
703
|
+
if (String.IsNullOrEmpty(account)) throw new ArgumentException("沙箱账号名为空");
|
|
704
|
+
if (filters == null || filters.Length == 0) throw new ArgumentException("filter 规格为空");
|
|
705
|
+
Guid providerKey = Guid.Parse(providerGuid);
|
|
706
|
+
Guid sublayerKey = Guid.Parse(sublayerGuid);
|
|
707
|
+
|
|
708
|
+
IntPtr engine = IntPtr.Zero;
|
|
709
|
+
IntPtr securityDescriptor = IntPtr.Zero;
|
|
710
|
+
IntPtr expectedSidPtr = IntPtr.Zero;
|
|
711
|
+
IntPtr accountNamePtr = IntPtr.Zero;
|
|
712
|
+
IntPtr conditionBlobPtr = IntPtr.Zero;
|
|
713
|
+
List<IntPtr> owned = new List<IntPtr>();
|
|
714
|
+
uint sizeNewSd = 0;
|
|
715
|
+
bool inTransaction = false;
|
|
716
|
+
try
|
|
717
|
+
{
|
|
718
|
+
// 引擎:serverName=null、认证 RPC_C_AUTHN_WINNT、session=null。装持久对象要求管理员令牌。
|
|
719
|
+
uint rc = FwpmEngineOpen0(IntPtr.Zero, RPC_C_AUTHN_WINNT, IntPtr.Zero, IntPtr.Zero, out engine);
|
|
720
|
+
Fail(rc, "FwpmEngineOpen0(装 WFP 需要管理员权限)");
|
|
721
|
+
|
|
722
|
+
// ALE_USER_ID 的条件值是一个自相对 SD,主体 = 沙箱账号 SID,ACE 掩码 = FWP_ACTRL_MATCH_FILTER(1)。
|
|
723
|
+
// 用 BuildExplicitAccessWithNameW 按名字解析(提权会话下账号已存在),再让 BuildSecurityDescriptorW 造 SD。
|
|
724
|
+
accountNamePtr = Marshal.StringToHGlobalUni(account);
|
|
725
|
+
EXPLICIT_ACCESS_W access = new EXPLICIT_ACCESS_W();
|
|
726
|
+
BuildExplicitAccessWithNameW(ref access, accountNamePtr, FWP_ACTRL_MATCH_FILTER, GRANT_ACCESS, 0);
|
|
727
|
+
rc = BuildSecurityDescriptorW(IntPtr.Zero, IntPtr.Zero, 1, ref access, 0, IntPtr.Zero, IntPtr.Zero, out sizeNewSd, out securityDescriptor);
|
|
728
|
+
Fail(rc, "BuildSecurityDescriptorW");
|
|
729
|
+
if (securityDescriptor == IntPtr.Zero) throw new InvalidOperationException("BuildSecurityDescriptorW 给出空 SD");
|
|
730
|
+
if (!ConvertStringSidToSidW(expectedSid, out expectedSidPtr)) throw Win32("ConvertStringSidToSidW");
|
|
731
|
+
// 读回校验:SD 里的 SID 必须是 Get-LocalUser 报的那个,否则 filter 会挂在别人身上(fail-closed)
|
|
732
|
+
VerifyUserSid(securityDescriptor, expectedSidPtr);
|
|
733
|
+
|
|
734
|
+
// FWP_BYTE_BLOB{size, data} 自己也要住在非托管内存里,直到所有 FwpmFilterAdd0 都返回
|
|
735
|
+
FWP_BYTE_BLOB conditionBlob = new FWP_BYTE_BLOB();
|
|
736
|
+
conditionBlob.size = sizeNewSd;
|
|
737
|
+
conditionBlob.data = securityDescriptor;
|
|
738
|
+
conditionBlobPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(FWP_BYTE_BLOB)));
|
|
739
|
+
Marshal.StructureToPtr(conditionBlob, conditionBlobPtr, false);
|
|
740
|
+
|
|
741
|
+
rc = FwpmTransactionBegin0(engine, 0);
|
|
742
|
+
Fail(rc, "FwpmTransactionBegin0");
|
|
743
|
+
inTransaction = true;
|
|
744
|
+
|
|
745
|
+
AddProvider(engine, providerKey);
|
|
746
|
+
AddSublayer(engine, sublayerKey, providerKey);
|
|
747
|
+
|
|
748
|
+
int installed = 0;
|
|
749
|
+
for (int i = 0; i < filters.Length; i++)
|
|
750
|
+
{
|
|
751
|
+
AddOneFilter(engine, filters[i], sublayerKey, providerKey, conditionBlobPtr, owned);
|
|
752
|
+
installed++;
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
rc = FwpmTransactionCommit0(engine);
|
|
756
|
+
Fail(rc, "FwpmTransactionCommit0");
|
|
757
|
+
inTransaction = false;
|
|
758
|
+
return installed;
|
|
759
|
+
}
|
|
760
|
+
catch
|
|
761
|
+
{
|
|
762
|
+
if (inTransaction)
|
|
763
|
+
{
|
|
764
|
+
try { FwpmTransactionAbort0(engine); } catch { }
|
|
765
|
+
}
|
|
766
|
+
throw;
|
|
767
|
+
}
|
|
768
|
+
finally
|
|
769
|
+
{
|
|
770
|
+
if (securityDescriptor != IntPtr.Zero) LocalFree(securityDescriptor);
|
|
771
|
+
if (expectedSidPtr != IntPtr.Zero) LocalFree(expectedSidPtr);
|
|
772
|
+
if (conditionBlobPtr != IntPtr.Zero) Marshal.FreeHGlobal(conditionBlobPtr);
|
|
773
|
+
if (accountNamePtr != IntPtr.Zero) Marshal.FreeHGlobal(accountNamePtr);
|
|
774
|
+
for (int i = 0; i < owned.Count; i++) Marshal.FreeHGlobal(owned[i]);
|
|
775
|
+
if (engine != IntPtr.Zero) FwpmEngineClose0(engine);
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
/// <summary>
|
|
780
|
+
/// 免提权自检:按账号名造一份 ALE_USER_ID 用的 SD,再把 SID 与 ACE 掩码读回来核对。
|
|
781
|
+
/// 这段逻辑(BuildExplicitAccessWithNameW + BuildSecurityDescriptorW + DACL 读回)不碰 WFP、
|
|
782
|
+
/// 不改系统,所以普通权限就能验——它是整套条件里最容易出错、代价最大的一环(SID 挂错账号
|
|
783
|
+
/// 等于围栏形同虚设),值得留一个能在装机前单独跑的验法。
|
|
784
|
+
/// </summary>
|
|
785
|
+
public static string SelfTestUserSd(string accountName, string expectedSid)
|
|
786
|
+
{
|
|
787
|
+
IntPtr securityDescriptor = IntPtr.Zero;
|
|
788
|
+
IntPtr expectedSidPtr = IntPtr.Zero;
|
|
789
|
+
IntPtr accountNamePtr = IntPtr.Zero;
|
|
790
|
+
try
|
|
791
|
+
{
|
|
792
|
+
accountNamePtr = Marshal.StringToHGlobalUni(accountName);
|
|
793
|
+
EXPLICIT_ACCESS_W access = new EXPLICIT_ACCESS_W();
|
|
794
|
+
BuildExplicitAccessWithNameW(ref access, accountNamePtr, FWP_ACTRL_MATCH_FILTER, GRANT_ACCESS, 0);
|
|
795
|
+
uint sizeNewSd = 0;
|
|
796
|
+
uint rc = BuildSecurityDescriptorW(IntPtr.Zero, IntPtr.Zero, 1, ref access, 0, IntPtr.Zero, IntPtr.Zero, out sizeNewSd, out securityDescriptor);
|
|
797
|
+
Fail(rc, "BuildSecurityDescriptorW");
|
|
798
|
+
if (securityDescriptor == IntPtr.Zero) throw new InvalidOperationException("BuildSecurityDescriptorW 给出空 SD");
|
|
799
|
+
if (!ConvertStringSidToSidW(expectedSid, out expectedSidPtr)) throw Win32("ConvertStringSidToSidW");
|
|
800
|
+
VerifyUserSid(securityDescriptor, expectedSidPtr);
|
|
801
|
+
return "OK sdSize=" + sizeNewSd.ToString(CultureInfo.InvariantCulture);
|
|
802
|
+
}
|
|
803
|
+
finally
|
|
804
|
+
{
|
|
805
|
+
if (securityDescriptor != IntPtr.Zero) LocalFree(securityDescriptor);
|
|
806
|
+
if (expectedSidPtr != IntPtr.Zero) LocalFree(expectedSidPtr);
|
|
807
|
+
if (accountNamePtr != IntPtr.Zero) Marshal.FreeHGlobal(accountNamePtr);
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
private static void AddProvider(IntPtr engine, Guid providerKey)
|
|
812
|
+
{
|
|
813
|
+
IntPtr namePtr = Marshal.StringToHGlobalUni(ProviderName);
|
|
814
|
+
IntPtr descPtr = Marshal.StringToHGlobalUni(ProviderDescription);
|
|
815
|
+
try
|
|
816
|
+
{
|
|
817
|
+
FWPM_PROVIDER0 provider = new FWPM_PROVIDER0();
|
|
818
|
+
provider.providerKey = providerKey;
|
|
819
|
+
provider.displayData.name = namePtr;
|
|
820
|
+
provider.displayData.description = descPtr;
|
|
821
|
+
provider.flags = FWPM_PROVIDER_FLAG_PERSISTENT;
|
|
822
|
+
provider.providerData.size = 0;
|
|
823
|
+
provider.providerData.data = IntPtr.Zero;
|
|
824
|
+
provider.serviceName = IntPtr.Zero;
|
|
825
|
+
uint rc = FwpmProviderAdd0(engine, ref provider, IntPtr.Zero);
|
|
826
|
+
// 已存在就是上一次装过(持久对象不会随进程消失),照常往下走
|
|
827
|
+
if (rc != 0 && rc != FWP_E_ALREADY_EXISTS) Fail(rc, "FwpmProviderAdd0");
|
|
828
|
+
}
|
|
829
|
+
finally
|
|
830
|
+
{
|
|
831
|
+
Marshal.FreeHGlobal(namePtr);
|
|
832
|
+
Marshal.FreeHGlobal(descPtr);
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
private static void AddSublayer(IntPtr engine, Guid sublayerKey, Guid providerKey)
|
|
837
|
+
{
|
|
838
|
+
IntPtr providerKeyPtr = Marshal.AllocHGlobal(16);
|
|
839
|
+
Marshal.StructureToPtr(providerKey, providerKeyPtr, false);
|
|
840
|
+
IntPtr namePtr = Marshal.StringToHGlobalUni(SublayerName);
|
|
841
|
+
IntPtr descPtr = Marshal.StringToHGlobalUni(SublayerDescription);
|
|
842
|
+
try
|
|
843
|
+
{
|
|
844
|
+
FWPM_SUBLAYER0 sublayer = new FWPM_SUBLAYER0();
|
|
845
|
+
sublayer.subLayerKey = sublayerKey;
|
|
846
|
+
sublayer.displayData.name = namePtr;
|
|
847
|
+
sublayer.displayData.description = descPtr;
|
|
848
|
+
sublayer.flags = FWPM_SUBLAYER_FLAG_PERSISTENT;
|
|
849
|
+
sublayer.providerKey = providerKeyPtr;
|
|
850
|
+
sublayer.providerData.size = 0;
|
|
851
|
+
sublayer.providerData.data = IntPtr.Zero;
|
|
852
|
+
sublayer.weight = DSC_SUBLAYER_WEIGHT;
|
|
853
|
+
uint rc = FwpmSubLayerAdd0(engine, ref sublayer, IntPtr.Zero);
|
|
854
|
+
if (rc != 0 && rc != FWP_E_ALREADY_EXISTS) Fail(rc, "FwpmSubLayerAdd0");
|
|
855
|
+
}
|
|
856
|
+
finally
|
|
857
|
+
{
|
|
858
|
+
Marshal.FreeHGlobal(providerKeyPtr);
|
|
859
|
+
Marshal.FreeHGlobal(namePtr);
|
|
860
|
+
Marshal.FreeHGlobal(descPtr);
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
private static void AddOneFilter(IntPtr engine, string line, Guid sublayerKey, Guid providerKey, IntPtr conditionBlobPtr, List<IntPtr> owned)
|
|
865
|
+
{
|
|
866
|
+
string[] parts = line.Split('|');
|
|
867
|
+
if (parts.Length != 5) throw new ArgumentException("filter 规格行字段数应为 5:" + line);
|
|
868
|
+
Guid filterKey = Guid.Parse(parts[0]);
|
|
869
|
+
string name = parts[1];
|
|
870
|
+
Guid layerKey = Guid.Parse(parts[2]);
|
|
871
|
+
string protocolText = parts[3];
|
|
872
|
+
string portText = parts[4];
|
|
873
|
+
|
|
874
|
+
IntPtr namePtr = Marshal.StringToHGlobalUni(name);
|
|
875
|
+
owned.Add(namePtr);
|
|
876
|
+
IntPtr descPtr = Marshal.StringToHGlobalUni(name + " —— dsc 沙箱账号网络围栏");
|
|
877
|
+
owned.Add(descPtr);
|
|
878
|
+
IntPtr providerKeyPtr = Marshal.AllocHGlobal(16);
|
|
879
|
+
Marshal.StructureToPtr(providerKey, providerKeyPtr, false);
|
|
880
|
+
owned.Add(providerKeyPtr);
|
|
881
|
+
|
|
882
|
+
int conditionCount = 1 + (protocolText.Length > 0 ? 1 : 0) + (portText.Length > 0 ? 1 : 0);
|
|
883
|
+
int conditionSize = Marshal.SizeOf(typeof(FWPM_FILTER_CONDITION0));
|
|
884
|
+
IntPtr conditions = Marshal.AllocHGlobal(conditionSize * conditionCount);
|
|
885
|
+
owned.Add(conditions);
|
|
886
|
+
int index = 0;
|
|
887
|
+
WriteCondition(conditions, conditionSize, index, CondAleUserId, FWP_SECURITY_DESCRIPTOR_TYPE, conditionBlobPtr);
|
|
888
|
+
index++;
|
|
889
|
+
if (protocolText.Length > 0)
|
|
890
|
+
{
|
|
891
|
+
int protocol = Int32.Parse(protocolText, CultureInfo.InvariantCulture);
|
|
892
|
+
WriteCondition(conditions, conditionSize, index, CondIpProtocol, FWP_UINT8, new IntPtr(protocol));
|
|
893
|
+
index++;
|
|
894
|
+
}
|
|
895
|
+
if (portText.Length > 0)
|
|
896
|
+
{
|
|
897
|
+
int port = Int32.Parse(portText, CultureInfo.InvariantCulture);
|
|
898
|
+
WriteCondition(conditions, conditionSize, index, CondIpRemotePort, FWP_UINT16, new IntPtr(port));
|
|
899
|
+
index++;
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
// delete-if-present:同 key 的老 filter 先删(不存在就容忍),保证重跑幂等
|
|
903
|
+
uint del = FwpmFilterDeleteByKey0(engine, ref filterKey);
|
|
904
|
+
if (del != 0 && del != FWP_E_FILTER_NOT_FOUND && del != FWP_E_NOT_FOUND) Fail(del, "FwpmFilterDeleteByKey0(" + name + ")");
|
|
905
|
+
|
|
906
|
+
FWPM_FILTER0 filter = new FWPM_FILTER0();
|
|
907
|
+
filter.filterKey = filterKey;
|
|
908
|
+
filter.displayData.name = namePtr;
|
|
909
|
+
filter.displayData.description = descPtr;
|
|
910
|
+
filter.flags = FWPM_FILTER_FLAG_PERSISTENT;
|
|
911
|
+
filter.providerKey = providerKeyPtr;
|
|
912
|
+
filter.providerData.size = 0;
|
|
913
|
+
filter.providerData.data = IntPtr.Zero;
|
|
914
|
+
filter.layerKey = layerKey;
|
|
915
|
+
filter.subLayerKey = sublayerKey;
|
|
916
|
+
filter.weight.type = FWP_EMPTY; // 不指定权重:由 sublayer 权重 0x8000 决定先于防火墙默认子层被评估
|
|
917
|
+
filter.numFilterConditions = (uint)conditionCount;
|
|
918
|
+
filter.filterCondition = conditions;
|
|
919
|
+
filter.action.type = FWP_ACTION_BLOCK;
|
|
920
|
+
filter.action.action.filterType = Guid.Empty;
|
|
921
|
+
filter.anon.rawContext = 0;
|
|
922
|
+
filter.reserved = IntPtr.Zero;
|
|
923
|
+
filter.filterId = 0;
|
|
924
|
+
filter.effectiveWeight.type = FWP_EMPTY;
|
|
925
|
+
|
|
926
|
+
ulong filterId;
|
|
927
|
+
uint rc = FwpmFilterAdd0(engine, ref filter, IntPtr.Zero, out filterId);
|
|
928
|
+
Fail(rc, "FwpmFilterAdd0(" + name + ")");
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
/// <summary>写一条 FWPM_FILTER_CONDITION0:小数/端口与 SD 指针都落在 union 的同一处。</summary>
|
|
932
|
+
private static void WriteCondition(IntPtr basePtr, int size, int index, Guid fieldKey, int valueType, IntPtr value)
|
|
933
|
+
{
|
|
934
|
+
FWPM_FILTER_CONDITION0 condition = new FWPM_FILTER_CONDITION0();
|
|
935
|
+
condition.fieldKey = fieldKey;
|
|
936
|
+
condition.matchType = FWP_MATCH_EQUAL;
|
|
937
|
+
condition.conditionValue.type = valueType;
|
|
938
|
+
condition.conditionValue.value.sd = value;
|
|
939
|
+
Marshal.StructureToPtr(condition, new IntPtr(basePtr.ToInt64() + (long)index * size), false);
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
/// <summary>读回 SD 的 DACL:ACE 条数 1、掩码 = FWP_ACTRL_MATCH_FILTER、SID = 期望的那个。</summary>
|
|
943
|
+
private static void VerifyUserSid(IntPtr securityDescriptor, IntPtr expectedSid)
|
|
944
|
+
{
|
|
945
|
+
bool daclPresent;
|
|
946
|
+
bool daclDefaulted;
|
|
947
|
+
IntPtr dacl;
|
|
948
|
+
if (!GetSecurityDescriptorDacl(securityDescriptor, out daclPresent, out dacl, out daclDefaulted) || !daclPresent || dacl == IntPtr.Zero)
|
|
949
|
+
{
|
|
950
|
+
throw new InvalidOperationException("构造出的账号 SD 没有 DACL,拒绝继续装 WFP filter");
|
|
951
|
+
}
|
|
952
|
+
ACL_SIZE_INFORMATION aclInfo = new ACL_SIZE_INFORMATION();
|
|
953
|
+
if (!GetAclInformation(dacl, out aclInfo, (uint)Marshal.SizeOf(typeof(ACL_SIZE_INFORMATION)), AclSizeInformation)) throw Win32("GetAclInformation");
|
|
954
|
+
if (aclInfo.AceCount != 1)
|
|
955
|
+
{
|
|
956
|
+
throw new InvalidOperationException("账号 SD 的 ACE 条数应为 1,实际 " + aclInfo.AceCount.ToString(CultureInfo.InvariantCulture));
|
|
957
|
+
}
|
|
958
|
+
IntPtr ace;
|
|
959
|
+
if (!GetAce(dacl, 0, out ace)) throw Win32("GetAce");
|
|
960
|
+
// ACCESS_ALLOWED_ACE:AceType@0(u8)、AceFlags@1(u8)、AceSize@2(u16)、Mask@4(u32)、SidStart@8
|
|
961
|
+
int mask = Marshal.ReadInt32(ace, 4);
|
|
962
|
+
if (mask != (int)FWP_ACTRL_MATCH_FILTER)
|
|
963
|
+
{
|
|
964
|
+
throw new InvalidOperationException("账号 SD 的 ACE 掩码应为 FWP_ACTRL_MATCH_FILTER(1),实际 " + mask.ToString(CultureInfo.InvariantCulture));
|
|
965
|
+
}
|
|
966
|
+
IntPtr aceSid = new IntPtr(ace.ToInt64() + 8);
|
|
967
|
+
if (!EqualSid(aceSid, expectedSid))
|
|
968
|
+
{
|
|
969
|
+
throw new InvalidOperationException("账号 SD 里的 SID 与 Get-LocalUser 报的 SID 不一致:ALE_USER_ID 会指向别的账号,拒绝继续");
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
private static void Fail(uint code, string operation)
|
|
974
|
+
{
|
|
975
|
+
if (code != 0)
|
|
976
|
+
{
|
|
977
|
+
throw new InvalidOperationException(operation + " 失败:0x" + code.ToString("X8", CultureInfo.InvariantCulture));
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
private static Exception Win32(string api)
|
|
982
|
+
{
|
|
983
|
+
return new InvalidOperationException(api + " 失败:Win32 错误码 " + Marshal.GetLastWin32Error().ToString(CultureInfo.InvariantCulture));
|
|
984
|
+
}
|
|
985
|
+
}
|
|
986
|
+
`;
|