From 544dda7c85965fc3200de3af17fca9ef9999b2ce Mon Sep 17 00:00:00 2001 From: Zeusina Date: Tue, 13 Oct 2026 15:47:20 +0300 Subject: [PATCH] fix: accept explicit socks5h for one-shot health Preserve configured proxy DNS semantics while reusing strict URL and loopback validation. Cover proxy forwarding, legacy socks5, and unsafe endpoints with offline tests; document the managed DoH boundary. Co-authored-by: GPT-6 --- README.md | 5 +- cmd/zapret-autotune/config.go | 10 ++-- cmd/zapret-autotune/health_test.go | 81 +++++++++++++++++------------- 3 files changed, 55 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index d189976..5531e47 100644 --- a/README.md +++ b/README.md @@ -109,8 +109,9 @@ not used and there is no direct fallback. The request times out after 30 seconds | `ZAPRET_AUTOTUNE_FAILURE_THRESHOLD` | `3` | | `ZAPRET_AUTOTUNE_RECOVERY_COOLDOWN` | `30m` | -Configuration is parsed once per health invocation. The proxy must use `socks5` -with a loopback IP or `localhost`, port 1–65535, and no credentials/path/query. +Configuration is parsed once per health invocation. The proxy must use `socks5` or `socks5h` +with a loopback IP or `localhost`, port 1–65535, and no credentials/path/query/fragment. +Explicit `socks5h` uses proxy-side hostname resolution (encrypted DNS only with managed DoH configured); explicit `socks5` retains local DNS semantics. Video IDs must be 11 characters; durations and the failure threshold must be positive. Explicit empty values are invalid. Interval, consecutive-failure threshold and cooldown configure `managed monitor` (the one-shot command still diff --git a/cmd/zapret-autotune/config.go b/cmd/zapret-autotune/config.go index 21323e1..389349d 100644 --- a/cmd/zapret-autotune/config.go +++ b/cmd/zapret-autotune/config.go @@ -33,7 +33,7 @@ func parseHealthConfig(lookup func(string) (string, bool)) (healthConfig, error) return healthConfig{}, err } if !validLoopbackSOCKS(cfg.SOCKSProxy) { - return healthConfig{}, fmt.Errorf("ZAPRET_AUTOTUNE_SOCKS_PROXY requires socks5://LOOPBACK:PORT without credentials") + return healthConfig{}, fmt.Errorf("ZAPRET_AUTOTUNE_SOCKS_PROXY requires socks5:// or socks5h://LOOPBACK:PORT without credentials") } if !videoIDPattern.MatchString(cfg.VideoID) { return healthConfig{}, fmt.Errorf("ZAPRET_AUTOTUNE_VIDEO_ID requires an 11-character video ID") @@ -112,11 +112,11 @@ func managedServiceEnabled(lookup func(string) (string, bool)) (bool, error) { } func validLoopbackSOCKS(raw string) bool { - if !validDiscoveryProxy(raw) { + 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 false } - u, err := url.Parse(raw) - if err != nil { + if !validDiscoveryProxy(strings.Replace(raw, "socks5h://", "socks5://", 1)) { return false } host := u.Hostname() @@ -124,7 +124,7 @@ func validLoopbackSOCKS(raw string) bool { } // Managed probes send hostnames through the owned SS listener, not local DNS. -// Legacy public discovery/health parsing deliberately keeps socks5 semantics. +// Public health preserves the explicit scheme; public discovery keeps socks5. 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 { diff --git a/cmd/zapret-autotune/health_test.go b/cmd/zapret-autotune/health_test.go index 1fd6f5d..e09c5dc 100644 --- a/cmd/zapret-autotune/health_test.go +++ b/cmd/zapret-autotune/health_test.go @@ -37,8 +37,8 @@ func TestHealthConfigDefaultsAndOverrides(t *testing.T) { if err != nil || !reflect.DeepEqual(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 { + for _, proxy := range []string{"socks5://localhost:1", "socks5://LOCALHOST:65535", "socks5://127.0.0.2:11080", "socks5://[::1]:11080", "socks5h://localhost:1", "socks5h://LOCALHOST:65535", "socks5h://127.0.0.2:11080", "socks5h://[::1]:11080"} { + if cfg, err := parseHealthConfig(configLookup(map[string]string{"ZAPRET_AUTOTUNE_SOCKS_PROXY": proxy})); err != nil || cfg.SOCKSProxy != proxy { t.Errorf("valid loopback rejected: %s: %v", proxy, err) } } @@ -47,12 +47,14 @@ func TestHealthConfigDefaultsAndOverrides(t *testing.T) { 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", + "", "http://127.0.0.1:11080", "https://127.0.0.1:11080", "socks4://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", + "socks5://@localhost:11080", "socks5://SECRET@localhost:11080", "socks5://localhost:11080?", "socks5://localhost:11080#", + "socks5://localhost:11080\x00", "socks5://localhost:11080\x1f", "socks5://localhost:11080\x7f", }, "ZAPRET_AUTOTUNE_VIDEO_ID": {"", "short", "dQw4w9WgXc!"}, "ZAPRET_AUTOTUNE_HEALTH_INTERVAL": {"", "0", "-5m", "5", "SECRET", "999999999999999999999h"}, @@ -60,6 +62,13 @@ func TestHealthConfigRejectsInvalid(t *testing.T) { "ZAPRET_AUTOTUNE_FAILURE_THRESHOLD": {"", "0", "-3", "1.5", "SECRET", "999999999999999999999"}, } for key, values := range invalid { + if key == "ZAPRET_AUTOTUNE_SOCKS_PROXY" { + for _, value := range values { + if strings.HasPrefix(value, "socks5://") { + values = append(values, strings.Replace(value, "socks5://", "socks5h://", 1)) + } + } + } 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) @@ -105,37 +114,41 @@ func setHealthEnvironment(t *testing.T) { } 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) + for _, proxy := range []string{"socks5://[::1]:12080", "socks5h://[::1]:12080", "socks5h://127.0.0.1:11082"} { + t.Run(proxy, func(t *testing.T) { + setHealthEnvironment(t) + t.Setenv("ZAPRET_AUTOTUNE_SOCKS_PROXY", proxy) + 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", proxy, "--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) + } + }) } }