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:
1 parent
bb010356a0
commit
2175c0e5aa
30 files changed
+1434
-126
No files matched your search
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user