Files
zapret-autotune/cmd/zapret-autotune/recover.go
T

113 lines
3.6 KiB
Go

package main
import (
"bytes"
"context"
"database/sql"
"errors"
"fmt"
"io"
"os"
"strings"
)
// Only complete, well-formed non-passing measurements permit scan fallback.
var errCandidateNonpass = errors.New("saved scenario did not pass both TLS protocols")
const managedFrontendHost = "www.youtube.com"
// Rechecks exact saved args using runCombinedBlockcheckForHost's cacheCheck
// contract. The caller supplies a fresh temporary artifact path; artifacts are
// retained, but no stored definition or evidence is updated. A fresh GGC host
// is allowed only as an exact endpoint, never as wildcard qualification.
func revalidateCandidate(ctx context.Context, c storedCandidate, group, host, path string, stderr io.Writer, check cacheCheck) ([]byte, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
if (group != "frontend" && group != "googlevideo") || c.Group != group || !c.Qualified || path == "" || check == nil {
return nil, fmt.Errorf("invalid candidate revalidation request")
}
definition := candidate{Group: c.Group, Domains: append([]string(nil), c.Endpoints...), Args: c.Args}
if err := definition.validate(); err != nil {
return nil, err
}
if !validDomain(host) || host != strings.ToLower(host) {
return nil, fmt.Errorf("invalid revalidation endpoint")
}
if group == "googlevideo" {
if !discoveryHostname(host) || !strings.HasSuffix(host, ".googlevideo.com") {
return nil, fmt.Errorf("invalid exact cache scope")
}
} else {
inScope := false
for _, endpoint := range c.Endpoints {
inScope = inScope || endpoint == host || strings.HasSuffix(host, "."+endpoint)
}
if !inScope {
return nil, fmt.Errorf("frontend endpoint outside candidate scope")
}
}
fmt.Fprintln(stderr, "Rechecking saved candidate:", c.ID, "exact endpoint:", host, "Blockcheck artifacts:", path)
// A checker that writes nothing must not reuse an earlier measurement.
if err := os.Remove(path + ".combined.json"); err != nil && !errors.Is(err, os.ErrNotExist) {
return nil, err
}
var raw bytes.Buffer
err := check(ctx, "blockcheckw", path, c.Args, host, &raw, &raw)
if e := os.WriteFile(path+".stdout", raw.Bytes(), 0600); e != nil {
return nil, e
}
if err != nil {
// Only our report parser may issue the nonpass authorization.
if errors.Is(err, errCandidateNonpass) {
return nil, fmt.Errorf("candidate checker failed: %v", err)
}
return nil, err
}
if err := ctx.Err(); err != nil {
return nil, err
}
report, err := os.ReadFile(path + ".combined.json")
if err != nil {
return nil, err
}
_, err = selectBlockcheckEvidence(bytes.NewReader(report), c.Args, group, host, true)
if contextErr := ctx.Err(); contextErr != nil {
return nil, contextErr
}
return report, err
}
func revalidateGooglevideo(ctx context.Context, db *sql.DB, host string, stderr io.Writer, check cacheCheck) (string, []byte, error) {
ids, err := qualifiedCandidateIDs(db, "googlevideo")
if err != nil {
return "", nil, &managedRecoveryFatal{Err: err}
}
for _, id := range ids {
if err := ctx.Err(); err != nil {
return "", nil, err
}
c, err := readStrategyCandidate(db, id)
if err != nil {
return "", nil, &managedRecoveryFatal{Err: err}
}
if !c.Qualified {
continue
}
artifact, err := os.CreateTemp("", "blockcheckw-recover-*.txt")
if err != nil {
return "", nil, err
}
path := artifact.Name()
if err := artifact.Close(); err != nil {
return "", nil, err
}
report, err := revalidateCandidate(ctx, c, "googlevideo", host, path, stderr, check)
if errors.Is(err, errCandidateNonpass) {
continue
}
return c.Args, report, err
}
return "", nil, ctx.Err()
}