@openchamber/web 1.13.3 → 1.13.5
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/cli-output.js +2 -7
- package/bin/cli-output.test.js +36 -0
- package/bin/cli.js +132 -5548
- package/bin/cli.test.js +686 -0
- package/bin/lib/DOCUMENTATION.md +121 -0
- package/bin/lib/cli-args.js +726 -0
- package/bin/lib/cli-errors.js +18 -0
- package/bin/lib/cli-executables.js +54 -0
- package/bin/lib/cli-http.js +216 -0
- package/bin/lib/cli-lifecycle.js +421 -0
- package/bin/lib/cli-log-files.js +93 -0
- package/bin/lib/cli-network.js +154 -0
- package/bin/lib/cli-paths.js +113 -0
- package/bin/lib/cli-ports.js +51 -0
- package/bin/lib/cli-process.js +292 -0
- package/bin/lib/cli-startup.js +370 -0
- package/bin/lib/cli-tunnel-capabilities.js +9 -0
- package/bin/lib/cli-tunnel-profiles.js +366 -0
- package/bin/lib/cli-tunnel-utils.js +300 -0
- package/bin/lib/commands-connect-url.js +173 -0
- package/bin/lib/commands-lifecycle.js +396 -0
- package/bin/lib/commands-logs.js +110 -0
- package/bin/lib/commands-serve.js +396 -0
- package/bin/lib/commands-startup.js +64 -0
- package/bin/lib/commands-status.js +114 -0
- package/bin/lib/commands-tunnel.js +1761 -0
- package/bin/lib/commands-update.js +141 -0
- package/dist/assets/{DiagramView-BmHROYsX.js → DiagramView-Bbj-QI7h.js} +1 -1
- package/dist/assets/{MarkdownRendererImpl-DiJE-v5Q.js → MarkdownRendererImpl-BNr_9Jsb.js} +3 -3
- package/dist/assets/MultiRunWindow-DMMu53kw.js +1 -0
- package/dist/assets/NewWorktreeDialog-5TYBoMMA.js +167 -0
- package/dist/assets/{OnboardingScreen-DpBF8dKJ.js → OnboardingScreen-BucKVBD7.js} +1 -1
- package/dist/assets/{SessionSwitcherDropdown-1xaHUxPK.js → SessionSwitcherDropdown-B6kvduse.js} +1 -1
- package/dist/assets/{SettingsWindow-ChwLyQAe.js → SettingsWindow-BEfDro9b.js} +1 -1
- package/dist/assets/{TerminalView-DGEBNuAp.js → TerminalView-CMdqMpTy.js} +1 -1
- package/dist/assets/es-Bk-yR0w-.js +15 -0
- package/dist/assets/fr-f1ibV7QU.js +13 -0
- package/dist/assets/index-B82A6jBD.css +1 -0
- package/dist/assets/index-BsXkdBYx.js +3 -0
- package/dist/assets/ja-D9mS3zWP.js +15 -0
- package/dist/assets/ko-Cpl1IA3D.js +15 -0
- package/dist/assets/main-DOdUrt3s.js +214 -0
- package/dist/assets/{main-fm_8J7Lk.js → main-KEVf2Yak.js} +2 -2
- package/dist/assets/miniChat-Nlzqr3Df.js +2 -0
- package/dist/assets/mobile-BIhOFhia.js +2 -0
- package/dist/assets/pl-CYke_fxL.js +15 -0
- package/dist/assets/pt-BR-Clb6Z8up.js +15 -0
- package/dist/assets/renderElectronMiniChatApp-Cb1iUq8M.js +2 -0
- package/dist/assets/{renderMobileApp-6s0G1iIJ.js → renderMobileApp-7chEBHPK.js} +5 -5
- package/dist/assets/uk-CXn8cxXa.js +15 -0
- package/dist/assets/{useAppFontEffects-CV5C5O0r.js → useAppFontEffects-Bd0ks39c.js} +124 -119
- package/dist/assets/{vendor-.bun-CC_Xso_s.js → vendor-.bun-By2L9XvG.js} +1 -1
- package/dist/assets/zh-CN-BRwwlEdG.js +15 -0
- package/dist/assets/zh-TW-D5pg5Z8l.js +15 -0
- package/dist/index.html +4 -4
- package/dist/mini-chat.html +4 -4
- package/dist/mobile.html +4 -4
- package/package.json +1 -1
- package/server/lib/cloudflare-tunnel.js +2 -2
- package/server/lib/event-stream/global-hub.js +1 -1
- package/server/lib/event-stream/index.js +0 -12
- package/server/lib/event-stream/runtime.js +1 -1
- package/server/lib/git/routes.test.js +4 -4
- package/server/lib/git/service.js +18 -0
- package/server/lib/git/service.test.js +48 -0
- package/server/lib/github/routes.js +1 -0
- package/server/lib/notifications/index.js +1 -4
- package/server/lib/opencode/agents.js +0 -6
- package/server/lib/opencode/commands.js +0 -5
- package/server/lib/opencode/config-entity-routes.js +39 -21
- package/server/lib/opencode/feature-routes-runtime.js +27 -62
- package/server/lib/opencode/lifecycle.js +13 -1
- package/server/lib/opencode/npm-registry.js +3 -3
- package/server/lib/opencode/proxy.js +3 -3
- package/server/lib/opencode/shared.js +0 -6
- package/server/lib/opencode/skills.js +0 -2
- package/server/lib/opencode/snippets.js +0 -2
- package/server/lib/package-manager.js +4 -4
- package/server/lib/package-manager.test.js +8 -1
- package/server/lib/projects/project-config.js +0 -8
- package/server/lib/quota/DOCUMENTATION.md +3 -1
- package/server/lib/quota/providers/claude.js +1 -1
- package/server/lib/quota/providers/codex.js +1 -1
- package/server/lib/quota/providers/copilot.js +1 -1
- package/server/lib/quota/providers/cursor.js +1 -1
- package/server/lib/quota/providers/google/auth.js +2 -2
- package/server/lib/quota/providers/google/index.js +14 -33
- package/server/lib/quota/providers/google/transforms.js +1 -1
- package/server/lib/quota/providers/index.js +4 -4
- package/server/lib/quota/providers/index.test.js +17 -0
- package/server/lib/quota/providers/kimi.js +1 -1
- package/server/lib/quota/providers/minimax-cn-coding-plan.js +1 -1
- package/server/lib/quota/providers/minimax-coding-plan.js +1 -1
- package/server/lib/quota/providers/nanogpt.js +1 -1
- package/server/lib/quota/providers/ollama-cloud.js +1 -1
- package/server/lib/quota/providers/openai.js +4 -4
- package/server/lib/quota/providers/openrouter.js +1 -1
- package/server/lib/quota/providers/wafer.js +1 -1
- package/server/lib/quota/providers/zai.js +1 -1
- package/server/lib/quota/providers/zhipuai-coding-plan.js +1 -1
- package/server/lib/skills-catalog/cache.js +0 -4
- package/server/lib/skills-catalog/clawdhub/api.js +0 -32
- package/server/lib/skills-catalog/clawdhub/scan.js +0 -52
- package/server/lib/skills-catalog/curated-sources.js +1 -1
- package/server/lib/skills-catalog/source.js +5 -0
- package/server/lib/terminal/runtime.js +11 -9
- package/server/lib/text/summarization.js +1 -1
- package/server/lib/tunnels/types.js +2 -2
- package/dist/assets/MultiRunWindow-yZoCT7F7.js +0 -1
- package/dist/assets/NewWorktreeDialog-CTMiE7Xd.js +0 -167
- package/dist/assets/es-CTE-E8we.js +0 -15
- package/dist/assets/fr-TVkZpyEo.js +0 -13
- package/dist/assets/index-B4pCDMbq.css +0 -1
- package/dist/assets/index-D9uMvTQF.js +0 -3
- package/dist/assets/ko-D_UCJIIL.js +0 -15
- package/dist/assets/main-eAd3a6tx.js +0 -214
- package/dist/assets/miniChat-Bbjg31IC.js +0 -2
- package/dist/assets/mobile-B3_j_DsB.js +0 -2
- package/dist/assets/pl-nDEkk-GZ.js +0 -15
- package/dist/assets/pt-BR-B_g2RcgU.js +0 -15
- package/dist/assets/renderElectronMiniChatApp-CB13zs6N.js +0 -2
- package/dist/assets/uk-CWLhnPGW.js +0 -15
- package/dist/assets/zh-CN-BFxe1b5m.js +0 -15
- package/dist/assets/zh-TW-n-d-xbGV.js +0 -15
- package/server/lib/opencode/index.js +0 -95
- package/server/lib/quota/providers/interface.js +0 -55
- package/server/lib/skills-catalog/clawdhub/index.js +0 -30
- package/server/lib/skills-catalog/index.js +0 -42
- package/server/lib/terminal/index.js +0 -31
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
|
|
3
|
+
const DEFAULT_TAIL_LINES = 200;
|
|
4
|
+
const LOG_ROTATE_MAX_BYTES = 10 * 1024 * 1024;
|
|
5
|
+
const LOG_ROTATE_KEEP = 5;
|
|
6
|
+
|
|
7
|
+
function rotateLogFile(logPath) {
|
|
8
|
+
try {
|
|
9
|
+
const stats = fs.statSync(logPath);
|
|
10
|
+
if (stats.size < LOG_ROTATE_MAX_BYTES) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
} catch {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
for (let i = LOG_ROTATE_KEEP - 1; i >= 1; i--) {
|
|
18
|
+
const src = `${logPath}.${i}`;
|
|
19
|
+
const dst = `${logPath}.${i + 1}`;
|
|
20
|
+
if (fs.existsSync(src)) {
|
|
21
|
+
try {
|
|
22
|
+
fs.renameSync(src, dst);
|
|
23
|
+
} catch {
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
if (fs.existsSync(logPath)) {
|
|
30
|
+
fs.renameSync(logPath, `${logPath}.1`);
|
|
31
|
+
}
|
|
32
|
+
} catch {
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
function readTailLines(filePath, lineCount = DEFAULT_TAIL_LINES) {
|
|
38
|
+
if (!fs.existsSync(filePath)) {
|
|
39
|
+
return [];
|
|
40
|
+
}
|
|
41
|
+
const raw = fs.readFileSync(filePath, 'utf8');
|
|
42
|
+
const lines = raw.split(/\r?\n/);
|
|
43
|
+
if (lines.length && lines[lines.length - 1] === '') {
|
|
44
|
+
lines.pop();
|
|
45
|
+
}
|
|
46
|
+
return lines.slice(Math.max(0, lines.length - lineCount));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function followFile(filePath, onLine) {
|
|
50
|
+
let position = 0;
|
|
51
|
+
try {
|
|
52
|
+
position = fs.statSync(filePath).size;
|
|
53
|
+
} catch {
|
|
54
|
+
position = 0;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
let remainder = '';
|
|
58
|
+
const interval = setInterval(() => {
|
|
59
|
+
try {
|
|
60
|
+
const stats = fs.statSync(filePath);
|
|
61
|
+
if (stats.size < position) {
|
|
62
|
+
position = 0;
|
|
63
|
+
}
|
|
64
|
+
if (stats.size === position) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const fd = fs.openSync(filePath, 'r');
|
|
69
|
+
try {
|
|
70
|
+
const length = stats.size - position;
|
|
71
|
+
const buffer = Buffer.alloc(length);
|
|
72
|
+
fs.readSync(fd, buffer, 0, length, position);
|
|
73
|
+
position = stats.size;
|
|
74
|
+
const chunk = remainder + buffer.toString('utf8');
|
|
75
|
+
const parts = chunk.split(/\r?\n/);
|
|
76
|
+
remainder = parts.pop() || '';
|
|
77
|
+
for (const line of parts) {
|
|
78
|
+
onLine(line);
|
|
79
|
+
}
|
|
80
|
+
} finally {
|
|
81
|
+
fs.closeSync(fd);
|
|
82
|
+
}
|
|
83
|
+
} catch {
|
|
84
|
+
}
|
|
85
|
+
}, 400);
|
|
86
|
+
|
|
87
|
+
return () => {
|
|
88
|
+
clearInterval(interval);
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
export { rotateLogFile, readTailLines, followFile };
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import dgram from 'dgram';
|
|
2
|
+
import os from 'os';
|
|
3
|
+
import { EXIT_CODE, TunnelCliError } from './cli-errors.js';
|
|
4
|
+
import {
|
|
5
|
+
getUnauthenticatedLanErrorMessage,
|
|
6
|
+
isNetworkExposedBindHost,
|
|
7
|
+
isUnsafeUnauthenticatedLanAllowed,
|
|
8
|
+
} from '../../server/lib/security/bind-host.js';
|
|
9
|
+
|
|
10
|
+
// Browser-unsafe ports (Fetch/Chromium restricted ports).
|
|
11
|
+
const UNSAFE_BROWSER_PORTS = new Set([
|
|
12
|
+
0, 1, 7, 9, 11, 13, 15, 17, 19, 20, 21, 22, 23, 25, 37, 42, 43, 53, 69,
|
|
13
|
+
77, 79, 87, 95, 101, 102, 103, 104, 109, 110, 111, 113, 115, 117, 119,
|
|
14
|
+
123, 135, 137, 139, 143, 161, 179, 389, 427, 465, 512, 513, 514, 515,
|
|
15
|
+
526, 530, 531, 532, 540, 548, 554, 556, 563, 587, 601, 636, 989, 990,
|
|
16
|
+
993, 995, 1719, 1720, 1723, 2049, 3659, 4045, 5060, 5061, 6000, 6566,
|
|
17
|
+
6665, 6666, 6667, 6668, 6669, 6697, 10080,
|
|
18
|
+
]);
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
function isUnsafeBrowserPort(port) {
|
|
22
|
+
return Number.isFinite(port) && UNSAFE_BROWSER_PORTS.has(Math.trunc(port));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function resolveConfiguredBindHost(hostOverride) {
|
|
26
|
+
const configured = typeof hostOverride === 'string' && hostOverride.trim()
|
|
27
|
+
? hostOverride.trim()
|
|
28
|
+
: typeof process.env.OPENCHAMBER_HOST === 'string'
|
|
29
|
+
? process.env.OPENCHAMBER_HOST.trim()
|
|
30
|
+
: '';
|
|
31
|
+
return configured || '127.0.0.1';
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function resolveServeHost(hostOverride) {
|
|
35
|
+
return resolveConfiguredBindHost(hostOverride);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function resolveApiHost(hostOverride) {
|
|
39
|
+
const configured = resolveConfiguredBindHost(hostOverride);
|
|
40
|
+
|
|
41
|
+
if (!configured) {
|
|
42
|
+
return '127.0.0.1';
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Wildcard bind hosts are not valid destination hosts.
|
|
46
|
+
if (configured === '0.0.0.0') {
|
|
47
|
+
return '127.0.0.1';
|
|
48
|
+
}
|
|
49
|
+
if (configured === '::' || configured === '[::]') {
|
|
50
|
+
return '::1';
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Strip brackets if user provided [::1]
|
|
54
|
+
if (configured.startsWith('[') && configured.endsWith(']')) {
|
|
55
|
+
return configured.slice(1, -1);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return configured;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function formatHostForUrl(host) {
|
|
62
|
+
if (typeof host !== 'string') return '127.0.0.1';
|
|
63
|
+
// Bracket IPv6 for URL usage.
|
|
64
|
+
return host.includes(':') ? `[${host}]` : host;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function buildLocalUrl(port, endpoint = '', hostOverride) {
|
|
68
|
+
const host = formatHostForUrl(resolveApiHost(hostOverride));
|
|
69
|
+
const pathPart = endpoint.startsWith('/') ? endpoint : `/${endpoint}`;
|
|
70
|
+
return `http://${host}:${port}${pathPart}`;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async function detectLanIPv4Address() {
|
|
74
|
+
const ip = await new Promise((resolve) => {
|
|
75
|
+
const socket = dgram.createSocket('udp4');
|
|
76
|
+
const finish = (value) => {
|
|
77
|
+
try { socket.close(); } catch {}
|
|
78
|
+
resolve(value);
|
|
79
|
+
};
|
|
80
|
+
socket.once('error', () => finish(null));
|
|
81
|
+
try {
|
|
82
|
+
socket.connect(80, '8.8.8.8', (error) => {
|
|
83
|
+
if (error) return finish(null);
|
|
84
|
+
try {
|
|
85
|
+
const addr = socket.address();
|
|
86
|
+
finish(addr && typeof addr.address === 'string' ? addr.address : null);
|
|
87
|
+
} catch {
|
|
88
|
+
finish(null);
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
} catch {
|
|
92
|
+
finish(null);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
if (ip && ip !== '0.0.0.0' && !ip.startsWith('127.')) return ip;
|
|
97
|
+
|
|
98
|
+
for (const entries of Object.values(os.networkInterfaces() || {})) {
|
|
99
|
+
for (const entry of entries || []) {
|
|
100
|
+
if (entry.family === 'IPv4' && !entry.internal && entry.address) {
|
|
101
|
+
return entry.address;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
function formatUnsafePortWarning(port) {
|
|
110
|
+
return `Port ${port} is browser-unsafe (ERR_UNSAFE_PORT) and is not supported for OpenChamber UI at ${buildLocalUrl(port, '/')}.`;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function assertSafeBrowserPort(port, { context = 'This action' } = {}) {
|
|
114
|
+
if (!isUnsafeBrowserPort(port)) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
throw new TunnelCliError(
|
|
118
|
+
`${context} cannot use port ${port}. ${formatUnsafePortWarning(port)} Use a safe port such as 3000, 5173, 8080, or a high ephemeral port.`,
|
|
119
|
+
EXIT_CODE.USAGE_ERROR,
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
function hasUiPasswordConfigured(password) {
|
|
125
|
+
return typeof password === 'string' && password.trim().length > 0;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function assertAuthenticatedNetworkExposure({ host, uiPassword }) {
|
|
129
|
+
const bindHost = resolveConfiguredBindHost(host);
|
|
130
|
+
if (hasUiPasswordConfigured(uiPassword)) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
if (!isNetworkExposedBindHost(bindHost)) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if (isUnsafeUnauthenticatedLanAllowed(process.env)) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
throw new TunnelCliError(getUnauthenticatedLanErrorMessage(bindHost), EXIT_CODE.AUTH_CONFIG_ERROR);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
export {
|
|
144
|
+
resolveConfiguredBindHost,
|
|
145
|
+
resolveServeHost,
|
|
146
|
+
resolveApiHost,
|
|
147
|
+
formatHostForUrl,
|
|
148
|
+
isUnsafeBrowserPort,
|
|
149
|
+
buildLocalUrl,
|
|
150
|
+
detectLanIPv4Address,
|
|
151
|
+
assertSafeBrowserPort,
|
|
152
|
+
hasUiPasswordConfigured,
|
|
153
|
+
assertAuthenticatedNetworkExposure,
|
|
154
|
+
};
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import os from 'os';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
|
|
5
|
+
const TUNNEL_PROFILES_FILE_NAME = 'tunnel-profiles.json';
|
|
6
|
+
const LEGACY_CLOUDFLARE_MANAGED_REMOTE_FILE_NAME = 'cloudflare-managed-remote-tunnels.json';
|
|
7
|
+
const TUNNEL_CLI_STATE_FILE_NAME = 'tunnel-cli-state.json';
|
|
8
|
+
|
|
9
|
+
function getDataDir() {
|
|
10
|
+
if (typeof process.env.OPENCHAMBER_DATA_DIR === 'string' && process.env.OPENCHAMBER_DATA_DIR.trim().length > 0) {
|
|
11
|
+
return path.resolve(process.env.OPENCHAMBER_DATA_DIR.trim());
|
|
12
|
+
}
|
|
13
|
+
return path.join(os.homedir(), '.config', 'openchamber');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function getLogsDir() {
|
|
17
|
+
return path.join(getDataDir(), 'logs');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function getSettingsFilePath() {
|
|
21
|
+
return path.join(getDataDir(), 'settings.json');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function readDesktopLocalPortFromSettings() {
|
|
25
|
+
try {
|
|
26
|
+
const raw = fs.readFileSync(getSettingsFilePath(), 'utf8');
|
|
27
|
+
const parsed = JSON.parse(raw);
|
|
28
|
+
const value = parsed?.desktopLocalPort;
|
|
29
|
+
if (Number.isFinite(value) && value > 0 && value <= 65535) {
|
|
30
|
+
return value;
|
|
31
|
+
}
|
|
32
|
+
return null;
|
|
33
|
+
} catch {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function ensureLogsDir() {
|
|
39
|
+
fs.mkdirSync(getLogsDir(), { recursive: true });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function getLogFilePath(port) {
|
|
43
|
+
return path.join(getLogsDir(), `openchamber-${port}.log`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function getTunnelProfilesFilePath() {
|
|
47
|
+
return path.join(getDataDir(), TUNNEL_PROFILES_FILE_NAME);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function getLegacyCloudflareManagedRemoteFilePath() {
|
|
51
|
+
return path.join(getDataDir(), LEGACY_CLOUDFLARE_MANAGED_REMOTE_FILE_NAME);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function getTunnelCliStateFilePath() {
|
|
55
|
+
return path.join(getDataDir(), TUNNEL_CLI_STATE_FILE_NAME);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function readTunnelCliState() {
|
|
59
|
+
const filePath = getTunnelCliStateFilePath();
|
|
60
|
+
try {
|
|
61
|
+
const raw = fs.readFileSync(filePath, 'utf8');
|
|
62
|
+
const parsed = JSON.parse(raw);
|
|
63
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
64
|
+
return {};
|
|
65
|
+
}
|
|
66
|
+
return parsed;
|
|
67
|
+
} catch {
|
|
68
|
+
return {};
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function readLastManagedLocalConfigPath() {
|
|
73
|
+
const state = readTunnelCliState();
|
|
74
|
+
if (typeof state.lastManagedLocalConfigPath !== 'string') {
|
|
75
|
+
return '';
|
|
76
|
+
}
|
|
77
|
+
return state.lastManagedLocalConfigPath.trim();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function writeLastManagedLocalConfigPath(configPath) {
|
|
81
|
+
if (typeof configPath !== 'string' || configPath.trim().length === 0) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
const filePath = getTunnelCliStateFilePath();
|
|
85
|
+
const current = readTunnelCliState();
|
|
86
|
+
const next = {
|
|
87
|
+
...current,
|
|
88
|
+
lastManagedLocalConfigPath: configPath.trim(),
|
|
89
|
+
updatedAt: Date.now(),
|
|
90
|
+
};
|
|
91
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
92
|
+
fs.writeFileSync(filePath, JSON.stringify(next, null, 2), 'utf8');
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
function getRunDir() {
|
|
97
|
+
const dir = path.join(getDataDir(), 'run');
|
|
98
|
+
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
99
|
+
return dir;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
export {
|
|
104
|
+
getDataDir,
|
|
105
|
+
readDesktopLocalPortFromSettings,
|
|
106
|
+
ensureLogsDir,
|
|
107
|
+
getLogFilePath,
|
|
108
|
+
getTunnelProfilesFilePath,
|
|
109
|
+
getLegacyCloudflareManagedRemoteFilePath,
|
|
110
|
+
readLastManagedLocalConfigPath,
|
|
111
|
+
writeLastManagedLocalConfigPath,
|
|
112
|
+
getRunDir,
|
|
113
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import net from 'net';
|
|
2
|
+
import { DEFAULT_PORT } from './cli-args.js';
|
|
3
|
+
import { fetchSystemInfoFromPort } from './cli-http.js';
|
|
4
|
+
|
|
5
|
+
async function isPortAvailable(port, host) {
|
|
6
|
+
if (!Number.isFinite(port) || port <= 0) {
|
|
7
|
+
return false;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return await new Promise((resolve) => {
|
|
11
|
+
const server = net.createServer();
|
|
12
|
+
server.unref();
|
|
13
|
+
server.on('error', () => resolve(false));
|
|
14
|
+
server.listen({ port, host }, () => {
|
|
15
|
+
server.close(() => resolve(true));
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async function resolveAvailablePort(desiredPort, explicitPort = false, onNotice) {
|
|
21
|
+
const startPort = Number.isFinite(desiredPort) ? Math.trunc(desiredPort) : DEFAULT_PORT;
|
|
22
|
+
if (explicitPort) {
|
|
23
|
+
return startPort;
|
|
24
|
+
}
|
|
25
|
+
if (await isPortAvailable(startPort)) {
|
|
26
|
+
return startPort;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const occupant = await fetchSystemInfoFromPort(startPort);
|
|
30
|
+
let message;
|
|
31
|
+
if (occupant?.runtime === 'desktop') {
|
|
32
|
+
message = `Port ${startPort} is used by OpenChamber Desktop; using a free port`;
|
|
33
|
+
} else if (occupant?.runtime) {
|
|
34
|
+
message = `Port ${startPort} is used by an existing OpenChamber instance; using a free port`;
|
|
35
|
+
} else {
|
|
36
|
+
message = `Port ${startPort} in use; using a free port`;
|
|
37
|
+
}
|
|
38
|
+
if (typeof onNotice === 'function' && message) {
|
|
39
|
+
onNotice({
|
|
40
|
+
level: 'warning',
|
|
41
|
+
code: 'PORT_REASSIGNED',
|
|
42
|
+
message,
|
|
43
|
+
});
|
|
44
|
+
} else if (message) {
|
|
45
|
+
console.warn(message);
|
|
46
|
+
}
|
|
47
|
+
return 0;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
export { isPortAvailable, resolveAvailablePort };
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { spawnSync } from 'child_process';
|
|
4
|
+
import { getRunDir } from './cli-paths.js';
|
|
5
|
+
|
|
6
|
+
async function getPidFilePath(port) {
|
|
7
|
+
return path.join(getRunDir(), `openchamber-${port}.pid`);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
async function getInstanceFilePath(port) {
|
|
11
|
+
return path.join(getRunDir(), `openchamber-${port}.json`);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function readPidFile(pidFilePath) {
|
|
15
|
+
try {
|
|
16
|
+
const content = fs.readFileSync(pidFilePath, 'utf8').trim();
|
|
17
|
+
const pid = parseInt(content, 10);
|
|
18
|
+
return Number.isFinite(pid) ? pid : null;
|
|
19
|
+
} catch {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function writePidFile(pidFilePath, pid, onNotice) {
|
|
25
|
+
try {
|
|
26
|
+
fs.writeFileSync(pidFilePath, String(pid), { mode: 0o600 });
|
|
27
|
+
} catch (error) {
|
|
28
|
+
const message = `Could not write PID file: ${error.message}`;
|
|
29
|
+
if (typeof onNotice === 'function') {
|
|
30
|
+
onNotice({ level: 'warning', code: 'PID_FILE_WRITE_FAILED', message });
|
|
31
|
+
} else {
|
|
32
|
+
console.warn(`Warning: ${message}`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function removePidFile(pidFilePath) {
|
|
38
|
+
try {
|
|
39
|
+
if (fs.existsSync(pidFilePath)) {
|
|
40
|
+
fs.unlinkSync(pidFilePath);
|
|
41
|
+
}
|
|
42
|
+
} catch {
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function readInstanceOptions(instanceFilePath) {
|
|
47
|
+
try {
|
|
48
|
+
return JSON.parse(fs.readFileSync(instanceFilePath, 'utf8'));
|
|
49
|
+
} catch {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function writeInstanceOptions(instanceFilePath, options, onNotice) {
|
|
55
|
+
try {
|
|
56
|
+
const toStore = {
|
|
57
|
+
port: options.port,
|
|
58
|
+
host: typeof options.host === 'string' && options.host.length > 0 ? options.host : undefined,
|
|
59
|
+
launchMode: options.launchMode === 'foreground' ? 'foreground' : 'daemon',
|
|
60
|
+
uiPassword: typeof options.uiPassword === 'string' ? options.uiPassword : undefined,
|
|
61
|
+
hasUiPassword: typeof options.uiPassword === 'string',
|
|
62
|
+
apiOnly: options.apiOnly === true,
|
|
63
|
+
startedAt: Number.isFinite(options.startedAt) ? options.startedAt : Date.now(),
|
|
64
|
+
};
|
|
65
|
+
fs.writeFileSync(instanceFilePath, JSON.stringify(toStore, null, 2), { mode: 0o600 });
|
|
66
|
+
} catch (error) {
|
|
67
|
+
const message = `Could not write instance file: ${error.message}`;
|
|
68
|
+
if (typeof onNotice === 'function') {
|
|
69
|
+
onNotice({ level: 'warning', code: 'INSTANCE_FILE_WRITE_FAILED', message });
|
|
70
|
+
} else {
|
|
71
|
+
console.warn(`Warning: ${message}`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function removeInstanceFile(instanceFilePath) {
|
|
77
|
+
try {
|
|
78
|
+
if (fs.existsSync(instanceFilePath)) {
|
|
79
|
+
fs.unlinkSync(instanceFilePath);
|
|
80
|
+
}
|
|
81
|
+
} catch {
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Liveness only — "is *some* process alive with this PID". Use this when the
|
|
86
|
+
// PID is known to be ours (a child we just spawned, or a process we are
|
|
87
|
+
// stopping). Do NOT use it to validate a PID read from a pid file: after an
|
|
88
|
+
// ungraceful shutdown the pid file is stale and the kernel may have recycled
|
|
89
|
+
// that PID to an unrelated process — see isOpenchamberProcessRunning.
|
|
90
|
+
function isProcessRunning(pid) {
|
|
91
|
+
try {
|
|
92
|
+
process.kill(pid, 0);
|
|
93
|
+
return true;
|
|
94
|
+
} catch {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Best-effort command line for a live PID, used for identity verification.
|
|
100
|
+
// Returns the cmdline string, '' when the process has no readable cmdline, or
|
|
101
|
+
// null when identity can't be determined on this platform (caller falls back to
|
|
102
|
+
// liveness — so behaviour is unchanged where we can't check).
|
|
103
|
+
function readProcessCmdline(pid) {
|
|
104
|
+
try {
|
|
105
|
+
if (process.platform === 'linux') {
|
|
106
|
+
// /proc/<pid>/cmdline is a NUL-delimited argv list.
|
|
107
|
+
return fs.readFileSync(`/proc/${pid}/cmdline`, 'utf8').replace(/\0/g, ' ').trim();
|
|
108
|
+
}
|
|
109
|
+
if (process.platform === 'darwin') {
|
|
110
|
+
const result = spawnSync('ps', ['-p', String(pid), '-o', 'command='], {
|
|
111
|
+
encoding: 'utf8',
|
|
112
|
+
timeout: 3000,
|
|
113
|
+
windowsHide: true,
|
|
114
|
+
});
|
|
115
|
+
const out = (result.stdout || '').trim();
|
|
116
|
+
return out.length > 0 ? out : null;
|
|
117
|
+
}
|
|
118
|
+
} catch {
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
// Windows / other: a process's full command line isn't cheaply available, so
|
|
122
|
+
// we can't verify identity — fall back to liveness-only.
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function isOpenchamberCmdline(cmdline) {
|
|
127
|
+
if (typeof cmdline !== 'string' || cmdline.length === 0) {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
// Every install path contains the "openchamber" segment — the npm package
|
|
131
|
+
// (@openchamber/web) and the source checkout both do, for the foreground
|
|
132
|
+
// (bin/cli.js) and daemon (server/index.js) entrypoints alike. Matching the
|
|
133
|
+
// path segment (not a generic "cli.js") keeps a recycled stranger such as
|
|
134
|
+
// "npm-cli.js" or "agentmemory" from being mistaken for us.
|
|
135
|
+
return cmdline.toLowerCase().includes('openchamber');
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Liveness + identity — "is the OpenChamber instance recorded in a pid file
|
|
139
|
+
// still the process running under this PID". Use this (not isProcessRunning)
|
|
140
|
+
// when validating a PID read from a pid file. After an ungraceful shutdown
|
|
141
|
+
// removePidFile never runs, so the stale PID can be recycled to an unrelated
|
|
142
|
+
// process; a liveness-only check then reports us as "already running" and aborts
|
|
143
|
+
// startup, which loops forever under systemd Restart=always (issue #1721).
|
|
144
|
+
// Where identity can't be determined (Windows, unreadable /proc or ps), we fall
|
|
145
|
+
// back to liveness so there are no false negatives on those platforms.
|
|
146
|
+
function isOpenchamberProcessRunning(pid) {
|
|
147
|
+
const state = getOpenchamberProcessState(pid);
|
|
148
|
+
return state === 'matched' || state === 'unknown';
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function getOpenchamberProcessState(pid, options = {}) {
|
|
152
|
+
const checkProcessRunning = typeof options.isProcessRunning === 'function'
|
|
153
|
+
? options.isProcessRunning
|
|
154
|
+
: isProcessRunning;
|
|
155
|
+
if (!Number.isFinite(pid) || pid <= 0 || !checkProcessRunning(pid)) {
|
|
156
|
+
return 'dead';
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const readCmdline = typeof options.readProcessCmdline === 'function'
|
|
160
|
+
? options.readProcessCmdline
|
|
161
|
+
: readProcessCmdline;
|
|
162
|
+
const cmdline = readCmdline(pid);
|
|
163
|
+
if (cmdline === null) {
|
|
164
|
+
return 'unknown';
|
|
165
|
+
}
|
|
166
|
+
return isOpenchamberCmdline(cmdline) ? 'matched' : 'mismatched';
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function hasOpenchamberRuntimeInfo(info) {
|
|
170
|
+
return Boolean(info && typeof info.runtime === 'string' && info.runtime.length > 0);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function waitForProcessExit(pid, timeoutMs) {
|
|
174
|
+
if (!Number.isFinite(pid) || pid <= 0) {
|
|
175
|
+
return Promise.resolve(true);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const deadline = Date.now() + timeoutMs;
|
|
179
|
+
return new Promise((resolve) => {
|
|
180
|
+
const check = () => {
|
|
181
|
+
if (!isProcessRunning(pid)) {
|
|
182
|
+
resolve(true);
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
if (Date.now() >= deadline) {
|
|
186
|
+
resolve(false);
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
setTimeout(check, 150);
|
|
190
|
+
};
|
|
191
|
+
check();
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
async function terminateProcessTree(pid, options = {}) {
|
|
196
|
+
if (!Number.isFinite(pid) || pid <= 0) {
|
|
197
|
+
return true;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const gracefulTimeoutMs = Number.isFinite(options.gracefulTimeoutMs) && options.gracefulTimeoutMs >= 0
|
|
201
|
+
? Math.trunc(options.gracefulTimeoutMs)
|
|
202
|
+
: 2500;
|
|
203
|
+
const forceTimeoutMs = Number.isFinite(options.forceTimeoutMs) && options.forceTimeoutMs >= 0
|
|
204
|
+
? Math.trunc(options.forceTimeoutMs)
|
|
205
|
+
: 3000;
|
|
206
|
+
|
|
207
|
+
if (process.platform === 'win32') {
|
|
208
|
+
try {
|
|
209
|
+
process.kill(pid);
|
|
210
|
+
} catch {
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (await waitForProcessExit(pid, 800)) {
|
|
214
|
+
return true;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
try {
|
|
218
|
+
spawnSync('taskkill', ['/pid', String(pid), '/t'], {
|
|
219
|
+
stdio: 'ignore',
|
|
220
|
+
timeout: 3000,
|
|
221
|
+
windowsHide: true,
|
|
222
|
+
});
|
|
223
|
+
} catch {
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (await waitForProcessExit(pid, gracefulTimeoutMs)) {
|
|
227
|
+
return true;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
try {
|
|
231
|
+
spawnSync('taskkill', ['/pid', String(pid), '/f', '/t'], {
|
|
232
|
+
stdio: 'ignore',
|
|
233
|
+
timeout: 5000,
|
|
234
|
+
windowsHide: true,
|
|
235
|
+
});
|
|
236
|
+
} catch {
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
return waitForProcessExit(pid, forceTimeoutMs);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
try {
|
|
243
|
+
process.kill(pid, 'SIGTERM');
|
|
244
|
+
} catch {
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (await waitForProcessExit(pid, gracefulTimeoutMs)) {
|
|
248
|
+
return true;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
try {
|
|
252
|
+
process.kill(pid, 'SIGKILL');
|
|
253
|
+
} catch {
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return waitForProcessExit(pid, forceTimeoutMs);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
async function stopInstanceProcess(pid, options = {}) {
|
|
260
|
+
if (!Number.isFinite(pid) || pid <= 0) {
|
|
261
|
+
return true;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const shutdownWaitMs = Number.isFinite(options.shutdownWaitMs) && options.shutdownWaitMs >= 0
|
|
265
|
+
? Math.trunc(options.shutdownWaitMs)
|
|
266
|
+
: 5000;
|
|
267
|
+
|
|
268
|
+
if (await waitForProcessExit(pid, shutdownWaitMs)) {
|
|
269
|
+
return true;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
return terminateProcessTree(pid, options);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
export {
|
|
277
|
+
getPidFilePath,
|
|
278
|
+
getInstanceFilePath,
|
|
279
|
+
readPidFile,
|
|
280
|
+
writePidFile,
|
|
281
|
+
removePidFile,
|
|
282
|
+
readInstanceOptions,
|
|
283
|
+
writeInstanceOptions,
|
|
284
|
+
removeInstanceFile,
|
|
285
|
+
isProcessRunning,
|
|
286
|
+
isOpenchamberCmdline,
|
|
287
|
+
isOpenchamberProcessRunning,
|
|
288
|
+
getOpenchamberProcessState,
|
|
289
|
+
hasOpenchamberRuntimeInfo,
|
|
290
|
+
terminateProcessTree,
|
|
291
|
+
stopInstanceProcess,
|
|
292
|
+
};
|