Documentation
¶
Overview ¶
Package agent implements the outbound-only WebSocket worker loop connecting EdgeFetch standby nodes (Docker containers, Home Assistant OS add-ons, Raspberry Pis, or bare-metal binaries) to Cloudflare's Hibernating WebSocket Durable Object (AgentHubDO).
Architecture & Security Invariants ¶
- Zero inbound ports: nodes initiate outbound TLS 1.3 WebSockets (`wss://<hub>/agent/ws`) authenticated via `Authorization: Bearer <agent_token>` (>= 24 characters) and self-identify capabilities (`http2-v1`, `scrape-v2`, `llms-v1`, `browser-v1`, `interact-v1`, `pdf-v1`) via `X-Agent-*` headers and `agent_hello`.
- Hibernation keepalive: sends lightweight `"ping"` text frames so Cloudflare's edge answers `"pong"` via `setWebSocketAutoResponse` at $0 Durable Object wall-clock duration cost while idle.
- Out-of-band R2 streaming: when `artifacts.mode == "r2"`, response bodies are gzipped, SHA-256 hashed, and uploaded directly via `PUT /agent/artifacts/:blobSha256` before the metadata envelope is sent to `POST /agent/reply`, preventing multi-megabyte payloads from passing through Durable Object memory.
- Graceful SIGTERM drain: active jobs continue up to `ShutdownDrain` (default 25s) before container exit.
Index ¶
- Constants
- Variables
- func FilterRelayResponseHeaders(h http.Header) map[string]string
- func IsRelayablePath(path string) bool
- func Run(ctx context.Context, cfg Config, crawler *crawl.Crawler, ...) error
- func RunAll(ctx context.Context, cfg Config, crawler *crawl.Crawler, ...) error
- func ValidateRelayRequest(req *RelayRequest) (int, string)
- type Config
- type HubRelayFunc
- type RelayRequest
- type RelayResponse
- type RuntimeOpts
Constants ¶
const ( DefaultPingInterval = 30 * time.Second DefaultReadWatchdog = 50 * time.Second DefaultHealthyResetDur = 30 * time.Second DefaultShutdownDrain = 25 * time.Second DefaultUploadTimeout = 45 * time.Second DefaultShutdownUploadTimeout = 10 * time.Second )
const ( DefaultHAOptionsPath = "/data/options.json" DefaultPauseMs = 1000 DefaultPauseJitterMs = 400 MinTokenLength = 24 )
const ( // MaxRelayRequestBytes caps a relayed request body (decoded). MaxRelayRequestBytes = 4 << 20 // MaxRelayResponseBytes caps a relayed response body; larger responses fail with 502 relay_response_too_large. MaxRelayResponseBytes = 16 << 20 )
Variables ¶
var Version = "2.8.37"
Version is the EdgeFetch binary/add-on release version reported in X-Agent-Version and agent_hello. Overridable at build time via -ldflags="-X github.com/bartrumb/edgefetch/v2/internal/agent.Version=...".
Functions ¶
func FilterRelayResponseHeaders ¶ added in v2.8.37
FilterRelayResponseHeaders keeps only the response headers a relay hub may pass back to callers.
func IsRelayablePath ¶ added in v2.8.37
IsRelayablePath reports whether path is part of the API surface a relay hub may forward: the REST API (/v1/...), the MCP endpoint and the OpenAPI document. Admin, agent, OAuth and every other hub-internal route are refused.
func Run ¶
func Run( ctx context.Context, cfg Config, crawler *crawl.Crawler, allow func(*url.URL) error, hubTransport http.RoundTripper, log *slog.Logger, rtOpts RuntimeOpts, ) error
Run maintains a persistent outbound WebSocket session to `cfg.HubURL/agent/ws`, automatically reconnecting with full-jitter exponential backoff on network drops and draining in-flight work cleanly on `SIGTERM`.
func RunAll ¶
func RunAll( ctx context.Context, cfg Config, crawler *crawl.Crawler, allow func(*url.URL) error, hubTransport http.RoundTripper, log *slog.Logger, rtOpts RuntimeOpts, ) error
RunAll maintains persistent outbound WebSocket sessions to every controller in `cfg.HubURLs` (falling back to `cfg.HubURL`), sharing one `*crawl.Crawler` and `*politeness.Registry` across active-active controllers with zero coordination overhead.
func ValidateRelayRequest ¶ added in v2.8.37
func ValidateRelayRequest(req *RelayRequest) (int, string)
ValidateRelayRequest normalizes req and reports a client-facing error when it must not be executed.
Types ¶
type Config ¶
type Config struct {
HubURL string `json:"hub_url"`
HubURLs []string `json:"hub_urls,omitempty"`
AgentToken string `json:"agent_token"`
SiteID string `json:"site_id"`
UserAgent string `json:"user_agent"`
PauseMs int `json:"pause_ms"`
PauseJitterMs int `json:"pause_jitter_ms"`
AllowedHosts []string `json:"allowed_hosts"`
RobotsBypassHosts []string `json:"robots_bypass_hosts,omitempty"`
AllowNoSandboxBrowser bool `json:"allow_no_sandbox_browser,omitempty"`
}
Config holds validated settings for outbound `agent` mode (including multi-controller HA via `HubURLs`).
func LoadConfig ¶
LoadConfig loads `/data/options.json` (when present) and overlays `EDGEFETCH_*` (or `CRAWLER_*`) environment variables.
type HubRelayFunc ¶ added in v2.8.37
type HubRelayFunc func(ctx context.Context, req RelayRequest) RelayResponse
HubRelayFunc answers a validated RelayRequest (cmd/edgefetch wires it to an in-process Go hub).
type RelayRequest ¶ added in v2.8.37
type RelayRequest struct {
Method string `json:"method"`
Path string `json:"path"`
RawQuery string `json:"rawQuery,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
BodyB64 string `json:"bodyB64,omitempty"`
}
RelayRequest is one hub HTTP request forwarded by a relay hub.
type RelayResponse ¶ added in v2.8.37
type RelayResponse struct {
Status int `json:"status"`
Headers map[string]string `json:"headers,omitempty"`
BodyB64 string `json:"bodyB64,omitempty"`
}
RelayResponse is the node's answer to a RelayRequest.
type RuntimeOpts ¶
type RuntimeOpts struct {
PingInterval time.Duration
ReadWatchdog time.Duration
HealthyResetDur time.Duration
ShutdownDrain time.Duration
UploadTimeout time.Duration
ShutdownUploadTimeout time.Duration
WarmUpOnStart bool
OnConnected func()
// HubRelay answers "relay" messages from a relay hub with an in-process Go hub; nil means the node refuses them.
HubRelay HubRelayFunc
}
RuntimeOpts allows integration tests to shorten keepalive/drain timers without altering production defaults.