Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224package cli
import ( "context" "errors" "fmt" "io" "net" "net/http" "os" "os/signal" "slices" "strconv" "strings" "syscall" "time"
"albedo/cli/internal/daemon" "albedo/cli/internal/proxyserver" "albedo/cli/internal/webui" "github.com/spf13/cobra")
func newProxy(deps Dependencies) *cobra.Command { var port int var allow []string var publicURL string var quiet, newKey, tailnetHTTPS, foreground, serve, stopServer, save, service bool command := &cobra.Command{Use: "proxy", Short: "serve your model providers through an OpenAI-compatible endpoint", Args: cobra.NoArgs, Long: `Serve the enabled proxy extension in the background; --stop stops it.Use --foreground to serve in this terminal until Ctrl-C.The daemon is found or started as for the terminal client. The API key printedhere grants access only to proxy models and chat completions, not the daemon.
It serves this machine only unless --allow opens a network (same syntax asalbedo web). --allow 100.64.0.0/10 shares your tailnet; --tailnet-https alsopublishes it through tailscale serve. --public-url names an existing reverseproxy's origin. Keep the printed key private.
--save remembers the networks and installs a login service.--save alone forgets them and removes the service.`, RunE: func(cmd *cobra.Command, _ []string) error { if port < 0 || port > 65535 { return errors.New("port must be between 0 and 65535") } networks, parseErr := proxyNetworks(allow) if parseErr != nil { return parseErr } var public string if publicURL != "" { origin, err := webui.ParseOrigin(publicURL) if err != nil { return err } public = origin.String() } if !foreground && !serve { return backgroundProxy(cmd, deps, webui.Options{Port: port, Allow: allow, Public: public, TailnetHTTPS: tailnetHTTPS}, quiet, newKey, stopServer, save) } lock, lockErr := serverLock("proxy") if lockErr != nil { return lockErr } defer lock.Close() if current, err := readProxyManifest(); err == nil && webui.ManifestAlive(current.Manifest) { return errors.New("albedo proxy is already running; run albedo proxy --stop first") } prepared, err := deps.Application.PrepareProxy(cmd.Context()) if err != nil { return err } if !prepared.Enabled { _, err := fmt.Fprintln(cmd.OutOrStdout(), "the proxy extension is off. turn it on in /extensions, then run albedo proxy again.") return err } settings, err := daemon.GetSettings(cmd.Context(), prepared.Connection) if err != nil { return err } if len(allow) == 0 { allow = settings.UI.ProxyAllow networks, err = proxyNetworks(allow) if err != nil { return err } } key, err := proxyserver.LoadKey(newKey) if err != nil { return err } var logs io.Writer if !quiet { logs = cmd.ErrOrStderr() if serve || service { output, err := proxyserver.OpenLog() if err != nil { return err } defer output.Close() logs = output } } server := proxyserver.New(prepared.Connection, logs) bind := "127.0.0.1" var addresses, names []string tailnet, onTailnet := webui.Tailnet{}, false if len(networks) > 0 || tailnetHTTPS { tailnet, onTailnet = webui.CurrentTailnet(cmd.Context()) } if len(networks) > 0 { machine, err := webui.NetworkAddresses() if err != nil { return err } addresses = server.Share(networks, machine) if len(addresses) == 0 { return fmt.Errorf("this machine has no address in %s", strings.Join(allow, ", ")) } bind = "0.0.0.0" if onTailnet && slices.ContainsFunc(addresses, func(a string) bool { return webui.TailscaleNetwork.Contains(net.ParseIP(a)) }) { names = tailnet.Names server.Answer(names...) } } if bind != "127.0.0.1" && port != 0 { probe, err := net.Listen("tcp", net.JoinHostPort("127.0.0.1", strconv.Itoa(port))) if err != nil { return fmt.Errorf("cannot listen on 127.0.0.1:%d (use --port to pick another): %w", port, err) } probe.Close() } listener, err := net.Listen("tcp", net.JoinHostPort(bind, strconv.Itoa(port))) if err != nil { return fmt.Errorf("cannot listen on %s:%d (use --port to pick another): %w", bind, port, err) } defer listener.Close() port = listener.Addr().(*net.TCPAddr).Port if tailnetHTTPS { claim, err := tailnet.ServeHTTPS(port) if err != nil { return err } origin, err := claim.Claim(cmd.Context()) if err != nil { return err } public = origin.String() defer releaseTailnet(cmd, port) } if public != "" { origin, err := webui.ParseOrigin(public) if err != nil { return err } server.Publish(origin) } manifest := proxyManifest{ Manifest: webui.Manifest{Options: webui.Options{Port: port, Allow: allow, Public: public, TailnetHTTPS: tailnetHTTPS}, Names: names, Addresses: addresses, PID: os.Getpid(), URL: webui.Loopback(port).String()}, Started: time.Now().UTC(), Quiet: quiet, Providers: prepared.Providers, Unavailable: prepared.Unavailable, } if err := writeProxyManifest(manifest); err != nil { return err } defer func() { if current, err := readProxyManifest(); err == nil && current.PID == os.Getpid() { _ = os.Remove(proxyManifestPath()) } }() if !serve && !service { printProxySummary(cmd.OutOrStdout(), key, manifest) fmt.Fprintln(cmd.OutOrStdout(), "\nserving until ctrl-c") } ctx, stop := signal.NotifyContext(cmd.Context(), os.Interrupt, syscall.SIGTERM) defer stop() httpServer := &http.Server{Handler: server, ReadHeaderTimeout: 10 * time.Second} finished := make(chan struct{}) go func() { defer close(finished) <-ctx.Done() shutdown, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() if httpServer.Shutdown(shutdown) != nil { httpServer.Close() } }() err = httpServer.Serve(listener) stop() <-finished if !errors.Is(err, http.ErrServerClosed) { return err } return nil }} command.Flags().BoolVar(&service, "service", false, "supervised proxy server") _ = command.Flags().MarkHidden("service") command.Flags().BoolVar(&foreground, "foreground", false, "serve in this terminal until interrupted") command.Flags().BoolVar(&serve, "serve", false, "background proxy server") _ = command.Flags().MarkHidden("serve") command.Flags().BoolVar(&stopServer, "stop", false, "stop the background proxy server") command.MarkFlagsMutuallyExclusive("foreground", "serve", "stop") command.Flags().IntVar(&port, "port", 4318, "port to serve on; 0 picks a free one") command.Flags().StringArrayVar(&allow, "allow", nil, "serve devices on this network (CIDR, or 10.0.0.0 for 10.0.0.0/8); repeatable") command.Flags().BoolVar(&save, "save", false, "remember the --allow networks and run at login (alone, forget them)") command.Flags().StringVar(&publicURL, "public-url", "", "origin a proxy in front serves this through (https://host), for links and requests") command.Flags().BoolVar(&tailnetHTTPS, "tailnet-https", false, "serve https on your tailnet through tailscale serve, printing its https link") command.MarkFlagsMutuallyExclusive("public-url", "tailnet-https") command.Flags().BoolVar(&quiet, "quiet", false, "omit per-request logs") command.Flags().BoolVar(&newKey, "new-key", false, "replace the proxy API key, revoking the previous key") command.MarkFlagsMutuallyExclusive("stop", "new-key") command.MarkFlagsMutuallyExclusive("save", "foreground", "serve", "stop") return command}
func proxyNetworks(allow []string) ([]*net.IPNet, error) { var networks []*net.IPNet for _, text := range allow { network, err := webui.ParseNetwork(text) if err != nil { return nil, err } networks = append(networks, network) } return networks, nil}