v0.1.1: external modules, publish YAML, proxy reset, binary-first README
All checks were successful
release / build-and-upload (push) Successful in 6m8s
modules-release / build-and-upload (push) Successful in 4m47s

- Host discovers/builds modules under modules/ (bbc, rte, tg4)
- module-publish.yaml tracked; local module.yaml gitignored
- drm --modules-dir / DRM_MODULES_DIR
- drm proxy reset; Magisk reinject reliability
- modules-v* Forgejo release workflow + build scripts
- BBC full Explore channel map including S4C
- README quickstart based on release binaries
This commit is contained in:
404errordeveloper 2026-10-08 23:16:14 +02:00
parent 1d36961a7c
commit f9c28be3f1
52 changed files with 2915 additions and 636 deletions

107
apps/pkg/modhost/build.go Normal file
View file

@ -0,0 +1,107 @@
package modhost
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
// EnsureBuilt returns the path to the module binary, building from Go source
// when needed. Prebuilt .bin/ is used as-is when source is absent.
func EnsureBuilt(m Module) (string, error) {
bin := m.BinPath()
hasSrc := hasGoSource(m.Dir)
binOK := fileExists(bin)
if !hasSrc && !binOK {
return "", fmt.Errorf("module %s: no Go source in %s and no binary at %s", m.Name, m.Dir, bin)
}
if binOK && (!hasSrc || !sourceNewerThan(m.Dir, bin)) {
return bin, nil
}
goBin, err := findGo()
if err != nil {
if binOK {
fmt.Fprintf(os.Stderr, "[!] go not found; using existing %s\n", bin)
return bin, nil
}
return "", fmt.Errorf("module %s needs building but go is not on PATH: %w", m.Name, err)
}
if err := os.MkdirAll(filepath.Dir(bin), 0o755); err != nil {
return "", err
}
fmt.Printf("[*] Building module %s → %s\n", m.Name, bin)
cmd := exec.Command(goBin, "build", "-trimpath", "-ldflags=-s -w", "-o", bin, ".")
cmd.Dir = m.Dir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("go build module %s: %w", m.Name, err)
}
return bin, nil
}
func hasGoSource(dir string) bool {
if fileExists(filepath.Join(dir, "go.mod")) {
return true
}
ents, err := os.ReadDir(dir)
if err != nil {
return false
}
for _, e := range ents {
if !e.IsDir() && strings.HasSuffix(e.Name(), ".go") {
return true
}
}
return false
}
func sourceNewerThan(dir, bin string) bool {
bst, err := os.Stat(bin)
if err != nil {
return true
}
binTime := bst.ModTime()
newer := false
_ = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() {
if info != nil && info.IsDir() && (info.Name() == ".bin" || info.Name() == "vendor") {
return filepath.SkipDir
}
return nil
}
name := info.Name()
if strings.HasSuffix(name, ".go") || name == "go.mod" || name == "go.sum" {
if info.ModTime().After(binTime) {
newer = true
}
}
return nil
})
return newer
}
func findGo() (string, error) {
if p, err := exec.LookPath("go"); err == nil {
return p, nil
}
home, _ := os.UserHomeDir()
candidates := []string{
filepath.Join(home, "go-sdk", "go", "bin", "go"),
filepath.Join(home, "go-sdk", "go", "bin", "go.exe"),
filepath.Join(home, "sdk", "go", "bin", "go"),
`C:\Program Files\Go\bin\go.exe`,
"/usr/local/go/bin/go",
}
for _, c := range candidates {
if st, err := os.Stat(c); err == nil && !st.IsDir() {
return c, nil
}
}
return "", fmt.Errorf("go binary not found (install Go or add it to PATH)")
}

View file

