From fd7775b4e9c24b6bb8f664beb7d4fd2ccecd8d85 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 12 Oct 2026 15:45:52 -0300 Subject: 9web: serve local sites with 9P build controls and passive listener discovery Add optional catalogue mode with an explicit site registry, per-site HTTP roots, MIME/index/pretty-URL handling, HEAD and byte ranges. Existing generated sites are available immediately, without preview processes. Expose build, watch, cancel, status, logs and state events through the shared 9P backend and HTTP dashboard. Supervise process groups, coalesce queued rebuilds and watch configured source paths. Discover Linux TCP listeners by socket inode ownership and executable/argument recognizers; never probe endpoints. Document configuration, the user-systemd unit, workstation ports, network access assumptions and limitations including in-place publication and static-only Worker sites. Keep the original upstream gateway mode. Validation: catalogue integration suite (including paginated directory reads, cancellation and parked events), existing HTTP filesystem and mux suites, real 9ns mount, dual-stack service restart and all five workstation sites. Real entomology build: 129 pages, zero failures. --- web/catalog.zig | 549 ++++++++++++++++++++++++++++++++++++++++++++++++ web/discover.py | 93 ++++++++ web/main.zig | 196 ++++++++++++++++- web/static/catalog.html | 14 ++ web/static/catalog.js | 56 +++++ 5 files changed, 905 insertions(+), 3 deletions(-) create mode 100644 web/catalog.zig create mode 100644 web/discover.py create mode 100644 web/static/catalog.html create mode 100644 web/static/catalog.js (limited to 'web') diff --git a/web/catalog.zig b/web/catalog.zig new file mode 100644 index 0000000..5e75555 --- /dev/null +++ b/web/catalog.zig @@ -0,0 +1,549 @@ +//! Local sites, build supervision and passive listener discovery. All state is +//! owned by 9web; HTTP and 9P use the same controls. Linux catalogue mode only. +const std = @import("std"); +const c9 = @import("cloud9"); +const Io = std.Io; +const A = std.heap.page_allocator; +const fs = c9.fs; +const Backend = struct { + pub const Req = fs.Req; + pub const Reply = fs.Reply; +}; +pub const Runner = c9.serve.Runner(Backend, .{ .fid_capacity = 512, .name_capacity = 255 }, .{ .msize = 65536, .connections = 16 }); + +pub const Site = struct { + name: []const u8, + title: []const u8, + cwd: []const u8, + root: []const u8, + port: u16, + build: []const []const u8 = &.{}, + watch: []const []const u8 = &.{}, + // Poll only these source paths for a managed rebuild watcher. + inputs: []const []const u8 = &.{}, +}; +const Settings = struct { sites: []const Site }; +const State = struct { + phase: []const u8 = "idle", + watching: bool = false, + queued: bool = false, + cancel: bool = false, + pid: ?i32 = null, + exit_code: ?u8 = null, + started: i64 = 0, + finished: i64 = 0, + generation: u64 = 0, + fingerprint: u64 = 0, + revision: u64 = 1, +}; +const EventCursor = struct { + used: bool = false, + revision: u64 = 0, + bytes: [2048]u8 = undefined, + len: usize = 0, + pos: usize = 0, +}; +pub const Catalog = struct { + io: Io, + sites: []const Site, + states: []State, + state_dir: []const u8, + mutex: Io.Mutex = .init, + nodes: std.ArrayList([]const u8) = .empty, + listeners: []const u8 = "[]", + discovery_error: []const u8 = "", + group: Io.Group = .init, + runner: *Runner, + http_port: u16 = 0, + cursors: [16][64]EventCursor = @splat(@splat(.{})), + + pub fn init(io: Io, config: []const u8, state_dir: []const u8) !*Catalog { + const bytes = try Io.Dir.cwd().readFileAlloc(io, config, A, .limited(1024 * 1024)); + defer A.free(bytes); + const parsed = try std.json.parseFromSlice(Settings, A, bytes, .{ .allocate = .alloc_always }); + // Config is immutable for the lifetime of the process. + var names: std.StringHashMap(void) = .init(A); + defer names.deinit(); + for (parsed.value.sites, 0..) |s, i| { + if (s.name.len == 0 or s.port == 0 or !std.fs.path.isAbsolute(s.root) or !std.fs.path.isAbsolute(s.cwd)) return error.InvalidSite; + for (s.name) |ch| if (!std.ascii.isAlphanumeric(ch) and ch != '-' and ch != '_') return error.InvalidSiteName; + if (names.contains(s.name)) return error.DuplicateSite; + try names.put(s.name, {}); + for (parsed.value.sites[0..i]) |other| if (other.port == s.port) return error.DuplicatePort; + for (s.inputs) |input| if (!std.fs.path.isAbsolute(input) and !safeRelative(input)) return error.InvalidWatchInput; + } + try Io.Dir.cwd().createDirPath(io, state_dir); + const cat = try A.create(Catalog); + cat.* = .{ .io = io, .sites = parsed.value.sites, .states = try A.alloc(State, parsed.value.sites.len), .state_dir = state_dir, .runner = try A.create(Runner) }; + @memset(cat.states, .{}); + try cat.nodes.append(A, ""); + cat.runner.init(.{ .io = io, .root = 1, .handler = .{ .ctx = cat, .serve = serve } }); + return cat; + } + + pub fn start(cat: *Catalog, socket: [:0]const u8) !void { + _ = try cat.runner.listen(.{ .unix = socket }, 16); + try cat.group.concurrent(cat.io, discoverLoop, .{cat}); + for (cat.sites, 0..) |_, i| try cat.group.concurrent(cat.io, siteLoop, .{ cat, i }); + } + + pub fn stop(cat: *Catalog) void { + cat.group.cancel(cat.io); + cat.runner.stop(); + } + + fn now(cat: *Catalog) i64 { + return Io.Timestamp.now(cat.io, .real).toSeconds(); + } + fn logPath(cat: *Catalog, a: std.mem.Allocator, i: usize) ![]const u8 { + return std.fmt.allocPrint(a, "{s}/{s}.log", .{ cat.state_dir, cat.sites[i].name }); + } + pub fn control(cat: *Catalog, i: usize, command: []const u8) !void { + cat.mutex.lockUncancelable(cat.io); + defer cat.mutex.unlock(cat.io); + const cmd = std.mem.trim(u8, command, " \r\n\t"); + const s = &cat.states[i]; + if (std.mem.eql(u8, cmd, "build")) { + if (cat.sites[i].build.len == 0) return error.NoBuildCommand; + if (s.watching and cat.sites[i].watch.len != 0) return error.NativeWatcherRunning; + s.queued = true; + } else if (std.mem.eql(u8, cmd, "watch on")) { + if (cat.sites[i].watch.len == 0 and (cat.sites[i].inputs.len == 0 or cat.sites[i].build.len == 0)) return error.NoWatchInputs; + s.watching = true; + s.queued = true; + } else if (std.mem.eql(u8, cmd, "watch off")) { + s.watching = false; + if (cat.sites[i].watch.len != 0) s.cancel = true; + } else if (std.mem.eql(u8, cmd, "cancel")) { + s.watching = false; + s.queued = false; + s.cancel = true; + } else return error.UnknownControl; + s.revision += 1; + cat.runner.wakeAll(); + } + + pub fn snapshot(cat: *Catalog, a: std.mem.Allocator) ![]const u8 { + cat.mutex.lockUncancelable(cat.io); + defer cat.mutex.unlock(cat.io); + const Row = struct { name: []const u8, title: []const u8, root: []const u8, port: u16, build: bool, watch: bool, status: State }; + const rows = try a.alloc(Row, cat.sites.len); + for (cat.sites, 0..) |s, i| rows[i] = .{ .name = s.name, .title = s.title, .root = s.root, .port = s.port, .build = s.build.len != 0, .watch = s.watch.len != 0 or s.inputs.len != 0, .status = cat.states[i] }; + const listeners = try std.json.parseFromSlice(std.json.Value, a, cat.listeners, .{}); + return std.json.Stringify.valueAlloc(a, .{ .sites = rows, .listeners = listeners.value, .discovery_error = cat.discovery_error }, .{}); + } + + fn discoverLoop(cat: *Catalog) void { + while (true) { + var arena = std.heap.ArenaAllocator.init(A); + defer arena.deinit(); + const a = arena.allocator(); + const pid = std.fmt.allocPrint(a, "{d}", .{std.os.linux.getpid()}) catch return; + var ports: std.Io.Writer.Allocating = .init(a); + ports.writer.print("{d}", .{cat.http_port}) catch return; + for (cat.sites) |site| ports.writer.print(",{d}", .{site.port}) catch return; + const result = std.process.run(A, cat.io, .{ .argv = &.{ "python3", "-c", @embedFile("discover.py"), pid, ports.written() }, .stdout_limit = .limited(2 * 1024 * 1024), .stderr_limit = .limited(8192), .timeout = .{ .duration = .{ .raw = .fromSeconds(15), .clock = .awake } } }) catch |err| { + if (err == error.Canceled) return; + cat.mutex.lockUncancelable(cat.io); + cat.discovery_error = @errorName(err); + cat.mutex.unlock(cat.io); + cat.io.sleep(.fromSeconds(5), .awake) catch return; + continue; + }; + defer A.free(result.stderr); + cat.mutex.lockUncancelable(cat.io); + if (result.term == .exited and result.term.exited == 0) { + if (cat.listeners.len > 2) A.free(cat.listeners); + cat.listeners = result.stdout; + cat.discovery_error = ""; + } else { + A.free(result.stdout); + cat.discovery_error = "listener inventory failed"; + } + cat.mutex.unlock(cat.io); + cat.io.sleep(.fromSeconds(5), .awake) catch return; + } + } + + fn fingerprint(cat: *Catalog, i: usize) u64 { + var hash = std.hash.Wyhash.init(0); + var arena = std.heap.ArenaAllocator.init(A); + defer arena.deinit(); + const a = arena.allocator(); + for (cat.sites[i].inputs) |input| { + const path = if (std.fs.path.isAbsolute(input)) input else std.fs.path.join(a, &.{ cat.sites[i].cwd, input }) catch continue; + const st = Io.Dir.cwd().statFile(cat.io, path, .{ .follow_symlinks = false }) catch continue; + hash.update(input); + if (st.kind != .directory) { + hash.update(std.mem.asBytes(&st.mtime)); + hash.update(std.mem.asBytes(&st.size)); + continue; + } + var dir = Io.Dir.cwd().openDir(cat.io, path, .{ .iterate = true, .follow_symlinks = false }) catch continue; + defer dir.close(cat.io); + var walker = dir.walkSelectively(a) catch continue; + defer walker.deinit(); + // XOR entry hashes so directory iteration ordering does not trigger builds. + var tree: u64 = 0; + while (walker.next(cat.io) catch null) |entry| { + if (entry.basename[0] == '.' or std.mem.eql(u8, entry.basename, "node_modules") or std.mem.eql(u8, entry.basename, "out") or std.mem.eql(u8, entry.basename, "build") or std.mem.eql(u8, entry.basename, "dist") or std.mem.eql(u8, entry.basename, "__pycache__")) continue; + if (entry.kind == .directory) { + walker.enter(cat.io, entry) catch {}; + continue; + } + if (entry.kind != .file) continue; + const es = entry.dir.statFile(cat.io, entry.basename, .{ .follow_symlinks = false }) catch continue; + var eh = std.hash.Wyhash.init(0); + eh.update(entry.path); + eh.update(std.mem.asBytes(&es.mtime)); + eh.update(std.mem.asBytes(&es.size)); + tree ^= eh.final(); + } + hash.update(std.mem.asBytes(&tree)); + } + return hash.final(); + } + + fn siteLoop(cat: *Catalog, i: usize) void { + while (true) { + cat.mutex.lockUncancelable(cat.io); + const scanning = cat.states[i].watching and cat.sites[i].watch.len == 0; + const previous = cat.states[i].fingerprint; + cat.mutex.unlock(cat.io); + const fp = if (scanning) cat.fingerprint(i) else previous; + cat.mutex.lockUncancelable(cat.io); + const s = &cat.states[i]; + if (s.watching and cat.sites[i].watch.len == 0 and fp != s.fingerprint) s.queued = true; + s.fingerprint = fp; + const run = s.queued; + const native = s.watching and cat.sites[i].watch.len != 0; + if (run) { + s.queued = false; + s.cancel = false; + s.phase = if (native) "watching" else "building"; + s.started = cat.now(); + s.exit_code = null; + s.revision += 1; + cat.runner.wakeAll(); + } + cat.mutex.unlock(cat.io); + if (run) cat.runBuild(i, native) catch |err| { + if (err == error.Canceled) return; + cat.mutex.lockUncancelable(cat.io); + cat.states[i].phase = @errorName(err); + cat.states[i].pid = null; + cat.states[i].finished = cat.now(); + cat.states[i].revision += 1; + cat.runner.wakeAll(); + cat.mutex.unlock(cat.io); + }; + cat.io.sleep(.fromSeconds(1), .awake) catch return; + } + } + + const Job = struct { + child: std.process.Child, + done: std.atomic.Value(bool) = .init(false), + term: ?std.process.Child.Term = null, + fn wait(job: *Job, io: Io) void { + job.term = job.child.wait(io) catch null; + job.done.store(true, .release); + } + }; + fn runBuild(cat: *Catalog, i: usize, native: bool) !void { + const path = try cat.logPath(A, i); + defer A.free(path); + const log = try Io.Dir.cwd().createFile(cat.io, path, .{}); + defer log.close(cat.io); + const cmd = if (native) cat.sites[i].watch else cat.sites[i].build; + const argv = try A.alloc([]const u8, cmd.len + 1); + defer A.free(argv); + argv[0] = "setsid"; + @memcpy(argv[1..], cmd); + var job: Job = .{ .child = try std.process.spawn(cat.io, .{ .argv = argv, .cwd = .{ .path = cat.sites[i].cwd }, .stdin = .ignore, .stdout = .{ .file = log }, .stderr = .{ .file = log } }) }; + const pid = job.child.id.?; + var group: Io.Group = .init; + // Cancel whole session, not just a shell, including on server shutdown. + defer { + _ = std.os.linux.kill(-pid, std.os.linux.SIG.KILL); + group.cancel(cat.io); + job.child.kill(cat.io); + } + cat.mutex.lockUncancelable(cat.io); + cat.states[i].pid = pid; + cat.mutex.unlock(cat.io); + try group.concurrent(cat.io, Job.wait, .{ &job, cat.io }); + var canceled = false; + while (!job.done.load(.acquire)) { + cat.mutex.lockUncancelable(cat.io); + const cancel = cat.states[i].cancel; + cat.mutex.unlock(cat.io); + if (cancel) { + canceled = true; + _ = std.os.linux.kill(-pid, std.os.linux.SIG.KILL); + } + try cat.io.sleep(.fromMilliseconds(100), .awake); + } + cat.mutex.lockUncancelable(cat.io); + defer cat.mutex.unlock(cat.io); + const s = &cat.states[i]; + const code: u8 = if (job.term) |term| (if (term == .exited) term.exited else 128) else 255; + s.pid = null; + s.exit_code = code; + s.finished = cat.now(); + s.phase = if (canceled) "canceled" else if (code == 0) "idle" else "failed"; + if (code == 0 and !canceled) s.generation += 1; + if (native) s.watching = false; + s.revision += 1; + cat.runner.wakeAll(); + } + + fn intern(cat: *Catalog, path: []const u8) !u64 { + for (cat.nodes.items, 0..) |n, i| if (std.mem.eql(u8, n, path)) return i + 1; + if (cat.nodes.items.len >= 65536) return error.TooManyPaths; + try cat.nodes.append(A, try A.dupe(u8, path)); + return cat.nodes.items.len; + } + fn sitePath(cat: *Catalog, path: []const u8) ?struct { i: usize, rest: []const u8 } { + if (!std.mem.startsWith(u8, path, "sites/")) return null; + const rest = path[6..]; + const n = std.mem.indexOfScalar(u8, rest, '/') orelse rest.len; + for (cat.sites, 0..) |s, i| if (std.mem.eql(u8, rest[0..n], s.name)) return .{ .i = i, .rest = if (n == rest.len) "" else rest[n + 1 ..] }; + return null; + } + fn attr(cat: *Catalog, a: std.mem.Allocator, path: []const u8) !fs.Attr { + var result = try cat.attrData(a, path); + result.node = try cat.intern(path); + return result; + } + fn attrData(cat: *Catalog, a: std.mem.Allocator, path: []const u8) !fs.Attr { + var result: fs.Attr = .{ .name = std.fs.path.basename(path), .mode = 0o444 }; + if (path.len == 0 or std.mem.eql(u8, path, "sites") or std.mem.eql(u8, path, "services") or std.mem.eql(u8, path, "listeners")) { + result.dir = true; + result.mode = 0o555; + return result; + } + if (cat.sitePath(path)) |p| { + if (p.rest.len == 0 or std.mem.eql(u8, p.rest, "public")) { + result.dir = true; + result.mode = 0o555; + return result; + } + if (std.mem.startsWith(u8, p.rest, "public/")) { + const file = try openBeneath(cat.io, cat.sites[p.i].root, p.rest[7..]); + defer file.close(cat.io); + const st = try file.stat(cat.io); + result.dir = st.kind == .directory; + result.mode = if (result.dir) 0o555 else 0o444; + result.size = st.size; + result.mtime = @intCast(@max(0, st.mtime.toSeconds())); + return result; + } + if (std.mem.eql(u8, p.rest, "ctl")) { + result.mode = 0o666; + return result; + } + } + const data = try cat.content(a, path); + result.size = data.len; + return result; + } + fn content(cat: *Catalog, a: std.mem.Allocator, path: []const u8) ![]const u8 { + if (std.mem.eql(u8, path, "index.html")) return @embedFile("static/catalog.html"); + if (std.mem.eql(u8, path, "listeners/index.json")) return cat.listeners; + if (std.mem.eql(u8, path, "services/index.json")) { + const parsed = try std.json.parseFromSlice([]std.json.Value, a, cat.listeners, .{}); + var known: std.ArrayList(std.json.Value) = .empty; + for (parsed.value) |v| if (!std.mem.eql(u8, v.object.get("protocol").?.string, "unknown")) { + try known.append(a, v); + }; + return std.json.Stringify.valueAlloc(a, known.items, .{}); + } + if (cat.sitePath(path)) |p| { + if (std.mem.eql(u8, p.rest, "status") or std.mem.eql(u8, p.rest, "events")) return std.json.Stringify.valueAlloc(a, cat.states[p.i], .{}); + if (std.mem.eql(u8, p.rest, "ctl")) return "build\nwatch on\nwatch off\ncancel\n"; + if (std.mem.eql(u8, p.rest, "log")) { + const f = Io.Dir.cwd().openFile(cat.io, try cat.logPath(a, p.i), .{}) catch return ""; + defer f.close(cat.io); + const st = try f.stat(cat.io); + const n = @min(st.size, 256 * 1024); + const b = try a.alloc(u8, @intCast(n)); + return b[0..try f.readPositionalAll(cat.io, b, st.size - n)]; + } + } + return error.FileNotFound; + } + fn children(cat: *Catalog, a: std.mem.Allocator, path: []const u8) ![][]const u8 { + var names: std.ArrayList([]const u8) = .empty; + if (path.len == 0) try names.appendSlice(a, &.{ "index.html", "sites", "services", "listeners" }) else if (std.mem.eql(u8, path, "sites")) { + for (cat.sites) |s| try names.append(a, s.name); + } else if (std.mem.eql(u8, path, "services") or std.mem.eql(u8, path, "listeners")) try names.append(a, "index.json") else if (cat.sitePath(path)) |p| { + if (p.rest.len == 0) try names.appendSlice(a, &.{ "public", "ctl", "status", "log", "events" }) else if (std.mem.eql(u8, p.rest, "public") or std.mem.startsWith(u8, p.rest, "public/")) { + const rel = if (p.rest.len == 6) "" else p.rest[7..]; + const f = try openBeneath(cat.io, cat.sites[p.i].root, rel); + defer f.close(cat.io); + var dir: Io.Dir = .{ .handle = f.handle }; + var it = dir.iterate(); + while (try it.next(cat.io)) |e| if (e.kind == .file or e.kind == .directory) { + try names.append(a, try a.dupe(u8, e.name)); + }; + } else return error.NotDir; + } else return error.NotDir; + std.mem.sort([]const u8, names.items, {}, struct { + fn less(_: void, x: []const u8, y: []const u8) bool { + return std.mem.lessThan(u8, x, y); + } + }.less); + return names.items; + } + fn serve(ctx: ?*anyopaque, conn: *Runner.Conn, req: fs.Req) void { + const cat: *Catalog = @ptrCast(@alignCast(ctx.?)); + var arena = std.heap.ArenaAllocator.init(A); + defer arena.deinit(); + // Controls take their own lock; all other node operations are serialized. + if (req.op == .write) { + cat.mutex.lockUncancelable(cat.io); + const path = if (req.node > 0 and req.node <= cat.nodes.items.len) cat.nodes.items[@intCast(req.node - 1)] else ""; + const p = cat.sitePath(path); + cat.mutex.unlock(cat.io); + if (p) |s| { + if (std.mem.eql(u8, s.rest, "ctl") and req.off == 0 and req.data.len <= 128) { + cat.control(s.i, req.data) catch |err| { + conn.reply(&.{ .tag = req.tag, .status = .err, .ename = @errorName(err) }, ""); + return; + }; + conn.reply(&.{ .tag = req.tag, .written = @intCast(req.data.len) }, ""); + return; + } + } + conn.reply(&fs.Reply.fail(req.tag, fs.E.PERM), ""); + return; + } + cat.mutex.lockUncancelable(cat.io); + defer cat.mutex.unlock(cat.io); + cat.answer(arena.allocator(), conn, req) catch |err| conn.reply(&.{ .tag = req.tag, .status = .err, .ename = if (err == error.FileNotFound) "not found" else @errorName(err) }, ""); + } + fn answer(cat: *Catalog, a: std.mem.Allocator, conn: *Runner.Conn, req: fs.Req) !void { + if (req.node == 0 or req.node > cat.nodes.items.len) return error.FileNotFound; + const path = cat.nodes.items[@intCast(req.node - 1)]; + var reply: fs.Reply = .{ .tag = req.tag }; + var bytes: []const u8 = ""; + switch (req.op) { + .lookup => { + const next = if (std.mem.eql(u8, req.data, ".")) path else if (std.mem.eql(u8, req.data, "..")) (std.fs.path.dirname(path) orelse "") else try std.fs.path.join(a, &.{ path, req.data }); + reply.attr = try cat.attr(a, next); + }, + .getattr => reply.attr = try cat.attr(a, path), + .setattr => { + if (!std.mem.endsWith(u8, path, "/ctl")) return error.PermissionDenied; + reply.attr = try cat.attr(a, path); + }, + .open => { + if ((req.omode & 3) != 0 and !std.mem.endsWith(u8, path, "/ctl")) return error.PermissionDenied; + if (std.mem.endsWith(u8, path, "/events")) { + var found = false; + for (&cat.cursors[conn.index], 0..) |*cursor, ci| if (!cursor.used) { + cursor.* = .{ .used = true }; + reply.handle = @intCast(ci + 1); + found = true; + break; + }; + if (!found) return error.TooManyEventReaders; + } + }, + .release => { + if (req.opened and req.handle != 0) cat.cursors[conn.index][req.handle - 1].used = false; + }, + .write => unreachable, + .read => { + if (cat.sitePath(path)) |p| { + if (std.mem.eql(u8, p.rest, "events") and req.handle != 0) { + const cursor = &cat.cursors[conn.index][req.handle - 1]; + if (cursor.pos == cursor.len) { + if (cursor.revision == cat.states[p.i].revision) { + conn.reply(&.{ .tag = req.tag, .status = .again }, ""); + return; + } + const json = try std.json.Stringify.valueAlloc(a, cat.states[p.i], .{}); + if (json.len + 1 > cursor.bytes.len) return error.EventTooLarge; + @memcpy(cursor.bytes[0..json.len], json); + cursor.bytes[json.len] = '\n'; + cursor.len = json.len + 1; + cursor.pos = 0; + cursor.revision = cat.states[p.i].revision; + } + const end = @min(cursor.len, cursor.pos + req.size); + bytes = cursor.bytes[cursor.pos..end]; + cursor.pos = end; + conn.reply(&reply, bytes); + return; + } + if (std.mem.startsWith(u8, p.rest, "public/")) { + const f = try openBeneath(cat.io, cat.sites[p.i].root, p.rest[7..]); + defer f.close(cat.io); + const b = try a.alloc(u8, req.size); + bytes = b[0..try f.readPositionalAll(cat.io, b, req.off)]; + conn.reply(&reply, bytes); + return; + } + } + const all = try cat.content(a, path); + const from = @min(req.off, all.len); + bytes = all[@intCast(from)..@intCast(@min(all.len, from + req.size))]; + }, + .readdir => { + var all: std.ArrayList(u8) = .empty; + const names = try cat.children(a, path); + // fs.Req.off is an ENTRY index, not the wire byte offset. + // The engine consumes compact node/dir/name records and owns + // Stat encoding plus the client's byte-offset cursor. + for (names[@intCast(@min(req.off, names.len))..]) |name| { + if (name.len > 255) continue; + const child_path = try std.fs.path.join(a, &.{ path, name }); + const at = cat.attr(a, child_path) catch continue; + var header: [10]u8 = undefined; + std.mem.writeInt(u64, header[0..8], at.node, .little); + header[8] = @intFromBool(at.dir); + header[9] = @intCast(name.len); + try all.appendSlice(a, &header); + try all.appendSlice(a, name); + if (all.items.len >= req.size) break; + } + bytes = all.items; + }, + } + conn.reply(&reply, bytes); + } +}; + +pub fn safeRelative(path: []const u8) bool { + if (std.fs.path.isAbsolute(path) or std.mem.indexOfScalar(u8, path, 0) != null or std.mem.indexOfScalar(u8, path, '\\') != null) return false; + var parts = std.mem.splitScalar(u8, path, '/'); + while (parts.next()) |p| if (std.mem.eql(u8, p, "..") or std.mem.eql(u8, p, ".")) return false; + return true; +} + +/// Descriptor-relative traversal refuses symlinks in every component, including +/// the leaf. This also prevents TOCTOU escapes when a build replaces a directory. +pub fn openBeneath(io: Io, root: []const u8, relative: []const u8) !Io.File { + if (!safeRelative(relative)) return error.AccessDenied; + var dir = try Io.Dir.cwd().openDir(io, root, .{}); + defer dir.close(io); + var parts = std.mem.tokenizeScalar(u8, relative, '/'); + var part = parts.next(); + while (part) |p| { + part = parts.next(); + if (part == null) return dir.openFile(io, p, .{ .follow_symlinks = false }); + const next = try dir.openDir(io, p, .{ .follow_symlinks = false }); + dir.close(io); + dir = next; + } + return dir.openFile(io, ".", .{ .follow_symlinks = false }); +} + +pub fn mime(path: []const u8) []const u8 { + const ext = std.fs.path.extension(path); + const types = .{ .{ ".html", "text/html; charset=utf-8" }, .{ ".css", "text/css" }, .{ ".js", "text/javascript" }, .{ ".mjs", "text/javascript" }, .{ ".json", "application/json" }, .{ ".svg", "image/svg+xml" }, .{ ".png", "image/png" }, .{ ".jpg", "image/jpeg" }, .{ ".jpeg", "image/jpeg" }, .{ ".gif", "image/gif" }, .{ ".webp", "image/webp" }, .{ ".ico", "image/x-icon" }, .{ ".woff2", "font/woff2" }, .{ ".woff", "font/woff" }, .{ ".pdf", "application/pdf" }, .{ ".wasm", "application/wasm" }, .{ ".txt", "text/plain; charset=utf-8" } }; + inline for (types) |pair| if (std.ascii.eqlIgnoreCase(ext, pair[0])) return pair[1]; + return "application/octet-stream"; +} diff --git a/web/discover.py b/web/discover.py new file mode 100644 index 0000000..8c750c1 --- /dev/null +++ b/web/discover.py @@ -0,0 +1,93 @@ +"""One-shot Linux listener inventory for 9web. Never connects to a listener. + +Only socket-inode ownership establishes a process/listener relationship. Parent +commands can identify a launcher, but never turn an arbitrary Node socket into +HTTP. Output deliberately omits full argv/environment (which may hold secrets). +""" +import ipaddress +import json +import os +import socket +import sys + + +def recognize(argv): + if not argv: + return None + exe = os.path.basename(argv[0]) + args = argv[1:] + if exe in ('miniserve', '9web'): + return exe, 'https' if '--tls-cert' in args else 'http', 'recognized executable' + if exe.startswith('python') and any(args[i:i+2] == ['-m', 'http.server'] for i in range(len(args))): + return 'python http.server', 'http', 'python -m http.server' + if exe == 'typst' and 'watch' in args: + return 'typst watch', 'http', 'typst watch' + # Require a CLI entrypoint, not a substring anywhere in arbitrary arguments. + if exe in ('node', 'nodejs', 'bun') and args: + entry = args[0].replace('\\', '/') + if entry.endswith(('/vite/bin/vite.js', '/vite/bin/vite.mjs')): + return 'vite', 'https' if '--https' in args else 'http', 'Vite CLI entrypoint' + if '/wrangler/' in entry and 'dev' in args: + return 'wrangler dev', 'http', 'Wrangler dev entrypoint' + return None + + +def inventory(web_pid=None, web_ports=()): + owners = {} + for name in os.listdir('/proc'): + if not name.isdecimal(): + continue + base = '/proc/' + name + try: + argv = open(base + '/cmdline', 'rb').read(65536).decode(errors='replace').rstrip('\0').split('\0') + exe = os.path.basename(os.readlink(base + '/exe')) + cwd = os.readlink(base + '/cwd') + fds = os.listdir(base + '/fd') + except (OSError, ValueError): + continue + recognized = recognize([exe] + argv[1:]) + # 9web has both HTTP and 9P sockets. Its own configured listeners are + # supplied separately by the catalogue, never inferred from its name. + if exe == '9web': + recognized = None + for fd in fds: + try: + link = os.readlink(base + '/fd/' + fd) + except OSError: + continue + if link.startswith('socket:['): + owners.setdefault(link[8:-1], []).append(dict( + pid=int(name), process=exe, cwd=cwd, recognized=recognized)) + rows = [] + for table, family in [('tcp', socket.AF_INET), ('tcp6', socket.AF_INET6)]: + try: + lines = open('/proc/net/' + table).readlines()[1:] + except OSError: + continue + for line in lines: + fields = line.split() + if fields[3] != '0A': + continue + raw, port = fields[1].split(':') + packed = b''.join(int(raw[i:i+8], 16).to_bytes(4, sys.byteorder) for i in range(0, len(raw), 8)) + address = socket.inet_ntop(family, packed) + inode = fields[9] + processes = {p['pid']: p for p in owners.get(inode, [])} + recognized = [p for p in processes.values() if p['recognized']] + p = recognized[0] if recognized else next(iter(processes.values()), None) + rec = p['recognized'] if p else None + if web_pid in processes and int(port, 16) in web_ports: + p = processes[web_pid] + rec = ('9web', 'http', '9web registered HTTP listener') + rows.append(dict(id=table + '-' + inode, address=address, port=int(port, 16), + protocol=rec[1] if rec else 'unknown', service=rec[0] if rec else None, + pid=p['pid'] if p else None, process=p['process'] if p else None, + cwd=p['cwd'] if p else None, + local=ipaddress.ip_address(address).is_loopback, + evidence=('socket inode ownership; ' + rec[2]) if rec else + ('socket inode ownership; unrecognized protocol' if p else 'process ownership unavailable'))) + return sorted(rows, key=lambda r: (r['port'], r['address'])) + + +if __name__ == '__main__': + print(json.dumps(inventory(int(sys.argv[1]), [int(p) for p in sys.argv[2].split(',')]) if len(sys.argv) == 3 else inventory())) diff --git a/web/main.zig b/web/main.zig index 130ed4b..8c3d6ad 100644 --- a/web/main.zig +++ b/web/main.zig @@ -11,6 +11,7 @@ const c9 = @import("cloud9"); const mux = @import("mux.zig"); const httpfs = @import("httpfs.zig"); const probe = @import("probe.zig"); +const catalog = @import("catalog.zig"); const Io = std.Io; const max_frame = 65536; @@ -29,6 +30,7 @@ const Config = struct { public_host: []const u8, timeout_ms: u32, browser_config: []const u8, + wildcard: bool = false, }; /// One downstream slot: buffers, the HTTP/WebSocket streams and the mux @@ -48,6 +50,7 @@ const Session = struct { conn: MuxT.Conn = undefined, wmutex: Io.Mutex = .init, is_ws: bool = false, + site: ?usize = null, fn sink(s: *Session) mux.Sink { return .{ .ctx = s, .send = sendReply }; @@ -70,6 +73,7 @@ pub const App = struct { io: Io, m: *MuxT, config: Config, + catalog: ?*catalog.Catalog = null, sessions: [max_connections]Session = @splat(.{}), active: std.atomic.Value(u32) = .init(0), @@ -120,6 +124,10 @@ pub fn main(init: std.process.Init) !void { var user: []const u8 = "user"; var tree: []const u8 = ""; var public_origin: ?[]const u8 = null; + var catalog_file: ?[]const u8 = null; + var catalog_socket: ?[]const u8 = null; + var state_dir: ?[]const u8 = null; + var explicit_upstream = false; var i: usize = 1; while (i < args.len) : (i += 1) { if (std.mem.eql(u8, args[i], "--help")) { @@ -127,6 +135,8 @@ pub fn main(init: std.process.Init) !void { \\Usage: 9web [--listen IP:PORT] [--upstream tcp:IP:PORT|unix:PATH|file:PATH] \\ [--serve tcp:IP:PORT|unix:PATH] [--probe unix:PATH|tcp:IP:PORT] \\ [--origin https://HOST:PORT] [--timeout-ms 300000] [--user NAME] [--tree NAME] + \\ [--catalog FILE --catalog-socket PATH --state-dir PATH] + \\Catalogue mode serves configured sites, build controls and passive listener discovery. \\One shared upstream, many downstreams. Default: http://127.0.0.1:8080 -> tcp:127.0.0.1:564 \\ , .{}); @@ -136,11 +146,36 @@ pub fn main(init: std.process.Init) !void { i += 1; const v = args[i]; const k = args[i - 1]; + if (std.mem.eql(u8, k, "--catalog")) { + catalog_file = v; + continue; + } + if (std.mem.eql(u8, k, "--catalog-socket")) { + catalog_socket = v; + continue; + } + if (std.mem.eql(u8, k, "--state-dir")) { + state_dir = v; + continue; + } + if (std.mem.eql(u8, k, "--upstream")) explicit_upstream = true; if (std.mem.eql(u8, k, "--listen")) listen_text = v else if (std.mem.eql(u8, k, "--upstream")) upstream_text = v else if (std.mem.eql(u8, k, "--serve")) serve_text = v else if (std.mem.eql(u8, k, "--probe")) probe_text = v else if (std.mem.eql(u8, k, "--origin")) public_origin = v else if (std.mem.eql(u8, k, "--timeout-ms")) timeout_ms = try std.fmt.parseInt(u32, v, 10) else if (std.mem.eql(u8, k, "--user")) user = v else if (std.mem.eql(u8, k, "--tree")) tree = v else return error.UnknownArgument; } if (user.len > 256 or tree.len > 256) return error.AttachNameTooLong; const io = init.io; + var cat: ?*catalog.Catalog = null; + if (catalog_file) |file| { + if (@import("builtin").os.tag == .linux) { + if (explicit_upstream) return error.CatalogAndUpstreamAreExclusive; + const socket = catalog_socket orelse return error.MissingCatalogSocket; + cat = try catalog.Catalog.init(io, file, state_dir orelse return error.MissingStateDir); + cat.?.http_port = (try Io.net.IpAddress.parseLiteral(listen_text)).getPort(); + try cat.?.start(try allocator.dupeSentinel(u8, socket, 0)); + upstream_text = try std.fmt.allocPrint(allocator, "unix:{s}", .{socket}); + } else return error.CatalogRequiresLinux; + } + defer if (cat) |c| c.stop(); const address = try Io.net.IpAddress.parseLiteral(listen_text); const upstream = try parseUpstream(allocator, upstream_text); @@ -167,12 +202,13 @@ pub fn main(init: std.process.Init) !void { origin_uri.path.percent_encoded.len != 0 or origin_uri.query != null or origin_uri.fragment != null) return error.InvalidOrigin; const browser_config = try std.json.Stringify.valueAlloc(allocator, .{ .user = user, .tree = tree }, .{}); - var app: App = .{ .io = io, .m = &m, .config = .{ + var app: App = .{ .io = io, .m = &m, .catalog = cat, .config = .{ .host = host, .origin = origin_text, .public_host = origin_text[origin_uri.scheme.len + 3 ..], .timeout_ms = timeout_ms, .browser_config = browser_config, + .wildcard = std.mem.startsWith(u8, listen_text, "0.0.0.0:") or std.mem.startsWith(u8, listen_text, "[::]:"), } }; std.debug.print("9web {s} -> {s}\n", .{ app.config.origin, upstream_text }); @@ -181,6 +217,19 @@ pub fn main(init: std.process.Init) !void { defer group.cancel(io); try group.concurrent(io, MuxT.readerLoop, .{&m}); + // Each site gets its own HTTP root, preserving /assets and /blog links. + // These listeners belong to this same 9web process, not preview daemons. + if (cat) |c| { + for (c.sites, 0..) |site, si| { + const l = try allocator.create(Io.net.Server); + var bind_address = address; + bind_address.setPort(site.port); + l.* = try c9.transport.listen(io, .{ .tcp = bind_address }, max_connections); + try group.concurrent(io, siteAcceptLoop, .{ &app, l, &group, si }); + std.debug.print("9web: site {s} on {f} from {s}\n", .{ site.name, l.socket.address, site.root }); + } + } + // Optional 9P listener for plan9port / 9ns clients (a mux like 9pserve). var serve_listener: ?Io.net.Server = null; if (serve_text) |t| { @@ -216,6 +265,7 @@ pub fn main(init: std.process.Init) !void { continue; }; s.app = &app; + s.site = null; s.stream = stream; group.concurrent(io, handleHttp, .{s}) catch { app.giveBack(s); @@ -225,6 +275,24 @@ pub fn main(init: std.process.Init) !void { } } +fn siteAcceptLoop(app: *App, listener: *Io.net.Server, group: *Io.Group, si: usize) void { + defer listener.deinit(app.io); + while (true) { + const stream = listener.accept(app.io) catch return; + const s = app.take() orelse { + stream.close(app.io); + continue; + }; + s.app = app; + s.stream = stream; + s.site = si; + group.concurrent(app.io, handleHttp, .{s}) catch { + app.giveBack(s); + stream.close(app.io); + }; + } +} + /// Accepts plain 9P clients on the `--serve` listener; each runs a raw mux /// session for its lifetime. fn serveLoop(app: *App, listener: *Io.net.Server, group: *Io.Group) void { @@ -318,15 +386,48 @@ fn serveHttp(s: *Session) !void { if (std.ascii.eqlIgnoreCase(header.name, "origin")) origin = header.value; if (std.ascii.eqlIgnoreCase(header.name, "sec-websocket-version")) version = header.value; } - if (!std.mem.eql(u8, host orelse "", config.host) and !std.mem.eql(u8, host orelse "", config.public_host)) + if (s.site) |si| return serveSite(s, &request, app.catalog.?.sites[si]); + const host_text = host orelse ""; + const literal = Io.net.IpAddress.parseLiteral(host_text) catch null; + const bound = try Io.net.IpAddress.parseLiteral(config.host); + const wildcard_host = config.wildcard and ((if (literal) |ip| ip.getPort() == bound.getPort() else false) or std.mem.eql(u8, host_text, try std.fmt.bufPrint(&s.body, "localhost:{d}", .{bound.getPort()}))); + if (!wildcard_host and !std.mem.eql(u8, host_text, config.host) and !std.mem.eql(u8, host_text, config.public_host)) return respond(&request, "Unexpected Host\n", "text/plain", .forbidden); const path = std.mem.sliceTo(request.head.target, '?'); + // Browsers must originate control writes from this dashboard. Site ports + // are separate origins. CLI and 9P clients do not send Origin. + const same_origin = if (origin) |o| std.mem.eql(u8, o, config.origin) or + (std.mem.startsWith(u8, o, "http://") and std.mem.eql(u8, o[7..], host_text)) else false; + if (app.catalog) |c| { + if (request.head.method != .GET and request.head.method != .HEAD and origin != null and !same_origin) + return respond(&request, "Unexpected Origin\n", "text/plain", .forbidden); + if (std.mem.eql(u8, path, "/")) return respond(&request, @embedFile("static/catalog.html"), "text/html; charset=utf-8", .ok); + if (std.mem.eql(u8, path, "/_cloud9/catalog.js")) return respond(&request, @embedFile("static/catalog.js"), "text/javascript", .ok); + if (std.mem.eql(u8, path, "/_cloud9/catalog.json")) { + var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); + defer arena.deinit(); + return respond(&request, try c.snapshot(arena.allocator()), "application/json", .ok); + } + if (std.mem.startsWith(u8, path, "/_cloud9/sites/") and std.mem.endsWith(u8, path, "/ctl")) { + if (request.head.method != .POST) return respond(&request, "Use POST\n", "text/plain", .method_not_allowed); + const name = path[15 .. path.len - 4]; + for (c.sites, 0..) |site, si| if (std.mem.eql(u8, name, site.name)) { + var body_buf: [256]u8 = undefined; + const reader = try request.readerExpectContinue(&body_buf); + const command = reader.allocRemaining(std.heap.page_allocator, .limited(128)) catch return respond(&request, "Command too long\n", "text/plain", .bad_request); + defer std.heap.page_allocator.free(command); + c.control(si, command) catch |err| return respond(&request, @errorName(err), "text/plain", .bad_request); + return respond(&request, "queued\n", "text/plain", .accepted); + }; + return respond(&request, "Unknown site\n", "text/plain", .not_found); + } + } // The WebSocket endpoint. if (std.mem.eql(u8, path, "/_cloud9/9p")) { if (request.head.method != .GET) return respond(&request, "Use GET\n", "text/plain", .method_not_allowed); - if (!std.mem.eql(u8, origin orelse "", config.origin)) return respond(&request, "Unexpected Origin\n", "text/plain", .forbidden); + if (!same_origin) return respond(&request, "Unexpected Origin\n", "text/plain", .forbidden); if (!std.mem.eql(u8, version orelse "", "13")) return respond(&request, "WebSocket version 13 required\n", "text/plain", .bad_request); return wsSession(s, &request); } @@ -375,3 +476,92 @@ fn wsSession(s: *Session, request: *std.http.Server.Request) !void { } } } + +fn serveSite(s: *Session, request: *std.http.Server.Request, site: catalog.Site) !void { + if (request.head.method != .GET and request.head.method != .HEAD) return respond(request, "Use GET or HEAD\n", "text/plain", .method_not_allowed); + const raw = std.mem.sliceTo(request.head.target, '?'); + if (raw.len == 0 or raw[0] != '/') return respond(request, "Bad path\n", "text/plain", .bad_request); + var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); + defer arena.deinit(); + const a = arena.allocator(); + const decoded = try a.alloc(u8, raw.len); + var n: usize = 0; + var i: usize = 1; + while (i < raw.len) : (i += 1) { + var ch = raw[i]; + if (ch == '%') { + if (i + 2 >= raw.len) return respond(request, "Bad escape\n", "text/plain", .bad_request); + ch = std.fmt.parseInt(u8, raw[i + 1 .. i + 3], 16) catch return respond(request, "Bad escape\n", "text/plain", .bad_request); + if (ch == '/' or ch == '\\') return respond(request, "Bad escape\n", "text/plain", .bad_request); + i += 2; + } + decoded[n] = ch; + n += 1; + } + var rel: []const u8 = decoded[0..n]; + if (!catalog.safeRelative(rel)) return respond(request, "Forbidden path\n", "text/plain", .forbidden); + // Dotfiles and builder/provider configuration are not public documents. + var components = std.mem.tokenizeScalar(u8, rel, '/'); + while (components.next()) |component| if (component[0] == '.' or std.mem.eql(u8, component, "_headers") or std.mem.eql(u8, component, "_redirects")) return respond(request, "Not found\n", "text/plain", .not_found); + var file = catalog.openBeneath(s.app.io, site.root, rel) catch blk: { + // Pretty URLs such as /blog/post resolve to /blog/post.html. + if (std.fs.path.extension(rel).len != 0) return respond(request, "Not found\n", "text/plain", .not_found); + rel = try std.fmt.allocPrint(a, "{s}.html", .{rel}); + break :blk catalog.openBeneath(s.app.io, site.root, rel) catch return respond(request, "Not found\n", "text/plain", .not_found); + }; + var st = try file.stat(s.app.io); + if (st.kind == .directory) { + file.close(s.app.io); + if (!std.mem.endsWith(u8, raw, "/")) { + const location = try std.fmt.allocPrint(a, "{s}/", .{raw}); + return request.respond("", .{ .status = .moved_permanently, .keep_alive = false, .extra_headers = &.{.{ .name = "location", .value = location }} }); + } + rel = try std.fs.path.join(a, &.{ rel, "index.html" }); + file = catalog.openBeneath(s.app.io, site.root, rel) catch return respond(request, "Site has not been built, or index.html is missing.\n", "text/plain", .not_found); + st = try file.stat(s.app.io); + } + defer file.close(s.app.io); + if (st.kind != .file) return respond(request, "Not a regular file\n", "text/plain", .forbidden); + var start: u64 = 0; + var end = st.size; + var partial = false; + var headers = request.iterateHeaders(); + while (headers.next()) |h| if (std.ascii.eqlIgnoreCase(h.name, "range")) { + const text = h.value; + if (!std.mem.startsWith(u8, text, "bytes=") or std.mem.indexOfScalar(u8, text, ',') != null) return respond(request, "Unsupported range\n", "text/plain", .range_not_satisfiable); + const dash = std.mem.indexOfScalar(u8, text[6..], '-') orelse return respond(request, "Bad range\n", "text/plain", .range_not_satisfiable); + const left = text[6 .. 6 + dash]; + const right = text[7 + dash ..]; + if (left.len == 0) { + const suffix = std.fmt.parseInt(u64, right, 10) catch return respond(request, "Bad range\n", "text/plain", .range_not_satisfiable); + start = st.size - @min(st.size, suffix); + } else { + start = std.fmt.parseInt(u64, left, 10) catch return respond(request, "Bad range\n", "text/plain", .range_not_satisfiable); + if (right.len != 0) { + const last = std.fmt.parseInt(u64, right, 10) catch return respond(request, "Bad range\n", "text/plain", .range_not_satisfiable); + end = @min(st.size, last +| 1); + } + } + if (start >= end) return respond(request, "Unsatisfiable range\n", "text/plain", .range_not_satisfiable); + partial = true; + }; + var extra: std.ArrayList(std.http.Header) = .empty; + try extra.appendSlice(a, &.{ .{ .name = "content-type", .value = catalog.mime(rel) }, .{ .name = "cache-control", .value = "no-cache" }, .{ .name = "x-content-type-options", .value = "nosniff" }, .{ .name = "accept-ranges", .value = "bytes" } }); + if (partial) try extra.append(a, .{ .name = "content-range", .value = try std.fmt.allocPrint(a, "bytes {d}-{d}/{d}", .{ start, end - 1, st.size }) }); + if (request.head.method == .HEAD) { + try extra.append(a, .{ .name = "content-length", .value = try std.fmt.allocPrint(a, "{d}", .{end - start}) }); + return request.respond("", .{ .status = if (partial) .partial_content else .ok, .keep_alive = false, .transfer_encoding = .none, .extra_headers = extra.items }); + } + var bw = try request.respondStreaming(&s.body, .{ .content_length = end - start, .respond_options = .{ .status = if (partial) .partial_content else .ok, .keep_alive = false, .extra_headers = extra.items } }); + if (request.head.method != .HEAD) { + var offset = start; + while (offset < end) { + const count: usize = @intCast(@min(end - offset, s.frame.len)); + const got = try file.readPositionalAll(s.app.io, s.frame[0..count], offset); + if (got == 0) return error.EndOfStream; + try bw.writer.writeAll(s.frame[0..got]); + offset += got; + } + } + try bw.end(); +} diff --git a/web/static/catalog.html b/web/static/catalog.html new file mode 100644 index 0000000..2160612 --- /dev/null +++ b/web/static/catalog.html @@ -0,0 +1,14 @@ + + +9web · local sites + +

