Bot checks escalate to headless Chrome with stealth, which claims the same identity as tls-client and hands its cookies back to the jar. The visit counts against the rate limits. If Chrome can't clear a check, the URL is marked needs-human. Pressing Enter on it in the TUI, or running 'dibs solve', opens a visible window; dibs carries on once the page loads. Processes now merge the cookie file on save, with the newest cookie winning.
111 lines
3.1 KiB
Go
111 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"log/slog"
|
|
"os"
|
|
"os/signal"
|
|
|
|
"git.security-command.org/Overlord/dibs/internal/fixtures"
|
|
"git.security-command.org/Overlord/dibs/internal/plugin"
|
|
)
|
|
|
|
// runFixtures implements `dibs fixtures [--dir DIR] [--only NAME]`: it fetches
|
|
// the pages listed in DIR/<shop>/cases.toml for the plugin tests.
|
|
func runFixtures(args []string, stdout, stderr io.Writer) int {
|
|
flags, configPath := newFlagSet("dibs fixtures", stderr)
|
|
dir := flags.String("dir", "testdata", "directory with <shop>/cases.toml files")
|
|
only := flags.String("only", "", "fetch only the case with this name")
|
|
|
|
pos, ok := parseArgs(flags, args)
|
|
if !ok { return 2 }
|
|
if len(pos) > 0 {
|
|
_, _ = fmt.Fprintf(stderr, "dibs fixtures: unexpected argument %q\n\n%s", pos[0], usage)
|
|
return 2
|
|
}
|
|
|
|
suites, err := fixtures.Load(*dir)
|
|
if err != nil { return fail(stderr, "fixtures", err) }
|
|
if len(suites) == 0 { return fail(stderr, "fixtures", fmt.Errorf("no */cases.toml in %s", *dir)) }
|
|
|
|
cfg, ok := loadOptionalConfig(*configPath, stderr)
|
|
if !ok { return 1 }
|
|
|
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
|
|
defer stop()
|
|
|
|
logger := slog.New(slog.NewTextHandler(stderr, nil))
|
|
|
|
host, err := newHost(cfg, logger)
|
|
if err != nil { return fail(stderr, "fixtures", err) }
|
|
defer host.Close()
|
|
|
|
e, err := openEnv(logger)
|
|
if err != nil { return fail(stderr, "fixtures", err) }
|
|
|
|
failed := 0
|
|
|
|
for _, suite := range suites {
|
|
p, ok := host.Plugin(suite.Shop)
|
|
if !ok {
|
|
_, _ = fmt.Fprintf(stderr, "dibs fixtures: %s: no plugin named %q, skipping\n", suite.Dir, suite.Shop)
|
|
continue
|
|
}
|
|
|
|
for _, c := range suite.Cases {
|
|
if *only != "" && c.Name != *only { continue }
|
|
|
|
if err := fetchFixture(ctx, e, p, suite, c, logger, stdout); err != nil {
|
|
if ctx.Err() != nil { return fail(stderr, "fixtures", ctx.Err()) }
|
|
|
|
_, _ = fmt.Fprintf(stderr, "dibs fixtures: %s: %v\n", c.Name, err)
|
|
failed++
|
|
}
|
|
}
|
|
}
|
|
|
|
if failed > 0 { return 1 }
|
|
|
|
return 0
|
|
}
|
|
|
|
func fetchFixture(ctx context.Context, e *env, p *plugin.Plugin, suite fixtures.Suite, c fixtures.Case, logger *slog.Logger, stdout io.Writer) error {
|
|
fetchURL, err := p.Normalize(ctx, c.URL)
|
|
if err != nil { return err }
|
|
|
|
// The saved page is what the shop sent, not what Chrome made of it.
|
|
resp, err := e.fetch(ctx, fetchURL, e.client.Get, logger)
|
|
if err != nil { return err }
|
|
|
|
report, err := plugin.Evaluate(ctx, p, c.URL, resp)
|
|
if err != nil { return err }
|
|
|
|
// Saved reports must not depend on how fast the shop answered.
|
|
report.ElapsedMS = 0
|
|
|
|
if err := suite.Save(c, resp.Body, report); err != nil { return err }
|
|
|
|
_, _ = fmt.Fprintf(stdout, "%-20s %s\n", c.Name, summary(report))
|
|
_, _ = fmt.Fprintf(stdout, "%-20s %s\n\n", "", c.Note)
|
|
|
|
return nil
|
|
}
|
|
|
|
func summary(r *plugin.Report) string {
|
|
s := r.Verdict
|
|
if r.Challenge != "" { s += " (" + r.Challenge + ")" }
|
|
if r.Result == nil { return s }
|
|
|
|
res := r.Result
|
|
|
|
avail := "unavailable"
|
|
if res.Available { avail = "AVAILABLE" }
|
|
|
|
price := "no price"
|
|
if res.Price != nil { price = fmt.Sprintf("%.2f %s", *res.Price, res.Currency) }
|
|
|
|
return fmt.Sprintf("%s, %s, %s, seller %s", s, avail, price, res.Seller)
|
|
}
|