oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package webread
2
3import (
4 "context"
5 "errors"
6 "fmt"
7 "io"
8 "net"
9 "net/netip"
10 "slices"
11 "time"
12)
13
14// ErrUnsupported is Launch where the fetcher cannot run (not Linux).
15var ErrUnsupported = errors.New("webread: the fetcher runs only on Linux")
16
17// DefaultCABundle is the CA bundle the fetcher's sandbox binds by default.
18const DefaultCABundle = "/etc/ssl/certs/ca-certificates.crt"
19
20// LaunchConfig is how the runner starts the fetcher.
21type LaunchConfig struct {
22 // Bwrap is the pinned bubblewrap; Binary the runner's own executable,
23 // which carries the webread subcommand.
24 Bwrap, Binary string
25 // OutDir is an empty directory, mode 0700, owned by the runner and
26 // reached through no symlink. The fetcher writes the bundle there.
27 OutDir string
28 // CABundle and ResolvConf are bound read-only; empty means
29 // DefaultCABundle and /etc/resolv.conf. Symlinks are resolved on the
30 // host and the files they name are bound.
31 CABundle, ResolvConf string
32 // Timeout bounds the whole process; zero means OverallTimeout plus 30 s.
33 Timeout time.Duration
34 // Stderr receives the fetcher's diagnostics, bounded; nil discards them.
35 Stderr io.Writer
36}
37
38// Main is the fetcher process, `equipo-runner webread <outdir>`: it
39// limits itself, reads the plan on stdin and writes the bundle. It never
40// starts another program. Exit 0 is a bundle written, 1 a failure while
41// fetching or writing, 2 refused input.
42func Main(args []string, stdin io.Reader, stderr io.Writer) int {
43 return mainWith(args, stdin, stderr, defaults(), limitSelf)
44}
45
46func mainWith(args []string, stdin io.Reader, stderr io.Writer, d deps, limit func() error) int {
47 if len(args) != 1 {
48 fmt.Fprintln(stderr, "webread: usage: webread <outdir>")
49 return 2
50 }
51 if err := limit(); err != nil {
52 fmt.Fprintln(stderr, "webread: limits:", err)
53 return 2
54 }
55 p, err := DecodePlan(stdin)
56 if err != nil {
57 fmt.Fprintln(stderr, err)
58 return 2
59 }
60 if _, err = fetch(context.Background(), p, args[0], d); err != nil {
61 fmt.Fprintln(stderr, err)
62 return 1
63 }
64 return 0
65}
66
67// LocalAddresses is every global unicast address on this machine's
68// interfaces, for the site inventory. An error means the list is
69// unknown, never that it is empty.
70func LocalAddresses() ([]netip.Addr, error) { return localAddresses(net.InterfaceAddrs) }
71
72func localAddresses(list func() ([]net.Addr, error)) ([]netip.Addr, error) {
73 addrs, err := list()
74 if err != nil {
75 return nil, err
76 }
77 var out []netip.Addr
78 for _, a := range addrs {
79 n, ok := a.(*net.IPNet)
80 if !ok {
81 continue
82 }
83 ip, ok := netip.AddrFromSlice(n.IP)
84 if ip = ip.Unmap(); !ok || !ip.IsGlobalUnicast() || slices.Contains(out, ip) {
85 continue
86 }
87 out = append(out, ip)
88 }
89 slices.SortFunc(out, func(a, b netip.Addr) int { return a.Compare(b) })
90 return out, nil
91}
92