Makefile
@@ -10,10 +10,12 @@ all: build # cmd/. MODULE is the library's import path, which -X needs. MODULE := $(shell go list -m) -# build names this checkout in the binary, so its bug button files into-# csi/bugs here; the package is built without it.+# build names this checkout in the reader's binary, so its bug button files+# into csi/bugs here; the package is built without it. The public binary+# has no bug button. build: CGO_ENABLED=0 go build -ldflags='-X $(MODULE).sourceDir=$(CURDIR)' -o build/$(NAME) ./cmd/local+ CGO_ENABLED=0 go build -o build/$(NAME)-public ./cmd/public # syntax builds the tree-sitter helper beside the binary, which finds it # there. It is a module of its own, in syntax/, since tree-sitter needs cgo@@ -43,7 +45,7 @@ test: # blackbox runs tests/ against the built binary. See doc/testing.md. blackbox: build- cd tests && TEST_BIN=$(CURDIR)/build/$(NAME) uv run pytest+ cd tests && TEST_BIN=$(CURDIR)/build/$(NAME) TEST_PUBLIC_BIN=$(CURDIR)/build/$(NAME)-public uv run pytest # bench runs the benchmarks in bench_test.go. BENCH picks them by pattern # and COUNT repeats each, eg COUNT=10 for benchstat.@@ -72,6 +74,7 @@ OLDUNIT := $(HOME)/.config/systemd/user/$(NAME).service install: build ln -sf $(CURDIR)/build/$(NAME) $(HOME)/.local/bin/$(NAME)+ ln -sf $(CURDIR)/build/$(NAME)-public $(HOME)/.local/bin/$(NAME)-public mkdir -p $(dir $(USERUNIT)) sed 's/@NAME@/$(NAME)/g; s|@BIN@|$(HOME)/.local/bin/$(NAME)|' systemd/dev.service.in > $(USERUNIT) -grep -qs '^ExecStart=$(HOME)/.local/bin/' $(OLDUNIT) && { systemctl --user disable $(NAME); rm $(OLDUNIT); }@@ -79,7 +82,7 @@ install: build -build/$(NAME) --restart uninstall:- rm -f $(HOME)/.local/bin/$(NAME) $(USERUNIT)+ rm -f $(HOME)/.local/bin/$(NAME) $(HOME)/.local/bin/$(NAME)-public $(USERUNIT) -systemctl --user daemon-reload # deb builds a Debian package in build/. The version comes from the last@@ -93,6 +96,7 @@ DEB := build/$(NAME)_$(VERSION)_$(ARCH).deb deb: rm -rf $(STAGE) CGO_ENABLED=0 GOARCH=$(ARCH) go build -trimpath -ldflags='-s -w' -o $(STAGE)/usr/bin/$(NAME) ./cmd/local+ CGO_ENABLED=0 GOARCH=$(ARCH) go build -trimpath -ldflags='-s -w' -o $(STAGE)/usr/bin/$(NAME)-public ./cmd/public install -D -m 0644 README.md $(STAGE)/usr/share/doc/$(NAME)/README.md { sed '/^@EXPAT@$$/,$$d; s/@NAME@/$(NAME)/g' deb/copyright.in; sed '1,4d; s/^$$/./; s/^/ /' LICENSE; \ sed '1,/^@EXPAT@$$/d' deb/copyright.in; sed 's/^$$/./; s/^/ /' assets/fonts/STIXTwoText-OFL.txt; } > $(STAGE)/copyright
README.md
@@ -20,12 +20,18 @@ www --restart [-f] [--no-open] [--debug] [--no-bug] www -l [-a ADDR | --share[=HOST]] [--] [DIR] www --kill [-a ADDR | --share[=HOST]] www --commit=REV [--] [DIR]-www --public --disk=SIZE (-a ADDR | --share[=HOST]) [--debug]- [--cache=SIZE] www -h | --help www -V | --version ``` +A second executable, `www-public`, serves online repos to the web and nothing else:++```+www-public --disk=SIZE (-a ADDR | --share[=HOST]) [--debug] [--cache=SIZE]+www-public -h | --help+www-public -V | --version+```+ Run `www` in a directory. It prints the tree's URL and opens the browser. It also writes a lockfile named after the endpoint, eg `127.31.1.1-8080.lock`. Delete the lockfile to stop serving the tree. Trees on one address share one server, which exits when its last lock is gone.@@ -36,7 +42,7 @@ www -p ~/notes serve ~/notes, and again after a reboot www --share=192.168.1.5 serve this directory to the LAN www -l list every served tree as keyval www --restart move every server to the current binary-www --public --disk=6G -a 127.0.0.1:8080+www-public --disk=6G -a 127.0.0.1:8080 serve online repos to the web, behind a proxy ``` @@ -48,7 +54,7 @@ To read a repo from GitHub, open `http://127.31.1.2/github.com/junegunn/fzf/`. www needs git. -`make install` links `build/www` into `~/.local/bin` and serves on port 8080.+`make install` links `build/www` and `build/www-public` into `~/.local/bin` and serves on port 8080. `make install-deb` builds and installs two Debian packages: www and its syntax tree helper. The package's binary may bind port 80, so URLs carry no port. @@ -67,7 +73,7 @@ www needs git. - [Log](doc/log.md): what www writes to the journal - [Debugging](doc/debugging.md): the bug button and the build stamp - [Install](doc/install.md): the Debian package, port 80 and locks in git-- [Development](doc/development.md): `make run`, the working name and the usage check+- [Development](doc/development.md): `make run`, the two executables, the working name and the usage check - [Testing](doc/testing.md): Go tests, the black-box suite and the heavy runs ## Licence
args.go
@@ -21,7 +21,7 @@ type config struct { noBug bool // hide the bug button and refuse reports cache string // the memory for cached git answers, eg 32M; "" takes defaultCache disk string // the disk for online repos, eg 4G; "" takes defaultDisk- public bool // serve only online repos, to the open web+ public bool // serve only online repos, to the open web; the public executable sets it help bool version bool restart bool@@ -45,8 +45,6 @@ var usageText = strings.NewReplacer("@NAME@", progName, "@ENV@", envPrefix).Repl @NAME@ -l [-a ADDR | --share[=HOST]] [--] [DIR] @NAME@ --kill [-a ADDR | --share[=HOST]] @NAME@ --commit=REV [--] [DIR]- @NAME@ --public --disk=SIZE (-a ADDR | --share[=HOST]) [--debug]- [--cache=SIZE] @NAME@ -h | --help @NAME@ -V | --version @@ -100,10 +98,6 @@ Options: its full hash, its stardate and its subject; REV is a stardate, eg 261024.4, a hash or any name git takes- --public serve only repos fetched from a forge, never a tree- here, in the foreground, to anyone who reaches the- address, eg through a proxy on -a; it lists no- cache, shows no stop or bug button and opens nothing -V, --version print when the binary was built and its commit -h, --help print this help `)@@ -123,7 +117,6 @@ func (c *config) switches() map[string]*bool { "force": &c.force, "list": &c.list, "kill": &c.kill,- "public": &c.public, } } @@ -211,6 +204,9 @@ func parseArgs(args []string) (config, error) { } continue }+ if name == "public" {+ return c, &errno{syscall.EINVAL, "--public is an executable of its own, " + publicName}+ } p, ok := sw[name] switch { case !ok:@@ -240,21 +236,9 @@ func parseArgs(args []string) (config, error) { } } if c.commit != "" && (c.restart || c.list || c.kill || c.fg || c.noopen || c.persist || c.debug || c.noBug ||- c.sharing || c.secret || c.addr != "" || c.cache != "" || c.disk != "" || c.public) {+ c.sharing || c.secret || c.addr != "" || c.cache != "" || c.disk != "") { return c, &errno{syscall.EINVAL, "--commit takes a directory and no other option"} }- // A public server runs under a service manager, so it stays in the- // foreground, opens nothing and files no bugs; it serves no tree.- if c.public && (c.restart || c.list || c.kill || c.fg || c.noopen || c.persist || c.noBug || c.secret || len(dirs) > 0) {- return c, &errno{syscall.EINVAL, "--public takes an address, --disk, --cache and --debug, and no directory"}- }- if c.public && c.disk == "" {- return c, &errno{syscall.EINVAL, "--public needs --disk, since any visitor can make it clone"}- }- // The default address is the user's own, where a reader's server runs.- if c.public && c.addr == "" && !c.sharing {- return c, &errno{syscall.EINVAL, "--public needs -a or --share"}- } if c.force && !c.restart { return c, &errno{syscall.EINVAL, "--force goes with --restart"} }@@ -280,7 +264,7 @@ func parseArgs(args []string) (config, error) { case 0: // --list without a directory lists every tree, and --restart // without one serves no tree beyond those it restarts.- if !c.list && !c.kill && !c.restart && !c.public {+ if !c.list && !c.kill && !c.restart { c.dir = "." } case 1:
args_test.go
@@ -54,14 +54,7 @@ func TestParseArgs(t *testing.T) { {"--cache=lots"}, {"--cache=-1M"}, {"--cache"},- {"--public", "-a", "127.0.0.1"},- {"--public", "--disk=6G"},- {"--public", "--disk=6G", "-a", "127.0.0.1", "docs"},- {"--public", "--disk=6G", "-a", "127.0.0.1", "--foreground"},- {"--public", "--disk=6G", "-a", "127.0.0.1", "-p"},- {"--public", "--disk=6G", "--share", "--secret"},- {"--public", "--disk=6G", "--restart", "-a", "127.0.0.1"},- {"--commit=HEAD", "--public"},+ {"--public", "--disk=6G", "-a", "127.0.0.1"}, } { if _, err := parseArgs(args); err == nil { t.Errorf("%q: no error", args)@@ -104,27 +97,96 @@ var parseOK = []struct { {[]string{"--commit=261024.4"}, config{dir: ".", commit: "261024.4"}}, {[]string{"--commit", "HEAD", "docs"}, config{dir: "docs", commit: "HEAD"}}, {[]string{"--kill", "--share"}, config{sharing: true, kill: true}},- {[]string{"--public", "--disk=6G", "-a", "127.0.0.1:8080"}, config{public: true, disk: "6G", addr: "127.0.0.1:8080"}},- {[]string{"--public", "--disk=6G", "--share=0.0.0.0", "--debug"}, config{public: true, disk: "6G", sharing: true, share: "0.0.0.0", debug: true}}, } -// The usage text is docopt, and docopt takes every command line in-// parseOK. docopt-ng runs through uv, and the test skips without it.-// docopt has no option with an optional value, so --share[=HOST] is-// checked as --share.+// The public executable takes an address, --disk, --cache and --debug. It+// takes no directory and none of the reader's other options, --public+// among them.+func TestParsePublicArgs(t *testing.T) {+ for _, tc := range parsePublicOK {+ got, err := parsePublicArgs(tc.args)+ if err != nil || got != tc.want {+ t.Errorf("%q: %+v, %v; want %+v", tc.args, got, err, tc.want)+ }+ }+ for _, args := range [][]string{+ nil,+ {"-a", "127.0.0.1"},+ {"--disk=6G"},+ {"--disk", "-a", "127.0.0.1"},+ {"--disk=", "-a", "127.0.0.1"},+ {"--disk=lots", "-a", "127.0.0.1"},+ {"--disk=6G", "-a"},+ {"--disk=6G", "--addr="},+ {"--disk=6G", "--share="},+ {"--disk=6G", "-a", "127.0.0.1", "--share"},+ {"--disk=6G", "-a", "127.0.0.1", "docs"},+ {"--disk=6G", "-a", "127.0.0.1", "--", "docs"},+ {"--disk=6G", "-a", "127.0.0.1", "--cache=lots"},+ {"--disk=6G", "-a", "127.0.0.1", "--debug=yes"},+ {"--disk=6G", "-a", "127.0.0.1", "--public"},+ {"--disk=6G", "-a", "127.0.0.1", "--foreground"},+ {"--disk=6G", "-a", "127.0.0.1", "--no-open"},+ {"--disk=6G", "-a", "127.0.0.1", "--no-bug"},+ {"--disk=6G", "-a", "127.0.0.1", "--restart"},+ {"--disk=6G", "-a", "127.0.0.1", "--kill"},+ {"--disk=6G", "-a", "127.0.0.1", "-p"},+ {"--disk=6G", "-a", "127.0.0.1", "-l"},+ {"--disk=6G", "-a", "127.0.0.1", "-f"},+ {"--disk=6G", "--share", "--secret"},+ {"--disk=6G", "-a", "127.0.0.1", "--commit=HEAD"},+ } {+ if _, err := parsePublicArgs(args); err == nil {+ t.Errorf("%q: no error", args)+ }+ }+}++// parsePublicOK holds command lines the public executable takes.+var parsePublicOK = []struct {+ args []string+ want config+}{+ {[]string{"--disk=6G", "-a", "127.0.0.1:8080"}, config{public: true, disk: "6G", addr: "127.0.0.1:8080"}},+ {[]string{"--disk", "6G", "-a127.0.0.1"}, config{public: true, disk: "6G", addr: "127.0.0.1"}},+ {[]string{"--addr=127.0.0.1", "--disk=6G", "--cache=64M"}, config{public: true, disk: "6G", addr: "127.0.0.1", cache: "64M"}},+ {[]string{"--disk=6G", "--share=0.0.0.0", "--debug"}, config{public: true, disk: "6G", sharing: true, share: "0.0.0.0", debug: true}},+ {[]string{"--disk=6G", "--share"}, config{public: true, disk: "6G", sharing: true}},+ {[]string{"-h"}, config{public: true, help: true}},+ {[]string{"--help"}, config{public: true, help: true}},+ {[]string{"-V"}, config{public: true, version: true}},+ {[]string{"--version"}, config{public: true, version: true}},+}++// Each executable's usage text is docopt, and docopt takes every command+// line in parseOK and parsePublicOK. docopt-ng runs through uv, and the+// test skips without it. docopt has no option with an optional value, so+// --share[=HOST] is checked as --share. func TestUsageIsDocopt(t *testing.T) {+ var local, public [][]string+ for _, tc := range parseOK {+ local = append(local, tc.args)+ }+ for _, tc := range parsePublicOK {+ public = append(public, tc.args)+ }+ t.Run("local", func(t *testing.T) { checkDocopt(t, usageText, local) })+ t.Run("public", func(t *testing.T) { checkDocopt(t, publicUsage, public) })+}++func checkDocopt(t *testing.T, text string, ok [][]string) { uv, err := exec.LookPath("uv") if err != nil { t.Skip("uv is not installed") } usage := filepath.Join(t.TempDir(), "usage")- if err := os.WriteFile(usage, []byte(strings.ReplaceAll(usageText, "--share[=HOST]", "--share")), 0o600); err != nil {+ if err := os.WriteFile(usage, []byte(strings.ReplaceAll(text, "--share[=HOST]", "--share")), 0o600); err != nil { t.Fatal(err) } var lines strings.Builder- for _, tc := range parseOK {- args := make([]string, len(tc.args))- for i, a := range tc.args {+ for _, line := range ok {+ args := make([]string, len(line))+ for i, a := range line { if strings.HasPrefix(a, "--share=") { a = "--share" }
bench_test.go
@@ -144,7 +144,7 @@ func BenchmarkSyntaxTree(b *testing.B) { b.Fatal(err) } defer root.Close()- s := newServer()+ s := newServer().local().local() s.add(newTree(root, dir, "/", "bench")) h := s.handler() b.ReportAllocs()
budget_test.go
@@ -45,7 +45,7 @@ func TestPageBudget(t *testing.T) { t.Fatalf("git init: %v\n%s", err, out) } commitAll(t, dir, "Add the README and render.go")- s := newServer()+ s := newServer().local().local() addTree(t, s, dir, "/", "budget") h := s.handler() c := head(t, dir, "HEAD")
cmd/local/main.go
@@ -4,4 +4,4 @@ package main import "www" -func main() { www.Main() }+func main() { www.Local() }
cmd/public/main.go
new file mode 100644@@ -0,0 +1,7 @@+// Command public is the executable for the open web: it serves only+// repos fetched from a forge, never a tree of this machine.+package main++import "www"++func main() { www.Public() }
doc/development.md
@@ -6,6 +6,12 @@ A later run replaces the server on 8080 with the new build, serving the same tre It opens the browser only when no page of this directory was open on the old server, and says which happened. A hidden tab holds no connection, so it does not count and gets a second tab. `www --kill -a 127.31.N.1:8080` stops it. +## Two executables++The root directory is a library package. Each executable has only its main under `cmd/`: `cmd/local` builds `www`, the reader, and `cmd/public` builds `www-public`, for the open web. `make build` builds both into `build/`.++The public executable leaves a feature out by never calling it, and the linker drops code that no call leads to. A server gets the stop form, the bug form and the index only through `local()`, which the public start never calls. `TestPublicLeavesOutTheReadersFeatures` builds both and reads their symbols: the public one must hold none of the listed names and the reader every one.+ ## The working name The program's name lives in the file `WORKING_NAME`, since www is a working name until a final one is chosen. The binary, its paths, environment variables, journal tag and URLs, the Makefile and the Debian package all take it from there. A test fails on any other copy outside prose and `go.mod`.
doc/install.md
@@ -16,7 +16,7 @@ Debian's needrestart lists the services that use old libraries after the install ## Port 80 -The package grants `/usr/bin/www` one capability, `cap_net_bind_service`, so it can bind port 80 while running as the user. `getcap /usr/bin/www` shows it.+The package holds both executables, `/usr/bin/www` and `/usr/bin/www-public`. It grants `/usr/bin/www` one capability, `cap_net_bind_service`, so it can bind port 80 while running as the user. `getcap /usr/bin/www` shows it. With it www serves on port 80 and URLs carry no port. Without it, as after `make install`, the port is 8080. www also takes port 80 where the machine lets every user bind low ports.
doc/online-repos.md
@@ -70,7 +70,9 @@ The listing names its source and the sources that failed first, so a partial lis ## On the open web -`www --public --disk=SIZE -a ADDR` serves online repos to anyone who reaches the address, eg through Caddy in front of it. It serves no tree from the machine and no other address, and it runs in the foreground for a service manager. `--share=HOST` binds a public address directly instead.+`www-public --disk=SIZE -a ADDR` serves online repos to anyone who reaches the address, eg through Caddy in front of it. It serves no tree from the machine and no other address, and it runs in the foreground for a service manager. `--share=HOST` binds a public address directly instead.++`www-public` is an executable of its own, built from the code `www` uses for online repos. It does not contain the bug button, the stop buttons, the index of servers, the browser launch, `--restart`, `-l`, `--kill`, `-p` or locks, so no flag or request can reach them. `make test` reads the binary's symbols and fails if one of them is there. `www --public` is gone and says where it went. A visitor learns nothing of the machine or of what others read. `/` says only how to read a repo. A listing that no forge or web frontend gives is a 404, never the cache. Pages write the cache's paths from `cache/`, git's errors included.
doc/options.md
@@ -20,6 +20,8 @@ - `-V`, `--version`: print when the binary was built and the commit it came from, as the stamp says it, eg `www built 2026-10-21 12:49, from 1aa282c+modified` - `-h`, `--help`: print the options +`www-public` takes five of these: `-a`, `--share`, `--disk`, which it requires, `--cache` and `--debug`, with `-V` and `-h`. It takes no directory. `www-public --help` prints them, and `doc/online-repos.md` says what it serves.+ Options follow GNU style. Short ones combine, as in `-pa 127.31.1.2`, and options may follow the directory. Optional values go joined, as in `--share=192.168.1.5`; with a space the address would be read as the directory, and www says so. `-f` and `-n` mean force and dry run in every tool of the author's, so `--foreground` and `--no-open` have no short form. The help text is written in docopt notation, except that docopt has no way to write the optional `=HOST`. `--list` and `--kill` print [keyval](https://unixfile.github.io/keyval/): three lines per tree, sorted, with every index the same width. The binary line is missing for a server that started before www recorded it.
doc/testing.md
@@ -10,7 +10,7 @@ Write new Go tests as tables, with the cases in `testdata/` where they fit. Purp ## Black-box tests -They run the built binary and check what a user sees. They cover what in-process tests cannot reach: flag parsing, the listener, `--public`, process lifecycle and browser pages. The first case is a missing path on `--public`, which shows the 404 coin.+They run the built binary and check what a user sees. They cover what in-process tests cannot reach: flag parsing, the listener, the public executable, process lifecycle and browser pages. The first case is a missing path on `--public`, which shows the 404 coin. The suite is pytest in `tests/`, with its own `pyproject.toml`. `make blackbox` builds the binary and runs it. HTTP, browser and process cases exist so far; the CLI kind below comes with its first case. @@ -21,9 +21,9 @@ The suite is pytest in `tests/`, with its own `pyproject.toml`. `make blackbox` Rules for the suite: -- It reads the binary from `TEST_BIN` and assumes nothing else about it+- It reads the reader's binary from `TEST_BIN` and the public one from `TEST_PUBLIC_BIN`, and assumes nothing else about them - An HTTP or CLI case is a TOML file in `tests/cases/`: argv with expected stdout and exit code, or a request with the expected page. A parametrized test loads it-- A case names the server forms it runs on: `public`, the `--public` flag, or `default`, a reader's server, as wwwvm's unit runs it+- A case names the server forms it runs on: `public`, the public executable, or `default`, a reader's server, as wwwvm's unit runs it - A case compares the page without its script and style, since a reader never sees their words - `tests/harness.py` seeds the cache on disk, since a public server clones only over https from the open internet. It is the one place the suite knows the cache's layout - Name each case after the decision it checks
links_test.go
@@ -56,7 +56,7 @@ func linkTree(t *testing.T) *server { t.Fatal(err) } }- s := newServer()+ s := newServer().local().local() addTree(t, s, dir, "/", "tree") return s }
main.go
@@ -19,8 +19,9 @@ import ( // readyEnv names the descriptor a detached child reports its start on. var readyEnv = envPrefix + "READY_FD" -// Main runs the program on the process's arguments and exits.-func Main() {+// Local runs the reader's executable on the process's arguments and+// exits. It serves trees of this machine.+func Local() { c, err := parseArgs(os.Args[1:]) switch { case err != nil:@@ -67,8 +68,6 @@ func Main() { fd := os.Getenv(readyEnv) switch {- case c.public:- os.Exit(servePublic(c)) case c.restart: os.Exit(restart(c)) case c.list:
name.go
@@ -22,3 +22,6 @@ var envPrefix = strings.ToUpper(progName) + "_" // stylesheet and the live reload stream. Trees mount at paths relative to // home, which this one, starting with an underscore, does not shadow. var ownPath = "/_" + progName + "/"++// publicName is the executable that serves online repos to the open web.+var publicName = progName + "-public"
public.go
new file mode 100644@@ -0,0 +1,234 @@+package www++import (+ "context"+ "errors"+ "fmt"+ "io"+ "log"+ "net"+ "net/http"+ "os"+ "os/signal"+ "strings"+ "syscall"+ "time"+)++// publicUsage is the public executable's usage, in docopt notation as+// usageText is. TestUsageIsDocopt checks it too.+var publicUsage = strings.NewReplacer("@NAME@", publicName, "@ENV@", envPrefix).Replace(`Usage:+ @NAME@ --disk=SIZE (-a ADDR | --share[=HOST]) [--debug] [--cache=SIZE]+ @NAME@ -h | --help+ @NAME@ -V | --version++Options:+ -a ADDR, --addr=ADDR serve on this loopback address, IP or IP:PORT, eg+ behind a proxy+ --share[=HOST] serve on the address of the default route, or on+ HOST, an address or name beyond loopback with an+ optional :PORT, eg 0.0.0.0; a bare --share takes+ @ENV@SHARE when it is set+ --disk=SIZE disk the server may fill with online repos, eg 6G;+ it drops the repos read longest ago+ --cache=SIZE memory the server may spend on cached git answers,+ eg 64M; 0 caches nothing [default: 32M]+ --debug show above each page's header, small and faint,+ the binary's build time, the seconds since, its+ commit and the page's render time+ -V, --version print when the binary was built and its commit+ -h, --help print this help++It serves only repos fetched from a forge, never a tree here, in the+foreground, to anyone who reaches the address. It lists no cache, shows no+stop or bug button and opens nothing.+`)++// Public runs the public executable on the process's arguments and exits.+func Public() {+ c, err := parsePublicArgs(os.Args[1:])+ if err != nil {+ noteRun()+ code := failNow(errnoOf(err), err.Error())+ io.WriteString(os.Stderr, publicUsage)+ os.Exit(code)+ }+ if lowPortOK() {+ defaultPort = 80+ }+ if c.help {+ io.WriteString(os.Stdout, publicUsage+logHelp())+ os.Exit(0)+ }+ if c.version {+ fmt.Println(versionLine(built, buildCommit))+ os.Exit(0)+ }+ ep, err := endpointOf(c, os.Getuid())+ if err != nil {+ noteRun()+ os.Exit(failNow(errnoOf(err), err.Error()))+ }+ c.ep = ep+ setLogEndpoint(endpointName(ep))+ noteRun()+ os.Exit(servePublic(c))+}++// parsePublicArgs reads the public executable's options as parseArgs+// reads the reader's: the GNU way, values joined or apart.+func parsePublicArgs(args []string) (config, error) {+ c := config{public: true}+ value := func(i *int, opt, joined string, has bool) (string, error) {+ switch {+ case joined != "":+ return joined, nil+ case has || *i+1 == len(args):+ return "", &errno{syscall.EINVAL, "option " + opt + " needs a value"}+ }+ *i+++ return args[*i], nil+ }+ for i := 0; i < len(args); i++ {+ a := args[i]+ switch {+ case a == "--":+ if i+1 < len(args) {+ return c, &errno{syscall.EINVAL, publicName + " takes no directory, got " + args[i+1]}+ }+ case strings.HasPrefix(a, "--"):+ name, val, has := strings.Cut(a[2:], "=")+ switch name {+ case "addr", "disk", "cache":+ v, err := value(&i, "--"+name, val, has)+ if err != nil {+ return c, err+ }+ if name == "addr" {+ c.addr = v+ continue+ }+ if _, err := parseSize(v); err != nil {+ return c, err+ }+ if name == "disk" {+ c.disk = v+ } else {+ c.cache = v+ }+ case "share":+ if has && val == "" {+ return c, &errno{syscall.EINVAL, "option --share= needs an address"}+ }+ c.sharing, c.share = true, val+ case "debug", "help", "version":+ if has {+ return c, &errno{syscall.EINVAL, "option --" + name + " takes no value"}+ }+ *map[string]*bool{"debug": &c.debug, "help": &c.help, "version": &c.version}[name] = true+ default:+ return c, &errno{syscall.EINVAL, "unknown option --" + name}+ }+ case len(a) > 1 && a[0] == '-':+ for j := 1; j < len(a); j++ {+ switch a[j] {+ case 'a':+ v, err := value(&i, "-a", a[j+1:], false)+ if err != nil {+ return c, err+ }+ c.addr = v+ j = len(a)+ case 'h':+ c.help = true+ case 'V':+ c.version = true+ default:+ return c, &errno{syscall.EINVAL, "unknown option -" + string(a[j])}+ }+ }+ default:+ return c, &errno{syscall.EINVAL, publicName + " takes no directory, got " + a}+ }+ }+ if c.help || c.version {+ return c, nil+ }+ if c.disk == "" {+ return c, &errno{syscall.EINVAL, publicName + " needs --disk, since any visitor can make it clone"}+ }+ // The default address is the user's own, where a reader's server runs.+ if c.addr == "" && !c.sharing {+ return c, &errno{syscall.EINVAL, publicName + " needs -a or --share"}+ }+ if c.addr != "" && c.sharing {+ return c, &errno{syscall.EINVAL, "-a and --share name one address each; give one"}+ }+ return c, nil+}++// servePublic serves online repos on c.ep until a signal, for strangers:+// no tree of the machine, no registry, no lock and no other address. A+// service manager keeps it running, so it never detaches. Its server is+// never made local, so the executable holds no stop form, bug form or+// index, and it prints its URL itself, since the reporter can open a+// browser.+func servePublic(c config) int {+ logInfo("public server starts on %s: %s", c.ep, build())+ go warmLexers()+ debugOn, bugOff = c.debug, true+ size := int64(defaultCache)+ if c.cache != "" {+ size, _ = parseSize(c.cache)+ }+ gitCache.reset(size)+ limit, _ := parseSize(c.disk)+ root, err := cacheDir()+ if err != nil {+ return failNow(errnoOf(err), "cannot find the cache: "+err.Error())+ }+ ln, err := net.Listen("tcp", c.ep.String())+ if err != nil {+ return failNow(errnoOf(err), "cannot listen on "+c.ep.String()+": "+err.Error())+ }+ ctx, cancel := context.WithCancel(context.Background())+ defer cancel()+ w, err := newWeb(ctx, root, limit)+ if err != nil {+ ln.Close()+ return failNow(errnoOf(err), "cannot serve online repos from "+root+": "+err.Error())+ }+ w.public, w.srv.public = true, true+ publicNet.Store(true)+ w.slots = make(chan struct{}, cloneSlots)+ w.srv.live = newLive()+ httpd := &http.Server{Handler: w.srv.handler(), ReadHeaderTimeout: 10 * time.Second,+ ErrorLog: log.New(logWriter{}, "", 0)}+ sigs := make(chan os.Signal, 1)+ signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)+ defer signal.Stop(sigs)+ why := make(chan string, 1)+ go func() {+ why <- signalName(<-sigs)+ // Live pages hold their streams open; closing them lets+ // Shutdown finish.+ w.srv.live.close()+ cancel()+ stop, done := context.WithTimeout(context.Background(), 5*time.Second)+ defer done()+ httpd.Shutdown(stop)+ }()+ logInfo("serves online repos at %s from %s, within %s", origin(c.ep), w.root, sizeText(limit))+ fmt.Println(origin(c.ep) + "/")+ for _, line := range firewallNote(c.ep) {+ fmt.Fprintln(os.Stderr, line)+ }+ if err := httpd.Serve(ln); !errors.Is(err, http.ErrServerClosed) {+ logErr("E%d: %s", syscall.EIO, err)+ fmt.Fprintf(os.Stderr, "E%d: %s\n", syscall.EIO, err)+ return int(syscall.EIO)+ }+ w.wg.Wait()+ logInfo("public server exits: %s", <-why)+ return 0+}
public_test.go
new file mode 100644@@ -0,0 +1,72 @@+package www++import (+ "os/exec"+ "path/filepath"+ "reflect"+ "strings"+ "testing"+)++// readerOnly names code of the features the public executable leaves out:+// the bug button, opening a browser, the stop buttons, the index of+// servers, --restart, -l, --kill, detaching, locks and the registry.+var readerOnly = []string{+ "(*server).handleBug",+ "fileBug",+ "openBrowser",+ "(*server).handleStop",+ "(*server).serveIndex",+ "restart",+ "restartServer",+ "list",+ "kill",+ "detach",+ "createLock",+ "openRegistry",+ "(*hub).adopt",+ "persistedTrees",+ "parseArgs",+}++// The public executable does not contain the reader's features, so no+// flag or request can reach them: the linker leaves out code that no call+// leads to. The reader's executable holds every name in the list, which+// shows that the list names real code.+func TestPublicLeavesOutTheReadersFeatures(t *testing.T) {+ if _, err := exec.LookPath("go"); err != nil {+ t.Skip("go is not installed")+ }+ // Symbols carry the package's import path, which the name test keeps+ // out of string literals.+ pkg := reflect.TypeOf(server{}).PkgPath()+ symbols := func(cmd string) map[string]bool {+ bin := filepath.Join(t.TempDir(), cmd)+ if out, err := exec.Command("go", "build", "-o", bin, "./cmd/"+cmd).CombinedOutput(); err != nil {+ t.Fatalf("go build ./cmd/%s: %v\n%s", cmd, err, out)+ }+ out, err := exec.Command("go", "tool", "nm", bin).Output()+ if err != nil {+ t.Fatalf("go tool nm: %v", err)+ }+ got := map[string]bool{}+ for _, line := range strings.Split(string(out), "\n") {+ if f := strings.Fields(line); len(f) == 3 {+ got[f[2]] = true+ }+ }+ return got+ }+ local, public := symbols("local"), symbols("public")+ if !public[pkg+".servePublic"] {+ t.Fatalf("the public executable lacks %s.servePublic, so its symbols were not read", pkg)+ }+ for _, name := range readerOnly {+ if !local[pkg+"."+name] {+ t.Errorf("the reader's executable has no %s; the list is out of date", name)+ }+ if public[pkg+"."+name] {+ t.Errorf("the public executable contains %s", name)+ }+ }+}
serve.go
@@ -220,18 +220,29 @@ func originHost(host string, port uint16) string { // this machine passes through loopback, so only a capture tells. func report(rep *reporter, ep netip.AddrPort, url string) { rep.send("url", url)- if shared(ep) {- port := strconv.Itoa(int(ep.Port()))- dest := ep.String()- if ep.Addr().IsUnspecified() {- dest = "port " + port- }- rep.send("note", "note: other machines connect to "+dest+"; a firewall here may drop them unseen.")- rep.send("note", " Check with: sudo tcpdump -ni "+ifaceOf(ep.Addr())+" tcp port "+port)+ for _, line := range firewallNote(ep) {+ rep.send("note", line) } rep.finish() } +// firewallNote is report's note for a server beyond loopback, and nothing+// for one on loopback.+func firewallNote(ep netip.AddrPort) []string {+ if !shared(ep) {+ return nil+ }+ port := strconv.Itoa(int(ep.Port()))+ dest := ep.String()+ if ep.Addr().IsUnspecified() {+ dest = "port " + port+ }+ return []string{+ "note: other machines connect to " + dest + "; a firewall here may drop them unseen.",+ " Check with: sudo tcpdump -ni " + ifaceOf(ep.Addr()) + " tcp port " + port,+ }+}+ // ifaceOf names the interface that holds a, or "any" for 0.0.0.0 or an // address no interface holds. func ifaceOf(a netip.Addr) string {@@ -261,7 +272,7 @@ func serve(c config, rep *reporter, ln net.Listener, reg *registry, dir, home, o size, _ = parseSize(c.cache) } gitCache.reset(size)- h := &hub{ep: c.ep, home: home, reg: reg, ln: ln, srv: newServer(), w: newWatcher(lockMask),+ h := &hub{ep: c.ep, home: home, reg: reg, ln: ln, srv: newServer().local(), w: newWatcher(lockMask), done: make(chan struct{}), fresh: map[net.Conn]bool{}} defer h.w.close() defer reg.close()@@ -394,7 +405,7 @@ func (h *hub) startWeb(c config, ep netip.AddrPort) { logErr("E%d: cannot serve online repos from %s: %v", errnoOf(err), root, err) return }- w.srv.live = newLive()+ w.srv.local().live = newLive() h.web, h.webLn, h.webStop = w, ln, cancel h.webHTTPD = &http.Server{Handler: w.srv.handler(), ReadHeaderTimeout: 10 * time.Second, ConnState: h.track, ErrorLog: log.New(logWriter{}, "", 0)}@@ -404,67 +415,6 @@ func (h *hub) startWeb(c config, ep netip.AddrPort) { logInfo("serves online repos at %s from %s, within %s", origin(ep), w.root, sizeText(limit)) } -// servePublic serves online repos on c.ep until a signal, for strangers:-// no tree of the machine, no registry, no lock and no other address. A-// service manager keeps it running, so it never detaches.-func servePublic(c config) int {- rep := &reporter{noopen: true}- logInfo("public server starts on %s: %s", c.ep, build())- go warmLexers()- debugOn, bugOff = c.debug, true- size := int64(defaultCache)- if c.cache != "" {- size, _ = parseSize(c.cache)- }- gitCache.reset(size)- limit, _ := parseSize(c.disk)- root, err := cacheDir()- if err != nil {- return rep.fail(errnoOf(err), "cannot find the cache: "+err.Error())- }- ln, err := net.Listen("tcp", c.ep.String())- if err != nil {- return rep.fail(errnoOf(err), "cannot listen on "+c.ep.String()+": "+err.Error())- }- ctx, cancel := context.WithCancel(context.Background())- defer cancel()- w, err := newWeb(ctx, root, limit)- if err != nil {- ln.Close()- return rep.fail(errnoOf(err), "cannot serve online repos from "+root+": "+err.Error())- }- w.public, w.srv.public = true, true- publicNet.Store(true)- w.slots = make(chan struct{}, cloneSlots)- w.srv.live = newLive()- httpd := &http.Server{Handler: w.srv.handler(), ReadHeaderTimeout: 10 * time.Second,- ErrorLog: log.New(logWriter{}, "", 0)}- sigs := make(chan os.Signal, 1)- signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)- defer signal.Stop(sigs)- why := make(chan string, 1)- go func() {- why <- signalName(<-sigs)- // Live pages hold their streams open; closing them lets- // Shutdown finish.- w.srv.live.close()- cancel()- stop, done := context.WithTimeout(context.Background(), 5*time.Second)- defer done()- httpd.Shutdown(stop)- }()- logInfo("serves online repos at %s from %s, within %s", origin(c.ep), w.root, sizeText(limit))- report(rep, c.ep, origin(c.ep)+"/")- if err := httpd.Serve(ln); !errors.Is(err, http.ErrServerClosed) {- logErr("E%d: %s", syscall.EIO, err)- fmt.Fprintf(os.Stderr, "E%d: %s\n", syscall.EIO, err)- return int(syscall.EIO)- }- w.wg.Wait()- logInfo("public server exits: %s", <-why)- return 0-}- // track keeps fresh up to date. func (h *hub) track(c net.Conn, state http.ConnState) { h.freshMu.Lock()
server.go
@@ -76,12 +76,24 @@ type server struct { // homeURL is, on the web address, the default address's URL, which // its index links back to. homeURL string+ // What only the reader's executable serves, set by local and nil in+ // the public executable, which so does not contain their code: the+ // stop buttons' form, the bug button's form and the index at /.+ stopForm http.HandlerFunc+ bugForm http.HandlerFunc+ index func(w http.ResponseWriter, r *http.Request, status int, msg string) } func newServer() *server { return &server{stop: func([]*tree, string) {}} } +// local gives s what only the reader's executable serves.+func (s *server) local() *server {+ s.stopForm, s.bugForm, s.index = s.handleStop, s.handleBug, s.serveIndex+ return s+}+ // tree is one served directory, exposed with everything below it. type tree struct { dir string@@ -223,12 +235,12 @@ func (s *server) handler() http.Handler { }) // A shared server has no buttons: no visitor may stop it. The web // address has none either; its server stops with the default address.- if !s.shared && s.web == nil {- mux.HandleFunc("POST "+ownPath+"stop", s.handleStop)+ if s.stopForm != nil && !s.shared && s.web == nil {+ mux.HandleFunc("POST "+ownPath+"stop", s.stopForm) } // The bug button is on every page but a share's, unless --no-bug.- if !bugOff && !s.shared {- mux.HandleFunc("POST "+ownPath+"bug", s.handleBug)+ if s.bugForm != nil && !bugOff && !s.shared {+ mux.HandleFunc("POST "+ownPath+"bug", s.bugForm) } mux.HandleFunc("GET "+ownPath+"live", s.handleLive) mux.HandleFunc("GET /", s.handlePath)@@ -327,8 +339,8 @@ func (s *server) handlePath(w http.ResponseWriter, r *http.Request) { case s.byPrefix(r.URL.Path+"/") != nil: // Temporary: the tree may be withdrawn and another served here. http.Redirect(w, r, s.byPrefix(r.URL.Path+"/").prefixURL(), http.StatusFound)- case r.URL.Path == "/":- s.serveIndex(w, r, http.StatusOK, "")+ case r.URL.Path == "/" && s.index != nil:+ s.index(w, r, http.StatusOK, "") default: s.serveStray(w, r) }
server_test.go
@@ -31,7 +31,7 @@ func serveTree(t *testing.T) (*server, *tree, string) { } } os.Symlink("/etc", filepath.Join(dir, "escape"))- s := newServer()+ s := newServer().local().local() tr := addTree(t, s, dir, "/", filepath.Base(dir)) return s, tr, dir }
share_test.go
@@ -44,7 +44,7 @@ func shareTree(t *testing.T) *server { os.Chmod(d, 0o755) } os.Chmod(filepath.Join(dir, "shut"), 0o700)- s := newServer()+ s := newServer().local().local() s.shared = true for _, tc := range []struct{ dir, token string }{{dir, ""}, {hid, "s3cret"}} { prefix, label := shareMount(tc.dir, tc.token)
tests/harness.py
@@ -35,6 +35,14 @@ def binary(): return Path(path).resolve() +def public_binary():+ """The executable for the open web, which make blackbox builds beside TEST_BIN."""+ path = os.environ.get("TEST_PUBLIC_BIN")+ if not path:+ raise RuntimeError("TEST_PUBLIC_BIN must name the public binary; make blackbox sets it")+ return Path(path).resolve()++ def environment(home): """The whole environment of a run: nothing leaks in from the caller's.""" return {@@ -100,12 +108,12 @@ def free_port(): class Server: """One running binary. origin is where its repos answer.""" - def __init__(self, home, argv, origin, listens):+ def __init__(self, home, exe, argv, origin, listens): self.origin = origin self.home = home self.log = home / "stderr" self.proc = subprocess.Popen(- [str(binary()), *argv],+ [str(exe), *argv], cwd=home, env=environment(home), stdin=subprocess.DEVNULL,@@ -144,7 +152,7 @@ class Server: self.proc.wait() -# public: the flag the README names for the open web.+# public: the executable the README names for the open web. # default: a reader's server, as wwwvm's unit runs it, whose online repos # answer on the .2 beside the user's default address. def start(form, home):@@ -154,7 +162,7 @@ def start(form, home): seed(cache, key, files) port = free_port() if form == "public":- return Server(home, ["--public", "--disk=1G", "-a", f"127.0.0.1:{port}"],+ return Server(home, public_binary(), ["--disk=1G", "-a", f"127.0.0.1:{port}"], f"http://127.0.0.1:{port}", ("127.0.0.1", port)) if form == "default": n = os.getuid() - 1000@@ -162,6 +170,6 @@ def start(form, home): raise OSError(f"uid {os.getuid()} has no default address") tree = home / "tree" tree.mkdir()- return Server(home, ["--foreground", "--no-open", "--no-bug", "--disk=1G", "-a", f"127.31.{n}.1:{port}", str(tree)],+ return Server(home, binary(), ["--foreground", "--no-open", "--no-bug", "--disk=1G", "-a", f"127.31.{n}.1:{port}", str(tree)], f"http://127.31.{n}.2:{port}", (f"127.31.{n}.2", port)) raise ValueError(form)