feat(discovery): find YouTube cache hosts

Co-authored-by: GPT-6 <gpt-6@openai.com>
This commit is contained in:
ZeusinaandGPT-6 Bot committed 2026-10-09 22:11:13 +03:00
1 parent 04fd9b370d
commit db92cff2b5
5 files changed
+357 -9

No files matched your search

+148
View File
@@ -0,0 +1,148 @@
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"net"
"net/url"
"os/exec"
"regexp"
"strconv"
"strings"
"time"
)
const playerURL = "https://www.youtube.com/youtubei/v1/player"
const playerUserAgent = "com.google.youtube/21.02.35(Linux;U;Android11)gzip"
const playerResponseLimit = 1 << 20
var videoIDPattern = regexp.MustCompile(`^[A-Za-z0-9_-]{11}$`)
var discoveryDNSLabel = regexp.MustCompile(`^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$`)
type playerRunner func(context.Context, []string, io.Writer, io.Writer) error
func runPlayerCurl(ctx context.Context, args []string, stdout, stderr io.Writer) error {
cmd := exec.CommandContext(ctx, "curl", args...)
cmd.Stdout, cmd.Stderr = stdout, stderr
return cmd.Run()
}
// A failed write stops collection rather than retaining a truncated response.
type playerBoundedWriter struct {
dst io.Writer
n int
limit int
exceeded bool
}
func (w *playerBoundedWriter) Write(p []byte) (int, error) {
if len(p) > w.limit-w.n {
w.exceeded = true
return 0, errors.New("player output limit exceeded")
}
w.n += len(p)
return w.dst.Write(p)
}
func discoveryHostname(host string) bool {
if len(host) > 253 || host == "" {
return false
}
for _, label := range strings.Split(strings.ToLower(host), ".") {
if !discoveryDNSLabel.MatchString(label) {
return false
}
}
return true
}
func validDiscoveryProxy(raw string) bool {
u, err := url.Parse(raw)
if err != nil || u.Scheme != "socks5" || u.User != nil || u.Opaque != "" ||
u.Path != "" || u.RawQuery != "" || u.ForceQuery || u.Fragment != "" ||
strings.ContainsAny(raw, " \t\r\n#") {
return false
}
host, port, err := net.SplitHostPort(u.Host)
if err != nil || (net.ParseIP(host) == nil && !discoveryHostname(host)) {
return false
}
n, err := strconv.Atoi(port)
return err == nil && n > 0 && n <= 65535
}
func playerHostname(body []byte) (string, error) {
var response struct {
PlayabilityStatus struct {
Status string `json:"status"`
} `json:"playabilityStatus"`
StreamingData struct {
ServerAbrStreamingURL string `json:"serverAbrStreamingUrl"`
} `json:"streamingData"`
}
if len(body) > playerResponseLimit || json.Unmarshal(body, &response) != nil {
return "", errors.New("invalid player response")
}
if response.PlayabilityStatus.Status != "OK" {
return "", errors.New("player response is not playable")
}
u, err := url.Parse(response.StreamingData.ServerAbrStreamingURL)
if err != nil || u.Scheme != "https" || u.User != nil || u.Opaque != "" {
return "", errors.New("missing or invalid streaming URL")
}
host := strings.ToLower(u.Hostname())
if !discoveryHostname(host) || !strings.HasSuffix(host, ".googlevideo.com") ||
(u.Port() != "" && u.Port() != "443") || strings.HasSuffix(u.Host, ":") {
return "", errors.New("invalid cache hostname")
}
return host, nil
}
func discoverYoutube(args []string, stdout, stderr io.Writer, runner playerRunner) int {
flags := flag.NewFlagSet("discover youtube", flag.ContinueOnError)
flags.SetOutput(io.Discard) // flag errors can contain untrusted arguments.
videoID := flags.String("video-id", "", "")
proxy := flags.String("proxy", "", "")
if flags.Parse(args) != nil || flags.NArg() != 0 || !videoIDPattern.MatchString(*videoID) || !validDiscoveryProxy(*proxy) {
fmt.Fprintln(stderr, "error: discover youtube requires --video-id (11 characters) and --proxy socks5://HOST:PORT without credentials")
return 2
}
payload, _ := json.Marshal(map[string]interface{}{
"videoId": *videoID,
"context": map[string]interface{}{"client": map[string]interface{}{
"clientName": "ANDROID", "clientVersion": "21.02.35", "androidSdkVersion": 30,
"userAgent": playerUserAgent, "osName": "Android", "osVersion": "11",
}},
})
ctx, cancel := context.WithTimeout(context.Background(), 35*time.Second)
defer cancel()
var body bytes.Buffer
output := &playerBoundedWriter{dst: &body, limit: playerResponseLimit}
diagnostics := &playerBoundedWriter{dst: io.Discard, limit: 64 << 10}
// Disable curlrc and NO_PROXY bypasses: the explicit proxy must be used.
err := runner(ctx, []string{"--disable", "--silent", "--show-error", "--fail", "--max-time", "30",
"--noproxy", "", "--proxy", *proxy, "--user-agent", playerUserAgent, "--json", string(payload), playerURL}, output, diagnostics)
if err != nil || output.exceeded || diagnostics.exceeded {
fmt.Fprintln(stderr, "error: player request failed or exceeded output limit")
return 1
}
host, err := playerHostname(body.Bytes())
if err != nil {
fmt.Fprintln(stderr, "error:", err)
return 1
}
result := struct {
VideoID string `json:"videoId"`
Hostnames []string `json:"hostnames"`
}{*videoID, []string{host}}
if json.NewEncoder(stdout).Encode(result) != nil {
fmt.Fprintln(stderr, "error: cannot write discovery result")
return 1
}
return 0
}