Add scheduler, rate limiting and headless mode.

Hard-coded 60s URL and 10s host floors with jitter and per-host
backoff, shared between dibs processes through a locked state file.
The scheduler shuffles due URLs each round and runs one worker per
host; 'dibs --headless' watches and logs JSON.
This commit is contained in:
Overlord committed 2026-10-14 09:04:15 +02:00
1 parent bb010356a0
commit 2175c0e5aa
30 files changed
+1434 -126

No files matched your search

+6 -7
View File
@@ -12,8 +12,7 @@ import (
)
// chromeMajor must match chromeProfile: the TLS and HTTP/2 fingerprint come
// from the profile, the headers from the version. Bump both together when
// updating tls-client.
// from the profile, the headers from the version, so the two change together.
const chromeMajor = 152
var chromeProfile = profiles.Chrome_152
@@ -26,8 +25,8 @@ type Browser struct {
Platform string // sec-ch-ua-platform, without quotes
}
// DefaultBrowser returns desktop Chrome on the current OS, so that cookies
// a real browser on this machine obtains later stay valid for us.
// DefaultBrowser returns desktop Chrome on the current OS, so that cookies a
// real browser on this machine obtains later stay valid for these requests.
func DefaultBrowser() Browser {
ua, platform := "Windows NT 10.0; Win64; x64", "Windows"
@@ -37,7 +36,7 @@ func DefaultBrowser() Browser {
case "linux":
ua, platform = "X11; Linux x86_64", "Linux"
default:
// Windows, and anything else, which Chrome supports no better.
// Windows. Other systems pass as Windows too.
}
return Browser{
@@ -86,8 +85,8 @@ func (b Browser) navigationHeaders(u *url.URL) http.Header {
// secCHUA builds Chrome's sec-ch-ua brand list. Chrome derives the GREASE
// brand and the brand order from its major version (GenerateBrandVersionList
// in Chromium's user_agent_utils.cc), so a fixed string would give us away
// after an update.
// in Chromium's user_agent_utils.cc), so a fixed string would stand out after
// an update.
func secCHUA(major int) string {
chars := []string{" ", "(", ":", "-", ".", "/", ")", ";", "=", "?", "_"}
versions := []string{"8", "99", "24"}
+2 -2
View File
@@ -16,8 +16,8 @@ const (
NotFound
// Challenge is a bot check that a real browser may clear.
Challenge
// Blocked is a refusal without a recognisable challenge, e.g. a plain 403
// or 429. Back off and retry later.
// Blocked is a refusal without a recognisable challenge, such as a plain
// 403 or 429. The host is backed off and retried later.
Blocked
// HTTPError is any other non-2xx status.
HTTPError
+23 -9
View File
@@ -13,6 +13,8 @@ import (
http "github.com/bogdanfinn/fhttp"
tlsclient "github.com/bogdanfinn/tls-client"
"git.security-command.org/Overlord/dibs/internal/ratelimit"
)
const (
@@ -23,14 +25,23 @@ const (
// ErrBodyTooLarge is returned when a response exceeds Options.MaxBody.
var ErrBodyTooLarge = errors.New("response body too large")
// Limiter decides when a page may be fetched. Every request waits for it.
type Limiter interface {
WaitCheck(ctx context.Context, url string) error
}
type Options struct {
// Browser to impersonate. Defaults to DefaultBrowser().
// Limiter defaults to a new in-memory ratelimit.Limiter, so requests are
// never unlimited.
Limiter Limiter
// Browser defaults to DefaultBrowser().
Browser *Browser
// Jar holds the cookies. Defaults to a new, empty jar.
// Jar defaults to a new, empty jar.
Jar *Jar
// Timeout per request, including redirects. Defaults to DefaultTimeout.
// Timeout covers a request and its redirects. It defaults to
// DefaultTimeout.
Timeout time.Duration
// MaxBody is the largest body read, after decompression. Defaults to
// MaxBody limits the body after decompression. It defaults to
// DefaultMaxBody.
MaxBody int64
}
@@ -38,12 +49,12 @@ type Options struct {
// Client fetches pages. It is safe for concurrent use.
type Client struct {
http tlsclient.HttpClient
limiter Limiter
browser Browser
jar *Jar
maxBody int64
}
// Response is a fetched page.
type Response struct {
URL string // as requested
FinalURL string // after redirects
@@ -60,6 +71,7 @@ type Response struct {
func New(opts Options) (*Client, error) {
browser := DefaultBrowser()
if opts.Browser != nil { browser = *opts.Browser }
if opts.Limiter == nil { opts.Limiter = ratelimit.New(nil) }
if opts.Jar == nil { opts.Jar = NewJar() }
if opts.Timeout == 0 { opts.Timeout = DefaultTimeout }
if opts.MaxBody == 0 { opts.MaxBody = DefaultMaxBody }
@@ -72,19 +84,21 @@ func New(opts Options) (*Client, error) {
)
if err != nil { return nil, fmt.Errorf("creating HTTP client: %w", err) }
return &Client{http: hc, browser: browser, jar: opts.Jar, maxBody: opts.MaxBody}, nil
return &Client{http: hc, limiter: opts.Limiter, browser: browser, jar: opts.Jar, maxBody: opts.MaxBody}, nil
}
// Jar returns the client's cookie jar.
func (c *Client) Jar() *Jar { return c.jar }
// Get fetches rawURL the way Chrome opens a bookmark. A response with a bad
// status is not an error: check Verdict.
// Get fetches rawURL the way Chrome opens a bookmark, once the limiter allows
// a check of it. A response with a bad status is not an error; its Verdict
// says what it is.
func (c *Client) Get(ctx context.Context, rawURL string) (*Response, error) {
u, err := url.Parse(rawURL)
if err != nil { return nil, err }
if u.Scheme != "http" && u.Scheme != "https" { return nil, fmt.Errorf("unsupported URL scheme %q", u.Scheme) }
if err := c.limiter.WaitCheck(ctx, rawURL); err != nil { return nil, err }
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil { return nil, err }
req.Header = c.browser.navigationHeaders(u)
+7
View File
@@ -172,9 +172,16 @@ func TestJarSaveLoad(t *testing.T) {
if err := NewJar().Load(filepath.Join(t.TempDir(), "missing.json")); err != nil { t.Errorf("missing file: %v", err) }
}
// noLimit lets tests talk to their local server without the host floor.
type noLimit struct{}
func (noLimit) WaitCheck(context.Context, string) error { return nil }
func newTestClient(t *testing.T, opts Options) *Client {
t.Helper()
if opts.Limiter == nil { opts.Limiter = noLimit{} }
c, err := New(opts)
if err != nil { t.Fatal(err) }
+3 -28
View File
@@ -7,7 +7,6 @@ import (
"maps"
"net/url"
"os"
"path/filepath"
"slices"
"strings"
"sync"
@@ -16,6 +15,8 @@ import (
http "github.com/bogdanfinn/fhttp"
"github.com/bogdanfinn/fhttp/cookiejar"
"golang.org/x/net/publicsuffix"
"git.security-command.org/Overlord/dibs/internal/fsutil"
)
// Cookie is a stored cookie, in a form that survives a restart and can be
@@ -54,7 +55,6 @@ func NewJar() *Jar {
}
}
// SetCookies implements http.CookieJar.
func (j *Jar) SetCookies(u *url.URL, cookies []*http.Cookie) {
j.mu.Lock()
defer j.mu.Unlock()
@@ -91,7 +91,6 @@ func (j *Jar) SetCookies(u *url.URL, cookies []*http.Cookie) {
}
}
// Cookies implements http.CookieJar.
func (j *Jar) Cookies(u *url.URL) []*http.Cookie {
j.mu.Lock()
defer j.mu.Unlock()
@@ -160,7 +159,7 @@ func (j *Jar) Save(path string) error {
data, err := json.MarshalIndent(j.Export(), "", " ")
if err != nil { return err }
return writeFileAtomic(path, data, 0o600)
return fsutil.WriteFileAtomic(path, data, 0o600)
}
// Load imports the cookies saved at path. A missing file is not an error.
@@ -186,27 +185,3 @@ func defaultPath(p string) string {
return p[:i]
}
// writeFileAtomic replaces path with data, so a crash never leaves a
// half-written file behind.
func writeFileAtomic(path string, data []byte, perm fs.FileMode) error {
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { return err }
tmp, err := os.CreateTemp(filepath.Dir(path), "."+filepath.Base(path)+".*")
if err != nil { return err }
defer func() { _ = os.Remove(tmp.Name()) }()
if err := tmp.Chmod(perm); err != nil {
_ = tmp.Close()
return err
}
if _, err := tmp.Write(data); err != nil {
_ = tmp.Close()
return err
}
if err := tmp.Close(); err != nil { return err }
return os.Rename(tmp.Name(), path)
}