Files
zapret-autotune/cmd/zapret-autotune/managed_googlevideo_runtime.go
T

217 lines
8.4 KiB
Go

package main
import (
"context"
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"os"
"reflect"
"strconv"
"strings"
"time"
)
// ponytail: one audited composite only. New native variants need an explicit
// semantic allowlist and regressions, not the generic shell-safe args validator.
const managedGooglevideoArgs = "--lua-init=@/opt/zapret2/lua/zapret-auto.lua --payload=all --in-range=-s34228 --lua-desync=circular:fails=1:retrans=2 --payload=tls_client_hello --lua-desync=fake:blob=fake_default_tls:tcp_ts=-1000:repeats=1:strategy=1 --lua-desync=multidisorder:pos=1,midsld:strategy=1 --lua-desync=fakeddisorder:pos=midsld:tcp_flags_set=SYN:strategy=2"
// Source inspected from the existing prod-derived prototype image. The default
// detector uses incoming seq >4096 for success and outgoing retrans <=32768;
// circular orchestrates only tagged instances, with contiguous strategy IDs.
// Incoming range must exceed 4096 (default x0-x0 cannot learn success).
const managedGooglevideoLuaHash = "aacfde0c95c3058f8e95f5d7d244398bdc03ebf846a8f17322129fb543366a3d"
const managedGooglevideoCollector = "owned-nfq-media-range-v1"
// Proof freshness is deliberately bounded to one day; expired proofs require a
// new owned run, never a refresh of their timestamp or cold summary rows.
const managedGooglevideoProofTTL = 24 * time.Hour
var errGooglevideoInsufficientSamples = errors.New("insufficient fresh ordinary signed GGC samples")
type GooglevideoRuntimeAttestation struct {
Collector string `json:"collector"`
LuaSHA256 string `json:"lua_sha256"`
RulesSHA256 string `json:"rules_sha256"`
WorkerArgs []string `json:"worker_args"`
CheckedAt time.Time `json:"checked_at"`
}
// Not serialized: DTO construction or unmarshalling cannot authorize a write.
// This binds the completed producer's exact facts, including failed warmups.
type googlevideoProducerSeal [32]byte
func googlevideoProofSeal(p GooglevideoQualificationProof) googlevideoProducerSeal {
raw, _ := json.Marshal(p)
return sha256.Sum256(raw)
}
func validateNativeGooglevideoArgs(args string) error {
if !reflect.DeepEqual(strings.Fields(args), strings.Fields(managedGooglevideoArgs)) {
return errors.New("unattested native circular semantics")
}
return nil
}
func (a *GooglevideoRuntimeAttestation) validate(c storedCandidate) error {
if err := a.validateStored(c); err != nil {
return err
}
if time.Since(a.CheckedAt) > managedGooglevideoProofTTL {
return errors.New("stale owned native attestation")
}
return nil
}
// Age affects new qualification, not reconstruction of journal-owned bytes.
func (a *GooglevideoRuntimeAttestation) validateStored(c storedCandidate) error {
if a == nil || a.Collector != managedGooglevideoCollector || a.LuaSHA256 != managedGooglevideoLuaHash || a.RulesSHA256 != fmt.Sprintf("%x", sha256.Sum256([]byte(managedAssetRules))) || !reflect.DeepEqual(a.WorkerArgs, managedProbeWorkerArgs(c.Endpoints, c.Args)) || a.CheckedAt.IsZero() || time.Since(a.CheckedAt) < -time.Minute {
return errors.New("missing, stale or mismatched owned native attestation")
}
return validateNativeGooglevideoArgs(c.Args)
}
// Caller gathers these resources from fresh Player queries for configured video
// samples. Never synthesize hosts by rewriting a signed URL. One-host inventory
// is an explicit unresolved result, before any worker or nft state is touched.
func newManagedGooglevideoVerifier(target managedDeps) func(context.Context, storedCandidate, []mediaResource) (GooglevideoQualificationProof, error) {
r := newManagedAssetRuntime(target)
guard := r.guard
r.guard = func() error {
if err := guard(); err != nil {
return err
}
lua, err := os.ReadFile("/opt/zapret2/lua/zapret-auto.lua")
if err != nil || fmt.Sprintf("%x", sha256.Sum256(lua)) != managedGooglevideoLuaHash {
return errors.New("unverified native automation Lua source")
}
return nil
}
return r.verifyGooglevideo
}
func (r managedAssetRuntime) verifyGooglevideo(ctx context.Context, c storedCandidate, resources []mediaResource) (GooglevideoQualificationProof, error) {
p := GooglevideoQualificationProof{Version: 1, Mode: QualificationAdaptive, SourceKind: NativeCircularPostWarmup, Args: c.Args}
if c.Group != "googlevideo" || c.Mode != QualificationAdaptive {
return p, errors.New("adaptive googlevideo definition required")
}
if err := validateNativeGooglevideoArgs(c.Args); err != nil {
return p, err
}
byHost := map[string]mediaResource{}
for _, resource := range resources {
u, err := privateResourceURL(resource.url, resource.host)
if err != nil || u.Path != "/videoplayback" || (resource.kind != "video/mp4" && resource.kind != "audio/mp4") {
return p, errGooglevideoInsufficientSamples
}
q := u.Query()
expires, err := strconv.ParseInt(q.Get("expire"), 10, 64)
if err != nil || expires <= time.Now().Unix()+120 || (q.Get("sig") == "" && q.Get("signature") == "" && q.Get("lsig") == "") {
return p, errGooglevideoInsufficientSamples
}
byHost[resource.host] = resource
}
// Validate representative host diversity using the shared gate before capture.
for _, host := range c.Endpoints {
if _, ok := byHost[host]; !ok {
return p, errGooglevideoInsufficientSamples
}
p.Hosts = append(p.Hosts, GooglevideoHostProof{Host: host})
}
if err := validateGooglevideoHosts(c.Endpoints); err != nil {
return p, errGooglevideoInsufficientSamples
}
err := r.withOwnedWorker(ctx, c.Endpoints, c.Args, func(probeCtx context.Context) error {
for i := range p.Hosts {
h := &p.Hosts[i]
h.WarmupLimit = 10
start := time.Now()
warmCtx, cancel := context.WithTimeout(probeCtx, 120*time.Second)
learned := map[string]bool{}
for h.WarmupAttempts < h.WarmupLimit && len(learned) < 2 && warmCtx.Err() == nil {
protocol := []string{"tls12", "tls13"}[h.WarmupAttempts%2]
h.WarmupAttempts++
_, pass, err := collectMediaTransfer(warmCtx, r.proxy, byHost[h.Host], protocol, r.curl)
if err != nil {
h.WarmupFailures++
if !managedGooglevideoTransferNonpass(err) && warmCtx.Err() == nil {
cancel()
return err
}
} else if pass.Bytes < 1024 {
h.WarmupFailures++
} else {
learned[protocol] = true
}
}
h.WarmupMS = float64(time.Since(start).Microseconds()) / 1000
cancel()
if probeCtx.Err() != nil {
return probeCtx.Err()
}
if len(learned) != 2 || h.WarmupMS > 120000 {
return errCandidateNonpass
}
for _, protocol := range []string{"tls12", "tls13"} {
for n := 0; n < 3; n++ {
_, pass, err := collectMediaTransfer(probeCtx, r.proxy, byHost[h.Host], protocol, r.curl)
if err != nil {
if managedGooglevideoTransferNonpass(err) {
return errCandidateNonpass
}
return err
}
if pass.Bytes < 1024 {
return errCandidateNonpass
}
h.Passes = append(h.Passes, pass)
}
}
}
return nil
})
if err != nil {
return p, err
}
// Nothing attests until the owned consumer has been stopped and joined.
p.Attestation = &GooglevideoRuntimeAttestation{Collector: managedGooglevideoCollector, LuaSHA256: managedGooglevideoLuaHash, RulesSHA256: fmt.Sprintf("%x", sha256.Sum256([]byte(managedAssetRules))), WorkerArgs: managedProbeWorkerArgs(c.Endpoints, c.Args), CheckedAt: time.Now().UTC()}
if err := p.validate(c); err != nil {
return p, err
}
seal := googlevideoProofSeal(p)
p.producerSeal = &seal
return p, nil
}
func managedGooglevideoTransferNonpass(err error) bool {
if managedAssetTransferNonpass(err) {
return true
}
if err == nil {
return false
}
if err.Error() == "invalid media range or container" {
return true
}
if e, ok := err.(interface{ Unwrap() error }); ok {
return managedGooglevideoTransferNonpass(e.Unwrap())
}
return false
}
func validateGooglevideoHosts(hosts []string) error {
seen, roots, prefixes := map[string]bool{}, map[string]bool{}, map[string]bool{}
for _, host := range hosts {
prefix, root, ok := strings.Cut(strings.TrimSuffix(host, ".googlevideo.com"), "---sn-")
if !ok || !strings.HasSuffix(host, ".googlevideo.com") || !validDomain(host) || host != strings.ToLower(host) || seen[host] || root == "" || len(prefix) < 3 || !strings.HasPrefix(prefix, "rr") || strings.Trim(prefix[2:], "0123456789") != "" {
return errGooglevideoInsufficientSamples
}
seen[host], roots[strings.Split(root, "-")[0]], prefixes[prefix] = true, true, true
}
if len(hosts) < 3 || len(roots) < 2 || len(prefixes) < 2 {
return errGooglevideoInsufficientSamples
}
return nil
}