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>
This commit is contained in:
ZeusinaandGPT-6 Bot committed 2026-10-15 16:23:54 +03:00
1 parent d324b4137b
commit f4853173b3
21 files changed
+1176 -74

No files matched your search

+29
View File
@@ -0,0 +1,29 @@
# Internal YouTube adapter
The compiled-in `youtube/1` module parses bounded Player JSON and supplies the
ordinary signed MP4 qualification policy and ordered host/TLS sample plan.
The existing YouTube execution paths select it explicitly through the core's
kind/version registry. Unsupported or absent modules fail closed.
The core retains origin validation, HTTPS/SNI/certificate checks, transport
framing/body bounds, retries/cancellation, worker ownership, cleanup/join and
proof sealing. Module output is not evidence or permission to claim PASS.
Signed URLs remain ephemeral and cannot serialize through the internal contract.
New adaptive proof v2 binds the module kind, version and immutable policy digest
inside its producer seal. Unbound proof v1 explicitly means the historical
YouTube v1 policy: fresh trusted stored proofs retain their original apply
eligibility; expired proofs reconstruct history but cannot authorize fresh apply.
Only new producer/persistence writes require sealed module-bound v2 evidence.
Cold historical evidence is unchanged. Discovery entrypoints preflight the module
before requests; absence/mismatched identity or policy is a fatal configuration
failure, not unavailable inventory or a no-strategy cooldown.
Legacy routing remains SOCKS5h with encrypted-resolver ownership guards and
origin/SNI/certificate/TLS checks, not generic all-answer approval/IP pinning.
Full request-policy capabilities and parity are prerequisites to generic cutover.
This is a real internal legacy-execution integration, not a fully generic
controller, public v3 configuration, native addon, runtime plugin or readiness
claim. Another compiled-in module can implement the closed internal contract;
public GET/HEAD v1/v2 configuration remains unchanged.
+120
View File
@@ -0,0 +1,120 @@
// 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") != "")
}
+90
View File
@@ -0,0 +1,90 @@
package youtube
import (
"encoding/json"
"fmt"
"strings"
"testing"
"time"
)
func TestDiscoverResponse(t *testing.T) {
m := Module{}
resources, err := m.DiscoverResponse([]byte(`{"playabilityStatus":{"status":"OK"},"streamingData":{"formats":[{"url":"https://rr1.unit.googlevideo.com/videoplayback?sig=secret","mimeType":"audio/mp4"}],"adaptiveFormats":[{"url":"https://rr2.unit.googlevideo.com/videoplayback?sig=secret","mimeType":"video/mp4; codecs=avc1"}]}}`))
if err != nil || len(resources) != 2 || resources[0].Kind != "audio/mp4" || resources[1].Kind != "video/mp4" {
t.Fatal(resources, err)
}
resources, err = m.DiscoverResponse([]byte(`{"playabilityStatus":{"status":"OK"},"streamingData":{"serverAbrStreamingUrl":"https://rr1.unit.googlevideo.com/sabr?sig=secret"}}`))
if err != nil || len(resources) != 0 {
t.Fatal("SABR fabricated ordinary readiness")
}
for _, body := range []string{"{", strings.Repeat(" ", (1<<20)+1), `{"playabilityStatus":{"status":"ERROR"}}`} {
if _, err := m.DiscoverResponse([]byte(body)); err == nil || strings.Contains(err.Error(), "secret") {
t.Fatal("invalid metadata accepted or disclosed")
}
}
}
func TestQualificationPlan(t *testing.T) {
m, now := Module{}, time.Unix(1800000000, 0)
if m.Binding().PolicySHA256 != "2cfc521a9784c574ceee16430aa485b9c0e9e64795486a23df5117b6265d89d5" {
t.Fatal("immutable youtube/1 policy changed: introduce a new version")
}
hosts := []string{"rr1---sn-alpha.googlevideo.com", "rr2---sn-beta.googlevideo.com", "rr5---sn-gamma.googlevideo.com"}
var resources []Resource
for _, host := range hosts {
resources = append(resources, Resource{URL: fmt.Sprintf("https://%s/videoplayback?expire=%d&sig=secret", host, now.Unix()+121), Host: host, Kind: "video/mp4"})
}
p, err := m.PlanQualification(hosts, resources, now)
if err != nil || strings.Join(p.Hosts, ",") != strings.Join(hosts, ",") || p.Protocols != [2]string{"tls12", "tls13"} || p.Samples != 3 || p.MinBytes != 1024 || p.WarmupLimit != 10 || p.WarmupTimeout != 120*time.Second {
t.Fatal(p, err)
}
for _, mode := range []string{"expired", "unsigned", "sabr", "missing", "diversity"} {
t.Run(mode, func(t *testing.T) {
r, h := append([]Resource(nil), resources...), append([]string(nil), hosts...)
switch mode {
case "expired":
r[0].URL = strings.Replace(r[0].URL, "1800000121", "1800000120", 1)
case "unsigned":
r[0].URL = strings.Split(r[0].URL, "&sig=")[0]
case "sabr":
r[0].URL = strings.Replace(r[0].URL, "videoplayback", "sabr", 1)
case "missing":
r = r[:2]
case "diversity":
h[1] = h[0]
}
if _, err := m.PlanQualification(h, r, now); err == nil {
t.Fatal("invalid sample accepted")
}
})
}
encoded, err := json.Marshal(struct {
Resources []Resource
Plan Plan
}{resources, p})
if err != nil || strings.Contains(string(encoded), "secret") || strings.Contains(string(encoded), hosts[0]) {
t.Fatal("ephemeral data serialized", string(encoded), err)
}
}
func TestSignedSampleFresh(t *testing.T) {
now := time.Unix(1800000000, 900000000)
for _, tc := range []struct {
query string
want bool
}{
{"expire=1800000121&sig=secret", true},
{"expire=1800000120&sig=secret", false},
{"expire=1800000121&signature=secret", true},
{"expire=1800000121&lsig=secret", true},
{"expire=1800000121", false},
{"expire=invalid&sig=secret", false},
{"expire=9223372036854775808&sig=secret", false},
} {
r := Resource{URL: "https://rr1---sn-alpha.googlevideo.com/videoplayback?" + tc.query, Kind: "video/mp4"}
if SignedSampleFresh(r, now, 120*time.Second) != tc.want {
t.Fatal("signed expiry boundary changed")
}
}
}