package webread import ( "context" "errors" "fmt" "io" "net" "net/netip" "slices" "time" ) // ErrUnsupported is Launch where the fetcher cannot run (not Linux). var ErrUnsupported = errors.New("webread: the fetcher runs only on Linux") // DefaultCABundle is the CA bundle the fetcher's sandbox binds by default. const DefaultCABundle = "/etc/ssl/certs/ca-certificates.crt" // LaunchConfig is how the runner starts the fetcher. type LaunchConfig struct { // Bwrap is the pinned bubblewrap; Binary the runner's own executable, // which carries the webread subcommand. Bwrap, Binary string // OutDir is an empty directory, mode 0700, owned by the runner and // reached through no symlink. The fetcher writes the bundle there. OutDir string // CABundle and ResolvConf are bound read-only; empty means // DefaultCABundle and /etc/resolv.conf. Symlinks are resolved on the // host and the files they name are bound. CABundle, ResolvConf string // Timeout bounds the whole process; zero means OverallTimeout plus 30 s. Timeout time.Duration // Stderr receives the fetcher's diagnostics, bounded; nil discards them. Stderr io.Writer } // Main is the fetcher process, `equipo-runner webread `: it // limits itself, reads the plan on stdin and writes the bundle. It never // starts another program. Exit 0 is a bundle written, 1 a failure while // fetching or writing, 2 refused input. func Main(args []string, stdin io.Reader, stderr io.Writer) int { return mainWith(args, stdin, stderr, defaults(), limitSelf) } func mainWith(args []string, stdin io.Reader, stderr io.Writer, d deps, limit func() error) int { if len(args) != 1 { fmt.Fprintln(stderr, "webread: usage: webread ") return 2 } if err := limit(); err != nil { fmt.Fprintln(stderr, "webread: limits:", err) return 2 } p, err := DecodePlan(stdin) if err != nil { fmt.Fprintln(stderr, err) return 2 } if _, err = fetch(context.Background(), p, args[0], d); err != nil { fmt.Fprintln(stderr, err) return 1 } return 0 } // LocalAddresses is every global unicast address on this machine's // interfaces, for the site inventory. An error means the list is // unknown, never that it is empty. func LocalAddresses() ([]netip.Addr, error) { return localAddresses(net.InterfaceAddrs) } func localAddresses(list func() ([]net.Addr, error)) ([]netip.Addr, error) { addrs, err := list() if err != nil { return nil, err } var out []netip.Addr for _, a := range addrs { n, ok := a.(*net.IPNet) if !ok { continue } ip, ok := netip.AddrFromSlice(n.IP) if ip = ip.Unmap(); !ok || !ip.IsGlobalUnicast() || slices.Contains(out, ip) { continue } out = append(out, ip) } slices.SortFunc(out, func(a, b netip.Addr) int { return a.Compare(b) }) return out, nil }