@openchamber/web 1.18.2 → 1.18.4
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/bin/lib/commands-models.js +1 -1
- package/dist/assets/{CodeMirrorCommentWidgets-CxcJ5x9h.js → CodeMirrorCommentWidgets-4qZbrEXU.js} +1 -1
- package/dist/assets/{CommitSection-C9sBBYAJ.js → CommitSection-MJmntVPJ.js} +1 -1
- package/dist/assets/{DiagramEditor-C7nUw7p3.js → DiagramEditor-jnG3Hbxg.js} +1 -1
- package/dist/assets/{DiagramView-cYxZP-HS.js → DiagramView-CFXxgg_a.js} +1 -1
- package/dist/assets/{DiffView-1zd0slLu.js → DiffView-DZIoOnV7.js} +2 -2
- package/dist/assets/FilesView-BKL6YlKG.js +14 -0
- package/dist/assets/{GitView-OSVIn5V1.js → GitView-DBFrDDBv.js} +1 -1
- package/dist/assets/{JsonTreeView-Kgepz4pR.js → JsonTreeView-DLW2Wy1g.js} +1 -1
- package/dist/assets/MarkdownImageGallery-DKIylIK7.js +1 -0
- package/dist/assets/MarkdownRendererImpl-CLv8hxvv.js +12 -0
- package/dist/assets/OnboardingScreen-D2Y6djMw.js +1 -0
- package/dist/assets/{PatchDiff-Cddz02Ya.js → PatchDiff-BuLPx7AN.js} +1 -1
- package/dist/assets/{PierreDiffViewer-B5k0KvPW.js → PierreDiffViewer-BHm4Yr0X.js} +2 -2
- package/dist/assets/{PlanView-BYgHBHDt.js → PlanView-ZYEJn8A_.js} +3 -3
- package/dist/assets/{SessionSwitcherDropdown-ig2OT22Z.js → SessionSwitcherDropdown-rro-SsaO.js} +1 -1
- package/dist/assets/SettingsView-5RmswFZe.js +28 -0
- package/dist/assets/{SettingsWindow-CNHM8ifT.js → SettingsWindow-BZhAYFdS.js} +1 -1
- package/dist/assets/{ToolOutputDialog-DrGeYq2p.js → ToolOutputDialog-Za6V4VMJ.js} +7 -7
- package/dist/assets/{ToolPartDiffPreview-BRmPpe94.js → ToolPartDiffPreview-BL-9ixyC.js} +1 -1
- package/dist/assets/{VirtualizedFileDiff-Dcu8Q-5P.js → VirtualizedFileDiff-CwGocYQd.js} +1 -1
- package/dist/assets/{WalkthroughView-HStEYdD2.js → WalkthroughView-5DyvN03T.js} +2 -2
- package/dist/assets/{computeVirtualFileMetrics-DQE7R9IY.js → computeVirtualFileMetrics-Da00Jg7M.js} +2 -2
- package/dist/assets/contextPanelEmbeddedChat-B-bSg3P5.js +1 -0
- package/dist/assets/de-u7gSnqWs.js +9 -0
- package/dist/assets/es-T9O7aAOr.js +15 -0
- package/dist/assets/fr-Oor7ewbE.js +13 -0
- package/dist/assets/index-BzIpNlNE.css +1 -0
- package/dist/assets/{index-BgQEgA2a.js → index-C0yNhuZw.js} +1 -1
- package/dist/assets/index-DRpChWux.js +7 -0
- package/dist/assets/{isDiffPlainText-XyBCe4eD.js → isDiffPlainText-D9YpoLrV.js} +1 -1
- package/dist/assets/{isFilePlainText-JbqDfa3n.js → isFilePlainText-gTibdNIe.js} +1 -1
- package/dist/assets/ja-DHpqbVmr.js +15 -0
- package/dist/assets/ko-CwLkS3nx.js +15 -0
- package/dist/assets/{main-LfcbGv7C.js → main-D7UcL3VQ.js} +2 -2
- package/dist/assets/main-DPMd2iKS.js +915 -0
- package/dist/assets/markdownCore-DDrHVneM.js +2 -0
- package/dist/assets/{miniChat-CUp_elRz.js → miniChat-O_GVoEbc.js} +2 -2
- package/dist/assets/{mobile-CBSN7BsH.js → mobile-Br4TtAqc.js} +2 -2
- package/dist/assets/{patchFileDiff-DCIXUHRJ.js → patchFileDiff-KNkxM1C1.js} +2 -2
- package/dist/assets/{pl-tHTP6XQx.js → pl-CcZILN8q.js} +4 -4
- package/dist/assets/pt-BR-DYHhVK6o.js +15 -0
- package/dist/assets/{renderElectronMiniChatApp-eZl-5STF.js → renderElectronMiniChatApp-CJpdxTT1.js} +2 -2
- package/dist/assets/renderMobileApp-toj9cXoh.js +25 -0
- package/dist/assets/runtimeEndpointReset-BVmispdD.js +13 -0
- package/dist/assets/scheduledTasksApi-ByKya3Un.js +1 -0
- package/dist/assets/{shikiHighlight-eU8abJ3T.js → shikiHighlight-C7LTbwFx.js} +1 -1
- package/dist/assets/uk-BA2kZR38.js +15 -0
- package/dist/assets/{useAppFontEffects-Bbpz77To.js → useAppFontEffects-BikB6enL.js} +138 -138
- package/dist/assets/useInlineCommentController-CYPvYJDt.js +2 -0
- package/dist/assets/{vendor-codemirror-language-data-Dzt9rWZZ.js → vendor-codemirror-language-data-CLHw8NWn.js} +2 -2
- package/dist/assets/{vendor-marked-i_gIkIP3.js → vendor-marked-CX41MkDF.js} +1 -1
- package/dist/assets/{web-components-DGvnI0zx.js → web-components-GMc3oR3i.js} +1 -1
- package/dist/assets/zh-CN-dlOQK6v9.js +15 -0
- package/dist/assets/zh-TW-BnqQ-WL5.js +15 -0
- package/dist/index.html +4 -4
- package/dist/mini-chat.html +3 -3
- package/dist/mobile.html +3 -3
- package/package.json +2 -2
- package/server/index.js +65 -19
- package/server/lib/agent-tool/DOCUMENTATION.md +21 -6
- package/server/lib/agent-tool/runtime.js +90 -16
- package/server/lib/agent-tool/runtime.test.js +86 -0
- package/server/lib/browser-control/DOCUMENTATION.md +50 -0
- package/server/lib/browser-control/broker.js +154 -0
- package/server/lib/browser-control/broker.test.js +191 -0
- package/server/lib/browser-control/routes.js +48 -0
- package/server/lib/browser-control/routes.test.js +108 -0
- package/server/lib/dev-servers/parse.js +203 -0
- package/server/lib/dev-servers/parse.test.js +174 -0
- package/server/lib/dev-servers/routes.js +135 -0
- package/server/lib/dev-tunnel/DOCUMENTATION.md +57 -0
- package/server/lib/dev-tunnel/client.js +193 -0
- package/server/lib/dev-tunnel/runtime.js +189 -0
- package/server/lib/dev-tunnel/tunnel.test.js +291 -0
- package/server/lib/git/credentials.js +0 -33
- package/server/lib/github/DOCUMENTATION.md +1 -1
- package/server/lib/markdown-image-grants/DOCUMENTATION.md +36 -0
- package/server/lib/markdown-image-grants/routes.js +340 -0
- package/server/lib/markdown-image-grants/routes.test.js +221 -0
- package/server/lib/openchamber-control/DOCUMENTATION.md +9 -0
- package/server/lib/openchamber-control/actions.js +33 -1
- package/server/lib/openchamber-control/screenshots.js +83 -0
- package/server/lib/openchamber-control/screenshots.test.js +84 -0
- package/server/lib/openchamber-control/service.js +141 -2
- package/server/lib/openchamber-control/service.test.js +55 -0
- package/server/lib/opencode/core-routes.js +31 -52
- package/server/lib/opencode/core-routes.test.js +37 -9
- package/server/lib/opencode/feature-routes-runtime.js +15 -0
- package/server/lib/opencode/server-startup-runtime.js +22 -1
- package/server/lib/opencode/server-startup-runtime.test.js +44 -0
- package/server/lib/opencode/settings-helpers.js +3 -9
- package/server/lib/projects/project-config.js +181 -2
- package/server/lib/projects/project-config.test.js +295 -0
- package/server/lib/quota/DOCUMENTATION.md +4 -2
- package/server/lib/quota/credentials/providers.js +0 -7
- package/server/lib/quota/credentials/store.js +11 -1
- package/server/lib/quota/credentials/store.test.js +9 -1
- package/server/lib/quota/providers/opencode-go.js +35 -35
- package/server/lib/quota/providers/opencode-go.test.js +52 -7
- package/server/lib/quota/routes.js +0 -2
- package/server/lib/relay/DOCUMENTATION.md +2 -0
- package/server/lib/relay/tunnel-host.js +157 -33
- package/server/lib/relay/tunnel-host.test.js +147 -0
- package/server/lib/scheduled-tasks/DOCUMENTATION.md +49 -0
- package/server/lib/scheduled-tasks/issue-2710-double-execution.test.js +488 -0
- package/server/lib/scheduled-tasks/routes.js +6 -0
- package/server/lib/scheduled-tasks/runtime.js +369 -108
- package/server/lib/scheduled-tasks/service.js +7 -1
- package/server/lib/scheduled-tasks/service.test.js +20 -0
- package/server/lib/walkthrough/index.js +0 -2
- package/server/sse-routes.test.js +3 -0
- package/dist/assets/FilesView-WkxsWHBE.js +0 -14
- package/dist/assets/MarkdownRendererImpl-CKAsRfIk.js +0 -12
- package/dist/assets/OnboardingScreen-BCgYA7J5.js +0 -1
- package/dist/assets/SettingsView-e7Fmxnz8.js +0 -28
- package/dist/assets/contextPanelEmbeddedChat-5VGWIT_J.js +0 -1
- package/dist/assets/de-B6t6T54h.js +0 -9
- package/dist/assets/es-DH9NUKge.js +0 -15
- package/dist/assets/fr-Do4t2KmA.js +0 -13
- package/dist/assets/index-B45s9aAX.css +0 -1
- package/dist/assets/index-CNrN5qDs.js +0 -7
- package/dist/assets/ja-DJT9EM80.js +0 -15
- package/dist/assets/ko-CfWRq2p4.js +0 -15
- package/dist/assets/main-B4oBRI9u.js +0 -193
- package/dist/assets/markdownCore-A3_L34oS.js +0 -2
- package/dist/assets/pt-BR-BDEzzltP.js +0 -15
- package/dist/assets/renderMobileApp-DA7wpIBi.js +0 -23
- package/dist/assets/runtimeEndpointReset-D5izT-u5.js +0 -13
- package/dist/assets/scheduledTasksApi-CTJdfSWc.js +0 -1
- package/dist/assets/uk-B6Q32nyk.js +0 -15
- package/dist/assets/useInlineCommentController-D7zs7-BQ.js +0 -2
- package/dist/assets/vendor-zumer-snapdom-Btyu_Xi2.js +0 -8
- package/dist/assets/zh-CN-7MGcwGaC.js +0 -15
- package/dist/assets/zh-TW-CFPfqxlL.js +0 -15
- package/server/lib/preview/proxy-runtime.js +0 -1604
- package/server/lib/preview/proxy-runtime.test.js +0 -528
- package/server/lib/quota/opencode-go-credentials.js +0 -11
- package/server/lib/quota/opencode-go-credentials.test.js +0 -32
- package/server/lib/quota/routes.test.js +0 -40
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parsers for listening-socket enumeration.
|
|
3
|
+
*
|
|
4
|
+
* Two platforms, two formats, one shape out. Both parsers are pure so the
|
|
5
|
+
* fiddly parts — grouped records, IPv6 brackets, wildcard binds — are covered
|
|
6
|
+
* by tests instead of by running the tools.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** Hosts that mean "this machine" when a socket reports its bind address. */
|
|
10
|
+
const LOOPBACK_TOKENS = new Set(['127.0.0.1', 'localhost', '[::1]', '::1']);
|
|
11
|
+
/** Wildcard binds are reachable over loopback too. */
|
|
12
|
+
const WILDCARD_TOKENS = new Set(['*', '0.0.0.0', '[::]', '::']);
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Ports that are listening but are never the thing a user wants to preview.
|
|
16
|
+
* Kept deliberately short: guessing too aggressively hides real dev servers.
|
|
17
|
+
*/
|
|
18
|
+
const IGNORED_PORTS = new Set([
|
|
19
|
+
22, // ssh
|
|
20
|
+
53, // dns
|
|
21
|
+
445, // smb
|
|
22
|
+
631, // cups
|
|
23
|
+
5432, // postgres
|
|
24
|
+
3306, // mysql
|
|
25
|
+
6379, // redis
|
|
26
|
+
27017, // mongodb
|
|
27
|
+
9229, // node inspector
|
|
28
|
+
]);
|
|
29
|
+
|
|
30
|
+
const splitHostPort = (value) => {
|
|
31
|
+
const raw = String(value || '').trim();
|
|
32
|
+
if (!raw) return null;
|
|
33
|
+
|
|
34
|
+
// IPv6 arrives bracketed: [::1]:5173
|
|
35
|
+
if (raw.startsWith('[')) {
|
|
36
|
+
const close = raw.indexOf(']');
|
|
37
|
+
if (close === -1) return null;
|
|
38
|
+
const host = raw.slice(0, close + 1);
|
|
39
|
+
const rest = raw.slice(close + 1);
|
|
40
|
+
if (!rest.startsWith(':')) return null;
|
|
41
|
+
return { host, port: rest.slice(1) };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const separator = raw.lastIndexOf(':');
|
|
45
|
+
if (separator === -1) return null;
|
|
46
|
+
return { host: raw.slice(0, separator), port: raw.slice(separator + 1) };
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const toPort = (value) => {
|
|
50
|
+
const port = Number.parseInt(String(value || '').trim(), 10);
|
|
51
|
+
return Number.isInteger(port) && port > 0 && port <= 65535 ? port : null;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* True when a bind address can be reached from this machine over loopback.
|
|
56
|
+
* A socket bound to a specific LAN address only is intentionally excluded:
|
|
57
|
+
* `http://localhost:<port>` would not reach it.
|
|
58
|
+
*/
|
|
59
|
+
export const isLocallyReachableHost = (host) => {
|
|
60
|
+
const value = String(host || '').trim().toLowerCase();
|
|
61
|
+
return LOOPBACK_TOKENS.has(value) || WILDCARD_TOKENS.has(value);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const isIgnoredDevPort = (port) => IGNORED_PORTS.has(port);
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Parses `lsof -iTCP -sTCP:LISTEN -P -n -F pcn`.
|
|
68
|
+
*
|
|
69
|
+
* The `-F` format emits one field per line, prefixed by a letter, and is
|
|
70
|
+
* stateful: `p`/`c` lines open a process record and every following `n` line
|
|
71
|
+
* belongs to it until the next `p`. A single process commonly reports the same
|
|
72
|
+
* port twice (IPv4 and IPv6), so results are de-duplicated by port.
|
|
73
|
+
*/
|
|
74
|
+
export const parseLsofListeners = (output) => {
|
|
75
|
+
const byPort = new Map();
|
|
76
|
+
let pid = null;
|
|
77
|
+
let command = '';
|
|
78
|
+
|
|
79
|
+
for (const line of String(output || '').split('\n')) {
|
|
80
|
+
if (!line) continue;
|
|
81
|
+
const tag = line[0];
|
|
82
|
+
const value = line.slice(1);
|
|
83
|
+
|
|
84
|
+
if (tag === 'p') {
|
|
85
|
+
const parsedPid = Number.parseInt(value, 10);
|
|
86
|
+
pid = Number.isInteger(parsedPid) ? parsedPid : null;
|
|
87
|
+
command = '';
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (tag === 'c') {
|
|
91
|
+
command = value.trim();
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
if (tag !== 'n') continue;
|
|
95
|
+
|
|
96
|
+
// `n` values look like `*:5173`, `127.0.0.1:5173`, or `[::1]:5173`.
|
|
97
|
+
// Established sockets contain `->`; LISTEN filtering should exclude them,
|
|
98
|
+
// but the guard keeps a mixed invocation honest.
|
|
99
|
+
if (value.includes('->')) continue;
|
|
100
|
+
|
|
101
|
+
const parsed = splitHostPort(value);
|
|
102
|
+
if (!parsed) continue;
|
|
103
|
+
const port = toPort(parsed.port);
|
|
104
|
+
if (port === null) continue;
|
|
105
|
+
if (!isLocallyReachableHost(parsed.host)) continue;
|
|
106
|
+
|
|
107
|
+
const existing = byPort.get(port);
|
|
108
|
+
if (existing && existing.pid !== null) continue;
|
|
109
|
+
byPort.set(port, { port, pid, command });
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return [...byPort.values()].sort((left, right) => left.port - right.port);
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Parses `netstat -ano -p TCP` on Windows, where no per-process command name is
|
|
117
|
+
* available without a second call; `command` stays empty and callers fall back
|
|
118
|
+
* to the port alone.
|
|
119
|
+
*/
|
|
120
|
+
export const parseNetstatListeners = (output) => {
|
|
121
|
+
const byPort = new Map();
|
|
122
|
+
|
|
123
|
+
for (const line of String(output || '').split('\n')) {
|
|
124
|
+
const parts = line.trim().split(/\s+/);
|
|
125
|
+
if (parts.length < 4) continue;
|
|
126
|
+
if (!/^tcp$/i.test(parts[0])) continue;
|
|
127
|
+
if (!/^LISTENING$/i.test(parts[3])) continue;
|
|
128
|
+
|
|
129
|
+
const parsed = splitHostPort(parts[1]);
|
|
130
|
+
if (!parsed) continue;
|
|
131
|
+
const port = toPort(parsed.port);
|
|
132
|
+
if (port === null) continue;
|
|
133
|
+
if (!isLocallyReachableHost(parsed.host)) continue;
|
|
134
|
+
|
|
135
|
+
const pid = Number.parseInt(parts[4] ?? '', 10);
|
|
136
|
+
if (byPort.has(port)) continue;
|
|
137
|
+
byPort.set(port, { port, pid: Number.isInteger(pid) ? pid : null, command: '' });
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return [...byPort.values()].sort((left, right) => left.port - right.port);
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Narrows raw listeners to the ones worth offering as a preview target.
|
|
145
|
+
*
|
|
146
|
+
* `ownPorts` removes OpenChamber's own listeners — offering the user a preview
|
|
147
|
+
* of the app they are already looking at is pure noise.
|
|
148
|
+
*/
|
|
149
|
+
export const selectDevServerCandidates = (listeners, { ownPorts = [], ownPids = [] } = {}) => {
|
|
150
|
+
const excludedPorts = new Set(ownPorts.filter((port) => Number.isInteger(port)));
|
|
151
|
+
const excludedPids = new Set(ownPids.filter((pid) => Number.isInteger(pid)));
|
|
152
|
+
|
|
153
|
+
return listeners.filter((entry) => {
|
|
154
|
+
if (excludedPorts.has(entry.port)) return false;
|
|
155
|
+
if (entry.pid !== null && excludedPids.has(entry.pid)) return false;
|
|
156
|
+
if (isIgnoredDevPort(entry.port)) return false;
|
|
157
|
+
return true;
|
|
158
|
+
});
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
/** Linux reports LISTEN as state 0A in /proc/net/tcp. */
|
|
162
|
+
const PROC_STATE_LISTEN = '0A';
|
|
163
|
+
/** Wildcard binds, as /proc writes them: IPv4 0.0.0.0 and IPv6 :: */
|
|
164
|
+
const PROC_WILDCARD_ADDRESSES = new Set(['00000000', '00000000000000000000000000000000']);
|
|
165
|
+
/** Loopback: 127.0.0.1 (little-endian per word) and ::1 */
|
|
166
|
+
const PROC_LOOPBACK_ADDRESSES = new Set(['0100007F', '00000000000000000000000001000000']);
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Parses `/proc/net/tcp` and `/proc/net/tcp6`.
|
|
170
|
+
*
|
|
171
|
+
* The fallback for hosts without `lsof`, which is most containers — and a
|
|
172
|
+
* deployed OpenChamber is exactly where a dev server needs discovering. Reads a
|
|
173
|
+
* kernel file rather than shelling out, so it cannot be defeated by a missing
|
|
174
|
+
* binary or a stripped PATH.
|
|
175
|
+
*
|
|
176
|
+
* No process name or pid: mapping a socket to its owner means walking every
|
|
177
|
+
* /proc/<pid>/fd, which is far more work than the label is worth.
|
|
178
|
+
*/
|
|
179
|
+
export const parseProcNetTcpListeners = (output) => {
|
|
180
|
+
const byPort = new Map();
|
|
181
|
+
|
|
182
|
+
for (const line of String(output || '').split('\n')) {
|
|
183
|
+
const parts = line.trim().split(/\s+/);
|
|
184
|
+
// sl, local_address, rem_address, st, ...
|
|
185
|
+
if (parts.length < 4) continue;
|
|
186
|
+
if (parts[3] !== PROC_STATE_LISTEN) continue;
|
|
187
|
+
|
|
188
|
+
const [address, portHex] = String(parts[1] || '').split(':');
|
|
189
|
+
if (!address || !portHex) continue;
|
|
190
|
+
|
|
191
|
+
const normalizedAddress = address.toUpperCase();
|
|
192
|
+
if (!PROC_WILDCARD_ADDRESSES.has(normalizedAddress) && !PROC_LOOPBACK_ADDRESSES.has(normalizedAddress)) {
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const port = Number.parseInt(portHex, 16);
|
|
197
|
+
if (!Number.isInteger(port) || port <= 0 || port > 65535) continue;
|
|
198
|
+
if (byPort.has(port)) continue;
|
|
199
|
+
byPort.set(port, { port, pid: null, command: '' });
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return [...byPort.values()].sort((left, right) => left.port - right.port);
|
|
203
|
+
};
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
isLocallyReachableHost,
|
|
5
|
+
parseLsofListeners,
|
|
6
|
+
parseProcNetTcpListeners,
|
|
7
|
+
parseNetstatListeners,
|
|
8
|
+
selectDevServerCandidates,
|
|
9
|
+
} from './parse.js';
|
|
10
|
+
|
|
11
|
+
describe('lsof listener parsing', () => {
|
|
12
|
+
test('associates every socket with the process record above it', () => {
|
|
13
|
+
const output = [
|
|
14
|
+
'p1234', 'cnode', 'n*:5173',
|
|
15
|
+
'p5678', 'cpython3', 'n127.0.0.1:8000',
|
|
16
|
+
].join('\n');
|
|
17
|
+
|
|
18
|
+
expect(parseLsofListeners(output)).toEqual([
|
|
19
|
+
{ port: 5173, pid: 1234, command: 'node' },
|
|
20
|
+
{ port: 8000, pid: 5678, command: 'python3' },
|
|
21
|
+
]);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test('keeps one entry when a process binds the same port on IPv4 and IPv6', () => {
|
|
25
|
+
const output = ['p1234', 'cnode', 'n*:5173', 'n[::1]:5173'].join('\n');
|
|
26
|
+
expect(parseLsofListeners(output)).toEqual([{ port: 5173, pid: 1234, command: 'node' }]);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test('unwraps bracketed IPv6 addresses', () => {
|
|
30
|
+
expect(parseLsofListeners(['p1', 'cnode', 'n[::1]:3000'].join('\n')))
|
|
31
|
+
.toEqual([{ port: 3000, pid: 1, command: 'node' }]);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test('skips sockets bound only to a LAN address, which localhost cannot reach', () => {
|
|
35
|
+
expect(parseLsofListeners(['p1', 'cnode', 'n192.168.1.10:5173'].join('\n'))).toEqual([]);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test('skips established connections that slipped past the LISTEN filter', () => {
|
|
39
|
+
expect(parseLsofListeners(['p1', 'cnode', 'n127.0.0.1:5173->127.0.0.1:60123'].join('\n')))
|
|
40
|
+
.toEqual([]);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test('returns sorted results', () => {
|
|
44
|
+
const output = ['p1', 'cnode', 'n*:9000', 'n*:3000', 'n*:5173'].join('\n');
|
|
45
|
+
expect(parseLsofListeners(output).map((entry) => entry.port)).toEqual([3000, 5173, 9000]);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test('tolerates empty and malformed output rather than throwing', () => {
|
|
49
|
+
expect(parseLsofListeners('')).toEqual([]);
|
|
50
|
+
expect(parseLsofListeners(null)).toEqual([]);
|
|
51
|
+
expect(parseLsofListeners('garbage\nn:\nnnotaport')).toEqual([]);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test('rejects out-of-range ports', () => {
|
|
55
|
+
expect(parseLsofListeners(['p1', 'cnode', 'n*:70000', 'n*:0'].join('\n'))).toEqual([]);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe('netstat listener parsing', () => {
|
|
60
|
+
const output = [
|
|
61
|
+
'Active Connections',
|
|
62
|
+
'',
|
|
63
|
+
' Proto Local Address Foreign Address State PID',
|
|
64
|
+
' TCP 0.0.0.0:5173 0.0.0.0:0 LISTENING 4242',
|
|
65
|
+
' TCP 127.0.0.1:8000 0.0.0.0:0 LISTENING 9001',
|
|
66
|
+
' TCP 192.168.0.5:9999 0.0.0.0:0 LISTENING 9002',
|
|
67
|
+
' TCP 127.0.0.1:5173 127.0.0.1:60123 ESTABLISHED 9003',
|
|
68
|
+
].join('\n');
|
|
69
|
+
|
|
70
|
+
test('takes listening loopback and wildcard sockets with their pid', () => {
|
|
71
|
+
expect(parseNetstatListeners(output)).toEqual([
|
|
72
|
+
{ port: 5173, pid: 4242, command: '' },
|
|
73
|
+
{ port: 8000, pid: 9001, command: '' },
|
|
74
|
+
]);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test('ignores established connections and LAN-only binds', () => {
|
|
78
|
+
const ports = parseNetstatListeners(output).map((entry) => entry.port);
|
|
79
|
+
expect(ports).not.toContain(9999);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test('tolerates empty output', () => {
|
|
83
|
+
expect(parseNetstatListeners('')).toEqual([]);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
describe('host reachability', () => {
|
|
88
|
+
test('accepts loopback and wildcard binds', () => {
|
|
89
|
+
for (const host of ['127.0.0.1', 'localhost', '[::1]', '*', '0.0.0.0', '[::]']) {
|
|
90
|
+
expect(isLocallyReachableHost(host)).toBe(true);
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test('rejects a specific LAN address', () => {
|
|
95
|
+
expect(isLocallyReachableHost('192.168.1.4')).toBe(false);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
describe('candidate selection', () => {
|
|
100
|
+
const listeners = [
|
|
101
|
+
{ port: 5173, pid: 10, command: 'node' },
|
|
102
|
+
{ port: 5432, pid: 11, command: 'postgres' },
|
|
103
|
+
{ port: 4096, pid: 12, command: 'openchamber' },
|
|
104
|
+
{ port: 3000, pid: 13, command: 'node' },
|
|
105
|
+
];
|
|
106
|
+
|
|
107
|
+
test('drops OpenChamber own ports so the app never offers itself', () => {
|
|
108
|
+
const ports = selectDevServerCandidates(listeners, { ownPorts: [4096] }).map((entry) => entry.port);
|
|
109
|
+
expect(ports).toEqual([5173, 3000]);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test('drops sockets owned by our own process', () => {
|
|
113
|
+
const ports = selectDevServerCandidates(listeners, { ownPids: [13] }).map((entry) => entry.port);
|
|
114
|
+
expect(ports).toEqual([5173, 4096]);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
test('drops well-known infrastructure ports that are never previewable', () => {
|
|
118
|
+
const ports = selectDevServerCandidates(listeners).map((entry) => entry.port);
|
|
119
|
+
expect(ports).not.toContain(5432);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
test('keeps everything else, including unusual ports', () => {
|
|
123
|
+
const ports = selectDevServerCandidates([{ port: 12345, pid: 1, command: 'bun' }]).map((entry) => entry.port);
|
|
124
|
+
expect(ports).toEqual([12345]);
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
describe('proc net tcp parsing', () => {
|
|
129
|
+
const header = ' sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode';
|
|
130
|
+
|
|
131
|
+
test('takes listening sockets on loopback and wildcard binds', () => {
|
|
132
|
+
const output = [
|
|
133
|
+
header,
|
|
134
|
+
' 0: 00000000:1F90 00000000:0000 0A 00000000:00000000 00:00000000 00000000 1000 0 12345 1 0000 100 0',
|
|
135
|
+
' 1: 0100007F:0BB8 00000000:0000 0A 00000000:00000000 00:00000000 00000000 1000 0 12346 1 0000 100 0',
|
|
136
|
+
].join('\n');
|
|
137
|
+
|
|
138
|
+
expect(parseProcNetTcpListeners(output)).toEqual([
|
|
139
|
+
{ port: 3000, pid: null, command: '' },
|
|
140
|
+
{ port: 8080, pid: null, command: '' },
|
|
141
|
+
]);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
test('ignores sockets that are not listening', () => {
|
|
145
|
+
const output = [
|
|
146
|
+
header,
|
|
147
|
+
' 0: 0100007F:1F90 0100007F:C350 01 00000000:00000000 00:00000000 00000000 1000 0 12345 1 0000 100 0',
|
|
148
|
+
].join('\n');
|
|
149
|
+
expect(parseProcNetTcpListeners(output)).toEqual([]);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
test('ignores a bind to a specific LAN address', () => {
|
|
153
|
+
const output = [
|
|
154
|
+
header,
|
|
155
|
+
' 0: 0A00020F:1F90 00000000:0000 0A 00000000:00000000 00:00000000 00000000 1000 0 12345 1 0000 100 0',
|
|
156
|
+
].join('\n');
|
|
157
|
+
expect(parseProcNetTcpListeners(output)).toEqual([]);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
test('reads the IPv6 table, including ::1 and ::', () => {
|
|
161
|
+
const output = [
|
|
162
|
+
header,
|
|
163
|
+
' 0: 00000000000000000000000001000000:1F90 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000 1000 0 1 1 0 0 0',
|
|
164
|
+
' 1: 00000000000000000000000000000000:0BB8 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000 1000 0 1 1 0 0 0',
|
|
165
|
+
].join('\n');
|
|
166
|
+
expect(parseProcNetTcpListeners(output).map((entry) => entry.port)).toEqual([3000, 8080]);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
test('tolerates an empty or malformed table', () => {
|
|
170
|
+
expect(parseProcNetTcpListeners('')).toEqual([]);
|
|
171
|
+
expect(parseProcNetTcpListeners(header)).toEqual([]);
|
|
172
|
+
expect(parseProcNetTcpListeners('garbage')).toEqual([]);
|
|
173
|
+
});
|
|
174
|
+
});
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dev-server discovery.
|
|
3
|
+
*
|
|
4
|
+
* Answers "what is listening on this machine that I could preview". The old
|
|
5
|
+
* approach guessed from `package.json` scripts, which told us what *could* be
|
|
6
|
+
* started, never what was actually running — so it was wrong exactly when the
|
|
7
|
+
* user needed it. Enumerating listening sockets reports the truth.
|
|
8
|
+
*
|
|
9
|
+
* Discovery is advisory. A failed scan reports failure; it never reports an
|
|
10
|
+
* empty list, because a caller cannot tell "nothing is running" from "the scan
|
|
11
|
+
* broke" and would render the wrong empty state.
|
|
12
|
+
*/
|
|
13
|
+
import fsPromises from 'node:fs/promises';
|
|
14
|
+
|
|
15
|
+
import {
|
|
16
|
+
parseLsofListeners,
|
|
17
|
+
parseNetstatListeners,
|
|
18
|
+
parseProcNetTcpListeners,
|
|
19
|
+
selectDevServerCandidates,
|
|
20
|
+
} from './parse.js';
|
|
21
|
+
|
|
22
|
+
const SCAN_TIMEOUT_MS = 2_500;
|
|
23
|
+
/** Enumeration is cheap but not free; a short cache absorbs panel re-renders. */
|
|
24
|
+
const CACHE_TTL_MS = 3_000;
|
|
25
|
+
|
|
26
|
+
const runCommand = (spawn, command, args, timeoutMs) => new Promise((resolve) => {
|
|
27
|
+
let child;
|
|
28
|
+
try {
|
|
29
|
+
child = spawn(command, args, { windowsHide: true, stdio: ['ignore', 'pipe', 'ignore'] });
|
|
30
|
+
} catch {
|
|
31
|
+
resolve(null);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
let stdout = '';
|
|
36
|
+
let settled = false;
|
|
37
|
+
const finish = (value) => {
|
|
38
|
+
if (settled) return;
|
|
39
|
+
settled = true;
|
|
40
|
+
clearTimeout(timer);
|
|
41
|
+
try { child.kill(); } catch { /* already exited */ }
|
|
42
|
+
resolve(value);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const timer = setTimeout(() => finish(null), timeoutMs);
|
|
46
|
+
child.stdout?.on('data', (chunk) => { stdout += String(chunk); });
|
|
47
|
+
child.on('error', () => finish(null));
|
|
48
|
+
child.on('close', (code) => finish(code === 0 || stdout ? stdout : null));
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Reads the kernel's socket tables. Containers routinely ship without `lsof`,
|
|
53
|
+
* and a deployed OpenChamber is precisely where discovery has to work, so this
|
|
54
|
+
* is tried whenever the command is unavailable.
|
|
55
|
+
*/
|
|
56
|
+
const readProcListeners = async (readFile) => {
|
|
57
|
+
const tables = await Promise.all(['/proc/net/tcp', '/proc/net/tcp6'].map(
|
|
58
|
+
(path) => readFile(path, 'utf8').catch(() => null),
|
|
59
|
+
));
|
|
60
|
+
if (tables.every((table) => table === null)) return null;
|
|
61
|
+
const byPort = new Map();
|
|
62
|
+
for (const table of tables) {
|
|
63
|
+
if (table === null) continue;
|
|
64
|
+
for (const entry of parseProcNetTcpListeners(table)) {
|
|
65
|
+
if (!byPort.has(entry.port)) byPort.set(entry.port, entry);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return [...byPort.values()].sort((left, right) => left.port - right.port);
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export const createDevServerScanner = ({ spawn, platform, readFile = fsPromises.readFile }) => {
|
|
72
|
+
let cache = null;
|
|
73
|
+
|
|
74
|
+
const scan = async () => {
|
|
75
|
+
const isWindows = platform === 'win32';
|
|
76
|
+
if (isWindows) {
|
|
77
|
+
const output = await runCommand(spawn, 'netstat', ['-ano', '-p', 'TCP'], SCAN_TIMEOUT_MS);
|
|
78
|
+
if (output === null) return { ok: false, reason: 'netstat-unavailable' };
|
|
79
|
+
return { ok: true, listeners: parseNetstatListeners(output) };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const output = await runCommand(spawn, 'lsof', ['-iTCP', '-sTCP:LISTEN', '-P', '-n', '-F', 'pcn'], SCAN_TIMEOUT_MS);
|
|
83
|
+
if (output !== null) return { ok: true, listeners: parseLsofListeners(output) };
|
|
84
|
+
|
|
85
|
+
const procListeners = await readProcListeners(readFile);
|
|
86
|
+
if (procListeners !== null) return { ok: true, listeners: procListeners };
|
|
87
|
+
|
|
88
|
+
return { ok: false, reason: 'no-listener-source' };
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
return {
|
|
92
|
+
/**
|
|
93
|
+
* @param {{ ownPorts?: number[] }} options
|
|
94
|
+
* @returns {Promise<{ ok: true, servers: Array<{ port: number, pid: number|null, command: string, url: string }> } | { ok: false, reason: string }>}
|
|
95
|
+
*/
|
|
96
|
+
async discover({ ownPorts = [] } = {}) {
|
|
97
|
+
const now = Date.now();
|
|
98
|
+
if (cache && now - cache.at < CACHE_TTL_MS) return cache.value;
|
|
99
|
+
|
|
100
|
+
const result = await scan();
|
|
101
|
+
if (!result.ok) {
|
|
102
|
+
// Not cached: a transient failure should not suppress the next attempt.
|
|
103
|
+
return result;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const servers = selectDevServerCandidates(result.listeners, {
|
|
107
|
+
ownPorts,
|
|
108
|
+
ownPids: [process.pid],
|
|
109
|
+
}).map((entry) => ({
|
|
110
|
+
...entry,
|
|
111
|
+
url: `http://localhost:${entry.port}/`,
|
|
112
|
+
}));
|
|
113
|
+
|
|
114
|
+
const value = { ok: true, servers };
|
|
115
|
+
cache = { at: now, value };
|
|
116
|
+
return value;
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
export function registerDevServerRoutes(app, { scanner, getOwnPorts }) {
|
|
122
|
+
app.get('/api/dev-servers', async (req, res) => {
|
|
123
|
+
try {
|
|
124
|
+
const ownPorts = typeof getOwnPorts === 'function' ? getOwnPorts() : [];
|
|
125
|
+
const result = await scanner.discover({ ownPorts: Array.isArray(ownPorts) ? ownPorts : [] });
|
|
126
|
+
if (!result.ok) {
|
|
127
|
+
res.status(503).json({ error: 'Port discovery is unavailable', reason: result.reason });
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
res.json({ servers: result.servers });
|
|
131
|
+
} catch (error) {
|
|
132
|
+
res.status(500).json({ error: error?.message || 'Port discovery failed' });
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# Dev Server Tunnel
|
|
2
|
+
|
|
3
|
+
## Purpose
|
|
4
|
+
|
|
5
|
+
This module carries raw TCP bytes between a desktop client and a dev server
|
|
6
|
+
running on the OpenChamber host, so a remote dev server can be opened in the
|
|
7
|
+
browser panel without anything being rewritten.
|
|
8
|
+
|
|
9
|
+
The page is served from a real origin at the root of its own host. That is the
|
|
10
|
+
whole design: absolute URLs resolve, cookies scope correctly, HMR sockets
|
|
11
|
+
connect, and developer tools behave as they do locally. No HTML, header, or
|
|
12
|
+
URL is inspected or modified, which is what the previous rewriting proxy did
|
|
13
|
+
and what made it fragile per framework.
|
|
14
|
+
|
|
15
|
+
## Boundaries
|
|
16
|
+
|
|
17
|
+
- `runtime.js` is the host end: it accepts the WebSocket upgrade at
|
|
18
|
+
`/api/dev-tunnel`, authenticates it, opens a TCP socket to the requested
|
|
19
|
+
local port, and pipes the two together.
|
|
20
|
+
- `client.js` is the local end: it binds a loopback listener on the user's
|
|
21
|
+
machine and pipes each accepted connection through one WebSocket. It lives in
|
|
22
|
+
this package because it needs a WebSocket client the package already depends
|
|
23
|
+
on; the desktop shell drives it over IPC.
|
|
24
|
+
- Port discovery is not owned here. `runtime.js` is given the reachable set by
|
|
25
|
+
the same dev-server discovery the user's own list is built from.
|
|
26
|
+
- The browser panel decides when to tunnel; this module never chooses a target.
|
|
27
|
+
`packages/ui/src/lib/browser/devTunnel.ts` owns that decision, including for
|
|
28
|
+
navigations the page starts itself: a tunnelled page that sends the view to
|
|
29
|
+
another loopback port means a port on the host, not on the user's machine.
|
|
30
|
+
|
|
31
|
+
## Invariants
|
|
32
|
+
|
|
33
|
+
- The reachable set is exactly what dev-server discovery offers the user, never
|
|
34
|
+
"any loopback port". Without that restriction an authenticated client could
|
|
35
|
+
dial arbitrary local services on the host — databases, admin panels, the
|
|
36
|
+
OpenCode API — through this socket.
|
|
37
|
+
- Authentication depends on whether the caller is a browser, and this is
|
|
38
|
+
deliberate rather than a relaxation:
|
|
39
|
+
- With an `Origin` header the request came from a browser context, and the
|
|
40
|
+
usual origin allowlist applies unchanged. That check is a CSRF defence: a
|
|
41
|
+
hostile page can make a browser open a WebSocket carrying ambient cookies,
|
|
42
|
+
and the origin is what exposes it.
|
|
43
|
+
- With no `Origin` the request must carry client-token auth. A browser cannot
|
|
44
|
+
reach this path — the WebSocket API always sends an origin and never lets a
|
|
45
|
+
page set an `Authorization` header — so this case is the desktop shell.
|
|
46
|
+
- Concurrency is capped per host, not per page, because one page load opens
|
|
47
|
+
many sockets.
|
|
48
|
+
- A connection that cannot be established fails the socket rather than holding
|
|
49
|
+
it open; a stalled connect is bounded by an explicit timeout, and so is the
|
|
50
|
+
WebSocket handshake. While it is pending the local socket is paused and its
|
|
51
|
+
buffered bytes are capped, so a local process writing into a stalled
|
|
52
|
+
handshake cannot grow the desktop app's memory.
|
|
53
|
+
- A tunnel that cannot be opened is reported to the panel, never replaced by the
|
|
54
|
+
plain loopback URL. On a remote instance that substitution would change which
|
|
55
|
+
machine answers and show local content under a remote address.
|
|
56
|
+
- Closing either end closes the other. A half-open pipe would leave the page
|
|
57
|
+
waiting on bytes that will never arrive.
|