Files
w3cjandClaude Opus 5.5 aeea3135b2 feat(proxy): the proxy's broker and its child
A VM's proxy runs as two processes, as design.md's "The proxy's broker"
says: devvm _proxy, the broker, which keeps what only the host should
do, and devvm _proxy-child, which reads the VM's network and can be
confined by the follow-ups.

- wire.go: the protocol over one socketpair (close-on-exec from the
  start: SOCK_CLOEXEC on Linux, ForkLock elsewhere), length-prefixed JSON
  frames of at most 2 MiB, unknown fields refused, bytes as base64, one
  descriptor on a frame's first bytes (SCM_RIGHTS, MSG_CTRUNC breaks the
  rules), wire types for every secret (redact.Secret at once).
- broker.go: newBroker, the start (handshake: version 1 and each side's
  executable's SHA-256, within 4 s), the answers (value through lookups,
  kept 30 s or 5 min, a failure 5 s, dropped when the bindings change;
  file by number; link connected by the broker and passed as a
  descriptor; token exchanged by the broker), each by something in its
  own config, with caps 1/8/4/256; live: the lifeline closes the socket
  and kills the child 6 s later, SIGTERM stops it within a second, a
  child that ends ends the broker, a rule broken stops the child.
- child.go: the start (startOf), the child's server (newChildServer),
  the broker as hostSide over the socket (brokerClient), runChildOn;
  ready only after it reads the host's addresses (kept: the last good
  read), roots and time zone loaded first.
- process.go: Main(ctx, ...) and ChildMain; the child spawned with
  /proc/self/exe on Linux, PR_SET_PDEATHSIG from a locked thread
  (daemon.Cmd.DieWithThread), descriptors 3 and 4, and TZ, SSL_CERT_*
  and GODEBUG with updatemaxprocs=0 alone; run/proxy-child.pid and
  run/proxy.confined ("none" until the follow-ups); KeepLogs in the
  broker. ChildMain refuses to start unless descriptors 3 and 4 are a
  unix socket and an IP one (checkInherited): run by hand, without
  them, those numbers can be the Go runtime's own, which closing
  crashed it (signal_recv: inconsistent state).
- capture.go: a captured token request goes to the broker
  (captureToken, exchanger), so a login's real tokens never enter the
  child; an error reply passes up to 1 MiB, longer gets 502.
- watched.go: the child stats each changing file and asks the broker
  for it once it changed, keeping the stamp of what the broker read.
- rotate.go: proxy.log rotated by both through its descriptor, under a
  lock of a description of its own; OpenLog opens a log never through a
  link, one link, yours, replacing what a child left in its place.
- nixcache.go: the trim and its count through a safefile.Root, nar/
  only as a folder, links deleted as themselves; reads never through a
  link; the lock tried for 5 s (ErrNixLocked, nix_cache_locked).
- daemon: Cmd.Err, Files, Env, DieWithThread; Handle.Kill, Exit;
  WritePID.
- netpolicy: Proxy.Stop also stops a child that outlived its broker
  (run/proxy-child.pid, _proxy-child -for); proxy.log opened with
  OpenLog; a refused launch keeps a running VM only while both run.
  devvm network log and lock read the event log through safefile.
- vm, vmssh, vmlog: links.log and ports.log opened with OpenLog, read
  through safefile, trimmed only as a regular file with one link.
- backend: the build proxy's logs in a logs/ folder of their own.
- cli: _proxy-child; _proxy waits for its child on a signal
  (StopsItsOwn); the child reads none of devvm's folders (no
  makePrivate); ui.Files.
- Tests: FuzzBrokerFrames, the frames' rules, the handshake and the
  pair's end in synctest bubbles, the caps, the broker naming its own
  alone, the client over a real socketpair with a passed descriptor,
  ChildMain, TestInheritedSocket and proxy.txtar's child run by hand,
  the frozen 0.1.0 configs starting a broker and child,
  two writers rotating once, planted links, FIFOs and hard links; the
  testscript runner stops _proxy-child too; e2e: broker and child, their
  arguments, descriptors and environment, stop, SIGKILL either, planted
  files in the logs folder and the Nix cache, build proxies gone.
