// Package catalogcmd resolves a channel's stream credentials from its provider's // public catalog, with no phone involved. Providers live under modules// // and are invoked via modhost. package catalogcmd import ( "encoding/json" "flag" "fmt" "os" "sort" "strings" "streamd/modhost" "streamd/repo" "streamd/session" ) // Run performs a catalog lookup for one channel. func Run(args []string) error { fs := flag.NewFlagSet("catalog", flag.ExitOnError) appName := fs.String("app", "", "app module under modules//") channel := fs.String("channel", "", "channel id") asJSON := fs.Bool("json", false, "print streamd-ready JSON only") list := fs.Bool("list", false, "list the app's channels and their catalog ids") writeOut := fs.Bool("out", false, "write outputs///session.json") withKeys := fs.Bool("keys", false, "also fetch PSSH and resolve KID:KEY via the local CDM") python := fs.String("python", "", "python for wvkey.py (default: .venv)") wvd := fs.String("wvd", "", "path to .wvd device file (required with --keys)") fs.Usage = func() { fmt.Fprintf(fs.Output(), "catalog — resolve a channel from its public catalog (no phone)\n\nUsage:\n catalog --app <%s> --channel ID [--keys] [--json]\n\nFlags:\n", strings.Join(catalogApps(), "|")) fs.PrintDefaults() } if err := fs.Parse(args); err != nil { return err } available := catalogApps() if strings.TrimSpace(*appName) == "" { if len(available) != 1 { msg := strings.Join(available, ", ") if msg == "" { msg = "(none — add a modules// with catalog support)" } return fmt.Errorf("--app required (apps with a catalog: %s)", msg) } *appName = available[0] } a, err := modhost.Open(repo.Root(), *appName) if err != nil { return modhost.FormatUnknown(repo.Root(), *appName) } if !a.Desc.HasCatalog { return fmt.Errorf("app %q has no catalog — capture it from the phone instead: drm capture --app %s", a.Name(), a.Name()) } if *list { return listChannels(a) } if strings.TrimSpace(*channel) == "" { return fmt.Errorf("--channel required (see: drm catalog --app %s --list)", a.Name()) } info, err := a.Resolve(*channel) if err != nil { return err } if *withKeys { fmt.Fprintln(os.Stderr, "[*] fetching PSSH + Widevine keys…") if err := a.EnrichWithKeys(&info, *python, *wvd); err != nil { return err } fmt.Fprintf(os.Stderr, "[+] key %s (%d total)\n", info.Key, len(info.Keys)) } if *writeOut || *withKeys { dir, err := session.WriteStream(repo.Root(), a.Name(), info) if err != nil { return err } fmt.Fprintf(os.Stderr, "[+] wrote %s\n", dir) } if *asJSON { enc := json.NewEncoder(os.Stdout) enc.SetIndent("", " ") return enc.Encode(info) } printInfo(a.Name(), info) return nil } // catalogApps lists external modules that advertise HasCatalog. func catalogApps() []string { listed, err := modhost.ListAll(repo.Root()) if err != nil { return nil } out := []string{} for _, L := range listed { if L.Desc.HasCatalog { out = append(out, L.Desc.Name) } } sort.Strings(out) return out } func listChannels(a *modhost.RemoteApp) error { fmt.Printf("%-10s %s\n", "CHANNEL", "LABEL") for _, ch := range a.Channels() { fmt.Printf("%-10s %s\n", ch.ID, ch.Label) } return nil } func printInfo(appName string, info session.Stream) { fmt.Printf("name: %s\n", info.Name) fmt.Printf("title: %s\n", info.Title) fmt.Printf("app/channel: %s / %s\n", info.App, info.Channel) if info.VideoID != "" { fmt.Printf("catalog id: %s\n", info.VideoID) } fmt.Printf("manifest: %s\n", info.MPD) fmt.Printf("manifest type: %s\n", info.ManifestType) fmt.Printf("license_url: %s\n", truncate(info.LicenseURL, 100)) if info.PSSH != "" { fmt.Printf("pssh: %s\n", truncate(info.PSSH, 64)) } if info.Key != "" { fmt.Printf("key: %s\n", info.Key) } for i, k := range info.Keys { if k == info.Key { continue } fmt.Printf("key[%d]: %s\n", i, k) } body, _ := json.MarshalIndent(map[string]any{ "name": info.Name, "title": info.Title, "app": info.App, "channel": info.Channel, "mpd": info.MPD, "key": info.Key, "headers_json": info.HeadersJSON, "rewriter": info.Rewriter, }, "", " ") fmt.Printf("\nstreamd create body:\n%s\n", body) fmt.Printf("\nTip: drm catalog --app %s --channel %s --keys --wvd path/to/device.wvd --json\n", appName, info.Channel) } func truncate(s string, n int) string { if len(s) <= n { return s } return s[:n] + "..." }