Files
zapret-autotune/cmd/zapret-autotune/managed_googlevideo_recovery.go
T
ZeusinaandGPT-6 Bot 3fc8563995 feat: discover and qualify Googlevideo CDNs autonomously
Retain strict three-host native qualification, discover fresh representative inventory automatically, and recheck deployed media before promotion. Document offline-only verification and outstanding field gates.

Co-authored-by: GPT-6 <gpt-6@openai.com>
2026-10-13 10:12:10 +03:00

283 lines
9.5 KiB
Go

package main
import (
"context"
"errors"
"fmt"
"sort"
"strconv"
"strings"
"time"
)
// Ordinary unresolved state, not a target/ownership guard failure.
type InsufficientAdaptiveSamples struct {
Attempts, DistinctHosts, Unavailable, Unsupported, Rejected int
}
func (e *InsufficientAdaptiveSamples) Error() string {
return fmt.Sprintf("DEGRADED: insufficient fresh ordinary signed GGC samples (attempts=%d distinct_hosts=%d unavailable=%d unsupported=%d rejected=%d)", e.Attempts, e.DistinctHosts, e.Unavailable, e.Unsupported, e.Rejected)
}
func (*InsufficientAdaptiveSamples) Unwrap() []error {
return []error{errGooglevideoInsufficientSamples}
}
// Sequential round-robin: at most 15 fresh requests, with no child goroutines.
const managedGooglevideoSampleRounds = 3
const managedGooglevideoDiscoveryTimeout = 3 * time.Minute
// Three hosts each need up to 120s warmup + six 35s transfers. Leave margin
// for daemon lifecycle as well; short-lived URLs are refetched, never rewritten.
const managedGooglevideoSampleTTL = 20 * time.Minute
func freshGooglevideoSample(r mediaResource) bool {
// The same exact GGC hostname syntax as the representative gate; reject bad
// inventory before counting it, without fetching any returned URL here.
prefix, root, ok := strings.Cut(strings.TrimSuffix(r.host, ".googlevideo.com"), "---sn-")
if !ok || !strings.HasSuffix(r.host, ".googlevideo.com") || !validDomain(r.host) || r.host != strings.ToLower(r.host) || root == "" || len(prefix) < 3 || !strings.HasPrefix(prefix, "rr") || strings.Trim(prefix[2:], "0123456789") != "" {
return false
}
u, err := privateResourceURL(r.url, r.host)
if err != nil || u.Path != "/videoplayback" || (r.kind != "video/mp4" && r.kind != "audio/mp4") {
return false
}
q := u.Query()
expires, err := strconv.ParseInt(q.Get("expire"), 10, 64)
return err == nil && expires > time.Now().Add(managedGooglevideoSampleTTL).Unix() && (q.Get("sig") != "" || q.Get("signature") != "" || q.Get("lsig") != "")
}
// Choose exactly three actual hosts passing the unchanged representative gate.
func representativeGooglevideoSamples(byHost map[string]mediaResource) []mediaResource {
hosts := make([]string, 0, len(byHost))
for host, r := range byHost {
if freshGooglevideoSample(r) {
hosts = append(hosts, host)
}
}
sort.Strings(hosts)
for i := 0; i < len(hosts); i++ {
for j := i + 1; j < len(hosts); j++ {
for k := j + 1; k < len(hosts); k++ {
if validateGooglevideoHosts([]string{hosts[i], hosts[j], hosts[k]}) == nil {
return []mediaResource{byHost[hosts[i]], byHost[hosts[j]], byHost[hosts[k]]}
}
}
}
}
return nil
}
func managedGooglevideoSamples(ids []string, request func(context.Context, string) ([]byte, error)) func(context.Context) ([]mediaResource, error) {
ids = append([]string(nil), ids...)
return func(ctx context.Context) ([]mediaResource, error) {
if len(ids) == 0 || len(ids) > 5 || request == nil {
return nil, &managedRecoveryFatal{Err: errors.New("invalid sample collector configuration")}
}
for _, id := range ids {
if !videoIDPattern.MatchString(id) {
return nil, &managedRecoveryFatal{Err: errors.New("invalid sample video ID")}
}
}
bounded, cancel := context.WithTimeout(ctx, managedGooglevideoDiscoveryTimeout)
defer cancel()
stats := &InsufficientAdaptiveSamples{}
byHost := map[string]mediaResource{}
for round := 0; round < managedGooglevideoSampleRounds; round++ {
for _, id := range ids {
if ctx.Err() != nil {
return nil, managedRecoveryContextError(ctx, ctx.Err())
}
if bounded.Err() != nil {
return nil, stats
}
stats.Attempts++
body, err := request(bounded, id)
var fatal *managedRecoveryFatal
if ctx.Err() != nil || errors.As(err, &fatal) {
return nil, managedRecoveryContextError(ctx, err)
}
if err != nil {
if !managedResourceTransferNonpass(err, true, false) && !(bounded.Err() != nil && managedRecoveryTimeoutSafe(err)) {
return nil, &managedRecoveryFatal{Err: err}
}
stats.Unavailable++
if bounded.Err() != nil {
return nil, stats
}
continue
}
r, _, err := playerMediaResource(body)
if err != nil {
stats.Unavailable++
continue
} // Missing/SABR is unknown, never fabricated.
if r.url == "" {
stats.Unsupported++
continue
}
if !freshGooglevideoSample(r) {
stats.Rejected++
continue
}
byHost[r.host] = r
stats.DistinctHosts = len(byHost)
if ctx.Err() != nil {
return nil, managedRecoveryContextError(ctx, ctx.Err())
}
if bounded.Err() != nil {
return nil, stats
}
if resources := representativeGooglevideoSamples(byHost); resources != nil {
return resources, nil
}
}
}
return nil, stats
}
}
func recoverManagedGooglevideo(ctx context.Context, d managedRecoveryDeps) (int64, error) {
m := *d.transaction
journal, err := m.load()
if err != nil {
return 0, &managedRecoveryFatal{Err: err}
}
if journal.Scope == GooglevideoScopeExperimental {
return recoverManagedExperimental(ctx, d)
}
return qualifyManagedGooglevideo(ctx, d)
}
// Both autonomous adaptive recovery and an operationally rechecked experiment
// use the same unchanged three-host owned proof and transactional apply gate.
func qualifyManagedGooglevideo(ctx context.Context, d managedRecoveryDeps) (int64, error) {
m := *d.transaction
if !d.service || d.mediaSamples == nil || d.verifyGooglevideo == nil {
return 0, &InsufficientAdaptiveSamples{}
}
current, err := m.ActiveCandidates(d.db)
if err != nil {
return 0, &managedRecoveryFatal{Err: err}
}
saved, err := listQualifiedCandidates(d.db, "googlevideo", 10)
if err != nil {
return 0, &managedRecoveryFatal{Err: err}
}
// Revalidate saved native definitions first. Old proof never authorizes apply.
trials := []storedCandidate{}
for _, c := range saved {
if c.Mode == QualificationAdaptive && validateNativeGooglevideoArgs(c.Args) == nil {
trials = append(trials, c)
}
}
trials = append(trials, storedCandidate{Args: managedGooglevideoArgs})
for _, c := range trials {
resources, err := d.mediaSamples(ctx) // Fresh on every stage, URLs stay private.
if err != nil {
return 0, err
}
c.ID, c.Group, c.Mode = 0, "googlevideo", QualificationAdaptive
c.Endpoints = nil
for _, r := range resources {
c.Endpoints = append(c.Endpoints, r.host)
}
if validateGooglevideoHosts(c.Endpoints) != nil {
return 0, &InsufficientAdaptiveSamples{}
}
err = d.run(ctx, managedZapretScript, "stop")
var proof GooglevideoQualificationProof
if err == nil {
proof, err = d.verifyGooglevideo(ctx, c, resources)
}
var unsafe *managedAssetCleanupFailure
if errors.As(err, &unsafe) {
return 0, err
}
cleanup, cancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second)
restoreErr := d.run(cleanup, managedZapretScript, "start")
cancel()
if restoreErr != nil {
return 0, &managedRecoveryFatal{Err: errors.Join(err, restoreErr)}
}
var fatal *managedRecoveryFatal
if ctx.Err() != nil || errors.As(err, &fatal) {
return 0, managedRecoveryContextError(ctx, err)
}
if errors.Is(err, errGooglevideoInsufficientSamples) {
return 0, &InsufficientAdaptiveSamples{}
}
if errors.Is(err, errCandidateNonpass) {
continue
}
if err != nil {
return 0, managedRecoveryContextError(ctx, err)
}
if err := ctx.Err(); err != nil {
return 0, err
}
tx, err := d.db.Begin()
if err != nil {
return 0, &managedRecoveryFatal{Err: err}
}
id, err := insertStrategyCandidate(tx, "googlevideo", c.Endpoints, c.Args, nil, QualificationAdaptive)
if err == nil {
err = saveAdaptiveGooglevideoEvidence(tx, id, proof)
}
if err == nil {
err = tx.Commit()
} else {
_ = tx.Rollback()
}
if err != nil {
return 0, &managedRecoveryFatal{Err: err}
}
desired, err := managedFullDesiredSet(d.db, current, []int64{id})
if err != nil {
return 0, &managedRecoveryFatal{Err: err}
}
m.Reload = func() error {
cleanup, cancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second)
defer cancel()
return d.run(cleanup, managedZapretScript, "restart")
}
m.Health = func() (string, error) {
if d.frontHealth == nil || d.recheckGooglevideo == nil {
return "DEGRADED", errors.New("frontend and deployed media health required")
}
status, err := d.frontHealth(ctx)
if err != nil || status != "OK" {
return status, managedRecoveryContextError(ctx, err)
}
deployed, err := readStrategyCandidate(d.db, id)
if err == nil {
err = d.recheckGooglevideo(ctx, deployed, resources, &m)
}
if err != nil {
var fatal *managedRecoveryFatal
if ctx.Err() != nil || errors.As(err, &fatal) || errors.Is(err, context.Canceled) {
return "DEGRADED", managedRecoveryContextError(ctx, err)
}
if managedGooglevideoTransferNonpass(err) || errors.Is(err, errCandidateNonpass) {
return "DEGRADED", nil
}
return "DEGRADED", managedRecoveryContextError(ctx, err)
}
if ctx.Err() != nil {
return "DEGRADED", managedRecoveryContextError(ctx, ctx.Err())
}
return "OK", nil // Stage readiness only; whole-service monitor is unchanged.
}
err = m.Apply(d.db, desired, GooglevideoScopeSuffix)
var rollback *managedRollbackError
var rejection *managedHealthRejection
if err != nil && !errors.As(err, &rejection) && !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) {
return 0, &managedRecoveryFatal{Err: err}
}
if errors.As(err, &rollback) {
return 0, &managedRecoveryFatal{Err: err}
}
return id, err
}
return 0, errNoQualifiedStrategy
}