-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathserver.mjs
More file actions
525 lines (525 loc) · 18.1 KB
/
Copy pathserver.mjs
File metadata and controls
525 lines (525 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
import { createHmac, randomUUID } from "node:crypto";
import { mkdirSync, readdirSync, readFileSync, statSync, unlinkSync } from "node:fs";
import { createServer } from "node:http";
import { createRequire } from "node:module";
import { homedir } from "node:os";
import { join } from "node:path";
import { getHeapStatistics, writeHeapSnapshot } from "node:v8";
import next from "next";
import { WebSocket, WebSocketServer } from "ws";
const require2 = createRequire(import.meta.url);
const pty = require2("node-pty");
if (process.env.COVEN_CAVE_BUNDLE === "1" && !process.env.__NEXT_PRIVATE_STANDALONE_CONFIG) {
try {
const requiredServerFiles = JSON.parse(
readFileSync(new URL(".next/required-server-files.json", import.meta.url), "utf8")
);
if (requiredServerFiles.config) {
process.env.__NEXT_PRIVATE_STANDALONE_CONFIG = JSON.stringify(requiredServerFiles.config);
}
} catch {
}
}
function persistedMobileAccessSecretFile() {
const port2 = (process.env.PORT || "3000").trim() || "3000";
const stateRoot = process.env.COVEN_CAVE_MOBILE_STATE_ROOT?.trim() || join(
process.env.XDG_STATE_HOME?.trim() || join(homedir(), ".local", "state"),
"coven-cave"
);
const stateDir = process.env.COVEN_CAVE_MOBILE_STATE_DIR?.trim() || join(stateRoot, `mobile-tailscale-${port2}`);
return join(stateDir, "access-token");
}
if (process.env.COVEN_CAVE_BUNDLE !== "1" && process.env.COVEN_CAVE_E2E !== "1" && !process.env.COVEN_CAVE_ACCESS_TOKEN?.trim()) {
try {
const persisted = readFileSync(persistedMobileAccessSecretFile(), "utf8").trim();
if (persisted) process.env.COVEN_CAVE_ACCESS_TOKEN = persisted;
} catch {
}
}
function accessToken() {
return process.env.COVEN_CAVE_ACCESS_TOKEN ?? "";
}
const SIDECAR_TOKEN = process.env.COVEN_CAVE_AUTH_TOKEN ?? "";
const LOCAL_PEER_HEADER = "x-coven-cave-local-peer";
const LOCAL_PEER_SECRET = randomUUID();
process.env.COVEN_CAVE_LOCAL_PEER_SECRET = LOCAL_PEER_SECRET;
const FORWARDING_HEADERS = [
"forwarded",
"x-forwarded-for",
"x-forwarded-host",
"x-forwarded-proto",
"via"
];
const ACCESS_COOKIE = "coven_cave_access";
const LEGACY_ACCESS_COOKIE = "coven_access_token";
const ACCESS_QUERY_PARAM = "coven_access_token";
const SIDECAR_QUERY_PARAM = "covenCaveToken";
const sessions = /* @__PURE__ */ new Map();
const SCROLLBACK_LIMIT_BYTES = 256 * 1024;
const DETACH_GRACE_MS = (() => {
const env = Number.parseInt(process.env.COVEN_CAVE_PTY_DETACH_GRACE_MS ?? "", 10);
return Number.isFinite(env) && env > 0 ? env : 3e5;
})();
function appendScrollback(session, data) {
session.scrollback.push(data);
session.scrollbackBytes += data.length;
while (session.scrollbackBytes > SCROLLBACK_LIMIT_BYTES && session.scrollback.length > 1) {
const dropped = session.scrollback.shift();
if (dropped) session.scrollbackBytes -= dropped.length;
}
}
function getTokensFromCookie(header) {
if (!header) return [];
const tokens = [];
for (const part of header.split(";")) {
const [key, ...rest] = part.trim().split("=");
if (key === ACCESS_COOKIE || key === LEGACY_ACCESS_COOKIE) {
tokens.push(decodeURIComponent(rest.join("=") ?? ""));
}
}
return tokens;
}
function timingSafeEqualString(a, b) {
const aBytes = Buffer.from(a);
const bBytes = Buffer.from(b);
if (aBytes.length !== bBytes.length) return false;
let diff = 0;
for (let i = 0; i < aBytes.length; i += 1) {
diff |= aBytes[i] ^ bBytes[i];
}
return diff === 0;
}
function isExpectedAccessToken(value) {
const secret = accessToken();
if (!secret || !value) return false;
if (timingSafeEqualString(value, secret)) return true;
return isValidSignedAccessToken(value, secret);
}
function isExpectedSidecarToken(value) {
return Boolean(SIDECAR_TOKEN && value && timingSafeEqualString(value, SIDECAR_TOKEN));
}
function isExpectedPtyToken(value) {
return isExpectedAccessToken(value) || isExpectedSidecarToken(value);
}
function isValidSignedAccessToken(value, secret) {
const parts = value.split(".");
if (parts.length !== 4 || parts[0] !== "v1") return false;
const expiresAt = Number(parts[1]);
if (!Number.isFinite(expiresAt) || expiresAt <= Date.now()) return false;
if (!parts[2] || !parts[3]) return false;
const expected = createHmac("sha256", secret).update(`v1.${parts[1]}.${parts[2]}`).digest("base64url");
return timingSafeEqualString(parts[3], expected);
}
function bearerToken(req) {
const auth = req.headers.authorization ?? "";
return auth.startsWith("Bearer ") ? auth.slice("Bearer ".length).trim() : null;
}
function isLoopbackHost(host) {
if (!host) return false;
const hostname2 = host.startsWith("[") ? host.slice(1, host.indexOf("]")) : host.split(":")[0];
return hostname2 === "127.0.0.1" || hostname2 === "localhost" || hostname2 === "::1";
}
function isLoopbackAddress(value) {
if (!value) return false;
if (value === "::1" || value === "127.0.0.1") return true;
if (value.startsWith("::ffff:")) return value.slice("::ffff:".length) === "127.0.0.1";
return false;
}
function isDirectLoopbackRequest(req) {
if (!isLoopbackAddress(req.socket.remoteAddress)) return false;
for (const header of FORWARDING_HEADERS) {
if (req.headers[header] !== void 0) return false;
}
return isLoopbackHost(req.headers.host);
}
function sameOrigin(value, expectedOrigin) {
if (!value) return true;
try {
const url = new URL(value);
if (url.origin === expectedOrigin) return true;
const expected = new URL(expectedOrigin);
if (url.host === expected.host) return true;
return url.protocol === expected.protocol && url.port === expected.port && isLoopbackHost(url.host) && isLoopbackHost(expected.host);
} catch {
return false;
}
}
function isAllowedUpgradeSource(req, tokenAuthenticated = false) {
const host = req.headers.host;
if (!isLoopbackAddress(req.socket.remoteAddress)) return false;
if (!isLoopbackHost(host)) {
if (!host) return false;
if (tokenAuthenticated) return sameOrigin(req.headers.origin, `http://${host}`);
return false;
}
return sameOrigin(req.headers.origin, `http://${host}`);
}
function firstQueryValue(value) {
return Array.isArray(value) ? value[0] : value;
}
const UPGRADE_URL_BASE = "http://localhost";
const MAX_UPGRADE_QUERY_SEGMENTS = 1e3;
const ABSOLUTE_FORM_RE = /^[a-z][a-z\d+.-]*:\/\//i;
function boundedUpgradeQuery(suffix) {
if (!suffix.startsWith("?")) return "";
const fragmentStart = suffix.indexOf("#", 1);
const rawQuery = suffix.slice(1, fragmentStart === -1 ? void 0 : fragmentStart);
let segmentCount = 1;
for (let index = 0; index < rawQuery.length; index += 1) {
if (rawQuery[index] !== "&") continue;
if (segmentCount >= MAX_UPGRADE_QUERY_SEGMENTS) return rawQuery.slice(0, index);
segmentCount += 1;
}
return rawQuery;
}
function parseUpgradeTarget(rawUrl) {
const pathEnd = rawUrl.search(/[?#]/);
const rawPath = pathEnd === -1 ? rawUrl : rawUrl.slice(0, pathEnd);
const suffix = pathEnd === -1 ? "" : rawUrl.slice(pathEnd);
const normalizedPath = rawPath.replaceAll("\\", "/");
const absoluteForm = ABSOLUTE_FORM_RE.exec(normalizedPath);
const rootedPath = normalizedPath.startsWith("/") ? normalizedPath : `/${normalizedPath}`;
const parsedUrl = absoluteForm ? new URL(normalizedPath) : new URL(`/.${rootedPath}`, UPGRADE_URL_BASE);
parsedUrl.search = `?${boundedUpgradeQuery(suffix)}`;
let pathname = normalizedPath;
if (absoluteForm) {
const pathStart = normalizedPath.indexOf("/", absoluteForm[0].length);
pathname = pathStart === -1 ? "/" : normalizedPath.slice(pathStart);
}
const query = /* @__PURE__ */ Object.create(null);
for (const [key, value] of parsedUrl.searchParams) {
const current = query[key];
if (current === void 0) query[key] = value;
else if (Array.isArray(current)) current.push(value);
else query[key] = [current, value];
}
return { pathname, query };
}
function isPtyAuthRequired() {
return Boolean(accessToken() || SIDECAR_TOKEN);
}
function isAuthorized(req, query) {
if (!isPtyAuthRequired()) return false;
const queryToken = firstQueryValue(query[ACCESS_QUERY_PARAM]);
const sidecarQueryToken = firstQueryValue(query[SIDECAR_QUERY_PARAM]);
const candidates = [bearerToken(req), queryToken, sidecarQueryToken, ...getTokensFromCookie(req.headers.cookie)];
return candidates.some(isExpectedPtyToken);
}
function defaultShell() {
if (process.platform === "darwin") return "/bin/zsh";
if (process.platform === "win32") {
return "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";
}
return process.env.SHELL ?? "/bin/bash";
}
function defaultShellArgs() {
if (process.platform === "win32") return ["-NoLogo"];
return ["-l"];
}
function augmentedPath() {
const inherited = process.env.PATH ?? "";
const sep = process.platform === "win32" ? ";" : ":";
const extras = process.platform === "win32" ? [
"C:\\Windows\\System32",
"C:\\Windows",
"C:\\Program Files\\Git\\cmd",
"C:\\Program Files\\nodejs"
] : [
"/opt/homebrew/bin",
"/opt/homebrew/sbin",
"/usr/local/bin",
"/usr/local/sbin",
"/usr/bin",
"/bin",
"/usr/sbin",
"/sbin"
];
const seen = /* @__PURE__ */ new Set();
const out = [];
for (const part of inherited.split(sep).concat(extras)) {
if (!part || seen.has(part)) continue;
seen.add(part);
out.push(part);
}
return out.join(sep);
}
function validateCwd(raw) {
if (!raw) return void 0;
const stat = statSync(raw);
if (!stat.isDirectory()) {
throw new Error("projectRoot must be a directory");
}
return raw;
}
const PTY_ENV_DROPPED = /* @__PURE__ */ new Set(["NODE_ENV", "INIT_CWD", "PNPM_SCRIPT_SRC_DIR"]);
const PTY_ENV_DROPPED_PREFIXES = ["COVEN_CAVE_", "__NEXT_PRIVATE_"];
function sanitizedEnv() {
const env = {};
for (const [key, value] of Object.entries(process.env)) {
if (value === void 0) continue;
if (/^npm_/i.test(key)) continue;
if (PTY_ENV_DROPPED.has(key)) continue;
if (PTY_ENV_DROPPED_PREFIXES.some((prefix) => key.startsWith(prefix))) continue;
env[key] = value;
}
return env;
}
function sendPtyData(ws, data) {
if (ws.readyState !== WebSocket.OPEN) return;
const encoded = Buffer.from(data, "utf8");
const frame = Buffer.allocUnsafe(1 + encoded.length);
frame[0] = 1;
encoded.copy(frame, 1);
ws.send(frame);
}
function sendPtyExit(ws, exitCode) {
if (ws.readyState !== WebSocket.OPEN) return;
const frame = Buffer.allocUnsafe(5);
frame[0] = 2;
frame.writeInt32LE(exitCode, 1);
ws.send(frame);
}
function spawnPty(threadId, ws, cols, rows, cwd) {
const shell = pty.spawn(defaultShell(), defaultShellArgs(), {
name: "xterm-256color",
cols: cols > 0 ? cols : 120,
rows: rows > 0 ? rows : 40,
cwd: cwd ?? process.env.HOME ?? process.cwd(),
env: {
...sanitizedEnv(),
PATH: augmentedPath(),
TERM: "xterm-256color",
COLORTERM: "truecolor",
COVENCAVE: "1",
LANG: process.env.LANG ?? "en_US.UTF-8",
LC_ALL: process.env.LC_ALL ?? "en_US.UTF-8"
}
});
const session = {
pty: shell,
ws,
scrollback: [],
scrollbackBytes: 0,
detachTimer: null
};
sessions.set(threadId, session);
shell.onData((data) => {
appendScrollback(session, Buffer.from(data, "utf8"));
if (session.ws) sendPtyData(session.ws, data);
});
shell.onExit(({ exitCode }) => {
const current = sessions.get(threadId);
if (current?.pty === shell) {
if (current.detachTimer) clearTimeout(current.detachTimer);
sessions.delete(threadId);
}
if (session.ws) {
sendPtyExit(session.ws, exitCode ?? 0);
session.ws.close(1e3, "pty exit");
}
});
}
function rawDataToBuffer(data) {
if (Buffer.isBuffer(data)) return data;
if (Array.isArray(data)) return Buffer.concat(data);
return Buffer.from(data);
}
function onWsMessage(threadId, data) {
const session = sessions.get(threadId);
if (!session) return;
const frame = rawDataToBuffer(data);
const tag = frame[0];
if (tag === 3) {
session.pty.write(frame.subarray(1).toString("utf8"));
} else if (tag === 4 && frame.length >= 5) {
const cols = frame.readUInt16LE(1);
const rows = frame.readUInt16LE(3);
if (cols > 0 && rows > 0) {
session.pty.resize(cols, rows);
}
} else if (tag === 5) {
if (session.detachTimer) clearTimeout(session.detachTimer);
sessions.delete(threadId);
try {
session.pty.kill();
} catch {
}
}
}
function adoptSession(session, ws, cols, rows) {
if (session.detachTimer) {
clearTimeout(session.detachTimer);
session.detachTimer = null;
}
const previous = session.ws;
session.ws = ws;
if (previous && previous !== ws) {
try {
previous.close(1e3, "replaced");
} catch {
}
}
if (cols > 0 && rows > 0) {
try {
session.pty.resize(cols, rows);
} catch {
}
}
if (session.scrollbackBytes > 0) {
sendPtyData(ws, Buffer.concat(session.scrollback).toString("utf8"));
}
}
function handlePtyConnection(ws, threadId, cols, rows, cwd) {
const existing = sessions.get(threadId);
if (existing) {
adoptSession(existing, ws, cols, rows);
} else {
spawnPty(threadId, ws, cols, rows, cwd);
}
ws.on("message", (data) => onWsMessage(threadId, data));
ws.on("close", () => {
const session = sessions.get(threadId);
if (!session || session.ws !== ws) return;
session.ws = null;
if (session.detachTimer) clearTimeout(session.detachTimer);
session.detachTimer = setTimeout(() => {
const current = sessions.get(threadId);
if (current !== session || current.ws) return;
sessions.delete(threadId);
try {
session.pty.kill();
} catch {
}
}, DETACH_GRACE_MS);
});
}
const dev = process.env.NODE_ENV !== "production";
const hostname = process.env.HOSTNAME ?? "127.0.0.1";
const port = Number.parseInt(process.env.PORT ?? "3000", 10);
const app = next({ dev, hostname, port });
const handle = app.getRequestHandler();
const wss = new WebSocketServer({ noServer: true });
await app.prepare();
const nextUpgradeHandler = app.getUpgradeHandler();
const server = createServer((req, res) => {
delete req.headers[LOCAL_PEER_HEADER];
if (isDirectLoopbackRequest(req)) {
req.headers[LOCAL_PEER_HEADER] = LOCAL_PEER_SECRET;
}
void handle(req, res);
});
server.on("upgrade", (req, socket, head) => {
let pathname;
let query;
try {
({ pathname, query } = parseUpgradeTarget(req.url ?? "/"));
} catch {
socket.write("HTTP/1.1 400 Bad Request\r\n\r\n");
socket.destroy();
return;
}
if (pathname !== "/api/pty-ws") {
void nextUpgradeHandler(req, socket, head).catch((err) => {
console.error(`Failed to handle websocket upgrade for ${req.url ?? "unknown url"}`, err);
socket.destroy();
});
return;
}
const tokenAuthenticated = isPtyAuthRequired() ? isAuthorized(req, query) : false;
if (!isAllowedUpgradeSource(req, tokenAuthenticated)) {
socket.write("HTTP/1.1 403 Forbidden\r\n\r\n");
socket.destroy();
return;
}
if (isPtyAuthRequired() && !tokenAuthenticated && !isDirectLoopbackRequest(req)) {
socket.write("HTTP/1.1 401 Unauthorized\r\n\r\n");
socket.destroy();
return;
}
const threadId = String(query.threadId ?? "");
if (!threadId) {
socket.write("HTTP/1.1 400 Bad Request\r\n\r\n");
socket.destroy();
return;
}
let cwd;
try {
cwd = validateCwd(query.projectRoot ? String(query.projectRoot) : void 0);
} catch {
socket.write("HTTP/1.1 400 Bad Request\r\n\r\n");
socket.destroy();
return;
}
const cols = Number.parseInt(String(query.cols ?? "120"), 10);
const rows = Number.parseInt(String(query.rows ?? "40"), 10);
wss.handleUpgrade(req, socket, head, (ws) => {
handlePtyConnection(ws, threadId, cols, rows, cwd);
});
});
server.keepAliveTimeout = 75e3;
server.headersTimeout = 8e4;
server.listen(port, hostname, () => {
console.log(`> Ready on http://${hostname}:${port}`);
});
server.once("error", (err) => {
console.error(err);
process.exit(1);
});
const HEAP_MONITOR_ENABLED = process.env.COVEN_CAVE_HEAP_MONITOR !== "0";
const HEAP_MONITOR_INTERVAL_MS = (() => {
const env = Number.parseInt(process.env.COVEN_CAVE_HEAP_MONITOR_INTERVAL_MS ?? "", 10);
return Number.isFinite(env) && env > 0 ? env : 3e5;
})();
const HEAP_WARN_RATIO = 0.85;
const HEAP_SNAPSHOT_RATIO = 0.95;
const HEAP_SNAPSHOT_KEEP = 2;
let heapSnapshotSeq = 0;
function heapDiagnosticsDir() {
const covenHome = process.env.COVEN_HOME || join(homedir(), ".coven");
const caveHome = process.env.COVEN_CAVE_HOME || join(covenHome, "cave");
return join(caveHome, "diagnostics");
}
const mb = (bytes) => `${Math.round(bytes / (1024 * 1024))}MB`;
function pruneHeapSnapshots(dir) {
const snapshots = readdirSync(dir).filter((name) => name.startsWith("cave-heap-") && name.endsWith(".heapsnapshot")).sort();
while (snapshots.length > HEAP_SNAPSHOT_KEEP) {
const oldest = snapshots.shift();
try {
unlinkSync(join(dir, oldest));
} catch {
}
}
}
function startHeapMonitor() {
if (!HEAP_MONITOR_ENABLED) return;
let snapshotWritten = false;
const tick = () => {
const heap = getHeapStatistics();
const ratio = heap.used_heap_size / heap.heap_size_limit;
if (ratio < HEAP_WARN_RATIO) {
snapshotWritten = false;
return;
}
const usage = process.memoryUsage();
console.warn(
`[heap-monitor] heapUsed=${mb(heap.used_heap_size)} heapLimit=${mb(heap.heap_size_limit)} (${Math.round(ratio * 100)}%) rss=${mb(usage.rss)} external=${mb(usage.external)} ptySessions=${sessions.size} uptimeMin=${Math.round(process.uptime() / 60)}`
);
if (ratio < HEAP_SNAPSHOT_RATIO || snapshotWritten) return;
try {
const dir = heapDiagnosticsDir();
mkdirSync(dir, { recursive: true });
const stamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-");
const seq = String(heapSnapshotSeq += 1).padStart(3, "0");
const file = join(dir, `cave-heap-${stamp}-pid${process.pid}-${seq}.heapsnapshot`);
writeHeapSnapshot(file);
snapshotWritten = true;
pruneHeapSnapshots(dir);
console.warn(`[heap-monitor] wrote heap snapshot ${file}`);
} catch (err) {
snapshotWritten = true;
console.warn(`[heap-monitor] failed to write heap snapshot`, err);
}
};
setInterval(tick, HEAP_MONITOR_INTERVAL_MS).unref();
}
startHeapMonitor();