package ui import ( "context" "errors" "fmt" "sort" "time" "100gex/internal/hosts" "100gex/internal/rex" "100gex/internal/sshtunnel" "100gex/internal/tailnet" ) // Hosts and tailnet machines, for the server chip's card and the palette. // localTarget is what reaches the local server. const localTarget = "" // hostItems lists the hosts to switch to: this machine, then the saved // hosts, then a way to add one. The server shown, reached by target and // called label, is marked current. func hostItems(saved []rex.Host, target, label string) []paletteItem { items := []paletteItem{{ title: "This machine", detail: "local", host: true, target: localTarget, current: target == localTarget, }} sort.Slice(saved, func(i, j int) bool { return saved[i].Label < saved[j].Label }) for _, h := range saved { detail := "" if len(h.Endpoints) > 0 { detail = h.Endpoints[0] } items = append(items, paletteItem{ title: h.Label, detail: detail, host: true, target: h.Label, current: label != "" && label == h.Label, }) } return append(items, paletteItem{title: "Add host…", detail: "from your tailnet", action: "client.host.add"}) } // machineItems lists the tailnet's machines: this one, then those online, // then the rest, each by name. func machineItems(ms []tailnet.Machine) []paletteItem { sort.SliceStable(ms, func(i, j int) bool { rank := func(m tailnet.Machine) int { switch { case m.ThisMachine: return 0 case m.Online: return 1 } return 2 } return rank(ms[i]) < rank(ms[j]) }) items := make([]paletteItem, 0, len(ms)) for i := range ms { m := ms[i] detail := m.OSName() switch { case m.ThisMachine: detail = "this machine" case !m.Online: detail += " · offline" } items = append(items, paletteItem{title: m.Name, detail: detail, machine: &m}) } return items } // loadPalette fetches what a host or machine list shows, in the // background, and hands it to the palette. func (u *UI) loadPalette(mode paletteMode) { target, label := u.srv.target, u.srv.label // read here, on the drawing goroutine go func() { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() var items []paletteItem var err error switch mode { case paletteHosts: var saved []rex.Host saved, err = hosts.Saved(ctx) if err != nil { // Without the local server there are no saved hosts, // but switching back to it, or adding one, still works. err = nil } items = hostItems(saved, target, label) case paletteMachines: var ms []tailnet.Machine ms, err = tailnet.Machines(ctx) items = machineItems(ms) } u.mu.Lock() u.paletteLoaded = &paletteLoad{mode: mode, items: items, err: err} u.mu.Unlock() u.notify() }() } // paletteLoad is a list loaded for the palette. type paletteLoad struct { mode paletteMode items []paletteItem err error } // addMachine looks for a Rex server on a tailnet machine: listening on the // tailnet, or failing that, over ssh, as the person's own server there. If // one answers, it saves the machine as a host, named as the tailnet names // it, at the address that worked, and switches to it. The palette says // what's happening meanwhile. func (u *UI) addMachine(m tailnet.Machine) { if m.ThisMachine { u.palette.close() u.Connect(localTarget) return } u.palette.status, u.palette.statusErr = fmt.Sprintf("Looking for Rex on %s…", m.Name), false go func() { say := func(s *paletteStatus) { u.mu.Lock() u.paletteStatus = s u.mu.Unlock() u.notify() } _, err := tailnet.Probe(context.Background(), m.Address) if err == nil { say(&paletteStatus{done: true, machine: m, address: m.Address}) return } var forbidden *rex.ForbiddenError switch { case !m.Online: say(&paletteStatus{text: fmt.Sprintf("%s is offline.", m.Name), err: true}) return case errors.As(err, &forbidden): say(&paletteStatus{text: fmt.Sprintf("%s's Rex server didn't let this device in: %s.", m.Name, forbidden.Reason), err: true}) return } say(&paletteStatus{text: fmt.Sprintf("Nothing listening on %s; trying ssh…", m.Name)}) address := "ssh://" + m.Address if sshErr := probeSSH(address); sshErr != nil { say(&paletteStatus{text: fmt.Sprintf("No Rex server answered on %s, and over ssh: %v", m.Name, sshErr), err: true}) return } say(&paletteStatus{done: true, machine: m, address: address}) }() } // probeSSH checks that a Rex server answers over ssh, starting it if need // be, as connecting would. func probeSSH(address string) error { ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() t, err := sshtunnel.Open(ctx, address) if err != nil { return err } defer t.Close() c, err := rex.Dial(ctx, t.Endpoint, rex.ClientInfo{Kind: "app", Name: "100gex (looking for Rex)"}) if err != nil { return t.Explain(err) } return c.Close() } // paletteStatus is news for the palette from a goroutine. type paletteStatus struct { text string err bool done bool // the machine has a Rex server: save it and switch machine tailnet.Machine // the machine, when done address string // what reached its server, when done } // finishAddMachine saves a machine found to run Rex, and switches to it. func (u *UI) finishAddMachine(m tailnet.Machine, address string) { u.palette.close() target := address go func() { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() if err := hosts.Save(ctx, m.Name, address); err != nil { u.report(fmt.Errorf("save host %s: %w", m.Name, err)) } }() u.Connect(target) u.mu.Lock() if u.connecting != nil { u.connecting.label = m.Name } u.mu.Unlock() }