9web

Local sites, builds, and listening services.

+

+

HTTP services

9P tree
+

Identified from socket ownership and process arguments. No endpoint probing. Loopback links work only on this computer.

+
ServiceAddressProcessEvidence
+
Other listeners
+

+ diff --git a/web/static/catalog.js b/web/static/catalog.js new file mode 100644 index 0000000..f868a16 --- /dev/null +++ b/web/static/catalog.js @@ -0,0 +1,56 @@ +const $ = id => document.getElementById(id); +let activeLog = null; +const element = (tag, text) => { const e = document.createElement(tag); e.textContent = text; return e; }; +function url(address, port, protocol = 'http') { + let host = address; + if (!host || host === '0.0.0.0' || host === '::') host = location.hostname; + host = host.replace(/^\[|\]$/g, ''); + return `${protocol}://${host.includes(':') ? `[${host}]` : host}:${port}/`; +} +async function control(name, command) { + try { + const r = await fetch(`/_cloud9/sites/${encodeURIComponent(name)}/ctl`, { method:'POST', headers:{'Content-Type':'text/plain'}, body:command }); + if (!r.ok) throw new Error(await r.text()); + $('message').textContent = `${name}: ${command} requested`; + await refresh(); + } catch (e) { $('message').textContent = e.message; } +} +function button(text, action, disabled = false) { const b = element('button', text); b.disabled = disabled; b.onclick = action; return b; } +function listenerRow(item) { + const tr = element('tr', ''); + const name = element('td', ''); + if (item.protocol !== 'unknown') { + const a = element('a', item.service); a.href = url(item.address, item.port, item.protocol); name.append(a); + } else name.textContent = item.process || 'Unknown'; + tr.append(name, element('td', `${item.address}:${item.port}${item.local ? ' · local only' : ''}`)); + const process = element('td', item.pid ? `PID ${item.pid}` : 'Unavailable'); process.className = 'process'; tr.append(process, element('td', item.evidence)); + return tr; +} +async function refresh() { + try { + const r = await fetch('/_cloud9/catalog.json'); if (!r.ok) throw new Error(`Dashboard: ${r.status}`); + const data = await r.json(); + $('sites').replaceChildren(); + for (const site of data.sites) { + const card = element('article', ''); const h = element('h2', ''); const a = element('a', site.title); a.href = url('', site.port); h.append(a); + const s = site.status; + card.append(h, element('code', site.root), element('p', `${s.phase}${s.watching ? ' · watch on' : ''}${s.queued ? ' · build queued' : ''}${s.exit_code !== null ? ` · exit ${s.exit_code}` : ''}`)); + if (s.finished) card.append(element('small', `Last finished ${new Date(s.finished * 1000).toLocaleString()}`), element('br', '')); + card.append(button('Build', () => control(site.name, 'build'), !site.build), button(s.watching ? 'Stop watching' : 'Watch', () => control(site.name, s.watching ? 'watch off' : 'watch on'), !site.watch), button('Cancel', () => control(site.name, 'cancel'), !s.pid && !s.queued), button('Log', () => { activeLog = site.name; $('log-title').textContent = site.title; $('logs').showModal(); refreshLog(); })); + $('sites').append(card); + } + const known = data.listeners.filter(x => x.protocol !== 'unknown'); + const unknown = data.listeners.filter(x => x.protocol === 'unknown'); + $('services').replaceChildren(...known.map(listenerRow)); + $('unknown').replaceChildren(...unknown.map(listenerRow)); + $('unknown-title').textContent = `Other listeners (${unknown.length})`; + if (data.discovery_error) $('message').textContent = data.discovery_error; + } catch (e) { $('message').textContent = e.message; } +} +async function refreshLog() { + if (!activeLog) return; + try { const r = await fetch(`/fs/sites/${encodeURIComponent(activeLog)}/log`); $('log-text').textContent = await r.text(); } catch (e) { $('log-text').textContent = e.message; } +} +$('close-log').onclick = () => { activeLog = null; $('logs').close(); }; +$('logs').onclose = () => { activeLog = null; }; +refresh(); setInterval(refresh, 5000); setInterval(refreshLog, 1500); -- cgit v1.3