Files
zapret-autotune/cmd/zapret-autotune/managed_googlevideo_runtime.go
T
ZeusinaandGPT-6 Bot f4853173b3 feat: extract versioned YouTube service adapter
Wire pure Player parsing and strict qualification planning through a compiled-in kind/version registry. Fail closed before discovery requests when the required module is absent or mismatched.

Require sealed module-bound v2 evidence for new writes while preserving fresh trusted v1 apply eligibility and historical rollback. Keep legacy SOCKS5h routing, separate suffix/asset policy and public configuration unchanged; generic controller cutover and field acceptance remain pending.

Co-authored-by: GPT-6 <gpt-6@openai.com>
2026-10-15 16:23:54 +03:00

270 lines
9.6 KiB
Go

package main
import (
"context"
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"reflect"
"strings"
"time"
"zapret-autotune/internal/adapter/youtube"
)
// 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: 2, 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
}
module, err := requiredYoutubeAdapter()
if err != nil {
return p, err
}
binding := module.Binding()
p.Adapter = &binding
byHost := map[string]mediaResource{}
var samples []youtube.Resource
for _, resource := range resources {
if _, err := privateResourceURL(resource.url, resource.host); err != nil {
return p, errGooglevideoInsufficientSamples
}
byHost[resource.host] = resource
samples = append(samples, youtube.Resource{URL: resource.url, Host: resource.host, Kind: resource.kind})
}
if validateGooglevideoHosts(c.Endpoints) != nil {
return p, errGooglevideoInsufficientSamples
}
plan, err := module.PlanQualification(c.Endpoints, samples, time.Now())
if err != nil {
return p, errGooglevideoInsufficientSamples
}
for _, host := range plan.Hosts {
p.Hosts = append(p.Hosts, GooglevideoHostProof{Host: host})
}
err = r.withOwnedWorker(ctx, c.Endpoints, c.Args, func(probeCtx context.Context) error {
for i := range p.Hosts {
h := &p.Hosts[i]
h.WarmupLimit = plan.WarmupLimit
start := time.Now()
warmCtx, cancel := context.WithTimeout(probeCtx, plan.WarmupTimeout)
learned := map[string]bool{}
for h.WarmupAttempts < h.WarmupLimit && len(learned) < len(plan.Protocols) && warmCtx.Err() == nil {
protocol := plan.Protocols[h.WarmupAttempts%len(plan.Protocols)]
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 < plan.MinBytes {
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) != len(plan.Protocols) || h.WarmupMS > float64(plan.WarmupTimeout/time.Millisecond) {
return errCandidateNonpass
}
for _, protocol := range plan.Protocols {
for n := 0; n < plan.Samples; 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 < plan.MinBytes {
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 {
return managedResourceTransferNonpass(err, false, false)
}
func managedResourceTransferNonpass(err error, metadata, image bool) bool {
// Collector normalization wraps known transfer failures as fatal for generic
// consumers. Only that boundary may unwrap the wrapper; guard/cleanup failures
// and mixed error sets must never authorize absence or a daemon restart.
if e, ok := err.(*resourceError); ok {
cause := e.cause
if fatal, ok := cause.(*managedRecoveryFatal); ok {
cause = fatal.Err
}
return managedResourceTransferNonpass(cause, metadata, image)
}
if _, ok := err.(*managedRecoveryFatal); ok {
return false
}
if joined, ok := err.(interface{ Unwrap() []error }); ok {
children := joined.Unwrap()
if len(children) == 0 {
return false
}
for _, child := range children {
if !managedResourceTransferNonpass(child, metadata, image) {
return false
}
}
return true
}
if wrapped, ok := err.(interface{ Unwrap() error }); ok {
return managedResourceTransferNonpass(wrapped.Unwrap(), metadata, image)
}
if _, ok := err.(imageBodyRejection); ok {
return image
}
// Metadata curl uses --fail. A non-2xx response is missing inventory,
// never media rejection or candidate-absence evidence.
if exit, ok := err.(*exec.ExitError); metadata && ok && exit.ExitCode() == 22 {
return true
}
if managedAssetTransferNonpass(err) {
return true
}
if err == nil {
return false
}
if err.Error() == "invalid media range or container" {
return true
}
return false
}
func validateGooglevideoHosts(hosts []string) error {
for _, host := range hosts {
if !validDomain(host) {
return errGooglevideoInsufficientSamples
}
}
module, err := requiredYoutubeAdapter()
if err != nil {
return err
}
if module.ValidateHosts(hosts) != nil {
return errGooglevideoInsufficientSamples
}
return nil
}
// Historical v1 reconstruction must not depend on a future registry default.
// This pinned contract remains available even when the execution module is absent.
func validateLegacyYoutubeV1Hosts(hosts []string) error {
for _, host := range hosts {
if !validDomain(host) {
return errGooglevideoInsufficientSamples
}
}
return (youtube.Module{}).ValidateHosts(hosts)
}