@ -0,0 +1,140 @@
package modhost
import (
"os"
"path/filepath"
"runtime"
"strings"
)
// Module is one on-disk provider under modules/<name>/.
type Module struct {
Name string // directory base name, e.g. "bbc"
Dir string // absolute path to modules/<name>
}
// ModulesDir returns the modules directory (honours --modules-dir / DRM_MODULES_DIR).
func ModulesDir(root string) string {
return EffectiveModulesDir(root)
}
// Discover lists modules/<name>/ directories that look like providers
// (have module.yaml / module-publish.yaml and/or Go source and/or a prebuilt binary).
func Discover(root string) ([]Module, error) {
dir := ModulesDir(root)
ents, err := os.ReadDir(dir)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
var out []Module
for _, e := range ents {
if !e.IsDir() {
continue
}
name := e.Name()
if strings.HasPrefix(name, ".") {
continue
}
modDir := filepath.Join(dir, name)
if !looksLikeModule(modDir, name) {
continue
}
out = append(out, Module{Name: name, Dir: modDir})
}
return out, nil
}
func looksLikeModule(dir, name string) bool {
if fileExists(filepath.Join(dir, "module.yaml")) {
return true
}
if fileExists(filepath.Join(dir, "module-publish.yaml")) {
return true
}
if fileExists(filepath.Join(dir, "go.mod")) {
return true
}
if fileExists(BinPath(dir, name)) {
return true
}
// Any .go file in the root of the module dir.
ents, err := os.ReadDir(dir)
if err != nil {
return false
}
for _, e := range ents {
if !e.IsDir() && strings.HasSuffix(e.Name(), ".go") {
return true
}
}
return false
}
// Find returns the module named name, or an error listing what exists.
func Find(root, name string) (Module, error) {
mods, err := Discover(root)
if err != nil {
return Module{}, err
}
for _, m := range mods {
if m.Name == name {
return m, nil
}
}
names := make([]string, 0, len(mods))
for _, m := range mods {
names = append(names, m.Name)
}
if len(names) == 0 {
return Module{}, errNoModules(root)
}
return Module{}, &NotFoundError{Name: name, Available: names}
}
// BinPath is modules/<name>/.bin/<name>[.exe].
func BinPath(modDir, name string) string {
bin := filepath.Join(modDir, ".bin", name)
if runtime.GOOS == "windows" {
bin += ".exe"
}
return bin
}
func (m Module) BinPath() string {
return BinPath(m.Dir, m.Name)
}
func fileExists(p string) bool {
st, err := os.Stat(p)
return err == nil && !st.IsDir()
}
func errNoModules(root string) error {
return &NotFoundError{
Name: "",
Available: nil,
Hint: "create modules/<name>/ with Go source + module.yaml (see docs/modules.md)",
Root: ModulesDir(root),
}
}
// NotFoundError is returned when --app does not match a modules/ directory.
type NotFoundError struct {
Name string
Available []string
Hint string
Root string
}
func (e *NotFoundError) Error() string {
if e.Name == "" {
return "no modules found under " + e.Root + " — " + e.Hint
}
if len(e.Available) == 0 {
return "unknown module " + e.Name + " (modules dir empty) — " + e.Hint
}
return "unknown module " + e.Name + " — available: " + strings.Join(e.Available, ", ")
}

78
apps/pkg/modhost/exec.go Normal file
View file

