omnicell-router 0.7.0 → 0.8.2
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/backend/dist/cli/clients/activity.d.ts +35 -0
- package/backend/dist/cli/clients/activity.js +67 -0
- package/backend/dist/cli/clients/base.d.ts +13 -2
- package/backend/dist/cli/clients/claude.d.ts +2 -2
- package/backend/dist/cli/clients/codex.d.ts +2 -2
- package/backend/dist/cli/clients/core-adapters.d.ts +3 -4
- package/backend/dist/cli/clients/core-adapters.js +3 -4
- package/backend/dist/cli/clients/cursor-proxy.d.ts +88 -0
- package/backend/dist/cli/clients/cursor-proxy.js +421 -0
- package/backend/dist/cli/clients/dsh.d.ts +2 -2
- package/backend/dist/cli/clients/grok.d.ts +2 -2
- package/backend/dist/cli/clients/index.d.ts +8 -1
- package/backend/dist/cli/clients/index.js +55 -21
- package/backend/dist/cli/clients/kimi.d.ts +2 -2
- package/backend/dist/cli/clients/launcher.d.ts +8 -0
- package/backend/dist/cli/clients/model-meta.js +1 -1
- package/backend/dist/cli/clients/omp.d.ts +2 -2
- package/backend/dist/cli/clients/openclaw.d.ts +2 -2
- package/backend/dist/cli/clients/opencode-proxy.d.ts +11 -0
- package/backend/dist/cli/clients/opencode-proxy.js +351 -0
- package/backend/dist/cli/clients/opencode.d.ts +2 -2
- package/backend/dist/cli/clients/pi.d.ts +2 -2
- package/backend/dist/cli/clients/plugin-adapters.d.ts +3 -1
- package/backend/dist/cli/clients/plugin-adapters.js +6 -29
- package/backend/dist/cli/clients/qoder-proxy.d.ts +84 -0
- package/backend/dist/cli/clients/qoder-proxy.js +497 -0
- package/backend/dist/cli/clients/reserved.d.ts +2 -6
- package/backend/dist/cli/clients/reserved.js +2 -0
- package/backend/dist/cli/clients/workbuddy.d.ts +2 -2
- package/backend/dist/cli/index.js +60 -9
- package/backend/dist/cli/types.d.ts +29 -8
- package/backend/dist/compression/caveman-prompts.d.ts +1 -1
- package/backend/dist/config/comments-en.js +6 -12
- package/backend/dist/config/comments-zh.js +6 -12
- package/backend/dist/config/defaults.d.ts +56 -1
- package/backend/dist/config/defaults.js +68 -0
- package/backend/dist/config/index.d.ts +9 -2
- package/backend/dist/config/index.js +26 -3
- package/backend/dist/config/types.d.ts +55 -43
- package/backend/dist/forward-proxy/egress.d.ts +10 -2
- package/backend/dist/forward-proxy/egress.js +42 -4
- package/backend/dist/forward-proxy/ordered-relay.d.ts +29 -0
- package/backend/dist/forward-proxy/ordered-relay.js +46 -0
- package/backend/dist/forward-proxy/rules.d.ts +66 -19
- package/backend/dist/forward-proxy/rules.js +61 -14
- package/backend/dist/forward-proxy/server.js +226 -77
- package/backend/dist/forward-proxy/system-proxy.d.ts +42 -2
- package/backend/dist/forward-proxy/system-proxy.js +199 -22
- package/backend/dist/harness/common/market.d.ts +3 -3
- package/backend/dist/harness/host/adapters/amp.d.ts +1 -1
- package/backend/dist/harness/host/adapters/antigravity.d.ts +1 -1
- package/backend/dist/harness/host/adapters/claude.d.ts +1 -1
- package/backend/dist/harness/host/adapters/cline.d.ts +1 -1
- package/backend/dist/harness/host/adapters/codex.d.ts +1 -1
- package/backend/dist/harness/host/adapters/cursor.d.ts +1 -1
- package/backend/dist/harness/host/adapters/dsh.d.ts +2 -2
- package/backend/dist/harness/host/adapters/factory.d.ts +1 -1
- package/backend/dist/harness/host/adapters/gemini-cli.d.ts +1 -1
- package/backend/dist/harness/host/adapters/grok.d.ts +1 -1
- package/backend/dist/harness/host/adapters/kimi.d.ts +1 -1
- package/backend/dist/harness/host/adapters/omp.d.ts +1 -1
- package/backend/dist/harness/host/adapters/openclaw.d.ts +1 -1
- package/backend/dist/harness/host/adapters/opencode.d.ts +1 -1
- package/backend/dist/harness/host/adapters/pi.d.ts +1 -1
- package/backend/dist/harness/host/adapters/universal.d.ts +1 -1
- package/backend/dist/harness/host/adapters/windsurf.d.ts +1 -1
- package/backend/dist/harness/host/adapters/zed.d.ts +1 -1
- package/backend/dist/harness/skill/skills-sh.d.ts +1 -1
- package/backend/dist/harness/translation.js +64 -21
- package/backend/dist/harness/types.d.ts +1 -1
- package/backend/dist/harness/types.js +1 -1
- package/backend/dist/index.js +31 -18
- package/backend/dist/ir/adapters/anthropic-stream.d.ts +18 -0
- package/backend/dist/ir/adapters/anthropic-stream.js +247 -0
- package/backend/dist/ir/adapters/anthropic.d.ts +29 -0
- package/backend/dist/ir/adapters/anthropic.js +788 -0
- package/backend/dist/ir/adapters/chat.d.ts +15 -0
- package/backend/dist/ir/adapters/chat.js +602 -0
- package/backend/dist/ir/adapters/google-stream.d.ts +22 -0
- package/backend/dist/ir/adapters/google-stream.js +242 -0
- package/backend/dist/ir/adapters/google.d.ts +9 -0
- package/backend/dist/ir/adapters/google.js +317 -0
- package/backend/dist/ir/adapters/openai-responses-output.js +8 -5
- package/backend/dist/ir/adapters/openai-responses.d.ts +15 -0
- package/backend/dist/ir/adapters/openai-responses.js +26 -1
- package/backend/dist/ir/adapters/responses-output.d.ts +50 -0
- package/backend/dist/ir/adapters/responses-output.js +348 -0
- package/backend/dist/ir/adapters/responses-stream.d.ts +12 -0
- package/backend/dist/ir/adapters/responses-stream.js +178 -0
- package/backend/dist/ir/adapters/responses.d.ts +14 -0
- package/backend/dist/ir/adapters/responses.js +646 -0
- package/backend/dist/ir/errors.d.ts +2 -2
- package/backend/dist/ir/types.d.ts +2 -1
- package/backend/dist/metrics/request-cost.d.ts +11 -2
- package/backend/dist/metrics/request-cost.js +12 -3
- package/backend/dist/observability/request-identity.d.ts +1 -1
- package/backend/dist/observability/trace-id.d.ts +40 -0
- package/backend/dist/observability/trace-id.js +134 -0
- package/backend/dist/observability/upstream-events.d.ts +82 -60
- package/backend/dist/observability/upstream-events.js +142 -67
- package/backend/dist/opencode/catalog/types.d.ts +9 -0
- package/backend/dist/opencode/credentials/types.d.ts +2 -2
- package/backend/dist/opencode/probe.d.ts +24 -1
- package/backend/dist/opencode/probe.js +28 -4
- package/backend/dist/opencode/user-config.d.ts +5 -0
- package/backend/dist/pipeline/directives.d.ts +6 -2
- package/backend/dist/pipeline/directives.js +7 -3
- package/backend/dist/pipeline/orchestrator.d.ts +28 -27
- package/backend/dist/pipeline/orchestrator.js +220 -194
- package/backend/dist/pipeline/unresolved-model.d.ts +33 -0
- package/backend/dist/pipeline/unresolved-model.js +43 -0
- package/backend/dist/plugins/catalog-def.d.ts +143 -0
- package/backend/dist/plugins/catalog-def.js +177 -0
- package/backend/dist/plugins/gateways.d.ts +62 -3
- package/backend/dist/plugins/gateways.js +139 -2
- package/backend/dist/plugins/host.d.ts +42 -5
- package/backend/dist/plugins/host.js +454 -38
- package/backend/dist/plugins/manifest.d.ts +119 -10
- package/backend/dist/plugins/manifest.js +265 -14
- package/backend/dist/plugins/pack.js +26 -0
- package/backend/dist/plugins/types.d.ts +187 -21
- package/backend/dist/protocols/systemone/convert.d.ts +1 -0
- package/backend/dist/protocols/systemone/convert.js +6 -1
- package/backend/dist/protocols/systemone/decide.js +18 -8
- package/backend/dist/providers/anthropic-messages.d.ts +12 -0
- package/backend/dist/providers/anthropic-messages.js +53 -0
- package/backend/dist/providers/anthropic.d.ts +7 -8
- package/backend/dist/providers/anthropic.js +36 -324
- package/backend/dist/providers/base.d.ts +14 -2
- package/backend/dist/providers/boot-direct.js +68 -38
- package/backend/dist/providers/dispatch.d.ts +5 -19
- package/backend/dist/providers/dispatch.js +46 -58
- package/backend/dist/providers/executors/anthropic-messages.d.ts +1 -0
- package/backend/dist/providers/executors/anthropic-messages.js +3 -0
- package/backend/dist/providers/executors/dispatch.d.ts +16 -2
- package/backend/dist/providers/executors/dispatch.js +39 -9
- package/backend/dist/providers/executors/google-gemini.d.ts +1 -0
- package/backend/dist/providers/executors/google-gemini.js +3 -0
- package/backend/dist/providers/executors/http-stream.js +16 -3
- package/backend/dist/providers/executors/openai-chat.d.ts +1 -0
- package/backend/dist/providers/executors/openai-chat.js +3 -0
- package/backend/dist/providers/executors/openai-responses.d.ts +1 -0
- package/backend/dist/providers/executors/openai-responses.js +3 -0
- package/backend/dist/providers/executors/plugin-adapter.js +4 -4
- package/backend/dist/providers/google-gemini.d.ts +12 -0
- package/backend/dist/providers/google-gemini.js +50 -0
- package/backend/dist/providers/google.d.ts +7 -8
- package/backend/dist/providers/google.js +35 -308
- package/backend/dist/providers/http-json.d.ts +19 -0
- package/backend/dist/providers/http-json.js +104 -0
- package/backend/dist/providers/http-stream.d.ts +52 -0
- package/backend/dist/providers/http-stream.js +423 -0
- package/backend/dist/providers/member-match.d.ts +4 -5
- package/backend/dist/providers/member-match.js +4 -5
- package/backend/dist/providers/openai-chat.d.ts +13 -0
- package/backend/dist/providers/openai-chat.js +99 -0
- package/backend/dist/providers/openai-compatible.d.ts +7 -4
- package/backend/dist/providers/openai-compatible.js +71 -368
- package/backend/dist/providers/openai-responses.d.ts +12 -0
- package/backend/dist/providers/openai-responses.js +68 -0
- package/backend/dist/providers/opencode-proxy.d.ts +5 -5
- package/backend/dist/providers/opencode-proxy.js +49 -125
- package/backend/dist/providers/plugin-adapter.d.ts +9 -0
- package/backend/dist/providers/plugin-adapter.js +31 -0
- package/backend/dist/providers/registry.d.ts +49 -24
- package/backend/dist/providers/registry.js +97 -55
- package/backend/dist/providers/responses.d.ts +7 -28
- package/backend/dist/providers/responses.js +45 -253
- package/backend/dist/providers/tool-markup-leak.d.ts +41 -0
- package/backend/dist/providers/tool-markup-leak.js +57 -0
- package/backend/dist/providers/tool-markup-recovery.d.ts +27 -0
- package/backend/dist/providers/tool-markup-recovery.js +786 -0
- package/backend/dist/providers/virtual-meta.d.ts +21 -2
- package/backend/dist/providers/virtual-meta.js +49 -9
- package/backend/dist/proxy/asset-download.d.ts +54 -0
- package/backend/dist/proxy/asset-download.js +420 -0
- package/backend/dist/proxy/manual-node-spec.d.ts +16 -16
- package/backend/dist/proxy/mihomo-binary.d.ts +17 -2
- package/backend/dist/proxy/mihomo-binary.js +62 -51
- package/backend/dist/proxy/mihomo-client.js +15 -1
- package/backend/dist/proxy/mihomo-config.js +11 -2
- package/backend/dist/proxy/mihomo-geo.d.ts +87 -0
- package/backend/dist/proxy/mihomo-geo.js +213 -0
- package/backend/dist/proxy/mihomo-logs.d.ts +27 -0
- package/backend/dist/proxy/mihomo-logs.js +70 -0
- package/backend/dist/proxy/mihomo-pac.d.ts +11 -0
- package/backend/dist/proxy/mihomo-pac.js +15 -0
- package/backend/dist/proxy/mihomo-rules-import.d.ts +1 -1
- package/backend/dist/proxy/mihomo-rules-import.js +7 -7
- package/backend/dist/proxy/mihomo-sidecar.d.ts +17 -1
- package/backend/dist/proxy/mihomo-sidecar.js +148 -26
- package/backend/dist/quota/adapters/minimax.js +36 -13
- package/backend/dist/quota/cli-login.d.ts +8 -0
- package/backend/dist/quota/cli-login.js +18 -0
- package/backend/dist/quota/declarations.js +10 -0
- package/backend/dist/quota/dsl.js +18 -5
- package/backend/dist/quota/engine.d.ts +22 -1
- package/backend/dist/quota/engine.js +170 -29
- package/backend/dist/quota/prober.d.ts +5 -0
- package/backend/dist/quota/prober.js +11 -2
- package/backend/dist/quota/types.d.ts +87 -8
- package/backend/dist/quota/verdict.d.ts +35 -0
- package/backend/dist/quota/verdict.js +135 -0
- package/backend/dist/ratelimit/admission.d.ts +131 -0
- package/backend/dist/ratelimit/admission.js +233 -0
- package/backend/dist/ratelimit/rate-limiter.js +6 -0
- package/backend/dist/resilience/types.d.ts +2 -2
- package/backend/dist/router/layer2-decision.d.ts +1 -1
- package/backend/dist/router/layer2-decision.js +5 -8
- package/backend/dist/routes/console.js +248 -52
- package/backend/dist/routes/mihomo.d.ts +2 -0
- package/backend/dist/routes/mihomo.js +334 -29
- package/backend/dist/routes/responses.d.ts +87 -8
- package/backend/dist/routes/responses.js +314 -33
- package/backend/dist/server.js +34 -6
- package/backend/dist/session/session-manager.d.ts +6 -0
- package/backend/dist/session/session-manager.js +20 -9
- package/backend/dist/surfaces/chat-pipeline.d.ts +2 -2
- package/backend/dist/surfaces/chat-pipeline.js +17 -7
- package/backend/dist/surfaces/chat.js +15 -0
- package/backend/dist/surfaces/decision-pipeline.js +7 -10
- package/backend/dist/surfaces/direct-forward.d.ts +3 -0
- package/backend/dist/surfaces/direct-forward.js +206 -0
- package/backend/dist/surfaces/rate-limit-response.d.ts +17 -0
- package/backend/dist/surfaces/rate-limit-response.js +57 -0
- package/backend/dist/surfaces/types.d.ts +11 -0
- package/backend/dist/trace/tracker.d.ts +32 -1
- package/backend/dist/trace/tracker.js +29 -4
- package/backend/dist/types/router.d.ts +34 -12
- package/backend/dist/types/router.js +35 -6
- package/backend/dist/utils/chat-stream.d.ts +21 -8
- package/backend/dist/utils/chat-stream.js +46 -9
- package/backend/dist/utils/event-ring.d.ts +4 -0
- package/backend/dist/utils/event-ring.js +4 -0
- package/backend/dist/utils/popularity.js +29 -4
- package/backend/dist/utils/proxy.d.ts +7 -0
- package/backend/dist/utils/proxy.js +35 -10
- package/backend/dist/utils/retry-after.d.ts +21 -0
- package/backend/dist/utils/retry-after.js +36 -0
- package/bin/ocr.js +2 -2
- package/frontend/dist/assets/index-55VVwfFQ.js +1521 -0
- package/frontend/dist/assets/{index-CQSLGFTV.css → index-DxZ9MHKB.css} +1 -1
- package/frontend/dist/assets/workbuddy.color-DOMpQkxJ.svg +29 -0
- package/frontend/dist/index.html +2 -2
- package/package.json +1 -1
- package/frontend/dist/assets/index-CLvxJjp_.js +0 -1476
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Geo databases for `GEOIP` / `GEOSITE` rules (ADR-0022).
|
|
3
|
+
*
|
|
4
|
+
* mihomo evaluates both types from data files in its work directory and
|
|
5
|
+
* downloads them itself while parsing its config — but a failed download
|
|
6
|
+
* (offline machine, blocked GitHub host) makes the core exit fatally, and the
|
|
7
|
+
* core is exactly the thing this gateway needs for its own outbound traffic.
|
|
8
|
+
* The gateway therefore installs the two files as an EXPLICIT action and both
|
|
9
|
+
* the rule save and the core start refuse a geo-rule config while they are
|
|
10
|
+
* missing, so the failure is a clear console message instead of a dead core.
|
|
11
|
+
* Once installed the files are used offline; mihomo never re-fetches them
|
|
12
|
+
* (the generated config leaves `geo-auto-update` off). Refreshing is an
|
|
13
|
+
* operator action with two egress legs: the machine's own network (OS system
|
|
14
|
+
* proxy when one is enabled, bypass list honoured) or the Clash node itself.
|
|
15
|
+
* Each asset streams through the shared resumable downloader
|
|
16
|
+
* (`proxy/asset-download.ts`), so an interrupted 8 MB database continues on the
|
|
17
|
+
* next click instead of starting from zero.
|
|
18
|
+
*/
|
|
19
|
+
import fs from 'node:fs';
|
|
20
|
+
import path from 'node:path';
|
|
21
|
+
import { MIHOMO_GEO_STALE_DAYS } from '../config/defaults.js';
|
|
22
|
+
import { osProxyBypasses, readOsProxyEndpoint } from '../forward-proxy/system-proxy.js';
|
|
23
|
+
import { renameWithRetry } from '../utils/atomic-write.js';
|
|
24
|
+
import { fetchViaProxy } from '../utils/proxy.js';
|
|
25
|
+
import { assetPartPaths, cleanupAssetPart, downloadAssetToFile, parseAssetDigest, withAssetLock } from './asset-download.js';
|
|
26
|
+
/** Release host shared with the binary installer (MetaCubeX publishes both). */
|
|
27
|
+
const GEO_DOWNLOAD_BASE = 'https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest';
|
|
28
|
+
const MAX_GEO_BYTES = 64 * 1024 * 1024;
|
|
29
|
+
const USER_AGENT = 'omnicell-router';
|
|
30
|
+
/** MetaDB/GeoSite release host: `…/latest/geoip.metadb.sha256sum` is a
|
|
31
|
+
* ~70-byte text file beside each database. The published digest comes from the
|
|
32
|
+
* same host as the database itself, so verification costs no API quota and
|
|
33
|
+
* stays as fresh as the asset. */
|
|
34
|
+
const SHA256SUM_SUFFIX = '.sha256sum';
|
|
35
|
+
export const MIHOMO_GEO_ASSETS = [
|
|
36
|
+
// mihomo's default `geoip.metadb` (mmdb mode) — what `GEOIP` reads.
|
|
37
|
+
{ key: 'geoip', file: 'geoip.metadb', url: `${GEO_DOWNLOAD_BASE}/geoip.metadb` },
|
|
38
|
+
// Saved under the capitalized name mihomo looks for in its work dir.
|
|
39
|
+
{ key: 'geosite', file: 'GeoSite.dat', url: `${GEO_DOWNLOAD_BASE}/geosite.dat` },
|
|
40
|
+
];
|
|
41
|
+
/** True when any row needs the geo databases. */
|
|
42
|
+
export function rulesNeedGeo(rules) {
|
|
43
|
+
return (rules ?? []).some((rule) => rule.type === 'GEOIP' || rule.type === 'GEOSITE');
|
|
44
|
+
}
|
|
45
|
+
function fileInfo(dir, asset) {
|
|
46
|
+
try {
|
|
47
|
+
const stat = fs.statSync(path.join(dir, asset.file));
|
|
48
|
+
const exists = stat.isFile() && stat.size > 0;
|
|
49
|
+
const stale = exists && Date.now() - stat.mtimeMs > MIHOMO_GEO_STALE_DAYS * 86_400_000;
|
|
50
|
+
return {
|
|
51
|
+
key: asset.key,
|
|
52
|
+
file: asset.file,
|
|
53
|
+
exists,
|
|
54
|
+
size: stat.size,
|
|
55
|
+
...(exists ? { updatedAt: stat.mtimeMs } : {}),
|
|
56
|
+
stale,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
return { key: asset.key, file: asset.file, exists: false, size: 0, stale: false };
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
export function geoReport(dir) {
|
|
64
|
+
const files = MIHOMO_GEO_ASSETS.map((asset) => fileInfo(dir, asset));
|
|
65
|
+
return { present: files.every((file) => file.exists), files };
|
|
66
|
+
}
|
|
67
|
+
export function geoAssetsPresent(dir) {
|
|
68
|
+
return geoReport(dir).present;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* The proxy URL the machine leg should dial for `url`: the OS system proxy
|
|
72
|
+
* unless the host sits on its bypass list. `undefined` means connect directly.
|
|
73
|
+
*/
|
|
74
|
+
export function machineLegProxy(endpoint, url) {
|
|
75
|
+
if (!endpoint)
|
|
76
|
+
return undefined;
|
|
77
|
+
let host;
|
|
78
|
+
try {
|
|
79
|
+
host = new URL(url).hostname.replace(/^\[|\]$/g, '');
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
if (osProxyBypasses(host, endpoint.bypass))
|
|
85
|
+
return undefined;
|
|
86
|
+
const server = endpoint.server.trim();
|
|
87
|
+
if (!server)
|
|
88
|
+
return undefined;
|
|
89
|
+
return /^https?:\/\//i.test(server) ? server : `http://${server}`;
|
|
90
|
+
}
|
|
91
|
+
function inputUrl(input) {
|
|
92
|
+
if (typeof input === 'string')
|
|
93
|
+
return input;
|
|
94
|
+
if (input instanceof URL)
|
|
95
|
+
return input.toString();
|
|
96
|
+
return input.url;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* The fetch implementation the geo download runs on.
|
|
100
|
+
*
|
|
101
|
+
* - `machine` — the way this box's other applications leave: through the OS
|
|
102
|
+
* system proxy when one is enabled (bypass list honoured), directly when
|
|
103
|
+
* none is. Needs no running core. The check runs per request, so the
|
|
104
|
+
* redirect to `objects.githubusercontent.com` is judged on its own host.
|
|
105
|
+
* - `node` — through the core's mixed port, i.e. the operator's selected
|
|
106
|
+
* node. The caller must refuse this leg while the core is down; by the time
|
|
107
|
+
* a request lands here the core is expected to be listening.
|
|
108
|
+
*/
|
|
109
|
+
export async function geoFetchForLeg(leg, opts = {}) {
|
|
110
|
+
if (leg === 'node') {
|
|
111
|
+
const port = opts.mixedPort;
|
|
112
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535) {
|
|
113
|
+
throw new Error('mixed port unavailable for the node leg');
|
|
114
|
+
}
|
|
115
|
+
const proxy = `http://127.0.0.1:${port}`;
|
|
116
|
+
return (input, init) => fetchViaProxy(inputUrl(input), init ?? {}, proxy);
|
|
117
|
+
}
|
|
118
|
+
const endpoint = await (opts.osProxyReader ?? readOsProxyEndpoint)();
|
|
119
|
+
return (input, init) => {
|
|
120
|
+
const url = inputUrl(input);
|
|
121
|
+
const proxy = machineLegProxy(endpoint, url);
|
|
122
|
+
return proxy ? fetchViaProxy(url, init ?? {}, proxy) : fetch(input, init);
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* The digest MetaCubeX publishes for one asset, or undefined when the host
|
|
127
|
+
* would not give one. Best-effort by design: a refused `.sha256sum` costs the
|
|
128
|
+
* verification, never the download — the operator still gets the database, and
|
|
129
|
+
* the transfer is still resumable from the recorded ETag.
|
|
130
|
+
*/
|
|
131
|
+
async function resolveAssetDigest(fetchImpl, url, headers, signal) {
|
|
132
|
+
try {
|
|
133
|
+
const res = await fetchImpl(`${url}${SHA256SUM_SUFFIX}`, { headers, ...(signal ? { signal } : {}) });
|
|
134
|
+
if (!res.ok)
|
|
135
|
+
return undefined;
|
|
136
|
+
return parseAssetDigest((await res.text()).trim().split(/\s+/)[0]);
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
return undefined;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
async function fetchAsset(asset, dir, fetchImpl, onProgress, signal) {
|
|
143
|
+
const dest = path.join(dir, asset.file);
|
|
144
|
+
const { partPath } = assetPartPaths(dest);
|
|
145
|
+
const headers = { 'User-Agent': USER_AGENT, Accept: 'text/plain' };
|
|
146
|
+
const digest = await resolveAssetDigest(fetchImpl, asset.url, headers, signal);
|
|
147
|
+
const downloaded = await downloadAssetToFile({
|
|
148
|
+
url: asset.url,
|
|
149
|
+
partPath,
|
|
150
|
+
fetchImpl,
|
|
151
|
+
maxBytes: MAX_GEO_BYTES,
|
|
152
|
+
...(digest ? { sha256: digest } : {}),
|
|
153
|
+
headers: { 'User-Agent': USER_AGENT },
|
|
154
|
+
...(signal ? { signal } : {}),
|
|
155
|
+
...(onProgress ? { onProgress: (bytes, total) => onProgress(asset.key, bytes, total) } : {}),
|
|
156
|
+
});
|
|
157
|
+
if (!downloaded.ok) {
|
|
158
|
+
if (downloaded.code === 'download-too-large') {
|
|
159
|
+
return { ok: false, code: 'geo-download-too-large', error: 'geo data download exceeded 64MB' };
|
|
160
|
+
}
|
|
161
|
+
if (downloaded.code === 'write-failed') {
|
|
162
|
+
const message = downloaded.error.includes('(') ? downloaded.error : `(${downloaded.error})`;
|
|
163
|
+
return { ok: false, code: 'geo-download-failed', error: `could not write ${asset.file} ${message}` };
|
|
164
|
+
}
|
|
165
|
+
if (downloaded.code === 'checksum-failed') {
|
|
166
|
+
// Distinct from a network failure: the host served a file that is not the
|
|
167
|
+
// published database, so retrying cannot help until upstream is fixed.
|
|
168
|
+
return { ok: false, code: 'checksum-failed', error: `${asset.file} did not match the published digest` };
|
|
169
|
+
}
|
|
170
|
+
// A cancelled or truncated transfer keeps its `.part`: the next sync picks up
|
|
171
|
+
// where this one stopped. Every other failure already destroyed the part.
|
|
172
|
+
return { ok: false, code: 'geo-download-failed', error: downloaded.error };
|
|
173
|
+
}
|
|
174
|
+
// The verified bytes already sit in the staging file, so the install is the
|
|
175
|
+
// rename alone: they are never copied back through memory, and the database
|
|
176
|
+
// mihomo parses is never a half-written one.
|
|
177
|
+
try {
|
|
178
|
+
if (fs.statSync(partPath).size === 0) {
|
|
179
|
+
cleanupAssetPart(partPath);
|
|
180
|
+
return { ok: false, code: 'geo-download-failed', error: `geo data download was empty (${asset.file})` };
|
|
181
|
+
}
|
|
182
|
+
renameWithRetry(partPath, dest);
|
|
183
|
+
}
|
|
184
|
+
catch (err) {
|
|
185
|
+
cleanupAssetPart(partPath);
|
|
186
|
+
const message = err instanceof Error ? err.message : 'write failed';
|
|
187
|
+
return { ok: false, code: 'geo-download-failed', error: `could not install ${asset.file} (${message})` };
|
|
188
|
+
}
|
|
189
|
+
// The part is gone with the rename; this clears its sidecar.
|
|
190
|
+
cleanupAssetPart(partPath);
|
|
191
|
+
return { ok: true };
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Install the geo databases into the core's work directory. Existing non-empty
|
|
195
|
+
* files are kept unless `refresh` asks for replacement (a retry after a partial
|
|
196
|
+
* failure only fetches the missing one); a file that fails to download or write
|
|
197
|
+
* never replaces a working copy. Each asset is one critical section (ADR-0022):
|
|
198
|
+
* a second click waits instead of writing the same part file underneath it.
|
|
199
|
+
*/
|
|
200
|
+
export async function downloadMihomoGeoAssets(dir, opts = {}) {
|
|
201
|
+
const fetchImpl = opts.fetchImpl ?? fetch;
|
|
202
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
203
|
+
for (const asset of MIHOMO_GEO_ASSETS) {
|
|
204
|
+
if (!opts.refresh && fileInfo(dir, asset).exists)
|
|
205
|
+
continue;
|
|
206
|
+
const outcome = await withAssetLock(path.join(dir, asset.file), () => fetchAsset(asset, dir, fetchImpl, opts.onProgress, opts.signal));
|
|
207
|
+
if (!outcome.ok) {
|
|
208
|
+
return { ok: false, present: geoAssetsPresent(dir), files: geoReport(dir).files, code: outcome.code, error: outcome.error };
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
const report = geoReport(dir);
|
|
212
|
+
return { ok: true, present: report.present, files: report.files };
|
|
213
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** `gateway` marks lines this process writes itself (spawn/exit markers), not core output. */
|
|
2
|
+
export type MihomoLogStream = 'stdout' | 'stderr' | 'gateway';
|
|
3
|
+
export interface MihomoCoreLogLine {
|
|
4
|
+
id: string;
|
|
5
|
+
ts: number;
|
|
6
|
+
stream: MihomoLogStream;
|
|
7
|
+
text: string;
|
|
8
|
+
}
|
|
9
|
+
export declare function appendMihomoCoreLog(stream: MihomoLogStream, raw: string): void;
|
|
10
|
+
/** Oldest first; `limit` keeps the newest N. */
|
|
11
|
+
export declare function mihomoCoreLogs(limit?: number): MihomoCoreLogLine[];
|
|
12
|
+
export declare function clearMihomoCoreLogs(): void;
|
|
13
|
+
/** The slice of a spawned child this module consumes (structural, so tests and
|
|
14
|
+
* the sidecar's `MihomoChildLike` both fit without importing each other). */
|
|
15
|
+
export interface MihomoLogSource {
|
|
16
|
+
stdout?: {
|
|
17
|
+
on(event: string, listener: (chunk: unknown) => void): void;
|
|
18
|
+
} | null;
|
|
19
|
+
stderr?: {
|
|
20
|
+
on(event: string, listener: (chunk: unknown) => void): void;
|
|
21
|
+
} | null;
|
|
22
|
+
}
|
|
23
|
+
/** Split both pipes into lines as they arrive. Partial lines are held until
|
|
24
|
+
* their newline shows up (mihomo writes one record per line). `sink` receives
|
|
25
|
+
* the same cleaned text the ring stores — the sidecar uses it to tell a bind
|
|
26
|
+
* failure apart from a slow start while the process is still its own. */
|
|
27
|
+
export declare function attachMihomoCoreLog(source: MihomoLogSource, sink?: (line: string) => void): void;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-memory ring of the Clash core's own stdout/stderr (ADR-0022).
|
|
3
|
+
*
|
|
4
|
+
* The core is spawned with piped stdio and every line lands here, so the
|
|
5
|
+
* console's Logs section can answer "why did it refuse to start / die" — the
|
|
6
|
+
* evidence the controller cannot provide once the process is gone. The ring
|
|
7
|
+
* outlives a crashed core on purpose and is memory-only: nothing is written to
|
|
8
|
+
* disk, and neither the controller secret nor a subscription URL can appear
|
|
9
|
+
* (the core prints neither; the generated config holds them).
|
|
10
|
+
*/
|
|
11
|
+
import { EventRing, MIHOMO_LOG_RING_CAPACITY, nextRingEventId, } from '../utils/event-ring.js';
|
|
12
|
+
/** One captured line is a diagnostic sample, not a log archive. */
|
|
13
|
+
const LINE_MAX_CHARS = 2000;
|
|
14
|
+
/** A stream that never emits a newline must not grow the pending buffer forever. */
|
|
15
|
+
const PENDING_MAX_CHARS = 8192;
|
|
16
|
+
/** Colour codes are terminal decoration; a captured line must not carry them. */
|
|
17
|
+
const ANSI_RE = /\x1b\[[0-9;]*m/g;
|
|
18
|
+
const ring = new EventRing(undefined, MIHOMO_LOG_RING_CAPACITY);
|
|
19
|
+
export function appendMihomoCoreLog(stream, raw) {
|
|
20
|
+
const text = raw.replace(ANSI_RE, '').replace(/\r$/, '');
|
|
21
|
+
if (!text.trim())
|
|
22
|
+
return;
|
|
23
|
+
const ts = Date.now();
|
|
24
|
+
ring.append({
|
|
25
|
+
id: nextRingEventId('l-', ts),
|
|
26
|
+
ts,
|
|
27
|
+
stream,
|
|
28
|
+
text: text.length > LINE_MAX_CHARS ? `${text.slice(0, LINE_MAX_CHARS)}…` : text,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
/** Oldest first; `limit` keeps the newest N. */
|
|
32
|
+
export function mihomoCoreLogs(limit) {
|
|
33
|
+
const rows = ring.rows();
|
|
34
|
+
if (limit == null || limit >= rows.length)
|
|
35
|
+
return rows;
|
|
36
|
+
return limit > 0 ? rows.slice(rows.length - limit) : [];
|
|
37
|
+
}
|
|
38
|
+
export function clearMihomoCoreLogs() {
|
|
39
|
+
ring.clear();
|
|
40
|
+
}
|
|
41
|
+
/** Split both pipes into lines as they arrive. Partial lines are held until
|
|
42
|
+
* their newline shows up (mihomo writes one record per line). `sink` receives
|
|
43
|
+
* the same cleaned text the ring stores — the sidecar uses it to tell a bind
|
|
44
|
+
* failure apart from a slow start while the process is still its own. */
|
|
45
|
+
export function attachMihomoCoreLog(source, sink) {
|
|
46
|
+
const streams = [
|
|
47
|
+
['stdout', source.stdout],
|
|
48
|
+
['stderr', source.stderr],
|
|
49
|
+
];
|
|
50
|
+
const emit = (stream, line) => {
|
|
51
|
+
appendMihomoCoreLog(stream, line);
|
|
52
|
+
sink?.(line.replace(ANSI_RE, '').replace(/\r$/, ''));
|
|
53
|
+
};
|
|
54
|
+
for (const [stream, pipe] of streams) {
|
|
55
|
+
if (!pipe)
|
|
56
|
+
continue;
|
|
57
|
+
let pending = '';
|
|
58
|
+
pipe.on('data', (chunk) => {
|
|
59
|
+
pending += typeof chunk === 'string' ? chunk : String(chunk);
|
|
60
|
+
const lines = pending.split(/\r?\n/);
|
|
61
|
+
pending = lines.pop() ?? '';
|
|
62
|
+
for (const line of lines)
|
|
63
|
+
emit(stream, line);
|
|
64
|
+
if (pending.length > PENDING_MAX_CHARS) {
|
|
65
|
+
emit(stream, pending);
|
|
66
|
+
pending = '';
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -17,6 +17,17 @@
|
|
|
17
17
|
*/
|
|
18
18
|
/** Served at the gateway root, outside /api (browsers fetch it unauthenticated). */
|
|
19
19
|
export declare const PAC_PATH = "/proxy.pac";
|
|
20
|
+
/**
|
|
21
|
+
* Absolute PAC URL for the client that reached the gateway.
|
|
22
|
+
*
|
|
23
|
+
* The request's own Host is the only origin that is guaranteed to reach this
|
|
24
|
+
* listener: a dev console is served by Vite on another port, and handing out
|
|
25
|
+
* `window.location.origin` would tie the OS/browser auto-config to a dev
|
|
26
|
+
* server that dies with the terminal. `changeOrigin` in the Vite proxy
|
|
27
|
+
* rewrites Host to the gateway, and a production console is served by the
|
|
28
|
+
* gateway itself, so both paths yield the gateway origin.
|
|
29
|
+
*/
|
|
30
|
+
export declare function pacUrlFor(requestHost: string | undefined, protocol?: string, gatewayPort?: number): string;
|
|
20
31
|
export interface PacScriptOptions {
|
|
21
32
|
/** The mihomo mixed port (HTTP + SOCKS5 on one listener). */
|
|
22
33
|
proxyPort: number;
|
|
@@ -15,8 +15,23 @@
|
|
|
15
15
|
* What is deliberately NOT mirrored: `proxy.includes` / `proxy.excludes` match
|
|
16
16
|
* provider/model names, not hosts, so they have no meaning in a PAC script.
|
|
17
17
|
*/
|
|
18
|
+
import { DEFAULT_GATEWAY_PORT } from '../config/defaults.js';
|
|
18
19
|
/** Served at the gateway root, outside /api (browsers fetch it unauthenticated). */
|
|
19
20
|
export const PAC_PATH = '/proxy.pac';
|
|
21
|
+
/**
|
|
22
|
+
* Absolute PAC URL for the client that reached the gateway.
|
|
23
|
+
*
|
|
24
|
+
* The request's own Host is the only origin that is guaranteed to reach this
|
|
25
|
+
* listener: a dev console is served by Vite on another port, and handing out
|
|
26
|
+
* `window.location.origin` would tie the OS/browser auto-config to a dev
|
|
27
|
+
* server that dies with the terminal. `changeOrigin` in the Vite proxy
|
|
28
|
+
* rewrites Host to the gateway, and a production console is served by the
|
|
29
|
+
* gateway itself, so both paths yield the gateway origin.
|
|
30
|
+
*/
|
|
31
|
+
export function pacUrlFor(requestHost, protocol = 'http', gatewayPort) {
|
|
32
|
+
const host = requestHost?.trim() || `127.0.0.1:${gatewayPort ?? DEFAULT_GATEWAY_PORT}`;
|
|
33
|
+
return `${protocol}://${host}${PAC_PATH}`;
|
|
34
|
+
}
|
|
20
35
|
/** RFC1918 / link-local / CGNAT ranges, spelled for `shExpMatch`. */
|
|
21
36
|
const PRIVATE_RANGES = [
|
|
22
37
|
'10.*',
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { MihomoCustomRule } from '../config/types.js';
|
|
2
2
|
/** Everything that could not be imported, keyed by why it could not. */
|
|
3
3
|
export interface RuleImportSkip {
|
|
4
|
-
/** `
|
|
4
|
+
/** `RULE-SET`, `MATCH`, `LOGICAL`, `unknown-target`, `limit` … */
|
|
5
5
|
reason: string;
|
|
6
6
|
count: number;
|
|
7
7
|
}
|
|
@@ -9,10 +9,12 @@
|
|
|
9
9
|
* gateway's single group. Importing their groups instead would put two
|
|
10
10
|
* selection sets in one config, and the node picked in the console would then
|
|
11
11
|
* apply to only half the traffic.
|
|
12
|
-
* - rule types the core cannot evaluate
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
12
|
+
* - rule types the core cannot evaluate at all — `RULE-SET` (fetches a remote
|
|
13
|
+
* document at load), logical rules, unknown source-scoped variants — are
|
|
14
|
+
* dropped and counted. `GEOIP` / `GEOSITE` are importable: they need the geo
|
|
15
|
+
* databases, but those are an explicit download and the save route refuses a
|
|
16
|
+
* geo-rule table without them, so an imported row can never be silently
|
|
17
|
+
* routed around.
|
|
16
18
|
*/
|
|
17
19
|
import { readFile, stat } from 'node:fs/promises';
|
|
18
20
|
import path from 'node:path';
|
|
@@ -20,10 +22,8 @@ import { parse } from 'yaml';
|
|
|
20
22
|
import { MIHOMO_CUSTOM_RULES_MAX, MIHOMO_CUSTOM_RULE_TYPES } from '../config/index.js';
|
|
21
23
|
const DIRECT_OR_REJECT = new Set(['DIRECT', 'REJECT']);
|
|
22
24
|
const TYPE_SET = new Set(MIHOMO_CUSTOM_RULE_TYPES);
|
|
23
|
-
/** Types that exist in Clash but
|
|
25
|
+
/** Types that exist in Clash but whose semantics these flat rows cannot carry. */
|
|
24
26
|
const UNSUPPORTED_HINT = {
|
|
25
|
-
GEOIP: 'GEOIP',
|
|
26
|
-
GEOSITE: 'GEOSITE',
|
|
27
27
|
'SRC-GEOIP': 'GEOIP',
|
|
28
28
|
'SRC-GEOSITE': 'GEOSITE',
|
|
29
29
|
'IP-ASN': 'IP-ASN',
|
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
import type { ProxyConfig } from '../config/types.js';
|
|
2
2
|
import { resolveMihomoBinary } from './mihomo-binary.js';
|
|
3
|
-
export type MihomoSidecarCode = 'disabled' | 'port-collision' | 'binary-missing' | 'spawn-failed' | 'spawn-exited' | 'not-ready' | 'pid-unknown' | 'kill-failed' | 'reload-failed';
|
|
3
|
+
export type MihomoSidecarCode = 'disabled' | 'port-collision' | 'port-in-use' | 'binary-missing' | 'geo-missing' | 'spawn-failed' | 'spawn-exited' | 'not-ready' | 'pid-unknown' | 'kill-failed' | 'reload-failed';
|
|
4
4
|
export interface MihomoChildLike {
|
|
5
5
|
pid?: number;
|
|
6
6
|
on?(event: string, listener: (...args: any[]) => void): void;
|
|
7
7
|
unref?(): void;
|
|
8
|
+
stdout?: {
|
|
9
|
+
on(event: string, listener: (chunk: unknown) => void): void;
|
|
10
|
+
} | null;
|
|
11
|
+
stderr?: {
|
|
12
|
+
on(event: string, listener: (chunk: unknown) => void): void;
|
|
13
|
+
} | null;
|
|
8
14
|
}
|
|
9
15
|
export type MihomoSpawnLike = (cmd: string, args: string[], opts: Record<string, unknown>) => MihomoChildLike;
|
|
10
16
|
export interface MihomoSidecarDeps {
|
|
@@ -49,6 +55,16 @@ export declare function setMihomoSidecarDepsForTests(deps: MihomoSidecarDeps | n
|
|
|
49
55
|
export declare function mihomoWorkDir(deps?: MihomoSidecarDeps): string;
|
|
50
56
|
/** Persist a generated secret through the normal config writer. Never logs it. */
|
|
51
57
|
export declare function persistMihomoSecret(secret: string): void;
|
|
58
|
+
/**
|
|
59
|
+
* Synchronous teardown for the gateway's own exit handlers (ADR-0022): a core
|
|
60
|
+
* this process spawned is OUR child and must not survive the process that owns
|
|
61
|
+
* it. `process.on('exit')` allows only synchronous work, and the Windows kill
|
|
62
|
+
* is already `taskkill` via spawnSync — so this is the one place that is safe
|
|
63
|
+
* there. An adopted (operator-owned) core never has an entry here and is left
|
|
64
|
+
* running; a hard-killed gateway cannot run any handler, and the next boot
|
|
65
|
+
* adopts the survivor (taking ownership) or reaps it if it holds no port.
|
|
66
|
+
*/
|
|
67
|
+
export declare function stopSpawnedMihomoSync(kill?: (pid: number) => boolean): void;
|
|
52
68
|
export declare function getMihomoStatus(extra?: MihomoSidecarDeps): Promise<MihomoStatus>;
|
|
53
69
|
export declare function startMihomoSidecar(extra?: MihomoSidecarDeps): Promise<MihomoOpResult>;
|
|
54
70
|
export declare function stopMihomoSidecar(extra?: MihomoSidecarDeps): Promise<MihomoOpResult>;
|