@neuralnomads/codenomad 0.9.3 → 0.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +60 -1
- package/dist/auth/manager.js +9 -0
- package/dist/config/schema.js +1 -0
- package/dist/filesystem/browser.js +3 -4
- package/dist/index.js +156 -27
- package/dist/opencode-config/package.json +3 -2
- package/dist/server/http-server.js +171 -27
- package/dist/server/network-addresses.js +72 -0
- package/dist/server/routes/auth.js +10 -4
- package/dist/server/routes/meta.js +23 -76
- package/dist/server/routes/worktrees.js +156 -0
- package/dist/server/tls.js +224 -0
- package/dist/workspaces/git-worktrees.js +199 -0
- package/dist/workspaces/instance-events.js +22 -2
- package/dist/workspaces/manager.js +2 -1
- package/dist/workspaces/worktree-map.js +116 -0
- package/package.json +5 -2
- package/public/apple-touch-icon-180x180.png +0 -0
- package/public/assets/core-DlQNAQKj.js +1 -0
- package/public/assets/index-BKijAOjE.js +1 -0
- package/public/assets/index-BrQt8BRm.js +1 -0
- package/public/assets/index-CcwbbFk8.js +1 -0
- package/public/assets/index-CqF9c3PC.css +1 -0
- package/public/assets/loading-CmEVQgyj.css +1 -0
- package/public/assets/loading-DhYlTYtL.js +1 -0
- package/public/assets/main-B7bETKAy.js +184 -0
- package/public/favicon.ico +0 -0
- package/public/index.html +7 -4
- package/public/loading.html +8 -5
- package/public/logo.png +0 -0
- package/public/manifest.webmanifest +1 -0
- package/public/maskable-icon-512x512.png +0 -0
- package/public/pwa-192x192.png +0 -0
- package/public/pwa-512x512.png +0 -0
- package/public/pwa-64x64.png +0 -0
- package/public/registerSW.js +1 -0
- package/public/sw.js +1 -0
- package/public/ui-version.json +1 -1
- package/public/workbox-60d14903.js +1 -0
- package/public/assets/index-Ciqv3YgW.js +0 -1
- package/public/assets/index-D0YCVzdv.css +0 -1
- package/public/assets/index-DPFZXALm.js +0 -1
- package/public/assets/loading-CnHVCykW.js +0 -1
- package/public/assets/loading-DHeOnB4V.css +0 -1
- package/public/assets/main-CgwL5PpX.js +0 -184
|
@@ -5,6 +5,7 @@ import replyFrom from "@fastify/reply-from";
|
|
|
5
5
|
import fs from "fs";
|
|
6
6
|
import path from "path";
|
|
7
7
|
import { fetch } from "undici";
|
|
8
|
+
import { isValidWorktreeSlug, listWorktrees, resolveRepoRoot } from "../workspaces/git-worktrees";
|
|
8
9
|
import { registerWorkspaceRoutes } from "./routes/workspaces";
|
|
9
10
|
import { registerConfigRoutes } from "./routes/config";
|
|
10
11
|
import { registerFilesystemRoutes } from "./routes/filesystem";
|
|
@@ -13,12 +14,17 @@ import { registerEventRoutes } from "./routes/events";
|
|
|
13
14
|
import { registerStorageRoutes } from "./routes/storage";
|
|
14
15
|
import { registerPluginRoutes } from "./routes/plugin";
|
|
15
16
|
import { registerBackgroundProcessRoutes } from "./routes/background-processes";
|
|
17
|
+
import { registerWorktreeRoutes } from "./routes/worktrees";
|
|
16
18
|
import { BackgroundProcessManager } from "../background-processes/manager";
|
|
17
19
|
import { registerAuthRoutes } from "./routes/auth";
|
|
18
20
|
import { sendUnauthorized, wantsHtml } from "../auth/http-auth";
|
|
19
|
-
const DEFAULT_HTTP_PORT = 9898;
|
|
20
21
|
export function createHttpServer(deps) {
|
|
21
|
-
|
|
22
|
+
// Fastify's type-level RawServer inference gets noisy when toggling HTTP vs HTTPS.
|
|
23
|
+
// We keep the runtime behavior correct and cast the instance to a generic FastifyInstance.
|
|
24
|
+
const app = Fastify({
|
|
25
|
+
logger: false,
|
|
26
|
+
...(deps.protocol === "https" && deps.httpsOptions ? { https: deps.httpsOptions } : {}),
|
|
27
|
+
});
|
|
22
28
|
const proxyLogger = deps.logger.child({ component: "proxy" });
|
|
23
29
|
const apiLogger = deps.logger.child({ component: "http" });
|
|
24
30
|
const sseLogger = deps.logger.child({ component: "sse" });
|
|
@@ -57,20 +63,37 @@ export function createHttpServer(deps) {
|
|
|
57
63
|
});
|
|
58
64
|
const allowedDevOrigins = new Set(["http://localhost:3000", "http://127.0.0.1:3000"]);
|
|
59
65
|
const isLoopbackHost = (host) => host === "127.0.0.1" || host === "::1" || host.startsWith("127.");
|
|
66
|
+
const getSelfOrigins = () => {
|
|
67
|
+
const origins = new Set();
|
|
68
|
+
const candidates = [deps.serverMeta.localUrl, deps.serverMeta.remoteUrl];
|
|
69
|
+
for (const candidate of candidates) {
|
|
70
|
+
if (!candidate)
|
|
71
|
+
continue;
|
|
72
|
+
try {
|
|
73
|
+
origins.add(new URL(candidate).origin);
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
// ignore
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
for (const addr of deps.serverMeta.addresses ?? []) {
|
|
80
|
+
try {
|
|
81
|
+
origins.add(new URL(addr.remoteUrl).origin);
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
// ignore
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return origins;
|
|
88
|
+
};
|
|
60
89
|
app.register(cors, {
|
|
61
90
|
origin: (origin, cb) => {
|
|
62
91
|
if (!origin) {
|
|
63
92
|
cb(null, true);
|
|
64
93
|
return;
|
|
65
94
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
selfOrigin = new URL(deps.serverMeta.httpBaseUrl).origin;
|
|
69
|
-
}
|
|
70
|
-
catch {
|
|
71
|
-
selfOrigin = null;
|
|
72
|
-
}
|
|
73
|
-
if (selfOrigin && origin === selfOrigin) {
|
|
95
|
+
const selfOrigins = getSelfOrigins();
|
|
96
|
+
if (selfOrigins.has(origin)) {
|
|
74
97
|
cb(null, true);
|
|
75
98
|
return;
|
|
76
99
|
}
|
|
@@ -79,7 +102,7 @@ export function createHttpServer(deps) {
|
|
|
79
102
|
return;
|
|
80
103
|
}
|
|
81
104
|
// When we bind to a non-loopback host (e.g., 0.0.0.0 or LAN IP), allow cross-origin UI access.
|
|
82
|
-
if (deps.
|
|
105
|
+
if (deps.bindHost === "0.0.0.0" || !isLoopbackHost(deps.bindHost)) {
|
|
83
106
|
cb(null, true);
|
|
84
107
|
return;
|
|
85
108
|
}
|
|
@@ -162,6 +185,7 @@ export function createHttpServer(deps) {
|
|
|
162
185
|
registerFilesystemRoutes(app, { fileSystemBrowser: deps.fileSystemBrowser });
|
|
163
186
|
registerMetaRoutes(app, { serverMeta: deps.serverMeta });
|
|
164
187
|
registerEventRoutes(app, { eventBus: deps.eventBus, registerClient: registerSseClient, logger: sseLogger });
|
|
188
|
+
registerWorktreeRoutes(app, { workspaceManager: deps.workspaceManager });
|
|
165
189
|
registerStorageRoutes(app, {
|
|
166
190
|
instanceStore: deps.instanceStore,
|
|
167
191
|
eventBus: deps.eventBus,
|
|
@@ -180,11 +204,11 @@ export function createHttpServer(deps) {
|
|
|
180
204
|
instance: app,
|
|
181
205
|
start: async () => {
|
|
182
206
|
const attemptListen = async (requestedPort) => {
|
|
183
|
-
const addressInfo = await app.listen({ port: requestedPort, host: deps.
|
|
207
|
+
const addressInfo = await app.listen({ port: requestedPort, host: deps.bindHost });
|
|
184
208
|
return { addressInfo, requestedPort };
|
|
185
209
|
};
|
|
186
|
-
const autoPortRequested = deps.
|
|
187
|
-
const primaryPort = autoPortRequested ?
|
|
210
|
+
const autoPortRequested = deps.bindPort === 0;
|
|
211
|
+
const primaryPort = autoPortRequested ? deps.defaultPort : deps.bindPort;
|
|
188
212
|
const shouldRetryWithEphemeral = (error) => {
|
|
189
213
|
if (!autoPortRequested)
|
|
190
214
|
return false;
|
|
@@ -218,14 +242,9 @@ export function createHttpServer(deps) {
|
|
|
218
242
|
actualPort = address.port;
|
|
219
243
|
}
|
|
220
244
|
}
|
|
221
|
-
const displayHost = deps.
|
|
222
|
-
const serverUrl =
|
|
223
|
-
deps.
|
|
224
|
-
deps.serverMeta.host = deps.host;
|
|
225
|
-
deps.serverMeta.port = actualPort;
|
|
226
|
-
deps.serverMeta.listeningMode = deps.host === "0.0.0.0" || !isLoopbackHost(deps.host) ? "all" : "local";
|
|
227
|
-
deps.logger.info({ port: actualPort, host: deps.host }, "HTTP server listening");
|
|
228
|
-
console.log(`CodeNomad Server is ready at ${serverUrl}`);
|
|
245
|
+
const displayHost = deps.bindHost === "127.0.0.1" ? "localhost" : deps.bindHost;
|
|
246
|
+
const serverUrl = `${deps.protocol}://${displayHost}:${actualPort}`;
|
|
247
|
+
deps.logger.info({ port: actualPort, host: deps.bindHost, protocol: deps.protocol }, "HTTP server listening");
|
|
229
248
|
return { port: actualPort, url: serverUrl, displayHost };
|
|
230
249
|
},
|
|
231
250
|
stop: () => {
|
|
@@ -243,6 +262,7 @@ function registerInstanceProxyRoutes(app, deps) {
|
|
|
243
262
|
request,
|
|
244
263
|
reply,
|
|
245
264
|
workspaceManager: deps.workspaceManager,
|
|
265
|
+
worktreeSlug: request.params.slug,
|
|
246
266
|
pathSuffix: "",
|
|
247
267
|
logger: deps.logger,
|
|
248
268
|
});
|
|
@@ -252,19 +272,78 @@ function registerInstanceProxyRoutes(app, deps) {
|
|
|
252
272
|
request,
|
|
253
273
|
reply,
|
|
254
274
|
workspaceManager: deps.workspaceManager,
|
|
275
|
+
worktreeSlug: request.params.slug,
|
|
255
276
|
pathSuffix: request.params["*"] ?? "",
|
|
256
277
|
logger: deps.logger,
|
|
257
278
|
});
|
|
258
279
|
};
|
|
259
|
-
instance.all("/workspaces/:id/instance", proxyBaseHandler);
|
|
260
|
-
instance.all("/workspaces/:id/instance/*", proxyWildcardHandler);
|
|
280
|
+
instance.all("/workspaces/:id/worktrees/:slug/instance", proxyBaseHandler);
|
|
281
|
+
instance.all("/workspaces/:id/worktrees/:slug/instance/*", proxyWildcardHandler);
|
|
261
282
|
});
|
|
262
283
|
}
|
|
263
284
|
const INSTANCE_PROXY_HOST = "127.0.0.1";
|
|
264
285
|
async function proxyWorkspaceRequest(args) {
|
|
265
|
-
const { request, reply, workspaceManager, logger } = args;
|
|
286
|
+
const { request, reply, workspaceManager, logger, worktreeSlug } = args;
|
|
266
287
|
const workspaceId = request.params.id;
|
|
267
288
|
const workspace = workspaceManager.get(workspaceId);
|
|
289
|
+
const bodyToJson = (body) => {
|
|
290
|
+
if (body == null)
|
|
291
|
+
return null;
|
|
292
|
+
const anyBody = body;
|
|
293
|
+
if (anyBody && typeof anyBody.pipe === "function") {
|
|
294
|
+
// Don't consume streams (would break proxying).
|
|
295
|
+
// Best-effort: if the stream already has buffered chunks, parse those.
|
|
296
|
+
try {
|
|
297
|
+
const buffered = anyBody?._readableState?.buffer;
|
|
298
|
+
if (Array.isArray(buffered) && buffered.length > 0) {
|
|
299
|
+
const chunks = [];
|
|
300
|
+
for (const entry of buffered) {
|
|
301
|
+
if (!entry)
|
|
302
|
+
continue;
|
|
303
|
+
if (Buffer.isBuffer(entry)) {
|
|
304
|
+
chunks.push(entry);
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
const data = entry.data;
|
|
308
|
+
if (Buffer.isBuffer(data)) {
|
|
309
|
+
chunks.push(data);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
if (chunks.length > 0) {
|
|
313
|
+
const text = Buffer.concat(chunks).toString("utf-8");
|
|
314
|
+
try {
|
|
315
|
+
return JSON.parse(text);
|
|
316
|
+
}
|
|
317
|
+
catch {
|
|
318
|
+
return { __raw: text };
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
catch {
|
|
324
|
+
// fall through
|
|
325
|
+
}
|
|
326
|
+
return { __stream: true };
|
|
327
|
+
}
|
|
328
|
+
const maybeParse = (input) => {
|
|
329
|
+
try {
|
|
330
|
+
return JSON.parse(input);
|
|
331
|
+
}
|
|
332
|
+
catch {
|
|
333
|
+
return { __raw: input };
|
|
334
|
+
}
|
|
335
|
+
};
|
|
336
|
+
if (Buffer.isBuffer(body)) {
|
|
337
|
+
return maybeParse(body.toString("utf-8"));
|
|
338
|
+
}
|
|
339
|
+
if (typeof body === "string") {
|
|
340
|
+
return maybeParse(body);
|
|
341
|
+
}
|
|
342
|
+
if (typeof body === "object") {
|
|
343
|
+
return body;
|
|
344
|
+
}
|
|
345
|
+
return body;
|
|
346
|
+
};
|
|
268
347
|
if (!workspace) {
|
|
269
348
|
reply.code(404).send({ error: "Workspace not found" });
|
|
270
349
|
return;
|
|
@@ -274,6 +353,20 @@ async function proxyWorkspaceRequest(args) {
|
|
|
274
353
|
reply.code(502).send({ error: "Workspace instance is not ready" });
|
|
275
354
|
return;
|
|
276
355
|
}
|
|
356
|
+
if (!isValidWorktreeSlug(worktreeSlug)) {
|
|
357
|
+
reply.code(400).send({ error: "Invalid worktree slug" });
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
const directory = await resolveWorktreeDirectory({
|
|
361
|
+
workspaceId,
|
|
362
|
+
workspacePath: workspace.path,
|
|
363
|
+
worktreeSlug,
|
|
364
|
+
logger,
|
|
365
|
+
});
|
|
366
|
+
if (!directory) {
|
|
367
|
+
reply.code(404).send({ error: "Worktree not found" });
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
277
370
|
const normalizedSuffix = normalizeInstanceSuffix(args.pathSuffix);
|
|
278
371
|
const queryIndex = (request.raw.url ?? "").indexOf("?");
|
|
279
372
|
const search = queryIndex >= 0 ? (request.raw.url ?? "").slice(queryIndex) : "";
|
|
@@ -288,12 +381,33 @@ async function proxyWorkspaceRequest(args) {
|
|
|
288
381
|
if (instanceAuthHeader) {
|
|
289
382
|
headers.authorization = instanceAuthHeader;
|
|
290
383
|
}
|
|
291
|
-
// Enforce per-workspace directory scoping for all proxied OpenCode requests.
|
|
292
384
|
// OpenCode expects the *full* path; we send it via header to avoid query tampering.
|
|
293
|
-
const directory = workspace.path;
|
|
294
385
|
const isNonASCII = /[^\x00-\x7F]/.test(directory);
|
|
295
386
|
const encodedDirectory = isNonASCII ? encodeURIComponent(directory) : directory;
|
|
296
387
|
headers["x-opencode-directory"] = encodedDirectory;
|
|
388
|
+
if (logger.isLevelEnabled("trace")) {
|
|
389
|
+
const outgoing = {};
|
|
390
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
391
|
+
outgoing[key] = value;
|
|
392
|
+
}
|
|
393
|
+
// Redact sensitive headers.
|
|
394
|
+
for (const key of Object.keys(outgoing)) {
|
|
395
|
+
const lower = key.toLowerCase();
|
|
396
|
+
if (lower === "authorization" || lower === "cookie" || lower === "set-cookie") {
|
|
397
|
+
outgoing[key] = "<redacted>";
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
logger.trace({
|
|
401
|
+
workspaceId,
|
|
402
|
+
method: request.method,
|
|
403
|
+
targetUrl,
|
|
404
|
+
worktreeSlug,
|
|
405
|
+
directory,
|
|
406
|
+
contentType: request.headers["content-type"],
|
|
407
|
+
body: bodyToJson(request.body),
|
|
408
|
+
headers: outgoing,
|
|
409
|
+
}, "Proxy -> OpenCode request");
|
|
410
|
+
}
|
|
297
411
|
return headers;
|
|
298
412
|
},
|
|
299
413
|
onError: (proxyReply, { error }) => {
|
|
@@ -311,6 +425,36 @@ function normalizeInstanceSuffix(pathSuffix) {
|
|
|
311
425
|
const trimmed = pathSuffix.replace(/^\/+/, "");
|
|
312
426
|
return trimmed.length === 0 ? "/" : `/${trimmed}`;
|
|
313
427
|
}
|
|
428
|
+
const WORKTREE_CACHE_TTL_MS = 2000;
|
|
429
|
+
const worktreeCache = new Map();
|
|
430
|
+
async function getCachedWorktrees(params) {
|
|
431
|
+
const cached = worktreeCache.get(params.workspaceId);
|
|
432
|
+
const now = Date.now();
|
|
433
|
+
if (cached && cached.expiresAt > now) {
|
|
434
|
+
return cached;
|
|
435
|
+
}
|
|
436
|
+
const { repoRoot } = await resolveRepoRoot(params.workspacePath, params.logger);
|
|
437
|
+
const worktrees = await listWorktrees({ repoRoot, workspaceFolder: params.workspacePath, logger: params.logger });
|
|
438
|
+
const entry = {
|
|
439
|
+
expiresAt: now + WORKTREE_CACHE_TTL_MS,
|
|
440
|
+
repoRoot,
|
|
441
|
+
worktrees: worktrees.map((wt) => ({ slug: wt.slug, directory: wt.directory })),
|
|
442
|
+
};
|
|
443
|
+
worktreeCache.set(params.workspaceId, entry);
|
|
444
|
+
return entry;
|
|
445
|
+
}
|
|
446
|
+
async function resolveWorktreeDirectory(params) {
|
|
447
|
+
const { worktreeSlug } = params;
|
|
448
|
+
const cached = await getCachedWorktrees({ workspaceId: params.workspaceId, workspacePath: params.workspacePath, logger: params.logger });
|
|
449
|
+
const match = cached.worktrees.find((wt) => wt.slug === worktreeSlug);
|
|
450
|
+
if (match) {
|
|
451
|
+
return match.directory;
|
|
452
|
+
}
|
|
453
|
+
// If the slug is new (e.g., created moments ago), refresh once.
|
|
454
|
+
worktreeCache.delete(params.workspaceId);
|
|
455
|
+
const refreshed = await getCachedWorktrees({ workspaceId: params.workspaceId, workspacePath: params.workspacePath, logger: params.logger });
|
|
456
|
+
return refreshed.worktrees.find((wt) => wt.slug === worktreeSlug)?.directory ?? null;
|
|
457
|
+
}
|
|
314
458
|
function setupStaticUi(app, uiDir, authManager) {
|
|
315
459
|
if (!uiDir) {
|
|
316
460
|
app.log.warn("UI static directory not provided; API endpoints only");
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import os from "os";
|
|
2
|
+
export function resolveNetworkAddresses(args) {
|
|
3
|
+
const { host, protocol, port } = args;
|
|
4
|
+
const interfaces = os.networkInterfaces();
|
|
5
|
+
const seen = new Set();
|
|
6
|
+
const results = [];
|
|
7
|
+
const addAddress = (ip, scope) => {
|
|
8
|
+
if (!ip || ip === "0.0.0.0")
|
|
9
|
+
return;
|
|
10
|
+
const key = `ipv4-${ip}`;
|
|
11
|
+
if (seen.has(key))
|
|
12
|
+
return;
|
|
13
|
+
seen.add(key);
|
|
14
|
+
results.push({ ip, family: "ipv4", scope, remoteUrl: `${protocol}://${ip}:${port}` });
|
|
15
|
+
};
|
|
16
|
+
const normalizeFamily = (value) => {
|
|
17
|
+
if (typeof value === "string") {
|
|
18
|
+
const lowered = value.toLowerCase();
|
|
19
|
+
if (lowered === "ipv4") {
|
|
20
|
+
return "ipv4";
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
if (value === 4)
|
|
24
|
+
return "ipv4";
|
|
25
|
+
return null;
|
|
26
|
+
};
|
|
27
|
+
if (host === "0.0.0.0") {
|
|
28
|
+
// Enumerate system interfaces (IPv4 only)
|
|
29
|
+
for (const entries of Object.values(interfaces)) {
|
|
30
|
+
if (!entries)
|
|
31
|
+
continue;
|
|
32
|
+
for (const entry of entries) {
|
|
33
|
+
const family = normalizeFamily(entry.family);
|
|
34
|
+
if (!family)
|
|
35
|
+
continue;
|
|
36
|
+
if (!entry.address || entry.address === "0.0.0.0")
|
|
37
|
+
continue;
|
|
38
|
+
const scope = entry.internal ? "loopback" : "external";
|
|
39
|
+
addAddress(entry.address, scope);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
// Always include loopback address
|
|
44
|
+
addAddress("127.0.0.1", "loopback");
|
|
45
|
+
// Include explicitly configured host if it was IPv4
|
|
46
|
+
if (isIPv4Address(host) && host !== "0.0.0.0") {
|
|
47
|
+
const isLoopback = host.startsWith("127.");
|
|
48
|
+
addAddress(host, isLoopback ? "loopback" : "external");
|
|
49
|
+
}
|
|
50
|
+
const scopeWeight = { external: 0, internal: 1, loopback: 2 };
|
|
51
|
+
return results.sort((a, b) => {
|
|
52
|
+
const scopeDelta = scopeWeight[a.scope] - scopeWeight[b.scope];
|
|
53
|
+
if (scopeDelta !== 0)
|
|
54
|
+
return scopeDelta;
|
|
55
|
+
return a.ip.localeCompare(b.ip);
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
function isIPv4Address(value) {
|
|
59
|
+
if (!value)
|
|
60
|
+
return false;
|
|
61
|
+
const parts = value.split(".");
|
|
62
|
+
if (parts.length !== 4)
|
|
63
|
+
return false;
|
|
64
|
+
return parts.every((part) => {
|
|
65
|
+
if (part.length === 0 || part.length > 3)
|
|
66
|
+
return false;
|
|
67
|
+
if (!/^[0-9]+$/.test(part))
|
|
68
|
+
return false;
|
|
69
|
+
const num = Number(part);
|
|
70
|
+
return Number.isInteger(num) && num >= 0 && num <= 255;
|
|
71
|
+
});
|
|
72
|
+
}
|
|
@@ -66,7 +66,7 @@ export function registerAuthRoutes(app, deps) {
|
|
|
66
66
|
return;
|
|
67
67
|
}
|
|
68
68
|
const session = deps.authManager.createSession(body.username);
|
|
69
|
-
deps.authManager.
|
|
69
|
+
deps.authManager.setSessionCookieWithOptions(reply, session.id, { secure: isSecureRequest(request) });
|
|
70
70
|
reply.send({ ok: true });
|
|
71
71
|
});
|
|
72
72
|
app.post("/api/auth/token", async (request, reply) => {
|
|
@@ -86,11 +86,11 @@ export function registerAuthRoutes(app, deps) {
|
|
|
86
86
|
}
|
|
87
87
|
const username = deps.authManager.getStatus().username;
|
|
88
88
|
const session = deps.authManager.createSession(username);
|
|
89
|
-
deps.authManager.
|
|
89
|
+
deps.authManager.setSessionCookieWithOptions(reply, session.id, { secure: isSecureRequest(request) });
|
|
90
90
|
reply.send({ ok: true });
|
|
91
91
|
});
|
|
92
|
-
app.post("/api/auth/logout", async (
|
|
93
|
-
deps.authManager.
|
|
92
|
+
app.post("/api/auth/logout", async (request, reply) => {
|
|
93
|
+
deps.authManager.clearSessionCookieWithOptions(reply, { secure: isSecureRequest(request) });
|
|
94
94
|
reply.send({ ok: true });
|
|
95
95
|
});
|
|
96
96
|
app.post("/api/auth/password", async (request, reply) => {
|
|
@@ -110,6 +110,12 @@ export function registerAuthRoutes(app, deps) {
|
|
|
110
110
|
}
|
|
111
111
|
});
|
|
112
112
|
}
|
|
113
|
+
function isSecureRequest(request) {
|
|
114
|
+
if (request.protocol === "https") {
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
return Boolean(request.raw?.socket && request.raw.socket.encrypted);
|
|
118
|
+
}
|
|
113
119
|
function escapeHtml(value) {
|
|
114
120
|
return value.replace(/[&<>"]/g, (char) => {
|
|
115
121
|
switch (char) {
|
|
@@ -1,23 +1,25 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { resolveNetworkAddresses } from "../network-addresses";
|
|
2
2
|
export function registerMetaRoutes(app, deps) {
|
|
3
3
|
app.get("/api/meta", async () => buildMetaResponse(deps.serverMeta));
|
|
4
4
|
}
|
|
5
5
|
function buildMetaResponse(meta) {
|
|
6
|
-
const
|
|
7
|
-
const
|
|
6
|
+
const localPort = resolveLocalPort(meta);
|
|
7
|
+
const remote = resolveRemote(meta);
|
|
8
|
+
const addresses = remote && remote.port > 0 ? resolveNetworkAddresses({ host: meta.host, protocol: remote.protocol, port: remote.port }) : [];
|
|
8
9
|
return {
|
|
9
10
|
...meta,
|
|
10
|
-
|
|
11
|
+
localPort,
|
|
12
|
+
remotePort: remote?.port,
|
|
11
13
|
listeningMode: meta.host === "0.0.0.0" || !isLoopbackHost(meta.host) ? "all" : "local",
|
|
12
14
|
addresses,
|
|
13
15
|
};
|
|
14
16
|
}
|
|
15
|
-
function
|
|
16
|
-
if (Number.isInteger(meta.
|
|
17
|
-
return meta.
|
|
17
|
+
function resolveLocalPort(meta) {
|
|
18
|
+
if (Number.isInteger(meta.localPort) && meta.localPort > 0) {
|
|
19
|
+
return meta.localPort;
|
|
18
20
|
}
|
|
19
21
|
try {
|
|
20
|
-
const parsed = new URL(meta.
|
|
22
|
+
const parsed = new URL(meta.localUrl);
|
|
21
23
|
const port = Number(parsed.port);
|
|
22
24
|
return Number.isInteger(port) && port > 0 ? port : 0;
|
|
23
25
|
}
|
|
@@ -25,76 +27,21 @@ function resolvePort(meta) {
|
|
|
25
27
|
return 0;
|
|
26
28
|
}
|
|
27
29
|
}
|
|
28
|
-
function
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
function resolveAddresses(port, host) {
|
|
32
|
-
const interfaces = os.networkInterfaces();
|
|
33
|
-
const seen = new Set();
|
|
34
|
-
const results = [];
|
|
35
|
-
const addAddress = (ip, scope) => {
|
|
36
|
-
if (!ip || ip === "0.0.0.0")
|
|
37
|
-
return;
|
|
38
|
-
const key = `ipv4-${ip}`;
|
|
39
|
-
if (seen.has(key))
|
|
40
|
-
return;
|
|
41
|
-
seen.add(key);
|
|
42
|
-
results.push({ ip, family: "ipv4", scope, url: `http://${ip}:${port}` });
|
|
43
|
-
};
|
|
44
|
-
const normalizeFamily = (value) => {
|
|
45
|
-
if (typeof value === "string") {
|
|
46
|
-
const lowered = value.toLowerCase();
|
|
47
|
-
if (lowered === "ipv4") {
|
|
48
|
-
return "ipv4";
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
if (value === 4)
|
|
52
|
-
return "ipv4";
|
|
30
|
+
function resolveRemote(meta) {
|
|
31
|
+
if (!meta.remoteUrl) {
|
|
53
32
|
return null;
|
|
54
|
-
};
|
|
55
|
-
if (host === "0.0.0.0") {
|
|
56
|
-
// Enumerate system interfaces (IPv4 only)
|
|
57
|
-
for (const entries of Object.values(interfaces)) {
|
|
58
|
-
if (!entries)
|
|
59
|
-
continue;
|
|
60
|
-
for (const entry of entries) {
|
|
61
|
-
const family = normalizeFamily(entry.family);
|
|
62
|
-
if (!family)
|
|
63
|
-
continue;
|
|
64
|
-
if (!entry.address || entry.address === "0.0.0.0")
|
|
65
|
-
continue;
|
|
66
|
-
const scope = entry.internal ? "loopback" : "external";
|
|
67
|
-
addAddress(entry.address, scope);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
33
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
34
|
+
try {
|
|
35
|
+
const parsed = new URL(meta.remoteUrl);
|
|
36
|
+
const protocol = parsed.protocol === "https:" ? "https" : "http";
|
|
37
|
+
const port = Number(parsed.port);
|
|
38
|
+
return { protocol, port: Number.isInteger(port) && port > 0 ? port : 0 };
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return null;
|
|
77
42
|
}
|
|
78
|
-
const scopeWeight = { external: 0, internal: 1, loopback: 2 };
|
|
79
|
-
return results.sort((a, b) => {
|
|
80
|
-
const scopeDelta = scopeWeight[a.scope] - scopeWeight[b.scope];
|
|
81
|
-
if (scopeDelta !== 0)
|
|
82
|
-
return scopeDelta;
|
|
83
|
-
return a.ip.localeCompare(b.ip);
|
|
84
|
-
});
|
|
85
43
|
}
|
|
86
|
-
function
|
|
87
|
-
|
|
88
|
-
return false;
|
|
89
|
-
const parts = value.split(".");
|
|
90
|
-
if (parts.length !== 4)
|
|
91
|
-
return false;
|
|
92
|
-
return parts.every((part) => {
|
|
93
|
-
if (part.length === 0 || part.length > 3)
|
|
94
|
-
return false;
|
|
95
|
-
if (!/^[0-9]+$/.test(part))
|
|
96
|
-
return false;
|
|
97
|
-
const num = Number(part);
|
|
98
|
-
return Number.isInteger(num) && num >= 0 && num <= 255;
|
|
99
|
-
});
|
|
44
|
+
function isLoopbackHost(host) {
|
|
45
|
+
return host === "127.0.0.1" || host === "::1" || host.startsWith("127.");
|
|
100
46
|
}
|
|
47
|
+
// NetworkAddress shape is resolved in ../network-addresses
|