Null-DRM-Official/apps/pkg/wvkey/wvkey.go
404errordeveloper 066e018749
All checks were successful
release / build-and-upload (push) Successful in 6m18s
modules-release / build-and-upload (push) Successful in 5m18s
streamd: preview quality picker, dashboard polish, C4 wrap keys
- Preview player can select FHD/HD/SD (or ABR master) from publish links
- Embed dashboard screenshots in README; Linux streamd+agent setup guide
- C4 Widevine wrap license mode + require --wvd; prefer Widevine PSSH
- Channel 4 and My5 modules; streamd auth, variants, schedules, HLS gate
2026-10-09 20:57:43 +02:00

142 lines
3.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 := opt.Python
if py == "" {
py = findPython()
}
script := opt.Script
if script == "" {
return nil, fmt.Errorf("wvkey script path required")
}
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
}
func findPython() string {
candidates := []string{
filepath.Join(".venv", "Scripts", "python.exe"),
filepath.Join(".venv", "bin", "python"),
"python3",
"python",
}
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"
}