package modhost import ( "encoding/json" "fmt" "path/filepath" "strings" "drmdecryption/adb" "drmdecryption/app" "drmdecryption/capture" "drmdecryption/mpd" "drmdecryption/session" ) // RemoteApp implements app.App by calling an external module binary. type RemoteApp struct { Mod Module Bin string Desc Describe } // Open builds and describes modules/, returning a RemoteApp. func Open(root, name string) (*RemoteApp, error) { m, bin, d, err := Load(root, name) if err != nil { return nil, err } return &RemoteApp{Mod: m, Bin: bin, Desc: d}, nil } func (r *RemoteApp) Name() string { return r.Desc.Name } func (r *RemoteApp) Package() string { return r.Desc.Package } func (r *RemoteApp) LicenseURL() string { return r.Desc.LicenseURL } func (r *RemoteApp) ProxyBin() string { return r.Desc.ProxyBin } func (r *RemoteApp) CAHash() string { return r.Desc.CAHash } func (r *RemoteApp) KeyMode() string { if r.Desc.KeyMode == "" { return "none" } return r.Desc.KeyMode } func (r *RemoteApp) Channels() []app.Channel { out := make([]app.Channel, 0, len(r.Desc.Channels)) for _, ch := range r.Desc.Channels { out = append(out, app.Channel{ID: ch.ID, Label: ch.Label}) } return out } func (r *RemoteApp) HasChannel(id string) bool { for _, ch := range r.Desc.Channels { if ch.ID == id { return true } } return false } func (r *RemoteApp) CaptureHints() capture.Hints { req := r.Desc.Require if len(req) == 0 { req = []string{"mpd"} } h := capture.DefaultHints() h.Require = req h.Score = capture.DefaultScoreCfg().Merge(r.Desc.Score) return h } func (r *RemoteApp) MPDRewriter() mpd.Rewriter { name := r.Desc.Rewriter if name == "" || name == "none" { return mpd.Passthrough{} } if rw, ok := mpd.Lookup(name); ok { return rw } return mpd.Passthrough{} } func (r *RemoteApp) UseTransparentMITM() bool { return r.Desc.TransparentMITM } func (r *RemoteApp) RequireWVD() bool { return r.Desc.RequireWVD } func (r *RemoteApp) MitmHosts() []string { out := make([]string, 0, len(r.Desc.MitmHosts)) for _, h := range r.Desc.MitmHosts { h = strings.TrimSpace(strings.ToLower(h)) if h != "" { out = append(out, h) } } return out } func (r *RemoteApp) CaptureHintsFor(channel string) capture.Hints { h := r.CaptureHints() if len(r.Desc.ChannelMPDPrefer) == 0 { return h } id := strings.ToLower(strings.TrimSpace(channel)) if prefs, ok := r.Desc.ChannelMPDPrefer[id]; ok && len(prefs) > 0 { h.PreferURLContains = append([]string{}, prefs...) return h } // Alias / label fallback: match prefer keys against known channel ids only. for _, ch := range r.Desc.Channels { if strings.EqualFold(ch.ID, id) || strings.EqualFold(ch.Label, id) { if prefs, ok := r.Desc.ChannelMPDPrefer[ch.ID]; ok && len(prefs) > 0 { h.PreferURLContains = append([]string{}, prefs...) } break } } return h } func (r *RemoteApp) RewriterName() string { if r.Desc.Rewriter == "" { return "none" } return r.Desc.Rewriter } func (r *RemoteApp) Launch(c *adb.Client) error { args := []string{"launch"} if c != nil && c.Serial != "" { args = append(args, "--serial", c.Serial) } return Run(r.Bin, args...) } func (r *RemoteApp) AutoPlay(c *adb.Client, channel string) error { args := []string{"autoplay", "--channel", channel} if c != nil && c.Serial != "" { args = append(args, "--serial", c.Serial) } return Run(r.Bin, args...) } // ValuesPath is where module.yaml should live for this module. func (r *RemoteApp) ValuesPath() string { if r.Desc.ValuesPath != "" { return r.Desc.ValuesPath } return filepath.Join(r.Mod.Dir, "module.yaml") } // Ensure RemoteApp satisfies the interfaces capture / catalog use. var ( _ app.App = (*RemoteApp)(nil) _ app.TransparentMITM = (*RemoteApp)(nil) _ app.RequireWVD = (*RemoteApp)(nil) _ app.MitmHostList = (*RemoteApp)(nil) _ app.ChannelCaptureHints = (*RemoteApp)(nil) _ app.StreamDefaults = (*RemoteApp)(nil) _ app.Catalog = (*RemoteApp)(nil) ) func (r *RemoteApp) VideoSelect() string { return "" } func (r *RemoteApp) AudioSelect() string { return "" } func (r *RemoteApp) LiveWaitSeconds() int { return 0 } func (r *RemoteApp) TSReadyBytes() int64 { return 0 } // Resolve runs `module catalog --channel ID --json`. func (r *RemoteApp) Resolve(channel string) (session.Stream, error) { if !r.Desc.HasCatalog { return session.Stream{}, fmt.Errorf("module %s has no catalog", r.Desc.Name) } out, err := RunOutput(r.Bin, "catalog", "--channel", channel, "--json") if err != nil { return session.Stream{}, err } out = strings.TrimSpace(out) if i := strings.IndexByte(out, '{'); i > 0 { out = out[i:] } var info session.Stream if err := json.Unmarshal([]byte(out), &info); err != nil { return session.Stream{}, fmt.Errorf("catalog JSON: %w\noutput: %s", err, out) } return info, nil } // EnrichWithKeys is not yet implemented for external modules. func (r *RemoteApp) EnrichWithKeys(info *session.Stream, python, wvd string) error { return fmt.Errorf("module %s: --keys via external module is not supported yet", r.Desc.Name) } // Listed is one discovered module with its describe payload. type Listed struct { Mod Module Bin string Desc Describe } // ListAll discovers, builds, and describes every modules//. func ListAll(root string) ([]Listed, error) { mods, err := Discover(root) if err != nil { return nil, err } out := make([]Listed, 0, len(mods)) for _, m := range mods { bin, err := EnsureBuilt(m) if err != nil { return out, fmt.Errorf("module %s: %w", m.Name, err) } d, err := RunDescribe(bin) if err != nil { return out, fmt.Errorf("module %s describe: %w", m.Name, err) } if d.Name == "" { d.Name = m.Name } out = append(out, Listed{Mod: m, Bin: bin, Desc: d}) } return out, nil } // Names lists discovered module directory names (no build). func Names(root string) ([]string, error) { mods, err := Discover(root) if err != nil { return nil, err } names := make([]string, 0, len(mods)) for _, m := range mods { names = append(names, m.Name) } return names, nil } // FindChannelInModules scans described modules for a channel id. func FindChannelInModules(root, channelID string) (appName string, ok bool) { listed, err := ListAll(root) if err != nil { return "", false } for _, L := range listed { for _, ch := range L.Desc.Channels { if ch.ID == channelID { return L.Mod.Name, true } } } return "", false } // FormatUnknown helps capturecmd error messages when --app is unknown. func FormatUnknown(root, name string) error { _, err := Find(root, name) if err != nil { if owner, ok := FindChannelInModules(root, name); ok { return fmt.Errorf("%q is a channel of --app %s, not an app name — use: capture --app %s --channel %s", name, owner, owner, name) } return err } return fmt.Errorf("unknown module %q", name) }