fix(monitor): seed known working strategies

This commit is contained in:
Zeusina committed 2026-10-10 14:12:13 +03:00
1 parent 324c5efa79
commit a997baa553
3 files changed
+93

No files matched your search

+51
View File
@@ -1,6 +1,7 @@
package main
import (
"bytes"
"context"
"errors"
"fmt"
@@ -13,6 +14,20 @@ import (
"time"
)
// Managed seeds are only starting hypotheses from an existing known-good
// profile; blockcheckw must re-rank and re-verify them for the exact hostname.
func managedSeedArgs(host string) string {
host = strings.ToLower(host)
switch {
case host == "www.youtube.com":
return "--payload=tls_client_hello --lua-desync=hostfakesplit:disorder_after:ip_autottl=-3,3-20:repeats=1 --lua-desync=multidisorder:pos=1,midsld"
case host == "googlevideo.com" || strings.HasSuffix(host, ".googlevideo.com"):
return "--payload=tls_client_hello --lua-desync=fakeddisorder:pos=midsld:tcp_flags_set=SYN"
default:
return ""
}
}
// runBlockcheck leaves the output file (including partial results on failure) to
// the caller to read and remove. stdout/stderr receive the unmodified streams.
// The caller controls cancellation through ctx; no overall deadline is imposed here.
@@ -24,10 +39,32 @@ func runBlockcheck(ctx context.Context, binary, domain string, stdout, stderr io
// namespace, so blockcheck may clean up its own scan workers before shortlist
// checks. Other callers preserve foreign nfqws2 processes by default.
func runManagedBlockcheck(ctx context.Context, binary, domain string, stdout, stderr io.Writer) (string, error) {
domain = strings.ToLower(domain)
if args := managedSeedArgs(domain); args != "" {
path, err := runBlockcheckWithConflictCleanupAndSeed(ctx, binary, domain, stdout, stderr, true, args)
if err == nil {
report, readErr := os.ReadFile(path + ".check.json")
group := "frontend"
if domain == "googlevideo.com" || strings.HasSuffix(domain, ".googlevideo.com") {
group = "googlevideo"
}
if readErr == nil {
if _, selectErr := selectBlockcheckArgsForTarget(bytes.NewReader(report), "", group, strings.ToLower(domain)); selectErr == nil {
return path, nil
}
}
_ = os.Remove(path)
_ = os.Remove(path + ".check.json")
}
}
return runBlockcheckWithConflictCleanup(ctx, binary, domain, stdout, stderr, true)
}
func runBlockcheckWithConflictCleanup(ctx context.Context, binary, domain string, stdout, stderr io.Writer, allowConflictCleanup bool) (string, error) {
return runBlockcheckWithConflictCleanupAndSeed(ctx, binary, domain, stdout, stderr, allowConflictCleanup, "")
}
func runBlockcheckWithConflictCleanupAndSeed(ctx context.Context, binary, domain string, stdout, stderr io.Writer, allowConflictCleanup bool, seedArgs string) (string, error) {
if !validDomain(domain) {
return "", fmt.Errorf("invalid DNS hostname: %q", domain)
}
@@ -55,7 +92,21 @@ func runBlockcheckWithConflictCleanup(ctx context.Context, binary, domain string
}
}
domain = strings.ToLower(domain)
var seedPath string
if seedArgs != "" {
rows, err := combinedVanillaRowsForHost(seedArgs, domain)
if err != nil {
return path, err
}
seedPath = filepath.Join(workdir, "managed-seed.txt")
if err := os.WriteFile(seedPath, []byte(rows), 0600); err != nil {
return path, err
}
}
scanArgs := []string{"--workers", "8", "scan", "--domain", domain, "--protocols", "tls12,tls13", "--auto"}
if seedPath != "" {
scanArgs = append(scanArgs, "--from-file", seedPath)
}
if !allowConflictCleanup {
scanArgs = append(scanArgs, "--no-conflict-cleanup")
}
+41
View File
@@ -12,6 +12,7 @@ import (
"os/signal"
"path/filepath"
"reflect"
"slices"
"strconv"
"strings"
"syscall"
@@ -227,6 +228,46 @@ func TestRunManagedBlockcheckAllowsWorkerCleanup(t *testing.T) {
}
}
func TestRunManagedBlockcheckSeedsKnownProfileThroughBlockcheck(t *testing.T) {
t.Setenv("TMPDIR", t.TempDir())
args := managedSeedArgs("www.youtube.com")
t.Setenv("ZAPRET_TEST_REPORT", combinedReportFixture(args))
argvPath := filepath.Join(t.TempDir(), "argv.jsonl")
t.Setenv("ZAPRET_TEST_ARGV", argvPath)
binary := fakeBlockcheck(t, "tune")
path, err := runManagedBlockcheck(context.Background(), binary, "www.youtube.com", io.Discard, io.Discard)
if err != nil {
t.Fatal(err)
}
defer os.Remove(path)
defer os.Remove(path + ".check.json")
data, err := os.ReadFile(path + ".check.json")
if err != nil {
t.Fatal(err)
}
if _, err := selectBlockcheckArgsForTarget(bytes.NewReader(data), args, "frontend", "www.youtube.com"); err != nil {
t.Fatalf("seed was not verified through blockcheck: %v", err)
}
argv, err := os.ReadFile(argvPath)
if err != nil {
t.Fatal(err)
}
decoder := json.NewDecoder(bytes.NewReader(argv))
var scanArgs, checkArgs []string
if err := decoder.Decode(&scanArgs); err != nil {
t.Fatal(err)
}
if err := decoder.Decode(&checkArgs); err != nil {
t.Fatal(err)
}
if !slices.Contains(scanArgs, "--from-file") || slices.Contains(scanArgs, "--no-conflict-cleanup") || slices.Contains(checkArgs, "--no-conflict-cleanup") {
t.Fatalf("managed seeded commands=%q then %q", scanArgs, checkArgs)
}
if seed := managedSeedArgs("rr3.googlevideo.com"); !strings.Contains(seed, "fakeddisorder") {
t.Fatalf("missing exact-host GGC seed: %q", seed)
}
}
func TestRunBlockcheckValidation(t *testing.T) {
for _, domain := range []string{"", "--auto", "a;touch x", "a/b", "a..b", "-a.com", strings.Repeat("a", 64) + ".com"} {
path, err := runBlockcheck(context.Background(), "nonexistent", domain, io.Discard, io.Discard)