omnicell-router 0.8.4 → 0.10.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/README.md +26 -7
- package/README.zh-CN.md +18 -5
- package/backend/dist/capture/recorder.d.ts +4 -10
- package/backend/dist/capture/recorder.js +6 -58
- package/backend/dist/cli/clients/base.d.ts +36 -11
- package/backend/dist/cli/clients/base.js +31 -15
- package/backend/dist/cli/clients/claude.d.ts +9 -0
- package/backend/dist/cli/clients/claude.js +6 -0
- package/backend/dist/cli/clients/codex.d.ts +8 -0
- package/backend/dist/cli/clients/codex.js +5 -0
- package/backend/dist/cli/clients/core-adapters.d.ts +1 -1
- package/backend/dist/cli/clients/core-adapters.js +1 -1
- package/backend/dist/cli/clients/dsh.d.ts +8 -0
- package/backend/dist/cli/clients/dsh.js +5 -0
- package/backend/dist/cli/clients/grok.d.ts +15 -1
- package/backend/dist/cli/clients/grok.js +16 -2
- package/backend/dist/cli/clients/index.d.ts +13 -1
- package/backend/dist/cli/clients/index.js +97 -19
- package/backend/dist/cli/clients/kimi.d.ts +6 -0
- package/backend/dist/cli/clients/kimi.js +3 -0
- package/backend/dist/cli/clients/launcher.d.ts +1 -1
- package/backend/dist/cli/clients/model-meta.js +6 -3
- package/backend/dist/cli/clients/omp.d.ts +6 -0
- package/backend/dist/cli/clients/omp.js +3 -0
- package/backend/dist/cli/clients/openclaw.d.ts +6 -0
- package/backend/dist/cli/clients/openclaw.js +3 -0
- package/backend/dist/cli/clients/opencode.d.ts +8 -0
- package/backend/dist/cli/clients/opencode.js +5 -0
- package/backend/dist/cli/clients/pi.d.ts +6 -0
- package/backend/dist/cli/clients/pi.js +3 -0
- package/backend/dist/cli/clients/update-check.d.ts +37 -0
- package/backend/dist/cli/clients/update-check.js +309 -0
- package/backend/dist/cli/clients/workbuddy.d.ts +19 -0
- package/backend/dist/cli/clients/workbuddy.js +66 -0
- package/backend/dist/cli/index.js +5 -4
- package/backend/dist/cli/paths.js +8 -0
- package/backend/dist/cli/types.d.ts +92 -16
- package/backend/dist/config/comments-en.js +31 -26
- package/backend/dist/config/comments-zh.js +29 -24
- package/backend/dist/config/defaults.d.ts +194 -33
- package/backend/dist/config/defaults.js +193 -32
- package/backend/dist/config/env.d.ts +10 -0
- package/backend/dist/config/env.js +3 -2
- package/backend/dist/config/index.d.ts +7 -0
- package/backend/dist/config/index.js +184 -43
- package/backend/dist/config/migrations/v0-to-v1.d.ts +2 -2
- package/backend/dist/config/migrations/v10-to-v11.d.ts +38 -0
- package/backend/dist/config/migrations/v10-to-v11.js +43 -0
- package/backend/dist/config/migrations/v6-to-v7.d.ts +45 -0
- package/backend/dist/config/migrations/v6-to-v7.js +97 -0
- package/backend/dist/config/migrations/v7-to-v8.d.ts +36 -0
- package/backend/dist/config/migrations/v7-to-v8.js +43 -0
- package/backend/dist/config/migrations/v8-to-v9.d.ts +38 -0
- package/backend/dist/config/migrations/v8-to-v9.js +43 -0
- package/backend/dist/config/migrations/v9-to-v10.d.ts +51 -0
- package/backend/dist/config/migrations/v9-to-v10.js +71 -0
- package/backend/dist/config/runtime.js +1 -1
- package/backend/dist/config/schema.d.ts +3 -3
- package/backend/dist/config/schema.js +14 -4
- package/backend/dist/config/types.d.ts +167 -38
- package/backend/dist/egress-proxy/asset-download.d.ts +54 -0
- package/backend/dist/egress-proxy/asset-download.js +420 -0
- package/backend/dist/egress-proxy/manual-node-spec.d.ts +92 -0
- package/backend/dist/egress-proxy/manual-node-spec.js +408 -0
- package/backend/dist/egress-proxy/mihomo-binary.d.ts +54 -0
- package/backend/dist/egress-proxy/mihomo-binary.js +278 -0
- package/backend/dist/egress-proxy/mihomo-client.d.ts +94 -0
- package/backend/dist/egress-proxy/mihomo-client.js +308 -0
- package/backend/dist/egress-proxy/mihomo-config.d.ts +40 -0
- package/backend/dist/egress-proxy/mihomo-config.js +161 -0
- package/backend/dist/egress-proxy/mihomo-geo.d.ts +87 -0
- package/backend/dist/egress-proxy/mihomo-geo.js +213 -0
- package/backend/dist/egress-proxy/mihomo-logs.d.ts +43 -0
- package/backend/dist/egress-proxy/mihomo-logs.js +84 -0
- package/backend/dist/egress-proxy/mihomo-pac.d.ts +37 -0
- package/backend/dist/egress-proxy/mihomo-pac.js +75 -0
- package/backend/dist/egress-proxy/mihomo-rules-import.d.ts +43 -0
- package/backend/dist/egress-proxy/mihomo-rules-import.js +193 -0
- package/backend/dist/egress-proxy/mihomo-sidecar.d.ts +81 -0
- package/backend/dist/egress-proxy/mihomo-sidecar.js +637 -0
- package/backend/dist/forward-proxy/egress.d.ts +50 -11
- package/backend/dist/forward-proxy/egress.js +50 -17
- package/backend/dist/forward-proxy/h2-forward.js +36 -10
- package/backend/dist/forward-proxy/index.d.ts +11 -8
- package/backend/dist/forward-proxy/index.js +22 -60
- package/backend/dist/forward-proxy/interceptor.d.ts +3 -2
- package/backend/dist/forward-proxy/interceptor.js +4 -4
- package/backend/dist/forward-proxy/relay.js +3 -3
- package/backend/dist/forward-proxy/rules.d.ts +4 -4
- package/backend/dist/forward-proxy/rules.js +25 -4
- package/backend/dist/forward-proxy/server.d.ts +0 -16
- package/backend/dist/forward-proxy/server.js +124 -156
- package/backend/dist/harness/host/hosts.js +3 -2
- package/backend/dist/index.js +53 -18
- package/backend/dist/ingress-proxy/access-log.d.ts +176 -0
- package/backend/dist/ingress-proxy/access-log.js +774 -0
- package/backend/dist/ingress-proxy/ca-inventory.d.ts +106 -0
- package/backend/dist/ingress-proxy/ca-inventory.js +525 -0
- package/backend/dist/ingress-proxy/ca-trust.d.ts +96 -0
- package/backend/dist/ingress-proxy/ca-trust.js +315 -0
- package/backend/dist/ingress-proxy/ca.d.ts +104 -0
- package/backend/dist/ingress-proxy/ca.js +273 -0
- package/backend/dist/ingress-proxy/data-dir.d.ts +28 -0
- package/backend/dist/ingress-proxy/data-dir.js +45 -0
- package/backend/dist/ingress-proxy/egress.d.ts +151 -0
- package/backend/dist/ingress-proxy/egress.js +364 -0
- package/backend/dist/ingress-proxy/h2-forward.d.ts +66 -0
- package/backend/dist/ingress-proxy/h2-forward.js +561 -0
- package/backend/dist/ingress-proxy/index.d.ts +24 -0
- package/backend/dist/ingress-proxy/index.js +144 -0
- package/backend/dist/ingress-proxy/interceptor.d.ts +25 -0
- package/backend/dist/ingress-proxy/interceptor.js +52 -0
- package/backend/dist/ingress-proxy/ordered-relay.d.ts +29 -0
- package/backend/dist/ingress-proxy/ordered-relay.js +46 -0
- package/backend/dist/ingress-proxy/relay.d.ts +54 -0
- package/backend/dist/ingress-proxy/relay.js +188 -0
- package/backend/dist/ingress-proxy/rules.d.ts +195 -0
- package/backend/dist/ingress-proxy/rules.js +348 -0
- package/backend/dist/ingress-proxy/server.d.ts +153 -0
- package/backend/dist/ingress-proxy/server.js +2065 -0
- package/backend/dist/ingress-proxy/sni.d.ts +35 -0
- package/backend/dist/ingress-proxy/sni.js +124 -0
- package/backend/dist/ingress-proxy/status.d.ts +32 -0
- package/backend/dist/ingress-proxy/status.js +15 -0
- package/backend/dist/ingress-proxy/system-proxy.d.ts +112 -0
- package/backend/dist/ingress-proxy/system-proxy.js +481 -0
- package/backend/dist/ingress-proxy/traffic-delete.d.ts +21 -0
- package/backend/dist/ingress-proxy/traffic-delete.js +64 -0
- package/backend/dist/ingress-proxy/traffic-delete.worker.d.ts +44 -0
- package/backend/dist/ingress-proxy/traffic-delete.worker.js +228 -0
- package/backend/dist/ingress-proxy/traffic-files.d.ts +10 -0
- package/backend/dist/ingress-proxy/traffic-files.js +10 -0
- package/backend/dist/ingress-proxy/upstream-tls.d.ts +46 -0
- package/backend/dist/ingress-proxy/upstream-tls.js +105 -0
- package/backend/dist/ingress-proxy/ws-frames.d.ts +45 -0
- package/backend/dist/ingress-proxy/ws-frames.js +153 -0
- package/backend/dist/ir/adapters/anthropic-messages-stream.js +6 -1
- package/backend/dist/ir/adapters/anthropic-messages.js +12 -5
- package/backend/dist/ir/adapters/google-gemini-stream.js +7 -2
- package/backend/dist/ir/adapters/google-gemini.js +5 -2
- package/backend/dist/ir/adapters/openai-chat-stream.js +5 -1
- package/backend/dist/ir/adapters/openai-chat.d.ts +25 -0
- package/backend/dist/ir/adapters/openai-chat.js +45 -3
- package/backend/dist/ir/adapters/openai-responses-output.d.ts +1 -0
- package/backend/dist/ir/adapters/openai-responses-output.js +67 -21
- package/backend/dist/ir/adapters/openai-responses-stream.d.ts +13 -0
- package/backend/dist/ir/adapters/openai-responses-stream.js +34 -1
- package/backend/dist/ir/adapters/openai-responses.d.ts +82 -24
- package/backend/dist/ir/adapters/openai-responses.js +146 -37
- package/backend/dist/ir/media.d.ts +20 -0
- package/backend/dist/ir/media.js +41 -0
- package/backend/dist/ir/tool-identity.d.ts +35 -0
- package/backend/dist/ir/tool-identity.js +40 -0
- package/backend/dist/ir/types.d.ts +9 -1
- package/backend/dist/local/build-jobs.d.ts +66 -0
- package/backend/dist/local/build-jobs.js +243 -0
- package/backend/dist/local/detect.d.ts +32 -0
- package/backend/dist/local/detect.js +145 -0
- package/backend/dist/local/draft-match.d.ts +58 -0
- package/backend/dist/local/draft-match.js +124 -0
- package/backend/dist/local/engine-runtime.d.ts +522 -0
- package/backend/dist/local/engine-runtime.js +1 -0
- package/backend/dist/local/engines.d.ts +241 -0
- package/backend/dist/local/engines.js +651 -0
- package/backend/dist/local/gguf.d.ts +35 -0
- package/backend/dist/local/gguf.js +314 -0
- package/backend/dist/local/hf.d.ts +120 -0
- package/backend/dist/local/hf.js +304 -0
- package/backend/dist/local/install.d.ts +90 -0
- package/backend/dist/local/install.js +421 -0
- package/backend/dist/local/llamacpp.d.ts +55 -0
- package/backend/dist/local/llamacpp.js +177 -0
- package/backend/dist/local/local-provider.d.ts +81 -0
- package/backend/dist/local/local-provider.js +301 -0
- package/backend/dist/local/machine-stats.d.ts +87 -0
- package/backend/dist/local/machine-stats.js +332 -0
- package/backend/dist/local/model-store.d.ts +347 -0
- package/backend/dist/local/model-store.js +1215 -0
- package/backend/dist/local/part-sweep.d.ts +32 -0
- package/backend/dist/local/part-sweep.js +156 -0
- package/backend/dist/local/plan.d.ts +65 -0
- package/backend/dist/local/plan.js +146 -0
- package/backend/dist/local/resources.d.ts +49 -0
- package/backend/dist/local/resources.js +64 -0
- package/backend/dist/local/run-stats.d.ts +95 -0
- package/backend/dist/local/run-stats.js +195 -0
- package/backend/dist/local/runner.d.ts +235 -0
- package/backend/dist/local/runner.js +1392 -0
- package/backend/dist/local/settings.d.ts +79 -0
- package/backend/dist/local/settings.js +180 -0
- package/backend/dist/local/strata-catalog.d.ts +30 -0
- package/backend/dist/local/strata-catalog.js +511 -0
- package/backend/dist/local/strata-drafting.d.ts +46 -0
- package/backend/dist/local/strata-drafting.js +265 -0
- package/backend/dist/local/strata.d.ts +278 -0
- package/backend/dist/local/strata.js +1392 -0
- package/backend/dist/local/tar.d.ts +20 -0
- package/backend/dist/local/tar.js +367 -0
- package/backend/dist/local/transport.d.ts +35 -0
- package/backend/dist/local/transport.js +89 -0
- package/backend/dist/local/tune.d.ts +58 -0
- package/backend/dist/local/tune.js +324 -0
- package/backend/dist/observability/http-exchange.d.ts +2 -2
- package/backend/dist/opencode/catalog/auto-pull.d.ts +14 -3
- package/backend/dist/opencode/catalog/auto-pull.js +276 -70
- package/backend/dist/opencode/catalog/ocr-store.js +3 -1
- package/backend/dist/opencode/catalog/overrides-store.d.ts +10 -1
- package/backend/dist/opencode/catalog/overrides-store.js +6 -0
- package/backend/dist/opencode/catalog/reasoning-declaration.d.ts +35 -0
- package/backend/dist/opencode/catalog/reasoning-declaration.js +66 -0
- package/backend/dist/opencode/catalog/repository.js +25 -2
- package/backend/dist/opencode/catalog/sources/models-dev.js +13 -3
- package/backend/dist/opencode/catalog/types.d.ts +35 -3
- package/backend/dist/opencode/catalog/types.js +38 -1
- package/backend/dist/opencode/credentials/ocr-store.js +11 -2
- package/backend/dist/opencode/credentials/sync.js +12 -3
- package/backend/dist/opencode/credentials/types.d.ts +31 -4
- package/backend/dist/opencode/credentials/types.js +25 -0
- package/backend/dist/opencode/probe.d.ts +21 -4
- package/backend/dist/opencode/probe.js +34 -10
- package/backend/dist/pipeline/directives.d.ts +9 -4
- package/backend/dist/pipeline/directives.js +14 -10
- package/backend/dist/pipeline/orchestrator.d.ts +42 -13
- package/backend/dist/pipeline/orchestrator.js +497 -165
- package/backend/dist/plugins/environment-ack.d.ts +51 -0
- package/backend/dist/plugins/environment-ack.js +81 -0
- package/backend/dist/plugins/gateways.d.ts +6 -6
- package/backend/dist/plugins/gateways.js +2 -2
- package/backend/dist/plugins/host.d.ts +98 -14
- package/backend/dist/plugins/host.js +622 -87
- package/backend/dist/plugins/install-run.d.ts +27 -0
- package/backend/dist/plugins/install-run.js +99 -0
- package/backend/dist/plugins/manifest.d.ts +84 -32
- package/backend/dist/plugins/manifest.js +149 -45
- package/backend/dist/plugins/market-install.d.ts +18 -0
- package/backend/dist/plugins/market-install.js +111 -0
- package/backend/dist/plugins/market-source.d.ts +100 -0
- package/backend/dist/plugins/market-source.js +449 -0
- package/backend/dist/plugins/market-updates.d.ts +70 -0
- package/backend/dist/plugins/market-updates.js +97 -0
- package/backend/dist/plugins/market.d.ts +677 -0
- package/backend/dist/plugins/market.js +2113 -0
- package/backend/dist/plugins/source-index.d.ts +155 -0
- package/backend/dist/plugins/source-index.js +217 -0
- package/backend/dist/plugins/types.d.ts +402 -37
- package/backend/dist/projects/worktrees.d.ts +88 -0
- package/backend/dist/projects/worktrees.js +220 -0
- package/backend/dist/providers/boot-direct.d.ts +41 -7
- package/backend/dist/providers/boot-direct.js +252 -36
- package/backend/dist/providers/compat/tool-markup-recovery.js +31 -6
- package/backend/dist/providers/executors/dispatch.d.ts +17 -7
- package/backend/dist/providers/executors/dispatch.js +28 -9
- package/backend/dist/providers/executors/openai-chat.js +14 -2
- package/backend/dist/providers/executors/plugin-adapter.d.ts +23 -1
- package/backend/dist/providers/executors/plugin-adapter.js +59 -3
- package/backend/dist/providers/member-match.d.ts +32 -9
- package/backend/dist/providers/member-match.js +65 -21
- package/backend/dist/providers/model-resolution.d.ts +62 -0
- package/backend/dist/providers/model-resolution.js +62 -0
- package/backend/dist/providers/price-order.d.ts +51 -0
- package/backend/dist/providers/price-order.js +54 -0
- package/backend/dist/providers/provider-takedown.d.ts +5 -2
- package/backend/dist/providers/registry.d.ts +84 -25
- package/backend/dist/providers/registry.js +209 -114
- package/backend/dist/providers/selection.d.ts +91 -0
- package/backend/dist/providers/selection.js +158 -0
- package/backend/dist/providers/virtual-meta.d.ts +33 -2
- package/backend/dist/providers/virtual-meta.js +74 -18
- package/backend/dist/quota/cli-login.d.ts +25 -4
- package/backend/dist/quota/cli-login.js +35 -5
- package/backend/dist/quota/coverage-generation.d.ts +24 -0
- package/backend/dist/quota/coverage-generation.js +29 -0
- package/backend/dist/quota/declarations.js +8 -4
- package/backend/dist/quota/engine.js +2 -2
- package/backend/dist/quota/prober.d.ts +43 -2
- package/backend/dist/quota/prober.js +97 -22
- package/backend/dist/ratelimit/rate-limiter.d.ts +1 -1
- package/backend/dist/resilience/error-classifier.js +29 -0
- package/backend/dist/router/index.d.ts +1 -1
- package/backend/dist/router/index.js +17 -1
- package/backend/dist/router/layer2-decision.d.ts +1 -1
- package/backend/dist/router/layer2-decision.js +2 -2
- package/backend/dist/routes/console.js +928 -310
- package/backend/dist/routes/environments.js +1 -1
- package/backend/dist/routes/local-models.d.ts +2 -0
- package/backend/dist/routes/local-models.js +1414 -0
- package/backend/dist/routes/mihomo.d.ts +2 -2
- package/backend/dist/routes/mihomo.js +20 -11
- package/backend/dist/routes/projects.js +121 -0
- package/backend/dist/server.js +318 -13
- package/backend/dist/session/session-manager.d.ts +42 -5
- package/backend/dist/session/session-manager.js +64 -11
- package/backend/dist/session/session-row.d.ts +29 -8
- package/backend/dist/session/session-row.js +18 -13
- package/backend/dist/snapshot/bundle.d.ts +40 -0
- package/backend/dist/snapshot/bundle.js +177 -0
- package/backend/dist/snapshot/handoff/compress.d.ts +46 -0
- package/backend/dist/snapshot/handoff/compress.js +77 -0
- package/backend/dist/snapshot/handoff/index.d.ts +119 -0
- package/backend/dist/snapshot/handoff/index.js +160 -0
- package/backend/dist/snapshot/handoff/jsonl-writer.d.ts +78 -0
- package/backend/dist/snapshot/handoff/jsonl-writer.js +175 -0
- package/backend/dist/snapshot/handoff/opencode-writer.d.ts +51 -0
- package/backend/dist/snapshot/handoff/opencode-writer.js +214 -0
- package/backend/dist/snapshot/handoff/summarize.d.ts +39 -0
- package/backend/dist/snapshot/handoff/summarize.js +118 -0
- package/backend/dist/snapshot/handoff/transcript.d.ts +71 -0
- package/backend/dist/snapshot/handoff/transcript.js +167 -0
- package/backend/dist/snapshot/media-fs.d.ts +10 -0
- package/backend/dist/snapshot/media-fs.js +50 -0
- package/backend/dist/snapshot/media.d.ts +32 -0
- package/backend/dist/snapshot/media.js +87 -0
- package/backend/dist/snapshot/store.d.ts +226 -0
- package/backend/dist/snapshot/store.js +887 -0
- package/backend/dist/snapshot/tree.d.ts +112 -0
- package/backend/dist/snapshot/tree.js +207 -0
- package/backend/dist/snapshot/types.d.ts +212 -0
- package/backend/dist/snapshot/types.js +14 -0
- package/backend/dist/snapshot/view.d.ts +19 -0
- package/backend/dist/snapshot/view.js +57 -0
- package/backend/dist/snapshot/write-worker.d.ts +67 -0
- package/backend/dist/snapshot/write-worker.js +129 -0
- package/backend/dist/surfaces/body-admission.d.ts +67 -2
- package/backend/dist/surfaces/body-admission.js +218 -13
- package/backend/dist/surfaces/chat-pipeline.js +8 -0
- package/backend/dist/surfaces/decision-pipeline.js +10 -7
- package/backend/dist/surfaces/gateway-catalog.d.ts +26 -0
- package/backend/dist/surfaces/gateway-catalog.js +202 -0
- package/backend/dist/surfaces/gateway-chat.d.ts +7 -11
- package/backend/dist/surfaces/gateway-chat.js +50 -12
- package/backend/dist/surfaces/ocr-headers.d.ts +6 -4
- package/backend/dist/surfaces/ocr-headers.js +11 -7
- package/backend/dist/surfaces/systemone.js +1 -1
- package/backend/dist/surfaces/transport-fastify.js +32 -5
- package/backend/dist/surfaces/transport-node-http.d.ts +9 -3
- package/backend/dist/surfaces/transport-node-http.js +6 -19
- package/backend/dist/surfaces/types.d.ts +11 -3
- package/backend/dist/trace/persist.js +21 -1
- package/backend/dist/trace/tracker.d.ts +35 -3
- package/backend/dist/trace/tracker.js +23 -0
- package/backend/dist/types/openai.d.ts +8 -0
- package/backend/dist/types/router.d.ts +17 -6
- package/backend/dist/types/router.js +14 -2
- package/backend/dist/utils/client-families.d.ts +66 -0
- package/backend/dist/utils/client-families.js +127 -0
- package/backend/dist/utils/event-ring.d.ts +1 -1
- package/backend/dist/utils/event-ring.js +1 -1
- package/backend/dist/utils/proxy-node.js +1 -1
- package/backend/dist/utils/proxy.d.ts +135 -11
- package/backend/dist/utils/proxy.js +144 -29
- package/backend/dist/utils/run-command.d.ts +21 -1
- package/backend/dist/utils/run-command.js +64 -4
- package/backend/dist/utils/server-liveness.d.ts +3 -2
- package/backend/dist/utils/server-liveness.js +3 -2
- package/backend/dist/utils/session-file.d.ts +28 -0
- package/backend/dist/utils/session-file.js +34 -0
- package/frontend/dist/assets/antigravity.color-DXFfVr6X.svg +39 -0
- package/frontend/dist/assets/gemini-cli.color-BfquLd1p.svg +1 -0
- package/frontend/dist/assets/index-CTZNvn1d.js +1707 -0
- package/frontend/dist/assets/{index-KI1OW5x6.css → index-CqDSOPQ0.css} +1 -1
- package/frontend/dist/index.html +2 -2
- package/package.json +3 -1
- package/frontend/dist/assets/index-Dk76uc7e.js +0 -1522
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { execFile } from 'node:child_process';
|
|
4
|
+
import { promisify } from 'node:util';
|
|
5
|
+
import { getOcrHomeDir } from '../cli/paths.js';
|
|
6
|
+
import { INGRESS_PROXY_DATA_DIR } from './data-dir.js';
|
|
7
|
+
import { getRuntimeConfigRef } from '../config/index.js';
|
|
8
|
+
import { caCertPath } from './ca.js';
|
|
9
|
+
/**
|
|
10
|
+
* Cross-platform OS system-proxy ownership for OCR's two local endpoints: the
|
|
11
|
+
* inbound ingress proxy (`owner: 'forward'`) and the Clash core's mixed port
|
|
12
|
+
* (`owner: 'clash'`).
|
|
13
|
+
*
|
|
14
|
+
* Model — ONE owner, snapshot-and-restore, never blind-off:
|
|
15
|
+
* - `enable` reads the CURRENT OS proxy settings (whatever they point at —
|
|
16
|
+
* including a user's clash/v2ray), snapshots them to
|
|
17
|
+
* `<ocrHome>/ingress-proxy/system-proxy.json`, then points the OS proxy at
|
|
18
|
+
* 127.0.0.1:<port> with a bypass list.
|
|
19
|
+
* - There is exactly ONE snapshot slot, and it always holds the PRE-OCR
|
|
20
|
+
* state. Re-enabling the same endpoint refreshes; enabling the OTHER
|
|
21
|
+
* endpoint REWRITES the OS pointer and updates the snapshot's `owner` —
|
|
22
|
+
* it never re-reads the OS, whose values at that moment are ours.
|
|
23
|
+
* - `disable` restores the snapshot verbatim and deletes it, whichever owner
|
|
24
|
+
* set it. Without a snapshot OCR owns nothing, so disable is a NO-OP — it
|
|
25
|
+
* never blind-offs a proxy it did not set (a user's clash/v2ray keeps
|
|
26
|
+
* working).
|
|
27
|
+
* - The snapshot file doubles as crash detection: if it exists while the OS
|
|
28
|
+
* no longer points at us (or after a hard kill), the state reads `stale`
|
|
29
|
+
* and the console offers one-click restore.
|
|
30
|
+
*
|
|
31
|
+
* The bypass list differs BY OWNER, because the endpoints route differently:
|
|
32
|
+
* the inbound proxy decides ingress per rule (unmatched traffic is tunneled
|
|
33
|
+
* through), so only loopback + literal `direct` hosts are bypassed; the Clash
|
|
34
|
+
* core's generated config ends in `MATCH,<group>`, so private/LAN ranges must
|
|
35
|
+
* also stay DIRECT or they would be dragged into the tunnel.
|
|
36
|
+
*
|
|
37
|
+
* Platforms: win32 (WinINET registry), darwin (networksetup, every enabled
|
|
38
|
+
* network service), linux with GNOME/gsettings. Anything else reports
|
|
39
|
+
* `unsupported` — the UI hides the controls instead of pretending.
|
|
40
|
+
*/
|
|
41
|
+
const exec = promisify(execFile);
|
|
42
|
+
const snapshotPath = () => path.join(getOcrHomeDir(), INGRESS_PROXY_DATA_DIR, 'system-proxy.json');
|
|
43
|
+
function readSnapshot() {
|
|
44
|
+
try {
|
|
45
|
+
const raw = fs.readFileSync(snapshotPath(), 'utf8');
|
|
46
|
+
const parsed = JSON.parse(raw);
|
|
47
|
+
if (!parsed.owner)
|
|
48
|
+
parsed.owner = 'forward';
|
|
49
|
+
return parsed;
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function writeSnapshot(s) {
|
|
56
|
+
fs.mkdirSync(path.dirname(snapshotPath()), { recursive: true });
|
|
57
|
+
fs.writeFileSync(snapshotPath(), JSON.stringify(s, null, 2));
|
|
58
|
+
}
|
|
59
|
+
function clearSnapshot() {
|
|
60
|
+
try {
|
|
61
|
+
fs.unlinkSync(snapshotPath());
|
|
62
|
+
}
|
|
63
|
+
catch { }
|
|
64
|
+
}
|
|
65
|
+
/** Loopback + the rule table's literal `direct` hosts, in the platform's override syntax. */
|
|
66
|
+
export function buildOverride(bypassList, platform) {
|
|
67
|
+
const hosts = ['localhost', '127.0.0.1', '<local>'];
|
|
68
|
+
for (const h of bypassList) {
|
|
69
|
+
if (h)
|
|
70
|
+
hosts.push(h);
|
|
71
|
+
}
|
|
72
|
+
return platform === 'win32' ? hosts.join(';') : JSON.stringify(hosts);
|
|
73
|
+
}
|
|
74
|
+
export function planSystemProxyAction(snapshot, targetServer) {
|
|
75
|
+
if (!snapshot)
|
|
76
|
+
return 'snapshot';
|
|
77
|
+
return snapshot.targetServer === targetServer ? 'refresh' : 'switch';
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Private / link-local / CGNAT ranges that stay DIRECT when the OS proxy points
|
|
81
|
+
* at the Clash mixed port. Same set as the PAC's direct list
|
|
82
|
+
* (proxy/mihomo-pac.ts `PRIVATE_RANGES`), in each bypass syntax: WinINET takes
|
|
83
|
+
* `x.*` wildcards, the networksetup / gsettings lists take CIDR.
|
|
84
|
+
*/
|
|
85
|
+
const LAN_BYPASS_WILDCARDS = [
|
|
86
|
+
'10.*',
|
|
87
|
+
'172.16.*', '172.17.*', '172.18.*', '172.19.*',
|
|
88
|
+
'172.20.*', '172.21.*', '172.22.*', '172.23.*',
|
|
89
|
+
'172.24.*', '172.25.*', '172.26.*', '172.27.*',
|
|
90
|
+
'172.28.*', '172.29.*', '172.30.*', '172.31.*',
|
|
91
|
+
'192.168.*',
|
|
92
|
+
'169.254.*',
|
|
93
|
+
'100.64.*',
|
|
94
|
+
];
|
|
95
|
+
const LAN_BYPASS_CIDR = [
|
|
96
|
+
'10.0.0.0/8',
|
|
97
|
+
'172.16.0.0/12',
|
|
98
|
+
'192.168.0.0/16',
|
|
99
|
+
'169.254.0.0/16',
|
|
100
|
+
'100.64.0.0/10',
|
|
101
|
+
];
|
|
102
|
+
export function lanBypassHosts(platform) {
|
|
103
|
+
return [...(platform === 'win32' ? LAN_BYPASS_WILDCARDS : LAN_BYPASS_CIDR)];
|
|
104
|
+
}
|
|
105
|
+
/* ── win32 ─────────────────────────────────────────────────────────────── */
|
|
106
|
+
const WIN_KEY = 'HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings';
|
|
107
|
+
/**
|
|
108
|
+
* Normalize the Windows Internet Settings into the single server the OS is
|
|
109
|
+
* ACTUALLY using:
|
|
110
|
+
* - `ProxyEnable=0` means the `ProxyServer` value is stale → nothing;
|
|
111
|
+
* - `AutoConfigURL` (PAC) wins over the manual server when present;
|
|
112
|
+
* - WinINET's per-scheme form (`http=host:port;https=host:port`) reduces to
|
|
113
|
+
* one `host:port` (https preferred) so ownership comparison and the port
|
|
114
|
+
* label see a real endpoint.
|
|
115
|
+
*/
|
|
116
|
+
export function normalizeProxyServer(raw, enabled, pac) {
|
|
117
|
+
if (pac.trim())
|
|
118
|
+
return pac.trim();
|
|
119
|
+
if (!enabled)
|
|
120
|
+
return undefined;
|
|
121
|
+
const value = raw.trim();
|
|
122
|
+
if (!value)
|
|
123
|
+
return undefined;
|
|
124
|
+
const https = /(?:^|;)https=([^;]+)/i.exec(value)?.[1];
|
|
125
|
+
const http = /(?:^|;)http=([^;]+)/i.exec(value)?.[1];
|
|
126
|
+
return (https || http || value.split(';')[0]).trim() || undefined;
|
|
127
|
+
}
|
|
128
|
+
async function winRead() {
|
|
129
|
+
const { stdout } = await exec('reg', ['query', WIN_KEY]);
|
|
130
|
+
const get = (name) => {
|
|
131
|
+
const m = new RegExp(`\\s${name}\\s+REG_(SZ|DWORD)\\s+(.*)`, 'i').exec(stdout);
|
|
132
|
+
return m ? m[2].trim() : '';
|
|
133
|
+
};
|
|
134
|
+
const hex = (v) => (v.startsWith('0x') ? parseInt(v, 16) : parseInt(v, 10) || 0);
|
|
135
|
+
return {
|
|
136
|
+
ProxyEnable: hex(get('ProxyEnable')),
|
|
137
|
+
ProxyServer: get('ProxyServer'),
|
|
138
|
+
ProxyOverride: get('ProxyOverride'),
|
|
139
|
+
AutoConfigURL: get('AutoConfigURL'),
|
|
140
|
+
_server: normalizeProxyServer(get('ProxyServer'), hex(get('ProxyEnable')), get('AutoConfigURL')),
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
async function winSet(server, override) {
|
|
144
|
+
const args = (v, t, d) => ['add', WIN_KEY, '/v', v, '/t', t, '/d', d, '/f'];
|
|
145
|
+
await exec('reg', args('ProxyEnable', 'REG_DWORD', '1'));
|
|
146
|
+
await exec('reg', args('ProxyServer', 'REG_SZ', server));
|
|
147
|
+
await exec('reg', args('ProxyOverride', 'REG_SZ', override));
|
|
148
|
+
// A PAC URL wins over ProxyEnable/ProxyServer — park it while we own the proxy.
|
|
149
|
+
await exec('reg', ['delete', WIN_KEY, '/v', 'AutoConfigURL', '/f']).catch(() => { });
|
|
150
|
+
}
|
|
151
|
+
async function winRestore(s) {
|
|
152
|
+
const args = (v, t, d) => ['add', WIN_KEY, '/v', v, '/t', t, '/d', d, '/f'];
|
|
153
|
+
await exec('reg', args('ProxyEnable', 'REG_DWORD', String(s.ProxyEnable)));
|
|
154
|
+
await exec('reg', args('ProxyServer', 'REG_SZ', s.ProxyServer));
|
|
155
|
+
await exec('reg', args('ProxyOverride', 'REG_SZ', s.ProxyOverride));
|
|
156
|
+
if (s.AutoConfigURL)
|
|
157
|
+
await exec('reg', args('AutoConfigURL', 'REG_SZ', s.AutoConfigURL)).catch(() => { });
|
|
158
|
+
}
|
|
159
|
+
/* ── darwin ────────────────────────────────────────────────────────────── */
|
|
160
|
+
async function macServices() {
|
|
161
|
+
const { stdout } = await exec('networksetup', ['-listallnetworkservices']);
|
|
162
|
+
return stdout
|
|
163
|
+
.split('\n')
|
|
164
|
+
.slice(1) // first line is the legend
|
|
165
|
+
.map(l => l.replace(/^\*/, '').trim())
|
|
166
|
+
.filter(l => l && !l.includes('(disabled)'));
|
|
167
|
+
}
|
|
168
|
+
async function macRead(service) {
|
|
169
|
+
const trip = async (kind) => {
|
|
170
|
+
const { stdout } = await exec('networksetup', [`-get${kind}proxy`, service]);
|
|
171
|
+
const enabled = /Enabled: Yes/.test(stdout);
|
|
172
|
+
const host = /Server: (.*)/.exec(stdout)?.[1]?.trim() ?? '';
|
|
173
|
+
const port = parseInt(/Port: (\d+)/.exec(stdout)?.[1] ?? '0', 10) || 0;
|
|
174
|
+
return { enabled, host, port };
|
|
175
|
+
};
|
|
176
|
+
const { stdout } = await exec('networksetup', ['-getproxybypassdomains', service]);
|
|
177
|
+
const bypass = stdout.split('\n').map(l => l.trim()).filter(l => l && !l.startsWith('There aren'));
|
|
178
|
+
return { service, web: await trip('web'), secure: await trip('secure'), bypass };
|
|
179
|
+
}
|
|
180
|
+
async function macSet(service, server, overrideHosts) {
|
|
181
|
+
const [host, port] = server.split(':');
|
|
182
|
+
await exec('networksetup', ['-setwebproxy', service, host, port]);
|
|
183
|
+
await exec('networksetup', ['-setsecurewebproxy', service, host, port]);
|
|
184
|
+
await exec('networksetup', ['-setproxybypassdomains', service, ...overrideHosts]);
|
|
185
|
+
}
|
|
186
|
+
async function macRestore(s) {
|
|
187
|
+
const svc = s.service;
|
|
188
|
+
if (s.web.enabled && s.web.host) {
|
|
189
|
+
await exec('networksetup', ['-setwebproxy', svc, s.web.host, String(s.web.port)]);
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
await exec('networksetup', ['-setwebproxystate', svc, 'off']);
|
|
193
|
+
}
|
|
194
|
+
if (s.secure.enabled && s.secure.host) {
|
|
195
|
+
await exec('networksetup', ['-setsecurewebproxy', svc, s.secure.host, String(s.secure.port)]);
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
await exec('networksetup', ['-setsecurewebproxystate', svc, 'off']);
|
|
199
|
+
}
|
|
200
|
+
if (s.bypass.length) {
|
|
201
|
+
await exec('networksetup', ['-setproxybypassdomains', svc, ...s.bypass]);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
/* ── linux GNOME ───────────────────────────────────────────────────────── */
|
|
205
|
+
async function gnomeRead() {
|
|
206
|
+
const g = async (key) => {
|
|
207
|
+
const { stdout } = await exec('gsettings', ['get', 'org.gnome.system.proxy', key]);
|
|
208
|
+
return stdout.trim();
|
|
209
|
+
};
|
|
210
|
+
const parseHost = (v) => (/^'(.*)'$/.exec(v)?.[1] ?? v.replace(/^'/, '').replace(/'$/, ''));
|
|
211
|
+
const trip = async (kind) => ({
|
|
212
|
+
host: parseHost(await g(`${kind}.host`)),
|
|
213
|
+
port: parseInt(await g(`${kind}.port`), 10) || 0,
|
|
214
|
+
});
|
|
215
|
+
const ignoreRaw = await g('ignore-hosts');
|
|
216
|
+
let ignoreHosts = [];
|
|
217
|
+
try {
|
|
218
|
+
ignoreHosts = JSON.parse(ignoreRaw.replace(/^@as /, ''));
|
|
219
|
+
}
|
|
220
|
+
catch { }
|
|
221
|
+
return { mode: parseHost(await g('mode')), http: await trip('http'), https: await trip('https'), ignoreHosts };
|
|
222
|
+
}
|
|
223
|
+
async function gnomeSet(server, ignoreHosts) {
|
|
224
|
+
const [host, port] = server.split(':');
|
|
225
|
+
const s = async (key, val) => {
|
|
226
|
+
await exec('gsettings', ['set', 'org.gnome.system.proxy', key, val]);
|
|
227
|
+
};
|
|
228
|
+
await s('mode', "'manual'");
|
|
229
|
+
await s('http.host', `'${host}'`);
|
|
230
|
+
await s('http.port', port);
|
|
231
|
+
await s('https.host', `'${host}'`);
|
|
232
|
+
await s('https.port', port);
|
|
233
|
+
await s('ignore-hosts', JSON.stringify(ignoreHosts));
|
|
234
|
+
}
|
|
235
|
+
async function gnomeRestore(snap) {
|
|
236
|
+
const set = async (key, val) => {
|
|
237
|
+
await exec('gsettings', ['set', 'org.gnome.system.proxy', key, val]);
|
|
238
|
+
};
|
|
239
|
+
await set('mode', `'${snap.mode === 'manual' ? 'manual' : 'none'}'`);
|
|
240
|
+
if (snap.mode === 'manual') {
|
|
241
|
+
await set('http.host', `'${snap.http.host}'`);
|
|
242
|
+
await set('http.port', String(snap.http.port));
|
|
243
|
+
await set('https.host', `'${snap.https.host}'`);
|
|
244
|
+
await set('https.port', String(snap.https.port));
|
|
245
|
+
await set('ignore-hosts', JSON.stringify(snap.ignoreHosts));
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
/* ── public surface ────────────────────────────────────────────────────── */
|
|
249
|
+
function platformSupported(platform) {
|
|
250
|
+
return platform === 'win32' || platform === 'darwin' || (platform === 'linux' && Boolean(process.env.XDG_CURRENT_DESKTOP && /gnome|unity|cinnamon/i.test(process.env.XDG_CURRENT_DESKTOP)));
|
|
251
|
+
}
|
|
252
|
+
/** Current snapshot server (the one we set), for ownership comparison. */
|
|
253
|
+
async function readCurrentServer(platform) {
|
|
254
|
+
try {
|
|
255
|
+
if (platform === 'win32')
|
|
256
|
+
return (await winRead())._server || undefined;
|
|
257
|
+
if (platform === 'darwin') {
|
|
258
|
+
// First enabled service is enough for a preflight: a proxy that owns the
|
|
259
|
+
// machine is normally set on the active service too.
|
|
260
|
+
const svc = (await macServices())[0];
|
|
261
|
+
if (!svc)
|
|
262
|
+
return undefined;
|
|
263
|
+
const s = await macRead(svc);
|
|
264
|
+
const pick = s.secure.enabled && s.secure.host ? s.secure : s.web.enabled && s.web.host ? s.web : undefined;
|
|
265
|
+
return pick ? `${pick.host}:${pick.port}` : undefined;
|
|
266
|
+
}
|
|
267
|
+
if (platform === 'linux') {
|
|
268
|
+
const g = await gnomeRead();
|
|
269
|
+
return g.mode === 'manual' && g.http.host ? `${g.http.host}:${g.http.port}` : undefined;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
catch {
|
|
273
|
+
return undefined;
|
|
274
|
+
}
|
|
275
|
+
return undefined;
|
|
276
|
+
}
|
|
277
|
+
export async function readOsProxyEndpoint() {
|
|
278
|
+
const platform = process.platform;
|
|
279
|
+
try {
|
|
280
|
+
if (platform === 'win32') {
|
|
281
|
+
const s = await winRead();
|
|
282
|
+
if (!s.ProxyEnable || s.AutoConfigURL.trim())
|
|
283
|
+
return undefined;
|
|
284
|
+
const server = pickWinServer(s.ProxyServer);
|
|
285
|
+
if (!server)
|
|
286
|
+
return undefined;
|
|
287
|
+
return { server, bypass: s.ProxyOverride.split(';').map(v => v.trim()).filter(Boolean) };
|
|
288
|
+
}
|
|
289
|
+
if (platform === 'darwin') {
|
|
290
|
+
for (const svc of await macServices()) {
|
|
291
|
+
const s = await macRead(svc);
|
|
292
|
+
const pick = s.secure.enabled && s.secure.host ? s.secure : s.web.enabled && s.web.host ? s.web : undefined;
|
|
293
|
+
if (pick)
|
|
294
|
+
return { server: `${pick.host}:${pick.port}`, bypass: s.bypass };
|
|
295
|
+
}
|
|
296
|
+
return undefined;
|
|
297
|
+
}
|
|
298
|
+
if (platform === 'linux') {
|
|
299
|
+
const g = await gnomeRead();
|
|
300
|
+
if (g.mode !== 'manual')
|
|
301
|
+
return undefined;
|
|
302
|
+
const pick = g.https.host ? g.https : g.http.host ? g.http : undefined;
|
|
303
|
+
return pick ? { server: `${pick.host}:${pick.port}`, bypass: g.ignoreHosts } : undefined;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
catch {
|
|
307
|
+
// A probe must never fail because the OS settings are unreadable; the
|
|
308
|
+
// caller treats "no endpoint" as "connect directly".
|
|
309
|
+
return undefined;
|
|
310
|
+
}
|
|
311
|
+
return undefined;
|
|
312
|
+
}
|
|
313
|
+
/** WinINET's per-scheme form (`http=host:port;https=host:port`) reduces to
|
|
314
|
+
* one endpoint; an `http://` prefix is stripped but `user:pass@` is kept.
|
|
315
|
+
* A SOCKS-only config (`socks=host:port`) yields nothing: the machine leg
|
|
316
|
+
* speaks the HTTP proxy protocol, and reporting a direct run is honester
|
|
317
|
+
* than dialing `socks=...` as a hostname. */
|
|
318
|
+
function pickWinServer(raw) {
|
|
319
|
+
const value = raw.trim();
|
|
320
|
+
if (!value)
|
|
321
|
+
return undefined;
|
|
322
|
+
const https = /(?:^|;)https=([^;]+)/i.exec(value)?.[1];
|
|
323
|
+
const http = /(?:^|;)http=([^;]+)/i.exec(value)?.[1];
|
|
324
|
+
const pick = (https || http || value.split(';')[0]).trim().replace(/^https?:\/\//i, '').replace(/\/+$/, '');
|
|
325
|
+
if (!pick || /^[a-z]+=/i.test(pick))
|
|
326
|
+
return undefined;
|
|
327
|
+
return pick;
|
|
328
|
+
}
|
|
329
|
+
function ipv4ToInt(addr) {
|
|
330
|
+
const m = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(addr);
|
|
331
|
+
if (!m)
|
|
332
|
+
return undefined;
|
|
333
|
+
const parts = m.slice(1).map(Number);
|
|
334
|
+
if (parts.some(n => n > 255))
|
|
335
|
+
return undefined;
|
|
336
|
+
return ((parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8) | parts[3]) >>> 0;
|
|
337
|
+
}
|
|
338
|
+
const isLoopbackHost = (host) => host === 'localhost' || host === '::1' || /^127\./.test(host);
|
|
339
|
+
/**
|
|
340
|
+
* Would the OS send `host` through its proxy, given the OS bypass list?
|
|
341
|
+
* Covers the syntaxes the three platforms actually store: WinINET wildcards
|
|
342
|
+
* (`192.168.*`, `<local>`), macOS/Linux CIDR entries and domain suffixes.
|
|
343
|
+
* Loopback is ALWAYS direct — a proxy in front of a loopback target would
|
|
344
|
+
* either loop back into this gateway or fail for no diagnosable reason.
|
|
345
|
+
*/
|
|
346
|
+
export function osProxyBypasses(host, bypass) {
|
|
347
|
+
const name = host.toLowerCase().replace(/^\[|\]$/g, '');
|
|
348
|
+
if (isLoopbackHost(name))
|
|
349
|
+
return true;
|
|
350
|
+
const ip = ipv4ToInt(name);
|
|
351
|
+
for (const entry of bypass) {
|
|
352
|
+
const e = entry.trim().toLowerCase();
|
|
353
|
+
if (!e)
|
|
354
|
+
continue;
|
|
355
|
+
if (e === '*')
|
|
356
|
+
return true;
|
|
357
|
+
if (e === '<local>') {
|
|
358
|
+
// WinINET: plain host names, no dot. An IP address is never "<local>".
|
|
359
|
+
if (!name.includes('.') && !ip)
|
|
360
|
+
return true;
|
|
361
|
+
continue;
|
|
362
|
+
}
|
|
363
|
+
if (e.includes('/') && ip !== undefined) {
|
|
364
|
+
const m = /^(\d{1,3}(?:\.\d{1,3}){3})\/(\d{1,2})$/.exec(e);
|
|
365
|
+
if (m) {
|
|
366
|
+
const base = ipv4ToInt(m[1]);
|
|
367
|
+
const bits = Number(m[2]);
|
|
368
|
+
if (base !== undefined && bits >= 0 && bits <= 32) {
|
|
369
|
+
const mask = bits === 0 ? 0 : (~0 << (32 - bits)) >>> 0;
|
|
370
|
+
if ((ip & mask) === (base & mask))
|
|
371
|
+
return true;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
continue;
|
|
375
|
+
}
|
|
376
|
+
if (e.startsWith('*.')) {
|
|
377
|
+
const suffix = e.slice(1); // `.example.com`
|
|
378
|
+
if (name === e.slice(2) || name.endsWith(suffix))
|
|
379
|
+
return true;
|
|
380
|
+
continue;
|
|
381
|
+
}
|
|
382
|
+
if (e.startsWith('.')) {
|
|
383
|
+
if (name === e.slice(1) || name.endsWith(e))
|
|
384
|
+
return true;
|
|
385
|
+
continue;
|
|
386
|
+
}
|
|
387
|
+
if (e.includes('*')) {
|
|
388
|
+
const pattern = e.split('*').map(part => part.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('.*');
|
|
389
|
+
if (new RegExp(`^${pattern}$`).test(name))
|
|
390
|
+
return true;
|
|
391
|
+
continue;
|
|
392
|
+
}
|
|
393
|
+
if (name === e)
|
|
394
|
+
return true;
|
|
395
|
+
}
|
|
396
|
+
return false;
|
|
397
|
+
}
|
|
398
|
+
export async function getSystemProxyStatus(targetPort, bypassList) {
|
|
399
|
+
const platform = process.platform;
|
|
400
|
+
const supported = platformSupported(platform);
|
|
401
|
+
const targetServer = `127.0.0.1:${targetPort}`;
|
|
402
|
+
const base = {
|
|
403
|
+
platform,
|
|
404
|
+
supported,
|
|
405
|
+
state: supported ? 'off' : 'unsupported',
|
|
406
|
+
targetServer,
|
|
407
|
+
override: buildOverride(bypassList, platform),
|
|
408
|
+
caPath: caCertPath(getRuntimeConfigRef()?.ingressProxy?.caDir),
|
|
409
|
+
};
|
|
410
|
+
if (!supported)
|
|
411
|
+
return base;
|
|
412
|
+
const snapshot = readSnapshot();
|
|
413
|
+
const currentServer = await readCurrentServer(platform);
|
|
414
|
+
if (snapshot) {
|
|
415
|
+
const owned = currentServer === snapshot.targetServer;
|
|
416
|
+
return { ...base, state: owned ? 'on' : 'stale', owner: snapshot.owner, currentServer, snapshot };
|
|
417
|
+
}
|
|
418
|
+
return { ...base, state: currentServer ? 'external' : 'off', currentServer };
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* Point the OS proxy at one of OCR's local endpoints, snapshotting first.
|
|
422
|
+
* See `planSystemProxyAction` for how an existing snapshot is treated.
|
|
423
|
+
*/
|
|
424
|
+
export async function enableSystemProxy(targetPort, bypassList, owner = 'forward') {
|
|
425
|
+
const platform = process.platform;
|
|
426
|
+
if (!platformSupported(platform))
|
|
427
|
+
throw new Error(`system proxy not supported on ${platform}`);
|
|
428
|
+
const targetServer = `127.0.0.1:${targetPort}`;
|
|
429
|
+
const snapshot = readSnapshot();
|
|
430
|
+
const plan = planSystemProxyAction(snapshot, targetServer);
|
|
431
|
+
if (plan === 'refresh') {
|
|
432
|
+
// Same endpoint: never re-read the OS here — it points at OUR server, and
|
|
433
|
+
// re-snapshotting would overwrite (and permanently lose) the user's
|
|
434
|
+
// original proxy configuration. Only a relabelled owner is worth a write.
|
|
435
|
+
if (snapshot.owner !== owner)
|
|
436
|
+
writeSnapshot({ ...snapshot, owner });
|
|
437
|
+
return getSystemProxyStatus(targetPort, bypassList);
|
|
438
|
+
}
|
|
439
|
+
const overrideHosts = ['localhost', '127.0.0.1', '<local>', ...bypassList.filter(Boolean)];
|
|
440
|
+
/** `switch` keeps the snapshot's PRE-OCR payload; only `snapshot` reads the OS. */
|
|
441
|
+
const original = plan === 'snapshot' ? undefined : snapshot;
|
|
442
|
+
if (platform === 'win32') {
|
|
443
|
+
const snap = original ? original.win : await winRead();
|
|
444
|
+
await winSet(targetServer, buildOverride(bypassList, platform));
|
|
445
|
+
writeSnapshot({ platform, setAt: Date.now(), targetServer, owner, win: snap });
|
|
446
|
+
}
|
|
447
|
+
else if (platform === 'darwin') {
|
|
448
|
+
const services = await macServices();
|
|
449
|
+
const snaps = original ? original.mac : await Promise.all(services.map(macRead));
|
|
450
|
+
await Promise.all(services.map(svc => macSet(svc, targetServer, overrideHosts)));
|
|
451
|
+
writeSnapshot({ platform, setAt: Date.now(), targetServer, owner, mac: snaps });
|
|
452
|
+
}
|
|
453
|
+
else {
|
|
454
|
+
const snap = original ? original.gnome : await gnomeRead();
|
|
455
|
+
await gnomeSet(targetServer, overrideHosts);
|
|
456
|
+
writeSnapshot({ platform, setAt: Date.now(), targetServer, owner, gnome: snap });
|
|
457
|
+
}
|
|
458
|
+
return getSystemProxyStatus(targetPort, bypassList);
|
|
459
|
+
}
|
|
460
|
+
/** Restore whatever the OS proxy was before OCR took ownership. */
|
|
461
|
+
export async function disableSystemProxy(targetPort, bypassList) {
|
|
462
|
+
const snapshot = readSnapshot();
|
|
463
|
+
if (!snapshot) {
|
|
464
|
+
// Nothing owned — deliberately a no-op: without a snapshot we cannot
|
|
465
|
+
// know what to restore, so touching the OS proxy here could blind-off
|
|
466
|
+
// a proxy OCR never set (a user's clash/v2ray). File-header contract.
|
|
467
|
+
return getSystemProxyStatus(targetPort, bypassList);
|
|
468
|
+
}
|
|
469
|
+
try {
|
|
470
|
+
if (snapshot.win)
|
|
471
|
+
await winRestore(snapshot.win);
|
|
472
|
+
if (snapshot.mac)
|
|
473
|
+
await Promise.all(snapshot.mac.map(macRestore));
|
|
474
|
+
if (snapshot.gnome)
|
|
475
|
+
await gnomeRestore(snapshot.gnome);
|
|
476
|
+
}
|
|
477
|
+
finally {
|
|
478
|
+
clearSnapshot();
|
|
479
|
+
}
|
|
480
|
+
return getSystemProxyStatus(targetPort, bypassList);
|
|
481
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { HistoryDeleteResult, HistoryDeleteTarget } from './traffic-delete.worker.js';
|
|
2
|
+
/**
|
|
3
|
+
* Single-flight singleton for the persisted history delete (ADR-0017 §5).
|
|
4
|
+
*
|
|
5
|
+
* The console asks; the gateway runs AT MOST ONE rewrite at a time, in a
|
|
6
|
+
* worker thread, and reports it through one status record. There is no job id
|
|
7
|
+
* and no queue: a second request while one is in flight is answered `busy`,
|
|
8
|
+
* which is the whole duplicate-submission guard the UI needs.
|
|
9
|
+
*/
|
|
10
|
+
export interface HistoryDeleteStatus {
|
|
11
|
+
state: 'idle' | 'running' | 'done' | 'failed';
|
|
12
|
+
startedAt?: number;
|
|
13
|
+
finishedAt?: number;
|
|
14
|
+
filesDone: number;
|
|
15
|
+
filesTotal: number;
|
|
16
|
+
deleted: HistoryDeleteResult;
|
|
17
|
+
error?: string;
|
|
18
|
+
}
|
|
19
|
+
export declare function historyDeleteStatus(): HistoryDeleteStatus;
|
|
20
|
+
/** Start the one allowed run; false means a run is already in flight. */
|
|
21
|
+
export declare function startHistoryDelete(target: HistoryDeleteTarget): boolean;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { Worker } from 'node:worker_threads';
|
|
2
|
+
import { compiledSnapshotExe } from '../cli/paths.js';
|
|
3
|
+
let status = { state: 'idle', filesDone: 0, filesTotal: 0, deleted: { proxy: 0, plugins: {} } };
|
|
4
|
+
export function historyDeleteStatus() {
|
|
5
|
+
return status;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Compiled sidecar: a plain path, resolved against the compiled ENTRY ROOT —
|
|
9
|
+
* a snapshot cannot resolve a worker through `import.meta.url` (Bun issue
|
|
10
|
+
* #16869), so the worker ships as a SECOND entrypoint (`build-sidecar.ts`),
|
|
11
|
+
* and the root is the entries' common directory (`backend/src`).
|
|
12
|
+
* Dev / `node dist`: the URL form, which Bun maps `.js` → `.ts`.
|
|
13
|
+
*/
|
|
14
|
+
function createWorker(target) {
|
|
15
|
+
if (compiledSnapshotExe())
|
|
16
|
+
return new Worker('./ingress-proxy/traffic-delete.worker.ts', { workerData: target });
|
|
17
|
+
return new Worker(new URL('./traffic-delete.worker.js', import.meta.url), { workerData: target });
|
|
18
|
+
}
|
|
19
|
+
/** Start the one allowed run; false means a run is already in flight. */
|
|
20
|
+
export function startHistoryDelete(target) {
|
|
21
|
+
if (status.state === 'running')
|
|
22
|
+
return false;
|
|
23
|
+
status = { state: 'running', startedAt: Date.now(), filesDone: 0, filesTotal: 0, deleted: { proxy: 0, plugins: {} } };
|
|
24
|
+
let worker;
|
|
25
|
+
try {
|
|
26
|
+
worker = createWorker(target);
|
|
27
|
+
}
|
|
28
|
+
catch (err) {
|
|
29
|
+
status = { ...status, state: 'failed', finishedAt: Date.now(), error: err instanceof Error ? err.message : String(err) };
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
worker.on('message', (msg) => {
|
|
33
|
+
if (status.state !== 'running')
|
|
34
|
+
return;
|
|
35
|
+
const m = msg;
|
|
36
|
+
if (m?.type === 'progress') {
|
|
37
|
+
status = { ...status, filesDone: m.filesDone ?? 0, filesTotal: m.filesTotal ?? 0, deleted: m.deleted ?? status.deleted };
|
|
38
|
+
}
|
|
39
|
+
else if (m?.type === 'done') {
|
|
40
|
+
status = {
|
|
41
|
+
...status,
|
|
42
|
+
state: 'done',
|
|
43
|
+
finishedAt: Date.now(),
|
|
44
|
+
filesDone: m.filesTotal ?? status.filesDone,
|
|
45
|
+
filesTotal: m.filesTotal ?? status.filesTotal,
|
|
46
|
+
deleted: m.deleted ?? status.deleted,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
else if (m?.type === 'error') {
|
|
50
|
+
status = { ...status, state: 'failed', finishedAt: Date.now(), error: m.message ?? 'delete worker failed' };
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
worker.on('error', (err) => {
|
|
54
|
+
if (status.state !== 'running')
|
|
55
|
+
return;
|
|
56
|
+
status = { ...status, state: 'failed', finishedAt: Date.now(), error: err instanceof Error ? err.message : String(err) };
|
|
57
|
+
});
|
|
58
|
+
worker.on('exit', (code) => {
|
|
59
|
+
if (status.state !== 'running')
|
|
60
|
+
return;
|
|
61
|
+
status = { ...status, state: 'failed', finishedAt: Date.now(), error: `delete worker exited with code ${code}` };
|
|
62
|
+
});
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* History-delete WORKER — the persisted-scope rewrite runs off the event loop.
|
|
3
|
+
*
|
|
4
|
+
* Deleting a filter's rows means parsing and rewriting every retained day file
|
|
5
|
+
* (hundreds of thousands of lines), which must never happen on the gateway's
|
|
6
|
+
* main thread: the HTTP exchange that asked for it would stall every in-flight
|
|
7
|
+
* proxy request behind it. The orchestrator (`traffic-delete.ts`) passes the
|
|
8
|
+
* absolute directories through `workerData` because the worker is a separate
|
|
9
|
+
* thread with its own module instances — the persist dir and the gateway
|
|
10
|
+
* registry are main-thread state.
|
|
11
|
+
*
|
|
12
|
+
* Layout grammars live in `traffic-files.ts`; filter semantics mirror the
|
|
13
|
+
* console's federated history read exactly (a predicate copy in another
|
|
14
|
+
* spelling would delete a different row set than the table shows).
|
|
15
|
+
*/
|
|
16
|
+
export interface HistoryDeleteFilters {
|
|
17
|
+
kind?: string;
|
|
18
|
+
method?: string;
|
|
19
|
+
status?: string;
|
|
20
|
+
/** Already lowercased by the route — proxy rows compare verbatim, plugin
|
|
21
|
+
* rows via their lowercased `authority` (the history read's asymmetry). */
|
|
22
|
+
host?: string;
|
|
23
|
+
}
|
|
24
|
+
export interface HistoryDeleteTarget {
|
|
25
|
+
/** `<ocrHome>/ingress-proxy/access-log/`; null when persistence is off. */
|
|
26
|
+
proxyDir: string | null;
|
|
27
|
+
/** One feed dir per registered dissector gateway. */
|
|
28
|
+
pluginDirs: {
|
|
29
|
+
id: string;
|
|
30
|
+
dir: string;
|
|
31
|
+
}[];
|
|
32
|
+
filters: HistoryDeleteFilters;
|
|
33
|
+
}
|
|
34
|
+
export interface HistoryDeleteResult {
|
|
35
|
+
proxy: number;
|
|
36
|
+
plugins: Record<string, number>;
|
|
37
|
+
}
|
|
38
|
+
export type HistoryDeleteProgress = (filesDone: number, filesTotal: number, deleted: HistoryDeleteResult) => void;
|
|
39
|
+
/**
|
|
40
|
+
* The whole persisted rewrite, synchronously (the caller is already off the
|
|
41
|
+
* event loop). Exported so tests drive the real rewrite in-process instead of
|
|
42
|
+
* spawning a thread for every case.
|
|
43
|
+
*/
|
|
44
|
+
export declare function runTrafficDelete(target: HistoryDeleteTarget, onProgress?: HistoryDeleteProgress): HistoryDeleteResult;
|