- Review fixes: the lifeline closes the socket and starts the kill's
  timer before logging; every log lock (proxy.log's, the event log's
  rotation) is tried without blocking for 200 ms (flockWithin, which the
  Nix lock shares), then written without or the rotation skipped; a
  copied log's rotation streams at most twice its limit from a line's
  start (copyFrom, copyAtomic); a reply too long to send goes as
  too_long; kept token headers capped at 1 KiB; the child gives up on a
  request whose context ended, dropping its late reply and holding its
  slot until then; a write deadline on the start frame; errors reach the
  child as codes (not_found, refused, busy, too_long, failed), their
  text in the broker's log; FuzzFrameStreams, and FuzzBrokerFrames
  writes from a goroutine; nix_cache_locked links to How it works.
- Docs: processes, network, overview, design (with threat rows for the
  logs' locks and a log made huge), troubleshooting, the proxy's
  README, structure, testing; CHANGELOG.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-15 22:04:32 -07:00

295 lines
12 KiB
Go

package proxy
import (
"context"
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"log"
"net"
"net/http"
"net/http/httputil"
"net/netip"
"path/filepath"
"slices"
"strconv"
"strings"
"syscall"
"time"
"github.com/devvmhq/devvm/internal/vmname"
)
// --- links between VMs ---
//
// The proxy is a second entrance to the VM's links (Config.Links): a request for one of their names and
// ports, CONNECT db:5432 or GET http://db:5432/, goes to that link's socket on the host, which
// devvm _links serves as it serves the link's remote forward, checking each connection there. The
// proxy checks the list devvm _links writes whenever the links it carries change on every request,
// before any rule, and reads it again when it has changed (watched): a host and port on it match exactly, after splitHostPort's one normalization
// (lower case, one trailing dot), and the list names each link's short name and its long one
// (NAME.devvm.internal). Its bytes pass as they are: no TLS check or interception, no secret put
// in, no rule asked, and no event of the proxy's own, as devvm _links logs the connection. A
// request names a host and a port, never a socket: the socket is the one the link sockets' folder
// (Config.LinkSockets, devvm's run folder) has for this VM's link at that address and port
// (LinkSocket), so a list holding anything else is ignored, and no request reaches another VM's
// link or any other socket. Without that folder, no link is reached.
// links is a proxy's way to the VM's links: set once, as it starts, and only read after.
type links struct {
file string // Config.Links: the VM's links list, which devvm _links writes; empty: none
list *watched[LinkList] // file's list; a list that can't be read has none, logged once
sockets string // Config.LinkSockets: the folder of the links' sockets; empty: none
vm vmname.VM // Config.VM, whose links' sockets they are
log *log.Logger
events *events
// dial connects to the link that host and port name, e its entry: to its socket, in this
// process (dialEntry), or through the broker, which finds it again (the child's).
dial func(ctx context.Context, host string, port int, e LinkEntry) (net.Conn, error)
}
// newLinks is the links in file, read by this process, whose sockets it connects to itself.
func newLinks(file, sockets string, vm vmname.VM, l *log.Logger, ev *events) links {
return newLinksOf(watchedFile{watchLinks, file}.local(), sockets, vm, l, ev, dialEntry)
}
// newLinksOf is the links in src, connected to with dial.
func newLinksOf(src watchSource, sockets string, vm vmname.VM, l *log.Logger, ev *events,
dial func(ctx context.Context, host string, port int, e LinkEntry) (net.Conn, error),
) links {
return links{
file: src.path, sockets: sockets, vm: vm, log: l, events: ev, dial: dial,
list: &watched[LinkList]{
path: src.path, read: src.read,
parse: func(b []byte) (LinkList, error) { return parseLinkList(src.path, b) },
onFail: func(err error) { l.Printf("links: %v", err) },
},
}
}
// dialEntry connects to e's socket (dialLink), which find checked is the link's.
func dialEntry(ctx context.Context, _ string, _ int, e LinkEntry) (net.Conn, error) {
return dialLink(ctx, e.Socket)
}
// LinkList is the file devvm _links writes for the VM's proxy: the links it carries now.
type LinkList struct {
Version int `json:"version"` // LinkListVersion
Links []LinkEntry `json:"links"`
}
// LinkListVersion is the list's version: a list of another isn't read.
const LinkListVersion = 1
// LinkEntry is one link the VM's proxy may reach: its names, its address and port in the VM, its
// target (for the log) and its socket on the host (LinkSocket).
type LinkEntry struct {
Hosts []string `json:"hosts"`
Addr string `json:"addr"`
Port int `json:"port"`
Target vmname.VM `json:"target"`
Socket string `json:"socket"`
}
// linkListMax bounds the list the proxy reads.
const linkListMax = 1 << 20
// LinkSocket is the socket on the host of VM vm's link at addr and port in it, in dir, devvm's run
// folder: named by a hash of the three, as a socket's path has fewer than 104 characters.
func LinkSocket(dir string, vm vmname.VM, addr netip.Addr, port int) string {
return filepath.Join(dir, "link-"+LinkHash(vm, "socket", addr.String(), strconv.Itoa(port))+".sock")
}
// LinkHash names one of a link session's files: the first 16 hex digits of the SHA-256 of the VM's
// name and parts, each after a NUL.
func LinkHash(vm vmname.VM, parts ...string) string {
sum := sha256.Sum256([]byte(strings.Join(append([]string{vm.String()}, parts...), "\x00")))
return hex.EncodeToString(sum[:])[:16]
}
// parseLinkList is the list b, read from path, of LinkListVersion.
func parseLinkList(path string, b []byte) (LinkList, error) {
var l LinkList
if err := json.Unmarshal(b, &l); err != nil {
return LinkList{}, fmt.Errorf("%s: %w", path, err)
}
if l.Version != LinkListVersion {
return LinkList{}, fmt.Errorf("%s: version %d", path, l.Version)
}
return l, nil
}
// find returns the VM's link that host and port name, from the list as it is now: its entry,
// with the socket it must have. A host and port on no entry, an entry whose socket isn't this VM's
// link's, and a list that can't be read give none, so the request is the policy's.
func (k links) find(host string, port int) (LinkEntry, bool) {
if k.file == "" || k.sockets == "" {
return LinkEntry{}, false
}
l := k.list.get()
for _, e := range l.Links {
if e.Port != port || !slices.Contains(e.Hosts, host) {
continue
}
a, err := netip.ParseAddr(e.Addr)
if err != nil || a.String() != e.Addr || !IsLinkAddr(a) || a != a.Unmap() || port < 1 || port > 65535 ||
e.Socket != LinkSocket(k.sockets, k.vm, a, port) {
k.log.Printf("links: %s:%d: not this VM's link's socket", host, port)
continue
}
return e, true
}
return LinkEntry{}, false
}
// linkNetA and linkNetB are the first two bytes of 127.77.0.0/16, where every link's address in
// its client is.
const linkNetA, linkNetB = 127, 77
// IsLinkAddr reports whether a is in 127.77.0.0/16, the links' addresses in a client, as itself
// or mapped into IPv6.
func IsLinkAddr(a netip.Addr) bool {
a = a.Unmap()
b := a.As16()
return a.Is4() && b[12] == linkNetA && b[13] == linkNetB
}
// LocalProxyAddr and LocalProxyPort are where a VM's local proxy listens in the VM, on its loopback:
// devvm-local-proxy, which the guest setup writes, adds the VM's password to each request and sends
// it here, for programs that ask for a proxy's password instead of taking it from HTTP_PROXY, such
// as the desktop's browsers. The address is devvm's alone, so no port forwarder puts the local
// proxy on the host (IsLocalProxyAddr).
const (
LocalProxyAddr = "127.0.77.1"
LocalProxyPort = 3128
)
// IsLocalProxyAddr reports whether a is LocalProxyAddr, as itself or mapped into IPv6.
func IsLocalProxyAddr(a netip.Addr) bool {
return a.Unmap() == netip.MustParseAddr(LocalProxyAddr)
}
// LinkAddr is the address i in 127.77.0.0/16.
func LinkAddr(i uint16) netip.Addr {
b := [4]byte{linkNetA, linkNetB}
binary.BigEndian.PutUint16(b[2:], i)
return netip.AddrFrom4(b)
}
// LinkDomain is the domain of the links' long names: a client reaches each of its links' targets
// as NAME.devvm.internal besides NAME. .internal is for private use, so it names no host on the
// internet, and a host under LinkDomain is a link's or nothing.
const LinkDomain = "devvm.internal"
// isLinkDomain reports whether host is under LinkDomain, or LinkDomain itself: a link's name, or
// one that's nothing's, which is never the policy's, so no rule or company proxy sends it anywhere.
func isLinkDomain(host string) bool {
return strings.HasSuffix(host, "."+LinkDomain) || host == LinkDomain
}
// reasonNotLink is the reason of a refused request for a name under LinkDomain that's none of the
// VM's links.
const reasonNotLink = "not one of this VM's links"
// refuse refuses e, a request for a host under LinkDomain that's none of the VM's links
// (isLinkDomain), its reason reasonNotLink: logged as a refused block, and 403.
func (k links) refuse(w http.ResponseWriter, e Event) {
e.Event, e.Reason, e.Refused = "blocked", reasonNotLink, true
k.events.record(e)
http.Error(w, "devvm proxy: "+net.JoinHostPort(e.Host, strconv.Itoa(e.Port))+" isn't one of this VM's links", http.StatusForbidden)
}
// linkDialTimeout bounds how long the proxy waits for a link's socket.
const linkDialTimeout = 10 * time.Second
// dialLink connects to a link's socket. A socket whose backlog is full (a burst of connections
// while devvm _links starts their relays) refuses a connection, or asks for another try, so a few
// more are made, for under a second, as a socket nothing listens on refuses too.
func dialLink(ctx context.Context, sock string) (net.Conn, error) {
d := net.Dialer{Timeout: linkDialTimeout}
wait := 10 * time.Millisecond
for i := 0; ; i++ {
c, err := d.DialContext(ctx, "unix", sock)
if err == nil || i == 6 || !errors.Is(err, syscall.ECONNREFUSED) && !errors.Is(err, syscall.EAGAIN) {
return c, err
}
select {
case <-ctx.Done():
return nil, err
case <-time.After(wait):
}
wait *= 2
}
}
// gone answers a request for a link whose socket doesn't answer (its link removed since the
// list was read, or devvm _links gone) and logs it as devvm _links logs a refusal.
func (k links) gone(w http.ResponseWriter, host string, port int, e LinkEntry, err error) {
k.log.Printf("links: %s:%d: %v", host, port, err)
k.events.record(Event{Event: "link_refused", Host: host, Port: port, Target: e.Target, Reason: linkNotCarried})
http.Error(w, "devvm proxy: the link "+net.JoinHostPort(host, strconv.Itoa(port))+" isn't carried now", http.StatusBadGateway)
}
// linkNotCarried is the reason of a refusal the proxy logs: the link's socket didn't answer.
const linkNotCarried = "not carried"
// connect tunnels a CONNECT for a link to its socket, the bytes as they are.
func (k links) connect(w http.ResponseWriter, r *http.Request, host string, port int, e LinkEntry) {
up, err := k.dial(r.Context(), host, port, e)
if err != nil {
k.gone(w, host, port, e, err)
return
}
defer func() { _ = up.Close() }()
client, err := hijack(w)
if err != nil {
return
}
splice(client, up, nil)
}
// linkDialError: a link's socket didn't answer.
type linkDialError struct{ err error }
func (e linkDialError) Error() string { return e.err.Error() }
// forward forwards an absolute-form request for a link (plain HTTP) to its socket, a new
// connection for each request, so devvm _links checks each one, and none of the proxy's headers.
func (k links) forward(w http.ResponseWriter, r *http.Request, host string, port int, e LinkEntry) {
tr := &http.Transport{
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
c, err := k.dial(ctx, host, port, e)
if err != nil {
return nil, linkDialError{err}
}
return c, nil
},
DisableKeepAlives: true, DisableCompression: true,
}
defer tr.CloseIdleConnections()
rp := &httputil.ReverseProxy{
Transport: tr,
ErrorLog: k.log,
FlushInterval: -1,
Rewrite: func(pr *httputil.ProxyRequest) {
pr.Out.URL.Scheme, pr.Out.URL.Host = "http", r.URL.Host
pr.Out.Header.Del("Proxy-Authorization")
pr.Out.Header.Del("Proxy-Connection")
},
ErrorHandler: func(w http.ResponseWriter, _ *http.Request, err error) {
if de := (linkDialError{}); errors.As(err, &de) {
k.gone(w, host, port, e, de.err)
return
}
k.log.Printf("links: %s:%d: %v", host, port, err)
http.Error(w, "devvm proxy: the link "+net.JoinHostPort(host, strconv.Itoa(port))+" didn't answer", http.StatusBadGateway)
},
}
// The client's own context: a request it gave up on lets its link connection go, so a target
// that never answers can't hold the client's links' connections.
rp.ServeHTTP(w, r)
}