fix: resolve managed probes through owned SS DNS
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>
This commit is contained in:
1 parent
e9a28c8cd3
commit
1afee7d3bd
20 files changed
+212
-30
No files matched your search
@@ -114,3 +114,35 @@ func validLoopbackSOCKS(raw string) bool {
|
||||
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
|
||||
}
|
||||
@@ -37,7 +37,7 @@ func run(args []string, stdout, stderr io.Writer) int {
|
||||
|
||||
func runWithPlayer(args []string, stdout, stderr io.Writer, player playerRunner) int {
|
||||
if len(args) == 1 && args[0] == "target-config-check" {
|
||||
if _, err := parseHealthConfig(os.LookupEnv); err != nil {
|
||||
if _, err := parseManagedHealthConfig(os.LookupEnv); err != nil {
|
||||
fmt.Fprintln(stderr, "configerror: invalid target health/service configuration")
|
||||
return 64
|
||||
}
|
||||
|
||||
@@ -41,8 +41,12 @@ func newManagedAssetVerifier(target managedDeps) func(context.Context, storedCan
|
||||
|
||||
func newManagedAssetRuntime(target managedDeps) managedAssetRuntime {
|
||||
proxy, _ := target.lookup("ZAPRET_AUTOTUNE_SOCKS_PROXY")
|
||||
proxy, proxyErr := managedProbeProxy(proxy)
|
||||
r := managedAssetRuntime{proxy: proxy, curl: runResourceCurl}
|
||||
r.guard = func() error {
|
||||
if proxyErr != nil {
|
||||
return proxyErr
|
||||
}
|
||||
if _, err := managedRuntimeGuard(target); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -71,10 +71,21 @@ type managedServiceInventory struct {
|
||||
func managedServiceResources(cfg healthConfig, player func(context.Context) ([]byte, error), page playerRunner) func(context.Context) (managedServiceInventory, error) {
|
||||
return func(ctx context.Context) (managedServiceInventory, error) {
|
||||
i := managedServiceInventory{images: make(map[string]string), proxy: cfg.SOCKSProxy, runner: runResourceCurl}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return i, managedRecoveryContextError(ctx, err)
|
||||
}
|
||||
proxy, err := managedProbeProxy(cfg.SOCKSProxy)
|
||||
if err != nil {
|
||||
return i, &managedRecoveryFatal{Err: err}
|
||||
}
|
||||
i.proxy = proxy
|
||||
body, err := player(ctx)
|
||||
if err != nil {
|
||||
return i, &managedResourceFailure{"front", managedFrontendHost, err}
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return i, managedRecoveryContextError(ctx, err)
|
||||
}
|
||||
var p struct {
|
||||
VideoDetails struct {
|
||||
Thumbnail struct {
|
||||
@@ -97,6 +108,9 @@ func managedServiceResources(cfg healthConfig, player func(context.Context) ([]b
|
||||
i.resourceErr = &managedResourceFailure{"media", "unknown", err}
|
||||
}
|
||||
for attempt := 0; attempt < 2; attempt++ {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return i, managedRecoveryContextError(ctx, err)
|
||||
}
|
||||
origin := "https://www.youtube.com/"
|
||||
if attempt == 1 {
|
||||
if !videoIDPattern.MatchString(cfg.VideoID) {
|
||||
@@ -108,7 +122,7 @@ func managedServiceResources(cfg healthConfig, player func(context.Context) ([]b
|
||||
var output bytes.Buffer
|
||||
out := &playerBoundedWriter{dst: &output, limit: 5 << 20}
|
||||
// Fixed trusted WWW origin; no redirects, credentials or diagnostics.
|
||||
err = page(bounded, []string{"--disable", "--silent", "--show-error", "--fail", "--noproxy", "", "--proxy", cfg.SOCKSProxy, "--proto", "=https", "--max-time", "30", "--max-filesize", "5242880", origin}, out, io.Discard)
|
||||
err = page(bounded, []string{"--disable", "--silent", "--show-error", "--fail", "--noproxy", "", "--proxy", proxy, "--proto", "=https", "--max-time", "30", "--max-filesize", "5242880", origin}, out, io.Discard)
|
||||
requestErr := bounded.Err()
|
||||
cancel()
|
||||
if ctx.Err() != nil {
|
||||
|
||||
@@ -72,9 +72,10 @@ func TestManagedAvatarInventoryFallback(t *testing.T) {
|
||||
t.Fatal("unbounded metadata or diagnostics")
|
||||
}
|
||||
joined := strings.Join(args, " ")
|
||||
if strings.Contains(joined, "private=") || strings.Contains(joined, "--insecure") || strings.Contains(joined, "--location") || !strings.Contains(joined, "--proto =https") || !strings.Contains(joined, "--max-filesize 5242880") || !strings.Contains(joined, "--proxy "+cfg.SOCKSProxy) {
|
||||
if strings.Contains(joined, "private=") || strings.Contains(joined, "--insecure") || strings.Contains(joined, "--location") || !strings.Contains(joined, "--proto =https") || !strings.Contains(joined, "--max-filesize 5242880") || !strings.Contains(joined, "--proxy socks5h://127.0.0.1:11080") {
|
||||
t.Fatal("unsafe metadata request")
|
||||
}
|
||||
assertManagedDNSArgs(t, args, "socks5h://127.0.0.1:11080")
|
||||
body, origin := tc.home, "https://www.youtube.com/"
|
||||
if calls == 2 {
|
||||
body, origin = tc.watch, "https://www.youtube.com/watch?v="+cfg.VideoID
|
||||
@@ -133,17 +134,40 @@ func TestManagedAvatarMetadataHealthClassification(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestManagedAvatarInventoryCancellationNoFallback(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
calls := 0
|
||||
resources := managedServiceResources(healthConfig{VideoID: "dQw4w9WgXcQ"}, func(context.Context) ([]byte, error) {
|
||||
return []byte(`{"playabilityStatus":{"status":"OK"}}`), nil
|
||||
}, func(ctx context.Context, _ []string, _, _ io.Writer) error {
|
||||
calls++
|
||||
return ctx.Err()
|
||||
})
|
||||
i, err := resources(ctx)
|
||||
if calls != 1 || !errors.Is(err, context.Canceled) || i.images["yt3.ggpht.com"] != "" {
|
||||
t.Fatal("cancellation lost or fallback attempted", calls, err)
|
||||
for _, timing := range []string{"before-player", "after-player", "during-home"} {
|
||||
t.Run(timing, func(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
if timing == "before-player" {
|
||||
cancel()
|
||||
}
|
||||
players, pages := 0, 0
|
||||
resources := managedServiceResources(healthConfig{VideoID: "dQw4w9WgXcQ", SOCKSProxy: "socks5h://127.0.0.1:11080"}, func(context.Context) ([]byte, error) {
|
||||
players++
|
||||
if timing == "after-player" {
|
||||
cancel()
|
||||
}
|
||||
return []byte(`{"playabilityStatus":{"status":"OK"}}`), nil
|
||||
}, func(ctx context.Context, args []string, out, _ io.Writer) error {
|
||||
pages++
|
||||
assertManagedDNSArgs(t, args, "socks5h://127.0.0.1:11080")
|
||||
if args[len(args)-1] != "https://www.youtube.com/" {
|
||||
t.Fatal("watch fallback after cancellation", args)
|
||||
}
|
||||
cancel()
|
||||
_, err := io.WriteString(out, `var ytInitialData = {};`)
|
||||
return err
|
||||
})
|
||||
i, err := resources(ctx)
|
||||
wantPlayers, wantPages := 1, 0
|
||||
if timing == "before-player" {
|
||||
wantPlayers = 0
|
||||
} else if timing == "during-home" {
|
||||
wantPages = 1
|
||||
}
|
||||
if players != wantPlayers || pages != wantPages || !errors.Is(err, context.Canceled) || i.images["yt3.ggpht.com"] != "" {
|
||||
t.Fatal("cancellation lost or request after cancellation", players, pages, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -395,7 +395,8 @@ func managedRuntimeGuard(d managedDeps) (int, error) {
|
||||
portText, _ := d.lookup("SOCKS_PORT")
|
||||
port, portErr := strconv.Atoi(portText)
|
||||
proxy, _ := d.lookup("ZAPRET_AUTOTUNE_SOCKS_PROXY")
|
||||
if portErr != nil || port < 1 || port > 65535 || strconv.Itoa(port) != portText || proxy != "socks5://127.0.0.1:"+portText {
|
||||
proxy, proxyErr := managedProbeProxy(proxy)
|
||||
if portErr != nil || port < 1 || port > 65535 || strconv.Itoa(port) != portText || proxyErr != nil || proxy != "socks5h://127.0.0.1:"+portText {
|
||||
return 2, fmt.Errorf("managed SOCKS proxy must match the target SOCKS_PORT listener")
|
||||
}
|
||||
if d.verifyRuntime == nil || d.verifySOCKS == nil {
|
||||
@@ -535,7 +536,7 @@ func managedActivate(m *managedTransaction, d managedDeps, ids []int64, expected
|
||||
if err != nil || !info.Mode().IsRegular() || info.Mode().Perm()&0111 == 0 {
|
||||
return fmt.Errorf("target zapret daemon init executable unavailable")
|
||||
}
|
||||
cfg, err := parseHealthConfig(d.lookup)
|
||||
cfg, err := parseManagedHealthConfig(d.lookup)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ func managedCLIFixture(t *testing.T) (managedDeps, map[string]string, *int) {
|
||||
return nil
|
||||
},
|
||||
player: func(_ context.Context, args []string, stdout, _ io.Writer) error {
|
||||
if !strings.Contains(strings.Join(args, "|"), "--noproxy||--proxy|socks5://127.0.0.1:11080|") {
|
||||
if !strings.Contains(strings.Join(args, "|"), "--noproxy||--proxy|socks5h://127.0.0.1:11080|") {
|
||||
t.Fatalf("unsafe health argv: %q", args)
|
||||
}
|
||||
_, err := io.WriteString(stdout, `{"playabilityStatus":{"status":"OK"}}`)
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func assertManagedDNSArgs(t *testing.T, args []string, proxy string) {
|
||||
t.Helper()
|
||||
joined := strings.Join(args, "|")
|
||||
if !strings.Contains(joined, "--noproxy||--proxy|"+proxy+"|") {
|
||||
t.Fatalf("missing managed remote DNS proxy: %q", args)
|
||||
}
|
||||
for _, arg := range args {
|
||||
if arg == "-k" || arg == "--insecure" || strings.HasPrefix(arg, "--resolve") || strings.HasPrefix(arg, "--connect-to") {
|
||||
t.Fatalf("DNS pin or TLS bypass: %q", args)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestManagedProbeProxy(t *testing.T) {
|
||||
for _, raw := range []string{"socks5://127.0.0.1:1080", "socks5h://127.0.0.1:1080", "socks5://localhost:1080", "socks5h://[::1]:1080"} {
|
||||
got, err := managedProbeProxy(raw)
|
||||
if err != nil || got != strings.Replace(raw, "socks5://", "socks5h://", 1) {
|
||||
t.Fatal(raw, got, err)
|
||||
}
|
||||
}
|
||||
for _, raw := range []string{"", "http://127.0.0.1:1080", "socks5h://proxy.example:1080", "socks5h://user:secret@127.0.0.1:1080", "socks5h://127.0.0.1:0", "socks5h://127.0.0.1:65536", "socks5h://127.0.0.1:1080/", "socks5h://127.0.0.1:1080?", "socks5h://127.0.0.1:1080#", "socks5h://127.0.0.1:1080\n", "socks5h://127.0.0.1:1080\x7f"} {
|
||||
if got, err := managedProbeProxy(raw); err == nil || got != "" || strings.Contains(err.Error(), "secret") {
|
||||
t.Fatal("unsafe proxy accepted or leaked", got, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestManagedDNSProductionPlayerAndGuard(t *testing.T) {
|
||||
for _, scheme := range []string{"socks5", "socks5h"} {
|
||||
t.Run(scheme, func(t *testing.T) {
|
||||
target, env, _ := managedCLIFixture(t)
|
||||
env["ZAPRET_AUTOTUNE_SOCKS_PROXY"] = scheme + "://127.0.0.1:11080"
|
||||
owned := 0
|
||||
target.verifySOCKS = func(port int) error {
|
||||
if port != 11080 {
|
||||
t.Fatal(port)
|
||||
}
|
||||
owned++
|
||||
return nil
|
||||
}
|
||||
requests := 0
|
||||
target.player = func(_ context.Context, args []string, out, _ io.Writer) error {
|
||||
assertManagedDNSArgs(t, args, "socks5h://127.0.0.1:11080")
|
||||
if args[len(args)-1] != playerURL {
|
||||
t.Fatal("Player hostname changed", args)
|
||||
}
|
||||
requests++
|
||||
_, err := io.WriteString(out, `{"playabilityStatus":{"status":"OK"},"streamingData":{"serverAbrStreamingUrl":"https://fresh.googlevideo.com/video?private=secret"}}`)
|
||||
return err
|
||||
}
|
||||
r, _, _ := recoveryFixture(t, false)
|
||||
d, err := managedRecoveryDependencies(target, r.db, r.scan, r.check, r.run)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if status, err := d.health(context.Background()); err != nil || status != "OK" {
|
||||
t.Fatal(status, err)
|
||||
}
|
||||
if host, err := d.discover(context.Background()); err != nil || host != "fresh.googlevideo.com" {
|
||||
t.Fatal(host, err)
|
||||
}
|
||||
if requests != 2 || owned != 3 {
|
||||
t.Fatal(requests, owned)
|
||||
}
|
||||
for _, proxy := range []string{scheme + "://127.0.0.1:1080", scheme + "://localhost:11080", scheme + "://[::1]:11080", scheme + "://proxy.example:11080"} {
|
||||
env["ZAPRET_AUTOTUNE_SOCKS_PROXY"] = proxy
|
||||
if _, err := managedRuntimeGuard(target); err == nil {
|
||||
t.Fatal("unowned endpoint accepted", proxy)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
lookup := configLookup(map[string]string{"ZAPRET_AUTOTUNE_SOCKS_PROXY": "socks5://127.0.0.1:11080"})
|
||||
legacy, err := parseHealthConfig(lookup)
|
||||
if err != nil || legacy.SOCKSProxy != "socks5://127.0.0.1:11080" || validDiscoveryProxy("socks5h://127.0.0.1:11080") {
|
||||
t.Fatal("legacy proxy contract changed", legacy, err)
|
||||
}
|
||||
}
|
||||
@@ -95,7 +95,7 @@ func managedExperimentalSuffix(m *managedTransaction, d managedDeps) error {
|
||||
if err != nil || fmt.Sprintf("%x", sha256.Sum256(lua)) != managedGooglevideoLuaHash {
|
||||
return fmt.Errorf("native Lua identity mismatch")
|
||||
}
|
||||
cfg, err := parseHealthConfig(d.lookup)
|
||||
cfg, err := parseManagedHealthConfig(d.lookup)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -56,6 +56,7 @@ func TestManagedGooglevideoOwnedPostWarmupProducer(t *testing.T) {
|
||||
r, c, resources, events := googlevideoRuntimeFixture(t)
|
||||
runner, calls := r.curl, 0
|
||||
r.curl = func(ctx context.Context, args []string, stdin io.Reader, out, diagnostic io.Writer) error {
|
||||
assertManagedDNSArgs(t, args, "socks5h://127.0.0.1:1080")
|
||||
calls++
|
||||
if calls <= 2 {
|
||||
return context.DeadlineExceeded
|
||||
|
||||
@@ -16,7 +16,7 @@ import (
|
||||
|
||||
// Provisioning owns executable, database, proxy and target paths; no path flags.
|
||||
func managedMonitorConfig(args []string, lookup func(string) (string, bool)) (healthConfig, error) {
|
||||
cfg, err := parseHealthConfig(lookup)
|
||||
cfg, err := parseManagedHealthConfig(lookup)
|
||||
if err != nil {
|
||||
return cfg, err
|
||||
}
|
||||
|
||||
@@ -352,7 +352,7 @@ func TestManagedMonitorProductionAdapterBinding(t *testing.T) {
|
||||
if ctx.Err() != nil {
|
||||
t.Fatal(ctx.Err())
|
||||
}
|
||||
if !strings.Contains(strings.Join(args, "|"), "--noproxy||--proxy|socks5://127.0.0.1:11080|") {
|
||||
if !strings.Contains(strings.Join(args, "|"), "--noproxy||--proxy|socks5h://127.0.0.1:11080|") {
|
||||
t.Fatal(args)
|
||||
}
|
||||
_, err := io.WriteString(out, `{"playabilityStatus":{"status":"OK"},"streamingData":{"serverAbrStreamingUrl":"https://fresh.googlevideo.com/video?secret=never-print"}}`)
|
||||
@@ -578,7 +578,7 @@ func TestManagedMonitorCLIBindingAndCancellation(t *testing.T) {
|
||||
requests := 0
|
||||
d.player = func(ctx context.Context, args []string, out, _ io.Writer) error {
|
||||
requests++
|
||||
if !strings.Contains(strings.Join(args, "|"), "--noproxy||--proxy|socks5://127.0.0.1:11080|") {
|
||||
if !strings.Contains(strings.Join(args, "|"), "--noproxy||--proxy|socks5h://127.0.0.1:11080|") {
|
||||
t.Fatal(args)
|
||||
}
|
||||
if requests == 1 {
|
||||
|
||||
@@ -52,7 +52,7 @@ func managedRecoveryDependencies(target managedDeps, db *sql.DB, scan cacheScan,
|
||||
if _, err := managedRuntimeGuard(target); err != nil {
|
||||
return d, err
|
||||
}
|
||||
cfg, err := parseHealthConfig(target.lookup)
|
||||
cfg, err := parseManagedHealthConfig(target.lookup)
|
||||
if err != nil {
|
||||
return d, err
|
||||
}
|
||||
|
||||
@@ -223,7 +223,8 @@ func collectResourceTransfer(ctx context.Context, proxy, raw, host, protocol str
|
||||
fail := func(err error) (assetTransfer, http.Header, error) {
|
||||
return assetTransfer{}, nil, resourceFailure(ctx, err)
|
||||
}
|
||||
if !validDiscoveryProxy(proxy) || runner == nil {
|
||||
proxy, proxyErr := managedProbeProxy(proxy)
|
||||
if proxyErr != nil || runner == nil {
|
||||
return fail(errors.New("invalid owned proxy or runner"))
|
||||
}
|
||||
if _, err := privateResourceURL(raw, host); err != nil {
|
||||
|
||||
@@ -63,6 +63,7 @@ func TestResourceChild(t *testing.T) {
|
||||
if os.Getenv("AUTOTUNE_RESOURCE_CHILD") != "1" {
|
||||
return
|
||||
}
|
||||
assertManagedDNSArgs(t, os.Args[3:], "socks5h://127.0.0.1:1080")
|
||||
b, err := io.ReadAll(io.LimitReader(os.Stdin, 32<<10))
|
||||
if err != nil {
|
||||
os.Exit(3)
|
||||
@@ -84,9 +85,10 @@ func TestResourcePrivateChildAndEvidence(t *testing.T) {
|
||||
if strings.Contains(joined, "private-secret") || strings.Contains(joined, "https://") || strings.Contains(joined, "-k") || args[0] != "--disable" {
|
||||
t.Fatalf("unsafe argv: %v", args)
|
||||
}
|
||||
if !strings.Contains(joined, "--noproxy --proxy socks5://127.0.0.1:1080") || !strings.Contains(joined, "--tlsv1.2 --tls-max 1.2") {
|
||||
if !strings.Contains(joined, "--noproxy --proxy socks5h://127.0.0.1:1080") || !strings.Contains(joined, "--tlsv1.2 --tls-max 1.2") {
|
||||
t.Fatal("missing explicit routing/TLS")
|
||||
}
|
||||
assertManagedDNSArgs(t, args, "socks5h://127.0.0.1:1080")
|
||||
if !strings.Contains(joined, "--http1.1 --raw") || !strings.Contains(joined, "--suppress-connect-headers") {
|
||||
t.Fatal("missing native HTTP/1.1 raw contract")
|
||||
}
|
||||
@@ -247,6 +249,7 @@ func TestResourceDiscoveryAndMedia(t *testing.T) {
|
||||
}
|
||||
for _, mode := range []string{"media", "bad-range", "root"} {
|
||||
runner := func(_ context.Context, args []string, _ io.Reader, out, diagnostic io.Writer) error {
|
||||
assertManagedDNSArgs(t, args, "socks5h://127.0.0.1:1080")
|
||||
if !strings.Contains(strings.Join(args, " "), "--range 0-65535") {
|
||||
t.Fatal("missing bounded range")
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user