diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-12 15:45:52 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:16 -0300 |
| commit | fd7775b4e9c24b6bb8f664beb7d4fd2ccecd8d85 (patch) | |
| tree | 33a4ba949a2c883d3cbd13d7fd069770f013f166 | |
| parent | 7f418a6bb3c2d5319d781a61a14aeaf15aff4c89 (diff) | |
| download | cloud9-fd7775b4e9c24b6bb8f664beb7d4fd2ccecd8d85.tar.gz cloud9-fd7775b4e9c24b6bb8f664beb7d4fd2ccecd8d85.zip | |
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.
| -rw-r--r-- | README.md | 4 | ||||
| -rw-r--r-- | build.zig | 5 | ||||
| -rw-r--r-- | docs/9web.service | 18 | ||||
| -rw-r--r-- | docs/catalog.example.json | 13 | ||||
| -rw-r--r-- | docs/catalog.md | 177 | ||||
| -rw-r--r-- | test/web/catalog.py | 217 | ||||
| -rw-r--r-- | web/catalog.zig | 549 | ||||
| -rw-r--r-- | web/discover.py | 93 | ||||
| -rw-r--r-- | web/main.zig | 196 | ||||
| -rw-r--r-- | web/static/catalog.html | 14 | ||||
| -rw-r--r-- | web/static/catalog.js | 56 |
11 files changed, 1339 insertions, 3 deletions
@@ -1,5 +1,9 @@ # cloud9 +`9web` also has a [local site catalogue](docs/catalog.md): serve generated +websites directly, trigger or watch builds over HTTP/9P, and discover local HTTP +listeners from process metadata without probing endpoints. + A Zig 0.17 library for base **9P2000** clients, servers, and transports. The protocol core uses caller-owned buffers and bounded request tables. It has @@ -40,6 +40,11 @@ pub fn build(b: *std.Build) void { }) }); bridge.root_module.addAnonymousImport("client.wasm", .{ .root_source_file = wasm.getEmittedBin() }); b.installArtifact(bridge); + if (target.result.os.tag == .linux) { + const catalog_test = b.addSystemCommand(&.{ "python3", "test/web/catalog.py" }); + catalog_test.addArtifactArg(bridge); + b.step("catalog-test", "Test local sites, controls, watcher and passive discovery").dependOn(&catalog_test.step); + } const run_bridge = b.addRunArtifact(bridge); run_bridge.addPassthruArgs(); b.step("serve", "Run the HTTP/WebSocket bridge (--upstream tcp:127.0.0.1:564)").dependOn(&run_bridge.step); diff --git a/docs/9web.service b/docs/9web.service new file mode 100644 index 0000000..a20bd89 --- /dev/null +++ b/docs/9web.service @@ -0,0 +1,18 @@ +[Unit] +Description=9web local sites, build controls and listener catalogue +After=network.target + +[Service] +ExecStart=%h/.local/libexec/9web --listen [::]:8090 --catalog %h/.config/9web/sites.json --catalog-socket %t/9web/catalog.sock --state-dir %h/.local/state/9web +Restart=on-failure +RestartSec=2 +RuntimeDirectory=9web +RuntimeDirectoryMode=0700 +UMask=0077 +KillMode=control-group +TimeoutStopSec=10 +NoNewPrivileges=yes +Environment=PATH=%h/.local/bin:/usr/local/bin:/usr/bin:/bin + +[Install] +WantedBy=default.target diff --git a/docs/catalog.example.json b/docs/catalog.example.json new file mode 100644 index 0000000..c5970f3 --- /dev/null +++ b/docs/catalog.example.json @@ -0,0 +1,13 @@ +{ + "sites": [ + { + "name": "notes", + "title": "My notes", + "cwd": "/home/me/notes", + "root": "/home/me/notes/site/out", + "port": 8091, + "build": ["just", "site"], + "inputs": ["index.typ", "lib", "notes", "site/build.py", "site/site.css"] + } + ] +} diff --git a/docs/catalog.md b/docs/catalog.md new file mode 100644 index 0000000..cd6eec9 --- /dev/null +++ b/docs/catalog.md @@ -0,0 +1,177 @@ +# 9web local catalogue (Linux) + +`9web --catalog FILE` runs a local website catalogue in the gateway process. +It serves existing generated websites immediately, supervises requested builds, +and inventories listening TCP sockets without making requests to them. No +separate site servers are needed. The existing `--upstream` gateway mode is +unchanged; catalogue mode and an explicit upstream are mutually exclusive. + +## Configuration and serving + +See [catalog.example.json](catalog.example.json). Every site has a unique `name`, +`title`, absolute `cwd` and `root`, and a distinct nonzero HTTP `port`. Optional +`build` and `watch` values are argument arrays, not shell command strings. +Use an explicit `sh -c` only when a trusted recipe needs shell syntax. + +``` +9web --listen '[::]:8090' \ + --catalog "$HOME/.config/9web/sites.json" \ + --catalog-socket "$XDG_RUNTIME_DIR/9web/catalog.sock" \ + --state-dir "$HOME/.local/state/9web" +``` + +Create the socket's parent directory first. Configuration is read at startup; +restart the service to change it. A failed site-port bind fails startup rather +than advertising a site that is not actually served. All site listeners use +the dashboard's bind address. `[::]` uses a dual-stack listener for all IPv6 and +IPv4 interfaces; `0.0.0.0` restricts this to IPv4. + +Each site occupies its own HTTP origin so root-relative `/assets/` and `/blog/` +URLs work without HTML rewriting. Directory URLs redirect to a trailing slash +and serve `index.html`; extensionless paths try `.html`. Static serving supports +MIME types, HEAD, single byte ranges, and streaming large files. Symlink traversal, +dotfiles, `..`, and encoded path separators are refused. Directory listings are +available through 9P, not through site HTTP roots. + +The HTTP site root is static: provider-specific `_redirects`, `_headers`, and +Worker APIs are not executed. For example the café's articles and assets work, +but its Cloudflare poke API is not reproduced locally. + +## Builds and watch + +The dashboard has Build, Watch, Cancel, and Log controls. Builds execute in the +configured working directory with the service's environment. One child job runs +per site. Repeated build requests during a build coalesce into one later build. +Build writes enqueue work immediately; serving does not wait for compilation. + +For managed watch, configure `inputs` as source paths relative to `cwd` (or absolute +paths for shared libraries outside the project). The +watcher checks source names, modification times and sizes once per second and +invokes the build recipe on changes. It skips hidden entries, symlinks and common +generated directories (`out`, `dist`, `build`, `node_modules`, `__pycache__`). +List a particular build script explicitly if it lives in one of those directories. +This is a metadata watcher, not a content hash or a Typst dependency graph. +Changes during a build are detected by the next scan. Browser refresh is manual. + +Alternatively, `watch` can specify an existing persistent watcher. Its stdout and +stderr go to the same log, and watch-off cancels its process group. A native watcher +may itself serve a preview port, but the catalogue never requires that preview to +serve the site's generated files. Manual builds are refused while a native watcher +is active. Cancel disables watching and clears queued work. Server shutdown under +systemd kills the whole service cgroup, including build descendants. + +Each new job replaces `<state-dir>/<name>.log`. The UI and 9P expose its last +256 KiB; the on-disk log is not rotated during a long-running native watcher. +Watch state and build status reset at service restart; generated output remains. +No build or watch is started automatically. + +Builders currently write to their configured output directories in place. +`9web` does **not** provide atomic publication or a last-good snapshot: a failed +build can leave partial output, and a file truncated during a response closes that +response. Builders needing atomic publication should generate a separate release +directory and arrange publication themselves. The served root can be a symlink; +symlinks below that root are refused. + +## 9P and HTTP controls + +The catalogue is itself a 9P backend, consumed by the ordinary gateway mux. +`--serve` and WebSocket clients see the same tree as direct socket clients: + +``` +/index.html +/sites/<name>/public/ complete generated output tree (read-only) +/sites/<name>/ctl build | watch on | watch off | cancel +/sites/<name>/status JSON state, PID, exit code, times, generation +/sites/<name>/log last 256 KiB of current/most recent job +/sites/<name>/events blocking stream of JSON state changes +/services/index.json recognized HTTP/HTTPS listeners +/listeners/index.json all observed TCP listeners, including unknown ones +``` + +``` +9ns --unix "$XDG_RUNTIME_DIR/9web/catalog.sock" --name web -- sh +echo build > /mnt/9p/web/sites/entomology/ctl +cat /mnt/9p/web/sites/entomology/status +cat /mnt/9p/web/sites/entomology/log +``` + +Event opens receive the current state, then park until its revision changes. +Each reader has its own cursor; slow readers receive the newest state, not a +durable history of every transition. 9P flush cancels parked reads. State events +also work through `/fs/sites/<name>/events?follow=1` as SSE. `status` is the finite +snapshot to use for ordinary polling. + +The dashboard reads `GET /_cloud9/catalog.json` and uses +`POST /_cloud9/sites/<name>/ctl` with a plain-text command. The existing +`PUT /fs/sites/<name>/ctl` also reaches the same control implementation. + +## Passive discovery + +Every five seconds, an embedded Python 3 inventory helper reads `/proc/net/tcp*`, +joins socket inodes to process descriptors, and recognizes executable/argument +patterns for miniserve, Python `http.server`, Typst watch, Vite and Wrangler dev. +9web advertises only its explicitly registered HTTP ports: its 9P listener is +never labelled HTTP just because the owning executable is 9web. + +No TCP connections or HTTP probes are performed. Unknown or inaccessible process +ownership stays unknown. Full command lines and environments are not published; +entries contain address, actual bound port, PID, executable, cwd and the evidence +for classification. Process races and permissions can make entries temporarily +unknown. Discovery is in the service's network namespace, not across containers. +The helper is a short-lived child of 9web, not another daemon. + +Wildcard addresses in dashboard links use the browser's current server hostname. +Specific addresses remain specific; loopback services are labelled local-only. +There is no automatic proxy to loopback-only services. Recognizers identify likely +HTTP listeners from process evidence; they cannot prove application health. + +## Service and access + +[9web.service](9web.service) is a user-systemd unit template. Install the binary +at `~/.local/libexec/9web`, the config at `~/.config/9web/sites.json`, and the unit +at `~/.config/systemd/user/9web.service`, then: + +``` +systemctl --user daemon-reload +systemctl --user enable --now 9web.service +systemctl --user status 9web.service +journalctl --user -u 9web.service +``` + +The unit supplies a private runtime directory and socket, restarts on failure, +and kills child jobs together with the service. Lingering must be enabled if the +user service should run without an interactive login. It needs `python3`, `setsid`, +and the configured project build tools in PATH. Miniserve remains independent. + +This is a trusted-network operator interface: it has **no login or TLS**. Anyone +who can reach the dashboard can read the catalogue and trigger configured jobs. +Browser control requests reject foreign Origins; numeric Host validation and +separate site origins prevent served pages from using the dashboard as a same-origin +control API. This is not authentication. Bind to loopback or place an authenticated +gateway in front of it when those network permissions are inappropriate. No +arbitrary command can be supplied through HTTP or 9P; commands come from the local +registry, which should remain writable only by its owner. + +## Verification + +``` +zig build catalog-test http-fs-test mux-test -Doptimize=ReleaseSafe +``` + +The catalogue integration test uses temporary real sites and processes. It checks +immediate static serving, pretty URLs, MIME, HEAD/ranges, traversal and symlink +rejection, Host/Origin checks, HTTP and raw 9P controls, watched rebuilds, failed +build status, process-group cancellation, nonblocking event readers, and actual +socket ownership discovery. Existing gateway HTTP/filesystem and mux suites cover +the original mode. + +## Installed workstation configuration + +The initial workstation setup uses dashboard port 8090 and site ports 8091–8095: +Entomology lab, 0x4200.cafe, Standards, Pardes website, and the older café hub archive. +The archive is read-only (no build recipe registered). Other sites have explicit +local build recipes and opt-in managed watches. Port 8081 remains miniserve's. +Runtime config is `~/.config/9web/sites.json`; the service uses a separately +installed binary so subsequent development builds do not change the running +service's executable on disk. Source and deployment changes are described in +the implementing git commit; no remote push is required for the local service. diff --git a/test/web/catalog.py b/test/web/catalog.py new file mode 100644 index 0000000..e1b153e --- /dev/null +++ b/test/web/catalog.py @@ -0,0 +1,217 @@ +#!/usr/bin/env python3 +"""Black-box catalogue checks: real HTTP, 9P, child processes and files.""" +from http.client import HTTPConnection, HTTPException +import importlib.util +import json +import os +from pathlib import Path +import socket +import struct +import subprocess +import sys +import tempfile +import time + + +def port(): + with socket.socket() as s: + s.bind(('127.0.0.1', 0)) + return s.getsockname()[1] + + +def eventually(f, timeout=15): + end = time.monotonic() + timeout + while time.monotonic() < end: + try: + value = f() + if value: + return value + except (OSError, HTTPException): + pass + time.sleep(.1) + raise AssertionError('timed out: ' + repr(f)) + + +def http(p, path='/', method='GET', body=None, headers=None): + c = HTTPConnection('127.0.0.1', p, timeout=5) + try: + c.request(method, path, body, headers or {}) + r = c.getresponse() + return r.status, dict(r.getheaders()), r.read() + finally: + c.close() + + +def string(s): + b = s.encode() + return struct.pack('<H', len(b)) + b + + +class Nine: + def __init__(self, path): + self.s = socket.socket(socket.AF_UNIX) + self.s.settimeout(5) + self.s.connect(path) + self.rpc(100, struct.pack('<I', 65536) + string('9P2000'), 65535) + self.rpc(104, struct.pack('<II', 1, 0xffffffff) + string('user') + string('')) + + def recv(self, n): + b = b'' + while len(b) < n: + chunk = self.s.recv(n-len(b)) + if not chunk: + raise AssertionError('9P disconnected') + b += chunk + return b + + def rpc(self, t, body, tag=1): + self.s.sendall(struct.pack('<IBH', len(body)+7, t, tag) + body) + size, rt, rtag = struct.unpack('<IBH', self.recv(7)) + result = self.recv(size-7) + assert rt != 107, result + assert rt == t+1 and rtag == tag, (rt, rtag) + return result + + def walk(self, path): + names = path.split('/') + r = self.rpc(110, struct.pack('<IIH', 1, 2, len(names)) + b''.join(map(string, names))) + assert struct.unpack('<H', r[:2])[0] == len(names), path + + def read(self, path): + self.walk(path) + self.rpc(112, struct.pack('<IB', 2, 0)) + r = self.rpc(116, struct.pack('<IQI', 2, 0, 32000)) + self.rpc(120, struct.pack('<I', 2)) + return r[4:] + + def write(self, path, text): + self.walk(path) + self.rpc(112, struct.pack('<IB', 2, 0x11)) + b = text.encode() + self.rpc(118, struct.pack('<IQI', 2, 0, len(b)) + b) + self.rpc(120, struct.pack('<I', 2)) + + def list(self, path): + self.walk(path) + self.rpc(112, struct.pack('<IB', 2, 0)) + offset, names = 0, [] + while True: + data = self.rpc(116, struct.pack('<IQI', 2, offset, 160))[4:] + if not data: break + offset += len(data) + while data: + size = struct.unpack('<H', data[:2])[0] + 2 + nlen = struct.unpack('<H', data[41:43])[0] + names.append(data[43:43+nlen].decode()) + data = data[size:] + self.rpc(120, struct.pack('<I', 2)) + return names + + +def main(): + binary = str(Path(sys.argv[1] if len(sys.argv) > 1 else 'zig-out/bin/9web').resolve()) + spec = importlib.util.spec_from_file_location('discover', Path(__file__).resolve().parents[2] / 'web/discover.py') + discover = importlib.util.module_from_spec(spec) + spec.loader.exec_module(discover) + assert discover.recognize(['python3', '-m', 'http.server', '8000']) + assert discover.recognize(['node', '/project/app.js', 'vite']) is None + assert discover.recognize(['miniserve', '--tls-cert', 'cert'])[1] == 'https' + with tempfile.TemporaryDirectory(prefix='9web-catalog-') as tmp: + root = Path(tmp) + public = root/'public' + public.mkdir() + (public/'index.html').write_text('<h1>catalog fixture</h1>') + (public/'style.css').write_text('body{color:red}') + (public/'pretty.html').write_text('pretty page') + (public/'nested').mkdir() + (public/'nested/index.html').write_text('nested page') + (root/'secret').write_text('must not serve') + (public/'escape').symlink_to(root/'secret') + (public/'escape-dir').symlink_to(root, target_is_directory=True) + (root/'source.txt').write_text('source') + dashboard, site_port, python_port = port(), port(), port() + cfg = dict(sites=[dict(name='fixture', title='Fixture', cwd=tmp, root=str(public), port=site_port, + build=['sh','-c','echo build-output; if test -f slow; then sleep 30 & echo $! > descendant; wait; fi; sleep 0.2; cp source.txt public/result.txt'], inputs=['source.txt'])]) + (root/'catalog.json').write_text(json.dumps(cfg)) + log = open(root/'server.log', 'wb') + proc = subprocess.Popen([binary, '--listen', f'0.0.0.0:{dashboard}', '--catalog', str(root/'catalog.json'), '--catalog-socket', str(root/'catalog.sock'), '--state-dir', str(root/'state')], stdout=log, stderr=log) + py = subprocess.Popen([sys.executable, '-m', 'http.server', str(python_port), '--bind', '127.0.0.1', '--directory', str(public)], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + try: + eventually(lambda: http(dashboard)[0] == 200) + assert http(site_port)[2] == b'<h1>catalog fixture</h1>' + assert http(site_port, '/style.css')[1]['content-type'] == 'text/css' + assert http(site_port, '/pretty')[2] == b'pretty page' + assert http(site_port, '/nested')[0] == 301 + assert http(site_port, '/nested/')[2] == b'nested page' + assert http(site_port, '/', 'HEAD')[2] == b'' + assert http(site_port, '/style.css', headers={'Range':'bytes=0-3'})[2] == b'body' + for path in ['/../secret', '/%2e%2e/secret', '/escape', '/escape-dir/secret', '/.git/config', '/%2fetc/passwd']: + assert http(site_port, path)[0] in (400,403,404), path + assert http(dashboard, headers={'Host':'attacker.invalid'})[0] == 403 + ctl = '/_cloud9/sites/fixture/ctl' + assert http(dashboard, ctl, 'POST', 'build', {'Origin':'https://attacker.invalid'})[0] == 403 + assert http(dashboard, ctl, 'POST', 'bogus')[0] == 400 + nine = Nine(str(root/'catalog.sock')) + assert nine.list('sites') == ['fixture'] + assert nine.list('sites/fixture') == ['ctl', 'events', 'log', 'public', 'status'] + assert nine.list('sites/fixture/public') == ['index.html', 'nested', 'pretty.html', 'style.css'] + assert nine.read('sites/fixture/public/index.html').startswith(b'<h1>') + assert b'build' in nine.read('sites/fixture/ctl') + nine.write('sites/fixture/ctl', 'build\n') + eventually(lambda: (public/'result.txt').exists()) + eventually(lambda: json.loads(nine.read('sites/fixture/status'))['generation'] == 1) + assert b'build-output' in nine.read('sites/fixture/log') + assert http(dashboard, ctl, 'POST', 'watch on')[0] == 202 + eventually(lambda: json.loads(nine.read('sites/fixture/status'))['generation'] >= 2) + (root/'source.txt').write_text('changed source') + eventually(lambda: (public/'result.txt').read_text() == 'changed source') + assert http(dashboard, ctl, 'POST', 'watch off')[0] == 202 + def discovered(): + data = json.loads(http(dashboard, '/_cloud9/catalog.json')[2]) + return any(x['port'] == python_port and x['service'] == 'python http.server' for x in data['listeners']) + eventually(discovered) + # A parked event reader must not stall HTTP or other 9P clients. + events = Nine(str(root/'catalog.sock')) + events.walk('sites/fixture/events') + events.rpc(112, struct.pack('<IB', 2, 0)) + first = events.rpc(116, struct.pack('<IQI', 2, 0, 2048)) + assert json.loads(first[4:])['revision'] > 0 + pending = struct.pack('<IQI', 2, len(first)-4, 2048) + events.s.sendall(struct.pack('<IBH', len(pending)+7, 116, 2) + pending) + assert http(site_port)[0] == 200 + (root/'slow').write_text('yes') + nine.write('sites/fixture/ctl', 'build') + size, typ, tag = struct.unpack('<IBH', events.recv(7)) + assert (typ, tag) == (117, 2) + assert json.loads(events.recv(size-7)[4:])['revision'] > json.loads(first[4:])['revision'] + eventually(lambda: (root/'descendant').exists()) + nine.write('sites/fixture/ctl', 'cancel') + eventually(lambda: json.loads(nine.read('sites/fixture/status'))['phase'] == 'canceled') + descendant = int((root/'descendant').read_text()) + def stopped(): + try: return Path(f'/proc/{descendant}/stat').read_text().split(') ')[1].startswith('Z') + except FileNotFoundError: return True + eventually(stopped) + events.s.close() + (root/'slow').unlink() + # A failed compile leaves readable existing output and reports failure. + (root/'source.txt').unlink() + nine.write('sites/fixture/ctl', 'build') + eventually(lambda: json.loads(nine.read('sites/fixture/status'))['phase'] == 'failed') + assert http(site_port)[0] == 200 + nine.s.close() + print('catalog: HTTP serving, paths, MIME, HEAD/range, controls, watch, failure, cancellation, 9P events and passive discovery passed') + except BaseException: + log.flush() + print((root/'server.log').read_text(), file=sys.stderr) + raise + finally: + proc.terminate(); py.terminate() + for p in (proc, py): + try: p.wait(timeout=5) + except subprocess.TimeoutExpired: p.kill(); p.wait() + log.close() + + +if __name__ == '__main__': + main() 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 @@ +<!doctype html> +<html lang="en"><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"> +<title>9web · local sites</title> +<style> +:root{color-scheme:light dark;font:16px/1.5 system-ui;background:#151917;color:#e3e9df}body{max-width:1100px;margin:3rem auto;padding:0 1.5rem}h1{font-size:2.5rem;margin-bottom:0}a{color:#b8dfa6}header p{color:#aab8a6}.sites{display:grid;grid-template-columns:repeat(auto-fit,minmax(290px,1fr));gap:1rem}article{background:#212922;border:1px solid #3c4b3c;border-radius:10px;padding:1.2rem}article h2{margin:0 0 .6rem;font-size:1.3rem}code,pre{font:13px/1.5 monospace;overflow-wrap:anywhere}button{background:#324631;color:inherit;border:1px solid #61765a;border-radius:5px;padding:.4rem .7rem;cursor:pointer;margin:.4rem .3rem .1rem 0}button:disabled{opacity:.4}table{width:100%;border-collapse:collapse}td,th{text-align:left;border-bottom:1px solid #354234;padding:.6rem;overflow-wrap:anywhere}th{color:#aab8a6}pre{max-height:35rem;overflow:auto;background:#101410;padding:1rem;white-space:pre-wrap}small{color:#aab8a6}.error{color:#ffb4a9}.toolbar{display:flex;gap:1rem;align-items:center}dialog{width:min(850px,85vw);background:#212922;color:inherit;border:1px solid #61765a}#message{min-height:1.5em}details{margin-top:1.5rem}@media(max-width:650px){td,th{padding:.4rem;font-size:13px}.process{display:none}} +</style> +<header><h1>9web</h1><p>Local sites, builds, and listening services.</p></header> +<p id="message" role="status"></p><main><div id="sites" class="sites"></div> +<div class="toolbar"><h2>HTTP services</h2><a href="/fs/">9P tree</a></div> +<p><small>Identified from socket ownership and process arguments. No endpoint probing. Loopback links work only on this computer.</small></p> +<table><thead><tr><th>Service</th><th>Address</th><th class="process">Process</th><th>Evidence</th></tr></thead><tbody id="services"></tbody></table> +<details><summary id="unknown-title">Other listeners</summary><table><tbody id="unknown"></tbody></table></details></main> +<dialog id="logs"><button id="close-log">Close</button><h2 id="log-title"></h2><pre id="log-text"></pre></dialog> +<script src="/_cloud9/catalog.js"></script></html> 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); |
