crawler

package
v1.0.0-beta.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 30 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package crawler implements gocrawl's concurrent crawl engine. It fetches pages within a configurable scope, captures redirect chains and robots.txt data, and produces a Result that analyzers consume. The engine has no knowledge of specific SEO/SEA checks.

Index

Constants

View Source
const (
	PauseReasonUser        = "user"
	PauseReasonRateLimited = "rate_limited"
)

Pause reasons reported in Progress.PauseReason.

View Source
const MaxCrawlDelay = 60 * time.Second

MaxCrawlDelay caps the robots.txt Crawl-delay the crawler obeys. Some sites set values of minutes or hours; obeying those literally makes a crawl look hung. A crawl that must go slower can still set a lower RatePerSecond.

View Source
const TapePreviewLen = 160

TapePreviewLen is the length of a crawl list's tape preview: one compact row in the UI (MAX_COMPACT_TICKS in web/src/components/tape/tapeModel.ts).

Variables

View Source
var ErrRobotsDisallowed = errors.New("disallowed by robots.txt")

ErrRobotsDisallowed is returned by a governed fetcher for a URL robots.txt forbids.

Functions

func ParseHeaderLines added in v0.9.0

func ParseHeaderLines(lines []string) (http.Header, error)

ParseHeaderLines parses "Name: value" lines into an http.Header. Blank lines and lines starting with '#' are skipped, so the same parser reads both --header flags and a header file. A name given twice keeps the later value — so an inline --header can override one from a file — rather than sending the header twice. Names and values are validated as HTTP field syntax (which rules out CR/LF header injection), and names net/http manages itself or that have a dedicated option are refused; see reservedHeaders. Errors name the 1-based line number and never quote the line, since a header value can be a credential.

func SanitizeSeed added in v0.4.0

func SanitizeSeed(raw string) (seed, user, pass string)

