Null-DRM-Official/apps/pkg/wvkey/wvkey.go
404errordeveloper a091922efa
All checks were successful
release / build-and-upload (push) Successful in 7m2s
streamd v0.2.2: session uptimes, egress DNS, Support, agent/Widevine
Dashboard Live uptimes + Recent runs from stream_sessions; overview.recent_sessions.
WireGuard netns resolv.conf + missing-deps WARN. Support donations card.
Agent start/stop/update, job_backoff default off, wvkey ensure/venv, phonecap retries,
modhost YAML enrich, DRM SetCredentials requires keys.
2026-10-10 19:26:39 +02:00

167 lines
4.5 KiB
Go

package wvkey
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
// Options for the Python wvkey.py helper.
type Options struct {
Python string // e.g. .venv/Scripts/python.exe
Script string // path to wvkey.py
WVD string
PSSH string
Auth string
PID string
LicenseURL string
UserAgent string
// WrapJSON is a captured phone license POST body (RedBee / wrapped Widevine).
WrapJSON string
}
// Fetch runs wvkey.py --quiet (ModularDrm) and returns the first KID:KEY line.
func Fetch(opt Options) (string, error) {
lines, err := run(opt, "modulardrm", false)
if err != nil {
return "", err
}
if len(lines) == 0 {
return "", fmt.Errorf("wvkey produced no KID:KEY")
}
return lines[0], nil
}
// FetchRaw runs wvkey.py --mode raw (Brightcove / octet-stream license).
func FetchRaw(opt Options) (string, error) {
lines, err := run(opt, "raw", false)
if err != nil {
return "", err
}
if len(lines) == 0 {
return "", fmt.Errorf("wvkey produced no KID:KEY")
}
return lines[0], nil
}
// FetchRawAll returns every CONTENT key from a raw license response.
func FetchRawAll(opt Options) ([]string, error) {
return run(opt, "raw", true)
}
// FetchWrapAll re-POSTs a captured JSON license body with a local CDM challenge.
func FetchWrapAll(opt Options) ([]string, error) {
return run(opt, "wrap", true)
}
func run(opt Options, mode string, all bool) ([]string, error) {
py := resolvePython(opt.Python)
script := opt.Script
if script == "" {
return nil, fmt.Errorf("wvkey script path required")
}
if !fileExists(script) {
return nil, fmt.Errorf("wvkey script not found: %s", script)
}
if !fileExists(py) {
// Bare names like "python3" are OK via LookPath inside resolvePython;
// absolute/relative paths must exist.
if filepath.IsAbs(py) || strings.Contains(py, string(os.PathSeparator)) || strings.Contains(py, "/") {
return nil, fmt.Errorf("wvkey python not found: %s (run agent once to auto-create .venv, or set python: in agent.yaml)", py)
}
}
if strings.TrimSpace(opt.WVD) == "" {
return nil, fmt.Errorf("wvkey --wvd path required")
}
if strings.TrimSpace(opt.PSSH) == "" {
return nil, fmt.Errorf("wvkey --pssh required")
}
if strings.TrimSpace(opt.LicenseURL) == "" {
return nil, fmt.Errorf("wvkey --license-url required")
}
args := []string{
script,
"--wvd", opt.WVD,
"--pssh", opt.PSSH,
"--license-url", opt.LicenseURL,
"--mode", mode,
"--quiet",
}
switch mode {
case "raw":
// no extra fields
case "wrap":
if strings.TrimSpace(opt.WrapJSON) == "" {
return nil, fmt.Errorf("wvkey wrap mode requires a captured license_body from the phone (playback must start so RedBee gets a token)")
}
args = append(args, "--wrap-json", opt.WrapJSON)
default:
args = append(args, "--auth", opt.Auth, "--pid", opt.PID)
}
if all {
args = append(args, "--all")
}
if opt.UserAgent != "" {
args = append(args, "--user-agent", opt.UserAgent)
}
cmd := exec.Command(py, args...)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("wvkey failed: %w\n%s", err, strings.TrimSpace(stderr.String()))
}
var out []string
for _, line := range strings.Split(strings.TrimSpace(stdout.String()), "\n") {
line = strings.TrimSpace(line)
if strings.Count(line, ":") == 1 && len(line) > 40 {
out = append(out, line)
}
}
if len(out) == 0 {
return nil, fmt.Errorf("wvkey produced no KID:KEY (stderr=%s)", strings.TrimSpace(stderr.String()))
}
return out, nil
}
// resolvePython uses an explicit path only when the file exists; otherwise
// discovers a usable interpreter. Passing a missing absolute path used to make
// exec fail with a confusing "not found in %PATH%" on Windows.
func resolvePython(explicit string) string {
if strings.TrimSpace(explicit) != "" {
if st, err := os.Stat(explicit); err == nil && !st.IsDir() {
return explicit
}
if !strings.Contains(explicit, string(os.PathSeparator)) && !strings.Contains(explicit, "/") {
if p, err := exec.LookPath(explicit); err == nil {
return p
}
}
}
return findPython()
}
func findPython() string {
candidates := []string{
filepath.Join(".venv", "Scripts", "python.exe"),
filepath.Join(".venv", "bin", "python"),
"python3",
"python",
"py",
}
for _, c := range candidates {
if filepath.IsAbs(c) || strings.Contains(c, string(os.PathSeparator)) {
if st, err := os.Stat(c); err == nil && !st.IsDir() {
return c
}
continue
}
if p, err := exec.LookPath(c); err == nil {
return p
}
}
return "python"
}