All checks were successful
release / build-and-upload (push) Successful in 7m5s
- Rename product/host binary to streamd (drm compat copy still shipped) - Go module path streamd; STREAMD_* env aliases; agent UA streamd-agent - Panel Modules store + agent pull sync (modstore / modsync) - Rolling stable + beta download tags; README/docs channel curl blocks - Release notes in docs/releases/v0.2.0.md attached by Forgejo Actions
277 lines
8.5 KiB
Go
277 lines
8.5 KiB
Go
// Package capturecmd is the one-shot phone MITM capture command, exposed as a
|
||
// library so the single drm binary can host it as a subcommand.
|
||
package capturecmd
|
||
|
||
import (
|
||
"encoding/json"
|
||
"flag"
|
||
"fmt"
|
||
"os"
|
||
"path/filepath"
|
||
"strings"
|
||
"time"
|
||
"unicode"
|
||
|
||
"streamd/adb"
|
||
"streamd/app"
|
||
"streamd/modcfg"
|
||
"streamd/modhost"
|
||
"streamd/phonecap"
|
||
"streamd/repo"
|
||
)
|
||
|
||
// Run performs one capture. Providers live under modules/<name>/ and are built
|
||
// and executed by modhost — they are not linked into streamd.
|
||
func Run(args []string) error {
|
||
args = normalizeArgs(args)
|
||
|
||
root := repo.Root()
|
||
available, _ := modhost.Names(root)
|
||
appChoices := strings.Join(available, "|")
|
||
if appChoices == "" {
|
||
appChoices = "NAME"
|
||
}
|
||
|
||
fs := flag.NewFlagSet("capture", flag.ContinueOnError)
|
||
appName := fs.String("app", "", "app module under modules/<name>/. Empty = passive MITM, no launch/auto-play")
|
||
channel := fs.String("channel", "", "channel id for --auto-play")
|
||
allChannels := fs.Bool("all", false, "capture every channel declared by the module (grab-all)")
|
||
autoPlay := fs.Bool("auto-play", false, "auto-navigate to --channel (implied when --channel / --all is set)")
|
||
waitSec := fs.Int("wait", 180, "seconds to wait for license/mpd capture (per channel with --all)")
|
||
python := fs.String("python", "", "python for wvkey.py (default: .venv)")
|
||
wvd := fs.String("wvd", "", "path to .wvd device file (required by some apps e.g. c4; MPD-only if omitted)")
|
||
userAgent := fs.String("user-agent", "", "optional User-Agent for license requests")
|
||
serial := fs.String("serial", "", "adb device serial (default: sole/USB selected device)")
|
||
closeApp := fs.Bool("close", true, "force-stop --app when capture finishes (ignored in passive mode)")
|
||
keyMode := fs.String("key-mode", "", "override the app's key mode: modulardrm | raw | wrap | none")
|
||
fs.Usage = func() {
|
||
fmt.Fprintf(fs.Output(), "capture — one-shot phone MITM capture\n\nUsage:\n capture --app <%s> [--channel ID | --all] [--auto-play] [--wvd PATH]\n capture (passive: play anything on the phone)\n\nExamples:\n capture --app bbc --channel bbcone --wait 120\n capture --app c4 --channel film4 --wvd data/device.wvd\n capture --app c4 --all --wvd data/device.wvd --wait 90\n\nFlags:\n", appChoices)
|
||
fs.PrintDefaults()
|
||
}
|
||
|
||
if err := fs.Parse(args); err != nil {
|
||
return err
|
||
}
|
||
if rest := fs.Args(); len(rest) > 0 {
|
||
return fmt.Errorf("unexpected arguments %v — use flags only (example: capture --app bbc --channel bbcone)", rest)
|
||
}
|
||
|
||
c := adb.New()
|
||
if *serial != "" {
|
||
c = c.WithSerial(*serial)
|
||
}
|
||
|
||
// No --app: passive MITM — the operator plays whatever they want and we dump
|
||
// whatever DRM traffic appears.
|
||
if strings.TrimSpace(*appName) == "" {
|
||
if *allChannels {
|
||
return fmt.Errorf("--all requires --app")
|
||
}
|
||
_, err := phonecap.RunPassive(phonecap.Options{
|
||
Client: c,
|
||
Python: *python,
|
||
WVD: *wvd,
|
||
UserAgent: *userAgent,
|
||
Wait: time.Duration(*waitSec) * time.Second,
|
||
KeyMode: *keyMode,
|
||
ClearProxy: true,
|
||
})
|
||
return err
|
||
}
|
||
|
||
a, err := modhost.Open(root, *appName)
|
||
if err != nil {
|
||
return modhost.FormatUnknown(root, *appName)
|
||
}
|
||
if needsWVD(a) && strings.TrimSpace(*wvd) == "" {
|
||
return fmt.Errorf("%s requires --wvd PATH (Widevine device file) — encrypted capture cannot be MPD-only", a.Name())
|
||
}
|
||
channelID := strings.TrimSpace(*channel)
|
||
if *allChannels && channelID != "" {
|
||
return fmt.Errorf("use either --channel or --all, not both")
|
||
}
|
||
if *allChannels {
|
||
return runAll(root, a, c, phonecap.Options{
|
||
App: a,
|
||
Client: c,
|
||
Python: *python,
|
||
WVD: *wvd,
|
||
UserAgent: *userAgent,
|
||
Wait: time.Duration(*waitSec) * time.Second,
|
||
CloseApp: *closeApp,
|
||
KeyMode: *keyMode,
|
||
ClearProxy: true,
|
||
})
|
||
}
|
||
// --channel implies interactive auto-play (Explore → channel → Watch live).
|
||
doAutoPlay := *autoPlay || channelID != ""
|
||
if *autoPlay && channelID == "" {
|
||
return fmt.Errorf("--auto-play requires --channel (%s)", knownChannels(a))
|
||
}
|
||
if channelID != "" && !a.HasChannel(channelID) {
|
||
return fmt.Errorf("unknown channel %q for %s (%s)", channelID, a.Name(), knownChannels(a))
|
||
}
|
||
|
||
_, err = phonecap.Run(phonecap.Options{
|
||
App: a,
|
||
Channel: channelID,
|
||
Client: c,
|
||
Python: *python,
|
||
WVD: *wvd,
|
||
UserAgent: *userAgent,
|
||
Wait: time.Duration(*waitSec) * time.Second,
|
||
NoAutoPlay: !doAutoPlay,
|
||
CloseApp: *closeApp,
|
||
KeyMode: *keyMode,
|
||
ClearProxy: true,
|
||
})
|
||
return err
|
||
}
|
||
|
||
type channelResult struct {
|
||
Channel string `json:"channel"`
|
||
Label string `json:"label"`
|
||
OK bool `json:"ok"`
|
||
MPD string `json:"mpd,omitempty"`
|
||
LicenseURL string `json:"license_url,omitempty"`
|
||
Key string `json:"key,omitempty"`
|
||
Path string `json:"path,omitempty"`
|
||
Error string `json:"error,omitempty"`
|
||
}
|
||
|
||
func runAll(root string, a app.App, c *adb.Client, base phonecap.Options) error {
|
||
chs := a.Channels()
|
||
if len(chs) == 0 {
|
||
return fmt.Errorf("no channels configured for %s (%s)", a.Name(), knownChannels(a))
|
||
}
|
||
fmt.Printf("[*] Grab-all %s — %d channel(s)\n", a.Name(), len(chs))
|
||
results := make([]channelResult, 0, len(chs))
|
||
okN := 0
|
||
for i, ch := range chs {
|
||
fmt.Printf("\n======== [%d/%d] %s (%s) ========\n", i+1, len(chs), ch.Label, ch.ID)
|
||
opt := base
|
||
opt.Channel = ch.ID
|
||
opt.NoAutoPlay = false
|
||
opt.App = a
|
||
opt.Client = c
|
||
res, err := phonecap.Run(opt)
|
||
row := channelResult{
|
||
Channel: ch.ID,
|
||
Label: ch.Label,
|
||
}
|
||
if err != nil {
|
||
row.Error = err.Error()
|
||
fmt.Fprintf(os.Stderr, "[!] %s: %v\n", ch.ID, err)
|
||
} else {
|
||
row.OK = res.MPD != ""
|
||
row.MPD = res.MPD
|
||
row.LicenseURL = res.Session.LicenseURL
|
||
row.Key = res.Key
|
||
row.Path = res.Path
|
||
if row.OK {
|
||
okN++
|
||
} else {
|
||
row.Error = "no mpd"
|
||
}
|
||
}
|
||
results = append(results, row)
|
||
if i+1 < len(chs) {
|
||
// Let iptables / appproxy fully release before the next transparent start.
|
||
time.Sleep(4 * time.Second)
|
||
}
|
||
}
|
||
|
||
stamp := time.Now().Format("20060102-150405")
|
||
batchDir := filepath.Join(root, "outputs", a.Name(), "all-"+stamp)
|
||
_ = os.MkdirAll(batchDir, 0o755)
|
||
summaryPath := filepath.Join(batchDir, "SUMMARY.json")
|
||
if b, err := json.MarshalIndent(results, "", " "); err == nil {
|
||
_ = os.WriteFile(summaryPath, b, 0o644)
|
||
}
|
||
printAllSummary(a.Name(), results, okN, summaryPath)
|
||
if okN == 0 {
|
||
return fmt.Errorf("grab-all %s: 0/%d channels captured", a.Name(), len(chs))
|
||
}
|
||
if okN < len(chs) {
|
||
return fmt.Errorf("grab-all %s: %d/%d channels captured (see %s)", a.Name(), okN, len(chs), summaryPath)
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func printAllSummary(appName string, results []channelResult, okN int, summaryPath string) {
|
||
fmt.Println()
|
||
fmt.Println("========== GRAB-ALL SUMMARY ==========")
|
||
fmt.Printf("app: %s ok: %d/%d\n", appName, okN, len(results))
|
||
fmt.Println("--------------------------------------")
|
||
fmt.Printf("%-12s %-14s %-6s %s\n", "ID", "LABEL", "OK", "MPD / ERROR")
|
||
for _, r := range results {
|
||
status := "no"
|
||
if r.OK {
|
||
status = "yes"
|
||
}
|
||
detail := r.Error
|
||
if r.OK {
|
||
detail = trimURL(r.MPD, 72)
|
||
}
|
||
fmt.Printf("%-12s %-14s %-6s %s\n", r.Channel, r.Label, status, detail)
|
||
}
|
||
fmt.Println("--------------------------------------")
|
||
fmt.Println("summary:", summaryPath)
|
||
fmt.Println("======================================")
|
||
}
|
||
|
||
func trimURL(s string, n int) string {
|
||
s = strings.TrimSpace(s)
|
||
if len(s) <= n {
|
||
return s
|
||
}
|
||
return s[:n] + "…"
|
||
}
|
||
|
||
// normalizeArgs rewrites Unicode dashes Word/Terminal often insert (— – −)
|
||
// into ASCII - so `--app` is not silently dropped as a positional token.
|
||
func normalizeArgs(args []string) []string {
|
||
out := make([]string, len(args))
|
||
for i, a := range args {
|
||
var b strings.Builder
|
||
b.Grow(len(a) + 4)
|
||
for _, r := range a {
|
||
switch r {
|
||
case '\u2014', '\u2013', '\u2212': // em, en, minus
|
||
// "—app" → "--app"; standalone dash stays single "-"
|
||
if b.Len() == 0 {
|
||
b.WriteString("--")
|
||
} else {
|
||
b.WriteByte('-')
|
||
}
|
||
default:
|
||
if unicode.Is(unicode.Cf, r) {
|
||
continue // strip zero-width / format chars
|
||
}
|
||
b.WriteRune(r)
|
||
}
|
||
}
|
||
out[i] = b.String()
|
||
}
|
||
return out
|
||
}
|
||
|
||
func needsWVD(a app.App) bool {
|
||
if rw, ok := a.(app.RequireWVD); ok {
|
||
return rw.RequireWVD()
|
||
}
|
||
return false
|
||
}
|
||
|
||
// knownChannels describes what the operator can pick, or where to declare it when
|
||
// the module has no local values file yet.
|
||
func knownChannels(a app.App) string {
|
||
ids := []string{}
|
||
for _, ch := range a.Channels() {
|
||
ids = append(ids, ch.ID)
|
||
}
|
||
if len(ids) == 0 {
|
||
return "no channels configured — add " + modcfg.Path(a.Name())
|
||
}
|
||
return "known: " + strings.Join(ids, ", ")
|
||
}
|