SanitizeSeed strips any userinfo (user:pass@) embedded in a seed URL and returns it separately, so callers can route it through Options.BasicAuthUser/BasicAuthPass instead of letting it ride along in the URL string — where it would propagate into Result.Seed, every report format, and `gocrawl history`, and (via Go's URL-resolution semantics) into every link resolved against the seed once crawled. raw is expected to already have a scheme (callers prefix a bare host with "https://" before calling this).

func TapeChar

func TapeChar(status int) byte

TapeChar is the tape character for a recorded page's Progress.Status: '2', '3', '4' or '5' for the status class, 'x' for a failed fetch (status 0) or anything outside 200-599.

func TapeClasses

func TapeClasses(tape string) map[string]int

TapeClasses counts tape characters per status class ('2', '3', '4', '5', 'x'). It returns nil for an empty tape.

func TapePreview

func TapePreview(tape string, limit int) string

TapePreview shortens tape to at most limit characters for a crawl list. Each output character stands for a run of ceil(len/limit) consecutive pages and takes the worst status among them, so a single 5xx in a long crawl still shows. A tape that already fits is returned unchanged; limit <= 0 returns "".

Types

type AdaptiveFetcher added in v0.4.0

type AdaptiveFetcher struct {
	Fetcher Fetcher
	Limiter *AdaptiveLimiter
}

AdaptiveFetcher wraps a Fetcher so every call waits on Limiter beforehand and lets it observe the response afterward. This is how tools that fetch a bounded list of URLs directly (rather than crawling through Engine) still back off politely on 429/503.

func (*AdaptiveFetcher) Fetch added in v0.4.0

func (f *AdaptiveFetcher) Fetch(ctx context.Context, rawURL string) (*Page, error)

Fetch implements Fetcher.

type AdaptiveLimiter added in v0.4.0

type AdaptiveLimiter struct {
	// contains filtered or unexported fields
}

AdaptiveLimiter is a requests-per-second limiter that halves its rate (down to a floor) whenever a fetch reports HTTP 429 or 503, honoring any Retry-After header, and debounces bursts of concurrent triggers into a single adjustment. It is shared by anything that fetches a live site directly — the crawl Engine and tools like check-redirects — so a run that starts too aggressively for the target backs off instead of hammering it into a bot-mitigation block.

func NewAdaptiveLimiter added in v0.4.0

func NewAdaptiveLimiter(ratePerSecond float64, enabled bool) *AdaptiveLimiter

NewAdaptiveLimiter creates a limiter starting at ratePerSecond (0 = unlimited). Wait always enforces the current rate; when enabled is false, OnResponse never backs it off.

func (*AdaptiveLimiter) CurrentRate added in v0.4.0

func (a *AdaptiveLimiter) CurrentRate() float64

CurrentRate reports the rate in effect after any backoff (0 if still unrestricted).

func (*AdaptiveLimiter) EffectiveRate

func (a *AdaptiveLimiter) EffectiveRate() float64

EffectiveRate reports the requests-per-second limit in effect: the backed-off rate when adaptive delay has lowered it, otherwise the configured rate. 0 means unlimited.

func (*AdaptiveLimiter) LastRetryAfter

func (a *AdaptiveLimiter) LastRetryAfter() int

LastRetryAfter reports the most recent Retry-After a 429/503 carried, in whole seconds (rounded up, clamped to maxRetryAfter), or 0 when none did.

func (*AdaptiveLimiter) OnResponse added in v0.4.0

func (a *AdaptiveLimiter) OnResponse(statusCode int, header http.Header) (adjusted bool, newRate float64)

OnResponse inspects a fetch outcome and, if it signals overload (429/503), halves the current rate down to backoffMinRate, never faster than any Retry-After header demands. It reports whether this call triggered an adjustment (false if disabled, the status wasn't 429/503, or a trigger already landed within backoffDebounce) and the resulting rate. Every 429/503 is also recorded for RecentThrottled and LastRetryAfter, whether or not adaptive delay is enabled.

func (*AdaptiveLimiter) RecentThrottled

func (a *AdaptiveLimiter) RecentThrottled() int

RecentThrottled reports how many 429/503 responses arrived within the last throttleWindow.

func (*AdaptiveLimiter) ResetRecent

func (a *AdaptiveLimiter) ResetRecent()

ResetRecent forgets the recent 429/503 window and the last Retry-After, so a resumed crawl gets a fresh minute before auto-pause can trigger again, and a second pause can't report the previous pause's wait.

func (*AdaptiveLimiter) SetRate

func (a *AdaptiveLimiter) SetRate(r float64)

SetRate replaces the current rate (an operator resuming at a chosen speed). r <= 0 is ignored. Later 429/503s halve from the new rate.

func (*AdaptiveLimiter) ThrottleCount added in v0.4.0

func (a *AdaptiveLimiter) ThrottleCount() int

ThrottleCount reports how many times OnResponse has backed off the rate.

func (*AdaptiveLimiter) Wait added in v0.4.0

func (a *AdaptiveLimiter) Wait(ctx context.Context) error

Wait blocks until the current rate allows another request.

type CertInfo added in v0.5.0

type CertInfo struct {
	Subject            string
	Issuer             string
	NotBefore          time.Time
	NotAfter           time.Time
	SignatureAlgorithm x509.SignatureAlgorithm
	// KeyType and KeyBits describe the public key ("RSA"/2048, "ECDSA"/256, "Ed25519"/256).
	// KeyBits is 0 for a key type we don't measure.
	KeyType string
	KeyBits int
	IsCA    bool
	// SelfSigned is true when issuer and subject are the same distinguished name — either a
	// root CA at the top of the chain, or an untrusted self-signed server certificate.
	SelfSigned bool
}

CertInfo is the subset of an X.509 certificate the security audit reads. The full certificate is not retained: the audit only needs identity, validity window, and the strength of the signature and key.

type Cookie struct {
	Name   string
	Domain string
	Path   string
	// Session is true for a cookie with no expiry, which the browser drops when it closes.
	Session bool
	// Expires is the cookie's expiry, zero for a session cookie.
	Expires  time.Time
	Secure   bool
	HTTPOnly bool
	SameSite string
}

Cookie is one entry of the browser's cookie jar, captured during a headless render. It carries what a compliance check needs — identity, scope, and lifetime — and never the value, which is frequently a personal identifier and has no place in a report.

type Coverage added in v0.2.1

type Coverage struct {
	// Complete is true when no in-scope, robots-allowed URL was left un-fetched because of a
	// depth or page-count limit.
	Complete bool `json:"complete"`
	// DiscoveredNotCrawled is the number of distinct in-scope URLs that were discovered but
	// never fetched because a limit was reached.
	DiscoveredNotCrawled int `json:"discovered_not_crawled,omitempty"`
	// PageLimitReached / DepthLimitReached record which configured limit cut the crawl short.
	PageLimitReached  bool `json:"page_limit_reached,omitempty"`
	DepthLimitReached bool `json:"depth_limit_reached,omitempty"`
	// Interrupted is true when the crawl's context was canceled before it finished on its own
	// (e.g. an operator's Ctrl-C) rather than a configured limit being reached. The site may
	// be far less covered than DiscoveredNotCrawled reflects, since much of it may never have
	// been discovered at all.
	Interrupted bool `json:"interrupted,omitempty"`
	// DurationLimitReached is true when the crawl stopped because it used up its --max-duration
	// budget of active time (time spent paused doesn't count) rather than being interrupted
	// externally. Also implies Interrupted, since the same context-cancellation path stops the
	// crawl either way.
	DurationLimitReached bool `json:"duration_limit_reached,omitempty"`
	// MaxPages / MaxDepth echo the limits in effect (0 = unlimited), for the report message.
	MaxPages int `json:"max_pages,omitempty"`
	MaxDepth int `json:"max_depth,omitempty"`
	// SitemapTotal is the number of in-scope URLs the site's sitemap lists, found by
	// runner.Run's pre-pass; SitemapFetched is how many of them this crawl fetched. Both are 0,
	// and omitted, when no sitemap was found. Set by runner.Run, not by Engine.Crawl.
	SitemapTotal   int `json:"sitemap_total,omitempty"`
	SitemapFetched int `json:"sitemap_fetched,omitempty"`
	// Tape is the crawl tape: one character per recorded page in recording order, '2'..'5' for
	// the status class or 'x' for a failed fetch (see TapeChar; a redirected page reports its
	// first hop). TapeOff has the same length when a sitemap was found: '1' where the page is
	// not in its in-scope part, else '0'; without a sitemap it is empty. Set by runner.Run from
	// the same progress events the live web tape is built from. Omitted when empty.
	Tape    string `json:"tape,omitempty"`
	TapeOff string `json:"tape_off,omitempty"`
}

Coverage summarizes how much of the in-scope site the crawl actually fetched. It exists so the report can warn that "0 broken links" might mean "the broken ones weren't reached" rather than "the site is clean".

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine crawls a website concurrently within the configured scope.

Lock order, outermost first: Crawl's result mutex → progMu → ps.mu → the leaf locks (frontier.mu, activeClock.mu, hostPacer.mu, and the AdaptiveLimiter's mutex and its rate.Limiter's). One leaf edge: frontier.next calls hostPacer.busy, so frontier.mu → hostPacer.mu. A lock may skip levels (the result mutex reads frontier.queued directly; progMu takes the limiter's lock and ps.mu in turn, never together) but is never taken while a later one is held. OnProgress runs with progMu held, plus the result mutex for page events; ps.mu and the leaf locks are never held across it, and the activeClock's onLimit runs on a timer goroutine holding nothing. Callers outside the package add one edge: a job manager's lock (the web server's, held while it reads Control.Active → Engine.ActiveTime) is taken before ps.mu and then the activeClock's mutex, and no engine path takes a job lock while holding either, because ps.mu and the leaf locks are never held across OnProgress.

func New

func New(opts Options, fetcher Fetcher) *Engine

New creates an Engine. fetcher is used to retrieve pages (raw or headless); a separate raw fetcher is always used for robots.txt regardless of render mode.

func (*Engine) ActiveTime

func (e *Engine) ActiveTime() time.Duration

ActiveTime reports how long the current or last crawl has been running, excluding pauses. It is 0 before the first Crawl starts.

func (*Engine) Crawl

func (e *Engine) Crawl(ctx context.Context, seed string) (*Result, error)

Crawl walks the site starting at seed and returns the collected Result.

func (*Engine) EffectiveRate

func (e *Engine) EffectiveRate() float64

EffectiveRate reports the requests-per-second limit in effect (0 = unlimited).

func (*Engine) Govern added in v0.9.0

func (e *Engine) Govern(inner Fetcher) Fetcher

Govern wraps inner so every fetch through it obeys the crawl's own politeness rules: it waits on the engine's rate limiter and the host's robots.txt Crawl-delay, refuses URLs robots.txt disallows when RespectRobots is set, and reports each response back to the adaptive limiter so a 429 seen here slows the whole run. Analyzers that fetch extra resources after the crawl (sitemap.xml, llms.txt, the --specialized WordPress and Shopify probes) are handed a governed fetcher by runner.Run; without it those requests would bypass both the rate limit and robots.txt the crawl itself honoured, and run at full speed the moment the crawl finished.

Scope is deliberately not enforced: a sitemap declared in robots.txt routinely lives on another host, and that is the caller's decision, not this wrapper's.

func (*Engine) InScope

func (e *Engine) InScope(rawURL string) bool

InScope reports whether rawURL falls inside the crawl scope: the same scheme, host, subdomain, include and exclude rules enqueue applies, after the same normalization. It does not consult robots.txt or depth. Call SetSeed (or Crawl) first.

func (*Engine) Normalize

func (e *Engine) Normalize(rawURL string) string

Normalize canonicalizes rawURL the way the crawl does for deduplication, so a URL from outside the crawl (a sitemap <loc>) can be matched against recorded pages.

func (*Engine) Pause

func (e *Engine) Pause()

Pause stops handing out new fetches. Requests already in flight finish and are recorded. Called before Crawl, the crawl starts paused; called after Crawl returned, it does nothing. Cancelling the crawl's context still ends a paused crawl promptly, with a partial result. Pause must not be called from Options.OnProgress.

func (*Engine) Paused

func (e *Engine) Paused() bool

Paused reports whether the crawl is paused (or queued to start paused).

func (*Engine) Resume

func (e *Engine) Resume(rate float64)

Resume continues a paused crawl. rate > 0 replaces the request rate; 0 keeps it. It also clears the recent-429 window and the remembered Retry-After so auto-pause doesn't fire again on the old responses. It does nothing unless the crawl is paused (or queued to start paused). Resume must not be called from Options.OnProgress.

func (*Engine) RobotsFor

func (e *Engine) RobotsFor(ctx context.Context, rawURL string) *RobotsData

RobotsFor returns the parsed robots.txt for rawURL's scheme and host, fetching and caching it the way the crawl does (the crawl reuses the cached copy). Nil when rawURL has no host.

func (*Engine) SetRate

func (e *Engine) SetRate(r float64)

SetRate replaces the request rate without touching the pause state; r <= 0 is ignored. Unlike Resume it works before Crawl too, for a caller that picks a rate while a crawl is still queued.

func (*Engine) SetSeed

func (e *Engine) SetSeed(seed string) (string, error)

SetSeed normalizes seed and fixes the host scope that InScope, enqueue and the credential guard check against. Crawl calls it itself; call it first only when something needs InScope or RobotsFor before the crawl starts (runner.Run's sitemap pre-pass). It must not be called while a crawl is running. It returns the normalized seed.

type Fetcher

type Fetcher interface {
	Fetch(ctx context.Context, rawURL string) (*Page, error)
}

Fetcher retrieves a single URL and returns a populated Page. Implementations include the raw HTTPFetcher and the (stubbed) headless renderer.

type HTTPFetcher

type HTTPFetcher struct {
	// contains filtered or unexported fields
}

HTTPFetcher fetches pages over HTTP(S). It follows redirects manually so the full redirect chain is recorded on the resulting Page.

func NewHTTPFetcher

func NewHTTPFetcher(opts Options) *HTTPFetcher

NewHTTPFetcher builds a fetcher from the given options. When opts.Proxies is non-empty the client routes each request through a proxy chosen by opts.ProxyRotation; otherwise Go's default proxy behavior (environment variables) applies. The User-Agent header is chosen per request from opts.UserAgents / opts.UserAgent via opts.UserAgentRotation.

func (*HTTPFetcher) Fetch

func (f *HTTPFetcher) Fetch(ctx context.Context, rawURL string) (*Page, error)

Fetch retrieves rawURL, following redirects manually up to maxRedirects.

func (*HTTPFetcher) RestrictCredentialsToHost added in v0.7.0

func (f *HTTPFetcher) RestrictCredentialsToHost(seedHost string, allowSubdomains bool)

RestrictCredentialsToHost limits this fetcher's Basic Auth, Cookie header, and custom headers to requests whose host is seedHost, or one of its subdomains when allowSubdomains is set — the same scope rule the crawl itself uses (see Engine.authHostAllowed). Exported so packages outside crawler that build their own HTTPFetcher for crawl-scoped work (currently runner.Run, for the sitemap/geo/wordpress analyzers) can apply the same restriction Engine.New wires onto its own fetchers.

type Link struct {
	URL      string `json:"url"`                // absolute, normalized for dedup (trailing slash stripped)
	Resolved string `json:"resolved,omitempty"` // absolute, normalized but trailing slash preserved
	Anchor   string `json:"anchor"`             // visible text
	Rel      string `json:"rel"`
	Nofollow bool   `json:"nofollow"`
	External bool   `json:"external"` // points outside the seed host
}

Link is an outbound link discovered on a page.

type Options

type Options struct {
	MaxDepth      int
	MaxPages      int
	Concurrency   int
	RatePerSecond float64
	UserAgent     string
	// UserAgents is an optional pool of User-Agent strings to rotate across. When non-empty it
	// supersedes UserAgent; UserAgentRotation picks one per request.
	UserAgents        []string
	UserAgentRotation RotationStrategy
	// Proxies is an optional pool of proxy URLs (http, https, or socks5) to route requests
	// through. When non-empty, ProxyRotation picks one per request. Empty leaves Go's default
	// proxy behavior (honoring HTTP_PROXY/HTTPS_PROXY/NO_PROXY) in place.
	Proxies       []*url.URL
	ProxyRotation RotationStrategy
	// BasicAuthUser and BasicAuthPass, when BasicAuthUser is non-empty, are sent as an HTTP
	// Basic Authorization header on every request. This targets server-level Basic Auth (the
	// realm challenge common on staging/acceptance environments), sent preemptively rather
	// than in response to a 401 challenge.
	BasicAuthUser string
	BasicAuthPass string
	// Cookie, when non-empty, is sent verbatim as the Cookie header on every request to the
	// crawled host — for app-level session gates (e.g. a Shopify storefront password page,
	// or any site whose access check lives in a cookie rather than a Basic Auth realm) where
	// the operator already has a valid session and supplies its cookie by hand. Scoped and
	// leak-guarded identically to BasicAuthUser/Pass (see HTTPFetcher.authHostAllowed).
	Cookie string
	// Headers are extra request headers sent on every request to the crawled host — e.g. an
	// access token, or the Signature / Signature-Input / Signature-Agent triple of a Shopify
	// crawler access key. Treated as credentials: scoped and leak-guarded identically to
	// BasicAuthUser/Pass and Cookie. Build it with ParseHeaderLines, which refuses names the
	// HTTP client manages itself.
	Headers         http.Header
	Include         []*regexp.Regexp
	Exclude         []*regexp.Regexp
	RespectRobots   bool
	AllowSubdomains bool
	FollowExternal  bool
	FollowNofollow  bool
	// StripQuery drops the query string when normalizing URLs, so URLs that differ only by
	// their query collapse to one and are crawled once.
	StripQuery   bool
	Timeout      time.Duration
	MaxBodyBytes int64
	MaxRedirects int
	// Verbose logs each fetch and every rate-limit change to stderr while crawling.
	Verbose bool
	// AdaptiveDelay automatically slows the crawl (halving requests-per-second, honoring any
	// Retry-After header) when the server responds with HTTP 429 or 503.
	AdaptiveDelay bool
	// MaxDuration bounds the crawl's active time: wall-clock time minus time spent paused.
	// Zero means unlimited. When it elapses, the crawl stops early and still returns
	// everything fetched so far as a partial result (see Coverage.DurationLimitReached), the
	// same way a canceled context (e.g. an operator's Ctrl-C) does.
	MaxDuration time.Duration
	// PauseOnThrottle makes the engine pause itself when the server keeps rate limiting it:
	// ten or more 429/503 responses within a minute pause the crawl (Progress.PauseReason
	// "rate_limited", with the latest Retry-After). With adaptive delay on, adaptive delay must
	// also have lowered the rate at least twice; with it off there are no backoffs to wait for,
	// so the responses alone trigger the pause. Only useful when someone can call Resume: the
	// web UI turns it on, the CLI and MCP leave it off.
	PauseOnThrottle bool
	// OnProgress, when non-nil, is called with a snapshot of the crawl's progress after each
	// page is recorded and on every pause and resume (Engine.Pause, Engine.Resume). Calls are
	// serialized and arrive in recording order: Fetched rises by exactly one per page event,
	// and a pause or resume event repeats the last page's Fetched with fresh Paused, Rate and
	// Throttled fields. Page events run on a worker goroutine, pause and resume events on the
	// goroutine that called Pause or Resume (or the crawl's own, when it ends while paused),
	// in both cases while the engine holds internal locks: it must return quickly and must
	// never call back into the Engine, Pause and Resume included.
	OnProgress func(Progress)
}

Options controls crawl scope and politeness.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns conservative, polite defaults. By default the crawl is bounded by the total page budget (MaxPages), not by link depth (MaxDepth = 0 = unlimited): a shallow depth cap silently hides whole sections of a site — and the broken links in them — whereas a page budget walks the site breadth-first and stops at a predictable, reported size.

type Page

type Page struct {
	RequestedURL string      `json:"requested_url"`
	FinalURL     string      `json:"final_url"`
	StatusCode   int         `json:"status_code"`
	Header       http.Header `json:"-"`
	// TLS records the handshake behind the final response: protocol version, cipher suite, and
	// the certificate chain the server presented. Nil for plain HTTP, and in headless render
	// mode, where the response never passes through Go's TLS stack.
	TLS         *TLSInfo          `json:"-"`
	ContentType string            `json:"content_type"`
	Body        []byte            `json:"-"`
	RawBody     []byte            `json:"-"` // pre-JS HTML captured during headless render; nil in raw mode
	Doc         *goquery.Document `json:"-"` // nil if not HTML or parse failed
	Redirects   []Redirect        `json:"redirects,omitempty"`
	Links       []Link            `json:"-"`
	Depth       int               `json:"depth"`
	Referrer    string            `json:"referrer,omitempty"`
	Duration    time.Duration     `json:"duration_ms"`
	FetchedAt   time.Time         `json:"fetched_at"`
	Err         string            `json:"error,omitempty"`
	Render      *RenderResult     `json:"render,omitempty"`
	// Truncated reports whether Body was cut short of the real response — either because it
	// hit the fetcher's body-size cap or because the connection failed partway through the
	// read. A truncated body may be missing elements (e.g. </head>, the closing tag of a
	// sitemap) that a downstream analyzer would otherwise expect to find, so treat findings
	// like "missing title" or "invalid sitemap" on a truncated page with that in mind.
	Truncated bool `json:"truncated,omitempty"`
}

Page is the unit passed from the engine to analyzers.

func (*Page) IsHTML

func (p *Page) IsHTML() bool

IsHTML reports whether the page body was parsed as an HTML document.

type Progress

type Progress struct {
	// Fetched is the number of pages recorded so far, including this one.
	Fetched int `json:"fetched"`
	// Queued is the number of URLs waiting in the frontier.
	Queued int `json:"queued"`
	// CurrentURL is the URL just recorded, as requested; FinalURL is where it landed after
	// redirects.
	CurrentURL string `json:"current_url"`
	FinalURL   string `json:"final_url,omitempty"`
	// Status is the HTTP status of the page just recorded: the first hop's status when the
	// fetch was redirected (so a redirect reads as 3xx), otherwise the final status, and 0
	// when the fetch failed.
	Status int `json:"status"`
	// DurationMS is how long that fetch took.
	DurationMS int64 `json:"duration_ms"`
	// Rate is the requests-per-second limit in effect (0 = unlimited), after any backoff.
	Rate float64 `json:"rate"`
	// Throttled counts HTTP 429/503 responses in the last 60 seconds.
	Throttled int `json:"throttled"`
	// Paused, PauseReason ("user" or "rate_limited") and RetryAfterSeconds describe a pause.
	Paused            bool   `json:"paused"`
	PauseReason       string `json:"pause_reason,omitempty"`
	RetryAfterSeconds int    `json:"retry_after_seconds,omitempty"`
}

Progress is a snapshot of a running crawl, delivered through Options.OnProgress after each page is recorded and on every pause and resume.

type Redirect

type Redirect struct {
	From   string `json:"from"`
	To     string `json:"to"`
	Status int    `json:"status"`
}

Redirect is one hop in a redirect chain.

type RenderResult

type RenderResult struct {
	Implemented bool   `json:"implemented"`
	Note        string `json:"note,omitempty"`
	// RawFallback is set when the rendered DOM came back substantially thinner than the raw
	// HTML — a sign the page had not finished rendering when it was snapshotted. The analyzed
	// Body/Doc are then taken from the raw fetch (so structural checks like the H1 and meta
	// tags aren't false-negatives), while the Core Web Vitals below stay from the render but
	// should be treated as unreliable for this page. RenderedBytes/RawBytes record the sizes
	// that triggered it.
	RawFallback   bool    `json:"raw_fallback,omitempty"`
	RenderedBytes int     `json:"rendered_bytes,omitempty"`
	RawBytes      int     `json:"raw_bytes,omitempty"`
	LCP           float64 `json:"lcp_ms,omitempty"`
	FCP           float64 `json:"fcp_ms,omitempty"`
	CLS           float64 `json:"cls,omitempty"`
	TBT           float64 `json:"tbt_ms,omitempty"`
	TTFB          float64 `json:"ttfb_ms,omitempty"`

	// DataLayerPresent reports whether window.dataLayer was an array after the page rendered.
	DataLayerPresent bool `json:"data_layer_present,omitempty"`
	// DataLayer is the post-render snapshot of window.dataLayer, each element the raw JSON of
	// one entry (a GTM event push or a gtag() arguments object). It is deliberately not
	// serialized into reports: it can carry PII, so the datalayer analyzer emits sanitized
	// findings from it instead. Nil in raw mode or when the page has no dataLayer.
	DataLayer []json.RawMessage `json:"-"`
	// Requests holds outbound request URLs observed during render, bounded to avoid unbounded
	// growth. The datalayer analyzer uses it to confirm analytics/marketing tags actually
	// fired a network beacon. Not serialized.
	Requests []string `json:"-"`
	// Cookies is the browser's cookie jar after the page settled — every cookie actually
	// stored, first- and third-party, however it was set (Set-Cookie header, document.cookie,
	// or a third-party script). Response headers alone cannot see the last two, which is why
	// this is captured separately.
	//
	// gocrawl never interacts with a consent banner, so this jar is the site's *pre-consent*
	// state: the consent analyzer reads it to find tracking cookies dropped before a visitor
	// has agreed to anything. The jar belongs to the browser, not the tab, so it accumulates
	// across the pages of one crawl — treat it as a site-level observation rather than proof
	// that a particular page set a particular cookie. Nil in raw mode.
	Cookies []Cookie `json:"-"`
}

RenderResult holds headless-rendering output, including lab-mode Core Web Vitals. Implemented is true when chromedp produced real measurements; false when rendering fell back to a raw fetch (Note carries the reason). Metric times are milliseconds; CLS is the unitless layout-shift score. Zero means not collected.

INP is a field-only metric and is not collected in lab mode. TBT (Total Blocking Time) is reported as a lab-mode proxy for responsiveness, matching Lighthouse.

type Result

type Result struct {
	Seed      string                 `json:"seed"`
	Pages     []*Page                `json:"pages"`
	Robots    map[string]*RobotsData `json:"robots,omitempty"`
	StartedAt time.Time              `json:"started_at"`
	Finished  time.Time              `json:"finished_at"`
	Opts      Options                `json:"-"`

	// ThrottleEvents counts how many times adaptive delay reduced the crawl rate after HTTP
	// 429/503 responses. FinalRate is the requests-per-second in effect when the crawl ended;
	// it is meaningful only when ThrottleEvents > 0.
	ThrottleEvents int     `json:"throttle_events,omitempty"`
	FinalRate      float64 `json:"final_rate,omitempty"`

	// Coverage reports whether the crawl visited every in-scope URL it discovered, or stopped
	// at a configured limit. When not Complete, findings that depend on fetching a page —
	// broken links above all — may be incomplete.
	Coverage Coverage `json:"coverage"`
	// contains filtered or unexported fields
}

Result is the complete output of a crawl, consumed by analyzers.

func (*Result) LinkPointsToRedirect added in v0.2.2

func (r *Result) LinkPointsToRedirect(resolved string, target *Page) bool

LinkPointsToRedirect reports whether an internal link whose resolved (slash-preserving) target is `resolved` points at `target`, a crawled page that redirects to a genuinely different URL. It returns false when the page's only redirect is the trailing slash the crawl index strips before fetching, so a link authored with the site's canonical trailing slash (e.g. WordPress permalinks) is not reported as pointing at a redirect.

func (*Result) Page

func (r *Result) Page(rawURL string) (*Page, bool)

Page looks up a crawled page by URL (matched against requested and final URLs after normalization). The second return value reports whether it was found.

func (*Result) Reindex

func (r *Result) Reindex()

Reindex rebuilds the URL lookup index from r.Pages. The engine populates the index incrementally during a crawl, so production code never needs this; it lets callers that construct a Result by hand (notably tests) make Page lookups resolve.

func (*Result) ReleaseBodies added in v0.4.0

func (r *Result) ReleaseBodies()

ReleaseBodies drops every page's Body, RawBody, and parsed Doc, freeing the memory they hold. Call this once analysis and report-building have both finished reading pages — nothing in this codebase reads a page's body or DOM afterward, and a large crawl of heavy pages can otherwise hold multiple GB of raw bytes and parsed DOM trees in memory for the rest of the process's life. That matters most for a long-lived caller handling many crawls in one process (e.g. the MCP server), where the memory would otherwise accumulate across calls until the next GC cycle catches up.

func (*Result) ResolveHref added in v0.4.0

func (r *Result) ResolveHref(from *Page, href string) (target *Page, resolved string, ok bool)

ResolveHref resolves href relative to from's own URL (mirroring how the crawl engine resolves an <a href> against a page's base — including an in-document <base href>, if any — via extractLinks) and looks up the resulting page in the crawl result. It exists for analyzers that read a raw href straight from the DOM (e.g. <link rel="amphtml"|"next"|"prev">) rather than from the engine's own extracted Links, so a relative href is resolved the same way an anchor link would be instead of failing the lookup outright. resolved is the slash-preserving absolute form, suitable for LinkPointsToRedirect. ok is false for an unusable href (empty, fragment-only, non-http(s) after resolution) or one with no match in the crawl.

type RobotsData

type RobotsData struct {
	Host     string   `json:"host"`
	Found    bool     `json:"found"`
	Status   int      `json:"status"`
	Sitemaps []string `json:"sitemaps,omitempty"`
	// contains filtered or unexported fields
}

RobotsData is the parsed robots.txt for a single host.

func (*RobotsData) CrawlDelay

func (r *RobotsData) CrawlDelay(userAgent string) time.Duration

CrawlDelay returns the robots.txt Crawl-delay for the group that applies to userAgent, or 0 when robots.txt has none (or was not found or not parsed).

func (*RobotsData) TestAgent

func (r *RobotsData) TestAgent(path, userAgent string) bool

TestAgent reports whether the given path is allowed for userAgent. Per RFC 9309, a 4xx robots.txt response means no rules apply (allow all); an unreachable robots.txt (network error or 5xx) means we can't know the site's intent, so crawling is disallowed until it's reachable. A 200 response we simply couldn't parse degrades to allow-all.

type RotationStrategy

type RotationStrategy int

RotationStrategy selects how a pool (of proxies or User-Agent strings) picks an entry for a given request. It is shared by the proxy pool and the User-Agent pool so both rotate with identical semantics.

const (
	// RotateRoundRobin cycles through the pool in order, one entry per request. It is the
	// default once a pool has more than one entry.
	RotateRoundRobin RotationStrategy = iota
	// RotateRandom picks a uniformly random entry per request.
	RotateRandom
	// RotateStickyHost maps each target host to a fixed entry (by hash), so every request to a
	// given host uses the same proxy/User-Agent. For proxies this avoids a server seeing a
	// single session arrive from several IPs.
	RotateStickyHost
	// RotateOff disables rotation: the first entry is always used. A pool with a single entry
	// behaves this way regardless of strategy.
	RotateOff
)

func ParseRotation

func ParseRotation(s string) (RotationStrategy, error)

ParseRotation converts a config string into a RotationStrategy. An empty string defaults to round-robin, so simply supplying a multi-entry pool rotates without extra configuration.

type TLSInfo added in v0.5.0

type TLSInfo struct {
	Version     uint16
	CipherSuite uint16
	// ALPN is the application protocol negotiated during the handshake ("h2", "http/1.1"), or
	// empty when the server offered none.
	ALPN string
	// Chain is the certificate chain the server presented, leaf first. It is what the server
	// actually sent, not the verified chain, so a server that omits its intermediates shows up
	// here as a one-element chain even though the fetch succeeded.
	Chain []CertInfo
}

TLSInfo is a snapshot of the TLS handshake that carried a page's final response: the negotiated protocol version and cipher suite, the ALPN protocol, and the certificate chain the server presented (leaf first). The engine captures it and knows nothing more about it; interpreting it is the security analyzer's opt-in audit mode's job.

It is deliberately not serialized into reports: every page of a crawl shares one host's handshake, so repeating an identical chain per page would bloat the JSON for no benefit. Findings derived from it travel as analyze.Issue values instead.

func (*TLSInfo) CipherName added in v0.5.0

func (t *TLSInfo) CipherName() string

CipherName renders the negotiated cipher suite ("TLS_AES_128_GCM_SHA256").

func (*TLSInfo) Leaf added in v0.5.0

func (t *TLSInfo) Leaf() *CertInfo

Leaf returns the server (end-entity) certificate, or nil when the chain is empty.

func (*TLSInfo) VersionName added in v0.5.0

func (t *TLSInfo) VersionName() string

VersionName renders the negotiated protocol version ("TLS 1.3").

type UAPool

type UAPool struct {
	// contains filtered or unexported fields
}

UAPool selects a User-Agent string per request. It is built from Options and shared by the raw HTTP fetcher and the headless renderer so both rotate identically. A nil pool, or one with no agents, yields the empty string (callers then leave the header unset).

func NewUAPool

func NewUAPool(opts Options) *UAPool

NewUAPool builds a User-Agent pool from opts. When UserAgents is set it is the pool; otherwise the single UserAgent (if any) is the only entry.

func (*UAPool) Default

func (p *UAPool) Default() string

Default returns the pool's first agent without advancing rotation state. The headless renderer uses it as the browser-level User-Agent; per-navigation rotation is layered on top.

func (*UAPool) Next

func (p *UAPool) Next(host string) string

Next returns the User-Agent to use for a request to host, advancing rotation state as the strategy requires.

func (*UAPool) Rotates

func (p *UAPool) Rotates() bool

Rotates reports whether the pool has more than one agent (i.e. rotation can occur).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL