// Package youtube contains pure service semantics, not transport or worker ownership. package youtube import ( "crypto/sha256" "encoding/json" "errors" "fmt" "mime" "net/url" "strconv" "strings" "time" "zapret-autotune/internal/adapter" ) const Kind = "youtube" const Version = 1 const InventoryTTL = 20 * time.Minute // Immutable v1 policy. Changes require another module version, not a new meaning // for existing evidence. Legacy proof v1 uses this same historical policy. const policyV1 = "youtube/1;ggc>=3;roots>=2;prefixes>=2;ordinary-signed-mp4;fresh>120s;inventory>20m;tls12,tls13;warmup<=10,120s;steady=3/protocol;min=1024" type Binding = adapter.Binding type Module struct{} func (Module) Binding() Binding { return Binding{Kind: Kind, Version: Version, PolicySHA256: fmt.Sprintf("%x", sha256.Sum256([]byte(policyV1)))} } // Resource is ephemeral: never serialize signed URLs or inventory metadata. type Resource = adapter.Resource // DiscoverResponse preserves player format order. The core must validate every // returned origin before selecting or fetching it. SABR is not supported. func (Module) DiscoverResponse(body []byte) ([]Resource, error) { type format struct { URL string `json:"url"` MimeType string `json:"mimeType"` } var p struct { PlayabilityStatus struct { Status string `json:"status"` } `json:"playabilityStatus"` StreamingData struct { Formats []format `json:"formats"` AdaptiveFormats []format `json:"adaptiveFormats"` } `json:"streamingData"` } if len(body) > 1<<20 || json.Unmarshal(body, &p) != nil || p.PlayabilityStatus.Status != "OK" { return nil, errors.New("invalid playable metadata") } var resources []Resource for _, f := range append(p.StreamingData.Formats, p.StreamingData.AdaptiveFormats...) { u, err := url.Parse(f.URL) if err != nil || !strings.HasSuffix(u.Hostname(), ".googlevideo.com") { continue } kind, _, err := mime.ParseMediaType(f.MimeType) if err == nil && (kind == "video/mp4" || kind == "audio/mp4") { resources = append(resources, Resource{URL: f.URL, Host: u.Hostname(), Kind: kind}) } } return resources, nil } // ValidateHosts checks service diversity; DNS/origin policy remains in the core. func (Module) ValidateHosts(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") || host != strings.ToLower(host) || seen[host] || root == "" || len(prefix) < 3 || !strings.HasPrefix(prefix, "rr") || strings.Trim(prefix[2:], "0123456789") != "" { return errors.New("insufficient representative GGC hosts") } seen[host], roots[strings.Split(root, "-")[0]], prefixes[prefix] = true, true, true } if len(hosts) < 3 || len(roots) < 2 || len(prefixes) < 2 { return errors.New("insufficient representative GGC hosts") } return nil } type Plan = adapter.Plan // PlanQualification grants no PASS capability. Core validates origins first, // executes the ordered schedule, validates measured facts, and seals after join. func (m Module) PlanQualification(hosts []string, resources []Resource, now time.Time) (Plan, error) { p := Plan{Hosts: append([]string(nil), hosts...), Protocols: [2]string{"tls12", "tls13"}, WarmupLimit: 10, WarmupTimeout: 120 * time.Second, Samples: 3, MinBytes: 1024} if err := m.ValidateHosts(hosts); err != nil { return Plan{}, err } seen := map[string]bool{} for _, r := range resources { if !SignedSampleFresh(r, now, 120*time.Second) { return Plan{}, errors.New("insufficient fresh signed media samples") } seen[r.Host] = true } for _, host := range hosts { if !seen[host] { return Plan{}, errors.New("missing representative sample") } } return p, nil } // SignedSampleFresh checks service semantics only, not permission to fetch. // The core independently validates HTTPS origins before calling this helper. func SignedSampleFresh(r Resource, now time.Time, ttl time.Duration) bool { u, err := url.Parse(r.URL) if err != nil || u.Path != "/videoplayback" || (r.Kind != "video/mp4" && r.Kind != "audio/mp4") { return false } q := u.Query() expire, err := strconv.ParseInt(q.Get("expire"), 10, 64) return err == nil && expire > now.Add(ttl).Unix() && (q.Get("sig") != "" || q.Get("signature") != "" || q.Get("lsig") != "") }