Null-DRM-Official/apps/pkg/wvkey/ensure.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

172 lines
4.8 KiB
Go

package wvkey
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
// Runtime is a resolved Python + wvkey.py pair ready for Fetch*.
type Runtime struct {
Python string
Script string
}
// ScriptPath returns <root>/apps/wvkey/wvkey.py.
func ScriptPath(root string) string {
return filepath.Join(root, "apps", "wvkey", "wvkey.py")
}
// VenvPython returns the platform venv interpreter under root, if present.
func VenvPython(root string) string {
candidates := []string{
filepath.Join(root, ".venv", "Scripts", "python.exe"),
filepath.Join(root, ".venv", "bin", "python"),
filepath.Join(root, ".venv", "bin", "python3"),
}
for _, c := range candidates {
if fileExists(c) {
return c
}
}
return ""
}
// EnsureRuntime makes sure wvkey.py exists and a usable Python with
// pywidevine is available under root (creating .venv when needed).
// Works on Windows, Linux, and macOS.
func EnsureRuntime(root string) (Runtime, error) {
var zero Runtime
if root == "" {
return zero, fmt.Errorf("wvkey: install root required")
}
script := ScriptPath(root)
if !fileExists(script) {
return zero, fmt.Errorf("wvkey script missing at %s — re-run streamd so bootstrap can materialise apps/wvkey/", script)
}
req := filepath.Join(root, "apps", "wvkey", "requirements.txt")
if py := VenvPython(root); py != "" {
if hasPywidevine(py) {
return Runtime{Python: py, Script: script}, nil
}
if err := pipInstall(py, req); err != nil {
return zero, err
}
if !hasPywidevine(py) {
return zero, fmt.Errorf("wvkey: installed requirements but pywidevine still missing in %s", py)
}
return Runtime{Python: py, Script: script}, nil
}
base, err := findSystemPython()
if err != nil {
return zero, err
}
venvDir := filepath.Join(root, ".venv")
fmt.Fprintf(os.Stderr, "agent: creating Widevine venv at %s …\n", venvDir)
if err := createVenv(base, venvDir); err != nil {
return zero, err
}
py := VenvPython(root)
if py == "" {
return zero, fmt.Errorf("wvkey: venv created at %s but no python binary found", venvDir)
}
if err := pipInstall(py, req); err != nil {
return zero, err
}
if !hasPywidevine(py) {
return zero, fmt.Errorf("wvkey: pywidevine missing after pip install (%s)", py)
}
fmt.Fprintf(os.Stderr, "agent: Widevine helper ready (%s)\n", py)
return Runtime{Python: py, Script: script}, nil
}
// FindPython prefers an existing install-root venv, then PATH helpers.
func FindPython(root string) string {
if root != "" {
if py := VenvPython(root); py != "" {
return py
}
}
return findPython()
}
func fileExists(path string) bool {
st, err := os.Stat(path)
return err == nil && !st.IsDir()
}
func hasPywidevine(py string) bool {
cmd := exec.Command(py, "-c", "import pywidevine")
return cmd.Run() == nil
}
func pipInstall(py, requirements string) error {
if !fileExists(requirements) {
return fmt.Errorf("wvkey: requirements missing at %s", requirements)
}
fmt.Fprintf(os.Stderr, "agent: pip install -r %s …\n", requirements)
cmd := exec.Command(py, "-m", "pip", "install", "-r", requirements)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("wvkey: pip install failed: %w\n%s", err, strings.TrimSpace(string(out)))
}
return nil
}
func createVenv(basePython, venvDir string) error {
cmd := exec.Command(basePython, "-m", "venv", venvDir)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("wvkey: python -m venv %s failed: %w\n%s", venvDir, err, strings.TrimSpace(string(out)))
}
return nil
}
func findSystemPython() (string, error) {
var names []string
switch runtime.GOOS {
case "windows":
// Prefer the py launcher (real install) over the Store stub named "python".
names = []string{"py", "python3", "python"}
default:
names = []string{"python3", "python"}
}
for _, name := range names {
if name == "py" {
// `py -3` on Windows
if p, err := exec.LookPath("py"); err == nil {
cmd := exec.Command(p, "-3", "-c", "import sys; print(sys.executable)")
out, err := cmd.Output()
if err == nil {
exe := strings.TrimSpace(string(out))
if fileExists(exe) {
return exe, nil
}
}
}
continue
}
if p, err := exec.LookPath(name); err == nil {
// Skip the WindowsApps Store alias stub when it cannot run -m venv.
if runtime.GOOS == "windows" && strings.Contains(strings.ToLower(p), `windowsapps`) {
cmd := exec.Command(p, "-c", "import sys; print(sys.executable)")
out, err := cmd.Output()
if err != nil {
continue
}
exe := strings.TrimSpace(string(out))
if !fileExists(exe) || strings.Contains(strings.ToLower(exe), `windowsapps`) {
continue
}
return exe, nil
}
return p, nil
}
}
return "", fmt.Errorf("wvkey: no system Python found — install Python 3.10+ (Windows/macOS/Linux) so the agent can create .venv for Widevine keys")
}