@ -0,0 +1,78 @@
package modhost
import (
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
)
// Run executes the module binary with args, inheriting stdout/stderr.
func Run(bin string, args ...string) error {
cmd := exec.Command(bin, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
if err := cmd.Run(); err != nil {
return fmt.Errorf("%s %s: %w", bin, strings.Join(args, " "), err)
}
return nil
}
// RunOutput executes the module binary and returns combined stdout.
func RunOutput(bin string, args ...string) (string, error) {
cmd := exec.Command(bin, args...)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return stdout.String(), fmt.Errorf("%s %s: %w\n%s", bin, strings.Join(args, " "), err, stderr.String())
}
return stdout.String(), nil
}
// RunDescribe runs `bin describe` and parses JSON.
func RunDescribe(bin string) (Describe, error) {
out, err := RunOutput(bin, "describe")
if err != nil {
return Describe{}, err
}
// Allow log lines before JSON: find first '{'.
out = strings.TrimSpace(out)
if i := strings.IndexByte(out, '{'); i > 0 {
out = out[i:]
}
var d Describe
if err := json.Unmarshal([]byte(out), &d); err != nil {
return Describe{}, fmt.Errorf("describe JSON: %w\noutput: %s", err, out)
}
if d.Protocol != 0 && d.Protocol != ProtocolVersion {
return d, fmt.Errorf("module protocol %d unsupported (host wants %d)", d.Protocol, ProtocolVersion)
}
if d.Protocol == 0 {
d.Protocol = ProtocolVersion
}
return d, nil
}
// Load builds (if needed) and describes a module by name.
func Load(root, name string) (Module, string, Describe, error) {
m, err := Find(root, name)
if err != nil {
return Module{}, "", Describe{}, err
}
bin, err := EnsureBuilt(m)
if err != nil {
return m, "", Describe{}, err
}
d, err := RunDescribe(bin)
if err != nil {
return m, bin, Describe{}, err
}
if d.Name == "" {
d.Name = m.Name
}
return m, bin, d, nil
}

View file

@ -0,0 +1,33 @@
// Package modhost discovers, builds, and runs external provider modules that
// live under modules/<name>/ (Go source + module.yaml), outside drm.exe.
package modhost
import "drmdecryption/capture"
// ProtocolVersion is bumped when describe/launch/autoplay CLI contract changes.
const ProtocolVersion = 1
// Channel is one live target advertised by a module.
type Channel struct {
ID string `json:"id"`
Label string `json:"label"`
}
// Describe is the JSON printed by `module-bin describe`.
type Describe struct {
Protocol int `json:"protocol"`
Name string `json:"name"`
Package string `json:"package"`
LicenseURL string `json:"license_url,omitempty"`
KeyMode string `json:"key_mode"`
ProxyBin string `json:"proxy_bin,omitempty"`
CAHash string `json:"ca_hash,omitempty"`
TransparentMITM bool `json:"transparent_mitm"`
Rewriter string `json:"rewriter,omitempty"`
Channels []Channel `json:"channels"`
Require []string `json:"require,omitempty"`
Score capture.ScoreCfg `json:"score,omitempty"`
HasCatalog bool `json:"has_catalog"`
ValuesPath string `json:"values_path,omitempty"`
ValuesLoaded bool `json:"values_loaded"`
}

View file

@ -0,0 +1,224 @@
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/<name>, 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) 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.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/<name>/.
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)
}

62
apps/pkg/modhost/root.go Normal file
View file

@ -0,0 +1,62 @@
package modhost
import (
"os"
"path/filepath"
"sync"
"drmdecryption/repo"
)
var (
rootMu sync.RWMutex
modulesDirOV string // absolute path override for the modules directory
)
// SetModulesDir overrides where providers are discovered (drm --modules-dir).
// Empty clears the override. Relative paths are resolved against the process cwd.
func SetModulesDir(dir string) error {
rootMu.Lock()
defer rootMu.Unlock()
if dir == "" {
modulesDirOV = ""
return nil
}
abs, err := filepath.Abs(dir)
if err != nil {
return err
}
modulesDirOV = abs
return nil
}
// ModulesDirOverride returns the --modules-dir override, or "".
func ModulesDirOverride() string {
rootMu.RLock()
defer rootMu.RUnlock()
return modulesDirOV
}
// EffectiveModulesDir is the directory that Discover/Open scan.
// Prefer --modules-dir / DRM_MODULES_DIR; else <root>/modules.
func EffectiveModulesDir(root string) string {
if ov := ModulesDirOverride(); ov != "" {
return ov
}
if root == "" {
root = repo.Root()
}
return filepath.Join(root, "modules")
}
// ApplyEnvModulesDir sets the override from DRM_MODULES_DIR when no flag was set.
func ApplyEnvModulesDir() error {
if ModulesDirOverride() != "" {
return nil
}
v := os.Getenv("DRM_MODULES_DIR")
if v == "" {
return nil
}
return SetModulesDir(v)
}