lceda-connect 0.2.0 → 0.4.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/CHANGELOG.md +28 -0
- package/README.md +111 -45
- package/dist/{analyze-DBkWgG82.mjs → analyze-TRlZx-Jv.mjs} +2 -190
- package/dist/{cli-B3GGyoP_.mjs → cli-1_urvve3.mjs} +5 -3
- package/dist/{cli-BegYbK44.mjs → cli-C8-HjAP0.mjs} +2 -2
- package/dist/cli-CHbcaMwS.mjs +1 -0
- package/dist/cli-CXk5pvVm.mjs +1253 -0
- package/dist/cli-CZEoeuFW.mjs +1 -0
- package/dist/{cli-DphIx1EQ.mjs → cli-CZmNLW3Q.mjs} +1 -1
- package/dist/{cli-BekOvZjB.mjs → cli-DFqBHofJ.mjs} +19 -3
- package/dist/cli-bridge-server.mjs +1 -1
- package/dist/cli-bridge.mjs +1 -1
- package/dist/cli-cdp.mjs +1 -1
- package/dist/cli-mcp.mjs +1 -1
- package/dist/cli.mjs +1 -1
- package/dist/config-BKbwyvsG.mjs +516 -0
- package/dist/{discover-D4ouuCrn.mjs → discover-DmRYlAlR.mjs} +13 -90
- package/dist/environment-BegP0n2N.mjs +439 -0
- package/dist/environment-Clx1-nDW.mjs +146 -0
- package/dist/http-Cdp6S2rm.mjs +366 -0
- package/dist/http-D7sYy2y4.mjs +1 -0
- package/dist/index.d.mts +226 -6
- package/dist/index.mjs +12 -9
- package/dist/{main-CzMqMptH.mjs → main-B3aaKvqY.mjs} +695 -90
- package/dist/mcpHost-CRoN6MnG.mjs +331 -0
- package/dist/{registry-DaqVJuvF.mjs → registry-3WTFCZsR.mjs} +86 -25
- package/dist/registry-DdJ8LEdd.mjs +1 -0
- package/dist/{server-qYlcXfzp.mjs → server-zw6t1Pmk.mjs} +10 -15
- package/dist/{targets-DYfgkrq3.mjs → session-AQbRX6Ys.mjs} +3 -37
- package/dist/{sideEffects-D33hMchc.mjs → sideEffects-Ds_T-_B4.mjs} +3 -2
- package/dist/stdio-BZPlcfDR.mjs +27 -0
- package/dist/stdio-Du1oLqfJ.mjs +1 -0
- package/dist/targets-8LBEgvbq.mjs +36 -0
- package/dist/term-DlGIoccA.mjs +27 -0
- package/dist/{touchstone-DOhfmSNh.mjs → touchstone-D4Fdg4Om.mjs} +1 -1
- package/dist/{verbs-CD4ZM9Es.mjs → verbs-DjQQTL2k.mjs} +191 -53
- package/dist/windowsHost-CTZXMA8D.mjs +1 -0
- package/dist/windowsHost-CtRidgJp.mjs +309 -0
- package/dist/zip-CAd_yYte.mjs +190 -0
- package/docs/architecture.md +81 -0
- package/docs/environments.md +175 -0
- package/docs/extensions.md +207 -0
- package/docs/quickstart.md +59 -4
- package/docs/transports.md +189 -0
- package/package.json +6 -2
- package/dist/cli-BqILMvnF.mjs +0 -1
- package/dist/cli-BzrDKC7x.mjs +0 -1
- package/dist/registry-5K1G1XmC.mjs +0 -1
- package/dist/server-DNMAlzZJ.mjs +0 -1
|
@@ -1,93 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { r as homeDir } from "./config-BKbwyvsG.mjs";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import fs from "node:fs";
|
|
4
|
-
// src/core/config.ts
|
|
5
|
-
/** 立创EDA专业版默认安装路径(与旧脚本保持一致,可用 LCEDA_PRO_EXE / --exe 覆盖)。 */
|
|
6
|
-
const DEFAULT_PRO_EXE = "D:\\Program Files\\lceda-pro\\lceda-pro.exe";
|
|
7
|
-
const DEFAULT_CDP_PORT$1 = 9222;
|
|
8
|
-
const LOOPBACK = "127.0.0.1";
|
|
9
|
-
/** bridge-server.mjs 的固定契约,改动前请同步核对服务端。 */
|
|
10
|
-
const BRIDGE_PORT_FIRST = 49620;
|
|
11
|
-
const BRIDGE_PORT_LAST = 49629;
|
|
12
|
-
const BRIDGE_SERVICE_ID = "easyeda-bridge";
|
|
13
|
-
const BRIDGE_MAX_LOG_BYTES = 5242880;
|
|
14
|
-
const BRIDGE_START_WAIT_MS = 2e4;
|
|
15
|
-
const BRIDGE_RESTART_DELAY_MS = 3e3;
|
|
16
|
-
/** CDP 启动后等待端点就绪的上限(旧脚本为 60 秒)。 */
|
|
17
|
-
const CDP_START_WAIT_MS = 6e4;
|
|
18
|
-
function intFromEnv(raw, fallback) {
|
|
19
|
-
if (!raw) return fallback;
|
|
20
|
-
const parsed = Number.parseInt(raw, 10);
|
|
21
|
-
return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
|
|
22
|
-
}
|
|
23
|
-
function homeDir(env = process.env) {
|
|
24
|
-
return env.USERPROFILE || env.HOME || homedir();
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* 配置优先级:命令行参数 > 环境变量 > 内置默认值。
|
|
28
|
-
* 目录约定:
|
|
29
|
-
* - CDP 运行态放在 ~/.lceda-connect/cdp(可用 LCEDA_CONNECT_HOME 改根)
|
|
30
|
-
* - Bridge 运行态沿用 %LOCALAPPDATA%\lceda-bridge,保证老的 state.json / bridge.log 继续可用
|
|
31
|
-
*/
|
|
32
|
-
function loadConfig(env = process.env, overrides = {}) {
|
|
33
|
-
const home = homeDir(env);
|
|
34
|
-
const connectHome = env.LCEDA_CONNECT_HOME || path.join(home, ".lceda-connect");
|
|
35
|
-
const cdpDir = path.join(connectHome, "cdp");
|
|
36
|
-
const localAppData = env.LOCALAPPDATA || path.join(home, "AppData", "Local");
|
|
37
|
-
const bridgeDir = env.LCEDA_BRIDGE_RUNTIME || path.join(localAppData, "lceda-bridge");
|
|
38
|
-
return {
|
|
39
|
-
cdp: {
|
|
40
|
-
exePath: overrides.cdp?.exePath ?? env.LCEDA_PRO_EXE ?? "D:\\Program Files\\lceda-pro\\lceda-pro.exe",
|
|
41
|
-
port: overrides.cdp?.port ?? intFromEnv(env.LCEDA_CDP_PORT, 9222),
|
|
42
|
-
bindHost: LOOPBACK,
|
|
43
|
-
runtimeDir: cdpDir,
|
|
44
|
-
pidFile: path.join(cdpDir, "lceda-cdp.pid"),
|
|
45
|
-
logFile: path.join(cdpDir, "lceda-cdp.log"),
|
|
46
|
-
legacyPidFile: path.join(home, ".lceda-cdp", "lceda-cdp.pid"),
|
|
47
|
-
startWaitMs: CDP_START_WAIT_MS
|
|
48
|
-
},
|
|
49
|
-
bridge: {
|
|
50
|
-
portFirst: BRIDGE_PORT_FIRST,
|
|
51
|
-
portLast: BRIDGE_PORT_LAST,
|
|
52
|
-
bindHost: LOOPBACK,
|
|
53
|
-
serviceId: BRIDGE_SERVICE_ID,
|
|
54
|
-
runtimeDir: bridgeDir,
|
|
55
|
-
logFile: path.join(bridgeDir, "bridge.log"),
|
|
56
|
-
stateFile: path.join(bridgeDir, "state.json"),
|
|
57
|
-
maxLogBytes: BRIDGE_MAX_LOG_BYTES,
|
|
58
|
-
startWaitMs: BRIDGE_START_WAIT_MS,
|
|
59
|
-
restartDelayMs: BRIDGE_RESTART_DELAY_MS,
|
|
60
|
-
nodePath: overrides.bridge?.nodePath ?? env.LCEDA_NODE,
|
|
61
|
-
bridgeScript: overrides.bridge?.bridgeScript ?? env.LCEDA_BRIDGE_SCRIPT
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
// src/core/term.ts
|
|
66
|
-
/** 极简 ANSI 着色:非 TTY / NO_COLOR / --json 场景自动降级为纯文本。 */
|
|
67
|
-
const COLOR_ENABLED = (() => {
|
|
68
|
-
const env = process.env;
|
|
69
|
-
if (env.NO_COLOR !== void 0 && env.NO_COLOR !== "") return false;
|
|
70
|
-
if (env.FORCE_COLOR !== void 0 && env.FORCE_COLOR !== "0") return true;
|
|
71
|
-
return Boolean(process.stdout.isTTY);
|
|
72
|
-
})();
|
|
73
|
-
function painter(open, close) {
|
|
74
|
-
return (text) => COLOR_ENABLED ? `\u001B[${open}m${text}\u001B[${close}m` : text;
|
|
75
|
-
}
|
|
76
|
-
const paint = {
|
|
77
|
-
cyan: painter(36, 39),
|
|
78
|
-
green: painter(32, 39),
|
|
79
|
-
yellow: painter(33, 39),
|
|
80
|
-
red: painter(31, 39),
|
|
81
|
-
gray: painter(90, 39),
|
|
82
|
-
white: painter(97, 39),
|
|
83
|
-
darkRed: painter(31, 39),
|
|
84
|
-
bold: painter(1, 22),
|
|
85
|
-
dim: painter(2, 22)
|
|
86
|
-
};
|
|
87
|
-
/** 中文对齐时 `-` 分隔线(按显示宽度无关的固定长度即可)。 */
|
|
88
|
-
function hr(width = 58) {
|
|
89
|
-
return "-".repeat(width);
|
|
90
|
-
}
|
|
91
4
|
// src/cdp/errors.ts
|
|
92
5
|
/**
|
|
93
6
|
* CDP 客户端的领域错误。
|
|
@@ -163,6 +76,16 @@ function requireLoopbackHost(host) {
|
|
|
163
76
|
if (!isLoopbackHost(host)) throw new CdpError("LOOPBACK_ONLY", `只允许回环地址,拒绝连接 ${host}`, { exitCode: 3 });
|
|
164
77
|
}
|
|
165
78
|
/**
|
|
79
|
+
* 允许回环地址,或显式列出的「当前环境的宿主地址」(WSL2 → Windows 宿主)。
|
|
80
|
+
* 边界仍然是本机:列表只能由环境发现或用户显式配置产生,不接受任意远端主机。
|
|
81
|
+
*/
|
|
82
|
+
function requireAllowedHost(host, allowedHosts = []) {
|
|
83
|
+
if (isLoopbackHost(host)) return;
|
|
84
|
+
const target = host.trim().toLowerCase();
|
|
85
|
+
if (allowedHosts.some((allowed) => allowed.trim().toLowerCase() === target)) return;
|
|
86
|
+
throw new CdpError("LOOPBACK_ONLY", `只允许回环地址或当前环境的宿主地址,拒绝连接 ${host}`, { exitCode: 3 });
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
166
89
|
* 候选 profile 根目录:Windows 扫 %LOCALAPPDATA% 下以 LCEDA / EasyEDA 开头的目录;
|
|
167
90
|
* 其他平台对应 `~/.config`。返回排序后的绝对路径。
|
|
168
91
|
*/
|
|
@@ -293,7 +216,7 @@ function attributionOf(item) {
|
|
|
293
216
|
* 的那份才是当前实例;没有匹配项时才退回「活但未记录 uuid」的候选(cli / scan)。
|
|
294
217
|
*/
|
|
295
218
|
async function discover(host = DEFAULT_CDP_HOST, options = {}) {
|
|
296
|
-
|
|
219
|
+
requireAllowedHost(host, options.allowedHosts);
|
|
297
220
|
const candidates = (options.records ?? readDevToolsActivePorts(profileRoots(options.env ?? process.env, options.platform ?? process.platform))).map((record) => ({
|
|
298
221
|
port: record.port,
|
|
299
222
|
source: "DevToolsActivePort",
|
|
@@ -368,4 +291,4 @@ function parsePortRange(text) {
|
|
|
368
291
|
if (low <= 0 || low > 65535 || high < low || high > 65535) return null;
|
|
369
292
|
return [low, high];
|
|
370
293
|
}
|
|
371
|
-
export {
|
|
294
|
+
export { CdpError as _, defaultPortFromEnv as a, coerceText as b, fetchJson as c, profileLabel as d, requireAllowedHost as f, versionWebSocketUrl as g, versionBrowser as h, browserUuid as i, parseBrowserUuidLine as l, resolveEndpoint as m, DEFAULT_CDP_PORT as n, discover as o, requireLoopbackHost as p, attributionOf as r, endpointUrl as s, DEFAULT_CDP_HOST as t, parsePortRange as u, asCdpError as v, clipText as y };
|
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import { spawnSync } from "node:child_process";
|
|
3
|
+
// src/core/environment.ts
|
|
4
|
+
const KIND_ALIASES = {
|
|
5
|
+
auto: "unknown",
|
|
6
|
+
"": "unknown",
|
|
7
|
+
win: "windows-native",
|
|
8
|
+
win32: "windows-native",
|
|
9
|
+
windows: "windows-native",
|
|
10
|
+
"windows-native": "windows-native",
|
|
11
|
+
"native-windows": "windows-native",
|
|
12
|
+
wsl: "wsl2",
|
|
13
|
+
wsl2: "wsl2",
|
|
14
|
+
"wsl-2": "wsl2",
|
|
15
|
+
"wsl2-native": "wsl2",
|
|
16
|
+
linux: "linux-native",
|
|
17
|
+
"linux-native": "linux-native",
|
|
18
|
+
mac: "macos-native",
|
|
19
|
+
macos: "macos-native",
|
|
20
|
+
darwin: "macos-native",
|
|
21
|
+
"macos-native": "macos-native"
|
|
22
|
+
};
|
|
23
|
+
/** 把用户输入归一成环境枚举;不认识时返回 null(调用方负责报错)。 */
|
|
24
|
+
function normalizeEnvironmentKind(raw) {
|
|
25
|
+
if (raw === null || raw === void 0) return null;
|
|
26
|
+
const key = raw.trim().toLowerCase();
|
|
27
|
+
if (key === "auto") return null;
|
|
28
|
+
return KIND_ALIASES[key] ?? null;
|
|
29
|
+
}
|
|
30
|
+
function parseNetworkingMode(raw) {
|
|
31
|
+
const value = (raw ?? "").trim().toLowerCase();
|
|
32
|
+
if (value === "nat") return "nat";
|
|
33
|
+
if (value === "mirrored") return "mirrored";
|
|
34
|
+
return "unknown";
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* 解析客户端实例位置优先级。接受 `"vm,host"`、`"host"`、`["vm","host"]`。
|
|
38
|
+
* 未知词与重复项会被丢弃(未知词记警告由调用方处理)。
|
|
39
|
+
*/
|
|
40
|
+
function parseClientLocations(raw) {
|
|
41
|
+
if (raw === null || raw === void 0) return {
|
|
42
|
+
locations: [],
|
|
43
|
+
unknown: []
|
|
44
|
+
};
|
|
45
|
+
const items = (typeof raw === "string" ? raw.split(",") : [...raw]).map((item) => item.trim().toLowerCase()).filter((item) => item.length > 0);
|
|
46
|
+
const locations = [];
|
|
47
|
+
const unknown = [];
|
|
48
|
+
for (const item of items) {
|
|
49
|
+
if (item !== "host" && item !== "vm") {
|
|
50
|
+
unknown.push(item);
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (!locations.includes(item)) locations.push(item);
|
|
54
|
+
}
|
|
55
|
+
return {
|
|
56
|
+
locations,
|
|
57
|
+
unknown
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function hostOsOf(kind) {
|
|
61
|
+
switch (kind) {
|
|
62
|
+
case "windows-native":
|
|
63
|
+
case "wsl2": return "windows";
|
|
64
|
+
case "linux-native": return "linux";
|
|
65
|
+
case "macos-native": return "macos";
|
|
66
|
+
default: return "unknown";
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
function pathStyleOf(platform) {
|
|
70
|
+
return platform === "win32" ? "windows" : "posix";
|
|
71
|
+
}
|
|
72
|
+
/** 是否是回环地址(与 cdp/discover 的判定保持一致,避免 core 反向依赖 cdp)。 */
|
|
73
|
+
function isLoopbackAddress(host) {
|
|
74
|
+
const value = host.trim().toLowerCase().replace(/^\[|\]$/g, "");
|
|
75
|
+
return value === "localhost" || value === "::1" || /^127(\.\d{1,3}){3}$/.test(value);
|
|
76
|
+
}
|
|
77
|
+
function isWindowsDrivePath(value) {
|
|
78
|
+
return /^[A-Za-z]:[\\/]/.test(value);
|
|
79
|
+
}
|
|
80
|
+
function isUncPath(value) {
|
|
81
|
+
return /^\\\\[^\\]+\\/.test(value);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Windows 路径 → WSL 挂载路径(`D:\Program Files\x` → `/mnt/d/Program Files/x`)。
|
|
85
|
+
* 已经是 POSIX 路径时原样返回。不做 `wslpath` 调用:规则固定且可测。
|
|
86
|
+
*/
|
|
87
|
+
function windowsPathToWslPath(value, mountRoot = "/mnt") {
|
|
88
|
+
const trimmed = value.trim();
|
|
89
|
+
if (!trimmed) return trimmed;
|
|
90
|
+
if (isWindowsDrivePath(trimmed)) return `${mountRoot}/${trimmed[0]?.toLowerCase() ?? ""}/${trimmed.slice(2).replace(/\\/g, "/").replace(/^\/+/, "")}`.replace(/\/{2,}/g, "/");
|
|
91
|
+
if (isUncPath(trimmed)) return `//${trimmed.slice(2).replace(/\\/g, "/")}`;
|
|
92
|
+
return trimmed;
|
|
93
|
+
}
|
|
94
|
+
/** WSL 挂载路径 → Windows 路径(`/mnt/d/x` → `D:\x`);不是挂载路径时返回 null。 */
|
|
95
|
+
function wslPathToWindowsPath(value, mountRoot = "/mnt") {
|
|
96
|
+
const trimmed = value.trim();
|
|
97
|
+
const prefix = mountRoot.replace(/\/+$/, "");
|
|
98
|
+
if (!trimmed.toLowerCase().startsWith(`${prefix.toLowerCase()}/`)) return null;
|
|
99
|
+
const rest = trimmed.slice(prefix.length + 1);
|
|
100
|
+
const drive = rest.slice(0, 1);
|
|
101
|
+
if (!/^[A-Za-z]$/.test(drive)) return null;
|
|
102
|
+
const tail = rest.slice(1).replace(/^\/+/, "").replace(/\//g, "\\");
|
|
103
|
+
return `${drive.toUpperCase()}:\\${tail}`;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* 解析 `/proc/net/route` 的默认网关。该文件的地址是主机字节序的 8 位十六进制,
|
|
107
|
+
* 需要按小端还原(`01E012AC` → `172.18.224.1`)。找不到默认路由时返回 null。
|
|
108
|
+
*/
|
|
109
|
+
function parseRouteGateway(table) {
|
|
110
|
+
for (const line of table.split(/\r?\n/)) {
|
|
111
|
+
const columns = line.trim().split(/\s+/);
|
|
112
|
+
if (columns.length < 3) continue;
|
|
113
|
+
if ((columns[1] ?? "").toLowerCase() !== "00000000") continue;
|
|
114
|
+
const hex = (columns[2] ?? "").padStart(8, "0");
|
|
115
|
+
if (!/^[0-9a-f]{8}$/i.test(hex)) continue;
|
|
116
|
+
const bytes = [
|
|
117
|
+
6,
|
|
118
|
+
4,
|
|
119
|
+
2,
|
|
120
|
+
0
|
|
121
|
+
].map((offset) => Number.parseInt(hex.slice(offset, offset + 2), 16));
|
|
122
|
+
if (bytes.every((byte) => byte === 0)) continue;
|
|
123
|
+
return bytes.join(".");
|
|
124
|
+
}
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
/** 取 `/etc/resolv.conf` 的第一个 nameserver(老版本 WSL2 用它指代宿主)。 */
|
|
128
|
+
function parseResolvNameserver(text) {
|
|
129
|
+
for (const line of text.split(/\r?\n/)) {
|
|
130
|
+
const match = /^\s*nameserver\s+(\S+)\s*$/.exec(line);
|
|
131
|
+
if (match?.[1]) return match[1];
|
|
132
|
+
}
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
const IS_WSL_OSRELEASE = /microsoft|wsl/i;
|
|
136
|
+
/** 合并注入探针与默认实现。默认实现刻意保持同步、只读。 */
|
|
137
|
+
function resolveProbes(probes = {}) {
|
|
138
|
+
return {
|
|
139
|
+
platform: probes.platform ?? process.platform,
|
|
140
|
+
env: probes.env ?? process.env,
|
|
141
|
+
readText: probes.readText ?? ((file) => {
|
|
142
|
+
try {
|
|
143
|
+
return fs.readFileSync(file, "utf8");
|
|
144
|
+
} catch {
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
}),
|
|
148
|
+
exists: probes.exists ?? ((file) => {
|
|
149
|
+
try {
|
|
150
|
+
return fs.existsSync(file);
|
|
151
|
+
} catch {
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
}),
|
|
155
|
+
runText: probes.runText ?? ((command, args, timeoutMs) => {
|
|
156
|
+
try {
|
|
157
|
+
const result = spawnSync(command, [...args], {
|
|
158
|
+
timeout: timeoutMs,
|
|
159
|
+
encoding: "utf8",
|
|
160
|
+
windowsHide: true
|
|
161
|
+
});
|
|
162
|
+
if (result.status !== 0) return null;
|
|
163
|
+
return `${result.stdout ?? ""}`.trim() || null;
|
|
164
|
+
} catch {
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
})
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
function validateHost(value) {
|
|
171
|
+
const trimmed = (value ?? "").trim();
|
|
172
|
+
if (!trimmed) return null;
|
|
173
|
+
if (trimmed.startsWith("[") && trimmed.endsWith("]")) return trimmed;
|
|
174
|
+
if (/^[A-Za-z0-9._:-]+$/.test(trimmed)) return trimmed;
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* 探测当前执行环境。显式给了 kind 时不再做自动发现(只保留证据与警告),
|
|
179
|
+
* 这正是 `--env` 的语义。
|
|
180
|
+
*/
|
|
181
|
+
const detectionCache = /* @__PURE__ */ new Map();
|
|
182
|
+
function detectEnvironment(options = {}) {
|
|
183
|
+
const cacheable = options.probes === void 0 && options.cache !== false;
|
|
184
|
+
const cacheKey = [
|
|
185
|
+
process.platform,
|
|
186
|
+
options.kind ?? "",
|
|
187
|
+
options.windowsHost ?? "",
|
|
188
|
+
options.kindSource ?? "",
|
|
189
|
+
options.windowsHostSource ?? "",
|
|
190
|
+
options.clientLocationSource ?? "",
|
|
191
|
+
typeof options.clientLocations === "string" ? options.clientLocations : (options.clientLocations ?? []).join(","),
|
|
192
|
+
process.env.WSL_DISTRO_NAME ?? ""
|
|
193
|
+
].join("|");
|
|
194
|
+
if (cacheable) {
|
|
195
|
+
const hit = detectionCache.get(cacheKey);
|
|
196
|
+
if (hit) return structuredClone(hit);
|
|
197
|
+
}
|
|
198
|
+
const probes = resolveProbes(options.probes);
|
|
199
|
+
const env = probes.env;
|
|
200
|
+
const platform = probes.platform;
|
|
201
|
+
const evidence = [];
|
|
202
|
+
const warnings = [];
|
|
203
|
+
const explicitKind = options.kind ? normalizeEnvironmentKind(options.kind) : null;
|
|
204
|
+
if (options.kind && !explicitKind) warnings.push(`无法识别的环境名 "${options.kind}",已退回自动发现`);
|
|
205
|
+
const osRelease = probes.readText("/proc/sys/kernel/osrelease") ?? "";
|
|
206
|
+
const procVersion = probes.readText("/proc/version") ?? "";
|
|
207
|
+
const wslEnvMarkers = ["WSL_INTEROP", "WSL_DISTRO_NAME"].filter((name) => (env[name] ?? "").trim() !== "");
|
|
208
|
+
const isWsl = platform === "linux" && (IS_WSL_OSRELEASE.test(osRelease) || IS_WSL_OSRELEASE.test(procVersion) || wslEnvMarkers.length > 0);
|
|
209
|
+
if (platform === "linux") {
|
|
210
|
+
if (osRelease.trim()) evidence.push({
|
|
211
|
+
source: "proc:osrelease",
|
|
212
|
+
value: osRelease.trim()
|
|
213
|
+
});
|
|
214
|
+
if (wslEnvMarkers.length > 0) evidence.push({
|
|
215
|
+
source: "env",
|
|
216
|
+
value: `WSL markers: ${wslEnvMarkers.join(", ")}`
|
|
217
|
+
});
|
|
218
|
+
if (!isWsl) evidence.push({
|
|
219
|
+
source: "proc:osrelease",
|
|
220
|
+
value: "未命中 WSL 标识"
|
|
221
|
+
});
|
|
222
|
+
} else evidence.push({
|
|
223
|
+
source: "platform",
|
|
224
|
+
value: platform
|
|
225
|
+
});
|
|
226
|
+
let kind;
|
|
227
|
+
let source;
|
|
228
|
+
if (explicitKind) {
|
|
229
|
+
kind = explicitKind;
|
|
230
|
+
source = options.kindSource ?? "argv";
|
|
231
|
+
evidence.unshift({
|
|
232
|
+
source: `explicit:${source}`,
|
|
233
|
+
value: explicitKind
|
|
234
|
+
});
|
|
235
|
+
if (explicitKind === "windows-native" && platform !== "win32" || explicitKind === "macos-native" && platform !== "darwin" || explicitKind === "linux-native" && platform === "win32" || explicitKind === "wsl2" && platform !== "linux") warnings.push(`显式指定环境 ${explicitKind} 与当前平台 ${platform} 不一致;按显式值执行,自动策略可能失效`);
|
|
236
|
+
} else if (platform === "win32") {
|
|
237
|
+
kind = "windows-native";
|
|
238
|
+
source = "auto-detected";
|
|
239
|
+
} else if (platform === "darwin") {
|
|
240
|
+
kind = "macos-native";
|
|
241
|
+
source = "auto-detected";
|
|
242
|
+
} else if (platform === "linux") {
|
|
243
|
+
kind = isWsl ? "wsl2" : "linux-native";
|
|
244
|
+
source = "auto-detected";
|
|
245
|
+
} else {
|
|
246
|
+
kind = "unknown";
|
|
247
|
+
source = "auto-detected";
|
|
248
|
+
warnings.push(`未识别的平台 ${platform};在线工具按默认回环策略尝试`);
|
|
249
|
+
}
|
|
250
|
+
const distro = isWsl ? (env.WSL_DISTRO_NAME ?? "").trim() || null : null;
|
|
251
|
+
let networkingMode = "unknown";
|
|
252
|
+
if (kind === "wsl2") {
|
|
253
|
+
networkingMode = parseNetworkingMode(probes.runText("wslinfo", ["--networking-mode"], 2e3));
|
|
254
|
+
if (networkingMode === "unknown") warnings.push("无法确认 WSL 网络模式(wslinfo 不可用);按 NAT 的保守策略探测");
|
|
255
|
+
else evidence.push({
|
|
256
|
+
source: "wslinfo",
|
|
257
|
+
value: `networking-mode=${networkingMode}`
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
const explicitHost = validateHost(options.windowsHost);
|
|
261
|
+
if (options.windowsHost && !explicitHost) warnings.push(`--windows-host 值 "${options.windowsHost}" 不像合法地址,已忽略`);
|
|
262
|
+
let windowsHost = null;
|
|
263
|
+
if (kind === "wsl2" && !isWsl && !explicitHost) warnings.push("显式指定 wsl2,但当前进程没有 WSL 特征;跳过宿主地址推导(可用 --windows-host 显式给出)");
|
|
264
|
+
if (kind === "wsl2" && (isWsl || explicitHost)) {
|
|
265
|
+
if (explicitHost) {
|
|
266
|
+
windowsHost = explicitHost;
|
|
267
|
+
evidence.push({
|
|
268
|
+
source: `explicit:${options.windowsHostSource ?? "argv"}`,
|
|
269
|
+
value: `windowsHost=${explicitHost}`
|
|
270
|
+
});
|
|
271
|
+
} else if (networkingMode === "mirrored") {
|
|
272
|
+
windowsHost = "127.0.0.1";
|
|
273
|
+
evidence.push({
|
|
274
|
+
source: "wsl-networking",
|
|
275
|
+
value: "mirrored 共享宿主回环"
|
|
276
|
+
});
|
|
277
|
+
} else {
|
|
278
|
+
const gateway = parseRouteGateway(probes.readText("/proc/net/route") ?? "");
|
|
279
|
+
if (gateway) {
|
|
280
|
+
windowsHost = gateway;
|
|
281
|
+
evidence.push({
|
|
282
|
+
source: "proc:route",
|
|
283
|
+
value: `default gateway ${gateway}`
|
|
284
|
+
});
|
|
285
|
+
} else {
|
|
286
|
+
const nameserver = parseResolvNameserver(probes.readText("/etc/resolv.conf") ?? "");
|
|
287
|
+
if (nameserver) {
|
|
288
|
+
windowsHost = nameserver;
|
|
289
|
+
warnings.push(`宿主地址由 /etc/resolv.conf 推断(${nameserver}),可能不是宿主地址`);
|
|
290
|
+
evidence.push({
|
|
291
|
+
source: "resolv.conf",
|
|
292
|
+
value: `nameserver ${nameserver}`
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
if (kind === "wsl2" && networkingMode === "nat") warnings.push("WSL2 NAT:Windows 回环服务默认不可达;在线工具需在 Windows 侧运行 MCP,或启用 mirrored 网络/端口转发");
|
|
299
|
+
if (kind !== "wsl2" && options.windowsHost) warnings.push("--windows-host 只在 WSL2 下生效;当前环境已忽略");
|
|
300
|
+
const requestedLocations = parseClientLocations(options.clientLocations);
|
|
301
|
+
const locationUnknown = requestedLocations.unknown;
|
|
302
|
+
let clientLocations = requestedLocations.locations;
|
|
303
|
+
let clientLocationSource = options.clientLocationSource ?? "argv";
|
|
304
|
+
if (locationUnknown.length > 0) warnings.push(`忽略无法识别的客户端位置:${locationUnknown.join(", ")}(只接受 host / vm)`);
|
|
305
|
+
if (clientLocations.length === 0) {
|
|
306
|
+
clientLocations = ["host"];
|
|
307
|
+
clientLocationSource = "default";
|
|
308
|
+
}
|
|
309
|
+
if (kind !== "wsl2" && clientLocations.includes("vm")) {
|
|
310
|
+
warnings.push(`环境 ${kind} 下没有 WSL VM,客户端位置 vm 已忽略`);
|
|
311
|
+
clientLocations = clientLocations.filter((item) => item !== "vm");
|
|
312
|
+
if (clientLocations.length === 0) clientLocations = ["host"];
|
|
313
|
+
}
|
|
314
|
+
evidence.push({
|
|
315
|
+
source: `client-locations:${clientLocationSource}`,
|
|
316
|
+
value: clientLocations.join(",")
|
|
317
|
+
});
|
|
318
|
+
const confidence = explicitKind || kind !== "unknown" ? "certain" : "unknown";
|
|
319
|
+
const result = {
|
|
320
|
+
kind,
|
|
321
|
+
hostOs: hostOsOf(kind),
|
|
322
|
+
platform,
|
|
323
|
+
pathStyle: pathStyleOf(platform),
|
|
324
|
+
isWsl,
|
|
325
|
+
distro,
|
|
326
|
+
networkingMode,
|
|
327
|
+
windowsHost,
|
|
328
|
+
clientLocations,
|
|
329
|
+
clientLocationSource,
|
|
330
|
+
explicit: Boolean(explicitKind),
|
|
331
|
+
source,
|
|
332
|
+
confidence,
|
|
333
|
+
evidence,
|
|
334
|
+
warnings
|
|
335
|
+
};
|
|
336
|
+
if (cacheable) detectionCache.set(cacheKey, structuredClone(result));
|
|
337
|
+
return result;
|
|
338
|
+
}
|
|
339
|
+
/** 把环境信息压成一行,给 CLI 与日志用(不要在这里做网络判断)。 */
|
|
340
|
+
function describeEnvironment(info) {
|
|
341
|
+
const parts = [info.kind];
|
|
342
|
+
if (info.distro) parts.push(info.distro);
|
|
343
|
+
if (info.networkingMode !== "unknown") parts.push(`net=${info.networkingMode}`);
|
|
344
|
+
if (info.windowsHost) parts.push(`windowsHost=${info.windowsHost}`);
|
|
345
|
+
if (info.kind === "wsl2") parts.push(`lceda=${info.clientLocations.join(",")}`);
|
|
346
|
+
return parts.join(" · ");
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* 在线通道(CDP)探测地址,按客户端位置优先级展开:
|
|
350
|
+
* - vm:客户端在 WSL 虚拟机内,回环直连;
|
|
351
|
+
* - host:客户端在 Windows 宿主,NAT 下只能先试宿主地址,mirrored 下回环可用。
|
|
352
|
+
*/
|
|
353
|
+
/**
|
|
354
|
+
* 显式探测地址被边界拒绝时给一句可操作警告。
|
|
355
|
+
* 返回 null 表示没给、合法、或本来就在允许范围(回环 / 当前环境宿主地址)。
|
|
356
|
+
*/
|
|
357
|
+
function hostOverrideWarning(info, explicitHost, label) {
|
|
358
|
+
if (explicitHost === null || explicitHost === void 0 || explicitHost.trim() === "") return null;
|
|
359
|
+
const explicit = validateHost(explicitHost);
|
|
360
|
+
if (!explicit) return `${label} "${explicitHost}" 不是合法地址,已忽略`;
|
|
361
|
+
if (isLoopbackAddress(explicit) || explicit === info.windowsHost) return null;
|
|
362
|
+
return `${label} ${explicit} 不在允许范围(只接受回环或当前环境宿主地址 ${info.windowsHost ?? "(无)"}),已忽略`;
|
|
363
|
+
}
|
|
364
|
+
function cdpProbeHosts(info, explicitHost) {
|
|
365
|
+
const explicit = validateHost(explicitHost);
|
|
366
|
+
if (explicit && (isLoopbackAddress(explicit) || explicit === info.windowsHost)) return [explicit];
|
|
367
|
+
const hosts = [];
|
|
368
|
+
for (const location of info.clientLocations) {
|
|
369
|
+
if (location === "vm") {
|
|
370
|
+
hosts.push("127.0.0.1");
|
|
371
|
+
continue;
|
|
372
|
+
}
|
|
373
|
+
if (info.kind === "wsl2" && info.networkingMode !== "mirrored" && info.windowsHost) hosts.push(info.windowsHost);
|
|
374
|
+
hosts.push("127.0.0.1");
|
|
375
|
+
}
|
|
376
|
+
const deduped = [...new Set(hosts)];
|
|
377
|
+
return deduped.length > 0 ? deduped : ["127.0.0.1"];
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* bridge 只监听回环,不能跨命名空间转发;所以探测地址永远是回环。
|
|
381
|
+
* 能否真正可达由 liveStrategy 的 per-location 结论说明。
|
|
382
|
+
*/
|
|
383
|
+
function bridgeProbeHost(info, explicitHost) {
|
|
384
|
+
const explicit = validateHost(explicitHost);
|
|
385
|
+
if (explicit && (isLoopbackAddress(explicit) || explicit === info.windowsHost)) return explicit;
|
|
386
|
+
return "127.0.0.1";
|
|
387
|
+
}
|
|
388
|
+
function locationStrategy(info, location) {
|
|
389
|
+
const isWsl = info.kind === "wsl2";
|
|
390
|
+
const hostCdpHosts = isWsl && info.networkingMode !== "mirrored" && info.windowsHost ? [info.windowsHost, "127.0.0.1"] : ["127.0.0.1"];
|
|
391
|
+
const hostBridgeReachable = !isWsl || info.networkingMode === "mirrored";
|
|
392
|
+
if (location === "vm") return {
|
|
393
|
+
location,
|
|
394
|
+
cdpHosts: ["127.0.0.1"],
|
|
395
|
+
bridgeHost: "127.0.0.1",
|
|
396
|
+
bridgeReachable: true,
|
|
397
|
+
exeStyle: "linux",
|
|
398
|
+
summary: "客户端在 WSL 虚拟机内:与 MCP 共享回环,CDP 与 bridge 都按本机处理"
|
|
399
|
+
};
|
|
400
|
+
return {
|
|
401
|
+
location,
|
|
402
|
+
cdpHosts: hostCdpHosts,
|
|
403
|
+
bridgeHost: "127.0.0.1",
|
|
404
|
+
bridgeReachable: hostBridgeReachable,
|
|
405
|
+
exeStyle: info.hostOs === "windows" ? "windows" : "native",
|
|
406
|
+
summary: isWsl ? `客户端在 Windows 宿主:CDP 探测 ${hostCdpHosts.join(" → ")};${hostBridgeReachable ? "mirrored 下回环可用" : "bridge 跨 OS 不可达"}` : "客户端与 MCP 同 OS:CDP 与 bridge 均走 127.0.0.1"
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
function liveStrategy(info, options = {}) {
|
|
410
|
+
const locations = info.clientLocations.map((location) => locationStrategy(info, location));
|
|
411
|
+
const cdpHosts = cdpProbeHosts(info, options.cdpHost);
|
|
412
|
+
const bridgeHost = bridgeProbeHost(info, options.bridgeHost);
|
|
413
|
+
const loopbackReachable = info.kind !== "wsl2" || info.networkingMode === "mirrored" || info.clientLocations.includes("vm");
|
|
414
|
+
if (info.kind === "wsl2" && !loopbackReachable) return {
|
|
415
|
+
loopbackReachable,
|
|
416
|
+
locations,
|
|
417
|
+
cdpHosts,
|
|
418
|
+
bridgeHost,
|
|
419
|
+
summary: "WSL2 NAT + 客户端在 Windows 宿主:回环与 Windows 不互通,CDP 只可能经宿主地址访问;bridge 不可达",
|
|
420
|
+
nextAction: "推荐在 Windows 侧运行 MCP(npm i -g lceda-connect 后让 MCP 客户端指向 Windows 命令);或启用 WSL mirrored 网络;或把客户端装进 WSL VM 后以 --lceda-in vm 重试"
|
|
421
|
+
};
|
|
422
|
+
if (info.kind === "wsl2") return {
|
|
423
|
+
loopbackReachable,
|
|
424
|
+
locations,
|
|
425
|
+
cdpHosts,
|
|
426
|
+
bridgeHost,
|
|
427
|
+
summary: info.networkingMode === "mirrored" ? "WSL2 mirrored:与宿主共享回环,CDP 与 bridge 都按本机处理" : "WSL2:客户端在 VM 内,回环直连",
|
|
428
|
+
nextAction: "直接按回环通道使用;客户端可执行文件仍要按位置(host→/mnt/...,vm→Linux 路径)选择"
|
|
429
|
+
};
|
|
430
|
+
return {
|
|
431
|
+
loopbackReachable,
|
|
432
|
+
locations,
|
|
433
|
+
cdpHosts,
|
|
434
|
+
bridgeHost,
|
|
435
|
+
summary: "客户端与 MCP 同 OS:CDP 与 bridge 均走 127.0.0.1",
|
|
436
|
+
nextAction: "先启动客户端并开调试端口 / 加载 bridge 扩展"
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
export { hostOverrideWarning as a, liveStrategy as c, detectEnvironment as i, windowsPathToWslPath as l, cdpProbeHosts as n, isLoopbackAddress as o, describeEnvironment as r, isWindowsDrivePath as s, bridgeProbeHost as t, wslPathToWindowsPath as u };
|