175 lines
5.6 KiB
Go
175 lines
5.6 KiB
Go
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
|
|
|
|
// Keep request diagnostics private while retaining typed causes for supervision.
|
|
type playerRequestError struct{ cause error }
|
|
|
|
func (e *playerRequestError) Error() string { return "player request failed or exceeded output limit" }
|
|
func (e *playerRequestError) Unwrap() error { return e.cause }
|
|
|
|
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 discoveryFlags(args []string) (string, string, error) {
|
|
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) {
|
|
return "", "", errors.New("requires --video-id (11 characters) and --proxy socks5://HOST:PORT without credentials")
|
|
}
|
|
return *videoID, *proxy, nil
|
|
}
|
|
|
|
func discoverYoutube(args []string, stdout, stderr io.Writer, runner playerRunner) int {
|
|
videoID, proxy, err := discoveryFlags(args)
|
|
if err != nil {
|
|
fmt.Fprintln(stderr, "error: discover youtube", err)
|
|
return 2
|
|
}
|
|
body, err := requestPlayer(videoID, proxy, runner)
|
|
if err != nil {
|
|
fmt.Fprintln(stderr, "error:", err)
|
|
return 1
|
|
}
|
|
host, err := playerHostname(body)
|
|
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
|
|
}
|
|
|
|
func requestPlayer(videoID, proxy string, runner playerRunner) ([]byte, error) {
|
|
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 {
|
|
if output.exceeded || diagnostics.exceeded {
|
|
// An output failure remains unsafe even if the child was also canceled.
|
|
err = errors.Join(err, errors.New("player output limit exceeded"))
|
|
}
|
|
return nil, &playerRequestError{cause: err}
|
|
}
|
|
return body.Bytes(), nil
|
|
}
|