lceda-connect 0.3.0 → 0.4.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/CHANGELOG.md +27 -0
- package/README.md +107 -31
- package/dist/{cli-C4ZkWlV2.mjs → cli-D-PphUlG.mjs} +1 -1
- package/dist/{cli-C6saKrQp.mjs → cli-D-kNq1F6.mjs} +18 -3
- package/dist/{cli-CjcJVM6z.mjs → cli-Dja1oai7.mjs} +4 -4
- 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-B3dElEFD.mjs +516 -0
- package/dist/{discover-D5mRrsxg.mjs → discover-D-YAeRWh.mjs} +13 -64
- package/dist/environment-BegP0n2N.mjs +439 -0
- package/dist/environment-MMVlAqxo.mjs +146 -0
- package/dist/http-BNbYGS4p.mjs +1 -0
- package/dist/http-C9RtNg_O.mjs +366 -0
- package/dist/index.d.mts +226 -6
- package/dist/index.mjs +7 -5
- package/dist/{main-CcKvXSJG.mjs → main-CFHsfo5I.mjs} +758 -88
- package/dist/mcpHost-CRoN6MnG.mjs +331 -0
- package/dist/{registry-Dmti2u6s.mjs → registry-Be9DNS6F.mjs} +84 -24
- package/dist/registry-KmkmrusO.mjs +1 -0
- package/dist/{server-BduSjwzu.mjs → server-4ROmddm7.mjs} +9 -14
- package/dist/{session-DvIXO5Hf.mjs → session-BypD6Iab.mjs} +2 -2
- package/dist/{sideEffects-DF_tIVsf.mjs → sideEffects-3OHHy4VU.mjs} +3 -3
- package/dist/stdio-Ji0B6uQT.mjs +1 -0
- package/dist/stdio-rNTpzX2t.mjs +27 -0
- package/dist/{targets-D5nmbsyz.mjs → targets-DVmXbqhN.mjs} +1 -1
- package/dist/{verbs-5M7rj1-E.mjs → verbs-d3wbVcHt.mjs} +191 -54
- package/dist/windowsHost-CTZXMA8D.mjs +1 -0
- package/dist/windowsHost-CtRidgJp.mjs +309 -0
- package/docs/architecture.md +81 -0
- package/docs/environments.md +175 -0
- package/docs/quickstart.md +59 -4
- package/docs/transports.md +191 -0
- package/package.json +5 -2
- package/dist/registry-zIKWeXV6.mjs +0 -1
- package/dist/server-Chka6eAQ.mjs +0 -1
|
@@ -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 };
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { i as loadConfig } from "./config-B3dElEFD.mjs";
|
|
2
|
+
import { c as liveStrategy, r as describeEnvironment } from "./environment-BegP0n2N.mjs";
|
|
3
|
+
import { a as parseArgs, n as boolValue } from "./cliArgs-BI3bJgCZ.mjs";
|
|
4
|
+
import { n as paint, t as hr } from "./term-DlGIoccA.mjs";
|
|
5
|
+
// src/commands/environment.ts
|
|
6
|
+
/**
|
|
7
|
+
* `lceda-connect env` —— 执行环境自检(只读)。
|
|
8
|
+
*
|
|
9
|
+
* 回答四个问题:当前是什么环境、客户端装在哪、在线通道该往哪个地址连、
|
|
10
|
+
* 配置最终来自哪里。不做网络写操作,也不启动客户端。
|
|
11
|
+
*/
|
|
12
|
+
const ENV_OPTIONS = [{
|
|
13
|
+
name: "json",
|
|
14
|
+
type: "boolean",
|
|
15
|
+
description: "以 JSON 输出"
|
|
16
|
+
}, {
|
|
17
|
+
name: "deep",
|
|
18
|
+
type: "boolean",
|
|
19
|
+
description: "额外用 where/which 深探客户端路径(较慢)"
|
|
20
|
+
}];
|
|
21
|
+
function envHelp() {
|
|
22
|
+
return [
|
|
23
|
+
"",
|
|
24
|
+
paint.cyan(" lceda-connect env -- 执行环境与在线通道策略自检"),
|
|
25
|
+
paint.gray(` ${hr()}`),
|
|
26
|
+
" 用法: lceda-connect env [选项]",
|
|
27
|
+
"",
|
|
28
|
+
paint.white(" 选项:"),
|
|
29
|
+
" --json 以 JSON 输出",
|
|
30
|
+
" --deep 额外用 where/which 深探客户端路径(较慢)",
|
|
31
|
+
" --env KIND 显式环境(不做自动发现)",
|
|
32
|
+
" --lceda-in WSL2 客户端位置:host / vm / vm,host(越靠前优先级越高)",
|
|
33
|
+
" --config 配置文件;--env-file 指定 .env",
|
|
34
|
+
"",
|
|
35
|
+
paint.gray(" 例子: lceda-connect env --json"),
|
|
36
|
+
""
|
|
37
|
+
];
|
|
38
|
+
}
|
|
39
|
+
function locationLabel(location) {
|
|
40
|
+
return location === "vm" ? "WSL VM" : "Windows 宿主";
|
|
41
|
+
}
|
|
42
|
+
async function runEnvironmentCommand(argv, io, env, runtime) {
|
|
43
|
+
if (argv.includes("--help") || argv.includes("-h") || argv.includes("help")) {
|
|
44
|
+
io.out(envHelp().join("\n"));
|
|
45
|
+
return 0;
|
|
46
|
+
}
|
|
47
|
+
let parsed;
|
|
48
|
+
try {
|
|
49
|
+
parsed = parseArgs(argv, ENV_OPTIONS);
|
|
50
|
+
} catch (error) {
|
|
51
|
+
io.err(paint.red(` ${error instanceof Error ? error.message : String(error)}`));
|
|
52
|
+
io.out(envHelp().join("\n"));
|
|
53
|
+
return 1;
|
|
54
|
+
}
|
|
55
|
+
if (parsed.positionals.length > 0) {
|
|
56
|
+
io.err(paint.red(` 多余的参数: ${parsed.positionals.join(" ")}`));
|
|
57
|
+
io.out(envHelp().join("\n"));
|
|
58
|
+
return 1;
|
|
59
|
+
}
|
|
60
|
+
const cfg = loadConfig(env, {}, { deepExe: boolValue(parsed, "deep") });
|
|
61
|
+
const strategy = liveStrategy(cfg.environment, {
|
|
62
|
+
cdpHost: cfg.cdp.host,
|
|
63
|
+
bridgeHost: cfg.bridge.host
|
|
64
|
+
});
|
|
65
|
+
const { environment, client } = cfg;
|
|
66
|
+
const payload = {
|
|
67
|
+
environment,
|
|
68
|
+
liveStrategy: strategy,
|
|
69
|
+
cdpProbeHosts: [...cfg.cdp.hosts],
|
|
70
|
+
bridgeProbeHost: cfg.bridge.host,
|
|
71
|
+
clientExe: {
|
|
72
|
+
primary: client.exe ? {
|
|
73
|
+
status: client.exe.status,
|
|
74
|
+
path: client.exe.path,
|
|
75
|
+
hostPath: client.exe.hostPath,
|
|
76
|
+
location: client.exe.location,
|
|
77
|
+
source: client.exe.source
|
|
78
|
+
} : null,
|
|
79
|
+
locations: client.plan.locations.map((item) => ({
|
|
80
|
+
location: item.location,
|
|
81
|
+
status: item.status,
|
|
82
|
+
path: item.path,
|
|
83
|
+
source: item.source,
|
|
84
|
+
candidates: item.candidates
|
|
85
|
+
})),
|
|
86
|
+
warnings: client.plan.warnings
|
|
87
|
+
},
|
|
88
|
+
config: {
|
|
89
|
+
path: runtime?.configPath ?? null,
|
|
90
|
+
envFile: runtime?.envFilePath ?? null,
|
|
91
|
+
sources: runtime?.sources ?? {},
|
|
92
|
+
warnings: runtime?.warnings ?? []
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
if (boolValue(parsed, "json")) {
|
|
96
|
+
io.out(JSON.stringify(payload, null, 2));
|
|
97
|
+
return client.exe ? 0 : 1;
|
|
98
|
+
}
|
|
99
|
+
io.out("");
|
|
100
|
+
io.out(paint.cyan(" 执行环境"));
|
|
101
|
+
io.out(` ${describeEnvironment(environment)}`);
|
|
102
|
+
io.out(` ${paint.gray(`宿主=${environment.hostOs} · 平台=${environment.platform} · 显式=${environment.explicit ? "是" : "否"} · 置信=${environment.confidence}`)}`);
|
|
103
|
+
if (environment.kind === "wsl2") io.out(` 客户端位置优先级: ${environment.clientLocations.map(locationLabel).join(" → ")} ${paint.gray(`(${environment.clientLocationSource})`)}`);
|
|
104
|
+
if (environment.windowsHost) io.out(` Windows 宿主地址: ${environment.windowsHost}`);
|
|
105
|
+
for (const item of environment.evidence) io.out(paint.gray(` · ${item.source}: ${item.value}`));
|
|
106
|
+
io.out("");
|
|
107
|
+
io.out(paint.cyan(" 客户端可执行文件"));
|
|
108
|
+
if (client.exe) {
|
|
109
|
+
io.out(` ${paint.green("OK")} ${client.exe.path}`);
|
|
110
|
+
io.out(paint.gray(` 来源=${client.exe.source} · 位置=${locationLabel(client.exe.location)} · hostPath=${client.exe.hostPath ?? "-"}`));
|
|
111
|
+
} else io.out(` ${paint.yellow("未找到")} 用 --exe 指定,或安装客户端后重试`);
|
|
112
|
+
for (const item of client.plan.locations) {
|
|
113
|
+
if (item.status === "discovered" || item.status === "explicit") continue;
|
|
114
|
+
const tried = item.candidates.slice(0, 6).map((candidate) => candidate.path);
|
|
115
|
+
io.out(paint.gray(` ${locationLabel(item.location)}: ${item.status}`));
|
|
116
|
+
for (const candidate of tried) io.out(paint.gray(` - ${candidate}`));
|
|
117
|
+
if (item.candidates.length > tried.length) io.out(paint.gray(` … 另有 ${item.candidates.length - tried.length} 个候选未显示`));
|
|
118
|
+
}
|
|
119
|
+
io.out("");
|
|
120
|
+
io.out(paint.cyan(" 在线通道策略"));
|
|
121
|
+
io.out(` ${strategy.summary}`);
|
|
122
|
+
for (const item of strategy.locations) io.out(paint.gray(` · ${locationLabel(item.location)}: CDP ${item.cdpHosts.join(" → ")} · bridge ${item.bridgeHost}${item.bridgeReachable ? "" : "(跨命名空间不可达)"}`));
|
|
123
|
+
io.out(` CDP 探测地址: ${strategy.cdpHosts.join(" → ")}`);
|
|
124
|
+
io.out(` 下一步: ${strategy.nextAction}`);
|
|
125
|
+
if (runtime?.configPath || runtime?.envFilePath) {
|
|
126
|
+
io.out("");
|
|
127
|
+
io.out(paint.cyan(" 配置来源"));
|
|
128
|
+
if (runtime.configPath) io.out(` 配置文件: ${runtime.configPath}`);
|
|
129
|
+
if (runtime.envFilePath) io.out(` .env: ${runtime.envFilePath}`);
|
|
130
|
+
const nonDefault = Object.entries(runtime.sources).filter(([, source]) => source !== "default").map(([key, source]) => `${key}=${source}`);
|
|
131
|
+
if (nonDefault.length > 0) io.out(paint.gray(` ${nonDefault.join(" · ")}`));
|
|
132
|
+
}
|
|
133
|
+
const warnings = [
|
|
134
|
+
...environment.warnings,
|
|
135
|
+
...client.plan.warnings,
|
|
136
|
+
...runtime?.warnings ?? []
|
|
137
|
+
];
|
|
138
|
+
if (warnings.length > 0) {
|
|
139
|
+
io.out("");
|
|
140
|
+
io.out(paint.yellow(" 提醒"));
|
|
141
|
+
for (const warning of warnings) io.out(` ! ${warning}`);
|
|
142
|
+
}
|
|
143
|
+
io.out("");
|
|
144
|
+
return client.exe ? 0 : 1;
|
|
145
|
+
}
|
|
146
|
+
export { runEnvironmentCommand };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { a as startHttpTransport } from "./http-C9RtNg_O.mjs";
|