// Package agentcmd is the agent CLI, exposed as a library so the single drm // binary can host it as a subcommand. package agentcmd import ( "flag" "fmt" "log" "os" "os/signal" "strconv" "strings" "syscall" "text/tabwriter" "streamd/apps/agent/internal/client" "streamd/apps/agent/internal/config" "streamd/apps/agent/internal/device" "streamd/apps/agent/internal/modsync" "streamd/apps/agent/internal/poller" "streamd/apps/agent/internal/queue" "streamd/apps/agent/internal/worker" "streamd/apps/agent/internal/wsclient" "streamd/modhost" "streamd/repo" "streamd/wvkey" ) // Run dispatches an agent subcommand. args[0] is the subcommand name. func Run(args []string) error { log.SetFlags(log.LstdFlags | log.Lmsgprefix) log.SetPrefix("agent: ") if len(args) < 1 { Usage() return fmt.Errorf("agent: subcommand required") } sub, rest := args[0], args[1:] switch sub { case "run": runCmd(sub, rest) case "start": return startDaemonCmd(rest) case "stop": return stopDaemonCmd(rest) case "restart": return restartDaemonCmd(rest) case "update": return updateDaemonCmd(rest) case "status": statusCmd(sub, rest) case "devices": devicesCmd(sub, rest) case "enqueue": enqueueCmd(rest) case "cancel": cancelCmd(rest) case "help", "-h", "--help": Usage() default: Usage() return fmt.Errorf("agent: unknown subcommand %q", sub) } return nil } // Usage prints the agent subcommand help. func Usage() { fmt.Fprintf(os.Stderr, `agent — always-on credentials refresher (queue + ADB) Usage: agent start [--config agent.yaml] # background (pid + log) agent stop [--config ...] agent restart [--config ...] agent status [--config ...] # process + recent jobs agent update [--config ...] [--beta] [--tag vX.Y.Z] [--no-restart] agent run [--config ...] # foreground (debug) agent devices [--config ...] agent enqueue --stream NAME [--reason manual] [--config ...] agent cancel --job ID [--config ...] Polls streamd for enabled streams that are down / missing credentials, enqueues jobs, claims a free phone that already has the app, captures via MITM, POSTs credentials, then force-stops the app. Job retries default to immediate (job_backoff: false). Set job_backoff: true in agent.yaml for exponential delays when several agents share work. Does not install apps. Does not run NRE/ffmpeg. `) } func loadCfg(sub string, args []string) (config.Config, *flag.FlagSet) { fs := flag.NewFlagSet(sub, flag.ExitOnError) cfgPath := fs.String("config", "", "path to apps/agent/agent.yaml") _ = fs.Parse(args) cfg, err := config.Load(*cfgPath) if err != nil { log.Fatal(err) } return cfg, fs } // newAPI builds the streamd client with this agent's identity attached, so // audit rows in streamd name the agent and not just its API key. func newAPI(cfg config.Config) *client.Client { c := client.New(cfg.StreamdURL, cfg.Token) c.AgentID = cfg.AgentID return c } func openQueue(cfg config.Config) *queue.Store { q, err := queue.Open(cfg.DataDir) if err != nil { log.Fatalf("queue: %v", err) } return q } func runCmd(sub string, args []string) { cfg, _ := loadCfg(sub, args) api := newAPI(cfg) if err := api.Health(); err != nil { log.Fatalf("streamd unreachable at %s: %v", cfg.StreamdURL, err) } // Health says the server is up; /api/me says our credential actually // works. Without this a missing or revoked key failed silently on every // poll instead of once, loudly, at startup. me, err := api.Me() if err != nil { log.Fatalf("streamd rejected this agent at %s: %v", cfg.StreamdURL, err) } if me.Role != "operator" && me.Role != "admin" { log.Fatalf("this agent's credential has role %q, but capturing needs at least `operator` — "+ "create an operator API key in the streamd dashboard", me.Role) } log.Printf("authenticated to streamd as %q (role %s)", me.Name, me.Role) // Widevine keys need apps/wvkey + a local .venv (pywidevine). Create them // once per install root so Windows / Linux / macOS agents all work. if rt, err := wvkey.EnsureRuntime(repo.Root()); err != nil { log.Fatalf("widevine helper: %v", err) } else { log.Printf("widevine helper ready python=%s script=%s", rt.Python, rt.Script) if strings.TrimSpace(cfg.Python) == "" { cfg.Python = rt.Python } } q := openQueue(cfg) defer q.Close() if n, err := q.RecoverStaleRunning(); err != nil { log.Fatalf("recover jobs: %v", err) } else if n > 0 { log.Printf("re-queued %d stuck running job(s) from previous run", n) } if n, err := q.ClearAllBackoffs(); err != nil { log.Printf("clear backoffs: %v", err) } else if n > 0 { log.Printf("cleared backoff on %d queued job(s) — runnable now", n) } if cfg.JobBackoffEnabled() { log.Printf("job_backoff=true (exponential delays after failures)") } else { log.Printf("job_backoff=false (retry failed captures immediately)") } pool := device.New("", cfg.Devices) stop := make(chan struct{}) p := &poller.Poller{ API: api, Queue: q, MaxAttempts: cfg.MaxAttempts, Interval: cfg.PollInterval(), Log: log.Default(), } go p.Loop(stop) // Prefer the panel-synced modules cache for capture jobs. if cfg.ModulesDir != "" { if err := modhost.SetModulesDir(cfg.ModulesDir); err != nil { log.Fatalf("modules dir: %v", err) } log.Printf("modules dir %s", cfg.ModulesDir) } var ms *modsync.Syncer if cfg.SyncModules { ms = &modsync.Syncer{ API: api, ModulesDir: cfg.ModulesDir, Interval: cfg.ModulesSyncInterval(), Prune: cfg.ModulesPrune, Log: log.Default(), } go ms.Loop(stop) log.Printf("modules pull sync enabled interval=%s dir=%s", cfg.ModulesSyncInterval(), cfg.ModulesDir) } ws := wsclient.New(cfg.StreamdURL, cfg.Token, cfg.AgentID, log.Default()) // streamd pushes a capture request when a viewer wakes an on-demand // stream or a scheduled event is about to start. Reacting here is what // makes those responsive instead of up to one poll interval late. ws.OnCapture = func(streamID int64, name, app, channel, reason string) { if cfg.PushedCapturesArePriority() { p.EnqueueNow(streamID, name, app, channel, reason) return } if j, added, err := q.EnqueueIfIdle(streamID, name, app, channel, reason, cfg.MaxAttempts); err != nil { log.Printf("pushed capture for %s failed: %v", name, err) } else if added { log.Printf("enqueued job=%d stream=%s reason=%s (pushed by streamd)", j.ID, name, reason) } } // A stream was edited: re-poll now rather than waiting out the interval. ws.OnStreamChanged = func(name, reason string) { go func() { if err := p.Once(); err != nil { log.Printf("re-poll after change to %s: %v", name, err) } }() } ws.OnModulesChanged = func(name, reason string) { if ms == nil { return } go func() { if err := ms.Once(); err != nil { log.Printf("modules sync after change to %s: %v", name, err) } }() } go ws.Loop() for i := 0; i < cfg.Workers; i++ { w := &worker.Worker{ ID: i + 1, API: api, Queue: q, Pool: pool, AgentID: cfg.AgentID, CaptureWait: cfg.CaptureWait(), Python: cfg.Python, WVD: cfg.WVD, UserAgent: cfg.UserAgent, JobBackoff: cfg.JobBackoffEnabled(), Log: log.Default(), } go w.Loop(stop) } log.Printf("running agent_id=%s streamd=%s data=%s workers=%d poll=%s", cfg.AgentID, cfg.StreamdURL, cfg.DataDir, cfg.Workers, cfg.PollInterval()) log.Printf("ws heartbeat enabled — claims auto-expire if this agent dies") log.Printf("waiting for down / missing-credential streams… Ctrl+C to stop") ch := make(chan os.Signal, 1) signal.Notify(ch, os.Interrupt, syscall.SIGTERM) <-ch log.Printf("shutting down…") ws.Stop() close(stop) } func statusCmd(sub string, args []string) { cfg, _ := loadCfg(sub, args) printDaemonStatus(cfg) q := openQueue(cfg) defer q.Close() jobs, err := q.ListRecent(40) if err != nil { log.Fatal(err) } tw := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0) fmt.Fprintln(tw, "ID\tSTREAM\tAPP\tCHANNEL\tSTATUS\tATTEMPTS\tDEVICE\tREASON\tERROR") for _, j := range jobs { fmt.Fprintf(tw, "%d\t%s\t%s\t%s\t%s\t%d/%d\t%s\t%s\t%s\n", j.ID, j.StreamName, j.App, j.Channel, j.Status, j.Attempts, j.MaxAttempts, j.DeviceSerial, j.Reason, trunc(j.LastError, 60)) } _ = tw.Flush() } func devicesCmd(sub string, args []string) { cfg, _ := loadCfg(sub, args) pool := device.New("", cfg.Devices) list, err := pool.List() if err != nil { log.Fatal(err) } if len(list) == 0 { fmt.Println("no adb devices") return } tw := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0) fmt.Fprintln(tw, "SERIAL\tSTATE\tMODEL\tALLOWED\tBUSY\tBUSY_FOR") for _, d := range list { fmt.Fprintf(tw, "%s\t%s\t%s\t%v\t%v\t%s\n", d.Serial, d.State, d.Model, d.Allowed, d.Busy, d.BusyFor) } _ = tw.Flush() } func enqueueCmd(args []string) { fs := flag.NewFlagSet("enqueue", flag.ExitOnError) cfgPath := fs.String("config", "", "path to apps/agent/agent.yaml") name := fs.String("stream", "", "streamd stream name") reason := fs.String("reason", "manual", "job reason") _ = fs.Parse(args) if *name == "" { log.Fatal("--stream required") } cfg, err := config.Load(*cfgPath) if err != nil { log.Fatal(err) } api := newAPI(cfg) streams, err := api.ListStreams() if err != nil { log.Fatal(err) } var st *client.Stream for i := range streams { if streams[i].Name == *name { st = &streams[i] break } } if st == nil { log.Fatalf("stream %q not found on streamd", *name) } q := openQueue(cfg) defer q.Close() j, added, err := q.EnqueueIfIdle(st.ID, st.Name, st.App, st.Channel, *reason, cfg.MaxAttempts) if err != nil { log.Fatal(err) } if !added { fmt.Printf("already has queued/running job for %s\n", st.Name) return } fmt.Printf("enqueued job=%d stream=%s app=%s channel=%s\n", j.ID, j.StreamName, j.App, j.Channel) } func cancelCmd(args []string) { fs := flag.NewFlagSet("cancel", flag.ExitOnError) cfgPath := fs.String("config", "", "path to apps/agent/agent.yaml") jobID := fs.String("job", "", "job id") _ = fs.Parse(args) if *jobID == "" { log.Fatal("--job required") } id, err := strconv.ParseInt(*jobID, 10, 64) if err != nil { log.Fatal(err) } cfg, err := config.Load(*cfgPath) if err != nil { log.Fatal(err) } q := openQueue(cfg) defer q.Close() if err := q.Cancel(id); err != nil { log.Fatal(err) } fmt.Println("cancelled", id) } func trunc(s string, n int) string { if len(s) <= n { return s } return s[:n] + "…" }