feat(health): add managed SOCKS probe configuration
Co-authored-by: GPT-6 <gpt-6@openai.com>
This commit is contained in:
1 parent
69077fb708
commit
5c540ce03b
7 files changed
+392
-24
No files matched your search
@@ -0,0 +1,69 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"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 !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")
|
||||
}
|
||||
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 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()
|
||||
}
|
||||
@@ -120,26 +120,12 @@ func discoverYoutube(args []string, stdout, stderr io.Writer, runner playerRunne
|
||||
fmt.Fprintln(stderr, "error: discover youtube", err)
|
||||
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")
|
||||
body, err := requestPlayer(videoID, proxy, runner)
|
||||
if err != nil {
|
||||
fmt.Fprintln(stderr, "error:", err)
|
||||
return 1
|
||||
}
|
||||
host, err := playerHostname(body.Bytes())
|
||||
host, err := playerHostname(body)
|
||||
if err != nil {
|
||||
fmt.Fprintln(stderr, "error:", err)
|
||||
return 1
|
||||
@@ -154,3 +140,25 @@ func discoverYoutube(args []string, stdout, stderr io.Writer, runner playerRunne
|
||||
}
|
||||
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 {
|
||||
return nil, errors.New("player request failed or exceeded output limit")
|
||||
}
|
||||
return body.Bytes(), nil
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
// API health is deliberately independent of streaming URLs and playback.
|
||||
func playerHealth(body []byte) error {
|
||||
var response struct {
|
||||
PlayabilityStatus struct {
|
||||
Status string `json:"status"`
|
||||
} `json:"playabilityStatus"`
|
||||
}
|
||||
if len(body) > playerResponseLimit || json.Unmarshal(body, &response) != nil {
|
||||
return errors.New("invalid player response")
|
||||
}
|
||||
if response.PlayabilityStatus.Status != "OK" {
|
||||
return errors.New("player API is unhealthy: missing or non-OK playability status")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func healthYoutube(stdout, stderr io.Writer, runner playerRunner) int {
|
||||
cfg, err := parseHealthConfig(os.LookupEnv)
|
||||
if err != nil {
|
||||
fmt.Fprintln(stderr, "error:", err)
|
||||
return 2
|
||||
}
|
||||
body, err := requestPlayer(cfg.VideoID, cfg.SOCKSProxy, runner)
|
||||
if err == nil {
|
||||
err = playerHealth(body)
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Fprintln(stderr, "error:", err)
|
||||
return 1
|
||||
}
|
||||
if _, err := fmt.Fprintln(stdout, "YouTube Player API: OK (not a playback check)"); err != nil {
|
||||
fmt.Fprintln(stderr, "error: cannot write health result")
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func configLookup(values map[string]string) func(string) (string, bool) {
|
||||
return func(key string) (string, bool) {
|
||||
value, ok := values[key]
|
||||
return value, ok
|
||||
}
|
||||
}
|
||||
|
||||
func TestHealthConfigDefaultsAndOverrides(t *testing.T) {
|
||||
cfg, err := parseHealthConfig(configLookup(nil))
|
||||
want := healthConfig{"socks5://127.0.0.1:11080", "dQw4w9WgXcQ", 5 * time.Minute, 3, 30 * time.Minute}
|
||||
if err != nil || cfg != want {
|
||||
t.Fatalf("defaults = %+v, %v; want %+v", cfg, err, want)
|
||||
}
|
||||
values := map[string]string{
|
||||
"ZAPRET_AUTOTUNE_SOCKS_PROXY": "socks5://[::1]:12080",
|
||||
"ZAPRET_AUTOTUNE_VIDEO_ID": "abcdefghijk",
|
||||
"ZAPRET_AUTOTUNE_HEALTH_INTERVAL": "90s",
|
||||
"ZAPRET_AUTOTUNE_FAILURE_THRESHOLD": "5",
|
||||
"ZAPRET_AUTOTUNE_RECOVERY_COOLDOWN": "1h",
|
||||
}
|
||||
cfg, err = parseHealthConfig(configLookup(values))
|
||||
want = healthConfig{"socks5://[::1]:12080", "abcdefghijk", 90 * time.Second, 5, time.Hour}
|
||||
if err != nil || cfg != want {
|
||||
t.Fatalf("overrides = %+v, %v; want %+v", cfg, err, want)
|
||||
}
|
||||
for _, proxy := range []string{"socks5://localhost:1", "socks5://LOCALHOST:65535", "socks5://127.0.0.2:11080", "socks5://[::1]:11080"} {
|
||||
if _, err := parseHealthConfig(configLookup(map[string]string{"ZAPRET_AUTOTUNE_SOCKS_PROXY": proxy})); err != nil {
|
||||
t.Errorf("valid loopback rejected: %s: %v", proxy, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHealthConfigRejectsInvalid(t *testing.T) {
|
||||
invalid := map[string][]string{
|
||||
"ZAPRET_AUTOTUNE_SOCKS_PROXY": {
|
||||
"", "http://127.0.0.1:11080", "socks5h://127.0.0.1:11080",
|
||||
"socks5://192.168.1.1:11080", "socks5://0.0.0.0:11080", "socks5://[::]:11080",
|
||||
"socks5://proxy.example:11080", "socks5://localhost.evil:11080",
|
||||
"socks5://user:SECRET@localhost:11080", "socks5://localhost", "socks5://localhost:0",
|
||||
"socks5://localhost:65536", "socks5://localhost:bad", "socks5://localhost:11080/path",
|
||||
"socks5://localhost:11080?SECRET", "socks5://localhost:11080#SECRET", " socks5://localhost:11080",
|
||||
},
|
||||
"ZAPRET_AUTOTUNE_VIDEO_ID": {"", "short", "dQw4w9WgXc!"},
|
||||
"ZAPRET_AUTOTUNE_HEALTH_INTERVAL": {"", "0", "-5m", "5", "SECRET", "999999999999999999999h"},
|
||||
"ZAPRET_AUTOTUNE_RECOVERY_COOLDOWN": {"", "0s", "-30m", "SECRET"},
|
||||
"ZAPRET_AUTOTUNE_FAILURE_THRESHOLD": {"", "0", "-3", "1.5", "SECRET", "999999999999999999999"},
|
||||
}
|
||||
for key, values := range invalid {
|
||||
for _, value := range values {
|
||||
if _, err := parseHealthConfig(configLookup(map[string]string{key: value})); err == nil || !strings.Contains(err.Error(), key) || strings.Contains(err.Error(), "SECRET") {
|
||||
t.Errorf("%s=%q: expected sanitized config error, got %v", key, value, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlayerHealth(t *testing.T) {
|
||||
for _, body := range []string{
|
||||
`{"playabilityStatus":{"status":"OK"}}`,
|
||||
`{"playabilityStatus":{"status":"OK"},"streamingData":{"serverAbrStreamingUrl":"invalid"}}`,
|
||||
} {
|
||||
if err := playerHealth([]byte(body)); err != nil {
|
||||
t.Errorf("OK must not require a streaming URL: %v", err)
|
||||
}
|
||||
}
|
||||
for _, body := range []string{
|
||||
`{"playabilityStatus":{"status":"LOGIN_REQUIRED","reason":"SECRET"}}`,
|
||||
`{"playabilityStatus":{"status":"UNPLAYABLE"}}`, `{"playabilityStatus":{"status":"ok"}}`,
|
||||
`{}`, `null`, `{"playabilityStatus":null}`, `{"playabilityStatus":{}}`,
|
||||
`{"playabilityStatus":{"status":null}}`, `{"playabilityStatus":{"status":42}}`,
|
||||
`{"playabilityStatus":"OK"}`, `not JSON SECRET`, `{"playabilityStatus":{"status":"OK"}} trailing`,
|
||||
strings.Repeat(" ", playerResponseLimit+1),
|
||||
} {
|
||||
if err := playerHealth([]byte(body)); err == nil || strings.Contains(err.Error(), "SECRET") {
|
||||
t.Errorf("invalid/non-OK response accepted or leaked: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func setHealthEnvironment(t *testing.T) {
|
||||
t.Helper()
|
||||
for key, value := range map[string]string{
|
||||
"ZAPRET_AUTOTUNE_SOCKS_PROXY": "socks5://127.0.0.1:11080",
|
||||
"ZAPRET_AUTOTUNE_VIDEO_ID": "dQw4w9WgXcQ",
|
||||
"ZAPRET_AUTOTUNE_HEALTH_INTERVAL": "5m",
|
||||
"ZAPRET_AUTOTUNE_FAILURE_THRESHOLD": "3",
|
||||
"ZAPRET_AUTOTUNE_RECOVERY_COOLDOWN": "30m",
|
||||
} {
|
||||
t.Setenv(key, value)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunHealthYoutube(t *testing.T) {
|
||||
setHealthEnvironment(t)
|
||||
t.Setenv("ZAPRET_AUTOTUNE_SOCKS_PROXY", "socks5://[::1]:12080")
|
||||
t.Setenv("ZAPRET_AUTOTUNE_VIDEO_ID", "abcdefghijk")
|
||||
for _, key := range []string{"ALL_PROXY", "HTTPS_PROXY", "https_proxy", "all_proxy"} {
|
||||
t.Setenv(key, "http://untrusted.example:8080")
|
||||
}
|
||||
t.Setenv("NO_PROXY", "*")
|
||||
var stdout, stderr bytes.Buffer
|
||||
calls := 0
|
||||
runner := func(ctx context.Context, args []string, out, diagnostics io.Writer) error {
|
||||
calls++
|
||||
deadline, ok := ctx.Deadline()
|
||||
if !ok || time.Until(deadline) <= 0 || time.Until(deadline) > 35*time.Second {
|
||||
t.Error("request must have bounded deadline")
|
||||
}
|
||||
prefix := []string{"--disable", "--silent", "--show-error", "--fail", "--max-time", "30", "--noproxy", "", "--proxy", "socks5://[::1]:12080", "--user-agent", playerUserAgent, "--json"}
|
||||
if len(args) != len(prefix)+2 || !reflect.DeepEqual(args[:len(prefix)], prefix) || args[len(args)-1] != playerURL {
|
||||
t.Fatalf("unexpected proxy/API arguments: %q", args)
|
||||
}
|
||||
var payload struct {
|
||||
VideoID string `json:"videoId"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(args[len(prefix)]), &payload); err != nil || payload.VideoID != "abcdefghijk" {
|
||||
t.Fatalf("unexpected video payload: %+v %v", payload, err)
|
||||
}
|
||||
io.WriteString(diagnostics, "SECRET")
|
||||
_, err := io.WriteString(out, `{"playabilityStatus":{"status":"OK"}}`)
|
||||
return err
|
||||
}
|
||||
if code := runWithPlayer([]string{"health", "youtube"}, &stdout, &stderr, runner); code != 0 || calls != 1 || stderr.Len() != 0 || !strings.Contains(stdout.String(), "Player API: OK (not a playback check)") {
|
||||
t.Fatalf("code=%d calls=%d stdout=%q stderr=%q", code, calls, &stdout, &stderr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHealthYoutubeFailures(t *testing.T) {
|
||||
setHealthEnvironment(t)
|
||||
for _, mode := range []string{"blocked", "missing", "invalid", "runner", "stdout limit", "stderr limit", "write result"} {
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
var stdout, stderr bytes.Buffer
|
||||
calls := 0
|
||||
runner := func(_ context.Context, _ []string, out, diagnostics io.Writer) error {
|
||||
calls++
|
||||
io.WriteString(diagnostics, "SECRET")
|
||||
switch mode {
|
||||
case "blocked":
|
||||
io.WriteString(out, `{"playabilityStatus":{"status":"UNPLAYABLE","reason":"SECRET"}}`)
|
||||
case "missing":
|
||||
io.WriteString(out, `{}`)
|
||||
case "invalid":
|
||||
io.WriteString(out, `SECRET`)
|
||||
case "runner":
|
||||
return errors.New("SECRET")
|
||||
case "stdout limit":
|
||||
out.Write(bytes.Repeat([]byte("S"), playerResponseLimit+1))
|
||||
case "stderr limit":
|
||||
diagnostics.Write(bytes.Repeat([]byte("S"), (64<<10)+1))
|
||||
default:
|
||||
io.WriteString(out, `{"playabilityStatus":{"status":"OK"}}`)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
var dest io.Writer = &stdout
|
||||
if mode == "write result" {
|
||||
dest = failingWriter{}
|
||||
}
|
||||
if code := runWithPlayer([]string{"health", "youtube"}, dest, &stderr, runner); code != 1 || calls != 1 || stdout.Len() != 0 || stderr.Len() == 0 || strings.Contains(stderr.String(), "SECRET") {
|
||||
t.Fatalf("code=%d calls=%d stdout=%q stderr=%q", code, calls, &stdout, &stderr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHealthDispatchAndConfigFailWithoutRequest(t *testing.T) {
|
||||
setHealthEnvironment(t)
|
||||
runner := func(context.Context, []string, io.Writer, io.Writer) error {
|
||||
t.Fatal("invalid command/config must not request or fall back")
|
||||
return nil
|
||||
}
|
||||
for _, args := range [][]string{{"health"}, {"health", "other"}, {"health", "youtube", "extra"}, {"health", "youtube", "--proxy", "socks5://localhost:1080"}} {
|
||||
var stdout, stderr bytes.Buffer
|
||||
if code := runWithPlayer(args, &stdout, &stderr, runner); code != 2 || stdout.Len() != 0 || !strings.Contains(stderr.String(), usage) {
|
||||
t.Fatalf("args=%q code=%d stdout=%q stderr=%q", args, code, &stdout, &stderr)
|
||||
}
|
||||
}
|
||||
t.Setenv("ZAPRET_AUTOTUNE_SOCKS_PROXY", "socks5://remote.example:11080")
|
||||
var stdout, stderr bytes.Buffer
|
||||
if code := runWithPlayer([]string{"health", "youtube"}, &stdout, &stderr, runner); code != 2 || stdout.Len() != 0 || !strings.Contains(stderr.String(), "ZAPRET_AUTOTUNE_SOCKS_PROXY") {
|
||||
t.Fatalf("invalid config: code=%d stdout=%q stderr=%q", code, &stdout, &stderr)
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@ Commands:
|
||||
strategies youtube List recorded proposals by group (not active)
|
||||
discover youtube --video-id ID --proxy socks5://HOST:PORT
|
||||
Discover cache hostname only (no media-transfer check)
|
||||
health youtube One-shot Player API health through configured loopback SOCKS
|
||||
check youtube Refuse checks until probe URLs/video IDs are configured and agreed
|
||||
tune youtube Test youtube.com frontend only; print options without applying
|
||||
tune googlevideo --video-id ID --proxy socks5://HOST:PORT
|
||||
@@ -28,11 +29,18 @@ func main() {
|
||||
}
|
||||
|
||||
func run(args []string, stdout, stderr io.Writer) int {
|
||||
return runWithPlayer(args, stdout, stderr, runPlayerCurl)
|
||||
}
|
||||
|
||||
func runWithPlayer(args []string, stdout, stderr io.Writer, player playerRunner) int {
|
||||
if len(args) == 2 && args[0] == "health" && args[1] == "youtube" {
|
||||
return healthYoutube(stdout, stderr, player)
|
||||
}
|
||||
if len(args) >= 2 && args[0] == "recover" && args[1] == "googlevideo" {
|
||||
return recoverGooglevideo(args[2:], stdout, stderr, runPlayerCurl, runBlockcheck, runCombinedBlockcheckForHost)
|
||||
return recoverGooglevideo(args[2:], stdout, stderr, player, runBlockcheck, runCombinedBlockcheckForHost)
|
||||
}
|
||||
if len(args) >= 2 && args[0] == "tune" && args[1] == "googlevideo" {
|
||||
return tuneGooglevideo(args[2:], stdout, stderr, runPlayerCurl, runBlockcheck, runCombinedBlockcheckForHost)
|
||||
return tuneGooglevideo(args[2:], stdout, stderr, player, runBlockcheck, runCombinedBlockcheckForHost)
|
||||
}
|
||||
if len(args) > 0 && args[0] == "history" {
|
||||
return history(args[1:], stdout, stderr)
|
||||
@@ -74,7 +82,7 @@ func run(args []string, stdout, stderr io.Writer) int {
|
||||
}
|
||||
}
|
||||
if len(args) >= 2 && args[0] == "discover" && args[1] == "youtube" {
|
||||
return discoverYoutube(args[2:], stdout, stderr, runPlayerCurl)
|
||||
return discoverYoutube(args[2:], stdout, stderr, player)
|
||||
}
|
||||
if len(args) == 2 && args[0] == "check" && args[1] == "youtube" {
|
||||
fmt.Fprintln(stderr, "error: check youtube is unavailable: YouTube probe URLs/video IDs have not been configured or agreed. No network requests were made; production health was not checked.")
|
||||
|
||||
Reference in new issue
Block a user