117 lines
3.6 KiB
Go
117 lines
3.6 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()
|
|
}
|