Canonicalize validated managed proxy schemes to socks5h for Player, metadata, resource and warmup probes; retain listener ownership, TLS/privacy checks and legacy CLI/pinned checker semantics. Stop canceled inventory requests before Player and metadata fallback, with explicit cancellation and runner-only DNS regressions. Verification: full native Go tests, affected race tests, go vet and static/config/supervision fixtures passed. An extra whole-package race attempt exceeded its 180s deadline in unrelated TestTuneCLI; affected race coverage completed separately. Co-authored-by: GPT-6 <gpt-6@openai.com>
149 lines
4.8 KiB
Go
149 lines
4.8 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net"
|
|
"net/url"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type healthConfig struct {
|
|
SOCKSProxy string
|
|
VideoID string
|
|
HealthInterval time.Duration
|
|
FailureThreshold int
|
|
RecoveryCooldown time.Duration
|
|
}
|
|
|
|
func parseHealthConfig(lookup func(string) (string, bool)) (healthConfig, error) {
|
|
value := func(key, fallback string) string {
|
|
if v, ok := lookup(key); ok {
|
|
return v
|
|
}
|
|
return fallback
|
|
}
|
|
cfg := healthConfig{
|
|
SOCKSProxy: value("ZAPRET_AUTOTUNE_SOCKS_PROXY", "socks5://127.0.0.1:11080"),
|
|
VideoID: value("ZAPRET_AUTOTUNE_VIDEO_ID", "dQw4w9WgXcQ"),
|
|
}
|
|
if _, err := managedServiceEnabled(lookup); err != nil {
|
|
return healthConfig{}, err
|
|
}
|
|
if !validLoopbackSOCKS(cfg.SOCKSProxy) {
|
|
return healthConfig{}, fmt.Errorf("ZAPRET_AUTOTUNE_SOCKS_PROXY requires socks5://LOOPBACK:PORT without credentials")
|
|
}
|
|
if !videoIDPattern.MatchString(cfg.VideoID) {
|
|
return healthConfig{}, fmt.Errorf("ZAPRET_AUTOTUNE_VIDEO_ID requires an 11-character video ID")
|
|
}
|
|
if _, _, err := managedGooglevideoConfig(lookup, cfg.VideoID); err != nil {
|
|
return healthConfig{}, err
|
|
}
|
|
for _, setting := range []struct {
|
|
key string
|
|
fallback string
|
|
dest *time.Duration
|
|
}{
|
|
{"ZAPRET_AUTOTUNE_HEALTH_INTERVAL", "5m", &cfg.HealthInterval},
|
|
{"ZAPRET_AUTOTUNE_RECOVERY_COOLDOWN", "30m", &cfg.RecoveryCooldown},
|
|
} {
|
|
duration, err := time.ParseDuration(value(setting.key, setting.fallback))
|
|
if err != nil || duration <= 0 {
|
|
return healthConfig{}, fmt.Errorf("%s requires a positive duration", setting.key)
|
|
}
|
|
*setting.dest = duration
|
|
}
|
|
threshold, err := strconv.Atoi(value("ZAPRET_AUTOTUNE_FAILURE_THRESHOLD", "3"))
|
|
if err != nil || threshold <= 0 {
|
|
return healthConfig{}, fmt.Errorf("ZAPRET_AUTOTUNE_FAILURE_THRESHOLD requires a positive integer")
|
|
}
|
|
cfg.FailureThreshold = threshold
|
|
return cfg, nil
|
|
}
|
|
|
|
func managedGooglevideoConfig(lookup func(string) (string, bool), primary string) (QualificationMode, []string, error) {
|
|
mode := QualificationCold
|
|
if raw, ok := lookup("ZAPRET_AUTOTUNE_GOOGLEVIDEO_MODE"); ok {
|
|
mode = QualificationMode(raw)
|
|
}
|
|
if mode != QualificationCold && mode != QualificationAdaptive {
|
|
return "", nil, fmt.Errorf("ZAPRET_AUTOTUNE_GOOGLEVIDEO_MODE requires cold or adaptive")
|
|
}
|
|
service, _ := managedServiceEnabled(lookup)
|
|
if mode == QualificationAdaptive && !service {
|
|
return "", nil, fmt.Errorf("adaptive googlevideo requires ZAPRET_AUTOTUNE_YOUTUBE_SERVICE=1")
|
|
}
|
|
// Only the configured primary is sampled by default; no implicit public playlist.
|
|
ids := []string{primary}
|
|
if raw, ok := lookup("ZAPRET_AUTOTUNE_GOOGLEVIDEO_VIDEO_IDS"); ok {
|
|
if json.Unmarshal([]byte(raw), &ids) != nil || len(ids) == 0 || len(ids) > 5 {
|
|
return "", nil, fmt.Errorf("GOOGLEVIDEO_VIDEO_IDS requires a JSON array of 1..5 video IDs")
|
|
}
|
|
seen := map[string]bool{}
|
|
for _, id := range ids {
|
|
if !videoIDPattern.MatchString(id) || seen[id] {
|
|
return "", nil, fmt.Errorf("GOOGLEVIDEO_VIDEO_IDS requires unique 11-character video IDs")
|
|
}
|
|
seen[id] = true
|
|
}
|
|
}
|
|
return mode, ids, nil
|
|
}
|
|
|
|
func managedServiceEnabled(lookup func(string) (string, bool)) (bool, error) {
|
|
value, exists := lookup("ZAPRET_AUTOTUNE_YOUTUBE_SERVICE")
|
|
if !exists {
|
|
return false, nil
|
|
}
|
|
if value != "0" && value != "1" {
|
|
return false, fmt.Errorf("ZAPRET_AUTOTUNE_YOUTUBE_SERVICE requires 0 or 1")
|
|
}
|
|
return value == "1", nil
|
|
}
|
|
|
|
func validLoopbackSOCKS(raw string) bool {
|
|
if !validDiscoveryProxy(raw) {
|
|
return false
|
|
}
|
|
u, err := url.Parse(raw)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
host := u.Hostname()
|
|
return strings.EqualFold(host, "localhost") || net.ParseIP(host).IsLoopback()
|
|
}
|
|
|
|
// Managed probes send hostnames through the owned SS listener, not local DNS.
|
|
// Legacy public discovery/health parsing deliberately keeps socks5 semantics.
|
|
func managedProbeProxy(raw string) (string, error) {
|
|
u, err := url.Parse(raw)
|
|
if err != nil || (u.Scheme != "socks5" && u.Scheme != "socks5h") || strings.IndexFunc(raw, func(r rune) bool { return r < 32 || r == 127 }) >= 0 {
|
|
return "", fmt.Errorf("invalid managed SOCKS proxy")
|
|
}
|
|
if !validLoopbackSOCKS(strings.Replace(raw, "socks5h://", "socks5://", 1)) {
|
|
return "", fmt.Errorf("invalid managed SOCKS proxy")
|
|
}
|
|
u.Scheme = "socks5h"
|
|
return u.String(), nil
|
|
}
|
|
|
|
func parseManagedHealthConfig(lookup func(string) (string, bool)) (healthConfig, error) {
|
|
// Reuse all legacy setting validation without changing its public contract.
|
|
cfg, err := parseHealthConfig(func(key string) (string, bool) {
|
|
raw, ok := lookup(key)
|
|
if key == "ZAPRET_AUTOTUNE_SOCKS_PROXY" && ok {
|
|
if proxy, e := managedProbeProxy(raw); e == nil {
|
|
return strings.Replace(proxy, "socks5h://", "socks5://", 1), true
|
|
}
|
|
}
|
|
return raw, ok
|
|
})
|
|
if err != nil {
|
|
return cfg, err
|
|
}
|
|
cfg.SOCKSProxy, err = managedProbeProxy(cfg.SOCKSProxy)
|
|
return cfg, err
|
|
}
|