// Package hosts finds Rex servers by what people call them: the local // server, a host's label, as rex hosts lists them, or an address, the way // the rex CLI's -S option takes them. It also lists and saves hosts, which // the local server keeps. package hosts import ( "context" "errors" "fmt" "log" "time" "100gex/internal/rex" "100gex/internal/sshtunnel" ) // clientInfo is what this program tells servers about itself. var clientInfo = rex.ClientInfo{Kind: "app", Name: "100gex"} // Dial opens a control connection to target: "" for the local server, a // host's label, or an address, which may be an ssh:// address. A label is // looked up in the local server's hosts first, ignoring case; a value with a // port is always an address. A host's addresses are tried in turn. It // returns the endpoint that answered, and the host's label if a label named // it. func Dial(ctx context.Context, target string) (*rex.Conn, rex.Endpoint, string, error) { if target == "" { ep, err := rex.DiscoverEndpoint() if err != nil { return nil, rex.Endpoint{}, "", err } c, err := rex.Dial(ctx, ep, clientInfo) return c, ep, "", err } addresses := []string{target} label := "" if rex.LooksLikeLabel(target) { if saved, err := Saved(ctx); err == nil { if host, ok := rex.FindHost(saved, target); ok { label, addresses = host.Label, host.Endpoints } } } var errs []error for _, address := range addresses { c, ep, err := dialAddress(ctx, address) if err == nil { return c, ep, label, nil } if label != "" { log.Printf("host %s: %v", label, err) } errs = append(errs, err) var forbidden *rex.ForbiddenError if errors.As(err, &forbidden) || ctx.Err() != nil { break } } if len(errs) == 0 { errs = append(errs, errors.New("the host has no addresses")) } return nil, rex.Endpoint{}, "", fmt.Errorf("connect to %s: %w", target, errors.Join(errs...)) } // dialAddress connects to the server at one address. Over ssh, the tunnel // lasts as long as the connection. func dialAddress(ctx context.Context, address string) (*rex.Conn, rex.Endpoint, error) { if sshtunnel.Is(address) { t, err := sshtunnel.Open(ctx, address) if err != nil { return nil, rex.Endpoint{}, err } c, err := rex.Dial(ctx, t.Endpoint, clientInfo) if err != nil { err = t.Explain(err) t.Close() return nil, rex.Endpoint{}, err } c.OnClose(t.Close) go func() { // If ssh goes, so has the server: say so now, rather than // when a write next fails. <-t.Done() c.Drop() }() return c, t.Endpoint, nil } eps, err := rex.Candidates(address) if err != nil { return nil, rex.Endpoint{}, err } return rex.DialAny(ctx, eps, clientInfo) } // local opens a short-lived connection to the local server, which keeps // the hosts. func local(ctx context.Context) (*rex.Conn, error) { ep, err := rex.DiscoverEndpoint() if err != nil { return nil, err } ctx, cancel := context.WithTimeout(ctx, 3*time.Second) defer cancel() return rex.Dial(ctx, ep, clientInfo) } // Saved returns the hosts the local server keeps, as rex hosts lists them. func Saved(ctx context.Context) ([]rex.Host, error) { c, err := local(ctx) if err != nil { return nil, err } defer c.Close() ctx, cancel := context.WithTimeout(ctx, 5*time.Second) defer cancel() return c.Hosts(ctx) } // Save adds a host to the local server's hosts, or replaces the one with // the same label, so that rex -S and other clients know it too. func Save(ctx context.Context, label, address string) error { c, err := local(ctx) if err != nil { return err } defer c.Close() ctx, cancel := context.WithTimeout(ctx, 5*time.Second) defer cancel() host := map[string]any{"label": label, "endpoints": []string{address}, "added_from": "100gex"} if saved, err := c.Hosts(ctx); err == nil { if h, ok := rex.FindHost(saved, label); ok { host["id"] = h.ID } } _, err = c.Call(ctx, "hosts.put", map[string]any{"host": host}, nil) return err } // Remove takes a host off the local server's hosts. func Remove(ctx context.Context, id string) error { c, err := local(ctx) if err != nil { return err } defer c.Close() _, err = c.Call(ctx, "hosts.remove", map[string]string{"id": id}, nil) return err }