Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

refactor: split oversized files by responsibility

Luigi Agostie7c18db950ea
diff --git a/internal/bridge/action_request.go b/internal/bridge/action_request.go
new file mode 100644
index 00000000..e500e102
--- /dev/null
+++ b/internal/bridge/action_request.go
@@ -0,0 +1,166 @@
+package bridge
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "log/slog"
+)
+
+// ActionFunc is the type for action handlers.
+type ActionFunc func(ctx context.Context, req ActionRequest) (map[string]any, error)
+
+// ActionRequest defines the parameters for a browser action.
+//
+// Element targeting uses a unified selector string that supports multiple
+// strategies via prefix detection (see the selector package):
+//
+// "e5" → ref from snapshot
+// "css:#login" → CSS selector (explicit)
+// "#login" → CSS selector (auto-detected)
+// "xpath://div" → XPath expression
+// "text:Submit" → text content match
+// "find:login btn" → semantic / natural-language query
+//
+// For backward compatibility, the legacy Ref and Selector (CSS) fields
+// are still accepted. Call NormalizeSelector() to merge them into the
+// unified Selector field.
+type ActionRequest struct {
+ TabID string `json:"tabId"`
+ Kind string `json:"kind"`
+ Ref string `json:"ref,omitempty"`
+ Selector string `json:"selector,omitempty"`
+ Text string `json:"text"`
+ Key string `json:"key"`
+ Value string `json:"value"`
+ NodeID int64 `json:"nodeId"`
+
+ // X/Y use omitempty so that re-marshaling an ActionRequest without
+ // explicit coordinates (e.g. when the tab-scoped handler forwards to
+ // the generic one) doesn't spuriously re-introduce "x":0, "y":0. The
+ // ActionRequest.UnmarshalJSON code infers HasXY from the presence of
+ // these keys, so preserving omission is what makes "use current
+ // pointer" work for mouse-down/up after a prior mouse-move. HasXY is
+ // still marshaled (with omitempty) when it was explicitly set, which
+ // preserves the explicit-click-at-(0,0) case through the round-trip.
+ X float64 `json:"x,omitempty"`
+ Y float64 `json:"y,omitempty"`
+ HasXY bool `json:"hasXY,omitempty"`
+ Button string `json:"button,omitempty"`
+
+ ScrollX int `json:"scrollX"`
+ ScrollY int `json:"scrollY"`
+ // DeltaX/DeltaY are explicit mouse-wheel deltas for low-level
+ // mouse-wheel actions. ScrollX/ScrollY remain for backward compatibility.
+ DeltaX int `json:"deltaX,omitempty"`
+ DeltaY int `json:"deltaY,omitempty"`
+ DragX int `json:"dragX"`
+ DragY int `json:"dragY"`
+
+ WaitNav bool `json:"waitNav"`
+ Fast bool `json:"fast"`
+ Owner string `json:"owner"`
+
+ // DialogAction arms a one-shot dialog auto-handler before the action
+ // executes. Used when clicking a button/link that opens a JS dialog
+ // (alert/confirm/prompt). Values: "accept" or "dismiss". When set, the
+ // dialog is handled automatically without a second HTTP call.
+ DialogAction string `json:"dialogAction,omitempty"`
+ // DialogText is the optional prompt text used when DialogAction is
+ // "accept" on a prompt() dialog.
+ DialogText string `json:"dialogText,omitempty"`
+}
+
+type actionRequestAlias ActionRequest
+
+func (r *ActionRequest) UnmarshalJSON(data []byte) error {
+ var alias actionRequestAlias
+ if err := json.Unmarshal(data, &alias); err != nil {
+ return err
+ }
+ raw := map[string]json.RawMessage{}
+ if err := json.Unmarshal(data, &raw); err != nil {
+ return err
+ }
+
+ *r = ActionRequest(alias)
+ r.Kind = CanonicalActionKind(r.Kind)
+ r.HasXY = r.HasXY || hasJSONKey(raw, "x") || hasJSONKey(raw, "y")
+ if hasJSONKey(raw, "deltaX") {
+ if err := json.Unmarshal(raw["deltaX"], &r.DeltaX); err != nil {
+ return err
+ }
+ }
+ if hasJSONKey(raw, "deltaY") {
+ if err := json.Unmarshal(raw["deltaY"], &r.DeltaY); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
+// NormalizeSelector merges legacy Ref and Selector (CSS) fields into the
+// unified Selector field. After calling this, only Selector needs to be
+// inspected for element targeting. The method is idempotent.
+//
+// Priority: Ref > Selector (if both are set, Ref wins).
+func (r *ActionRequest) NormalizeSelector() {
+ if r.Ref != "" && r.Selector == "" {
+ r.Selector = r.Ref
+ }
+}
+
+func CanonicalActionKind(kind string) string {
+ return kind
+}
+
+func hasJSONKey(raw map[string]json.RawMessage, key string) bool {
+ _, ok := raw[key]
+ return ok
+}
+
+func (b *Bridge) ExecuteAction(ctx context.Context, kind string, req ActionRequest) (map[string]any, error) {
+ kind = CanonicalActionKind(kind)
+ req.Kind = CanonicalActionKind(req.Kind)
+ fn, ok := b.Actions[kind]
+ if !ok {
+ return nil, fmt.Errorf("unknown action: %s", kind)
+ }
+ guardEnabled := b.Config == nil || b.Config.EnableActionGuards
+ checkNav := guardEnabled && shouldCheckUnexpectedNavigation(req)
+ urlReader := b.URLReader
+ if urlReader == nil {
+ urlReader = defaultActionURLReader
+ slog.Debug("URLReader is nil, using default fallback (guard checks may be no-ops without chromedp context)")
+ }
+ var beforeURL string
+ if checkNav {
+ if u, err := urlReader(ctx); err == nil {
+ beforeURL = u
+ }
+ }
+
+ res, err := fn(ctx, req)
+ if err != nil {
+ return nil, classifyActionError(err)
+ }
+
+ if checkNav && beforeURL != "" {
+ afterURL, uErr := urlReader(ctx)
+ if uErr == nil {
+ if navErr := checkUnexpectedNavigation(beforeURL, afterURL); navErr != nil {
+ return nil, navErr
+ }
+ }
+ }
+
+ return res, nil
+}
+
+func (b *Bridge) AvailableActions() []string {
+ keys := make([]string, 0, len(b.Actions))
+ for k := range b.Actions {
+ keys = append(keys, k)
+ }
+ return keys
+}
diff --git a/internal/bridge/bridge.go b/internal/bridge/bridge.go
index c775d453..f267ee8d 100644
--- a/internal/bridge/bridge.go
+++ b/internal/bridge/bridge.go
@@ -2,103 +2,16 @@ package bridge
import (
"context"
- "crypto/rand"
- "encoding/json"
"fmt"
- "log/slog"
- "math/big"
- "os"
"sync"
"time"
- "github.com/chromedp/cdproto/page"
"github.com/chromedp/cdproto/target"
- "github.com/chromedp/chromedp"
"github.com/pinchtab/pinchtab/internal/config"
"github.com/pinchtab/pinchtab/internal/ids"
"github.com/pinchtab/pinchtab/internal/stealth"
)
-type TabEntry struct {
- Ctx context.Context
- Cancel context.CancelFunc
- Accessed bool
- CDPID string // raw CDP target ID
- CreatedAt time.Time // when the tab was first created/registered
- LastUsed time.Time // last time the tab was accessed via TabContext
- Policy TabPolicyState
- Watching bool
- ConsoleCaptureEnabled bool
-
- // Lifecycle auto-close timer. autoCloseGen is bumped on every (re)schedule
- // so a fire that races with a reset/cancel can detect itself and bail.
- autoCloseTimer *time.Timer
- autoCloseGen uint64
-}
-
-type RefTarget struct {
- BackendNodeID int64 `json:"backendNodeId"`
- FrameID string `json:"frameId,omitempty"`
- FrameURL string `json:"frameUrl,omitempty"`
- FrameName string `json:"frameName,omitempty"`
- ChildFrameID string `json:"childFrameId,omitempty"`
- ChildFrameURL string `json:"childFrameUrl,omitempty"`
- ChildFrameName string `json:"childFrameName,omitempty"`
-}
-
-type RefCache struct {
- Refs map[string]int64
- Targets map[string]RefTarget
- Nodes []A11yNode
-}
-
-func (c *RefCache) Lookup(ref string) (RefTarget, bool) {
- if c == nil {
- return RefTarget{}, false
- }
- if c.Targets != nil {
- if target, ok := c.Targets[ref]; ok {
- return target, true
- }
- }
- if c.Refs != nil {
- if nid, ok := c.Refs[ref]; ok {
- return RefTarget{BackendNodeID: nid}, true
- }
- }
- return RefTarget{}, false
-}
-
-func RefTargetsFromNodes(nodes []A11yNode) map[string]RefTarget {
- targets := make(map[string]RefTarget, len(nodes))
- for _, node := range nodes {
- if node.Ref == "" || node.NodeID == 0 {
- continue
- }
- targets[node.Ref] = RefTarget{
- BackendNodeID: node.NodeID,
- FrameID: node.FrameID,
- FrameURL: node.FrameURL,
- FrameName: node.FrameName,
- ChildFrameID: node.ChildFrameID,
- ChildFrameURL: node.ChildFrameURL,
- ChildFrameName: node.ChildFrameName,
- }
- }
- return targets
-}
-
-type FrameScope struct {
- FrameID string `json:"frameId,omitempty"`
- FrameURL string `json:"frameUrl,omitempty"`
- FrameName string `json:"frameName,omitempty"`
- OwnerRef string `json:"ownerRef,omitempty"`
-}
-
-func (s FrameScope) Active() bool {
- return s.FrameID != ""
-}
-
type Bridge struct {
AllocCtx context.Context
AllocCancel context.CancelFunc
@@ -159,7 +72,6 @@ func New(allocCtx, browserCtx context.Context, cfg *config.RuntimeConfig) *Bridg
}
b.ensureStealthBundle()
b.Dialogs = NewDialogManager()
- // Only initialize TabManager if browserCtx is provided (not lazy-init case)
if cfg != nil && browserCtx != nil {
b.TabManager = NewTabManager(browserCtx, cfg, idMgr, logStore, b.tabSetup)
b.SetOnAfterClose(func() { go b.SaveState() })
@@ -174,68 +86,6 @@ func New(allocCtx, browserCtx context.Context, cfg *config.RuntimeConfig) *Bridg
return b
}
-func (b *Bridge) quietStealthObservers() bool {
- return b != nil && b.Config != nil && stealth.NormalizeLevel(b.Config.StealthLevel) == stealth.LevelFull
-}
-
-func (b *Bridge) RestartStatus() (bool, time.Duration) {
- if b == nil {
- return false, 0
- }
- b.initMu.Lock()
- defer b.initMu.Unlock()
- if !b.draining {
- return false, 0
- }
- remaining := time.Until(b.drainUntil)
- if remaining < 0 {
- remaining = 0
- }
- return true, remaining
-}
-
-func (b *Bridge) injectStealth(ctx context.Context) {
- if b.StealthBundle == nil || b.StealthBundle.Script == "" {
- return
- }
- if err := chromedp.Run(ctx,
- chromedp.ActionFunc(func(ctx context.Context) error {
- _, err := page.AddScriptToEvaluateOnNewDocument(b.StealthBundle.Script).Do(ctx)
- return err
- }),
- ); err != nil {
- slog.Warn("stealth injection failed", "err", err)
- }
-}
-
-func (b *Bridge) applyTargetStealth(ctx context.Context) {
- if b == nil || b.Config == nil {
- return
- }
-
- ua := ""
- if b.StealthBundle != nil {
- ua = b.StealthBundle.LaunchUserAgent()
- }
-
- if err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {
- return stealth.ApplyTargetEmulation(ctx, b.Config, ua)
- })); err != nil {
- slog.Warn("stealth target emulation failed", "err", err)
- }
-}
-
-func (b *Bridge) tabSetup(ctx context.Context) {
- b.applyTargetStealth(ctx)
- b.installWorkerStealthParity(ctx)
- b.injectStealth(ctx)
- if b.Config.NoAnimations {
- if err := b.InjectNoAnimations(ctx); err != nil {
- slog.Warn("no-animations injection failed", "err", err)
- }
- }
-}
-
// StartNetworkCapture enables network monitoring for a specific tab.
// This is called lazily when network data is first requested for a tab.
func (b *Bridge) StartNetworkCapture(tabCtx context.Context, tabID string) error {
@@ -361,292 +211,6 @@ func (b *Bridge) ClearErrorLogs(tabID string) {
}
}
-func (b *Bridge) EnsureChrome(cfg *config.RuntimeConfig) error {
- b.initMu.Lock()
- defer b.initMu.Unlock()
-
- if b.draining {
- return ErrBrowserDraining
- }
-
- if b.initialized && b.BrowserCtx != nil {
- // Check if browser context is still alive
- if b.BrowserCtx.Err() == nil {
- return nil
- }
- // Chrome died — reset state for re-initialization
- slog.Warn("chrome context cancelled, re-initializing")
- b.initialized = false
- b.BrowserCtx = nil
- b.BrowserCancel = nil
- b.AllocCtx = nil
- b.AllocCancel = nil
- b.TabManager = nil
- }
-
- if b.BrowserCtx != nil {
- if b.BrowserCtx.Err() == nil {
- return nil
- }
- b.BrowserCtx = nil
- b.BrowserCancel = nil
- }
-
- slog.Debug("ensure chrome called", "headless", cfg.Headless, "profile", cfg.ProfileDir)
-
- // Initialize Chrome if not already done
- if err := AcquireProfileLock(cfg.ProfileDir); err != nil {
- if cfg.Headless {
- // If we are in headless mode, we are more flexible.
- // Instead of failing, we can use a unique temporary profile dir.
- uniqueDir, tmpErr := os.MkdirTemp("", "pinchtab-profile-*")
- if tmpErr == nil {
- slog.Warn("profile in use; using unique temporary profile for headless instance",
- "requested", cfg.ProfileDir, "using", uniqueDir, "reason", err.Error())
- cfg.ProfileDir = uniqueDir
- b.tempProfileDir = uniqueDir
- // Re-acquire lock for the new temp dir (should always succeed)
- _ = AcquireProfileLock(cfg.ProfileDir)
- } else {
- slog.Error("cannot acquire profile lock and failed to create temp dir", "profile", cfg.ProfileDir, "err", err.Error(), "tmpErr", tmpErr.Error())
- return fmt.Errorf("profile lock: %w (temp dir failed: %v)", err, tmpErr)
- }
- } else {
- slog.Error("cannot acquire profile lock; another pinchtab may be active", "profile", cfg.ProfileDir, "err", err.Error())
- return fmt.Errorf("profile lock: %w", err)
- }
- }
-
- slog.Info("starting chrome with confirmed profile", "headless", cfg.Headless, "profile", cfg.ProfileDir)
- b.ensureStealthBundle()
- allocCtx, allocCancel, browserCtx, browserCancel, launchMode, err := InitChrome(cfg, b.StealthBundle)
- if err != nil {
- return fmt.Errorf("failed to initialize chrome: %w", err)
- }
-
- b.AllocCtx = allocCtx
- b.AllocCancel = allocCancel
- b.BrowserCtx = browserCtx
- b.BrowserCancel = browserCancel
- b.initialized = true
- b.stealthLaunchMode = launchMode
-
- // Initialize TabManager now that browser is ready
- if b.Config != nil && b.TabManager == nil {
- if b.IdMgr == nil {
- b.IdMgr = ids.NewManager()
- }
- if b.LogStore == nil {
- b.LogStore = NewConsoleLogStore(1000)
- }
- b.TabManager = NewTabManager(browserCtx, b.Config, b.IdMgr, b.LogStore, b.tabSetup)
- b.SetOnAfterClose(func() { go b.SaveState() })
- b.SetDialogManager(b.Dialogs)
- b.SetNetworkMonitor(b.netMonitor)
- if !b.quietStealthObservers() {
- b.StartBrowserGuards()
- }
- }
-
- // Ensure action registry is populated (idempotent)
- if b.Actions == nil {
- b.InitActionRegistry()
- }
-
- // Restore tabs from previous session (if any saved state exists). Opt-in
- // via instanceDefaults.tabPolicy.restore=true; otherwise tabs closed
- // before shutdown stay closed.
- if b.tempProfileDir == "" && b.Config != nil && b.Config.TabRestore {
- b.RestoreState()
- }
-
- // Start crash monitoring
- if !b.quietStealthObservers() {
- b.MonitorCrashes(nil)
- }
-
- return nil
-}
-
-// Cleanup releases browser resources and removes temporary profile directories.
-// Must be called on shutdown to prevent Chrome process and disk leaks.
-func (b *Bridge) RestartBrowser(cfg *config.RuntimeConfig) error {
- if cfg == nil {
- cfg = b.Config
- }
- if cfg == nil {
- return fmt.Errorf("runtime config is required")
- }
-
- const drainWindow = 2 * time.Second
-
- b.initMu.Lock()
- b.draining = true
- b.drainUntil = time.Now().Add(drainWindow)
- b.initMu.Unlock()
-
- slog.Info("browser soft restart: draining requests before restart", "drain_window", drainWindow)
- time.Sleep(drainWindow)
-
- b.initMu.Lock()
-
- if b.BrowserCancel != nil {
- b.BrowserCancel()
- slog.Info("browser soft restart: cancelled browser context")
- }
- if b.AllocCancel != nil {
- b.AllocCancel()
- slog.Info("browser soft restart: cancelled allocator context")
- }
-
- profileDir := ""
- if b.tempProfileDir != "" {
- profileDir = b.tempProfileDir
- } else {
- profileDir = cfg.ProfileDir
- }
- if profileDir != "" {
- time.Sleep(200 * time.Millisecond)
- killed := killChromeByProfileDir(profileDir)
- if killed > 0 {
- slog.Info("browser soft restart: killed surviving chrome processes", "count", killed, "profileDir", profileDir)
- }
- ClearChromeSessions(profileDir)
- }
- b.ClearSavedState()
-
- if b.tempProfileDir != "" {
- if err := os.RemoveAll(b.tempProfileDir); err != nil {
- slog.Warn("failed to remove temp profile dir during restart", "path", b.tempProfileDir, "err", err)
- } else {
- slog.Info("removed temp profile dir during restart", "path", b.tempProfileDir)
- }
- b.tempProfileDir = ""
- }
-
- b.initialized = false
- b.BrowserCtx = nil
- b.BrowserCancel = nil
- b.AllocCtx = nil
- b.AllocCancel = nil
- b.TabManager = nil
- b.stealthLaunchMode = stealth.LaunchModeUninitialized
-
- b.LogStore = NewConsoleLogStore(1000)
- b.netMonitor = NewNetworkMonitor(DefaultNetworkBufferSize)
- if cfg.NetworkBufferSize > 0 {
- b.netMonitor = NewNetworkMonitor(cfg.NetworkBufferSize)
- }
- b.fingerprintMu.Lock()
- b.fingerprintOverlays = make(map[string]bool)
- b.fingerprintMu.Unlock()
- b.workerStealthTargets = sync.Map{}
- b.Dialogs = NewDialogManager()
- b.Locks = NewLockManager()
- b.Config = cfg
-
- b.StealthBundle = nil
- b.Actions = nil
- b.InitActionRegistry()
-
- b.draining = false
- b.drainUntil = time.Time{}
- b.initMu.Unlock()
-
- if err := b.EnsureChrome(cfg); err != nil {
- return err
- }
- b.CleanupSavedStateBackup()
- return nil
-}
-
-func (b *Bridge) Cleanup() {
- // Persist open tabs so next startup can restore them
- if b.TabManager != nil && b.tempProfileDir == "" {
- b.SaveState()
- }
-
- // Mark a clean exit so Chrome doesn't show a crash recovery bar
- if b.Config != nil && b.tempProfileDir == "" {
- MarkCleanExit(b.Config.ProfileDir)
- }
-
- // Cancel chromedp contexts (kills main Chrome process)
- if b.BrowserCancel != nil {
- b.BrowserCancel()
- slog.Debug("chrome browser context cancelled")
- }
- if b.AllocCancel != nil {
- b.AllocCancel()
- slog.Debug("chrome allocator context cancelled")
- }
-
- // Chrome spawns helpers (GPU, renderer) in their own process groups.
- // Context cancellation only kills the main process. Kill survivors
- // by scanning for processes using our profile directory.
- profileDir := ""
- if b.tempProfileDir != "" {
- profileDir = b.tempProfileDir
- } else if b.Config != nil {
- profileDir = b.Config.ProfileDir
- }
- if profileDir != "" {
- // Brief wait for context cancel to propagate
- time.Sleep(200 * time.Millisecond)
- killed := killChromeByProfileDir(profileDir)
- if killed > 0 {
- slog.Info("cleanup: killed surviving chrome processes", "count", killed, "profileDir", profileDir)
- }
- }
-
- if b.tempProfileDir != "" {
- if err := os.RemoveAll(b.tempProfileDir); err != nil {
- slog.Warn("failed to remove temp profile dir", "path", b.tempProfileDir, "err", err)
- } else {
- slog.Info("removed temp profile dir", "path", b.tempProfileDir)
- }
- b.tempProfileDir = ""
- }
-}
-
-func (b *Bridge) SetBrowserContexts(allocCtx context.Context, allocCancel context.CancelFunc, browserCtx context.Context, browserCancel context.CancelFunc) {
- b.initMu.Lock()
- defer b.initMu.Unlock()
-
- b.AllocCtx = allocCtx
- b.AllocCancel = allocCancel
- b.BrowserCtx = browserCtx
- b.BrowserCancel = browserCancel
- b.initialized = true
- b.stealthLaunchMode = stealth.LaunchModeAttached
-
- // Now initialize TabManager with the browser context
- if b.Config != nil && b.TabManager == nil {
- if b.IdMgr == nil {
- b.IdMgr = ids.NewManager()
- }
- if b.LogStore == nil {
- b.LogStore = NewConsoleLogStore(1000)
- }
- b.TabManager = NewTabManager(browserCtx, b.Config, b.IdMgr, b.LogStore, b.tabSetup)
- b.SetOnAfterClose(func() { go b.SaveState() })
- b.SetDialogManager(b.Dialogs)
- b.SetNetworkMonitor(b.netMonitor)
- }
-}
-
-func (b *Bridge) ensureStealthBundle() {
- if b.StealthBundle != nil || b.Config == nil {
- return
- }
- b.StealthBundle = stealth.NewBundle(b.Config, cryptoRandSeed())
-}
-
-func (b *Bridge) StealthStatus() *stealth.Status {
- b.ensureStealthBundle()
- return stealth.StatusFromBundle(b.StealthBundle, b.Config, b.stealthLaunchMode)
-}
-
func (b *Bridge) SetFingerprintRotateActive(tabID string, active bool) {
if tabID == "" {
return
@@ -669,44 +233,6 @@ func (b *Bridge) BrowserContext() context.Context {
return b.BrowserCtx
}
-func (b *Bridge) ExecuteAction(ctx context.Context, kind string, req ActionRequest) (map[string]any, error) {
- kind = CanonicalActionKind(kind)
- req.Kind = CanonicalActionKind(req.Kind)
- fn, ok := b.Actions[kind]
- if !ok {
- return nil, fmt.Errorf("unknown action: %s", kind)
- }
- guardEnabled := b.Config == nil || b.Config.EnableActionGuards
- checkNav := guardEnabled && shouldCheckUnexpectedNavigation(req)
- urlReader := b.URLReader
- if urlReader == nil {
- urlReader = defaultActionURLReader
- slog.Debug("URLReader is nil, using default fallback (guard checks may be no-ops without chromedp context)")
- }
- var beforeURL string
- if checkNav {
- if u, err := urlReader(ctx); err == nil {
- beforeURL = u
- }
- }
-
- res, err := fn(ctx, req)
- if err != nil {
- return nil, classifyActionError(err)
- }
-
- if checkNav && beforeURL != "" {
- afterURL, uErr := urlReader(ctx)
- if uErr == nil {
- if navErr := checkUnexpectedNavigation(beforeURL, afterURL); navErr != nil {
- return nil, navErr
- }
- }
- }
-
- return res, nil
-}
-
// Execute delegates to TabManager.Execute for safe parallel tab execution.
// If TabManager is not initialized, the task runs directly.
func (b *Bridge) Execute(ctx context.Context, tabID string, task func(ctx context.Context) error) error {
@@ -721,138 +247,7 @@ func (b *Bridge) NetworkMonitor() *NetworkMonitor {
return b.netMonitor
}
-func (b *Bridge) AvailableActions() []string {
- keys := make([]string, 0, len(b.Actions))
- for k := range b.Actions {
- keys = append(keys, k)
- }
- return keys
-}
-
// GetDialogManager returns the bridge's dialog manager.
func (b *Bridge) GetDialogManager() *DialogManager {
return b.Dialogs
}
-
-// ActionFunc is the type for action handlers.
-type ActionFunc func(ctx context.Context, req ActionRequest) (map[string]any, error)
-
-// ActionRequest defines the parameters for a browser action.
-//
-// Element targeting uses a unified selector string that supports multiple
-// strategies via prefix detection (see the selector package):
-//
-// "e5" → ref from snapshot
-// "css:#login" → CSS selector (explicit)
-// "#login" → CSS selector (auto-detected)
-// "xpath://div" → XPath expression
-// "text:Submit" → text content match
-// "find:login btn" → semantic / natural-language query
-//
-// For backward compatibility, the legacy Ref and Selector (CSS) fields
-// are still accepted. Call NormalizeSelector() to merge them into the
-// unified Selector field.
-type ActionRequest struct {
- TabID string `json:"tabId"`
- Kind string `json:"kind"`
- Ref string `json:"ref,omitempty"`
- Selector string `json:"selector,omitempty"`
- Text string `json:"text"`
- Key string `json:"key"`
- Value string `json:"value"`
- NodeID int64 `json:"nodeId"`
-
- // X/Y use omitempty so that re-marshaling an ActionRequest without
- // explicit coordinates (e.g. when the tab-scoped handler forwards to
- // the generic one) doesn't spuriously re-introduce "x":0, "y":0. The
- // ActionRequest.UnmarshalJSON code infers HasXY from the presence of
- // these keys, so preserving omission is what makes "use current
- // pointer" work for mouse-down/up after a prior mouse-move. HasXY is
- // still marshaled (with omitempty) when it was explicitly set, which
- // preserves the explicit-click-at-(0,0) case through the round-trip.
- X float64 `json:"x,omitempty"`
- Y float64 `json:"y,omitempty"`
- HasXY bool `json:"hasXY,omitempty"`
- Button string `json:"button,omitempty"`
-
- ScrollX int `json:"scrollX"`
- ScrollY int `json:"scrollY"`
- // DeltaX/DeltaY are explicit mouse-wheel deltas for low-level
- // mouse-wheel actions. ScrollX/ScrollY remain for backward compatibility.
- DeltaX int `json:"deltaX,omitempty"`
- DeltaY int `json:"deltaY,omitempty"`
- DragX int `json:"dragX"`
- DragY int `json:"dragY"`
-
- WaitNav bool `json:"waitNav"`
- Fast bool `json:"fast"`
- Owner string `json:"owner"`
-
- // DialogAction arms a one-shot dialog auto-handler before the action
- // executes. Used when clicking a button/link that opens a JS dialog
- // (alert/confirm/prompt). Values: "accept" or "dismiss". When set, the
- // dialog is handled automatically without a second HTTP call.
- DialogAction string `json:"dialogAction,omitempty"`
- // DialogText is the optional prompt text used when DialogAction is
- // "accept" on a prompt() dialog.
- DialogText string `json:"dialogText,omitempty"`
-}
-
-type actionRequestAlias ActionRequest
-
-func (r *ActionRequest) UnmarshalJSON(data []byte) error {
- var alias actionRequestAlias
- if err := json.Unmarshal(data, &alias); err != nil {
- return err
- }
- raw := map[string]json.RawMessage{}
- if err := json.Unmarshal(data, &raw); err != nil {
- return err
- }
-
- *r = ActionRequest(alias)
- r.Kind = CanonicalActionKind(r.Kind)
- r.HasXY = r.HasXY || hasJSONKey(raw, "x") || hasJSONKey(raw, "y")
- if hasJSONKey(raw, "deltaX") {
- if err := json.Unmarshal(raw["deltaX"], &r.DeltaX); err != nil {
- return err
- }
- }
- if hasJSONKey(raw, "deltaY") {
- if err := json.Unmarshal(raw["deltaY"], &r.DeltaY); err != nil {
- return err
- }
- }
- return nil
-}
-
-// NormalizeSelector merges legacy Ref and Selector (CSS) fields into the
-// unified Selector field. After calling this, only Selector needs to be
-// inspected for element targeting. The method is idempotent.
-//
-// Priority: Ref > Selector (if both are set, Ref wins).
-func (r *ActionRequest) NormalizeSelector() {
- if r.Ref != "" && r.Selector == "" {
- // Legacy ref field → unified selector
- r.Selector = r.Ref
- }
- // If Selector is already set (either from JSON or from Ref promotion),
- // leave it as-is — Parse() will auto-detect the kind.
-}
-
-func CanonicalActionKind(kind string) string {
- return kind
-}
-
-func hasJSONKey(raw map[string]json.RawMessage, key string) bool {
- _, ok := raw[key]
- return ok
-}
-
-func cryptoRandSeed() int64 {
- n, err := rand.Int(rand.Reader, big.NewInt(1000000000))
- if err != nil {
- return 42
- }
- return n.Int64()
-}
diff --git a/internal/bridge/bridge_lifecycle.go b/internal/bridge/bridge_lifecycle.go
new file mode 100644
index 00000000..dc89c670
--- /dev/null
+++ b/internal/bridge/bridge_lifecycle.go
@@ -0,0 +1,366 @@
+package bridge
+
+import (
+ "context"
+ "crypto/rand"
+ "fmt"
+ "log/slog"
+ "math/big"
+ "os"
+ "sync"
+ "time"
+
+ "github.com/chromedp/cdproto/page"
+ "github.com/chromedp/chromedp"
+ "github.com/pinchtab/pinchtab/internal/config"
+ "github.com/pinchtab/pinchtab/internal/ids"
+ "github.com/pinchtab/pinchtab/internal/stealth"
+)
+
+func (b *Bridge) quietStealthObservers() bool {
+ return b != nil && b.Config != nil && stealth.NormalizeLevel(b.Config.StealthLevel) == stealth.LevelFull
+}
+
+func (b *Bridge) RestartStatus() (bool, time.Duration) {
+ if b == nil {
+ return false, 0
+ }
+ b.initMu.Lock()
+ defer b.initMu.Unlock()
+ if !b.draining {
+ return false, 0
+ }
+ remaining := time.Until(b.drainUntil)
+ if remaining < 0 {
+ remaining = 0
+ }
+ return true, remaining
+}
+
+func (b *Bridge) injectStealth(ctx context.Context) {
+ if b.StealthBundle == nil || b.StealthBundle.Script == "" {
+ return
+ }
+ if err := chromedp.Run(ctx,
+ chromedp.ActionFunc(func(ctx context.Context) error {
+ _, err := page.AddScriptToEvaluateOnNewDocument(b.StealthBundle.Script).Do(ctx)
+ return err
+ }),
+ ); err != nil {
+ slog.Warn("stealth injection failed", "err", err)
+ }
+}
+
+func (b *Bridge) applyTargetStealth(ctx context.Context) {
+ if b == nil || b.Config == nil {
+ return
+ }
+
+ ua := ""
+ if b.StealthBundle != nil {
+ ua = b.StealthBundle.LaunchUserAgent()
+ }
+
+ if err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {
+ return stealth.ApplyTargetEmulation(ctx, b.Config, ua)
+ })); err != nil {
+ slog.Warn("stealth target emulation failed", "err", err)
+ }
+}
+
+func (b *Bridge) tabSetup(ctx context.Context) {
+ b.applyTargetStealth(ctx)
+ b.installWorkerStealthParity(ctx)
+ b.injectStealth(ctx)
+ if b.Config.NoAnimations {
+ if err := b.InjectNoAnimations(ctx); err != nil {
+ slog.Warn("no-animations injection failed", "err", err)
+ }
+ }
+}
+
+func (b *Bridge) ensureStealthBundle() {
+ if b.StealthBundle != nil || b.Config == nil {
+ return
+ }
+ b.StealthBundle = stealth.NewBundle(b.Config, cryptoRandSeed())
+}
+
+func (b *Bridge) StealthStatus() *stealth.Status {
+ b.ensureStealthBundle()
+ return stealth.StatusFromBundle(b.StealthBundle, b.Config, b.stealthLaunchMode)
+}
+
+func (b *Bridge) EnsureChrome(cfg *config.RuntimeConfig) error {
+ b.initMu.Lock()
+ defer b.initMu.Unlock()
+
+ if b.draining {
+ return ErrBrowserDraining
+ }
+
+ if b.initialized && b.BrowserCtx != nil {
+ if b.BrowserCtx.Err() == nil {
+ return nil
+ }
+ // Chrome died — reset state for re-initialization
+ slog.Warn("chrome context cancelled, re-initializing")
+ b.initialized = false
+ b.BrowserCtx = nil
+ b.BrowserCancel = nil
+ b.AllocCtx = nil
+ b.AllocCancel = nil
+ b.TabManager = nil
+ }
+
+ if b.BrowserCtx != nil {
+ if b.BrowserCtx.Err() == nil {
+ return nil
+ }
+ b.BrowserCtx = nil
+ b.BrowserCancel = nil
+ }
+
+ slog.Debug("ensure chrome called", "headless", cfg.Headless, "profile", cfg.ProfileDir)
+
+ if err := AcquireProfileLock(cfg.ProfileDir); err != nil {
+ if cfg.Headless {
+ // If we are in headless mode, we are more flexible.
+ // Instead of failing, we can use a unique temporary profile dir.
+ uniqueDir, tmpErr := os.MkdirTemp("", "pinchtab-profile-*")
+ if tmpErr == nil {
+ slog.Warn("profile in use; using unique temporary profile for headless instance",
+ "requested", cfg.ProfileDir, "using", uniqueDir, "reason", err.Error())
+ cfg.ProfileDir = uniqueDir
+ b.tempProfileDir = uniqueDir
+ _ = AcquireProfileLock(cfg.ProfileDir)
+ } else {
+ slog.Error("cannot acquire profile lock and failed to create temp dir", "profile", cfg.ProfileDir, "err", err.Error(), "tmpErr", tmpErr.Error())
+ return fmt.Errorf("profile lock: %w (temp dir failed: %v)", err, tmpErr)
+ }
+ } else {
+ slog.Error("cannot acquire profile lock; another pinchtab may be active", "profile", cfg.ProfileDir, "err", err.Error())
+ return fmt.Errorf("profile lock: %w", err)
+ }
+ }
+
+ slog.Info("starting chrome with confirmed profile", "headless", cfg.Headless, "profile", cfg.ProfileDir)
+ b.ensureStealthBundle()
+ allocCtx, allocCancel, browserCtx, browserCancel, launchMode, err := InitChrome(cfg, b.StealthBundle)
+ if err != nil {
+ return fmt.Errorf("failed to initialize chrome: %w", err)
+ }
+
+ b.AllocCtx = allocCtx
+ b.AllocCancel = allocCancel
+ b.BrowserCtx = browserCtx
+ b.BrowserCancel = browserCancel
+ b.initialized = true
+ b.stealthLaunchMode = launchMode
+
+ if b.Config != nil && b.TabManager == nil {
+ if b.IdMgr == nil {
+ b.IdMgr = ids.NewManager()
+ }
+ if b.LogStore == nil {
+ b.LogStore = NewConsoleLogStore(1000)
+ }
+ b.TabManager = NewTabManager(browserCtx, b.Config, b.IdMgr, b.LogStore, b.tabSetup)
+ b.SetOnAfterClose(func() { go b.SaveState() })
+ b.SetDialogManager(b.Dialogs)
+ b.SetNetworkMonitor(b.netMonitor)
+ if !b.quietStealthObservers() {
+ b.StartBrowserGuards()
+ }
+ }
+
+ if b.Actions == nil {
+ b.InitActionRegistry()
+ }
+
+ // Restore tabs from previous session (if any saved state exists). Opt-in
+ // via instanceDefaults.tabPolicy.restore=true; otherwise tabs closed
+ // before shutdown stay closed.
+ if b.tempProfileDir == "" && b.Config != nil && b.Config.TabRestore {
+ b.RestoreState()
+ }
+
+ if !b.quietStealthObservers() {
+ b.MonitorCrashes(nil)
+ }
+
+ return nil
+}
+
+// RestartBrowser performs a soft restart: drains in-flight requests, tears
+// down Chrome contexts, and re-initializes via EnsureChrome.
+func (b *Bridge) RestartBrowser(cfg *config.RuntimeConfig) error {
+ if cfg == nil {
+ cfg = b.Config
+ }
+ if cfg == nil {
+ return fmt.Errorf("runtime config is required")
+ }
+
+ const drainWindow = 2 * time.Second
+
+ b.initMu.Lock()
+ b.draining = true
+ b.drainUntil = time.Now().Add(drainWindow)
+ b.initMu.Unlock()
+
+ slog.Info("browser soft restart: draining requests before restart", "drain_window", drainWindow)
+ time.Sleep(drainWindow)
+
+ b.initMu.Lock()
+
+ if b.BrowserCancel != nil {
+ b.BrowserCancel()
+ slog.Info("browser soft restart: cancelled browser context")
+ }
+ if b.AllocCancel != nil {
+ b.AllocCancel()
+ slog.Info("browser soft restart: cancelled allocator context")
+ }
+
+ profileDir := ""
+ if b.tempProfileDir != "" {
+ profileDir = b.tempProfileDir
+ } else {
+ profileDir = cfg.ProfileDir
+ }
+ if profileDir != "" {
+ time.Sleep(200 * time.Millisecond)
+ killed := killChromeByProfileDir(profileDir)
+ if killed > 0 {
+ slog.Info("browser soft restart: killed surviving chrome processes", "count", killed, "profileDir", profileDir)
+ }
+ ClearChromeSessions(profileDir)
+ }
+ b.ClearSavedState()
+
+ if b.tempProfileDir != "" {
+ if err := os.RemoveAll(b.tempProfileDir); err != nil {
+ slog.Warn("failed to remove temp profile dir during restart", "path", b.tempProfileDir, "err", err)
+ } else {
+ slog.Info("removed temp profile dir during restart", "path", b.tempProfileDir)
+ }
+ b.tempProfileDir = ""
+ }
+
+ b.initialized = false
+ b.BrowserCtx = nil
+ b.BrowserCancel = nil
+ b.AllocCtx = nil
+ b.AllocCancel = nil
+ b.TabManager = nil
+ b.stealthLaunchMode = stealth.LaunchModeUninitialized
+
+ b.LogStore = NewConsoleLogStore(1000)
+ b.netMonitor = NewNetworkMonitor(DefaultNetworkBufferSize)
+ if cfg.NetworkBufferSize > 0 {
+ b.netMonitor = NewNetworkMonitor(cfg.NetworkBufferSize)
+ }
+ b.fingerprintMu.Lock()
+ b.fingerprintOverlays = make(map[string]bool)
+ b.fingerprintMu.Unlock()
+ b.workerStealthTargets = sync.Map{}
+ b.Dialogs = NewDialogManager()
+ b.Locks = NewLockManager()
+ b.Config = cfg
+
+ b.StealthBundle = nil
+ b.Actions = nil
+ b.InitActionRegistry()
+
+ b.draining = false
+ b.drainUntil = time.Time{}
+ b.initMu.Unlock()
+
+ if err := b.EnsureChrome(cfg); err != nil {
+ return err
+ }
+ b.CleanupSavedStateBackup()
+ return nil
+}
+
+// Cleanup releases browser resources and removes temporary profile directories.
+// Must be called on shutdown to prevent Chrome process and disk leaks.
+func (b *Bridge) Cleanup() {
+ if b.TabManager != nil && b.tempProfileDir == "" {
+ b.SaveState()
+ }
+
+ // Mark a clean exit so Chrome doesn't show a crash recovery bar
+ if b.Config != nil && b.tempProfileDir == "" {
+ MarkCleanExit(b.Config.ProfileDir)
+ }
+
+ if b.BrowserCancel != nil {
+ b.BrowserCancel()
+ slog.Debug("chrome browser context cancelled")
+ }
+ if b.AllocCancel != nil {
+ b.AllocCancel()
+ slog.Debug("chrome allocator context cancelled")
+ }
+
+ // Chrome spawns helpers (GPU, renderer) in their own process groups.
+ // Context cancellation only kills the main process. Kill survivors
+ // by scanning for processes using our profile directory.
+ profileDir := ""
+ if b.tempProfileDir != "" {
+ profileDir = b.tempProfileDir
+ } else if b.Config != nil {
+ profileDir = b.Config.ProfileDir
+ }
+ if profileDir != "" {
+ time.Sleep(200 * time.Millisecond)
+ killed := killChromeByProfileDir(profileDir)
+ if killed > 0 {
+ slog.Info("cleanup: killed surviving chrome processes", "count", killed, "profileDir", profileDir)
+ }
+ }
+
+ if b.tempProfileDir != "" {
+ if err := os.RemoveAll(b.tempProfileDir); err != nil {
+ slog.Warn("failed to remove temp profile dir", "path", b.tempProfileDir, "err", err)
+ } else {
+ slog.Info("removed temp profile dir", "path", b.tempProfileDir)
+ }
+ b.tempProfileDir = ""
+ }
+}
+
+func (b *Bridge) SetBrowserContexts(allocCtx context.Context, allocCancel context.CancelFunc, browserCtx context.Context, browserCancel context.CancelFunc) {
+ b.initMu.Lock()
+ defer b.initMu.Unlock()
+
+ b.AllocCtx = allocCtx
+ b.AllocCancel = allocCancel
+ b.BrowserCtx = browserCtx
+ b.BrowserCancel = browserCancel
+ b.initialized = true
+ b.stealthLaunchMode = stealth.LaunchModeAttached
+
+ if b.Config != nil && b.TabManager == nil {
+ if b.IdMgr == nil {
+ b.IdMgr = ids.NewManager()
+ }
+ if b.LogStore == nil {
+ b.LogStore = NewConsoleLogStore(1000)
+ }
+ b.TabManager = NewTabManager(browserCtx, b.Config, b.IdMgr, b.LogStore, b.tabSetup)
+ b.SetOnAfterClose(func() { go b.SaveState() })
+ b.SetDialogManager(b.Dialogs)
+ b.SetNetworkMonitor(b.netMonitor)
+ }
+}
+
+func cryptoRandSeed() int64 {
+ n, err := rand.Int(rand.Reader, big.NewInt(1000000000))
+ if err != nil {
+ return 42
+ }
+ return n.Int64()
+}
diff --git a/internal/bridge/bridge_types.go b/internal/bridge/bridge_types.go
new file mode 100644
index 00000000..51b1e73d
--- /dev/null
+++ b/internal/bridge/bridge_types.go
@@ -0,0 +1,86 @@
+package bridge
+
+import (
+ "context"
+ "time"
+)
+
+type TabEntry struct {
+ Ctx context.Context
+ Cancel context.CancelFunc
+ Accessed bool
+ CDPID string // raw CDP target ID
+ CreatedAt time.Time // when the tab was first created/registered
+ LastUsed time.Time // last time the tab was accessed via TabContext
+ Policy TabPolicyState
+ Watching bool
+ ConsoleCaptureEnabled bool
+
+ // Lifecycle auto-close timer. autoCloseGen is bumped on every (re)schedule
+ // so a fire that races with a reset/cancel can detect itself and bail.
+ autoCloseTimer *time.Timer
+ autoCloseGen uint64
+}
+
+type RefTarget struct {
+ BackendNodeID int64 `json:"backendNodeId"`
+ FrameID string `json:"frameId,omitempty"`
+ FrameURL string `json:"frameUrl,omitempty"`
+ FrameName string `json:"frameName,omitempty"`
+ ChildFrameID string `json:"childFrameId,omitempty"`
+ ChildFrameURL string `json:"childFrameUrl,omitempty"`
+ ChildFrameName string `json:"childFrameName,omitempty"`
+}
+
+type RefCache struct {
+ Refs map[string]int64
+ Targets map[string]RefTarget
+ Nodes []A11yNode
+}
+
+func (c *RefCache) Lookup(ref string) (RefTarget, bool) {
+ if c == nil {
+ return RefTarget{}, false
+ }
+ if c.Targets != nil {
+ if target, ok := c.Targets[ref]; ok {
+ return target, true
+ }
+ }
+ if c.Refs != nil {
+ if nid, ok := c.Refs[ref]; ok {
+ return RefTarget{BackendNodeID: nid}, true
+ }
+ }
+ return RefTarget{}, false
+}
+
+func RefTargetsFromNodes(nodes []A11yNode) map[string]RefTarget {
+ targets := make(map[string]RefTarget, len(nodes))
+ for _, node := range nodes {
+ if node.Ref == "" || node.NodeID == 0 {
+ continue
+ }
+ targets[node.Ref] = RefTarget{
+ BackendNodeID: node.NodeID,
+ FrameID: node.FrameID,
+ FrameURL: node.FrameURL,
+ FrameName: node.FrameName,
+ ChildFrameID: node.ChildFrameID,
+ ChildFrameURL: node.ChildFrameURL,
+ ChildFrameName: node.ChildFrameName,
+ }
+ }
+ return targets
+}
+
+type FrameScope struct {
+ FrameID string `json:"frameId,omitempty"`
+ FrameURL string `json:"frameUrl,omitempty"`
+ FrameName string `json:"frameName,omitempty"`
+ OwnerRef string `json:"ownerRef,omitempty"`
+}
+
+func (s FrameScope) Active() bool {
+ return s.FrameID != ""
+}
diff --git a/internal/bridge/console_capture.go b/internal/bridge/console_capture.go
new file mode 100644
index 00000000..2c58b425
--- /dev/null
+++ b/internal/bridge/console_capture.go
@@ -0,0 +1,223 @@
+package bridge
+
+import (
+ "context"
+ "net/url"
+ "strings"
+ "sync"
+ "time"
+
+ "github.com/chromedp/cdproto/runtime"
+ "github.com/chromedp/chromedp"
+)
+
+// setupConsoleCapture enables runtime domain and listens for console/exception events.
+func (tm *TabManager) setupConsoleCapture(ctx context.Context, rawCDPID string) {
+ if tm.logStore == nil {
+ return
+ }
+
+ execContextSources := make(map[runtime.ExecutionContextID]string)
+ var execContextMu sync.RWMutex
+
+ chromedp.ListenTarget(ctx, func(ev any) {
+ switch ev := ev.(type) {
+ case *runtime.EventExecutionContextCreated:
+ if ev.Context == nil {
+ return
+ }
+ execContextMu.Lock()
+ execContextSources[ev.Context.ID] = executionContextSource(ev.Context)
+ execContextMu.Unlock()
+
+ case *runtime.EventExecutionContextDestroyed:
+ execContextMu.Lock()
+ delete(execContextSources, ev.ExecutionContextID)
+ execContextMu.Unlock()
+
+ case *runtime.EventExecutionContextsCleared:
+ execContextMu.Lock()
+ clear(execContextSources)
+ execContextMu.Unlock()
+
+ case *runtime.EventConsoleAPICalled:
+ var msg string
+ for _, arg := range ev.Args {
+ if len(arg.Value) > 0 {
+ val := string(arg.Value)
+ if len(val) >= 2 && val[0] == '"' && val[len(val)-1] == '"' {
+ val = val[1 : len(val)-1]
+ }
+ msg += val
+ } else if arg.Description != "" {
+ msg += arg.Description
+ }
+ msg += " "
+ }
+
+ var ts time.Time
+ if ev.Timestamp != nil {
+ ts = time.Time(*ev.Timestamp)
+ } else {
+ ts = time.Now()
+ }
+
+ source := stackTraceSource(ev.StackTrace)
+ if source == "" {
+ execContextMu.RLock()
+ source = execContextSources[ev.ExecutionContextID]
+ execContextMu.RUnlock()
+ }
+ if source == "" {
+ source = strings.TrimSpace(ev.Context)
+ }
+ if isInternalConsoleSource(source) {
+ return
+ }
+
+ tm.logStore.AddConsoleLog(rawCDPID, LogEntry{
+ Timestamp: ts,
+ Level: string(ev.Type),
+ Message: msg,
+ Source: source,
+ })
+
+ case *runtime.EventExceptionThrown:
+ msg := ev.ExceptionDetails.Text
+ if ev.ExceptionDetails.Exception != nil && ev.ExceptionDetails.Exception.Description != "" {
+ msg += ": " + ev.ExceptionDetails.Exception.Description
+ }
+
+ var ts time.Time
+ if ev.Timestamp != nil {
+ ts = time.Time(*ev.Timestamp)
+ } else {
+ ts = time.Now()
+ }
+
+ source := exceptionSource(ev.ExceptionDetails)
+ if source == "" {
+ execContextMu.RLock()
+ source = execContextSources[ev.ExceptionDetails.ExecutionContextID]
+ execContextMu.RUnlock()
+ }
+ if isInternalConsoleSource(source) {
+ return
+ }
+
+ stack := ""
+ if ev.ExceptionDetails.Exception != nil {
+ stack = ev.ExceptionDetails.Exception.Description
+ }
+
+ tm.logStore.AddErrorLog(rawCDPID, ErrorEntry{
+ Timestamp: ts,
+ Message: msg,
+ URL: ev.ExceptionDetails.URL,
+ Line: ev.ExceptionDetails.LineNumber,
+ Column: ev.ExceptionDetails.ColumnNumber,
+ Stack: stack,
+ })
+ }
+ })
+
+ go func() {
+ _ = chromedp.Run(ctx, chromedp.ActionFunc(func(c context.Context) error {
+ return runtime.Enable().Do(c)
+ }))
+ }()
+}
+
+func (tm *TabManager) shouldEagerlyCaptureConsole() bool {
+ if tm == nil || tm.config == nil {
+ return true
+ }
+ return !strings.EqualFold(strings.TrimSpace(tm.config.StealthLevel), "full")
+}
+
+func (tm *TabManager) EnsureConsoleCapture(tabID string) {
+ if tm == nil || tm.logStore == nil {
+ return
+ }
+
+ tm.mu.Lock()
+ entry := tm.tabs[tabID]
+ if entry == nil && tabID == "" {
+ entry = tm.tabs[tm.currentTab]
+ }
+ if entry == nil || entry.Ctx == nil || entry.ConsoleCaptureEnabled {
+ tm.mu.Unlock()
+ return
+ }
+ entry.ConsoleCaptureEnabled = true
+ ctx := entry.Ctx
+ rawCDPID := entry.CDPID
+ tm.mu.Unlock()
+
+ tm.setupConsoleCapture(ctx, rawCDPID)
+}
+
+func executionContextSource(ctx *runtime.ExecutionContextDescription) string {
+ if ctx == nil {
+ return ""
+ }
+ if source := strings.TrimSpace(ctx.Origin); source != "" {
+ return source
+ }
+ return strings.TrimSpace(ctx.Name)
+}
+
+func exceptionSource(details *runtime.ExceptionDetails) string {
+ if details == nil {
+ return ""
+ }
+ if source := strings.TrimSpace(details.URL); source != "" {
+ return source
+ }
+ return stackTraceSource(details.StackTrace)
+}
+
+func stackTraceSource(trace *runtime.StackTrace) string {
+ for trace != nil {
+ for _, frame := range trace.CallFrames {
+ if frame == nil {
+ continue
+ }
+ if source := strings.TrimSpace(frame.URL); source != "" {
+ return source
+ }
+ }
+ trace = trace.Parent
+ }
+ return ""
+}
+
+func isInternalConsoleSource(source string) bool {
+ source = strings.TrimSpace(source)
+ if source == "" {
+ return false
+ }
+
+ lower := strings.ToLower(source)
+ switch {
+ case strings.HasPrefix(lower, "chrome-extension://"),
+ strings.HasPrefix(lower, "edge-extension://"),
+ strings.HasPrefix(lower, "moz-extension://"),
+ strings.HasPrefix(lower, "safari-extension://"),
+ strings.HasPrefix(lower, "devtools://"),
+ strings.HasPrefix(lower, "chrome://"),
+ strings.HasPrefix(lower, "about:"):
+ return true
+ }
+
+ parsed, err := url.Parse(source)
+ if err != nil {
+ return false
+ }
+ switch strings.ToLower(parsed.Scheme) {
+ case "chrome-extension", "edge-extension", "moz-extension", "safari-extension", "devtools", "chrome", "about":
+ return true
+ default:
+ return false
+ }
+}
diff --git a/internal/bridge/tab_cleanup.go b/internal/bridge/tab_cleanup.go
new file mode 100644
index 00000000..03fd4f83
--- /dev/null
+++ b/internal/bridge/tab_cleanup.go
@@ -0,0 +1,177 @@
+package bridge
+
+import (
+ "context"
+ "fmt"
+ "log/slog"
+ "time"
+)
+
+// closeOldestTab evicts the tab with the earliest CreatedAt timestamp.
+func (tm *TabManager) closeOldestTab() error {
+ tm.mu.RLock()
+ var oldestID string
+ var oldestTime time.Time
+ for id, entry := range tm.tabs {
+ if oldestID == "" || entry.CreatedAt.Before(oldestTime) {
+ oldestID = id
+ oldestTime = entry.CreatedAt
+ }
+ }
+ tm.mu.RUnlock()
+
+ if oldestID == "" {
+ return fmt.Errorf("no tabs to evict")
+ }
+ slog.Info("evicting oldest tab", "id", oldestID, "createdAt", oldestTime)
+ return tm.CloseTab(oldestID)
+}
+
+// closeLRUTab evicts the tab with the earliest LastUsed timestamp.
+func (tm *TabManager) closeLRUTab() error {
+ tm.mu.RLock()
+ var lruID string
+ var lruTime time.Time
+ for id, entry := range tm.tabs {
+ t := entry.LastUsed
+ if t.IsZero() {
+ t = entry.CreatedAt
+ }
+ if lruID == "" || t.Before(lruTime) {
+ lruID = id
+ lruTime = t
+ }
+ }
+ tm.mu.RUnlock()
+
+ if lruID == "" {
+ return fmt.Errorf("no tabs to evict")
+ }
+ slog.Info("evicting LRU tab", "id", lruID, "lastUsed", lruTime)
+ return tm.CloseTab(lruID)
+}
+
+func (tm *TabManager) CleanStaleTabs(ctx context.Context, interval time.Duration) {
+ ticker := time.NewTicker(interval)
+ defer ticker.Stop()
+
+ for {
+ select {
+ case <-ctx.Done():
+ return
+ case <-ticker.C:
+ }
+
+ targets, err := tm.ListTargets()
+ if err != nil {
+ continue
+ }
+
+ alive := make(map[string]bool, len(targets))
+ for _, t := range targets {
+ alive[string(t.TargetID)] = true
+ }
+
+ type staleTab struct {
+ tabID string
+ cdpID string
+ }
+ var staleTabs []staleTab
+ tm.mu.RLock()
+ for id, entry := range tm.tabs {
+ if !alive[id] {
+ cdpID := entry.CDPID
+ if cdpID == "" {
+ cdpID = id
+ }
+ staleTabs = append(staleTabs, staleTab{tabID: id, cdpID: cdpID})
+ }
+ }
+ tm.mu.RUnlock()
+
+ for _, stale := range staleTabs {
+ tm.purgeTrackedTabState(stale.tabID, stale.cdpID)
+ slog.Info("cleaned stale tab", "id", stale.tabID)
+ }
+ }
+}
+
+func (tm *TabManager) purgeTrackedTabState(tabID, cdpTargetID string) bool {
+ resolvedTabID, resolvedCDPID, cancel, ok := tm.lookupTrackedTabForCleanup(tabID, cdpTargetID)
+ if !ok {
+ return false
+ }
+ if cancel != nil {
+ cancel()
+ }
+
+ tm.mu.Lock()
+ if entry, ok := tm.tabs[resolvedTabID]; ok && entry.autoCloseTimer != nil {
+ entry.autoCloseTimer.Stop()
+ entry.autoCloseTimer = nil
+ entry.autoCloseGen++
+ }
+ delete(tm.tabs, resolvedTabID)
+ delete(tm.snapshots, resolvedTabID)
+ delete(tm.frameScope, resolvedTabID)
+ delete(tm.accessed, resolvedTabID)
+ if tm.currentTab == resolvedTabID {
+ tm.currentTab = ""
+ }
+ tm.mu.Unlock()
+
+ if tm.dialogMgr != nil {
+ tm.dialogMgr.ClearPending(resolvedTabID)
+ }
+ if tm.executor != nil {
+ tm.executor.RemoveTab(resolvedTabID)
+ }
+ if tm.logStore != nil {
+ tm.logStore.RemoveTab(resolvedCDPID)
+ }
+ // Notify listeners (e.g. session persistence) that a tab disappeared,
+ // regardless of whether the trigger was a deliberate CloseTab, an eviction,
+ // the auto-close lifecycle timer, or Chrome reporting the target gone
+ // (CleanStaleTabs / user closing the tab in headed Chrome directly).
+ if tm.onAfterClose != nil {
+ tm.onAfterClose()
+ }
+ return true
+}
+
+func (tm *TabManager) lookupTrackedTabForCleanup(tabID, cdpTargetID string) (string, string, context.CancelFunc, bool) {
+ tm.mu.RLock()
+ defer tm.mu.RUnlock()
+
+ if tabID != "" {
+ if entry, ok := tm.tabs[tabID]; ok {
+ resolvedCDPID := cdpTargetID
+ if resolvedCDPID == "" {
+ resolvedCDPID = entry.CDPID
+ }
+ if resolvedCDPID == "" {
+ resolvedCDPID = tabID
+ }
+ return tabID, resolvedCDPID, entry.Cancel, true
+ }
+ }
+
+ if cdpTargetID == "" {
+ return "", "", nil, false
+ }
+
+ for id, entry := range tm.tabs {
+ resolvedCDPID := entry.CDPID
+ if resolvedCDPID == "" {
+ resolvedCDPID = id
+ }
+ if id == cdpTargetID || resolvedCDPID == cdpTargetID {
+ return id, resolvedCDPID, entry.Cancel, true
+ }
+ }
+ return "", "", nil, false
+}
+
+func (tm *TabManager) purgeTrackedTabStateByTargetID(cdpTargetID string) bool {
+ return tm.purgeTrackedTabState("", cdpTargetID)
+}
diff --git a/internal/bridge/tab_lookup.go b/internal/bridge/tab_lookup.go
new file mode 100644
index 00000000..839e0fe8
--- /dev/null
+++ b/internal/bridge/tab_lookup.go
@@ -0,0 +1,216 @@
+package bridge
+
+import (
+ "context"
+ "fmt"
+ "log/slog"
+ "time"
+
+ "github.com/chromedp/cdproto/target"
+ "github.com/chromedp/chromedp"
+)
+
+func (tm *TabManager) markAccessed(tabID string) {
+ tm.mu.Lock()
+ tm.accessed[tabID] = true
+ if entry, ok := tm.tabs[tabID]; ok {
+ entry.LastUsed = time.Now()
+ }
+ tm.currentTab = tabID
+ tm.mu.Unlock()
+}
+
+// selectCurrentTrackedTab returns the current tab ID, falling back to the most
+// recently used tab if the explicit pointer is stale or unset.
+func (tm *TabManager) selectCurrentTrackedTab() string {
+ if tm.currentTab != "" {
+ if _, ok := tm.tabs[tm.currentTab]; ok {
+ return tm.currentTab
+ }
+ }
+
+ var best string
+ var bestTime time.Time
+ for id, entry := range tm.tabs {
+ if entry.LastUsed.After(bestTime) {
+ best = id
+ bestTime = entry.LastUsed
+ }
+ }
+ if best == "" {
+ for id, entry := range tm.tabs {
+ if entry.CreatedAt.After(bestTime) {
+ best = id
+ bestTime = entry.CreatedAt
+ }
+ }
+ }
+ return best
+}
+
+// AccessedTabIDs returns the set of tab IDs that were accessed this session.
+func (tm *TabManager) AccessedTabIDs() map[string]bool {
+ tm.mu.RLock()
+ defer tm.mu.RUnlock()
+ out := make(map[string]bool, len(tm.accessed))
+ for k := range tm.accessed {
+ out[k] = true
+ }
+ return out
+}
+
+func (tm *TabManager) TabContext(tabID string) (context.Context, string, error) {
+ if tm == nil {
+ return nil, "", fmt.Errorf("tab manager not initialized")
+ }
+ if tabID == "" {
+ tm.mu.RLock()
+ tabID = tm.selectCurrentTrackedTab()
+ tm.mu.RUnlock()
+
+ if tabID == "" {
+ targets, err := tm.ListTargets()
+ if err != nil {
+ return nil, "", fmt.Errorf("list targets: %w", err)
+ }
+ if len(targets) == 0 {
+ return nil, "", fmt.Errorf("no tabs open")
+ }
+ rawID := string(targets[0].TargetID)
+ tabID = tm.idMgr.TabIDFromCDPTarget(rawID)
+ }
+ }
+
+ tm.mu.RLock()
+ entry, ok := tm.tabs[tabID]
+ tm.mu.RUnlock()
+
+ if !ok {
+ targets, err := tm.ListTargets()
+ if err == nil {
+ for _, t := range targets {
+ raw := string(t.TargetID)
+ if tm.idMgr.TabIDFromCDPTarget(raw) == tabID {
+ ctx, cancel := chromedp.NewContext(tm.browserCtx, chromedp.WithTargetID(target.ID(raw)))
+ if tm.onTabSetup != nil {
+ tm.onTabSetup(ctx)
+ }
+ if tm.netMonitor != nil {
+ if err := tm.netMonitor.StartCapture(ctx, tabID); err != nil {
+ slog.Warn("eager network capture failed", "tab", tabID, "err", err)
+ }
+ }
+ if tm.dialogMgr != nil {
+ autoAccept := tm.config != nil && tm.config.DialogAutoAccept
+ ListenDialogEvents(ctx, tabID, tm.dialogMgr, autoAccept)
+ if err := EnableDialogEvents(ctx); err != nil {
+ slog.Warn("enable dialog events failed", "tabId", tabID, "err", err)
+ }
+ }
+ tm.RegisterTabWithCancel(tabID, raw, ctx, cancel)
+
+ tm.mu.RLock()
+ entry = tm.tabs[tabID]
+ tm.mu.RUnlock()
+ ok = true
+ break
+ }
+ }
+ }
+ }
+
+ if !ok {
+ return nil, "", fmt.Errorf("tab %s not found", tabID)
+ }
+
+ if entry.Ctx == nil {
+ return nil, "", fmt.Errorf("tab %s has no active context", tabID)
+ }
+
+ tm.markAccessed(tabID)
+
+ return entry.Ctx, tabID, nil
+}
+
+// ResolveTabByIndex resolves a 1-based tab index to a tab ID.
+func (tm *TabManager) ResolveTabByIndex(index int) (string, string, string, error) {
+ targets, err := tm.ListTargets()
+ if err != nil {
+ return "", "", "", err
+ }
+ if index < 1 || index > len(targets) {
+ return "", "", "", fmt.Errorf("tab index %d out of range (1-%d)", index, len(targets))
+ }
+ t := targets[index-1]
+ tabID := tm.idMgr.TabIDFromCDPTarget(string(t.TargetID))
+ return tabID, t.URL, t.Title, nil
+}
+
+func (tm *TabManager) ListTargets() ([]*target.Info, error) {
+ if tm == nil {
+ return nil, fmt.Errorf("tab manager not initialized")
+ }
+ if tm.browserCtx == nil {
+ return nil, fmt.Errorf("no browser connection")
+ }
+
+ execCtx, err := browserExecutorContext(tm.browserCtx)
+ if err != nil {
+ return nil, fmt.Errorf("browser executor: %w", err)
+ }
+
+ ctx, cancel := context.WithTimeout(execCtx, 5*time.Second)
+ defer cancel()
+
+ targets, err := target.GetTargets().Do(ctx)
+ if err != nil {
+ return nil, fmt.Errorf("get targets: %w", err)
+ }
+
+ pages := make([]*target.Info, 0, len(targets))
+ for _, t := range targets {
+ if t.Type == TargetTypePage {
+ pages = append(pages, t)
+ }
+ }
+ return pages, nil
+}
+
+// ListTargetsWithContext is like ListTargets but uses a custom context for timeout control.
+func (tm *TabManager) ListTargetsWithContext(ctx context.Context) ([]*target.Info, error) {
+ if tm == nil {
+ return nil, fmt.Errorf("tab manager not initialized")
+ }
+ if tm.browserCtx == nil {
+ return nil, fmt.Errorf("no browser connection")
+ }
+
+ execCtx, err := browserExecutorContext(tm.browserCtx)
+ if err != nil {
+ return nil, fmt.Errorf("browser executor: %w", err)
+ }
+
+ mergedCtx, cancel := context.WithCancel(execCtx)
+ defer cancel()
+
+ go func() {
+ select {
+ case <-ctx.Done():
+ cancel()
+ case <-mergedCtx.Done():
+ }
+ }()
+
+ targets, err := target.GetTargets().Do(mergedCtx)
+ if err != nil {
+ return nil, fmt.Errorf("get targets: %w", err)
+ }
+
+ pages := make([]*target.Info, 0, len(targets))
+ for _, t := range targets {
+ if t.Type == TargetTypePage {
+ pages = append(pages, t)
+ }
+ }
+ return pages, nil
+}
diff --git a/internal/bridge/tab_manager.go b/internal/bridge/tab_manager.go
index 85d67d4a..811c0a1c 100644
--- a/internal/bridge/tab_manager.go
+++ b/internal/bridge/tab_manager.go
@@ -4,19 +4,15 @@ import (
"context"
"fmt"
"log/slog"
- "net/url"
- "strings"
"sync"
"time"
cdp "github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/page"
- "github.com/chromedp/cdproto/runtime"
"github.com/chromedp/cdproto/target"
"github.com/chromedp/chromedp"
"github.com/pinchtab/pinchtab/internal/config"
"github.com/pinchtab/pinchtab/internal/ids"
- internalurls "github.com/pinchtab/pinchtab/internal/urls"
)
type TabSetupFunc func(ctx context.Context)
@@ -81,68 +77,9 @@ func (tm *TabManager) SetNetworkMonitor(nm *NetworkMonitor) {
tm.netMonitor = nm
}
-func shouldBlockPopupTarget(info *target.Info) bool {
- return info != nil && info.Type == TargetTypePage && info.OpenerID != ""
-}
-
-func (tm *TabManager) StartBrowserGuards() {
- if tm == nil || tm.browserCtx == nil {
- return
- }
-
- tm.guardOnce.Do(func() {
- if err := chromedp.Run(tm.browserCtx, chromedp.ActionFunc(func(ctx context.Context) error {
- c := chromedp.FromContext(ctx)
- if c == nil || c.Browser == nil {
- return fmt.Errorf("no browser executor")
- }
- return target.SetDiscoverTargets(true).Do(cdp.WithExecutor(ctx, c.Browser))
- })); err != nil {
- slog.Warn("browser popup guard unavailable", "err", err)
- return
- }
-
- chromedp.ListenBrowser(tm.browserCtx, func(ev any) {
- created, ok := ev.(*target.EventTargetCreated)
- if !ok || !shouldBlockPopupTarget(created.TargetInfo) {
- return
- }
-
- info := created.TargetInfo
- go tm.closePopupTarget(info.TargetID, info.OpenerID, info.URL)
- })
- })
-}
-
-func (tm *TabManager) closePopupTarget(targetID, openerID target.ID, url string) {
- closeCtx, cancel := context.WithTimeout(tm.browserCtx, 5*time.Second)
- defer cancel()
- logURL := internalurls.RedactForLog(url)
-
- execCtx, err := browserExecutorContext(closeCtx)
- if err != nil {
- slog.Debug("popup close skipped", "targetId", targetID, "openerId", openerID, "url", logURL, "err", err)
- return
- }
-
- if err := target.CloseTarget(targetID).Do(execCtx); err != nil {
- slog.Debug("popup close failed", "targetId", targetID, "openerId", openerID, "url", logURL, "err", err)
- return
- }
-
- slog.Info("blocked popup target", "targetId", targetID, "openerId", openerID, "url", logURL)
-}
-
-func (tm *TabManager) markAccessed(tabID string) {
- tm.mu.Lock()
- tm.accessed[tabID] = true
- if entry, ok := tm.tabs[tabID]; ok {
- entry.LastUsed = time.Now()
- }
- tm.currentTab = tabID
- tm.mu.Unlock()
-}
-
+// browserExecutorContext returns a context bound to the top-level browser
+// executor, suitable for issuing browser-scoped CDP calls (e.g. target.*).
+// Shared helper used by tab lifecycle, lookup, popup-guard, and cleanup paths.
func browserExecutorContext(ctx context.Context) (context.Context, error) {
if ctx == nil {
return nil, fmt.Errorf("no browser context available")
@@ -154,169 +91,6 @@ func browserExecutorContext(ctx context.Context) (context.Context, error) {
return cdp.WithExecutor(ctx, c.Browser), nil
}
-// selectCurrentTrackedTab returns the current tab ID, falling back to the most
-// recently used tab if the explicit pointer is stale or unset.
-func (tm *TabManager) selectCurrentTrackedTab() string {
- // Prefer explicit current tab if still tracked
- if tm.currentTab != "" {
- if _, ok := tm.tabs[tm.currentTab]; ok {
- return tm.currentTab
- }
- }
-
- // Fallback: pick the tab with the most recent LastUsed
- var best string
- var bestTime time.Time
- for id, entry := range tm.tabs {
- if entry.LastUsed.After(bestTime) {
- best = id
- bestTime = entry.LastUsed
- }
- }
- // If no LastUsed set, fall back to most recent CreatedAt
- if best == "" {
- for id, entry := range tm.tabs {
- if entry.CreatedAt.After(bestTime) {
- best = id
- bestTime = entry.CreatedAt
- }
- }
- }
- return best
-}
-
-// AccessedTabIDs returns the set of tab IDs that were accessed this session.
-func (tm *TabManager) AccessedTabIDs() map[string]bool {
- tm.mu.RLock()
- defer tm.mu.RUnlock()
- out := make(map[string]bool, len(tm.accessed))
- for k := range tm.accessed {
- out[k] = true
- }
- return out
-}
-
-func (tm *TabManager) TabContext(tabID string) (context.Context, string, error) {
- if tm == nil {
- return nil, "", fmt.Errorf("tab manager not initialized")
- }
- if tabID == "" {
- // Resolve to current tracked tab
- tm.mu.RLock()
- tabID = tm.selectCurrentTrackedTab()
- tm.mu.RUnlock()
-
- if tabID == "" {
- // No tracked tabs — try to find one from CDP targets
- targets, err := tm.ListTargets()
- if err != nil {
- return nil, "", fmt.Errorf("list targets: %w", err)
- }
- if len(targets) == 0 {
- return nil, "", fmt.Errorf("no tabs open")
- }
- rawID := string(targets[0].TargetID)
- tabID = tm.idMgr.TabIDFromCDPTarget(rawID)
- }
- }
-
- tm.mu.RLock()
- entry, ok := tm.tabs[tabID]
- tm.mu.RUnlock()
-
- if !ok {
- // Attempt to auto-track the tab if it's open but untracked
- targets, err := tm.ListTargets()
- if err == nil {
- for _, t := range targets {
- raw := string(t.TargetID)
- if tm.idMgr.TabIDFromCDPTarget(raw) == tabID {
- // Initialize context and register it
- ctx, cancel := chromedp.NewContext(tm.browserCtx, chromedp.WithTargetID(target.ID(raw)))
- if tm.onTabSetup != nil {
- tm.onTabSetup(ctx)
- }
- if tm.netMonitor != nil {
- if err := tm.netMonitor.StartCapture(ctx, tabID); err != nil {
- slog.Warn("eager network capture failed", "tab", tabID, "err", err)
- }
- }
- if tm.dialogMgr != nil {
- autoAccept := tm.config != nil && tm.config.DialogAutoAccept
- ListenDialogEvents(ctx, tabID, tm.dialogMgr, autoAccept)
- if err := EnableDialogEvents(ctx); err != nil {
- slog.Warn("enable dialog events failed", "tabId", tabID, "err", err)
- }
- }
- tm.RegisterTabWithCancel(tabID, raw, ctx, cancel)
-
- tm.mu.RLock()
- entry = tm.tabs[tabID]
- tm.mu.RUnlock()
- ok = true
- break
- }
- }
- }
- }
-
- if !ok {
- return nil, "", fmt.Errorf("tab %s not found", tabID)
- }
-
- if entry.Ctx == nil {
- return nil, "", fmt.Errorf("tab %s has no active context", tabID)
- }
-
- tm.markAccessed(tabID)
-
- return entry.Ctx, tabID, nil
-}
-
-// closeOldestTab evicts the tab with the earliest CreatedAt timestamp.
-func (tm *TabManager) closeOldestTab() error {
- tm.mu.RLock()
- var oldestID string
- var oldestTime time.Time
- for id, entry := range tm.tabs {
- if oldestID == "" || entry.CreatedAt.Before(oldestTime) {
- oldestID = id
- oldestTime = entry.CreatedAt
- }
- }
- tm.mu.RUnlock()
-
- if oldestID == "" {
- return fmt.Errorf("no tabs to evict")
- }
- slog.Info("evicting oldest tab", "id", oldestID, "createdAt", oldestTime)
- return tm.CloseTab(oldestID)
-}
-
-// closeLRUTab evicts the tab with the earliest LastUsed timestamp.
-func (tm *TabManager) closeLRUTab() error {
- tm.mu.RLock()
- var lruID string
- var lruTime time.Time
- for id, entry := range tm.tabs {
- t := entry.LastUsed
- if t.IsZero() {
- t = entry.CreatedAt
- }
- if lruID == "" || t.Before(lruTime) {
- lruID = id
- lruTime = t
- }
- }
- tm.mu.RUnlock()
-
- if lruID == "" {
- return fmt.Errorf("no tabs to evict")
- }
- slog.Info("evicting LRU tab", "id", lruID, "lastUsed", lruTime)
- return tm.CloseTab(lruID)
-}
-
func (tm *TabManager) CreateTab(url string) (string, context.Context, context.CancelFunc, error) {
if tm == nil {
return "", nil, nil, fmt.Errorf("tab manager not initialized")
@@ -365,7 +139,6 @@ func (tm *TabManager) CreateTab(url string) (string, context.Context, context.Ca
}
createCancel()
- // Create a context for the new tab
ctx, cancel := chromedp.NewContext(tm.browserCtx,
chromedp.WithTargetID(targetID),
)
@@ -415,7 +188,6 @@ func (tm *TabManager) CreateTab(url string) (string, context.Context, context.Ca
now := time.Now()
- // Set up dialog event listening for this tab
if tm.dialogMgr != nil {
autoAccept := tm.config != nil && tm.config.DialogAutoAccept
ListenDialogEvents(ctx, tabID, tm.dialogMgr, autoAccept)
@@ -469,7 +241,6 @@ func (tm *TabManager) CloseTab(tabID string) error {
entry.Cancel()
}
- // Resolve to raw CDP target ID for the actual CDP close call
cdpTargetID := tabID
if tracked && entry.CDPID != "" {
cdpTargetID = entry.CDPID
@@ -509,7 +280,6 @@ func (tm *TabManager) FocusTab(tabID string) error {
return err
}
- // Bring the tab to front via CDP
if err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {
return page.BringToFront().Do(ctx)
})); err != nil {
@@ -526,152 +296,6 @@ func (tm *TabManager) FocusTab(tabID string) error {
return nil
}
-// ResolveTabByIndex resolves a 1-based tab index to a tab ID.
-// Returns the tab ID and its URL/title for display.
-func (tm *TabManager) ResolveTabByIndex(index int) (string, string, string, error) {
- targets, err := tm.ListTargets()
- if err != nil {
- return "", "", "", err
- }
- if index < 1 || index > len(targets) {
- return "", "", "", fmt.Errorf("tab index %d out of range (1-%d)", index, len(targets))
- }
- t := targets[index-1]
- tabID := tm.idMgr.TabIDFromCDPTarget(string(t.TargetID))
- return tabID, t.URL, t.Title, nil
-}
-
-func (tm *TabManager) ListTargets() ([]*target.Info, error) {
- if tm == nil {
- return nil, fmt.Errorf("tab manager not initialized")
- }
- if tm.browserCtx == nil {
- return nil, fmt.Errorf("no browser connection")
- }
-
- execCtx, err := browserExecutorContext(tm.browserCtx)
- if err != nil {
- return nil, fmt.Errorf("browser executor: %w", err)
- }
-
- ctx, cancel := context.WithTimeout(execCtx, 5*time.Second)
- defer cancel()
-
- targets, err := target.GetTargets().Do(ctx)
- if err != nil {
- return nil, fmt.Errorf("get targets: %w", err)
- }
-
- pages := make([]*target.Info, 0, len(targets))
- for _, t := range targets {
- if t.Type == TargetTypePage {
- pages = append(pages, t)
- }
- }
- return pages, nil
-}
-
-// ListTargetsWithContext is like ListTargets but uses a custom context for timeout control.
-func (tm *TabManager) ListTargetsWithContext(ctx context.Context) ([]*target.Info, error) {
- if tm == nil {
- return nil, fmt.Errorf("tab manager not initialized")
- }
- if tm.browserCtx == nil {
- return nil, fmt.Errorf("no browser connection")
- }
-
- execCtx, err := browserExecutorContext(tm.browserCtx)
- if err != nil {
- return nil, fmt.Errorf("browser executor: %w", err)
- }
-
- mergedCtx, cancel := context.WithCancel(execCtx)
- defer cancel()
-
- go func() {
- select {
- case <-ctx.Done():
- cancel()
- case <-mergedCtx.Done():
- }
- }()
-
- targets, err := target.GetTargets().Do(mergedCtx)
- if err != nil {
- return nil, fmt.Errorf("get targets: %w", err)
- }
-
- pages := make([]*target.Info, 0, len(targets))
- for _, t := range targets {
- if t.Type == TargetTypePage {
- pages = append(pages, t)
- }
- }
- return pages, nil
-}
-
-func (tm *TabManager) GetRefCache(tabID string) *RefCache {
- tm.mu.RLock()
- defer tm.mu.RUnlock()
- return tm.snapshots[tabID]
-}
-
-func (tm *TabManager) SetRefCache(tabID string, cache *RefCache) {
- tm.mu.Lock()
- defer tm.mu.Unlock()
- tm.snapshots[tabID] = cache
-}
-
-func (tm *TabManager) DeleteRefCache(tabID string) {
- tm.mu.Lock()
- defer tm.mu.Unlock()
- delete(tm.snapshots, tabID)
-}
-
-func (tm *TabManager) GetFrameScope(tabID string) (FrameScope, bool) {
- tm.mu.RLock()
- defer tm.mu.RUnlock()
- scope, ok := tm.frameScope[tabID]
- return scope, ok && scope.Active()
-}
-
-func (tm *TabManager) SetFrameScope(tabID string, scope FrameScope) {
- tm.mu.Lock()
- defer tm.mu.Unlock()
- if !scope.Active() {
- delete(tm.frameScope, tabID)
- return
- }
- tm.frameScope[tabID] = scope
-}
-
-func (tm *TabManager) ClearFrameScope(tabID string) {
- tm.mu.Lock()
- defer tm.mu.Unlock()
- delete(tm.frameScope, tabID)
-}
-
-func (tm *TabManager) RegisterTab(tabID string, ctx context.Context) {
- now := time.Now()
- tm.mu.Lock()
- tm.tabs[tabID] = &TabEntry{Ctx: ctx, CreatedAt: now, LastUsed: now}
- tm.currentTab = tabID
- tm.mu.Unlock()
-
- tm.startTabPolicyWatcher(tabID, ctx)
-}
-
-// RegisterTabWithCancel registers a tab ID with its context and cancel function.
-func (tm *TabManager) RegisterTabWithCancel(tabID, rawCDPID string, ctx context.Context, cancel context.CancelFunc) {
- now := time.Now()
- tm.mu.Lock()
- tm.tabs[tabID] = &TabEntry{Ctx: ctx, Cancel: cancel, CDPID: rawCDPID, CreatedAt: now, LastUsed: now}
- tm.currentTab = tabID
- tm.mu.Unlock()
-
- tm.startTabPolicyWatcher(tabID, ctx)
-}
-
// Execute runs a task for a tab through the TabExecutor, ensuring per-tab
// sequential execution with cross-tab parallelism bounded by the semaphore.
// If the TabExecutor has not been initialized, the task runs directly.
@@ -686,343 +310,3 @@ func (tm *TabManager) Execute(ctx context.Context, tabID string, task func(ctx c
func (tm *TabManager) Executor() *TabExecutor {
return tm.executor
}
-
-func (tm *TabManager) CleanStaleTabs(ctx context.Context, interval time.Duration) {
- ticker := time.NewTicker(interval)
- defer ticker.Stop()
-
- for {
- select {
- case <-ctx.Done():
- return
- case <-ticker.C:
- }
-
- targets, err := tm.ListTargets()
- if err != nil {
- continue
- }
-
- alive := make(map[string]bool, len(targets))
- for _, t := range targets {
- alive[string(t.TargetID)] = true
- }
-
- type staleTab struct {
- tabID string
- cdpID string
- }
- var staleTabs []staleTab
- tm.mu.RLock()
- for id, entry := range tm.tabs {
- if !alive[id] {
- cdpID := entry.CDPID
- if cdpID == "" {
- cdpID = id
- }
- staleTabs = append(staleTabs, staleTab{tabID: id, cdpID: cdpID})
- }
- }
- tm.mu.RUnlock()
-
- for _, stale := range staleTabs {
- tm.purgeTrackedTabState(stale.tabID, stale.cdpID)
- slog.Info("cleaned stale tab", "id", stale.tabID)
- }
- }
-}
-
-func (tm *TabManager) purgeTrackedTabState(tabID, cdpTargetID string) bool {
- resolvedTabID, resolvedCDPID, cancel, ok := tm.lookupTrackedTabForCleanup(tabID, cdpTargetID)
- if !ok {
- return false
- }
- if cancel != nil {
- cancel()
- }
-
- tm.mu.Lock()
- if entry, ok := tm.tabs[resolvedTabID]; ok && entry.autoCloseTimer != nil {
- entry.autoCloseTimer.Stop()
- entry.autoCloseTimer = nil
- entry.autoCloseGen++
- }
- delete(tm.tabs, resolvedTabID)
- delete(tm.snapshots, resolvedTabID)
- delete(tm.frameScope, resolvedTabID)
- delete(tm.accessed, resolvedTabID)
- if tm.currentTab == resolvedTabID {
- tm.currentTab = ""
- }
- tm.mu.Unlock()
-
- if tm.dialogMgr != nil {
- tm.dialogMgr.ClearPending(resolvedTabID)
- }
- if tm.executor != nil {
- tm.executor.RemoveTab(resolvedTabID)
- }
- if tm.logStore != nil {
- tm.logStore.RemoveTab(resolvedCDPID)
- }
- // Notify listeners (e.g. session persistence) that a tab disappeared,
- // regardless of whether the trigger was a deliberate CloseTab, an eviction,
- // the auto-close lifecycle timer, or Chrome reporting the target gone
- // (CleanStaleTabs / user closing the tab in headed Chrome directly).
- if tm.onAfterClose != nil {
- tm.onAfterClose()
- }
- return true
-}
-
-func (tm *TabManager) lookupTrackedTabForCleanup(tabID, cdpTargetID string) (string, string, context.CancelFunc, bool) {
- tm.mu.RLock()
- defer tm.mu.RUnlock()
-
- if tabID != "" {
- if entry, ok := tm.tabs[tabID]; ok {
- resolvedCDPID := cdpTargetID
- if resolvedCDPID == "" {
- resolvedCDPID = entry.CDPID
- }
- if resolvedCDPID == "" {
- resolvedCDPID = tabID
- }
- return tabID, resolvedCDPID, entry.Cancel, true
- }
- }
-
- if cdpTargetID == "" {
- return "", "", nil, false
- }
-
- for id, entry := range tm.tabs {
- resolvedCDPID := entry.CDPID
- if resolvedCDPID == "" {
- resolvedCDPID = id
- }
- if id == cdpTargetID || resolvedCDPID == cdpTargetID {
- return id, resolvedCDPID, entry.Cancel, true
- }
- }
- return "", "", nil, false
-}
-
-func (tm *TabManager) purgeTrackedTabStateByTargetID(cdpTargetID string) bool {
- return tm.purgeTrackedTabState("", cdpTargetID)
-}
-
-// setupConsoleCapture enables runtime domain and listens for console/exception events.
-func (tm *TabManager) setupConsoleCapture(ctx context.Context, rawCDPID string) {
- if tm.logStore == nil {
- return
- }
-
- execContextSources := make(map[runtime.ExecutionContextID]string)
- var execContextMu sync.RWMutex
-
- // Listen for console API calls and exceptions
- chromedp.ListenTarget(ctx, func(ev any) {
- switch ev := ev.(type) {
- case *runtime.EventExecutionContextCreated:
- if ev.Context == nil {
- return
- }
- execContextMu.Lock()
- execContextSources[ev.Context.ID] = executionContextSource(ev.Context)
- execContextMu.Unlock()
-
- case *runtime.EventExecutionContextDestroyed:
- execContextMu.Lock()
- delete(execContextSources, ev.ExecutionContextID)
- execContextMu.Unlock()
-
- case *runtime.EventExecutionContextsCleared:
- execContextMu.Lock()
- clear(execContextSources)
- execContextMu.Unlock()
-
- case *runtime.EventConsoleAPICalled:
- var msg string
- for _, arg := range ev.Args {
- if len(arg.Value) > 0 {
- // arg.Value is jsontext.Value ([]byte), use as string directly
- // Strip quotes if it's a JSON string
- val := string(arg.Value)
- if len(val) >= 2 && val[0] == '"' && val[len(val)-1] == '"' {
- val = val[1 : len(val)-1]
- }
- msg += val
- } else if arg.Description != "" {
- msg += arg.Description
- }
- msg += " "
- }
-
- var ts time.Time
- if ev.Timestamp != nil {
- ts = time.Time(*ev.Timestamp)
- } else {
- ts = time.Now()
- }
-
- source := stackTraceSource(ev.StackTrace)
- if source == "" {
- execContextMu.RLock()
- source = execContextSources[ev.ExecutionContextID]
- execContextMu.RUnlock()
- }
- if source == "" {
- source = strings.TrimSpace(ev.Context)
- }
- if isInternalConsoleSource(source) {
- return
- }
-
- tm.logStore.AddConsoleLog(rawCDPID, LogEntry{
- Timestamp: ts,
- Level: string(ev.Type),
- Message: msg,
- Source: source,
- })
-
- case *runtime.EventExceptionThrown:
- msg := ev.ExceptionDetails.Text
- if ev.ExceptionDetails.Exception != nil && ev.ExceptionDetails.Exception.Description != "" {
- msg += ": " + ev.ExceptionDetails.Exception.Description
- }
-
- var ts time.Time
- if ev.Timestamp != nil {
- ts = time.Time(*ev.Timestamp)
- } else {
- ts = time.Now()
- }
-
- source := exceptionSource(ev.ExceptionDetails)
- if source == "" {
- execContextMu.RLock()
- source = execContextSources[ev.ExceptionDetails.ExecutionContextID]
- execContextMu.RUnlock()
- }
- if isInternalConsoleSource(source) {
- return
- }
-
- stack := ""
- if ev.ExceptionDetails.Exception != nil {
- stack = ev.ExceptionDetails.Exception.Description
- }
-
- tm.logStore.AddErrorLog(rawCDPID, ErrorEntry{
- Timestamp: ts,
- Message: msg,
- URL: ev.ExceptionDetails.URL,
- Line: ev.ExceptionDetails.LineNumber,
- Column: ev.ExceptionDetails.ColumnNumber,
- Stack: stack,
- })
- }
- })
-
- // Enable runtime domain to receive console/exception events
- go func() {
- _ = chromedp.Run(ctx, chromedp.ActionFunc(func(c context.Context) error {
- return runtime.Enable().Do(c)
- }))
- }()
-}
-
-func (tm *TabManager) shouldEagerlyCaptureConsole() bool {
- if tm == nil || tm.config == nil {
- return true
- }
- return !strings.EqualFold(strings.TrimSpace(tm.config.StealthLevel), "full")
-}
-
-func (tm *TabManager) EnsureConsoleCapture(tabID string) {
- if tm == nil || tm.logStore == nil {
- return
- }
-
- tm.mu.Lock()
- entry := tm.tabs[tabID]
- if entry == nil && tabID == "" {
- entry = tm.tabs[tm.currentTab]
- }
- if entry == nil || entry.Ctx == nil || entry.ConsoleCaptureEnabled {
- tm.mu.Unlock()
- return
- }
- entry.ConsoleCaptureEnabled = true
- ctx := entry.Ctx
- rawCDPID := entry.CDPID
- tm.mu.Unlock()
-
- tm.setupConsoleCapture(ctx, rawCDPID)
-}
-
-func executionContextSource(ctx *runtime.ExecutionContextDescription) string {
- if ctx == nil {
- return ""
- }
- if source := strings.TrimSpace(ctx.Origin); source != "" {
- return source
- }
- return strings.TrimSpace(ctx.Name)
-}
-
-func exceptionSource(details *runtime.ExceptionDetails) string {
- if details == nil {
- return ""
- }
- if source := strings.TrimSpace(details.URL); source != "" {
- return source
- }
- return stackTraceSource(details.StackTrace)
-}
-
-func stackTraceSource(trace *runtime.StackTrace) string {
- for trace != nil {
- for _, frame := range trace.CallFrames {
- if frame == nil {
- continue
- }
- if source := strings.TrimSpace(frame.URL); source != "" {
- return source
- }
- }
- trace = trace.Parent
- }
- return ""
-}
-
-func isInternalConsoleSource(source string) bool {
- source = strings.TrimSpace(source)
- if source == "" {
- return false
- }
-
- lower := strings.ToLower(source)
- switch {
- case strings.HasPrefix(lower, "chrome-extension://"),
- strings.HasPrefix(lower, "edge-extension://"),
- strings.HasPrefix(lower, "moz-extension://"),
- strings.HasPrefix(lower, "safari-extension://"),
- strings.HasPrefix(lower, "devtools://"),
- strings.HasPrefix(lower, "chrome://"),
- strings.HasPrefix(lower, "about:"):
- return true
- }
-
- parsed, err := url.Parse(source)
- if err != nil {
- return false
- }
- switch strings.ToLower(parsed.Scheme) {
- case "chrome-extension", "edge-extension", "moz-extension", "safari-extension", "devtools", "chrome", "about":
- return true
- default:
- return false
- }
-}
diff --git a/internal/bridge/tab_popup_guard.go b/internal/bridge/tab_popup_guard.go
new file mode 100644
index 00000000..4b2ab5f0
--- /dev/null
+++ b/internal/bridge/tab_popup_guard.go
@@ -0,0 +1,65 @@
+package bridge
+
+import (
+ "context"
+ "fmt"
+ "log/slog"
+ "time"
+
+ cdp "github.com/chromedp/cdproto/cdp"
+ "github.com/chromedp/cdproto/target"
+ "github.com/chromedp/chromedp"
+ internalurls "github.com/pinchtab/pinchtab/internal/urls"
+)
+
+func shouldBlockPopupTarget(info *target.Info) bool {
+ return info != nil && info.Type == TargetTypePage && info.OpenerID != ""
+}
+
+func (tm *TabManager) StartBrowserGuards() {
+ if tm == nil || tm.browserCtx == nil {
+ return
+ }
+
+ tm.guardOnce.Do(func() {
+ if err := chromedp.Run(tm.browserCtx, chromedp.ActionFunc(func(ctx context.Context) error {
+ c := chromedp.FromContext(ctx)
+ if c == nil || c.Browser == nil {
+ return fmt.Errorf("no browser executor")
+ }
+ return target.SetDiscoverTargets(true).Do(cdp.WithExecutor(ctx, c.Browser))
+ })); err != nil {
+ slog.Warn("browser popup guard unavailable", "err", err)
+ return
+ }
+
+ chromedp.ListenBrowser(tm.browserCtx, func(ev any) {
+ created, ok := ev.(*target.EventTargetCreated)
+ if !ok || !shouldBlockPopupTarget(created.TargetInfo) {
+ return
+ }
+
+ info := created.TargetInfo
+ go tm.closePopupTarget(info.TargetID, info.OpenerID, info.URL)
+ })
+ })
+}
+
+func (tm *TabManager) closePopupTarget(targetID, openerID target.ID, url string) {
+ closeCtx, cancel := context.WithTimeout(tm.browserCtx, 5*time.Second)
+ defer cancel()
+ logURL := internalurls.RedactForLog(url)
+
+ execCtx, err := browserExecutorContext(closeCtx)
+ if err != nil {
+ slog.Debug("popup close skipped", "targetId", targetID, "openerId", openerID, "url", logURL, "err", err)
+ return
+ }
+
+ if err := target.CloseTarget(targetID).Do(execCtx); err != nil {
+ slog.Debug("popup close failed", "targetId", targetID, "openerId", openerID, "url", logURL, "err", err)
+ return
+ }
+
+ slog.Info("blocked popup target", "targetId", targetID, "openerId", openerID, "url", logURL)
+}
diff --git a/internal/bridge/tab_state.go b/internal/bridge/tab_state.go
new file mode 100644
index 00000000..0180dd46
--- /dev/null
+++ b/internal/bridge/tab_state.go
@@ -0,0 +1,68 @@
+package bridge
+
+import (
+ "context"
+ "time"
+)
+
+func (tm *TabManager) GetRefCache(tabID string) *RefCache {
+ tm.mu.RLock()
+ defer tm.mu.RUnlock()
+ return tm.snapshots[tabID]
+}
+
+func (tm *TabManager) SetRefCache(tabID string, cache *RefCache) {
+ tm.mu.Lock()
+ defer tm.mu.Unlock()
+ tm.snapshots[tabID] = cache
+}
+
+func (tm *TabManager) DeleteRefCache(tabID string) {
+ tm.mu.Lock()
+ defer tm.mu.Unlock()
+ delete(tm.snapshots, tabID)
+}
+
+func (tm *TabManager) GetFrameScope(tabID string) (FrameScope, bool) {
+ tm.mu.RLock()
+ defer tm.mu.RUnlock()
+ scope, ok := tm.frameScope[tabID]
+ return scope, ok && scope.Active()
+}
+
+func (tm *TabManager) SetFrameScope(tabID string, scope FrameScope) {
+ tm.mu.Lock()
+ defer tm.mu.Unlock()
+ if !scope.Active() {
+ delete(tm.frameScope, tabID)
+ return
+ }
+ tm.frameScope[tabID] = scope
+}
+
+func (tm *TabManager) ClearFrameScope(tabID string) {
+ tm.mu.Lock()
+ defer tm.mu.Unlock()
+ delete(tm.frameScope, tabID)
+}
+
+func (tm *TabManager) RegisterTab(tabID string, ctx context.Context) {
+ now := time.Now()
+ tm.mu.Lock()
+ tm.tabs[tabID] = &TabEntry{Ctx: ctx, CreatedAt: now, LastUsed: now}
+ tm.currentTab = tabID
+ tm.mu.Unlock()
+
+ tm.startTabPolicyWatcher(tabID, ctx)
+}
+
+// RegisterTabWithCancel registers a tab ID with its context and cancel function.
+func (tm *TabManager) RegisterTabWithCancel(tabID, rawCDPID string, ctx context.Context, cancel context.CancelFunc) {
+ now := time.Now()
+ tm.mu.Lock()
+ tm.tabs[tabID] = &TabEntry{Ctx: ctx, Cancel: cancel, CDPID: rawCDPID, CreatedAt: now, LastUsed: now}
+ tm.currentTab = tabID
+ tm.mu.Unlock()
+
+ tm.startTabPolicyWatcher(tabID, ctx)
+}
diff --git a/internal/config/config_file.go b/internal/config/config_file.go
index 6a07fa34..5351726d 100644
--- a/internal/config/config_file.go
+++ b/internal/config/config_file.go
@@ -1,10 +1,7 @@
package config
import (
- "encoding/json"
"path/filepath"
- "strings"
- "time"
)
// CurrentConfigVersion is bumped when config schema changes require migration or wizard re-run.
@@ -169,759 +166,3 @@ func DefaultFileConfig() FileConfig {
},
}
}
-
-type fileConfigJSON struct {
- ConfigVersion string `json:"configVersion,omitempty"`
- Server serverConfigJSON `json:"server"`
- Browser browserConfigJSON `json:"browser"`
- InstanceDefaults instanceDefaultsConfigJSON `json:"instanceDefaults"`
- Security securityConfigJSON `json:"security"`
- Profiles profilesConfigJSON `json:"profiles"`
- MultiInstance multiInstanceConfigJSON `json:"multiInstance"`
- Timeouts timeoutsConfigJSON `json:"timeouts"`
- Scheduler schedulerFileConfigJSON `json:"scheduler"`
- Observability observabilityFileConfigJSON `json:"observability"`
- Sessions sessionsFileConfigJSON `json:"sessions"`
- AutoSolver autoSolverFileConfigJSON `json:"autoSolver,omitempty"`
-}
-
-type serverConfigJSON struct {
- Port string `json:"port"`
- Bind string `json:"bind"`
- Token string `json:"token"`
- StateDir string `json:"stateDir"`
- Engine string `json:"engine"`
- NetworkBufferSize *int `json:"networkBufferSize,omitempty"`
- TrustProxyHeaders *bool `json:"trustProxyHeaders,omitempty"`
- CookieSecure *bool `json:"cookieSecure,omitempty"`
-}
-
-type browserConfigJSON struct {
- ChromeVersion string `json:"version"`
- ChromeBinary string `json:"binary"`
- ChromeDebugPort *int `json:"remoteDebuggingPort,omitempty"`
- ChromeExtraFlags string `json:"extraFlags"`
- ExtensionPaths []string `json:"extensionPaths"`
-}
-
-type instanceDefaultsConfigJSON struct {
- Mode string `json:"mode"`
- NoRestore *bool `json:"noRestore"`
- Timezone string `json:"timezone"`
- BlockImages *bool `json:"blockImages"`
- BlockMedia *bool `json:"blockMedia"`
- BlockAds *bool `json:"blockAds"`
- MaxTabs *int `json:"maxTabs"`
- MaxParallelTabs *int `json:"maxParallelTabs"`
- UserAgent string `json:"userAgent"`
- NoAnimations *bool `json:"noAnimations"`
- StealthLevel string `json:"stealthLevel"`
- TabEvictionPolicy string `json:"tabEvictionPolicy"`
- TabPolicy *TabPolicyDefaults `json:"tabPolicy,omitempty"`
-}
-
-type profilesConfigJSON struct {
- BaseDir string `json:"baseDir"`
- DefaultProfile string `json:"defaultProfile"`
-}
-
-type securityConfigJSON struct {
- AllowEvaluate *bool `json:"allowEvaluate"`
- AllowMacro *bool `json:"allowMacro"`
- AllowScreencast *bool `json:"allowScreencast"`
- AllowDownload *bool `json:"allowDownload"`
- AllowedDomains []string `json:"allowedDomains"`
- DownloadAllowedDomains []string `json:"downloadAllowedDomains"`
- DownloadMaxBytes *int `json:"downloadMaxBytes"`
- AllowUpload *bool `json:"allowUpload"`
- AllowClipboard *bool `json:"allowClipboard"`
- AllowStateExport *bool `json:"allowStateExport"`
- StateEncryptionKey *string `json:"stateEncryptionKey"`
- EnableActionGuards *bool `json:"enableActionGuards"`
- UploadMaxRequestBytes *int `json:"uploadMaxRequestBytes"`
- UploadMaxFiles *int `json:"uploadMaxFiles"`
- UploadMaxFileBytes *int `json:"uploadMaxFileBytes"`
- UploadMaxTotalBytes *int `json:"uploadMaxTotalBytes"`
- MaxRedirects *int `json:"maxRedirects"`
- TrustedProxyCIDRs []string `json:"trustedProxyCIDRs"`
- TrustedResolveCIDRs []string `json:"trustedResolveCIDRs"`
- Attach attachJSON `json:"attach"`
- IDPI idpiConfigJSON `json:"idpi"`
-}
-
-type attachJSON struct {
- Enabled *bool `json:"enabled"`
- AllowHosts []string `json:"allowHosts"`
- AllowSchemes []string `json:"allowSchemes"`
-}
-
-type idpiConfigJSON struct {
- Enabled bool `json:"enabled"`
- StrictMode bool `json:"strictMode"`
- ScanContent bool `json:"scanContent"`
- WrapContent bool `json:"wrapContent"`
- CustomPatterns []string `json:"customPatterns"`
- ScanTimeoutSec int `json:"scanTimeoutSec"`
- ShieldThreshold int `json:"shieldThreshold"`
-}
-
-type multiInstanceConfigJSON struct {
- Strategy string `json:"strategy"`
- AllocationPolicy string `json:"allocationPolicy"`
- InstancePortStart *int `json:"instancePortStart"`
- InstancePortEnd *int `json:"instancePortEnd"`
- Restart multiInstanceRestartJSON `json:"restart"`
-}
-
-type multiInstanceRestartJSON struct {
- MaxRestarts *int `json:"maxRestarts"`
- InitBackoffSec *int `json:"initBackoffSec"`
- MaxBackoffSec *int `json:"maxBackoffSec"`
- StableAfterSec *int `json:"stableAfterSec"`
-}
-
-type timeoutsConfigJSON struct {
- ActionSec int `json:"actionSec"`
- NavigateSec int `json:"navigateSec"`
- ShutdownSec int `json:"shutdownSec"`
- WaitNavMs int `json:"waitNavMs"`
-}
-
-type schedulerFileConfigJSON struct {
- Enabled *bool `json:"enabled"`
- Strategy string `json:"strategy"`
- MaxQueueSize *int `json:"maxQueueSize"`
- MaxPerAgent *int `json:"maxPerAgent"`
- MaxInflight *int `json:"maxInflight"`
- MaxPerAgentFlight *int `json:"maxPerAgentInflight"`
- ResultTTLSec *int `json:"resultTTLSec"`
- WorkerCount *int `json:"workerCount"`
-}
-
-type observabilityFileConfigJSON struct {
- Activity activityConfigJSON `json:"activity"`
-}
-
-type activityConfigJSON struct {
- Enabled *bool `json:"enabled"`
- SessionIdleSec *int `json:"sessionIdleSec"`
- RetentionDays *int `json:"retentionDays"`
- StateDir string `json:"stateDir"`
- Events activityEventsConfigJSON `json:"events"`
-}
-
-type activityEventsConfigJSON struct {
- Dashboard *bool `json:"dashboard,omitempty"`
- Server *bool `json:"server,omitempty"`
- Bridge *bool `json:"bridge,omitempty"`
- Orchestrator *bool `json:"orchestrator,omitempty"`
- Scheduler *bool `json:"scheduler,omitempty"`
- MCP *bool `json:"mcp,omitempty"`
- Other *bool `json:"other,omitempty"`
-}
-
-type sessionsFileConfigJSON struct {
- Dashboard dashboardSessionConfigJSON `json:"dashboard"`
-}
-
-type dashboardSessionConfigJSON struct {
- Persist *bool `json:"persist,omitempty"`
- IdleTimeoutSec *int `json:"idleTimeoutSec,omitempty"`
- MaxLifetimeSec *int `json:"maxLifetimeSec,omitempty"`
- ElevationWindowSec *int `json:"elevationWindowSec,omitempty"`
- PersistElevationAcrossRestart *bool `json:"persistElevationAcrossRestart,omitempty"`
- RequireElevation *bool `json:"requireElevation,omitempty"`
-}
-
-type autoSolverFileConfigJSON struct {
- Enabled *bool `json:"enabled,omitempty"`
- AutoTrigger *bool `json:"autoTrigger,omitempty"`
- TriggerOnNavigate *bool `json:"triggerOnNavigate,omitempty"`
- TriggerOnAction *bool `json:"triggerOnAction,omitempty"`
- MaxAttempts *int `json:"maxAttempts,omitempty"`
- SolverTimeoutSec *int `json:"solverTimeoutSec,omitempty"`
- RetryBaseDelayMs *int `json:"retryBaseDelayMs,omitempty"`
- RetryMaxDelayMs *int `json:"retryMaxDelayMs,omitempty"`
- Solvers []string `json:"solvers,omitempty"`
- LLMProvider string `json:"llmProvider,omitempty"`
- LLMFallback *bool `json:"llmFallback,omitempty"`
- External autoSolverExtConfigJSON `json:"external,omitempty"`
- Credentials autoSolverCredentialsConfigJSON `json:"credentials,omitempty"`
-}
-
-type autoSolverExtConfigJSON struct {
- CapsolverKey string `json:"capsolverKey,omitempty"`
- TwoCaptchaKey string `json:"twoCaptchaKey,omitempty"`
-}
-
-type autoSolverCredentialsConfigJSON struct {
- Login autoSolverLoginConfigJSON `json:"login,omitempty"`
- Signup autoSolverSignupConfigJSON `json:"signup,omitempty"`
- Form autoSolverFormConfigJSON `json:"form,omitempty"`
-}
-
-type autoSolverLoginConfigJSON struct {
- User string `json:"user,omitempty"`
- Password string `json:"password,omitempty"`
-}
-
-type autoSolverSignupConfigJSON struct {
- Name string `json:"name,omitempty"`
- Email string `json:"email,omitempty"`
- Password string `json:"password,omitempty"`
-}
-
-type autoSolverFormConfigJSON struct {
- Field1 string `json:"field1,omitempty"`
- Field2 string `json:"field2,omitempty"`
- Email string `json:"email,omitempty"`
-}
-
-func copyStringSlice(items []string) []string {
- if items == nil {
- return []string{}
- }
- if len(items) == 0 {
- return []string{}
- }
- return append([]string(nil), items...)
-}
-
-func (fc FileConfig) MarshalJSON() ([]byte, error) {
- return json.Marshal(fileConfigJSON{
- ConfigVersion: fc.ConfigVersion,
- Server: serverConfigJSON{
- Port: fc.Server.Port,
- Bind: fc.Server.Bind,
- Token: fc.Server.Token,
- StateDir: fc.Server.StateDir,
- Engine: fc.Server.Engine,
- NetworkBufferSize: fc.Server.NetworkBufferSize,
- TrustProxyHeaders: fc.Server.TrustProxyHeaders,
- CookieSecure: fc.Server.CookieSecure,
- },
- Browser: browserConfigJSON{
- ChromeVersion: fc.Browser.ChromeVersion,
- ChromeBinary: fc.Browser.ChromeBinary,
- ChromeDebugPort: fc.Browser.ChromeDebugPort,
- ChromeExtraFlags: fc.Browser.ChromeExtraFlags,
- ExtensionPaths: copyStringSlice(fc.Browser.ExtensionPaths),
- },
- InstanceDefaults: instanceDefaultsConfigJSON{
- Mode: fc.InstanceDefaults.Mode,
- NoRestore: fc.InstanceDefaults.NoRestore,
- Timezone: fc.InstanceDefaults.Timezone,
- BlockImages: fc.InstanceDefaults.BlockImages,
- BlockMedia: fc.InstanceDefaults.BlockMedia,
- BlockAds: fc.InstanceDefaults.BlockAds,
- MaxTabs: fc.InstanceDefaults.MaxTabs,
- MaxParallelTabs: fc.InstanceDefaults.MaxParallelTabs,
- UserAgent: fc.InstanceDefaults.UserAgent,
- NoAnimations: fc.InstanceDefaults.NoAnimations,
- StealthLevel: fc.InstanceDefaults.StealthLevel,
- TabEvictionPolicy: fc.InstanceDefaults.TabEvictionPolicy,
- TabPolicy: fc.InstanceDefaults.TabPolicy,
- },
- Security: securityConfigJSON{
- AllowEvaluate: fc.Security.AllowEvaluate,
- AllowMacro: fc.Security.AllowMacro,
- AllowScreencast: fc.Security.AllowScreencast,
- AllowDownload: fc.Security.AllowDownload,
- AllowedDomains: effectiveSecurityAllowedDomains(fc.Security),
- DownloadAllowedDomains: copyStringSlice(fc.Security.DownloadAllowedDomains),
- DownloadMaxBytes: fc.Security.DownloadMaxBytes,
- AllowUpload: fc.Security.AllowUpload,
- AllowClipboard: fc.Security.AllowClipboard,
- AllowStateExport: fc.Security.AllowStateExport,
- StateEncryptionKey: fc.Security.StateEncryptionKey,
- EnableActionGuards: fc.Security.EnableActionGuards,
- UploadMaxRequestBytes: fc.Security.UploadMaxRequestBytes,
- UploadMaxFiles: fc.Security.UploadMaxFiles,
- UploadMaxFileBytes: fc.Security.UploadMaxFileBytes,
- UploadMaxTotalBytes: fc.Security.UploadMaxTotalBytes,
- MaxRedirects: fc.Security.MaxRedirects,
- TrustedProxyCIDRs: copyStringSlice(fc.Security.TrustedProxyCIDRs),
- TrustedResolveCIDRs: copyStringSlice(fc.Security.TrustedResolveCIDRs),
- Attach: attachJSON{
- Enabled: fc.Security.Attach.Enabled,
- AllowHosts: copyStringSlice(fc.Security.Attach.AllowHosts),
- AllowSchemes: copyStringSlice(fc.Security.Attach.AllowSchemes),
- },
- IDPI: idpiConfigJSON{
- Enabled: fc.Security.IDPI.Enabled,
- StrictMode: fc.Security.IDPI.StrictMode,
- ScanContent: fc.Security.IDPI.ScanContent,
- WrapContent: fc.Security.IDPI.WrapContent,
- CustomPatterns: copyStringSlice(fc.Security.IDPI.CustomPatterns),
- ScanTimeoutSec: fc.Security.IDPI.ScanTimeoutSec,
- ShieldThreshold: fc.Security.IDPI.ShieldThreshold,
- },
- },
- Profiles: profilesConfigJSON{
- BaseDir: fc.Profiles.BaseDir,
- DefaultProfile: fc.Profiles.DefaultProfile,
- },
- MultiInstance: multiInstanceConfigJSON{
- Strategy: fc.MultiInstance.Strategy,
- AllocationPolicy: fc.MultiInstance.AllocationPolicy,
- InstancePortStart: fc.MultiInstance.InstancePortStart,
- InstancePortEnd: fc.MultiInstance.InstancePortEnd,
- Restart: multiInstanceRestartJSON{
- MaxRestarts: fc.MultiInstance.Restart.MaxRestarts,
- InitBackoffSec: fc.MultiInstance.Restart.InitBackoffSec,
- MaxBackoffSec: fc.MultiInstance.Restart.MaxBackoffSec,
- StableAfterSec: fc.MultiInstance.Restart.StableAfterSec,
- },
- },
- Timeouts: timeoutsConfigJSON{
- ActionSec: fc.Timeouts.ActionSec,
- NavigateSec: fc.Timeouts.NavigateSec,
- ShutdownSec: fc.Timeouts.ShutdownSec,
- WaitNavMs: fc.Timeouts.WaitNavMs,
- },
- Scheduler: schedulerFileConfigJSON{
- Enabled: fc.Scheduler.Enabled,
- Strategy: fc.Scheduler.Strategy,
- MaxQueueSize: fc.Scheduler.MaxQueueSize,
- MaxPerAgent: fc.Scheduler.MaxPerAgent,
- MaxInflight: fc.Scheduler.MaxInflight,
- MaxPerAgentFlight: fc.Scheduler.MaxPerAgentFlight,
- ResultTTLSec: fc.Scheduler.ResultTTLSec,
- WorkerCount: fc.Scheduler.WorkerCount,
- },
- Observability: observabilityFileConfigJSON{
- Activity: activityConfigJSON{
- Enabled: fc.Observability.Activity.Enabled,
- SessionIdleSec: fc.Observability.Activity.SessionIdleSec,
- RetentionDays: fc.Observability.Activity.RetentionDays,
- StateDir: fc.Observability.Activity.StateDir,
- Events: activityEventsConfigJSON{
- Dashboard: fc.Observability.Activity.Events.Dashboard,
- Server: fc.Observability.Activity.Events.Server,
- Bridge: fc.Observability.Activity.Events.Bridge,
- Orchestrator: fc.Observability.Activity.Events.Orchestrator,
- Scheduler: fc.Observability.Activity.Events.Scheduler,
- MCP: fc.Observability.Activity.Events.MCP,
- Other: fc.Observability.Activity.Events.Other,
- },
- },
- },
- Sessions: sessionsFileConfigJSON{
- Dashboard: dashboardSessionConfigJSON{
- Persist: fc.Sessions.Dashboard.Persist,
- IdleTimeoutSec: fc.Sessions.Dashboard.IdleTimeoutSec,
- MaxLifetimeSec: fc.Sessions.Dashboard.MaxLifetimeSec,
- ElevationWindowSec: fc.Sessions.Dashboard.ElevationWindowSec,
- PersistElevationAcrossRestart: fc.Sessions.Dashboard.PersistElevationAcrossRestart,
- RequireElevation: fc.Sessions.Dashboard.RequireElevation,
- },
- },
- AutoSolver: autoSolverFileConfigJSON{
- Enabled: fc.AutoSolver.Enabled,
- AutoTrigger: fc.AutoSolver.AutoTrigger,
- TriggerOnNavigate: fc.AutoSolver.TriggerOnNavigate,
- TriggerOnAction: fc.AutoSolver.TriggerOnAction,
- MaxAttempts: fc.AutoSolver.MaxAttempts,
- SolverTimeoutSec: fc.AutoSolver.SolverTimeoutSec,
- RetryBaseDelayMs: fc.AutoSolver.RetryBaseDelayMs,
- RetryMaxDelayMs: fc.AutoSolver.RetryMaxDelayMs,
- Solvers: copyStringSlice(fc.AutoSolver.Solvers),
- LLMProvider: fc.AutoSolver.LLMProvider,
- LLMFallback: fc.AutoSolver.LLMFallback,
- External: autoSolverExtConfigJSON{
- CapsolverKey: fc.AutoSolver.External.CapsolverKey,
- TwoCaptchaKey: fc.AutoSolver.External.TwoCaptchaKey,
- },
- Credentials: autoSolverCredentialsConfigJSON{
- Login: autoSolverLoginConfigJSON{
- User: fc.AutoSolver.Credentials.Login.User,
- Password: fc.AutoSolver.Credentials.Login.Password,
- },
- Signup: autoSolverSignupConfigJSON{
- Name: fc.AutoSolver.Credentials.Signup.Name,
- Email: fc.AutoSolver.Credentials.Signup.Email,
- Password: fc.AutoSolver.Credentials.Signup.Password,
- },
- Form: autoSolverFormConfigJSON{
- Field1: fc.AutoSolver.Credentials.Form.Field1,
- Field2: fc.AutoSolver.Credentials.Form.Field2,
- Email: fc.AutoSolver.Credentials.Form.Email,
- },
- },
- },
- })
-}
-
-func (fc *FileConfig) UnmarshalJSON(data []byte) error {
- type rawFileConfig FileConfig
- tmp := rawFileConfig(*fc)
- if err := json.Unmarshal(data, &tmp); err != nil {
- return err
- }
- *fc = FileConfig(tmp)
- NormalizeFileConfigAliasesFromJSON(fc, data)
- return nil
-}
-
-// FileConfigFromRuntime converts the effective runtime configuration back into a
-// nested file configuration shape.
-func FileConfigFromRuntime(cfg *RuntimeConfig) FileConfig {
- if cfg == nil {
- return DefaultFileConfig()
- }
-
- noRestore := cfg.NoRestore
- blockImages := cfg.BlockImages
- blockMedia := cfg.BlockMedia
- blockAds := cfg.BlockAds
- maxTabs := cfg.MaxTabs
- maxParallelTabs := cfg.MaxParallelTabs
- noAnimations := cfg.NoAnimations
- allowEvaluate := cfg.AllowEvaluate
- allowMacro := cfg.AllowMacro
- allowScreencast := cfg.AllowScreencast
- allowDownload := cfg.AllowDownload
- downloadAllowedDomains := copyStringSlice(cfg.DownloadAllowedDomains)
- downloadMaxBytes := cfg.EffectiveDownloadMaxBytes()
- allowUpload := cfg.AllowUpload
- allowClipboard := cfg.AllowClipboard
- allowStateExport := cfg.AllowStateExport
- enableActionGuards := cfg.EnableActionGuards
- uploadMaxRequestBytes := cfg.EffectiveUploadMaxRequestBytes()
- uploadMaxFiles := cfg.EffectiveUploadMaxFiles()
- uploadMaxFileBytes := cfg.EffectiveUploadMaxFileBytes()
- uploadMaxTotalBytes := cfg.EffectiveUploadMaxTotalBytes()
- maxRedirects := cfg.MaxRedirects
- attachEnabled := cfg.AttachEnabled
- start := cfg.InstancePortStart
- end := cfg.InstancePortEnd
- restartMaxRestarts := cfg.RestartMaxRestarts
- restartInitBackoffSec := int(cfg.RestartInitBackoff / time.Second)
- restartMaxBackoffSec := int(cfg.RestartMaxBackoff / time.Second)
- restartStableAfterSec := int(cfg.RestartStableAfter / time.Second)
- activityEnabled := cfg.Observability.Activity.Enabled
- activitySessionIdleSec := cfg.Observability.Activity.SessionIdleSec
- activityRetentionDays := cfg.Observability.Activity.RetentionDays
- activityDashboardEvents := cfg.Observability.Activity.Events.Dashboard
- activityServerEvents := cfg.Observability.Activity.Events.Server
- activityBridgeEvents := cfg.Observability.Activity.Events.Bridge
- activityOrchestratorEvents := cfg.Observability.Activity.Events.Orchestrator
- activitySchedulerEvents := cfg.Observability.Activity.Events.Scheduler
- activityMCPEvents := cfg.Observability.Activity.Events.MCP
- activityOtherEvents := cfg.Observability.Activity.Events.Other
- dashboardSessionPersist := cfg.Sessions.Dashboard.Persist
- dashboardSessionIdleSec := int(cfg.Sessions.Dashboard.IdleTimeout / time.Second)
- dashboardSessionMaxLifetimeSec := int(cfg.Sessions.Dashboard.MaxLifetime / time.Second)
- dashboardSessionElevationWindowSec := int(cfg.Sessions.Dashboard.ElevationWindow / time.Second)
- dashboardSessionPersistElevationAcrossRestart := cfg.Sessions.Dashboard.PersistElevationAcrossRestart
- dashboardSessionRequireElevation := cfg.Sessions.Dashboard.RequireElevation
- autoSolverEnabled := cfg.AutoSolver.Enabled
- autoSolverAutoTrigger := cfg.AutoSolver.AutoTrigger
- autoSolverTriggerOnNavigate := cfg.AutoSolver.TriggerOnNavigate
- autoSolverTriggerOnAction := cfg.AutoSolver.TriggerOnAction
- autoSolverMaxAttempts := cfg.AutoSolver.MaxAttempts
- autoSolverSolverTimeoutSec := cfg.AutoSolver.SolverTimeoutSec
- autoSolverRetryBaseDelayMs := cfg.AutoSolver.RetryBaseDelayMs
- autoSolverRetryMaxDelayMs := cfg.AutoSolver.RetryMaxDelayMs
- autoSolverLLMFallback := cfg.AutoSolver.LLMFallback
-
- mode := "headless"
- if !cfg.Headless {
- mode = "headed"
- }
-
- var netBufSize *int
- if cfg.NetworkBufferSize > 0 {
- v := cfg.NetworkBufferSize
- netBufSize = &v
- }
-
- fc := FileConfig{
- Server: ServerConfig{
- Port: cfg.Port,
- Bind: cfg.Bind,
- Token: cfg.Token,
- StateDir: cfg.StateDir,
- Engine: cfg.Engine,
- NetworkBufferSize: netBufSize,
- TrustProxyHeaders: &cfg.TrustProxyHeaders,
- CookieSecure: cfg.CookieSecure,
- },
- Browser: BrowserConfig{
- ChromeVersion: cfg.ChromeVersion,
- ChromeBinary: cfg.ChromeBinary,
- ChromeDebugPort: intPtrIfPositive(cfg.ChromeDebugPort),
- ChromeExtraFlags: cfg.ChromeExtraFlags,
- ExtensionPaths: append([]string(nil), cfg.ExtensionPaths...),
- },
- InstanceDefaults: InstanceDefaultsConfig{
- Mode: mode,
- NoRestore: &noRestore,
- Timezone: cfg.Timezone,
- BlockImages: &blockImages,
- BlockMedia: &blockMedia,
- BlockAds: &blockAds,
- MaxTabs: &maxTabs,
- MaxParallelTabs: &maxParallelTabs,
- UserAgent: cfg.UserAgent,
- NoAnimations: &noAnimations,
- StealthLevel: cfg.StealthLevel,
- TabEvictionPolicy: cfg.TabEvictionPolicy,
- TabPolicy: tabPolicyDefaultsFromRuntime(cfg),
- },
- Security: SecurityConfig{
- AllowEvaluate: &allowEvaluate,
- AllowMacro: &allowMacro,
- AllowScreencast: &allowScreencast,
- AllowDownload: &allowDownload,
- AllowedDomains: append([]string(nil), cfg.AllowedDomains...),
- DownloadAllowedDomains: downloadAllowedDomains,
- DownloadMaxBytes: &downloadMaxBytes,
- AllowUpload: &allowUpload,
- AllowClipboard: &allowClipboard,
- AllowStateExport: &allowStateExport,
- EnableActionGuards: &enableActionGuards,
- UploadMaxRequestBytes: &uploadMaxRequestBytes,
- UploadMaxFiles: &uploadMaxFiles,
- UploadMaxFileBytes: &uploadMaxFileBytes,
- UploadMaxTotalBytes: &uploadMaxTotalBytes,
- MaxRedirects: &maxRedirects,
- TrustedProxyCIDRs: append([]string(nil), cfg.TrustedProxyCIDRs...),
- TrustedResolveCIDRs: append([]string(nil), cfg.TrustedResolveCIDRs...),
- Attach: AttachConfig{
- Enabled: &attachEnabled,
- AllowHosts: append([]string(nil), cfg.AttachAllowHosts...),
- AllowSchemes: append([]string(nil), cfg.AttachAllowSchemes...),
- },
- IDPI: cfg.IDPI,
- },
- Profiles: ProfilesConfig{
- BaseDir: cfg.ProfilesBaseDir,
- DefaultProfile: cfg.DefaultProfile,
- },
- MultiInstance: MultiInstanceConfig{
- Strategy: cfg.Strategy,
- AllocationPolicy: cfg.AllocationPolicy,
- InstancePortStart: &start,
- InstancePortEnd: &end,
- Restart: MultiInstanceRestartConfig{
- MaxRestarts: &restartMaxRestarts,
- InitBackoffSec: &restartInitBackoffSec,
- MaxBackoffSec: &restartMaxBackoffSec,
- StableAfterSec: &restartStableAfterSec,
- },
- },
- Timeouts: TimeoutsConfig{
- ActionSec: int(cfg.ActionTimeout / time.Second),
- NavigateSec: int(cfg.NavigateTimeout / time.Second),
- ShutdownSec: int(cfg.ShutdownTimeout / time.Second),
- WaitNavMs: int(cfg.WaitNavDelay / time.Millisecond),
- },
- Observability: ObservabilityFileConfig{
- Activity: ActivityFileConfig{
- Enabled: &activityEnabled,
- SessionIdleSec: &activitySessionIdleSec,
- RetentionDays: &activityRetentionDays,
- Events: ActivityEventsFileConfig{
- Dashboard: &activityDashboardEvents,
- Server: &activityServerEvents,
- Bridge: &activityBridgeEvents,
- Orchestrator: &activityOrchestratorEvents,
- Scheduler: &activitySchedulerEvents,
- MCP: &activityMCPEvents,
- Other: &activityOtherEvents,
- },
- },
- },
- Sessions: SessionsFileConfig{
- Dashboard: DashboardSessionFileConfig{
- Persist: &dashboardSessionPersist,
- IdleTimeoutSec: &dashboardSessionIdleSec,
- MaxLifetimeSec: &dashboardSessionMaxLifetimeSec,
- ElevationWindowSec: &dashboardSessionElevationWindowSec,
- PersistElevationAcrossRestart: &dashboardSessionPersistElevationAcrossRestart,
- RequireElevation: &dashboardSessionRequireElevation,
- },
- },
- AutoSolver: AutoSolverFileConfig{
- Enabled: &autoSolverEnabled,
- AutoTrigger: &autoSolverAutoTrigger,
- TriggerOnNavigate: &autoSolverTriggerOnNavigate,
- TriggerOnAction: &autoSolverTriggerOnAction,
- MaxAttempts: &autoSolverMaxAttempts,
- SolverTimeoutSec: &autoSolverSolverTimeoutSec,
- RetryBaseDelayMs: &autoSolverRetryBaseDelayMs,
- RetryMaxDelayMs: &autoSolverRetryMaxDelayMs,
- Solvers: copyStringSlice(cfg.AutoSolver.Solvers),
- LLMProvider: cfg.AutoSolver.LLMProvider,
- LLMFallback: &autoSolverLLMFallback,
- External: AutoSolverExtConf{
- CapsolverKey: cfg.AutoSolver.CapsolverKey,
- TwoCaptchaKey: cfg.AutoSolver.TwoCaptchaKey,
- },
- Credentials: AutoSolverCredentialsConf{
- Login: AutoSolverLoginConf{
- User: cfg.AutoSolver.Credentials.Login.User,
- Password: cfg.AutoSolver.Credentials.Login.Password,
- },
- Signup: AutoSolverSignupConf{
- Name: cfg.AutoSolver.Credentials.Signup.Name,
- Email: cfg.AutoSolver.Credentials.Signup.Email,
- Password: cfg.AutoSolver.Credentials.Signup.Password,
- },
- Form: AutoSolverFormConf{
- Field1: cfg.AutoSolver.Credentials.Form.Field1,
- Field2: cfg.AutoSolver.Credentials.Form.Field2,
- Email: cfg.AutoSolver.Credentials.Form.Email,
- },
- },
- },
- }
-
- return fc
-}
-
-func intPtrIfPositive(v int) *int {
- if v <= 0 {
- return nil
- }
- n := v
- return &n
-}
-
-// legacyFileConfig is the old flat structure for backward compatibility.
-type legacyFileConfig struct {
- Port string `json:"port"`
- InstancePortStart *int `json:"instancePortStart,omitempty"`
- InstancePortEnd *int `json:"instancePortEnd,omitempty"`
- Token string `json:"token,omitempty"`
- AllowEvaluate *bool `json:"allowEvaluate,omitempty"`
- AllowMacro *bool `json:"allowMacro,omitempty"`
- AllowScreencast *bool `json:"allowScreencast,omitempty"`
- AllowDownload *bool `json:"allowDownload,omitempty"`
- AllowUpload *bool `json:"allowUpload,omitempty"`
- AllowClipboard *bool `json:"allowClipboard,omitempty"`
- StateDir string `json:"stateDir"`
- ProfileDir string `json:"profileDir"`
- Headless *bool `json:"headless,omitempty"`
- NoRestore bool `json:"noRestore"`
- MaxTabs *int `json:"maxTabs,omitempty"`
- TimeoutSec int `json:"timeoutSec,omitempty"`
- NavigateSec int `json:"navigateSec,omitempty"`
-}
-
-// convertLegacyConfig converts flat config to nested structure.
-func convertLegacyConfig(lc *legacyFileConfig) *FileConfig {
- fc := &FileConfig{}
-
- // Server
- fc.Server.Port = lc.Port
- fc.Server.Token = lc.Token
- fc.Server.StateDir = lc.StateDir
-
- // Browser / instance defaults
- if lc.Headless != nil {
- if *lc.Headless {
- fc.InstanceDefaults.Mode = "headless"
- } else {
- fc.InstanceDefaults.Mode = "headed"
- }
- }
- fc.InstanceDefaults.MaxTabs = lc.MaxTabs
- if lc.NoRestore {
- b := true
- fc.InstanceDefaults.NoRestore = &b
- }
-
- // Profiles
- if lc.ProfileDir != "" {
- fc.Profiles.BaseDir = filepath.Dir(lc.ProfileDir)
- fc.Profiles.DefaultProfile = filepath.Base(lc.ProfileDir)
- }
-
- // Security
- fc.Security.AllowEvaluate = lc.AllowEvaluate
- fc.Security.AllowMacro = lc.AllowMacro
- fc.Security.AllowScreencast = lc.AllowScreencast
- fc.Security.AllowDownload = lc.AllowDownload
- fc.Security.AllowUpload = lc.AllowUpload
- fc.Security.AllowClipboard = lc.AllowClipboard
-
- // Timeouts
- fc.Timeouts.ActionSec = lc.TimeoutSec
- fc.Timeouts.NavigateSec = lc.NavigateSec
-
- // Multi-instance
- fc.MultiInstance.InstancePortStart = lc.InstancePortStart
- fc.MultiInstance.InstancePortEnd = lc.InstancePortEnd
-
- return fc
-}
-
-// isLegacyConfig detects if JSON is flat (legacy) or nested (new).
-func isLegacyConfig(data []byte) bool {
- var probe map[string]json.RawMessage
- if err := json.Unmarshal(data, &probe); err != nil {
- return false
- }
-
- // If any new nested keys exist, it's new format
- newKeys := []string{"server", "browser", "instanceDefaults", "profiles", "multiInstance", "security", "attach", "timeouts", "sessions"}
- for _, key := range newKeys {
- if _, has := probe[key]; has {
- return false
- }
- }
-
- // If "port" or "headless" exist at top level, it's legacy
- if _, hasPort := probe["port"]; hasPort {
- return true
- }
- if _, hasHeadless := probe["headless"]; hasHeadless {
- return true
- }
-
- return false
-}
-
-func modeToHeadless(mode string, fallback bool) bool {
- switch strings.ToLower(strings.TrimSpace(mode)) {
- case "":
- return fallback
- case "headless":
- return true
- case "headed":
- return false
- default:
- return fallback
- }
-}
-
-// tabPolicyDefaultsFromRuntime emits a TabPolicyDefaults block when the runtime
-// config carries any non-default tab-policy setting (lifecycle, close delay, or
-// restore). Returns nil for a fully vanilla config so round-tripping doesn't
-// introduce a noisy tabPolicy block.
-func tabPolicyDefaultsFromRuntime(cfg *RuntimeConfig) *TabPolicyDefaults {
- if cfg == nil {
- return nil
- }
- hasLifecycle := cfg.TabLifecyclePolicy != "" &&
- (cfg.TabLifecyclePolicy != "close_idle" || cfg.TabCloseDelay != 5*time.Minute)
- hasRestore := cfg.TabRestore
- if !hasLifecycle && !hasRestore {
- return nil
- }
- out := &TabPolicyDefaults{}
- if hasLifecycle {
- out.Lifecycle = cfg.TabLifecyclePolicy
- if cfg.TabLifecyclePolicy == "close_idle" && cfg.TabCloseDelay > 0 {
- sec := int(cfg.TabCloseDelay / time.Second)
- out.CloseDelaySec = &sec
- }
- }
- if hasRestore {
- v := cfg.TabRestore
- out.Restore = &v
- }
- return out
-}
diff --git a/internal/config/config_file_json.go b/internal/config/config_file_json.go
new file mode 100644
index 00000000..4a27435b
--- /dev/null
+++ b/internal/config/config_file_json.go
@@ -0,0 +1,208 @@
+package config
+
+type fileConfigJSON struct {
+ ConfigVersion string `json:"configVersion,omitempty"`
+ Server serverConfigJSON `json:"server"`
+ Browser browserConfigJSON `json:"browser"`
+ InstanceDefaults instanceDefaultsConfigJSON `json:"instanceDefaults"`
+ Security securityConfigJSON `json:"security"`
+ Profiles profilesConfigJSON `json:"profiles"`
+ MultiInstance multiInstanceConfigJSON `json:"multiInstance"`
+ Timeouts timeoutsConfigJSON `json:"timeouts"`
+ Scheduler schedulerFileConfigJSON `json:"scheduler"`
+ Observability observabilityFileConfigJSON `json:"observability"`
+ Sessions sessionsFileConfigJSON `json:"sessions"`
+ AutoSolver autoSolverFileConfigJSON `json:"autoSolver,omitempty"`
+}
+
+type serverConfigJSON struct {
+ Port string `json:"port"`
+ Bind string `json:"bind"`
+ Token string `json:"token"`
+ StateDir string `json:"stateDir"`
+ Engine string `json:"engine"`
+ NetworkBufferSize *int `json:"networkBufferSize,omitempty"`
+ TrustProxyHeaders *bool `json:"trustProxyHeaders,omitempty"`
+ CookieSecure *bool `json:"cookieSecure,omitempty"`
+}
+
+type browserConfigJSON struct {
+ ChromeVersion string `json:"version"`
+ ChromeBinary string `json:"binary"`
+ ChromeDebugPort *int `json:"remoteDebuggingPort,omitempty"`
+ ChromeExtraFlags string `json:"extraFlags"`
+ ExtensionPaths []string `json:"extensionPaths"`
+}
+
+type instanceDefaultsConfigJSON struct {
+ Mode string `json:"mode"`
+ NoRestore *bool `json:"noRestore"`
+ Timezone string `json:"timezone"`
+ BlockImages *bool `json:"blockImages"`
+ BlockMedia *bool `json:"blockMedia"`
+ BlockAds *bool `json:"blockAds"`
+ MaxTabs *int `json:"maxTabs"`
+ MaxParallelTabs *int `json:"maxParallelTabs"`
+ UserAgent string `json:"userAgent"`
+ NoAnimations *bool `json:"noAnimations"`
+ StealthLevel string `json:"stealthLevel"`
+ TabEvictionPolicy string `json:"tabEvictionPolicy"`
+ TabPolicy *TabPolicyDefaults `json:"tabPolicy,omitempty"`
+}
+
+type profilesConfigJSON struct {
+ BaseDir string `json:"baseDir"`
+ DefaultProfile string `json:"defaultProfile"`
+}
+
+type securityConfigJSON struct {
+ AllowEvaluate *bool `json:"allowEvaluate"`
+ AllowMacro *bool `json:"allowMacro"`
+ AllowScreencast *bool `json:"allowScreencast"`
+ AllowDownload *bool `json:"allowDownload"`
+ AllowedDomains []string `json:"allowedDomains"`
+ DownloadAllowedDomains []string `json:"downloadAllowedDomains"`
+ DownloadMaxBytes *int `json:"downloadMaxBytes"`
+ AllowUpload *bool `json:"allowUpload"`
+ AllowClipboard *bool `json:"allowClipboard"`
+ AllowStateExport *bool `json:"allowStateExport"`
+ StateEncryptionKey *string `json:"stateEncryptionKey"`
+ EnableActionGuards *bool `json:"enableActionGuards"`
+ UploadMaxRequestBytes *int `json:"uploadMaxRequestBytes"`
+ UploadMaxFiles *int `json:"uploadMaxFiles"`
+ UploadMaxFileBytes *int `json:"uploadMaxFileBytes"`
+ UploadMaxTotalBytes *int `json:"uploadMaxTotalBytes"`
+ MaxRedirects *int `json:"maxRedirects"`
+ TrustedProxyCIDRs []string `json:"trustedProxyCIDRs"`
+ TrustedResolveCIDRs []string `json:"trustedResolveCIDRs"`
+ Attach attachJSON `json:"attach"`
+ IDPI idpiConfigJSON `json:"idpi"`
+}
+
+type attachJSON struct {
+ Enabled *bool `json:"enabled"`
+ AllowHosts []string `json:"allowHosts"`
+ AllowSchemes []string `json:"allowSchemes"`
+}
+
+type idpiConfigJSON struct {
+ Enabled bool `json:"enabled"`
+ StrictMode bool `json:"strictMode"`
+ ScanContent bool `json:"scanContent"`
+ WrapContent bool `json:"wrapContent"`
+ CustomPatterns []string `json:"customPatterns"`
+ ScanTimeoutSec int `json:"scanTimeoutSec"`
+ ShieldThreshold int `json:"shieldThreshold"`
+}
+
+type multiInstanceConfigJSON struct {
+ Strategy string `json:"strategy"`
+ AllocationPolicy string `json:"allocationPolicy"`
+ InstancePortStart *int `json:"instancePortStart"`
+ InstancePortEnd *int `json:"instancePortEnd"`
+ Restart multiInstanceRestartJSON `json:"restart"`
+}
+
+type multiInstanceRestartJSON struct {
+ MaxRestarts *int `json:"maxRestarts"`
+ InitBackoffSec *int `json:"initBackoffSec"`
+ MaxBackoffSec *int `json:"maxBackoffSec"`
+ StableAfterSec *int `json:"stableAfterSec"`
+}
+
+type timeoutsConfigJSON struct {
+ ActionSec int `json:"actionSec"`
+ NavigateSec int `json:"navigateSec"`
+ ShutdownSec int `json:"shutdownSec"`
+ WaitNavMs int `json:"waitNavMs"`
+}
+
+type schedulerFileConfigJSON struct {
+ Enabled *bool `json:"enabled"`
+ Strategy string `json:"strategy"`
+ MaxQueueSize *int `json:"maxQueueSize"`
+ MaxPerAgent *int `json:"maxPerAgent"`
+ MaxInflight *int `json:"maxInflight"`
+ MaxPerAgentFlight *int `json:"maxPerAgentInflight"`
+ ResultTTLSec *int `json:"resultTTLSec"`
+ WorkerCount *int `json:"workerCount"`
+}
+
+type observabilityFileConfigJSON struct {
+ Activity activityConfigJSON `json:"activity"`
+}
+
+type activityConfigJSON struct {
+ Enabled *bool `json:"enabled"`
+ SessionIdleSec *int `json:"sessionIdleSec"`
+ RetentionDays *int `json:"retentionDays"`
+ StateDir string `json:"stateDir"`
+ Events activityEventsConfigJSON `json:"events"`
+}
+
+type activityEventsConfigJSON struct {
+ Dashboard *bool `json:"dashboard,omitempty"`
+ Server *bool `json:"server,omitempty"`
+ Bridge *bool `json:"bridge,omitempty"`
+ Orchestrator *bool `json:"orchestrator,omitempty"`
+ Scheduler *bool `json:"scheduler,omitempty"`
+ MCP *bool `json:"mcp,omitempty"`
+ Other *bool `json:"other,omitempty"`
+}
+
+type sessionsFileConfigJSON struct {
+ Dashboard dashboardSessionConfigJSON `json:"dashboard"`
+}
+
+type dashboardSessionConfigJSON struct {
+ Persist *bool `json:"persist,omitempty"`
+ IdleTimeoutSec *int `json:"idleTimeoutSec,omitempty"`
+ MaxLifetimeSec *int `json:"maxLifetimeSec,omitempty"`
+ ElevationWindowSec *int `json:"elevationWindowSec,omitempty"`
+ PersistElevationAcrossRestart *bool `json:"persistElevationAcrossRestart,omitempty"`
+ RequireElevation *bool `json:"requireElevation,omitempty"`
+}
+
+type autoSolverFileConfigJSON struct {
+ Enabled *bool `json:"enabled,omitempty"`
+ AutoTrigger *bool `json:"autoTrigger,omitempty"`
+ TriggerOnNavigate *bool `json:"triggerOnNavigate,omitempty"`
+ TriggerOnAction *bool `json:"triggerOnAction,omitempty"`
+ MaxAttempts *int `json:"maxAttempts,omitempty"`
+ SolverTimeoutSec *int `json:"solverTimeoutSec,omitempty"`
+ RetryBaseDelayMs *int `json:"retryBaseDelayMs,omitempty"`
+ RetryMaxDelayMs *int `json:"retryMaxDelayMs,omitempty"`
+ Solvers []string `json:"solvers,omitempty"`
+ LLMProvider string `json:"llmProvider,omitempty"`
+ LLMFallback *bool `json:"llmFallback,omitempty"`
+ External autoSolverExtConfigJSON `json:"external,omitempty"`
+ Credentials autoSolverCredentialsConfigJSON `json:"credentials,omitempty"`
+}
+
+type autoSolverExtConfigJSON struct {
+ CapsolverKey string `json:"capsolverKey,omitempty"`
+ TwoCaptchaKey string `json:"twoCaptchaKey,omitempty"`
+}
+
+type autoSolverCredentialsConfigJSON struct {
+ Login autoSolverLoginConfigJSON `json:"login,omitempty"`
+ Signup autoSolverSignupConfigJSON `json:"signup,omitempty"`
+ Form autoSolverFormConfigJSON `json:"form,omitempty"`
+}
+
+type autoSolverLoginConfigJSON struct {
+ User string `json:"user,omitempty"`
+ Password string `json:"password,omitempty"`
+}
+
+type autoSolverSignupConfigJSON struct {
+ Name string `json:"name,omitempty"`
+ Email string `json:"email,omitempty"`
+ Password string `json:"password,omitempty"`
+}
+
+type autoSolverFormConfigJSON struct {
+ Field1 string `json:"field1,omitempty"`
+ Field2 string `json:"field2,omitempty"`
+ Email string `json:"email,omitempty"`
+}
diff --git a/internal/config/config_file_legacy.go b/internal/config/config_file_legacy.go
new file mode 100644
index 00000000..5030955c
--- /dev/null
+++ b/internal/config/config_file_legacy.go
@@ -0,0 +1,107 @@
+package config
+
+import (
+ "encoding/json"
+ "path/filepath"
+ "strings"
+)
+
+// legacyFileConfig is the old flat structure for backward compatibility.
+type legacyFileConfig struct {
+ Port string `json:"port"`
+ InstancePortStart *int `json:"instancePortStart,omitempty"`
+ InstancePortEnd *int `json:"instancePortEnd,omitempty"`
+ Token string `json:"token,omitempty"`
+ AllowEvaluate *bool `json:"allowEvaluate,omitempty"`
+ AllowMacro *bool `json:"allowMacro,omitempty"`
+ AllowScreencast *bool `json:"allowScreencast,omitempty"`
+ AllowDownload *bool `json:"allowDownload,omitempty"`
+ AllowUpload *bool `json:"allowUpload,omitempty"`
+ AllowClipboard *bool `json:"allowClipboard,omitempty"`
+ StateDir string `json:"stateDir"`
+ ProfileDir string `json:"profileDir"`
+ Headless *bool `json:"headless,omitempty"`
+ NoRestore bool `json:"noRestore"`
+ MaxTabs *int `json:"maxTabs,omitempty"`
+ TimeoutSec int `json:"timeoutSec,omitempty"`
+ NavigateSec int `json:"navigateSec,omitempty"`
+}
+
+// convertLegacyConfig converts flat config to nested structure.
+func convertLegacyConfig(lc *legacyFileConfig) *FileConfig {
+ fc := &FileConfig{}
+
+ fc.Server.Port = lc.Port
+ fc.Server.Token = lc.Token
+ fc.Server.StateDir = lc.StateDir
+
+ if lc.Headless != nil {
+ if *lc.Headless {
+ fc.InstanceDefaults.Mode = "headless"
+ } else {
+ fc.InstanceDefaults.Mode = "headed"
+ }
+ }
+ fc.InstanceDefaults.MaxTabs = lc.MaxTabs
+ if lc.NoRestore {
+ b := true
+ fc.InstanceDefaults.NoRestore = &b
+ }
+
+ if lc.ProfileDir != "" {
+ fc.Profiles.BaseDir = filepath.Dir(lc.ProfileDir)
+ fc.Profiles.DefaultProfile = filepath.Base(lc.ProfileDir)
+ }
+
+ fc.Security.AllowEvaluate = lc.AllowEvaluate
+ fc.Security.AllowMacro = lc.AllowMacro
+ fc.Security.AllowScreencast = lc.AllowScreencast
+ fc.Security.AllowDownload = lc.AllowDownload
+ fc.Security.AllowUpload = lc.AllowUpload
+ fc.Security.AllowClipboard = lc.AllowClipboard
+
+ fc.Timeouts.ActionSec = lc.TimeoutSec
+ fc.Timeouts.NavigateSec = lc.NavigateSec
+
+ fc.MultiInstance.InstancePortStart = lc.InstancePortStart
+ fc.MultiInstance.InstancePortEnd = lc.InstancePortEnd
+
+ return fc
+}
+
+// isLegacyConfig detects if JSON is flat (legacy) or nested (new).
+func isLegacyConfig(data []byte) bool {
+ var probe map[string]json.RawMessage
+ if err := json.Unmarshal(data, &probe); err != nil {
+ return false
+ }
+
+ newKeys := []string{"server", "browser", "instanceDefaults", "profiles", "multiInstance", "security", "attach", "timeouts", "sessions"}
+ for _, key := range newKeys {
+ if _, has := probe[key]; has {
+ return false
+ }
+ }
+
+ if _, hasPort := probe["port"]; hasPort {
+ return true
+ }
+ if _, hasHeadless := probe["headless"]; hasHeadless {
+ return true
+ }
+
+ return false
+}
+
+func modeToHeadless(mode string, fallback bool) bool {
+ switch strings.ToLower(strings.TrimSpace(mode)) {
+ case "":
+ return fallback
+ case "headless":
+ return true
+ case "headed":
+ return false
+ default:
+ return fallback
+ }
+}
diff --git a/internal/config/config_file_marshal.go b/internal/config/config_file_marshal.go
new file mode 100644
index 00000000..2baac6c5
--- /dev/null
+++ b/internal/config/config_file_marshal.go
@@ -0,0 +1,447 @@
+package config
+
+import (
+ "encoding/json"
+ "time"
+)
+
+func copyStringSlice(items []string) []string {
+ if items == nil {
+ return []string{}
+ }
+ if len(items) == 0 {
+ return []string{}
+ }
+ return append([]string(nil), items...)
+}
+
+func intPtrIfPositive(v int) *int {
+ if v <= 0 {
+ return nil
+ }
+ n := v
+ return &n
+}
+
+// tabPolicyDefaultsFromRuntime emits a TabPolicyDefaults block when the runtime
+// config carries any non-default tab-policy setting (lifecycle, close delay, or
+// restore). Returns nil for a fully vanilla config so round-tripping doesn't
+// introduce a noisy tabPolicy block.
+func tabPolicyDefaultsFromRuntime(cfg *RuntimeConfig) *TabPolicyDefaults {
+ if cfg == nil {
+ return nil
+ }
+ hasLifecycle := cfg.TabLifecyclePolicy != "" &&
+ (cfg.TabLifecyclePolicy != "close_idle" || cfg.TabCloseDelay != 5*time.Minute)
+ hasRestore := cfg.TabRestore
+ if !hasLifecycle && !hasRestore {
+ return nil
+ }
+ out := &TabPolicyDefaults{}
+ if hasLifecycle {
+ out.Lifecycle = cfg.TabLifecyclePolicy
+ if cfg.TabLifecyclePolicy == "close_idle" && cfg.TabCloseDelay > 0 {
+ sec := int(cfg.TabCloseDelay / time.Second)
+ out.CloseDelaySec = &sec
+ }
+ }
+ if hasRestore {
+ v := cfg.TabRestore
+ out.Restore = &v
+ }
+ return out
+}
+
+func (fc FileConfig) MarshalJSON() ([]byte, error) {
+ return json.Marshal(fileConfigJSON{
+ ConfigVersion: fc.ConfigVersion,
+ Server: serverConfigJSON{
+ Port: fc.Server.Port,
+ Bind: fc.Server.Bind,
+ Token: fc.Server.Token,
+ StateDir: fc.Server.StateDir,
+ Engine: fc.Server.Engine,
+ NetworkBufferSize: fc.Server.NetworkBufferSize,
+ TrustProxyHeaders: fc.Server.TrustProxyHeaders,
+ CookieSecure: fc.Server.CookieSecure,
+ },
+ Browser: browserConfigJSON{
+ ChromeVersion: fc.Browser.ChromeVersion,
+ ChromeBinary: fc.Browser.ChromeBinary,
+ ChromeDebugPort: fc.Browser.ChromeDebugPort,
+ ChromeExtraFlags: fc.Browser.ChromeExtraFlags,
+ ExtensionPaths: copyStringSlice(fc.Browser.ExtensionPaths),
+ },
+ InstanceDefaults: instanceDefaultsConfigJSON{
+ Mode: fc.InstanceDefaults.Mode,
+ NoRestore: fc.InstanceDefaults.NoRestore,
+ Timezone: fc.InstanceDefaults.Timezone,
+ BlockImages: fc.InstanceDefaults.BlockImages,
+ BlockMedia: fc.InstanceDefaults.BlockMedia,
+ BlockAds: fc.InstanceDefaults.BlockAds,
+ MaxTabs: fc.InstanceDefaults.MaxTabs,
+ MaxParallelTabs: fc.InstanceDefaults.MaxParallelTabs,
+ UserAgent: fc.InstanceDefaults.UserAgent,
+ NoAnimations: fc.InstanceDefaults.NoAnimations,
+ StealthLevel: fc.InstanceDefaults.StealthLevel,
+ TabEvictionPolicy: fc.InstanceDefaults.TabEvictionPolicy,
+ TabPolicy: fc.InstanceDefaults.TabPolicy,
+ },
+ Security: securityConfigJSON{
+ AllowEvaluate: fc.Security.AllowEvaluate,
+ AllowMacro: fc.Security.AllowMacro,
+ AllowScreencast: fc.Security.AllowScreencast,
+ AllowDownload: fc.Security.AllowDownload,
+ AllowedDomains: effectiveSecurityAllowedDomains(fc.Security),
+ DownloadAllowedDomains: copyStringSlice(fc.Security.DownloadAllowedDomains),
+ DownloadMaxBytes: fc.Security.DownloadMaxBytes,
+ AllowUpload: fc.Security.AllowUpload,
+ AllowClipboard: fc.Security.AllowClipboard,
+ AllowStateExport: fc.Security.AllowStateExport,
+ StateEncryptionKey: fc.Security.StateEncryptionKey,
+ EnableActionGuards: fc.Security.EnableActionGuards,
+ UploadMaxRequestBytes: fc.Security.UploadMaxRequestBytes,
+ UploadMaxFiles: fc.Security.UploadMaxFiles,
+ UploadMaxFileBytes: fc.Security.UploadMaxFileBytes,
+ UploadMaxTotalBytes: fc.Security.UploadMaxTotalBytes,
+ MaxRedirects: fc.Security.MaxRedirects,
+ TrustedProxyCIDRs: copyStringSlice(fc.Security.TrustedProxyCIDRs),
+ TrustedResolveCIDRs: copyStringSlice(fc.Security.TrustedResolveCIDRs),
+ Attach: attachJSON{
+ Enabled: fc.Security.Attach.Enabled,
+ AllowHosts: copyStringSlice(fc.Security.Attach.AllowHosts),
+ AllowSchemes: copyStringSlice(fc.Security.Attach.AllowSchemes),
+ },
+ IDPI: idpiConfigJSON{
+ Enabled: fc.Security.IDPI.Enabled,
+ StrictMode: fc.Security.IDPI.StrictMode,
+ ScanContent: fc.Security.IDPI.ScanContent,
+ WrapContent: fc.Security.IDPI.WrapContent,
+ CustomPatterns: copyStringSlice(fc.Security.IDPI.CustomPatterns),
+ ScanTimeoutSec: fc.Security.IDPI.ScanTimeoutSec,
+ ShieldThreshold: fc.Security.IDPI.ShieldThreshold,
+ },
+ },
+ Profiles: profilesConfigJSON{
+ BaseDir: fc.Profiles.BaseDir,
+ DefaultProfile: fc.Profiles.DefaultProfile,
+ },
+ MultiInstance: multiInstanceConfigJSON{
+ Strategy: fc.MultiInstance.Strategy,
+ AllocationPolicy: fc.MultiInstance.AllocationPolicy,
+ InstancePortStart: fc.MultiInstance.InstancePortStart,
+ InstancePortEnd: fc.MultiInstance.InstancePortEnd,
+ Restart: multiInstanceRestartJSON{
+ MaxRestarts: fc.MultiInstance.Restart.MaxRestarts,
+ InitBackoffSec: fc.MultiInstance.Restart.InitBackoffSec,
+ MaxBackoffSec: fc.MultiInstance.Restart.MaxBackoffSec,
+ StableAfterSec: fc.MultiInstance.Restart.StableAfterSec,
+ },
+ },
+ Timeouts: timeoutsConfigJSON{
+ ActionSec: fc.Timeouts.ActionSec,
+ NavigateSec: fc.Timeouts.NavigateSec,
+ ShutdownSec: fc.Timeouts.ShutdownSec,
+ WaitNavMs: fc.Timeouts.WaitNavMs,
+ },
+ Scheduler: schedulerFileConfigJSON{
+ Enabled: fc.Scheduler.Enabled,
+ Strategy: fc.Scheduler.Strategy,
+ MaxQueueSize: fc.Scheduler.MaxQueueSize,
+ MaxPerAgent: fc.Scheduler.MaxPerAgent,
+ MaxInflight: fc.Scheduler.MaxInflight,
+ MaxPerAgentFlight: fc.Scheduler.MaxPerAgentFlight,
+ ResultTTLSec: fc.Scheduler.ResultTTLSec,
+ WorkerCount: fc.Scheduler.WorkerCount,
+ },
+ Observability: observabilityFileConfigJSON{
+ Activity: activityConfigJSON{
+ Enabled: fc.Observability.Activity.Enabled,
+ SessionIdleSec: fc.Observability.Activity.SessionIdleSec,
+ RetentionDays: fc.Observability.Activity.RetentionDays,
+ StateDir: fc.Observability.Activity.StateDir,
+ Events: activityEventsConfigJSON{
+ Dashboard: fc.Observability.Activity.Events.Dashboard,
+ Server: fc.Observability.Activity.Events.Server,
+ Bridge: fc.Observability.Activity.Events.Bridge,
+ Orchestrator: fc.Observability.Activity.Events.Orchestrator,
+ Scheduler: fc.Observability.Activity.Events.Scheduler,
+ MCP: fc.Observability.Activity.Events.MCP,
+ Other: fc.Observability.Activity.Events.Other,
+ },
+ },
+ },
+ Sessions: sessionsFileConfigJSON{
+ Dashboard: dashboardSessionConfigJSON{
+ Persist: fc.Sessions.Dashboard.Persist,
+ IdleTimeoutSec: fc.Sessions.Dashboard.IdleTimeoutSec,
+ MaxLifetimeSec: fc.Sessions.Dashboard.MaxLifetimeSec,
+ ElevationWindowSec: fc.Sessions.Dashboard.ElevationWindowSec,
+ PersistElevationAcrossRestart: fc.Sessions.Dashboard.PersistElevationAcrossRestart,
+ RequireElevation: fc.Sessions.Dashboard.RequireElevation,
+ },
+ },
+ AutoSolver: autoSolverFileConfigJSON{
+ Enabled: fc.AutoSolver.Enabled,
+ AutoTrigger: fc.AutoSolver.AutoTrigger,
+ TriggerOnNavigate: fc.AutoSolver.TriggerOnNavigate,
+ TriggerOnAction: fc.AutoSolver.TriggerOnAction,
+ MaxAttempts: fc.AutoSolver.MaxAttempts,
+ SolverTimeoutSec: fc.AutoSolver.SolverTimeoutSec,
+ RetryBaseDelayMs: fc.AutoSolver.RetryBaseDelayMs,
+ RetryMaxDelayMs: fc.AutoSolver.RetryMaxDelayMs,
+ Solvers: copyStringSlice(fc.AutoSolver.Solvers),
+ LLMProvider: fc.AutoSolver.LLMProvider,
+ LLMFallback: fc.AutoSolver.LLMFallback,
+ External: autoSolverExtConfigJSON{
+ CapsolverKey: fc.AutoSolver.External.CapsolverKey,
+ TwoCaptchaKey: fc.AutoSolver.External.TwoCaptchaKey,
+ },
+ Credentials: autoSolverCredentialsConfigJSON{
+ Login: autoSolverLoginConfigJSON{
+ User: fc.AutoSolver.Credentials.Login.User,
+ Password: fc.AutoSolver.Credentials.Login.Password,
+ },
+ Signup: autoSolverSignupConfigJSON{
+ Name: fc.AutoSolver.Credentials.Signup.Name,
+ Email: fc.AutoSolver.Credentials.Signup.Email,
+ Password: fc.AutoSolver.Credentials.Signup.Password,
+ },
+ Form: autoSolverFormConfigJSON{
+ Field1: fc.AutoSolver.Credentials.Form.Field1,
+ Field2: fc.AutoSolver.Credentials.Form.Field2,
+ Email: fc.AutoSolver.Credentials.Form.Email,
+ },
+ },
+ },
+ })
+}
+
+func (fc *FileConfig) UnmarshalJSON(data []byte) error {
+ type rawFileConfig FileConfig
+ tmp := rawFileConfig(*fc)
+ if err := json.Unmarshal(data, &tmp); err != nil {
+ return err
+ }
+ *fc = FileConfig(tmp)
+ NormalizeFileConfigAliasesFromJSON(fc, data)
+ return nil
+}
+
+// FileConfigFromRuntime converts the effective runtime configuration back into a
+// nested file configuration shape.
+func FileConfigFromRuntime(cfg *RuntimeConfig) FileConfig {
+ if cfg == nil {
+ return DefaultFileConfig()
+ }
+
+ noRestore := cfg.NoRestore
+ blockImages := cfg.BlockImages
+ blockMedia := cfg.BlockMedia
+ blockAds := cfg.BlockAds
+ maxTabs := cfg.MaxTabs
+ maxParallelTabs := cfg.MaxParallelTabs
+ noAnimations := cfg.NoAnimations
+ allowEvaluate := cfg.AllowEvaluate
+ allowMacro := cfg.AllowMacro
+ allowScreencast := cfg.AllowScreencast
+ allowDownload := cfg.AllowDownload
+ downloadAllowedDomains := copyStringSlice(cfg.DownloadAllowedDomains)
+ downloadMaxBytes := cfg.EffectiveDownloadMaxBytes()
+ allowUpload := cfg.AllowUpload
+ allowClipboard := cfg.AllowClipboard
+ allowStateExport := cfg.AllowStateExport
+ enableActionGuards := cfg.EnableActionGuards
+ uploadMaxRequestBytes := cfg.EffectiveUploadMaxRequestBytes()
+ uploadMaxFiles := cfg.EffectiveUploadMaxFiles()
+ uploadMaxFileBytes := cfg.EffectiveUploadMaxFileBytes()
+ uploadMaxTotalBytes := cfg.EffectiveUploadMaxTotalBytes()
+ maxRedirects := cfg.MaxRedirects
+ attachEnabled := cfg.AttachEnabled
+ start := cfg.InstancePortStart
+ end := cfg.InstancePortEnd
+ restartMaxRestarts := cfg.RestartMaxRestarts
+ restartInitBackoffSec := int(cfg.RestartInitBackoff / time.Second)
+ restartMaxBackoffSec := int(cfg.RestartMaxBackoff / time.Second)
+ restartStableAfterSec := int(cfg.RestartStableAfter / time.Second)
+ activityEnabled := cfg.Observability.Activity.Enabled
+ activitySessionIdleSec := cfg.Observability.Activity.SessionIdleSec
+ activityRetentionDays := cfg.Observability.Activity.RetentionDays
+ activityDashboardEvents := cfg.Observability.Activity.Events.Dashboard
+ activityServerEvents := cfg.Observability.Activity.Events.Server
+ activityBridgeEvents := cfg.Observability.Activity.Events.Bridge
+ activityOrchestratorEvents := cfg.Observability.Activity.Events.Orchestrator
+ activitySchedulerEvents := cfg.Observability.Activity.Events.Scheduler
+ activityMCPEvents := cfg.Observability.Activity.Events.MCP
+ activityOtherEvents := cfg.Observability.Activity.Events.Other
+ dashboardSessionPersist := cfg.Sessions.Dashboard.Persist
+ dashboardSessionIdleSec := int(cfg.Sessions.Dashboard.IdleTimeout / time.Second)
+ dashboardSessionMaxLifetimeSec := int(cfg.Sessions.Dashboard.MaxLifetime / time.Second)
+ dashboardSessionElevationWindowSec := int(cfg.Sessions.Dashboard.ElevationWindow / time.Second)
+ dashboardSessionPersistElevationAcrossRestart := cfg.Sessions.Dashboard.PersistElevationAcrossRestart
+ dashboardSessionRequireElevation := cfg.Sessions.Dashboard.RequireElevation
+ autoSolverEnabled := cfg.AutoSolver.Enabled
+ autoSolverAutoTrigger := cfg.AutoSolver.AutoTrigger
+ autoSolverTriggerOnNavigate := cfg.AutoSolver.TriggerOnNavigate
+ autoSolverTriggerOnAction := cfg.AutoSolver.TriggerOnAction
+ autoSolverMaxAttempts := cfg.AutoSolver.MaxAttempts
+ autoSolverSolverTimeoutSec := cfg.AutoSolver.SolverTimeoutSec
+ autoSolverRetryBaseDelayMs := cfg.AutoSolver.RetryBaseDelayMs
+ autoSolverRetryMaxDelayMs := cfg.AutoSolver.RetryMaxDelayMs
+ autoSolverLLMFallback := cfg.AutoSolver.LLMFallback
+
+ mode := "headless"
+ if !cfg.Headless {
+ mode = "headed"
+ }
+
+ var netBufSize *int
+ if cfg.NetworkBufferSize > 0 {
+ v := cfg.NetworkBufferSize
+ netBufSize = &v
+ }
+
+ fc := FileConfig{
+ Server: ServerConfig{
+ Port: cfg.Port,
+ Bind: cfg.Bind,
+ Token: cfg.Token,
+ StateDir: cfg.StateDir,
+ Engine: cfg.Engine,
+ NetworkBufferSize: netBufSize,
+ TrustProxyHeaders: &cfg.TrustProxyHeaders,
+ CookieSecure: cfg.CookieSecure,
+ },
+ Browser: BrowserConfig{
+ ChromeVersion: cfg.ChromeVersion,
+ ChromeBinary: cfg.ChromeBinary,
+ ChromeDebugPort: intPtrIfPositive(cfg.ChromeDebugPort),
+ ChromeExtraFlags: cfg.ChromeExtraFlags,
+ ExtensionPaths: append([]string(nil), cfg.ExtensionPaths...),
+ },
+ InstanceDefaults: InstanceDefaultsConfig{
+ Mode: mode,
+ NoRestore: &noRestore,
+ Timezone: cfg.Timezone,
+ BlockImages: &blockImages,
+ BlockMedia: &blockMedia,
+ BlockAds: &blockAds,
+ MaxTabs: &maxTabs,
+ MaxParallelTabs: &maxParallelTabs,
+ UserAgent: cfg.UserAgent,
+ NoAnimations: &noAnimations,
+ StealthLevel: cfg.StealthLevel,
+ TabEvictionPolicy: cfg.TabEvictionPolicy,
+ TabPolicy: tabPolicyDefaultsFromRuntime(cfg),
+ },
+ Security: SecurityConfig{
+ AllowEvaluate: &allowEvaluate,
+ AllowMacro: &allowMacro,
+ AllowScreencast: &allowScreencast,
+ AllowDownload: &allowDownload,
+ AllowedDomains: append([]string(nil), cfg.AllowedDomains...),
+ DownloadAllowedDomains: downloadAllowedDomains,
+ DownloadMaxBytes: &downloadMaxBytes,
+ AllowUpload: &allowUpload,
+ AllowClipboard: &allowClipboard,
+ AllowStateExport: &allowStateExport,
+ EnableActionGuards: &enableActionGuards,
+ UploadMaxRequestBytes: &uploadMaxRequestBytes,
+ UploadMaxFiles: &uploadMaxFiles,
+ UploadMaxFileBytes: &uploadMaxFileBytes,
+ UploadMaxTotalBytes: &uploadMaxTotalBytes,
+ MaxRedirects: &maxRedirects,
+ TrustedProxyCIDRs: append([]string(nil), cfg.TrustedProxyCIDRs...),
+ TrustedResolveCIDRs: append([]string(nil), cfg.TrustedResolveCIDRs...),
+ Attach: AttachConfig{
+ Enabled: &attachEnabled,
+ AllowHosts: append([]string(nil), cfg.AttachAllowHosts...),
+ AllowSchemes: append([]string(nil), cfg.AttachAllowSchemes...),
+ },
+ IDPI: cfg.IDPI,
+ },
+ Profiles: ProfilesConfig{
+ BaseDir: cfg.ProfilesBaseDir,
+ DefaultProfile: cfg.DefaultProfile,
+ },
+ MultiInstance: MultiInstanceConfig{
+ Strategy: cfg.Strategy,
+ AllocationPolicy: cfg.AllocationPolicy,
+ InstancePortStart: &start,
+ InstancePortEnd: &end,
+ Restart: MultiInstanceRestartConfig{
+ MaxRestarts: &restartMaxRestarts,
+ InitBackoffSec: &restartInitBackoffSec,
+ MaxBackoffSec: &restartMaxBackoffSec,
+ StableAfterSec: &restartStableAfterSec,
+ },
+ },
+ Timeouts: TimeoutsConfig{
+ ActionSec: int(cfg.ActionTimeout / time.Second),
+ NavigateSec: int(cfg.NavigateTimeout / time.Second),
+ ShutdownSec: int(cfg.ShutdownTimeout / time.Second),
+ WaitNavMs: int(cfg.WaitNavDelay / time.Millisecond),
+ },
+ Observability: ObservabilityFileConfig{
+ Activity: ActivityFileConfig{
+ Enabled: &activityEnabled,
+ SessionIdleSec: &activitySessionIdleSec,
+ RetentionDays: &activityRetentionDays,
+ Events: ActivityEventsFileConfig{
+ Dashboard: &activityDashboardEvents,
+ Server: &activityServerEvents,
+ Bridge: &activityBridgeEvents,
+ Orchestrator: &activityOrchestratorEvents,
+ Scheduler: &activitySchedulerEvents,
+ MCP: &activityMCPEvents,
+ Other: &activityOtherEvents,
+ },
+ },
+ },
+ Sessions: SessionsFileConfig{
+ Dashboard: DashboardSessionFileConfig{
+ Persist: &dashboardSessionPersist,
+ IdleTimeoutSec: &dashboardSessionIdleSec,
+ MaxLifetimeSec: &dashboardSessionMaxLifetimeSec,
+ ElevationWindowSec: &dashboardSessionElevationWindowSec,
+ PersistElevationAcrossRestart: &dashboardSessionPersistElevationAcrossRestart,
+ RequireElevation: &dashboardSessionRequireElevation,
+ },
+ },
+ AutoSolver: AutoSolverFileConfig{
+ Enabled: &autoSolverEnabled,
+ AutoTrigger: &autoSolverAutoTrigger,
+ TriggerOnNavigate: &autoSolverTriggerOnNavigate,
+ TriggerOnAction: &autoSolverTriggerOnAction,
+ MaxAttempts: &autoSolverMaxAttempts,
+ SolverTimeoutSec: &autoSolverSolverTimeoutSec,
+ RetryBaseDelayMs: &autoSolverRetryBaseDelayMs,
+ RetryMaxDelayMs: &autoSolverRetryMaxDelayMs,
+ Solvers: copyStringSlice(cfg.AutoSolver.Solvers),
+ LLMProvider: cfg.AutoSolver.LLMProvider,
+ LLMFallback: &autoSolverLLMFallback,
+ External: AutoSolverExtConf{
+ CapsolverKey: cfg.AutoSolver.CapsolverKey,
+ TwoCaptchaKey: cfg.AutoSolver.TwoCaptchaKey,
+ },
+ Credentials: AutoSolverCredentialsConf{
+ Login: AutoSolverLoginConf{
+ User: cfg.AutoSolver.Credentials.Login.User,
+ Password: cfg.AutoSolver.Credentials.Login.Password,
+ },
+ Signup: AutoSolverSignupConf{
+ Name: cfg.AutoSolver.Credentials.Signup.Name,
+ Email: cfg.AutoSolver.Credentials.Signup.Email,
+ Password: cfg.AutoSolver.Credentials.Signup.Password,
+ },
+ Form: AutoSolverFormConf{
+ Field1: cfg.AutoSolver.Credentials.Form.Field1,
+ Field2: cfg.AutoSolver.Credentials.Form.Field2,
+ Email: cfg.AutoSolver.Credentials.Form.Email,
+ },
+ },
+ },
+ }
+
+ return fc
+}
diff --git a/internal/dashboard/activity_ingest.go b/internal/dashboard/activity_ingest.go
new file mode 100644
index 00000000..301c0829
--- /dev/null
+++ b/internal/dashboard/activity_ingest.go
@@ -0,0 +1,233 @@
+package dashboard
+
+import (
+ "strings"
+ "time"
+
+ "github.com/pinchtab/pinchtab/internal/activity"
+ apiTypes "github.com/pinchtab/pinchtab/internal/api/types"
+)
+
+const persistedAgentBootstrapLimit = 1000
+
+// RecordActivityEvent converts a backend activity record into a live tool-call event.
+func (d *Dashboard) RecordActivityEvent(evt activity.Event) {
+ d.recordEvents([]apiTypes.ActivityEvent{activityEventToLiveEvent(evt)})
+}
+
+func activityEventToLiveEvent(evt activity.Event) apiTypes.ActivityEvent {
+ details := map[string]any{
+ "status": evt.Status,
+ "durationMs": evt.DurationMs,
+ }
+ if evt.Source != "" {
+ details["source"] = evt.Source
+ }
+ if evt.RequestID != "" {
+ details["requestId"] = evt.RequestID
+ }
+ if evt.SessionID != "" {
+ details["sessionId"] = evt.SessionID
+ }
+ if evt.InstanceID != "" {
+ details["instanceId"] = evt.InstanceID
+ }
+ if evt.ProfileID != "" {
+ details["profileId"] = evt.ProfileID
+ }
+ if evt.ProfileName != "" {
+ details["profileName"] = evt.ProfileName
+ }
+ if evt.TabID != "" {
+ details["tabId"] = evt.TabID
+ }
+ if evt.URL != "" {
+ details["url"] = evt.URL
+ }
+ if evt.Ref != "" {
+ details["ref"] = evt.Ref
+ }
+ if evt.Engine != "" {
+ details["engine"] = evt.Engine
+ }
+ if evt.Action != "" {
+ details["action"] = evt.Action
+ }
+
+ return apiTypes.ActivityEvent{
+ ID: evt.RequestID,
+ AgentID: agentIDOrAnonymous(evt.AgentID),
+ Channel: "tool_call",
+ Type: classifyActivityType(evt),
+ Method: evt.Method,
+ Path: evt.Path,
+ Timestamp: evt.Timestamp,
+ Details: details,
+ }
+}
+
+// RecordEvent records an activity event, updates the live agent summary, and broadcasts to SSE subscribers.
+func (d *Dashboard) RecordEvent(evt apiTypes.ActivityEvent) {
+ d.recordEvents([]apiTypes.ActivityEvent{evt})
+}
+
+func (d *Dashboard) upsertAgentLocked(evt apiTypes.ActivityEvent) {
+ agentID := agentIDOrAnonymous(evt.AgentID)
+ agent, ok := d.agents[agentID]
+ if !ok {
+ agent = &apiTypes.Agent{
+ ID: agentID,
+ Name: agentID,
+ ConnectedAt: evt.Timestamp,
+ }
+ d.agents[agentID] = agent
+ }
+ agent.LastActivity = evt.Timestamp
+ agent.RequestCount++
+}
+
+// IngestPersistedAgentActivity loads new agent-tagged requests from the shared
+// activity store into the live dashboard cache.
+func (d *Dashboard) IngestPersistedAgentActivity(rec activity.Recorder, since time.Time) (time.Time, error) {
+ if d == nil || rec == nil || !rec.Enabled() {
+ return since, nil
+ }
+
+ events, err := rec.Query(activity.Filter{
+ Source: "client",
+ Since: since,
+ Limit: persistedAgentBootstrapLimit,
+ })
+ if err != nil {
+ return since, err
+ }
+
+ latest := since
+ batch := make([]apiTypes.ActivityEvent, 0, len(events))
+ for _, evt := range events {
+ if evt.Timestamp.After(latest) {
+ latest = evt.Timestamp
+ }
+ if !shouldTrackPersistedAgentActivity(evt) {
+ continue
+ }
+ batch = append(batch, activityEventToLiveEvent(evt))
+ }
+ d.recordEvents(batch)
+
+ return latest, nil
+}
+
+// LoadPersistedAgentActivity rebuilds the in-memory agent summaries and recent
+// tool-call history from the persisted activity log on server startup.
+func (d *Dashboard) LoadPersistedAgentActivity(rec activity.Recorder) error {
+ _, err := d.IngestPersistedAgentActivity(rec, time.Time{})
+ return err
+}
+
+func shouldTrackPersistedAgentActivity(evt activity.Event) bool {
+ return evt.Source == "client"
+}
+
+func (d *Dashboard) rememberEventIDLocked(id string) {
+ if id == "" {
+ return
+ }
+ d.seenEventIDs[id] = struct{}{}
+ d.seenEventLog = append(d.seenEventLog, id)
+ if len(d.seenEventLog) <= d.maxSeenIDs {
+ return
+ }
+ evicted := d.seenEventLog[0]
+ d.seenEventLog = d.seenEventLog[1:]
+ delete(d.seenEventIDs, evicted)
+}
+
+func normalizeEvent(evt apiTypes.ActivityEvent) apiTypes.ActivityEvent {
+ if evt.ID == "" {
+ evt.ID = generateID()
+ }
+ if evt.Timestamp.IsZero() {
+ evt.Timestamp = time.Now().UTC()
+ }
+ if evt.Channel == "" {
+ evt.Channel = "tool_call"
+ }
+ if evt.AgentID == "" {
+ evt.AgentID = "anonymous"
+ }
+ return evt
+}
+
+func (d *Dashboard) recordEvents(events []apiTypes.ActivityEvent) {
+ if d == nil || len(events) == 0 {
+ return
+ }
+
+ d.mu.Lock()
+ broadcast := make([]apiTypes.ActivityEvent, 0, len(events))
+ for _, raw := range events {
+ evt := normalizeEvent(raw)
+ if _, ok := d.seenEventIDs[evt.ID]; ok {
+ continue
+ }
+ d.rememberEventIDLocked(evt.ID)
+ d.upsertAgentLocked(evt)
+ if len(d.recentEvents) >= d.maxEvents {
+ copy(d.recentEvents, d.recentEvents[1:])
+ d.recentEvents[len(d.recentEvents)-1] = evt
+ } else {
+ d.recentEvents = append(d.recentEvents, evt)
+ }
+ broadcast = append(broadcast, evt)
+ }
+
+ chans := make([]chan apiTypes.ActivityEvent, 0, len(d.activityConns))
+ for ch := range d.activityConns {
+ chans = append(chans, ch)
+ }
+ d.mu.Unlock()
+
+ for _, evt := range broadcast {
+ for _, ch := range chans {
+ select {
+ case ch <- evt:
+ default:
+ }
+ }
+ }
+}
+
+func classifyActivityType(evt activity.Event) string {
+ if evt.Action != "" {
+ switch evt.Action {
+ case "navigate", "snapshot", "screenshot", "text":
+ return evt.Action
+ default:
+ return "action"
+ }
+ }
+ path := evt.Path
+ switch {
+ case strings.Contains(path, "/navigate"):
+ return "navigate"
+ case strings.Contains(path, "/snapshot"):
+ return "snapshot"
+ case strings.Contains(path, "/screenshot"):
+ return "screenshot"
+ case strings.Contains(path, "/text"):
+ return "text"
+ case strings.Contains(path, "/action"):
+ return "action"
+ default:
+ return "other"
+ }
+}
+
+func agentIDOrAnonymous(agentID string) string {
+ agentID = strings.TrimSpace(agentID)
+ if agentID == "" {
+ return "anonymous"
+ }
+ return agentID
+}
diff --git a/internal/dashboard/dashboard.go b/internal/dashboard/dashboard.go
index ceaaa8ca..838f7943 100644
--- a/internal/dashboard/dashboard.go
+++ b/internal/dashboard/dashboard.go
@@ -2,23 +2,14 @@ package dashboard
import (
"context"
- "crypto/rand"
"embed"
- "encoding/hex"
- "encoding/json"
- "fmt"
- "io/fs"
- "net/http"
"os"
"sort"
- "strings"
"sync"
"time"
- "github.com/pinchtab/pinchtab/internal/activity"
apiTypes "github.com/pinchtab/pinchtab/internal/api/types"
"github.com/pinchtab/pinchtab/internal/bridge"
- "github.com/pinchtab/pinchtab/internal/httpx"
)
func envWithFallback(newKey, oldKey string) string {
@@ -69,7 +60,45 @@ type Dashboard struct {
mu sync.RWMutex
}
-const persistedAgentBootstrapLimit = 1000
+func NewDashboard(cfg *DashboardConfig) *Dashboard {
+ c := DashboardConfig{
+ IdleTimeout: 30 * time.Second,
+ DisconnectTimeout: 5 * time.Minute,
+ ReaperInterval: 10 * time.Second,
+ SSEBufferSize: 64,
+ }
+ if cfg != nil {
+ if cfg.IdleTimeout > 0 {
+ c.IdleTimeout = cfg.IdleTimeout
+ }
+ if cfg.DisconnectTimeout > 0 {
+ c.DisconnectTimeout = cfg.DisconnectTimeout
+ }
+ if cfg.ReaperInterval > 0 {
+ c.ReaperInterval = cfg.ReaperInterval
+ }
+ if cfg.SSEBufferSize > 0 {
+ c.SSEBufferSize = cfg.SSEBufferSize
+ }
+ }
+
+ _, cancel := context.WithCancel(context.Background())
+ return &Dashboard{
+ cfg: c,
+ activityConns: make(map[chan apiTypes.ActivityEvent]struct{}),
+ sysConns: make(map[chan SystemEvent]struct{}),
+ cancel: cancel,
+ childAuthToken: envWithFallback("PINCHTAB_TOKEN", "BRIDGE_TOKEN"),
+ agents: make(map[string]*apiTypes.Agent),
+ recentEvents: make([]apiTypes.ActivityEvent, 0, 200),
+ maxEvents: 200,
+ seenEventIDs: make(map[string]struct{}),
+ seenEventLog: make([]string, 0, 2000),
+ maxSeenIDs: 2000,
+ }
+}
+
+func (d *Dashboard) Shutdown() { d.cancel() }
// BroadcastSystemEvent sends a system event to all SSE clients.
func (d *Dashboard) BroadcastSystemEvent(evt SystemEvent) {
@@ -93,82 +122,6 @@ func (d *Dashboard) SetInstanceLister(il InstanceLister) {
d.instances = il
}
-// RecordActivityEvent converts a backend activity record into a live tool-call event.
-func (d *Dashboard) RecordActivityEvent(evt activity.Event) {
- d.recordEvents([]apiTypes.ActivityEvent{activityEventToLiveEvent(evt)})
-}
-
-func activityEventToLiveEvent(evt activity.Event) apiTypes.ActivityEvent {
- details := map[string]any{
- "status": evt.Status,
- "durationMs": evt.DurationMs,
- }
- if evt.Source != "" {
- details["source"] = evt.Source
- }
- if evt.RequestID != "" {
- details["requestId"] = evt.RequestID
- }
- if evt.SessionID != "" {
- details["sessionId"] = evt.SessionID
- }
- if evt.InstanceID != "" {
- details["instanceId"] = evt.InstanceID
- }
- if evt.ProfileID != "" {
- details["profileId"] = evt.ProfileID
- }
- if evt.ProfileName != "" {
- details["profileName"] = evt.ProfileName
- }
- if evt.TabID != "" {
- details["tabId"] = evt.TabID
- }
- if evt.URL != "" {
- details["url"] = evt.URL
- }
- if evt.Ref != "" {
- details["ref"] = evt.Ref
- }
- if evt.Engine != "" {
- details["engine"] = evt.Engine
- }
- if evt.Action != "" {
- details["action"] = evt.Action
- }
-
- return apiTypes.ActivityEvent{
- ID: evt.RequestID,
- AgentID: agentIDOrAnonymous(evt.AgentID),
- Channel: "tool_call",
- Type: classifyActivityType(evt),
- Method: evt.Method,
- Path: evt.Path,
- Timestamp: evt.Timestamp,
- Details: details,
- }
-}
-
-// RecordEvent records an activity event, updates the live agent summary, and broadcasts to SSE subscribers.
-func (d *Dashboard) RecordEvent(evt apiTypes.ActivityEvent) {
- d.recordEvents([]apiTypes.ActivityEvent{evt})
-}
-
-func (d *Dashboard) upsertAgentLocked(evt apiTypes.ActivityEvent) {
- agentID := agentIDOrAnonymous(evt.AgentID)
- agent, ok := d.agents[agentID]
- if !ok {
- agent = &apiTypes.Agent{
- ID: agentID,
- Name: agentID,
- ConnectedAt: evt.Timestamp,
- }
- d.agents[agentID] = agent
- }
- agent.LastActivity = evt.Timestamp
- agent.RequestCount++
-}
-
// Agents returns the current agent summary list ordered by most recent activity.
func (d *Dashboard) Agents() []apiTypes.Agent {
d.mu.RLock()
@@ -226,468 +179,3 @@ func (d *Dashboard) EventsForAgent(agentID, mode string) []apiTypes.ActivityEven
}
return out
}
-
-// IngestPersistedAgentActivity loads new agent-tagged requests from the shared
-// activity store into the live dashboard cache.
-func (d *Dashboard) IngestPersistedAgentActivity(rec activity.Recorder, since time.Time) (time.Time, error) {
- if d == nil || rec == nil || !rec.Enabled() {
- return since, nil
- }
-
- events, err := rec.Query(activity.Filter{
- Source: "client",
- Since: since,
- Limit: persistedAgentBootstrapLimit,
- })
- if err != nil {
- return since, err
- }
-
- latest := since
- batch := make([]apiTypes.ActivityEvent, 0, len(events))
- for _, evt := range events {
- if evt.Timestamp.After(latest) {
- latest = evt.Timestamp
- }
- if !shouldTrackPersistedAgentActivity(evt) {
- continue
- }
- batch = append(batch, activityEventToLiveEvent(evt))
- }
- d.recordEvents(batch)
-
- return latest, nil
-}
-
-// LoadPersistedAgentActivity rebuilds the in-memory agent summaries and recent
-// tool-call history from the persisted activity log on server startup.
-func (d *Dashboard) LoadPersistedAgentActivity(rec activity.Recorder) error {
- _, err := d.IngestPersistedAgentActivity(rec, time.Time{})
- return err
-}
-
-func NewDashboard(cfg *DashboardConfig) *Dashboard {
- c := DashboardConfig{
- IdleTimeout: 30 * time.Second,
- DisconnectTimeout: 5 * time.Minute,
- ReaperInterval: 10 * time.Second,
- SSEBufferSize: 64,
- }
- if cfg != nil {
- if cfg.IdleTimeout > 0 {
- c.IdleTimeout = cfg.IdleTimeout
- }
- if cfg.DisconnectTimeout > 0 {
- c.DisconnectTimeout = cfg.DisconnectTimeout
- }
- if cfg.ReaperInterval > 0 {
- c.ReaperInterval = cfg.ReaperInterval
- }
- if cfg.SSEBufferSize > 0 {
- c.SSEBufferSize = cfg.SSEBufferSize
- }
- }
-
- _, cancel := context.WithCancel(context.Background())
- return &Dashboard{
- cfg: c,
- activityConns: make(map[chan apiTypes.ActivityEvent]struct{}),
- sysConns: make(map[chan SystemEvent]struct{}),
- cancel: cancel,
- childAuthToken: envWithFallback("PINCHTAB_TOKEN", "BRIDGE_TOKEN"),
- agents: make(map[string]*apiTypes.Agent),
- recentEvents: make([]apiTypes.ActivityEvent, 0, 200),
- maxEvents: 200,
- seenEventIDs: make(map[string]struct{}),
- seenEventLog: make([]string, 0, 2000),
- maxSeenIDs: 2000,
- }
-}
-
-func (d *Dashboard) Shutdown() { d.cancel() }
-
-func (d *Dashboard) RegisterHandlers(mux *http.ServeMux) {
- // API endpoints
- mux.HandleFunc("GET /api/events", d.handleSSE)
- mux.HandleFunc("GET /api/agents", d.handleAgents)
- mux.HandleFunc("GET /api/agents/{id}", d.handleAgent)
- mux.HandleFunc("GET /api/agents/{id}/events", d.handleAgentSSE)
- mux.HandleFunc("POST /api/agents/{id}/events", d.handleAgentEventsByID)
-
- // Static files served at /dashboard/
- sub, _ := fs.Sub(dashboardFS, "dashboard")
- fileServer := http.FileServer(http.FS(sub))
-
- // Serve static assets under /dashboard/ with long cache (hashed filenames)
- mux.Handle("GET /dashboard/assets/", http.StripPrefix("/dashboard", d.withLongCache(fileServer)))
- mux.Handle("GET /dashboard/favicon.png", http.StripPrefix("/dashboard", d.withLongCache(fileServer)))
-
- // SPA: serve dashboard.html for /, /login, and /dashboard/*
- mux.Handle("GET /{$}", d.withNoCache(http.HandlerFunc(d.handleDashboardUI)))
- mux.Handle("GET /login", d.withNoCache(http.HandlerFunc(d.handleDashboardUI)))
- mux.Handle("GET /dashboard", d.withNoCache(http.HandlerFunc(d.handleDashboardUI)))
- mux.Handle("GET /dashboard/{path...}", d.withNoCache(http.HandlerFunc(d.handleDashboardUI)))
-}
-
-func (d *Dashboard) handleAgents(w http.ResponseWriter, _ *http.Request) {
- httpx.JSON(w, http.StatusOK, d.Agents())
-}
-
-func (d *Dashboard) handleAgent(w http.ResponseWriter, r *http.Request) {
- agentID := r.PathValue("id")
- agent, ok := d.Agent(agentID)
- if !ok {
- httpx.ErrorCode(w, http.StatusNotFound, "agent_not_found", "agent not found", false, nil)
- return
- }
-
- mode := strings.TrimSpace(r.URL.Query().Get("mode"))
- if mode == "" {
- mode = "both"
- }
- if mode != "tool_calls" && mode != "progress" && mode != "both" {
- httpx.ErrorCode(w, http.StatusBadRequest, "bad_mode", "mode must be tool_calls, progress, or both", false, nil)
- return
- }
-
- httpx.JSON(w, http.StatusOK, apiTypes.AgentDetail{
- Agent: agent,
- Events: d.EventsForAgent(agentID, mode),
- })
-}
-
-func (d *Dashboard) handleAgentEventsByID(w http.ResponseWriter, r *http.Request) {
- pathAgentID := agentIDOrAnonymous(r.PathValue("id"))
- evt, ok := d.decodeProgressEvent(w, r, pathAgentID)
- if !ok {
- return
- }
- if evt.AgentID != "" && evt.AgentID != pathAgentID {
- httpx.ErrorCode(w, http.StatusBadRequest, "agent_mismatch", "agentId must match route parameter", false, nil)
- return
- }
- evt.AgentID = pathAgentID
- d.RecordEvent(evt)
- httpx.JSON(w, http.StatusCreated, map[string]string{"status": "ok", "id": evt.ID})
-}
-
-func (d *Dashboard) decodeProgressEvent(w http.ResponseWriter, r *http.Request, fallbackAgentID string) (apiTypes.ActivityEvent, bool) {
- var evt apiTypes.ActivityEvent
- if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 8<<10)).Decode(&evt); err != nil {
- httpx.ErrorCode(w, http.StatusBadRequest, "bad_agent_event", "invalid activity event payload", false, nil)
- return apiTypes.ActivityEvent{}, false
- }
-
- evt.AgentID = strings.TrimSpace(evt.AgentID)
- if evt.AgentID == "" {
- evt.AgentID = agentIDOrAnonymous(fallbackAgentID)
- }
- evt.Message = strings.TrimSpace(evt.Message)
- if evt.AgentID == "" {
- httpx.ErrorCode(w, http.StatusBadRequest, "missing_agent_id", "agentId is required", false, nil)
- return apiTypes.ActivityEvent{}, false
- }
- if evt.Message == "" {
- httpx.ErrorCode(w, http.StatusBadRequest, "missing_message", "message is required", false, nil)
- return apiTypes.ActivityEvent{}, false
- }
- if evt.Channel != "" && evt.Channel != "progress" {
- httpx.ErrorCode(w, http.StatusBadRequest, "invalid_channel", "channel must be progress", false, nil)
- return apiTypes.ActivityEvent{}, false
- }
-
- evt.Channel = "progress"
- evt.Type = "progress"
- return evt, true
-}
-
-func (d *Dashboard) handleAgentSSE(w http.ResponseWriter, r *http.Request) {
- agentID := agentIDOrAnonymous(r.PathValue("id"))
- if _, ok := d.Agent(agentID); !ok {
- httpx.ErrorCode(w, http.StatusNotFound, "agent_not_found", "agent not found", false, nil)
- return
- }
-
- r2 := r.Clone(r.Context())
- q := r2.URL.Query()
- q.Set("agentId", agentID)
- r2.URL.RawQuery = q.Encode()
- d.handleSSE(w, r2)
-}
-
-func (d *Dashboard) handleSSE(w http.ResponseWriter, r *http.Request) {
- flusher, ok := w.(http.Flusher)
- if !ok {
- httpx.Problem(w, http.StatusInternalServerError, "streaming_not_supported", "streaming not supported", false, nil)
- return
- }
-
- // SSE connections are intentionally long-lived. Clear the server-level write
- // deadline for this response so the stream is not terminated after
- // http.Server.WriteTimeout elapses.
- if err := http.NewResponseController(w).SetWriteDeadline(time.Time{}); err != nil {
- httpx.Problem(w, http.StatusInternalServerError, "streaming_deadline_unsupported", "streaming deadline unsupported", false, nil)
- return
- }
-
- mode := strings.TrimSpace(r.URL.Query().Get("mode"))
- if mode == "" {
- mode = "tool_calls"
- }
- if mode != "tool_calls" && mode != "progress" && mode != "both" {
- httpx.ErrorCode(w, http.StatusBadRequest, "bad_mode", "mode must be tool_calls, progress, or both", false, nil)
- return
- }
-
- w.Header().Set("Content-Type", "text/event-stream")
- w.Header().Set("Cache-Control", "no-cache")
- w.Header().Set("Connection", "keep-alive")
- w.Header().Set("X-Accel-Buffering", "no")
-
- activityCh := make(chan apiTypes.ActivityEvent, d.cfg.SSEBufferSize)
- sysCh := make(chan SystemEvent, d.cfg.SSEBufferSize)
- d.mu.Lock()
- d.activityConns[activityCh] = struct{}{}
- d.sysConns[sysCh] = struct{}{}
- d.mu.Unlock()
-
- defer func() {
- d.mu.Lock()
- delete(d.activityConns, activityCh)
- delete(d.sysConns, sysCh)
- d.mu.Unlock()
- }()
-
- includeMemory := r.URL.Query().Get("memory") == "1"
- agentFilter := agentIDOrAnonymous(strings.TrimSpace(r.URL.Query().Get("agentId")))
- if strings.TrimSpace(r.URL.Query().Get("agentId")) == "" {
- agentFilter = ""
- }
- agents := d.Agents()
- data, _ := json.Marshal(agents)
- _, _ = fmt.Fprintf(w, "event: init\ndata: %s\n\n", data)
- flusher.Flush()
-
- for _, evt := range d.RecentEvents() {
- if !matchesMode(mode, evt.Channel) {
- continue
- }
- if agentFilter != "" && evt.AgentID != agentFilter {
- continue
- }
- d.emitActivityEvent(w, flusher, evt)
- }
-
- if d.monitoring != nil || d.instances != nil {
- data, _ = json.Marshal(d.monitoringSnapshot(includeMemory))
- _, _ = fmt.Fprintf(w, "event: monitoring\ndata: %s\n\n", data)
- flusher.Flush()
- }
-
- keepalive := time.NewTicker(30 * time.Second)
- monitoring := time.NewTicker(5 * time.Second)
- defer keepalive.Stop()
- defer monitoring.Stop()
-
- for {
- select {
- case evt := <-activityCh:
- if matchesMode(mode, evt.Channel) && (agentFilter == "" || evt.AgentID == agentFilter) {
- d.emitActivityEvent(w, flusher, evt)
- }
- case evt := <-sysCh:
- data, _ := json.Marshal(evt)
- _, _ = fmt.Fprintf(w, "event: system\ndata: %s\n\n", data)
- flusher.Flush()
- if d.monitoring != nil || d.instances != nil {
- data, _ = json.Marshal(d.monitoringSnapshot(includeMemory))
- _, _ = fmt.Fprintf(w, "event: monitoring\ndata: %s\n\n", data)
- flusher.Flush()
- }
- case <-monitoring.C:
- if d.monitoring != nil || d.instances != nil {
- data, _ := json.Marshal(d.monitoringSnapshot(includeMemory))
- _, _ = fmt.Fprintf(w, "event: monitoring\ndata: %s\n\n", data)
- flusher.Flush()
- }
- case <-keepalive.C:
- _, _ = fmt.Fprintf(w, ": keepalive\n\n")
- flusher.Flush()
- case <-r.Context().Done():
- return
- }
- }
-}
-
-func (d *Dashboard) emitActivityEvent(w http.ResponseWriter, flusher http.Flusher, evt apiTypes.ActivityEvent) {
- name := "action"
- if evt.Channel == "progress" {
- name = "progress"
- }
- data, _ := json.Marshal(evt)
- _, _ = fmt.Fprintf(w, "event: %s\ndata: %s\n\n", name, data)
- flusher.Flush()
-}
-
-func matchesMode(mode, channel string) bool {
- switch mode {
- case "both":
- return channel == "tool_call" || channel == "progress"
- case "progress":
- return channel == "progress"
- default:
- return channel == "tool_call"
- }
-}
-
-func shouldTrackPersistedAgentActivity(evt activity.Event) bool {
- return evt.Source == "client"
-}
-
-func (d *Dashboard) rememberEventIDLocked(id string) {
- if id == "" {
- return
- }
- d.seenEventIDs[id] = struct{}{}
- d.seenEventLog = append(d.seenEventLog, id)
- if len(d.seenEventLog) <= d.maxSeenIDs {
- return
- }
- evicted := d.seenEventLog[0]
- d.seenEventLog = d.seenEventLog[1:]
- delete(d.seenEventIDs, evicted)
-}
-
-func normalizeEvent(evt apiTypes.ActivityEvent) apiTypes.ActivityEvent {
- if evt.ID == "" {
- evt.ID = generateID()
- }
- if evt.Timestamp.IsZero() {
- evt.Timestamp = time.Now().UTC()
- }
- if evt.Channel == "" {
- evt.Channel = "tool_call"
- }
- if evt.AgentID == "" {
- evt.AgentID = "anonymous"
- }
- return evt
-}
-
-func (d *Dashboard) recordEvents(events []apiTypes.ActivityEvent) {
- if d == nil || len(events) == 0 {
- return
- }
-
- d.mu.Lock()
- broadcast := make([]apiTypes.ActivityEvent, 0, len(events))
- for _, raw := range events {
- evt := normalizeEvent(raw)
- if _, ok := d.seenEventIDs[evt.ID]; ok {
- continue
- }
- d.rememberEventIDLocked(evt.ID)
- d.upsertAgentLocked(evt)
- if len(d.recentEvents) >= d.maxEvents {
- copy(d.recentEvents, d.recentEvents[1:])
- d.recentEvents[len(d.recentEvents)-1] = evt
- } else {
- d.recentEvents = append(d.recentEvents, evt)
- }
- broadcast = append(broadcast, evt)
- }
-
- chans := make([]chan apiTypes.ActivityEvent, 0, len(d.activityConns))
- for ch := range d.activityConns {
- chans = append(chans, ch)
- }
- d.mu.Unlock()
-
- for _, evt := range broadcast {
- for _, ch := range chans {
- select {
- case ch <- evt:
- default:
- }
- }
- }
-}
-
-func classifyActivityType(evt activity.Event) string {
- if evt.Action != "" {
- switch evt.Action {
- case "navigate", "snapshot", "screenshot", "text":
- return evt.Action
- default:
- return "action"
- }
- }
- path := evt.Path
- switch {
- case strings.Contains(path, "/navigate"):
- return "navigate"
- case strings.Contains(path, "/snapshot"):
- return "snapshot"
- case strings.Contains(path, "/screenshot"):
- return "screenshot"
- case strings.Contains(path, "/text"):
- return "text"
- case strings.Contains(path, "/action"):
- return "action"
- default:
- return "other"
- }
-}
-
-func agentIDOrAnonymous(agentID string) string {
- agentID = strings.TrimSpace(agentID)
- if agentID == "" {
- return "anonymous"
- }
- return agentID
-}
-
-const fallbackHTML = `<!DOCTYPE html>
-<html lang="en"><head><meta charset="UTF-8"/><meta name="viewport" content="width=device-width,initial-scale=1.0"/>
-<title>PinchTab Dashboard</title>
-<style>body{font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,sans-serif;display:flex;justify-content:center;align-items:center;min-height:100vh;margin:0;background:#0a0a0a;color:#e0e0e0}.c{text-align:center;max-width:480px;padding:2rem}h1{font-size:1.5rem;margin-bottom:.5rem}p{color:#888;line-height:1.6}code{background:#1a1a2e;padding:2px 8px;border-radius:4px;font-size:.9em}</style>
-</head><body><div class="c"><h1>🦀 Dashboard not built</h1>
-<p>The React dashboard needs to be compiled before use.<br/>
-Run <code>./dev build</code> or <code>./scripts/build-dashboard.sh</code> then rebuild the Go binary.</p>
-</div></body></html>`
-
-func (d *Dashboard) handleDashboardUI(w http.ResponseWriter, r *http.Request) {
- w.Header().Set("Content-Type", "text/html")
- w.Header().Set("Cache-Control", "no-store")
- w.Header().Set("Pragma", "no-cache")
- w.Header().Set("Expires", "0")
- data, err := dashboardFS.ReadFile("dashboard/dashboard.html")
- if err != nil {
- _, _ = w.Write([]byte(fallbackHTML))
- return
- }
- _, _ = w.Write(data)
-}
-
-func (d *Dashboard) withNoCache(next http.Handler) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- w.Header().Set("Cache-Control", "no-store")
- w.Header().Set("Pragma", "no-cache")
- w.Header().Set("Expires", "0")
- next.ServeHTTP(w, r)
- })
-}
-
-func (d *Dashboard) withLongCache(next http.Handler) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- // Assets have hashes in filenames - cache for 1 year
- w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
- next.ServeHTTP(w, r)
- })
-}
-
-func generateID() string {
- b := make([]byte, 8)
- _, _ = rand.Read(b)
- return hex.EncodeToString(b)
-}
diff --git a/internal/dashboard/handlers.go b/internal/dashboard/handlers.go
new file mode 100644
index 00000000..bde349f7
--- /dev/null
+++ b/internal/dashboard/handlers.go
@@ -0,0 +1,292 @@
+package dashboard
+
+import (
+ "crypto/rand"
+ "encoding/hex"
+ "encoding/json"
+ "fmt"
+ "io/fs"
+ "net/http"
+ "strings"
+ "time"
+
+ apiTypes "github.com/pinchtab/pinchtab/internal/api/types"
+ "github.com/pinchtab/pinchtab/internal/httpx"
+)
+
+func (d *Dashboard) RegisterHandlers(mux *http.ServeMux) {
+ mux.HandleFunc("GET /api/events", d.handleSSE)
+ mux.HandleFunc("GET /api/agents", d.handleAgents)
+ mux.HandleFunc("GET /api/agents/{id}", d.handleAgent)
+ mux.HandleFunc("GET /api/agents/{id}/events", d.handleAgentSSE)
+ mux.HandleFunc("POST /api/agents/{id}/events", d.handleAgentEventsByID)
+
+ sub, _ := fs.Sub(dashboardFS, "dashboard")
+ fileServer := http.FileServer(http.FS(sub))
+
+ // Serve static assets under /dashboard/ with long cache (hashed filenames)
+ mux.Handle("GET /dashboard/assets/", http.StripPrefix("/dashboard", d.withLongCache(fileServer)))
+ mux.Handle("GET /dashboard/favicon.png", http.StripPrefix("/dashboard", d.withLongCache(fileServer)))
+
+ // SPA: serve dashboard.html for /, /login, and /dashboard/*
+ mux.Handle("GET /{$}", d.withNoCache(http.HandlerFunc(d.handleDashboardUI)))
+ mux.Handle("GET /login", d.withNoCache(http.HandlerFunc(d.handleDashboardUI)))
+ mux.Handle("GET /dashboard", d.withNoCache(http.HandlerFunc(d.handleDashboardUI)))
+ mux.Handle("GET /dashboard/{path...}", d.withNoCache(http.HandlerFunc(d.handleDashboardUI)))
+}
+
+func (d *Dashboard) handleAgents(w http.ResponseWriter, _ *http.Request) {
+ httpx.JSON(w, http.StatusOK, d.Agents())
+}
+
+func (d *Dashboard) handleAgent(w http.ResponseWriter, r *http.Request) {
+ agentID := r.PathValue("id")
+ agent, ok := d.Agent(agentID)
+ if !ok {
+ httpx.ErrorCode(w, http.StatusNotFound, "agent_not_found", "agent not found", false, nil)
+ return
+ }
+
+ mode := strings.TrimSpace(r.URL.Query().Get("mode"))
+ if mode == "" {
+ mode = "both"
+ }
+ if mode != "tool_calls" && mode != "progress" && mode != "both" {
+ httpx.ErrorCode(w, http.StatusBadRequest, "bad_mode", "mode must be tool_calls, progress, or both", false, nil)
+ return
+ }
+
+ httpx.JSON(w, http.StatusOK, apiTypes.AgentDetail{
+ Agent: agent,
+ Events: d.EventsForAgent(agentID, mode),
+ })
+}
+
+func (d *Dashboard) handleAgentEventsByID(w http.ResponseWriter, r *http.Request) {
+ pathAgentID := agentIDOrAnonymous(r.PathValue("id"))
+ evt, ok := d.decodeProgressEvent(w, r, pathAgentID)
+ if !ok {
+ return
+ }
+ if evt.AgentID != "" && evt.AgentID != pathAgentID {
+ httpx.ErrorCode(w, http.StatusBadRequest, "agent_mismatch", "agentId must match route parameter", false, nil)
+ return
+ }
+ evt.AgentID = pathAgentID
+ d.RecordEvent(evt)
+ httpx.JSON(w, http.StatusCreated, map[string]string{"status": "ok", "id": evt.ID})
+}
+
+func (d *Dashboard) decodeProgressEvent(w http.ResponseWriter, r *http.Request, fallbackAgentID string) (apiTypes.ActivityEvent, bool) {
+ var evt apiTypes.ActivityEvent
+ if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 8<<10)).Decode(&evt); err != nil {
+ httpx.ErrorCode(w, http.StatusBadRequest, "bad_agent_event", "invalid activity event payload", false, nil)
+ return apiTypes.ActivityEvent{}, false
+ }
+
+ evt.AgentID = strings.TrimSpace(evt.AgentID)
+ if evt.AgentID == "" {
+ evt.AgentID = agentIDOrAnonymous(fallbackAgentID)
+ }
+ evt.Message = strings.TrimSpace(evt.Message)
+ if evt.AgentID == "" {
+ httpx.ErrorCode(w, http.StatusBadRequest, "missing_agent_id", "agentId is required", false, nil)
+ return apiTypes.ActivityEvent{}, false
+ }
+ if evt.Message == "" {
+ httpx.ErrorCode(w, http.StatusBadRequest, "missing_message", "message is required", false, nil)
+ return apiTypes.ActivityEvent{}, false
+ }
+ if evt.Channel != "" && evt.Channel != "progress" {
+ httpx.ErrorCode(w, http.StatusBadRequest, "invalid_channel", "channel must be progress", false, nil)
+ return apiTypes.ActivityEvent{}, false
+ }
+
+ evt.Channel = "progress"
+ evt.Type = "progress"
+ return evt, true
+}
+
+func (d *Dashboard) handleAgentSSE(w http.ResponseWriter, r *http.Request) {
+ agentID := agentIDOrAnonymous(r.PathValue("id"))
+ if _, ok := d.Agent(agentID); !ok {
+ httpx.ErrorCode(w, http.StatusNotFound, "agent_not_found", "agent not found", false, nil)
+ return
+ }
+
+ r2 := r.Clone(r.Context())
+ q := r2.URL.Query()
+ q.Set("agentId", agentID)
+ r2.URL.RawQuery = q.Encode()
+ d.handleSSE(w, r2)
+}
+
+func (d *Dashboard) handleSSE(w http.ResponseWriter, r *http.Request) {
+ flusher, ok := w.(http.Flusher)
+ if !ok {
+ httpx.Problem(w, http.StatusInternalServerError, "streaming_not_supported", "streaming not supported", false, nil)
+ return
+ }
+
+ // SSE connections are intentionally long-lived. Clear the server-level write
+ // deadline for this response so the stream is not terminated after
+ // http.Server.WriteTimeout elapses.
+ if err := http.NewResponseController(w).SetWriteDeadline(time.Time{}); err != nil {
+ httpx.Problem(w, http.StatusInternalServerError, "streaming_deadline_unsupported", "streaming deadline unsupported", false, nil)
+ return
+ }
+
+ mode := strings.TrimSpace(r.URL.Query().Get("mode"))
+ if mode == "" {
+ mode = "tool_calls"
+ }
+ if mode != "tool_calls" && mode != "progress" && mode != "both" {
+ httpx.ErrorCode(w, http.StatusBadRequest, "bad_mode", "mode must be tool_calls, progress, or both", false, nil)
+ return
+ }
+
+ w.Header().Set("Content-Type", "text/event-stream")
+ w.Header().Set("Cache-Control", "no-cache")
+ w.Header().Set("Connection", "keep-alive")
+ w.Header().Set("X-Accel-Buffering", "no")
+
+ activityCh := make(chan apiTypes.ActivityEvent, d.cfg.SSEBufferSize)
+ sysCh := make(chan SystemEvent, d.cfg.SSEBufferSize)
+ d.mu.Lock()
+ d.activityConns[activityCh] = struct{}{}
+ d.sysConns[sysCh] = struct{}{}
+ d.mu.Unlock()
+
+ defer func() {
+ d.mu.Lock()
+ delete(d.activityConns, activityCh)
+ delete(d.sysConns, sysCh)
+ d.mu.Unlock()
+ }()
+
+ includeMemory := r.URL.Query().Get("memory") == "1"
+ agentFilter := agentIDOrAnonymous(strings.TrimSpace(r.URL.Query().Get("agentId")))
+ if strings.TrimSpace(r.URL.Query().Get("agentId")) == "" {
+ agentFilter = ""
+ }
+ agents := d.Agents()
+ data, _ := json.Marshal(agents)
+ _, _ = fmt.Fprintf(w, "event: init\ndata: %s\n\n", data)
+ flusher.Flush()
+
+ for _, evt := range d.RecentEvents() {
+ if !matchesMode(mode, evt.Channel) {
+ continue
+ }
+ if agentFilter != "" && evt.AgentID != agentFilter {
+ continue
+ }
+ d.emitActivityEvent(w, flusher, evt)
+ }
+
+ if d.monitoring != nil || d.instances != nil {
+ data, _ = json.Marshal(d.monitoringSnapshot(includeMemory))
+ _, _ = fmt.Fprintf(w, "event: monitoring\ndata: %s\n\n", data)
+ flusher.Flush()
+ }
+
+ keepalive := time.NewTicker(30 * time.Second)
+ monitoring := time.NewTicker(5 * time.Second)
+ defer keepalive.Stop()
+ defer monitoring.Stop()
+
+ for {
+ select {
+ case evt := <-activityCh:
+ if matchesMode(mode, evt.Channel) && (agentFilter == "" || evt.AgentID == agentFilter) {
+ d.emitActivityEvent(w, flusher, evt)
+ }
+ case evt := <-sysCh:
+ data, _ := json.Marshal(evt)
+ _, _ = fmt.Fprintf(w, "event: system\ndata: %s\n\n", data)
+ flusher.Flush()
+ if d.monitoring != nil || d.instances != nil {
+ data, _ = json.Marshal(d.monitoringSnapshot(includeMemory))
+ _, _ = fmt.Fprintf(w, "event: monitoring\ndata: %s\n\n", data)
+ flusher.Flush()
+ }
+ case <-monitoring.C:
+ if d.monitoring != nil || d.instances != nil {
+ data, _ := json.Marshal(d.monitoringSnapshot(includeMemory))
+ _, _ = fmt.Fprintf(w, "event: monitoring\ndata: %s\n\n", data)
+ flusher.Flush()
+ }
+ case <-keepalive.C:
+ _, _ = fmt.Fprintf(w, ": keepalive\n\n")
+ flusher.Flush()
+ case <-r.Context().Done():
+ return
+ }
+ }
+}
+
+func (d *Dashboard) emitActivityEvent(w http.ResponseWriter, flusher http.Flusher, evt apiTypes.ActivityEvent) {
+ name := "action"
+ if evt.Channel == "progress" {
+ name = "progress"
+ }
+ data, _ := json.Marshal(evt)
+ _, _ = fmt.Fprintf(w, "event: %s\ndata: %s\n\n", name, data)
+ flusher.Flush()
+}
+
+func matchesMode(mode, channel string) bool {
+ switch mode {
+ case "both":
+ return channel == "tool_call" || channel == "progress"
+ case "progress":
+ return channel == "progress"
+ default:
+ return channel == "tool_call"
+ }
+}
+
+const fallbackHTML = `<!DOCTYPE html>
+<html lang="en"><head><meta charset="UTF-8"/><meta name="viewport" content="width=device-width,initial-scale=1.0"/>
+<title>PinchTab Dashboard</title>
+<style>body{font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,sans-serif;display:flex;justify-content:center;align-items:center;min-height:100vh;margin:0;background:#0a0a0a;color:#e0e0e0}.c{text-align:center;max-width:480px;padding:2rem}h1{font-size:1.5rem;margin-bottom:.5rem}p{color:#888;line-height:1.6}code{background:#1a1a2e;padding:2px 8px;border-radius:4px;font-size:.9em}</style>
+</head><body><div class="c"><h1>🦀 Dashboard not built</h1>
+<p>The React dashboard needs to be compiled before use.<br/>
+Run <code>./dev build</code> or <code>./scripts/build-dashboard.sh</code> then rebuild the Go binary.</p>
+</div></body></html>`
+
+func (d *Dashboard) handleDashboardUI(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "text/html")
+ w.Header().Set("Cache-Control", "no-store")
+ w.Header().Set("Pragma", "no-cache")
+ w.Header().Set("Expires", "0")
+ data, err := dashboardFS.ReadFile("dashboard/dashboard.html")
+ if err != nil {
+ _, _ = w.Write([]byte(fallbackHTML))
+ return
+ }
+ _, _ = w.Write(data)
+}
+
+func (d *Dashboard) withNoCache(next http.Handler) http.Handler {
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Cache-Control", "no-store")
+ w.Header().Set("Pragma", "no-cache")
+ w.Header().Set("Expires", "0")
+ next.ServeHTTP(w, r)
+ })
+}
+
+func (d *Dashboard) withLongCache(next http.Handler) http.Handler {
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ // Assets have hashes in filenames - cache for 1 year
+ w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
+ next.ServeHTTP(w, r)
+ })
+}
+
+func generateID() string {
+ b := make([]byte, 8)
+ _, _ = rand.Read(b)
+ return hex.EncodeToString(b)
+}
diff --git a/internal/handlers/download.go b/internal/handlers/download.go
index 49964208..fe52efd1 100644
--- a/internal/handlers/download.go
+++ b/internal/handlers/download.go
@@ -1,24 +1,17 @@
package handlers
import (
- "compress/gzip"
"context"
"encoding/base64"
- "errors"
"fmt"
- "io"
"log/slog"
"net/http"
"net/url"
"os"
- "path"
"path/filepath"
- "strconv"
"strings"
- "sync"
- "time"
-
"sync/atomic"
+ "time"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/fetch"
@@ -26,226 +19,9 @@ import (
"github.com/chromedp/chromedp"
"github.com/pinchtab/pinchtab/internal/activity"
"github.com/pinchtab/pinchtab/internal/authn"
- "github.com/pinchtab/pinchtab/internal/bridge"
- "github.com/pinchtab/pinchtab/internal/config"
"github.com/pinchtab/pinchtab/internal/httpx"
- "github.com/pinchtab/pinchtab/internal/idpi"
- "github.com/pinchtab/pinchtab/internal/netguard"
)
-var errDownloadTooLarge = errors.New("download response too large")
-
-type downloadURLGuard struct {
- allowedDomains []string
-}
-
-func newDownloadURLGuard(allowedDomains []string) *downloadURLGuard {
- return &downloadURLGuard{allowedDomains: append([]string(nil), allowedDomains...)}
-}
-
-func (g *downloadURLGuard) isHostAllowed(host string) bool {
- if len(g.allowedDomains) == 0 {
- return false
- }
- host = netguard.NormalizeHost(host)
- if host == "" {
- return false
- }
- return g.isDomainAllowed("https://" + host)
-}
-
-// isDomainAllowed reports whether rawURL's domain is on the configured
-// allowlist. Allowlisted domains bypass private-IP checks because they
-// are explicitly trusted by the operator (e.g. internal docker hosts).
-func (g *downloadURLGuard) isDomainAllowed(rawURL string) bool {
- if len(g.allowedDomains) == 0 {
- return false
- }
- result := idpi.CheckDomain(rawURL, config.IDPIConfig{
- Enabled: true,
- StrictMode: true,
- }, g.allowedDomains)
- return !result.Blocked
-}
-
-func (g *downloadURLGuard) Validate(rawURL string) error {
- parsed, err := url.Parse(rawURL)
- if err != nil {
- return fmt.Errorf("invalid URL")
- }
-
- if parsed.Scheme != "http" && parsed.Scheme != "https" {
- return fmt.Errorf("only http/https schemes are allowed")
- }
-
- host := netguard.NormalizeHost(parsed.Hostname())
- if host == "" || netguard.IsLocalHost(host) {
- return fmt.Errorf("internal or blocked host")
- }
-
- // Allowlisted domains bypass IP validation (e.g. internal docker hosts).
- if len(g.allowedDomains) > 0 {
- result := idpi.CheckDomain(rawURL, config.IDPIConfig{
- Enabled: true,
- StrictMode: true,
- }, g.allowedDomains)
- if result.Blocked {
- return fmt.Errorf("domain not allowed by security.downloadAllowedDomains")
- }
- return nil
- }
-
- ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second)
- defer cancel()
-
- if _, err := netguard.ResolveAndValidatePublicIPs(ctx, host); err != nil {
- if errors.Is(err, netguard.ErrResolveHost) {
- return fmt.Errorf("could not resolve host")
- }
- if errors.Is(err, netguard.ErrPrivateInternalIP) {
- return fmt.Errorf("private/internal IP blocked")
- }
- return fmt.Errorf("could not resolve host")
- }
- return nil
-}
-
-func validateDownloadRemoteIPAddress(raw string) error {
- raw = strings.TrimSpace(raw)
- if raw == "" {
- // Best-effort mitigation: some responses may not expose a remote IP
- // (for example, cached responses). Skip the post-connect check then.
- return nil
- }
-
- normalized := netguard.NormalizeRemoteIP(raw)
- if err := netguard.ValidateRemoteIPAddress(raw); err != nil {
- if errors.Is(err, netguard.ErrUnparseableRemoteIP) {
- return fmt.Errorf("download connected to an unparseable remote IP %q", normalized)
- }
- if errors.Is(err, netguard.ErrPrivateInternalIP) {
- return fmt.Errorf("download connected to blocked remote IP %s", normalized)
- }
- return fmt.Errorf("download connected to an unparseable remote IP %q", raw)
- }
- return nil
-}
-
-// validateDownloadURL blocks file://, internal hosts, private IPs, and cloud metadata.
-// Only public http/https URLs are allowed.
-func validateDownloadURL(rawURL string) error {
- return newDownloadURLGuard(nil).Validate(rawURL)
-}
-
-func validateTabScopedDownloadURL(currentURL, requestedURL string) error {
- currentParsed, err := url.Parse(strings.TrimSpace(currentURL))
- if err != nil || currentParsed.Scheme == "" || currentParsed.Host == "" {
- return fmt.Errorf("tab-scoped downloads require an active http(s) page")
- }
- if currentParsed.Scheme != "http" && currentParsed.Scheme != "https" {
- return fmt.Errorf("tab-scoped downloads require an active http(s) page")
- }
-
- requestedParsed, err := url.Parse(strings.TrimSpace(requestedURL))
- if err != nil || requestedParsed.Scheme == "" || requestedParsed.Host == "" {
- return fmt.Errorf("invalid download URL")
- }
-
- if strings.EqualFold(currentParsed.Scheme, requestedParsed.Scheme) &&
- strings.EqualFold(currentParsed.Host, requestedParsed.Host) {
- return nil
- }
-
- return fmt.Errorf("tab-scoped downloads are limited to the current page origin")
-}
-
-type downloadRequestGuard struct {
- validator *downloadURLGuard
- maxRedirects int
- redirects atomic.Int32
-
- mu sync.Mutex
- blockedErr error
-}
-
-func newDownloadRequestGuard(validator *downloadURLGuard, maxRedirects int) *downloadRequestGuard {
- return &downloadRequestGuard{
- validator: validator,
- maxRedirects: maxRedirects,
- }
-}
-
-func (g *downloadRequestGuard) Validate(rawURL string, redirected bool) error {
- // Skip validation for Chrome internal URLs (about:blank, chrome-error://, etc.)
- // that fire during tab creation before the actual navigation begins.
- if rawURL == "about:blank" || strings.HasPrefix(rawURL, "chrome") {
- return nil
- }
- if err := g.validator.Validate(rawURL); err != nil {
- return fmt.Errorf("unsafe browser request: %w", err)
- }
- if redirected && g.maxRedirects >= 0 {
- count := int(g.redirects.Add(1))
- if count > g.maxRedirects {
- return fmt.Errorf("%w: got %d, max %d", bridge.ErrTooManyRedirects, count, g.maxRedirects)
- }
- }
- return nil
-}
-
-func (g *downloadRequestGuard) NoteBlocked(err error) {
- g.mu.Lock()
- if g.blockedErr == nil {
- g.blockedErr = err
- }
- g.mu.Unlock()
-}
-
-func (g *downloadRequestGuard) BlockedError() error {
- g.mu.Lock()
- defer g.mu.Unlock()
- return g.blockedErr
-}
-
-func downloadTooLargeError(size int64, maxBytes int) error {
- return fmt.Errorf("%w: received %d bytes, max %d", errDownloadTooLarge, size, maxBytes)
-}
-
-func parseContentLengthHeader(headers network.Headers) (int64, bool) {
- for key, raw := range headers {
- if !strings.EqualFold(strings.TrimSpace(key), "Content-Length") {
- continue
- }
- value := strings.TrimSpace(fmt.Sprint(raw))
- if value == "" {
- return 0, false
- }
- size, err := strconv.ParseInt(value, 10, 64)
- if err != nil || size < 0 {
- return 0, false
- }
- return size, true
- }
- return 0, false
-}
-
-func writeDownloadGuardError(w http.ResponseWriter, err error, maxBytes int) bool {
- if err == nil {
- return false
- }
- switch {
- case errors.Is(err, bridge.ErrTooManyRedirects):
- httpx.Error(w, 422, fmt.Errorf("download: %w", err))
- case errors.Is(err, errDownloadTooLarge):
- httpx.ErrorCode(w, http.StatusRequestEntityTooLarge, "download_too_large", err.Error(), false, map[string]any{
- "maxBytes": maxBytes,
- })
- default:
- httpx.Error(w, 400, err)
- }
- return true
-}
-
// HandleDownload fetches a URL using the browser's session (cookies, stealth)
// and returns the content. This preserves authentication and fingerprint.
//
@@ -332,7 +108,6 @@ func (h *Handlers) HandleDownload(w http.ResponseWriter, r *http.Request) {
defer tCancel()
go httpx.CancelOnClientDone(r.Context(), tCancel)
- // Enable network tracking to capture response metadata.
var requestID network.RequestID
var responseMIME string
var responseStatus int
@@ -464,7 +239,6 @@ func (h *Handlers) HandleDownload(w http.ResponseWriter, r *http.Request) {
slog.Info("download: Chrome navigation aborted, falling back to direct fetch", "url", dlURL)
body, mime, status, fetchErr := h.fetchDirectWithCookies(tCtx, browserCtx, dlURL, validator, maxDownloadBytes)
if fetchErr != nil {
- // Return 400 for redirect-blocked errors (SSRF protection)
errMsg := fetchErr.Error()
if strings.Contains(errMsg, "blocked") || strings.Contains(errMsg, "private") {
httpx.Error(w, 400, fmt.Errorf("unsafe browser request: %w", fetchErr))
@@ -486,7 +260,6 @@ func (h *Handlers) HandleDownload(w http.ResponseWriter, r *http.Request) {
return
}
- // Wait for response.
select {
case <-done:
case <-tCtx.Done():
@@ -510,7 +283,6 @@ func (h *Handlers) HandleDownload(w http.ResponseWriter, r *http.Request) {
return
}
- // Get response body via CDP.
var body []byte
if err := chromedp.Run(tCtx,
chromedp.ActionFunc(func(ctx context.Context) error {
@@ -592,113 +364,6 @@ func (h *Handlers) writeDownloadResponse(w http.ResponseWriter, body []byte, mim
})
}
-// fetchDirectWithCookies performs a Go HTTP fetch with browser cookies.
-// Fallback for when Chrome navigation aborts (e.g. .gz files).
-func (h *Handlers) fetchDirectWithCookies(ctx context.Context, browserCtx context.Context, dlURL string, validator *downloadURLGuard, maxBytes int) (body []byte, contentType string, statusCode int, err error) {
- var browserCookies []*network.Cookie
- if fetchErr := chromedp.Run(browserCtx,
- chromedp.ActionFunc(func(ctx context.Context) error {
- var err error
- browserCookies, err = network.GetCookies().WithURLs([]string{dlURL}).Do(ctx)
- return err
- }),
- ); fetchErr != nil {
- slog.Debug("download fallback: failed to get browser cookies", "err", fetchErr)
- }
-
- req, err := http.NewRequestWithContext(ctx, "GET", dlURL, nil)
- if err != nil {
- return nil, "", 0, err
- }
- if h.Config.UserAgent != "" {
- req.Header.Set("User-Agent", h.Config.UserAgent)
- }
- req.Header.Set("Accept", "*/*")
-
- for _, c := range browserCookies {
- req.AddCookie(&http.Cookie{Name: c.Name, Value: c.Value})
- }
-
- client := newGuardedDownloadClient(validator, h.Config.MaxRedirects, 30)
- resp, err := client.Do(req)
- if err != nil {
- return nil, "", 0, err
- }
- defer func() { _ = resp.Body.Close() }()
-
- if cl := resp.Header.Get("Content-Length"); cl != "" {
- if contentLength, parseErr := strconv.ParseInt(cl, 10, 64); parseErr == nil {
- if contentLength > int64(maxBytes) {
- return nil, "", 0, errDownloadTooLarge
- }
- }
- }
-
- reader := io.Reader(resp.Body)
- isGzip := isGzipContent(resp.Header.Get("Content-Type"), dlURL) &&
- !strings.EqualFold(resp.Header.Get("Content-Encoding"), "gzip")
-
- if isGzip {
- gz, gzErr := gzip.NewReader(resp.Body)
- if gzErr != nil {
- return nil, "", 0, fmt.Errorf("gzip decompress: %w", gzErr)
- }
- defer func() { _ = gz.Close() }()
- reader = gz
- }
-
- // LimitReader on decompressed stream protects against gzip bombs.
- data, err := io.ReadAll(io.LimitReader(reader, int64(maxBytes)+1))
- if err != nil {
- return nil, "", 0, err
- }
- if len(data) > maxBytes {
- return nil, "", 0, errDownloadTooLarge
- }
-
- ct := resp.Header.Get("Content-Type")
- if ct == "" {
- ct = "application/octet-stream"
- }
- // If we decompressed gzip, report the inner content type
- if isGzip {
- ct = inferDecompressedContentType(dlURL, ct)
- }
-
- return data, ct, resp.StatusCode, nil
-}
-
-func isGzipContent(contentType, rawURL string) bool {
- if strings.Contains(strings.ToLower(contentType), "gzip") {
- return true
- }
- // Use path.Ext for precise extension matching (.gz, not .pgz or .ngz)
- parsed, err := url.Parse(rawURL)
- if err != nil {
- return false
- }
- return strings.EqualFold(path.Ext(parsed.Path), ".gz")
-}
-
-func inferDecompressedContentType(rawURL, fallback string) string {
- lower := strings.ToLower(rawURL)
- // Strip .gz to infer inner type
- if strings.HasSuffix(lower, ".xml.gz") {
- return "application/xml"
- }
- if strings.HasSuffix(lower, ".json.gz") {
- return "application/json"
- }
- if strings.HasSuffix(lower, ".txt.gz") || strings.HasSuffix(lower, ".csv.gz") {
- return "text/plain"
- }
- return "application/octet-stream"
-}
-
-func isNavigationAborted(err error) bool {
- return err != nil && strings.Contains(err.Error(), "net::ERR_ABORTED")
-}
-
// HandleTabDownload fetches a URL using the browser session for a tab identified by path ID.
//
// @Endpoint GET /tabs/{id}/download
diff --git a/internal/handlers/download_fetch.go b/internal/handlers/download_fetch.go
new file mode 100644
index 00000000..0ea74c8f
--- /dev/null
+++ b/internal/handlers/download_fetch.go
@@ -0,0 +1,122 @@
+package handlers
+
+import (
+ "compress/gzip"
+ "context"
+ "fmt"
+ "io"
+ "log/slog"
+ "net/http"
+ "net/url"
+ "path"
+ "strconv"
+ "strings"
+
+ "github.com/chromedp/cdproto/network"
+ "github.com/chromedp/chromedp"
+)
+
+// fetchDirectWithCookies performs a Go HTTP fetch with browser cookies.
+// Fallback for when Chrome navigation aborts (e.g. .gz files).
+func (h *Handlers) fetchDirectWithCookies(ctx context.Context, browserCtx context.Context, dlURL string, validator *downloadURLGuard, maxBytes int) (body []byte, contentType string, statusCode int, err error) {
+ var browserCookies []*network.Cookie
+ if fetchErr := chromedp.Run(browserCtx,
+ chromedp.ActionFunc(func(ctx context.Context) error {
+ var err error
+ browserCookies, err = network.GetCookies().WithURLs([]string{dlURL}).Do(ctx)
+ return err
+ }),
+ ); fetchErr != nil {
+ slog.Debug("download fallback: failed to get browser cookies", "err", fetchErr)
+ }
+
+ req, err := http.NewRequestWithContext(ctx, "GET", dlURL, nil)
+ if err != nil {
+ return nil, "", 0, err
+ }
+ if h.Config.UserAgent != "" {
+ req.Header.Set("User-Agent", h.Config.UserAgent)
+ }
+ req.Header.Set("Accept", "*/*")
+
+ for _, c := range browserCookies {
+ req.AddCookie(&http.Cookie{Name: c.Name, Value: c.Value})
+ }
+
+ client := newGuardedDownloadClient(validator, h.Config.MaxRedirects, 30)
+ resp, err := client.Do(req)
+ if err != nil {
+ return nil, "", 0, err
+ }
+ defer func() { _ = resp.Body.Close() }()
+
+ if cl := resp.Header.Get("Content-Length"); cl != "" {
+ if contentLength, parseErr := strconv.ParseInt(cl, 10, 64); parseErr == nil {
+ if contentLength > int64(maxBytes) {
+ return nil, "", 0, errDownloadTooLarge
+ }
+ }
+ }
+
+ reader := io.Reader(resp.Body)
+ isGzip := isGzipContent(resp.Header.Get("Content-Type"), dlURL) &&
+ !strings.EqualFold(resp.Header.Get("Content-Encoding"), "gzip")
+
+ if isGzip {
+ gz, gzErr := gzip.NewReader(resp.Body)
+ if gzErr != nil {
+ return nil, "", 0, fmt.Errorf("gzip decompress: %w", gzErr)
+ }
+ defer func() { _ = gz.Close() }()
+ reader = gz
+ }
+
+ // LimitReader on decompressed stream protects against gzip bombs.
+ data, err := io.ReadAll(io.LimitReader(reader, int64(maxBytes)+1))
+ if err != nil {
+ return nil, "", 0, err
+ }
+ if len(data) > maxBytes {
+ return nil, "", 0, errDownloadTooLarge
+ }
+
+ ct := resp.Header.Get("Content-Type")
+ if ct == "" {
+ ct = "application/octet-stream"
+ }
+ if isGzip {
+ ct = inferDecompressedContentType(dlURL, ct)
+ }
+
+ return data, ct, resp.StatusCode, nil
+}
+
+func isGzipContent(contentType, rawURL string) bool {
+ if strings.Contains(strings.ToLower(contentType), "gzip") {
+ return true
+ }
+ // Use path.Ext for precise extension matching (.gz, not .pgz or .ngz)
+ parsed, err := url.Parse(rawURL)
+ if err != nil {
+ return false
+ }
+ return strings.EqualFold(path.Ext(parsed.Path), ".gz")
+}
+
+func inferDecompressedContentType(rawURL, fallback string) string {
+ lower := strings.ToLower(rawURL)
+ if strings.HasSuffix(lower, ".xml.gz") {
+ return "application/xml"
+ }
+ if strings.HasSuffix(lower, ".json.gz") {
+ return "application/json"
+ }
+ if strings.HasSuffix(lower, ".txt.gz") || strings.HasSuffix(lower, ".csv.gz") {
+ return "text/plain"
+ }
+ return "application/octet-stream"
+}
+
+func isNavigationAborted(err error) bool {
+ return err != nil && strings.Contains(err.Error(), "net::ERR_ABORTED")
+}
diff --git a/internal/handlers/download_guard.go b/internal/handlers/download_guard.go
new file mode 100644
index 00000000..6740335d
--- /dev/null
+++ b/internal/handlers/download_guard.go
@@ -0,0 +1,233 @@
+package handlers
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "net/http"
+ "net/url"
+ "strconv"
+ "strings"
+ "sync"
+ "sync/atomic"
+ "time"
+
+ "github.com/chromedp/cdproto/network"
+ "github.com/pinchtab/pinchtab/internal/bridge"
+ "github.com/pinchtab/pinchtab/internal/config"
+ "github.com/pinchtab/pinchtab/internal/httpx"
+ "github.com/pinchtab/pinchtab/internal/idpi"
+ "github.com/pinchtab/pinchtab/internal/netguard"
+)
+
+var errDownloadTooLarge = errors.New("download response too large")
+
+type downloadURLGuard struct {
+ allowedDomains []string
+}
+
+func newDownloadURLGuard(allowedDomains []string) *downloadURLGuard {
+ return &downloadURLGuard{allowedDomains: append([]string(nil), allowedDomains...)}
+}
+
+func (g *downloadURLGuard) isHostAllowed(host string) bool {
+ if len(g.allowedDomains) == 0 {
+ return false
+ }
+ host = netguard.NormalizeHost(host)
+ if host == "" {
+ return false
+ }
+ return g.isDomainAllowed("https://" + host)
+}
+
+// isDomainAllowed reports whether rawURL's domain is on the configured
+// allowlist. Allowlisted domains bypass private-IP checks because they
+// are explicitly trusted by the operator (e.g. internal docker hosts).
+func (g *downloadURLGuard) isDomainAllowed(rawURL string) bool {
+ if len(g.allowedDomains) == 0 {
+ return false
+ }
+ result := idpi.CheckDomain(rawURL, config.IDPIConfig{
+ Enabled: true,
+ StrictMode: true,
+ }, g.allowedDomains)
+ return !result.Blocked
+}
+
+func (g *downloadURLGuard) Validate(rawURL string) error {
+ parsed, err := url.Parse(rawURL)
+ if err != nil {
+ return fmt.Errorf("invalid URL")
+ }
+
+ if parsed.Scheme != "http" && parsed.Scheme != "https" {
+ return fmt.Errorf("only http/https schemes are allowed")
+ }
+
+ host := netguard.NormalizeHost(parsed.Hostname())
+ if host == "" || netguard.IsLocalHost(host) {
+ return fmt.Errorf("internal or blocked host")
+ }
+
+ if len(g.allowedDomains) > 0 {
+ result := idpi.CheckDomain(rawURL, config.IDPIConfig{
+ Enabled: true,
+ StrictMode: true,
+ }, g.allowedDomains)
+ if result.Blocked {
+ return fmt.Errorf("domain not allowed by security.downloadAllowedDomains")
+ }
+ return nil
+ }
+
+ ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second)
+ defer cancel()
+
+ if _, err := netguard.ResolveAndValidatePublicIPs(ctx, host); err != nil {
+ if errors.Is(err, netguard.ErrResolveHost) {
+ return fmt.Errorf("could not resolve host")
+ }
+ if errors.Is(err, netguard.ErrPrivateInternalIP) {
+ return fmt.Errorf("private/internal IP blocked")
+ }
+ return fmt.Errorf("could not resolve host")
+ }
+ return nil
+}
+
+func validateDownloadRemoteIPAddress(raw string) error {
+ raw = strings.TrimSpace(raw)
+ if raw == "" {
+ // Best-effort mitigation: some responses may not expose a remote IP
+ // (for example, cached responses). Skip the post-connect check then.
+ return nil
+ }
+
+ normalized := netguard.NormalizeRemoteIP(raw)
+ if err := netguard.ValidateRemoteIPAddress(raw); err != nil {
+ if errors.Is(err, netguard.ErrUnparseableRemoteIP) {
+ return fmt.Errorf("download connected to an unparseable remote IP %q", normalized)
+ }
+ if errors.Is(err, netguard.ErrPrivateInternalIP) {
+ return fmt.Errorf("download connected to blocked remote IP %s", normalized)
+ }
+ return fmt.Errorf("download connected to an unparseable remote IP %q", raw)
+ }
+ return nil
+}
+
+// validateDownloadURL blocks file://, internal hosts, private IPs, and cloud metadata.
+// Only public http/https URLs are allowed.
+func validateDownloadURL(rawURL string) error {
+ return newDownloadURLGuard(nil).Validate(rawURL)
+}
+
+func validateTabScopedDownloadURL(currentURL, requestedURL string) error {
+ currentParsed, err := url.Parse(strings.TrimSpace(currentURL))
+ if err != nil || currentParsed.Scheme == "" || currentParsed.Host == "" {
+ return fmt.Errorf("tab-scoped downloads require an active http(s) page")
+ }
+ if currentParsed.Scheme != "http" && currentParsed.Scheme != "https" {
+ return fmt.Errorf("tab-scoped downloads require an active http(s) page")
+ }
+
+ requestedParsed, err := url.Parse(strings.TrimSpace(requestedURL))
+ if err != nil || requestedParsed.Scheme == "" || requestedParsed.Host == "" {
+ return fmt.Errorf("invalid download URL")
+ }
+
+ if strings.EqualFold(currentParsed.Scheme, requestedParsed.Scheme) &&
+ strings.EqualFold(currentParsed.Host, requestedParsed.Host) {
+ return nil
+ }
+
+ return fmt.Errorf("tab-scoped downloads are limited to the current page origin")
+}
+
+type downloadRequestGuard struct {
+ validator *downloadURLGuard
+ maxRedirects int
+ redirects atomic.Int32
+
+ mu sync.Mutex
+ blockedErr error
+}
+
+func newDownloadRequestGuard(validator *downloadURLGuard, maxRedirects int) *downloadRequestGuard {
+ return &downloadRequestGuard{
+ validator: validator,
+ maxRedirects: maxRedirects,
+ }
+}
+
+func (g *downloadRequestGuard) Validate(rawURL string, redirected bool) error {
+ // Skip validation for Chrome internal URLs (about:blank, chrome-error://, etc.)
+ // that fire during tab creation before the actual navigation begins.
+ if rawURL == "about:blank" || strings.HasPrefix(rawURL, "chrome") {
+ return nil
+ }
+ if err := g.validator.Validate(rawURL); err != nil {
+ return fmt.Errorf("unsafe browser request: %w", err)
+ }
+ if redirected && g.maxRedirects >= 0 {
+ count := int(g.redirects.Add(1))
+ if count > g.maxRedirects {
+ return fmt.Errorf("%w: got %d, max %d", bridge.ErrTooManyRedirects, count, g.maxRedirects)
+ }
+ }
+ return nil
+}
+
+func (g *downloadRequestGuard) NoteBlocked(err error) {
+ g.mu.Lock()
+ if g.blockedErr == nil {
+ g.blockedErr = err
+ }
+ g.mu.Unlock()
+}
+
+func (g *downloadRequestGuard) BlockedError() error {
+ g.mu.Lock()
+ defer g.mu.Unlock()
+ return g.blockedErr
+}
+
+func downloadTooLargeError(size int64, maxBytes int) error {
+ return fmt.Errorf("%w: received %d bytes, max %d", errDownloadTooLarge, size, maxBytes)
+}
+
+func parseContentLengthHeader(headers network.Headers) (int64, bool) {
+ for key, raw := range headers {
+ if !strings.EqualFold(strings.TrimSpace(key), "Content-Length") {
+ continue
+ }
+ value := strings.TrimSpace(fmt.Sprint(raw))
+ if value == "" {
+ return 0, false
+ }
+ size, err := strconv.ParseInt(value, 10, 64)
+ if err != nil || size < 0 {
+ return 0, false
+ }
+ return size, true
+ }
+ return 0, false
+}
+
+func writeDownloadGuardError(w http.ResponseWriter, err error, maxBytes int) bool {
+ if err == nil {
+ return false
+ }
+ switch {
+ case errors.Is(err, bridge.ErrTooManyRedirects):
+ httpx.Error(w, 422, fmt.Errorf("download: %w", err))
+ case errors.Is(err, errDownloadTooLarge):
+ httpx.ErrorCode(w, http.StatusRequestEntityTooLarge, "download_too_large", err.Error(), false, map[string]any{
+ "maxBytes": maxBytes,
+ })
+ default:
+ httpx.Error(w, 400, err)
+ }
+ return true
+}
diff --git a/internal/handlers/history.go b/internal/handlers/history.go
new file mode 100644
index 00000000..4617270b
--- /dev/null
+++ b/internal/handlers/history.go
@@ -0,0 +1,134 @@
+package handlers
+
+import (
+ "context"
+ "fmt"
+ "net/http"
+ "time"
+
+ "github.com/chromedp/cdproto/page"
+ "github.com/chromedp/chromedp"
+ "github.com/pinchtab/pinchtab/internal/httpx"
+)
+
+// HandleTabBack navigates a specific tab back in history.
+func (h *Handlers) HandleTabBack(w http.ResponseWriter, r *http.Request) {
+ q := r.URL.Query()
+ q.Set("tabId", r.PathValue("id"))
+ r.URL.RawQuery = q.Encode()
+ h.HandleBack(w, r)
+}
+
+// HandleTabForward navigates a specific tab forward in history.
+func (h *Handlers) HandleTabForward(w http.ResponseWriter, r *http.Request) {
+ q := r.URL.Query()
+ q.Set("tabId", r.PathValue("id"))
+ r.URL.RawQuery = q.Encode()
+ h.HandleForward(w, r)
+}
+
+// HandleTabReload reloads a specific tab.
+func (h *Handlers) HandleTabReload(w http.ResponseWriter, r *http.Request) {
+ q := r.URL.Query()
+ q.Set("tabId", r.PathValue("id"))
+ r.URL.RawQuery = q.Encode()
+ h.HandleReload(w, r)
+}
+
+// HandleBack navigates the current (or specified) tab back in history.
+func (h *Handlers) HandleBack(w http.ResponseWriter, r *http.Request) {
+ if !h.ensureChromeOrRespond(w) {
+ return
+ }
+ tabID := r.URL.Query().Get("tabId")
+ ctx, resolvedID, err := h.Bridge.TabContext(tabID)
+ if err != nil {
+ httpx.Error(w, 404, err)
+ return
+ }
+
+ // Use CDP directly instead of chromedp.NavigateBack() which wraps in
+ // responseAction() and waits for Page.loadEventFired — hangs indefinitely.
+ var noHistory bool
+ if err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {
+ cur, entries, err := page.GetNavigationHistory().Do(ctx)
+ if err != nil {
+ return fmt.Errorf("get history: %w", err)
+ }
+ if cur <= 0 || cur > int64(len(entries)-1) {
+ noHistory = true
+ return nil
+ }
+ return page.NavigateToHistoryEntry(entries[cur-1].ID).Do(ctx)
+ })); err != nil {
+ httpx.Error(w, 500, fmt.Errorf("back: %w", err))
+ return
+ }
+ if !noHistory {
+ time.Sleep(200 * time.Millisecond)
+ }
+
+ var curURL string
+ _ = chromedp.Run(ctx, chromedp.Location(&curURL))
+ httpx.JSON(w, 200, map[string]any{"tabId": resolvedID, "url": curURL})
+}
+
+// HandleForward navigates the current (or specified) tab forward in history.
+func (h *Handlers) HandleForward(w http.ResponseWriter, r *http.Request) {
+ if !h.ensureChromeOrRespond(w) {
+ return
+ }
+ tabID := r.URL.Query().Get("tabId")
+ ctx, resolvedID, err := h.Bridge.TabContext(tabID)
+ if err != nil {
+ httpx.Error(w, 404, err)
+ return
+ }
+
+ var noHistory bool
+ if err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {
+ cur, entries, err := page.GetNavigationHistory().Do(ctx)
+ if err != nil {
+ return fmt.Errorf("get history: %w", err)
+ }
+ if cur < 0 || cur >= int64(len(entries)-1) {
+ noHistory = true
+ return nil
+ }
+ return page.NavigateToHistoryEntry(entries[cur+1].ID).Do(ctx)
+ })); err != nil {
+ httpx.Error(w, 500, fmt.Errorf("forward: %w", err))
+ return
+ }
+ if !noHistory {
+ time.Sleep(200 * time.Millisecond)
+ }
+
+ var curURL string
+ _ = chromedp.Run(ctx, chromedp.Location(&curURL))
+ httpx.JSON(w, 200, map[string]any{"tabId": resolvedID, "url": curURL})
+}
+
+// HandleReload reloads the current (or specified) tab.
+func (h *Handlers) HandleReload(w http.ResponseWriter, r *http.Request) {
+ if !h.ensureChromeOrRespond(w) {
+ return
+ }
+ tabID := r.URL.Query().Get("tabId")
+ ctx, resolvedID, err := h.Bridge.TabContext(tabID)
+ if err != nil {
+ httpx.Error(w, 404, err)
+ return
+ }
+
+ if err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {
+ return page.Reload().Do(ctx)
+ })); err != nil {
+ httpx.Error(w, 500, fmt.Errorf("reload: %w", err))
+ return
+ }
+
+ var curURL string
+ _ = chromedp.Run(ctx, chromedp.Location(&curURL))
+ httpx.JSON(w, 200, map[string]any{"tabId": resolvedID, "url": curURL})
+}
diff --git a/internal/handlers/middleware.go b/internal/handlers/middleware.go
index c34cae14..864ff429 100644
--- a/internal/handlers/middleware.go
+++ b/internal/handlers/middleware.go
@@ -6,9 +6,7 @@ import (
"encoding/hex"
"log/slog"
"net/http"
- "net/url"
"strings"
- "sync"
"sync/atomic"
"time"
@@ -26,13 +24,6 @@ var (
metricRequestLatencyN uint64
metricRateLimited uint64
metricStaleRefRetries uint64
-
- streamMu sync.Mutex
- streamConnections = map[string]int{}
-)
-
-const (
- maxConcurrentStreamRequestsPerHost = 8
)
const (
@@ -129,7 +120,6 @@ func AuthMiddlewareWithSessions(cfg *config.RuntimeConfig, sessions *browsersess
httpx.ErrorCode(w, 401, "bad_session", "invalid or expired agent session", false, nil)
return
}
- // Inject agent identity into request headers for activity tracking
r.Header.Set(activity.HeaderAgentID, sess.AgentID)
r.Header.Set(activity.HeaderPTSessionID, sess.ID)
activity.EnrichRequest(r, activity.Update{
@@ -178,526 +168,6 @@ func AuthMiddlewareWithSessions(cfg *config.RuntimeConfig, sessions *browsersess
})
}
-func sessionRequestAllowed(r *http.Request, sess *session.Session) bool {
- method := strings.ToUpper(strings.TrimSpace(r.Method))
- path := strings.TrimSpace(r.URL.Path)
- if method == http.MethodPost && sessionRevokePath(path) {
- return true
- }
- if sessionAdminRoute(method, path) {
- return false
- }
- if sess == nil {
- return false
- }
-
- grants := normalizedSessionGrants(sess.Grants)
- if len(grants) == 0 || grants["*"] {
- return true
- }
-
- for grant := range grants {
- if sessionGrantAllows(grant, method, path) {
- return true
- }
- }
- return false
-}
-
-func normalizedSessionGrants(grants []string) map[string]bool {
- out := make(map[string]bool, len(grants))
- for _, grant := range grants {
- normalized := strings.ToLower(strings.TrimSpace(grant))
- if normalized == "" {
- continue
- }
- out[normalized] = true
- }
- return out
-}
-
-func sessionAdminRoute(method, path string) bool {
- switch {
- case method == http.MethodGet && path == "/api/config":
- return true
- case method == http.MethodPut && path == "/api/config":
- return true
- case method == http.MethodPost && path == "/shutdown":
- return true
- case method == http.MethodPost && (path == "/browser/restart" || path == "/ensure-chrome"):
- return true
- case method == http.MethodPost && path == "/fingerprint/rotate":
- return true
- case method == http.MethodGet && path == "/api/events":
- return true
- case method == http.MethodGet && path == "/api/metrics":
- return true
- case method == http.MethodGet && path == "/api/agents":
- return true
- case method == http.MethodGet && strings.HasPrefix(path, "/api/agents/") && !strings.HasSuffix(path, "/events"):
- return true
- case path == "/sessions" || strings.HasPrefix(path, "/sessions/"):
- return path != "/sessions/me"
- case path == "/instances" || strings.HasPrefix(path, "/instances/"):
- return true
- case path == "/profiles" || strings.HasPrefix(path, "/profiles/"):
- return true
- case method == http.MethodGet && path == "/cache/status":
- return true
- case method == http.MethodPost && path == "/cache/clear":
- return true
- default:
- return false
- }
-}
-
-func sessionRevokePath(path string) bool {
- if !strings.HasPrefix(path, "/sessions/") || !strings.HasSuffix(path, "/revoke") {
- return false
- }
- sessionID := strings.TrimSuffix(strings.TrimPrefix(path, "/sessions/"), "/revoke")
- return strings.Trim(sessionID, "/") != ""
-}
-
-func sessionGrantAllows(grant, method, path string) bool {
- switch grant {
- case "browse":
- return sessionBrowseGrantAllows(method, path)
- case "network":
- return sessionNetworkGrantAllows(method, path)
- case "media":
- return sessionMediaGrantAllows(method, path)
- case "cookies":
- return sessionCookiesGrantAllows(method, path)
- case "clipboard":
- return sessionClipboardGrantAllows(method, path)
- case "evaluate":
- return sessionEvaluateGrantAllows(method, path)
- case "storage":
- return sessionStorageGrantAllows(method, path)
- case "console":
- return sessionConsoleGrantAllows(method, path)
- case "solve":
- return sessionSolveGrantAllows(method, path)
- case "tasks":
- return sessionTasksGrantAllows(method, path)
- case "activity":
- return sessionActivityGrantAllows(method, path)
- default:
- return false
- }
-}
-
-func sessionBrowseGrantAllows(method, path string) bool {
- switch method {
- case http.MethodGet:
- switch {
- case path == "/tabs",
- path == "/navigate",
- path == "/action",
- path == "/snapshot",
- path == "/screenshot",
- path == "/text",
- path == "/openapi.json",
- path == "/help",
- path == "/health",
- path == "/sessions/me":
- return true
- case tabRouteHasSuffix(path, "/snapshot"),
- tabRouteHasSuffix(path, "/screenshot"),
- tabRouteHasSuffix(path, "/text"),
- tabRouteHasSuffix(path, "/metrics"):
- return true
- }
- case http.MethodPost:
- switch {
- case path == "/tab",
- path == "/navigate",
- path == "/back",
- path == "/forward",
- path == "/reload",
- path == "/action",
- path == "/actions",
- path == "/macro",
- path == "/find",
- path == "/wait",
- path == "/dialog",
- path == "/lock",
- path == "/unlock":
- return true
- case tabRouteHasSuffix(path, "/navigate"),
- tabRouteHasSuffix(path, "/back"),
- tabRouteHasSuffix(path, "/forward"),
- tabRouteHasSuffix(path, "/reload"),
- tabRouteHasSuffix(path, "/action"),
- tabRouteHasSuffix(path, "/actions"),
- tabRouteHasSuffix(path, "/find"),
- tabRouteHasSuffix(path, "/wait"),
- tabRouteHasSuffix(path, "/dialog"),
- tabRouteHasSuffix(path, "/lock"),
- tabRouteHasSuffix(path, "/unlock"):
- return true
- }
- }
- return false
-}
-
-func sessionNetworkGrantAllows(method, path string) bool {
- switch method {
- case http.MethodGet:
- switch {
- case path == "/network",
- path == "/network/stream",
- path == "/network/export",
- path == "/network/export/stream":
- return true
- case strings.HasPrefix(path, "/network/"):
- return true
- case tabRouteHasSuffix(path, "/network"),
- tabRouteHasSuffix(path, "/network/stream"),
- tabRouteHasSuffix(path, "/network/export"),
- tabRouteHasSuffix(path, "/network/export/stream"):
- return true
- case strings.HasPrefix(path, "/tabs/") && strings.Contains(path, "/network/"):
- return true
- }
- case http.MethodPost:
- return path == "/network/clear"
- }
- return false
-}
-
-func sessionMediaGrantAllows(method, path string) bool {
- switch method {
- case http.MethodGet:
- switch {
- case path == "/pdf",
- path == "/download",
- path == "/screencast",
- path == "/screencast/tabs":
- return true
- case tabRouteHasSuffix(path, "/pdf"),
- tabRouteHasSuffix(path, "/download"):
- return true
- }
- case http.MethodPost:
- switch {
- case path == "/pdf",
- path == "/upload":
- return true
- case tabRouteHasSuffix(path, "/pdf"),
- tabRouteHasSuffix(path, "/upload"):
- return true
- }
- }
- return false
-}
-
-func sessionCookiesGrantAllows(method, path string) bool {
- switch method {
- case http.MethodGet, http.MethodPost:
- return path == "/cookies" || tabRouteHasSuffix(path, "/cookies")
- default:
- return false
- }
-}
-
-func sessionClipboardGrantAllows(method, path string) bool {
- switch method {
- case http.MethodGet:
- return path == "/clipboard/read" || path == "/clipboard/paste"
- case http.MethodPost:
- return path == "/clipboard/write" || path == "/clipboard/copy"
- default:
- return false
- }
-}
-
-func sessionEvaluateGrantAllows(method, path string) bool {
- if method != http.MethodPost {
- return false
- }
- return path == "/evaluate" || tabRouteHasSuffix(path, "/evaluate")
-}
-
-func sessionStorageGrantAllows(method, path string) bool {
- switch method {
- case http.MethodGet:
- return path == "/storage" || path == "/state/list" || path == "/state/show"
- case http.MethodPost:
- return path == "/storage" || path == "/state/save" || path == "/state/load" || path == "/state/clean"
- case http.MethodDelete:
- return path == "/storage" || path == "/state"
- default:
- return false
- }
-}
-
-func sessionConsoleGrantAllows(method, path string) bool {
- switch method {
- case http.MethodGet:
- return path == "/console" || path == "/errors"
- case http.MethodPost:
- return path == "/console/clear" || path == "/errors/clear"
- default:
- return false
- }
-}
-
-func sessionSolveGrantAllows(method, path string) bool {
- switch method {
- case http.MethodGet:
- return path == "/solvers" || path == "/config/autosolver"
- case http.MethodPost:
- switch {
- case path == "/solve" || strings.HasPrefix(path, "/solve/"):
- return true
- case tabRouteHasSuffix(path, "/solve") || (strings.HasPrefix(path, "/tabs/") && strings.Contains(path, "/solve/")):
- return true
- }
- }
- return false
-}
-
-func sessionTasksGrantAllows(method, path string) bool {
- switch method {
- case http.MethodGet:
- return path == "/tasks" || path == "/scheduler/stats" || strings.HasPrefix(path, "/tasks/")
- case http.MethodPost:
- return path == "/tasks" || path == "/tasks/batch" || (strings.HasPrefix(path, "/tasks/") && strings.HasSuffix(path, "/cancel"))
- default:
- return false
- }
-}
-
-func sessionActivityGrantAllows(method, path string) bool {
- switch method {
- case http.MethodGet:
- return path == "/api/activity" || (strings.HasPrefix(path, "/api/agents/") && strings.HasSuffix(path, "/events"))
- case http.MethodPost:
- return strings.HasPrefix(path, "/api/agents/") && strings.HasSuffix(path, "/events")
- default:
- return false
- }
-}
-
-func tabRouteHasSuffix(path, suffix string) bool {
- return strings.HasPrefix(path, "/tabs/") && strings.HasSuffix(path, suffix)
-}
-
-func isPublicDashboardPath(path string) bool {
- switch path {
- case "/", "/login", "/dashboard", "/dashboard/":
- return true
- }
- return strings.HasPrefix(path, "/dashboard/") || path == "/dashboard/favicon.png"
-}
-
-func isPublicAuthPath(path string) bool {
- switch path {
- case "/api/auth/login", "/api/auth/logout":
- return true
- default:
- return false
- }
-}
-
-func cookieAuthAllowed(r *http.Request) bool {
- path := strings.TrimSpace(r.URL.Path)
- switch r.Method {
- case http.MethodGet:
- switch {
- case path == "/health",
- path == "/metrics",
- path == "/api/metrics",
- path == "/api/activity",
- path == "/api/agents",
- path == "/api/events",
- path == "/api/config",
- path == "/sessions",
- strings.HasPrefix(path, "/sessions/"),
- path == "/profiles",
- path == "/instances",
- path == "/instances/tabs",
- path == "/instances/metrics":
- return true
- case strings.HasPrefix(path, "/instances/") && strings.HasSuffix(path, "/tabs"),
- strings.HasPrefix(path, "/api/agents/") && !strings.HasSuffix(path, "/events"),
- strings.HasPrefix(path, "/api/agents/") && strings.HasSuffix(path, "/events"),
- strings.HasPrefix(path, "/instances/") && strings.HasSuffix(path, "/logs"),
- strings.HasPrefix(path, "/instances/") && strings.HasSuffix(path, "/logs/stream"),
- strings.HasPrefix(path, "/instances/") && strings.HasSuffix(path, "/proxy/screencast"),
- strings.HasPrefix(path, "/tabs/") && strings.HasSuffix(path, "/screenshot"),
- strings.HasPrefix(path, "/tabs/") && strings.HasSuffix(path, "/pdf"):
- return true
- }
- case http.MethodPost:
- switch {
- case path == "/api/auth/elevate":
- return true
- case strings.HasPrefix(path, "/api/agents/") && strings.HasSuffix(path, "/events"):
- return true
- case path == "/action":
- return true
- case path == "/instances/start":
- return true
- case path == "/instances/launch":
- return true
- case strings.HasPrefix(path, "/tabs/") && strings.HasSuffix(path, "/close"):
- return true
- case strings.HasPrefix(path, "/instances/") && strings.HasSuffix(path, "/stop"):
- return true
- case path == "/profiles":
- return true
- }
- case http.MethodPut:
- return path == "/api/config"
- case http.MethodPatch:
- return strings.HasPrefix(path, "/profiles/")
- case http.MethodDelete:
- return strings.HasPrefix(path, "/profiles/")
- }
- return false
-}
-
-func cookieElevationRequired(r *http.Request, cfg *config.RuntimeConfig) bool {
- if cfg == nil || !cfg.Sessions.Dashboard.RequireElevation {
- return false
- }
- path := strings.TrimSpace(r.URL.Path)
- switch r.Method {
- case http.MethodPut:
- return path == "/api/config"
- case http.MethodPost:
- return path == "/shutdown"
- }
- return false
-}
-
-func CorsMiddleware(cfg *config.RuntimeConfig, next http.Handler) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- allowedOrigin := corsAllowedOrigin(cfg, r)
- if allowedOrigin != "" {
- w.Header().Set("Access-Control-Allow-Origin", allowedOrigin)
- if allowedOrigin != "*" {
- w.Header().Set("Access-Control-Allow-Credentials", "true")
- w.Header().Add("Vary", "Origin")
- }
- }
- w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS")
- w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
- if r.Method == "OPTIONS" {
- if strings.TrimSpace(r.Header.Get("Origin")) != "" && allowedOrigin == "" && strings.TrimSpace(cfg.Token) != "" {
- httpx.ErrorCode(w, 403, "cors_forbidden", "cross-origin requests are disabled when auth is enabled", false, nil)
- return
- }
- w.WriteHeader(204)
- return
- }
- next.ServeHTTP(w, r)
- })
-}
-
-func corsAllowedOrigin(cfg *config.RuntimeConfig, r *http.Request) string {
- origin := strings.TrimSpace(r.Header.Get("Origin"))
- if origin == "" {
- return ""
- }
- if strings.TrimSpace(cfg.Token) == "" {
- return "*"
- }
- if sameOriginRequest(origin, r, cfg.TrustProxyHeaders) {
- return origin
- }
- return ""
-}
-
-func sameOriginRequest(origin string, r *http.Request, trustProxy ...bool) bool {
- parsed, err := url.Parse(origin)
- if err != nil || parsed.Scheme == "" || parsed.Host == "" {
- return false
- }
- trust := len(trustProxy) > 0 && trustProxy[0]
- return strings.EqualFold(parsed.Scheme, requestScheme(r, trust)) && strings.EqualFold(parsed.Host, requestHost(r, trust))
-}
-
-func cookieOriginAllowed(r *http.Request, trustProxy bool) bool {
- if isWebSocketUpgrade(r) {
- origin := strings.TrimSpace(r.Header.Get("Origin"))
- return origin != "" && sameOriginRequest(origin, r, trustProxy)
- }
-
- if origin := strings.TrimSpace(r.Header.Get("Origin")); origin != "" {
- return sameOriginRequest(origin, r, trustProxy)
- }
- if referer := strings.TrimSpace(r.Header.Get("Referer")); referer != "" {
- return sameOriginRequest(referer, r, trustProxy)
- }
- return false
-}
-
-func isWebSocketUpgrade(r *http.Request) bool {
- if r == nil {
- return false
- }
- if !strings.EqualFold(strings.TrimSpace(r.Header.Get("Upgrade")), "websocket") {
- return false
- }
- return strings.Contains(strings.ToLower(r.Header.Get("Connection")), "upgrade")
-}
-
-func cookieSecureSetting(cfg *config.RuntimeConfig) *bool {
- if cfg == nil {
- return nil
- }
- return cfg.CookieSecure
-}
-
-func requestScheme(r *http.Request, trustProxy bool) string {
- if r == nil {
- return "http"
- }
- if trustProxy {
- if forwarded := strings.TrimSpace(r.Header.Get("X-Forwarded-Proto")); forwarded != "" {
- return strings.ToLower(strings.TrimSpace(strings.Split(forwarded, ",")[0]))
- }
- if forwarded := strings.TrimSpace(r.Header.Get("Forwarded")); forwarded != "" {
- for _, part := range strings.Split(forwarded, ";") {
- key, value, ok := strings.Cut(strings.TrimSpace(part), "=")
- if !ok || !strings.EqualFold(key, "proto") {
- continue
- }
- return strings.ToLower(strings.Trim(value, `"`))
- }
- }
- }
- if r.TLS != nil {
- return "https"
- }
- return "http"
-}
-
-func requestHost(r *http.Request, trustProxy bool) string {
- if r == nil {
- return ""
- }
- if trustProxy {
- if forwarded := strings.TrimSpace(r.Header.Get("X-Forwarded-Host")); forwarded != "" {
- return strings.TrimSpace(strings.Split(forwarded, ",")[0])
- }
- if forwarded := strings.TrimSpace(r.Header.Get("Forwarded")); forwarded != "" {
- for _, part := range strings.Split(forwarded, ";") {
- key, value, ok := strings.Cut(strings.TrimSpace(part), "=")
- if !ok || !strings.EqualFold(key, "host") {
- continue
- }
- return strings.Trim(value, `"`)
- }
- }
- }
- return strings.TrimSpace(r.Host)
-}
-
func RequestIDMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rid := r.Header.Get("X-Request-Id")
@@ -711,127 +181,3 @@ func RequestIDMiddleware(next http.Handler) http.Handler {
next.ServeHTTP(w, r)
})
}
-
-var (
- rateMu sync.Mutex
- rateBuckets = map[string][]time.Time{}
- rateLimiterStarted sync.Once
-)
-
-const (
- rateLimitWindow = 10 * time.Second
- rateLimitMaxReq = 300
- evictionInterval = 30 * time.Second
-)
-
-func RateLimitMiddleware(next http.Handler) http.Handler {
- startRateLimiterJanitor(rateLimitWindow, evictionInterval)
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- if isLongLivedStreamRequest(r) {
- host := authn.ClientIP(r)
- if !acquireStreamConnection(host) {
- atomic.AddUint64(&metricRateLimited, 1)
- httpx.ErrorCode(w, 429, "stream_limit_reached", "too many concurrent streaming connections", true, map[string]any{
- "maxConcurrent": maxConcurrentStreamRequestsPerHost,
- })
- return
- }
- defer releaseStreamConnection(host)
- next.ServeHTTP(w, r)
- return
- }
-
- host := authn.ClientIP(r)
-
- now := time.Now()
- rateMu.Lock()
- hits := rateBuckets[host]
- filtered := hits[:0]
- for _, t := range hits {
- if now.Sub(t) < rateLimitWindow {
- filtered = append(filtered, t)
- }
- }
- if len(filtered) >= rateLimitMaxReq {
- rateBuckets[host] = filtered
- rateMu.Unlock()
- atomic.AddUint64(&metricRateLimited, 1)
- httpx.ErrorCode(w, 429, "rate_limited", "too many requests", true, map[string]any{"windowSec": int(rateLimitWindow.Seconds()), "max": rateLimitMaxReq})
- return
- }
- rateBuckets[host] = append(filtered, now)
- rateMu.Unlock()
-
- next.ServeHTTP(w, r)
- })
-}
-
-func isLongLivedStreamRequest(r *http.Request) bool {
- if r == nil || r.Method != http.MethodGet {
- return false
- }
- path := strings.TrimSpace(r.URL.Path)
- switch {
- case path == "/api/events":
- return true
- case strings.HasPrefix(path, "/api/agents/") && strings.HasSuffix(path, "/events"):
- return true
- case strings.HasPrefix(path, "/instances/") && strings.HasSuffix(path, "/logs/stream"):
- return true
- default:
- return false
- }
-}
-
-func acquireStreamConnection(host string) bool {
- streamMu.Lock()
- defer streamMu.Unlock()
-
- if streamConnections[host] >= maxConcurrentStreamRequestsPerHost {
- return false
- }
- streamConnections[host]++
- return true
-}
-
-func releaseStreamConnection(host string) {
- streamMu.Lock()
- defer streamMu.Unlock()
-
- current := streamConnections[host]
- if current <= 1 {
- delete(streamConnections, host)
- return
- }
- streamConnections[host] = current - 1
-}
-
-func startRateLimiterJanitor(window, interval time.Duration) {
- rateLimiterStarted.Do(func() {
- go func() {
- ticker := time.NewTicker(interval)
- defer ticker.Stop()
- for now := range ticker.C {
- evictStaleRateBuckets(now, window)
- }
- }()
- })
-}
-
-func evictStaleRateBuckets(now time.Time, window time.Duration) {
- rateMu.Lock()
- defer rateMu.Unlock()
- for host, hits := range rateBuckets {
- filtered := hits[:0]
- for _, t := range hits {
- if now.Sub(t) < window {
- filtered = append(filtered, t)
- }
- }
- if len(filtered) == 0 {
- delete(rateBuckets, host)
- } else {
- rateBuckets[host] = filtered
- }
- }
-}
diff --git a/internal/handlers/middleware_cookie_auth.go b/internal/handlers/middleware_cookie_auth.go
new file mode 100644
index 00000000..9bbf55a0
--- /dev/null
+++ b/internal/handlers/middleware_cookie_auth.go
@@ -0,0 +1,184 @@
+package handlers
+
+import (
+ "net/http"
+ "net/url"
+ "strings"
+
+ "github.com/pinchtab/pinchtab/internal/config"
+)
+
+func isPublicDashboardPath(path string) bool {
+ switch path {
+ case "/", "/login", "/dashboard", "/dashboard/":
+ return true
+ }
+ return strings.HasPrefix(path, "/dashboard/") || path == "/dashboard/favicon.png"
+}
+
+func isPublicAuthPath(path string) bool {
+ switch path {
+ case "/api/auth/login", "/api/auth/logout":
+ return true
+ default:
+ return false
+ }
+}
+
+func cookieAuthAllowed(r *http.Request) bool {
+ path := strings.TrimSpace(r.URL.Path)
+ switch r.Method {
+ case http.MethodGet:
+ switch {
+ case path == "/health",
+ path == "/metrics",
+ path == "/api/metrics",
+ path == "/api/activity",
+ path == "/api/agents",
+ path == "/api/events",
+ path == "/api/config",
+ path == "/sessions",
+ strings.HasPrefix(path, "/sessions/"),
+ path == "/profiles",
+ path == "/instances",
+ path == "/instances/tabs",
+ path == "/instances/metrics":
+ return true
+ case strings.HasPrefix(path, "/instances/") && strings.HasSuffix(path, "/tabs"),
+ strings.HasPrefix(path, "/api/agents/") && !strings.HasSuffix(path, "/events"),
+ strings.HasPrefix(path, "/api/agents/") && strings.HasSuffix(path, "/events"),
+ strings.HasPrefix(path, "/instances/") && strings.HasSuffix(path, "/logs"),
+ strings.HasPrefix(path, "/instances/") && strings.HasSuffix(path, "/logs/stream"),
+ strings.HasPrefix(path, "/instances/") && strings.HasSuffix(path, "/proxy/screencast"),
+ strings.HasPrefix(path, "/tabs/") && strings.HasSuffix(path, "/screenshot"),
+ strings.HasPrefix(path, "/tabs/") && strings.HasSuffix(path, "/pdf"):
+ return true
+ }
+ case http.MethodPost:
+ switch {
+ case path == "/api/auth/elevate":
+ return true
+ case strings.HasPrefix(path, "/api/agents/") && strings.HasSuffix(path, "/events"):
+ return true
+ case path == "/action":
+ return true
+ case path == "/instances/start":
+ return true
+ case path == "/instances/launch":
+ return true
+ case strings.HasPrefix(path, "/tabs/") && strings.HasSuffix(path, "/close"):
+ return true
+ case strings.HasPrefix(path, "/instances/") && strings.HasSuffix(path, "/stop"):
+ return true
+ case path == "/profiles":
+ return true
+ }
+ case http.MethodPut:
+ return path == "/api/config"
+ case http.MethodPatch:
+ return strings.HasPrefix(path, "/profiles/")
+ case http.MethodDelete:
+ return strings.HasPrefix(path, "/profiles/")
+ }
+ return false
+}
+
+func cookieElevationRequired(r *http.Request, cfg *config.RuntimeConfig) bool {
+ if cfg == nil || !cfg.Sessions.Dashboard.RequireElevation {
+ return false
+ }
+ path := strings.TrimSpace(r.URL.Path)
+ switch r.Method {
+ case http.MethodPut:
+ return path == "/api/config"
+ case http.MethodPost:
+ return path == "/shutdown"
+ }
+ return false
+}
+
+func cookieOriginAllowed(r *http.Request, trustProxy bool) bool {
+ if isWebSocketUpgrade(r) {
+ origin := strings.TrimSpace(r.Header.Get("Origin"))
+ return origin != "" && sameOriginRequest(origin, r, trustProxy)
+ }
+
+ if origin := strings.TrimSpace(r.Header.Get("Origin")); origin != "" {
+ return sameOriginRequest(origin, r, trustProxy)
+ }
+ if referer := strings.TrimSpace(r.Header.Get("Referer")); referer != "" {
+ return sameOriginRequest(referer, r, trustProxy)
+ }
+ return false
+}
+
+func cookieSecureSetting(cfg *config.RuntimeConfig) *bool {
+ if cfg == nil {
+ return nil
+ }
+ return cfg.CookieSecure
+}
+
+func sameOriginRequest(origin string, r *http.Request, trustProxy ...bool) bool {
+ parsed, err := url.Parse(origin)
+ if err != nil || parsed.Scheme == "" || parsed.Host == "" {
+ return false
+ }
+ trust := len(trustProxy) > 0 && trustProxy[0]
+ return strings.EqualFold(parsed.Scheme, requestScheme(r, trust)) && strings.EqualFold(parsed.Host, requestHost(r, trust))
+}
+
+func isWebSocketUpgrade(r *http.Request) bool {
+ if r == nil {
+ return false
+ }
+ if !strings.EqualFold(strings.TrimSpace(r.Header.Get("Upgrade")), "websocket") {
+ return false
+ }
+ return strings.Contains(strings.ToLower(r.Header.Get("Connection")), "upgrade")
+}
+
+func requestScheme(r *http.Request, trustProxy bool) string {
+ if r == nil {
+ return "http"
+ }
+ if trustProxy {
+ if forwarded := strings.TrimSpace(r.Header.Get("X-Forwarded-Proto")); forwarded != "" {
+ return strings.ToLower(strings.TrimSpace(strings.Split(forwarded, ",")[0]))
+ }
+ if forwarded := strings.TrimSpace(r.Header.Get("Forwarded")); forwarded != "" {
+ for _, part := range strings.Split(forwarded, ";") {
+ key, value, ok := strings.Cut(strings.TrimSpace(part), "=")
+ if !ok || !strings.EqualFold(key, "proto") {
+ continue
+ }
+ return strings.ToLower(strings.Trim(value, `"`))
+ }
+ }
+ }
+ if r.TLS != nil {
+ return "https"
+ }
+ return "http"
+}
+
+func requestHost(r *http.Request, trustProxy bool) string {
+ if r == nil {
+ return ""
+ }
+ if trustProxy {
+ if forwarded := strings.TrimSpace(r.Header.Get("X-Forwarded-Host")); forwarded != "" {
+ return strings.TrimSpace(strings.Split(forwarded, ",")[0])
+ }
+ if forwarded := strings.TrimSpace(r.Header.Get("Forwarded")); forwarded != "" {
+ for _, part := range strings.Split(forwarded, ";") {
+ key, value, ok := strings.Cut(strings.TrimSpace(part), "=")
+ if !ok || !strings.EqualFold(key, "host") {
+ continue
+ }
+ return strings.Trim(value, `"`)
+ }
+ }
+ }
+ return strings.TrimSpace(r.Host)
+}
diff --git a/internal/handlers/middleware_cors.go b/internal/handlers/middleware_cors.go
new file mode 100644
index 00000000..aff3196b
--- /dev/null
+++ b/internal/handlers/middleware_cors.go
@@ -0,0 +1,47 @@
+package handlers
+
+import (
+ "net/http"
+ "strings"
+
+ "github.com/pinchtab/pinchtab/internal/config"
+ "github.com/pinchtab/pinchtab/internal/httpx"
+)
+
+func CorsMiddleware(cfg *config.RuntimeConfig, next http.Handler) http.Handler {
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ allowedOrigin := corsAllowedOrigin(cfg, r)
+ if allowedOrigin != "" {
+ w.Header().Set("Access-Control-Allow-Origin", allowedOrigin)
+ if allowedOrigin != "*" {
+ w.Header().Set("Access-Control-Allow-Credentials", "true")
+ w.Header().Add("Vary", "Origin")
+ }
+ }
+ w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS")
+ w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
+ if r.Method == "OPTIONS" {
+ if strings.TrimSpace(r.Header.Get("Origin")) != "" && allowedOrigin == "" && strings.TrimSpace(cfg.Token) != "" {
+ httpx.ErrorCode(w, 403, "cors_forbidden", "cross-origin requests are disabled when auth is enabled", false, nil)
+ return
+ }
+ w.WriteHeader(204)
+ return
+ }
+ next.ServeHTTP(w, r)
+ })
+}
+
+func corsAllowedOrigin(cfg *config.RuntimeConfig, r *http.Request) string {
+ origin := strings.TrimSpace(r.Header.Get("Origin"))
+ if origin == "" {
+ return ""
+ }
+ if strings.TrimSpace(cfg.Token) == "" {
+ return "*"
+ }
+ if sameOriginRequest(origin, r, cfg.TrustProxyHeaders) {
+ return origin
+ }
+ return ""
+}
diff --git a/internal/handlers/middleware_ratelimit.go b/internal/handlers/middleware_ratelimit.go
new file mode 100644
index 00000000..9c505502
--- /dev/null
+++ b/internal/handlers/middleware_ratelimit.go
@@ -0,0 +1,140 @@
+package handlers
+
+import (
+ "net/http"
+ "strings"
+ "sync"
+ "sync/atomic"
+ "time"
+
+ "github.com/pinchtab/pinchtab/internal/authn"
+ "github.com/pinchtab/pinchtab/internal/httpx"
+)
+
+const (
+ maxConcurrentStreamRequestsPerHost = 8
+ rateLimitWindow = 10 * time.Second
+ rateLimitMaxReq = 300
+ evictionInterval = 30 * time.Second
+)
+
+var (
+ streamMu sync.Mutex
+ streamConnections = map[string]int{}
+
+ rateMu sync.Mutex
+ rateBuckets = map[string][]time.Time{}
+ rateLimiterStarted sync.Once
+)
+
+func RateLimitMiddleware(next http.Handler) http.Handler {
+ startRateLimiterJanitor(rateLimitWindow, evictionInterval)
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if isLongLivedStreamRequest(r) {
+ host := authn.ClientIP(r)
+ if !acquireStreamConnection(host) {
+ atomic.AddUint64(&metricRateLimited, 1)
+ httpx.ErrorCode(w, 429, "stream_limit_reached", "too many concurrent streaming connections", true, map[string]any{
+ "maxConcurrent": maxConcurrentStreamRequestsPerHost,
+ })
+ return
+ }
+ defer releaseStreamConnection(host)
+ next.ServeHTTP(w, r)
+ return
+ }
+
+ host := authn.ClientIP(r)
+
+ now := time.Now()
+ rateMu.Lock()
+ hits := rateBuckets[host]
+ filtered := hits[:0]
+ for _, t := range hits {
+ if now.Sub(t) < rateLimitWindow {
+ filtered = append(filtered, t)
+ }
+ }
+ if len(filtered) >= rateLimitMaxReq {
+ rateBuckets[host] = filtered
+ rateMu.Unlock()
+ atomic.AddUint64(&metricRateLimited, 1)
+ httpx.ErrorCode(w, 429, "rate_limited", "too many requests", true, map[string]any{"windowSec": int(rateLimitWindow.Seconds()), "max": rateLimitMaxReq})
+ return
+ }
+ rateBuckets[host] = append(filtered, now)
+ rateMu.Unlock()
+
+ next.ServeHTTP(w, r)
+ })
+}
+
+func isLongLivedStreamRequest(r *http.Request) bool {
+ if r == nil || r.Method != http.MethodGet {
+ return false
+ }
+ path := strings.TrimSpace(r.URL.Path)
+ switch {
+ case path == "/api/events":
+ return true
+ case strings.HasPrefix(path, "/api/agents/") && strings.HasSuffix(path, "/events"):
+ return true
+ case strings.HasPrefix(path, "/instances/") && strings.HasSuffix(path, "/logs/stream"):
+ return true
+ default:
+ return false
+ }
+}
+
+func acquireStreamConnection(host string) bool {
+ streamMu.Lock()
+ defer streamMu.Unlock()
+
+ if streamConnections[host] >= maxConcurrentStreamRequestsPerHost {
+ return false
+ }
+ streamConnections[host]++
+ return true
+}
+
+func releaseStreamConnection(host string) {
+ streamMu.Lock()
+ defer streamMu.Unlock()
+
+ current := streamConnections[host]
+ if current <= 1 {
+ delete(streamConnections, host)
+ return
+ }
+ streamConnections[host] = current - 1
+}
+
+func startRateLimiterJanitor(window, interval time.Duration) {
+ rateLimiterStarted.Do(func() {
+ go func() {
+ ticker := time.NewTicker(interval)
+ defer ticker.Stop()
+ for now := range ticker.C {
+ evictStaleRateBuckets(now, window)
+ }
+ }()
+ })
+}
+
+func evictStaleRateBuckets(now time.Time, window time.Duration) {
+ rateMu.Lock()
+ defer rateMu.Unlock()
+ for host, hits := range rateBuckets {
+ filtered := hits[:0]
+ for _, t := range hits {
+ if now.Sub(t) < window {
+ filtered = append(filtered, t)
+ }
+ }
+ if len(filtered) == 0 {
+ delete(rateBuckets, host)
+ } else {
+ rateBuckets[host] = filtered
+ }
+ }
+}
diff --git a/internal/handlers/middleware_session_grants.go b/internal/handlers/middleware_session_grants.go
new file mode 100644
index 00000000..19ebf240
--- /dev/null
+++ b/internal/handlers/middleware_session_grants.go
@@ -0,0 +1,315 @@
+package handlers
+
+import (
+ "net/http"
+ "strings"
+
+ "github.com/pinchtab/pinchtab/internal/session"
+)
+
+func sessionRequestAllowed(r *http.Request, sess *session.Session) bool {
+ method := strings.ToUpper(strings.TrimSpace(r.Method))
+ path := strings.TrimSpace(r.URL.Path)
+ if method == http.MethodPost && sessionRevokePath(path) {
+ return true
+ }
+ if sessionAdminRoute(method, path) {
+ return false
+ }
+ if sess == nil {
+ return false
+ }
+
+ grants := normalizedSessionGrants(sess.Grants)
+ if len(grants) == 0 || grants["*"] {
+ return true
+ }
+
+ for grant := range grants {
+ if sessionGrantAllows(grant, method, path) {
+ return true
+ }
+ }
+ return false
+}
+
+func normalizedSessionGrants(grants []string) map[string]bool {
+ out := make(map[string]bool, len(grants))
+ for _, grant := range grants {
+ normalized := strings.ToLower(strings.TrimSpace(grant))
+ if normalized == "" {
+ continue
+ }
+ out[normalized] = true
+ }
+ return out
+}
+
+func sessionAdminRoute(method, path string) bool {
+ switch {
+ case method == http.MethodGet && path == "/api/config":
+ return true
+ case method == http.MethodPut && path == "/api/config":
+ return true
+ case method == http.MethodPost && path == "/shutdown":
+ return true
+ case method == http.MethodPost && (path == "/browser/restart" || path == "/ensure-chrome"):
+ return true
+ case method == http.MethodPost && path == "/fingerprint/rotate":
+ return true
+ case method == http.MethodGet && path == "/api/events":
+ return true
+ case method == http.MethodGet && path == "/api/metrics":
+ return true
+ case method == http.MethodGet && path == "/api/agents":
+ return true
+ case method == http.MethodGet && strings.HasPrefix(path, "/api/agents/") && !strings.HasSuffix(path, "/events"):
+ return true
+ case path == "/sessions" || strings.HasPrefix(path, "/sessions/"):
+ return path != "/sessions/me"
+ case path == "/instances" || strings.HasPrefix(path, "/instances/"):
+ return true
+ case path == "/profiles" || strings.HasPrefix(path, "/profiles/"):
+ return true
+ case method == http.MethodGet && path == "/cache/status":
+ return true
+ case method == http.MethodPost && path == "/cache/clear":
+ return true
+ default:
+ return false
+ }
+}
+
+func sessionRevokePath(path string) bool {
+ if !strings.HasPrefix(path, "/sessions/") || !strings.HasSuffix(path, "/revoke") {
+ return false
+ }
+ sessionID := strings.TrimSuffix(strings.TrimPrefix(path, "/sessions/"), "/revoke")
+ return strings.Trim(sessionID, "/") != ""
+}
+
+func sessionGrantAllows(grant, method, path string) bool {
+ switch grant {
+ case "browse":
+ return sessionBrowseGrantAllows(method, path)
+ case "network":
+ return sessionNetworkGrantAllows(method, path)
+ case "media":
+ return sessionMediaGrantAllows(method, path)
+ case "cookies":
+ return sessionCookiesGrantAllows(method, path)
+ case "clipboard":
+ return sessionClipboardGrantAllows(method, path)
+ case "evaluate":
+ return sessionEvaluateGrantAllows(method, path)
+ case "storage":
+ return sessionStorageGrantAllows(method, path)
+ case "console":
+ return sessionConsoleGrantAllows(method, path)
+ case "solve":
+ return sessionSolveGrantAllows(method, path)
+ case "tasks":
+ return sessionTasksGrantAllows(method, path)
+ case "activity":
+ return sessionActivityGrantAllows(method, path)
+ default:
+ return false
+ }
+}
+
+func sessionBrowseGrantAllows(method, path string) bool {
+ switch method {
+ case http.MethodGet:
+ switch {
+ case path == "/tabs",
+ path == "/navigate",
+ path == "/action",
+ path == "/snapshot",
+ path == "/screenshot",
+ path == "/text",
+ path == "/openapi.json",
+ path == "/help",
+ path == "/health",
+ path == "/sessions/me":
+ return true
+ case tabRouteHasSuffix(path, "/snapshot"),
+ tabRouteHasSuffix(path, "/screenshot"),
+ tabRouteHasSuffix(path, "/text"),
+ tabRouteHasSuffix(path, "/metrics"):
+ return true
+ }
+ case http.MethodPost:
+ switch {
+ case path == "/tab",
+ path == "/navigate",
+ path == "/back",
+ path == "/forward",
+ path == "/reload",
+ path == "/action",
+ path == "/actions",
+ path == "/macro",
+ path == "/find",
+ path == "/wait",
+ path == "/dialog",
+ path == "/lock",
+ path == "/unlock":
+ return true
+ case tabRouteHasSuffix(path, "/navigate"),
+ tabRouteHasSuffix(path, "/back"),
+ tabRouteHasSuffix(path, "/forward"),
+ tabRouteHasSuffix(path, "/reload"),
+ tabRouteHasSuffix(path, "/action"),
+ tabRouteHasSuffix(path, "/actions"),
+ tabRouteHasSuffix(path, "/find"),
+ tabRouteHasSuffix(path, "/wait"),
+ tabRouteHasSuffix(path, "/dialog"),
+ tabRouteHasSuffix(path, "/lock"),
+ tabRouteHasSuffix(path, "/unlock"):
+ return true
+ }
+ }
+ return false
+}
+
+func sessionNetworkGrantAllows(method, path string) bool {
+ switch method {
+ case http.MethodGet:
+ switch {
+ case path == "/network",
+ path == "/network/stream",
+ path == "/network/export",
+ path == "/network/export/stream":
+ return true
+ case strings.HasPrefix(path, "/network/"):
+ return true
+ case tabRouteHasSuffix(path, "/network"),
+ tabRouteHasSuffix(path, "/network/stream"),
+ tabRouteHasSuffix(path, "/network/export"),
+ tabRouteHasSuffix(path, "/network/export/stream"):
+ return true
+ case strings.HasPrefix(path, "/tabs/") && strings.Contains(path, "/network/"):
+ return true
+ }
+ case http.MethodPost:
+ return path == "/network/clear"
+ }
+ return false
+}
+
+func sessionMediaGrantAllows(method, path string) bool {
+ switch method {
+ case http.MethodGet:
+ switch {
+ case path == "/pdf",
+ path == "/download",
+ path == "/screencast",
+ path == "/screencast/tabs":
+ return true
+ case tabRouteHasSuffix(path, "/pdf"),
+ tabRouteHasSuffix(path, "/download"):
+ return true
+ }
+ case http.MethodPost:
+ switch {
+ case path == "/pdf",
+ path == "/upload":
+ return true
+ case tabRouteHasSuffix(path, "/pdf"),
+ tabRouteHasSuffix(path, "/upload"):
+ return true
+ }
+ }
+ return false
+}
+
+func sessionCookiesGrantAllows(method, path string) bool {
+ switch method {
+ case http.MethodGet, http.MethodPost:
+ return path == "/cookies" || tabRouteHasSuffix(path, "/cookies")
+ default:
+ return false
+ }
+}
+
+func sessionClipboardGrantAllows(method, path string) bool {
+ switch method {
+ case http.MethodGet:
+ return path == "/clipboard/read" || path == "/clipboard/paste"
+ case http.MethodPost:
+ return path == "/clipboard/write" || path == "/clipboard/copy"
+ default:
+ return false
+ }
+}
+
+func sessionEvaluateGrantAllows(method, path string) bool {
+ if method != http.MethodPost {
+ return false
+ }
+ return path == "/evaluate" || tabRouteHasSuffix(path, "/evaluate")
+}
+
+func sessionStorageGrantAllows(method, path string) bool {
+ switch method {
+ case http.MethodGet:
+ return path == "/storage" || path == "/state/list" || path == "/state/show"
+ case http.MethodPost:
+ return path == "/storage" || path == "/state/save" || path == "/state/load" || path == "/state/clean"
+ case http.MethodDelete:
+ return path == "/storage" || path == "/state"
+ default:
+ return false
+ }
+}
+
+func sessionConsoleGrantAllows(method, path string) bool {
+ switch method {
+ case http.MethodGet:
+ return path == "/console" || path == "/errors"
+ case http.MethodPost:
+ return path == "/console/clear" || path == "/errors/clear"
+ default:
+ return false
+ }
+}
+
+func sessionSolveGrantAllows(method, path string) bool {
+ switch method {
+ case http.MethodGet:
+ return path == "/solvers" || path == "/config/autosolver"
+ case http.MethodPost:
+ switch {
+ case path == "/solve" || strings.HasPrefix(path, "/solve/"):
+ return true
+ case tabRouteHasSuffix(path, "/solve") || (strings.HasPrefix(path, "/tabs/") && strings.Contains(path, "/solve/")):
+ return true
+ }
+ }
+ return false
+}
+
+func sessionTasksGrantAllows(method, path string) bool {
+ switch method {
+ case http.MethodGet:
+ return path == "/tasks" || path == "/scheduler/stats" || strings.HasPrefix(path, "/tasks/")
+ case http.MethodPost:
+ return path == "/tasks" || path == "/tasks/batch" || (strings.HasPrefix(path, "/tasks/") && strings.HasSuffix(path, "/cancel"))
+ default:
+ return false
+ }
+}
+
+func sessionActivityGrantAllows(method, path string) bool {
+ switch method {
+ case http.MethodGet:
+ return path == "/api/activity" || (strings.HasPrefix(path, "/api/agents/") && strings.HasSuffix(path, "/events"))
+ case http.MethodPost:
+ return strings.HasPrefix(path, "/api/agents/") && strings.HasSuffix(path, "/events")
+ default:
+ return false
+ }
+}
+
+func tabRouteHasSuffix(path, suffix string) bool {
+ return strings.HasPrefix(path, "/tabs/") && strings.HasSuffix(path, suffix)
+}
diff --git a/internal/handlers/navigation.go b/internal/handlers/navigation.go
index 0e1d303d..5d987d6c 100644
--- a/internal/handlers/navigation.go
+++ b/internal/handlers/navigation.go
@@ -13,9 +13,7 @@ import (
"strings"
"time"
- "github.com/chromedp/cdproto/page"
"github.com/chromedp/chromedp"
- "github.com/pinchtab/pinchtab/internal/activity"
"github.com/pinchtab/pinchtab/internal/bridge"
"github.com/pinchtab/pinchtab/internal/engine"
"github.com/pinchtab/pinchtab/internal/httpx"
@@ -145,7 +143,6 @@ func (h *Handlers) HandleNavigate(w http.ResponseWriter, r *http.Request) {
h.recordEngine(r, "chrome")
w.Header().Set("X-Engine", "chrome")
- // Ensure Chrome is initialized
if !h.ensureChromeOrRespond(w) {
return
}
@@ -276,7 +273,6 @@ func (h *Handlers) HandleNavigate(w http.ResponseWriter, r *http.Request) {
if len(blockPatterns) > 0 {
_ = bridge.SetResourceBlocking(tCtx, blockPatterns)
} else {
- // Clear any existing blocking patterns
_ = bridge.SetResourceBlocking(tCtx, nil)
}
@@ -362,277 +358,6 @@ func (h *Handlers) HandleTabNavigate(w http.ResponseWriter, r *http.Request) {
h.HandleNavigate(w, req)
}
-const tabActionNew = "new"
-
-func (h *Handlers) HandleTab(w http.ResponseWriter, r *http.Request) {
- var req struct {
- Action string `json:"action"`
- TabID string `json:"tabId"`
- URL string `json:"url"`
- }
- if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, maxBodySize)).Decode(&req); err != nil {
- httpx.Error(w, 400, fmt.Errorf("decode: %w", err))
- return
- }
-
- switch req.Action {
- case tabActionNew:
- var target *validatedNavigateTarget
- trustedResolveCIDRs := parseCIDRs(h.Config.TrustedResolveCIDRs)
- trustedCIDRs := parseCIDRs(h.Config.TrustedProxyCIDRs)
- if req.URL != "" && req.URL != "about:blank" {
- if err := validateNavigateURL(req.URL); err != nil {
- httpx.Error(w, 400, err)
- return
- }
- domainResult := h.IDPIGuard.CheckDomain(req.URL)
- if domainResult.Blocked {
- httpx.Error(w, http.StatusForbidden, fmt.Errorf("navigation blocked by IDPI: %s", domainResult.Reason))
- return
- }
- if domainResult.Threat {
- w.Header().Set("X-IDPI-Warning", domainResult.Reason)
- }
- var err error
- target, err = validateNavigateTarget(req.URL, h.IDPIGuard.DomainAllowed(req.URL), trustedResolveCIDRs)
- if err != nil {
- httpx.Error(w, http.StatusForbidden, err)
- return
- }
- }
-
- if !h.ensureChromeOrRespond(w) {
- return
- }
-
- // Create a blank tab first so the requested URL becomes the first
- // real history entry.
- newTabID, ctx, _, err := h.Bridge.CreateTab("")
- if err != nil {
- httpx.Error(w, 500, err)
- return
- }
-
- if req.URL != "" && req.URL != "about:blank" {
- tCtx, tCancel := context.WithTimeout(ctx, h.Config.NavigateTimeout)
- defer tCancel()
- go httpx.CancelOnClientDone(r.Context(), tCancel)
- navGuard, err := installNavigateRuntimeGuard(tCtx, tCancel, target, trustedCIDRs)
- if err != nil {
- _ = h.Bridge.CloseTab(newTabID)
- httpx.Error(w, 500, fmt.Errorf("navigation guard: %w", err))
- return
- }
- if err := bridge.NavigatePageWithRedirectLimit(tCtx, req.URL, h.Config.MaxRedirects); err != nil {
- if navGuard != nil {
- if blockedErr := navGuard.blocked(); blockedErr != nil {
- _ = h.Bridge.CloseTab(newTabID)
- httpx.Error(w, http.StatusForbidden, blockedErr)
- return
- }
- }
- _ = h.Bridge.CloseTab(newTabID)
- code := 500
- if errors.Is(err, bridge.ErrTooManyRedirects) {
- code = 422
- }
- navigateErrorWithHint(w, code, err, req.URL)
- return
- }
- }
-
- var curURL, title string
- _ = chromedp.Run(ctx, chromedp.Location(&curURL), chromedp.Title(&title))
-
- httpx.JSON(w, 200, map[string]any{"tabId": newTabID, "url": curURL, "title": title})
-
- case "focus":
- if req.TabID == "" {
- httpx.Error(w, 400, fmt.Errorf("tabId required"))
- return
- }
- if !h.ensureChromeOrRespond(w) {
- return
- }
- if err := h.Bridge.FocusTab(req.TabID); err != nil {
- httpx.Error(w, 404, err)
- return
- }
-
- h.recordActivity(r, activity.Update{Action: "tab.focus", TabID: req.TabID})
-
- httpx.JSON(w, 200, map[string]any{"focused": true, "tabId": req.TabID})
-
- default:
- httpx.Error(w, 400, fmt.Errorf("action must be 'new' or 'focus'; use /close to close a tab"))
- }
-}
-
-// HandleTabClose closes the tab identified by the path. It is the tab-scoped
-// equivalent of POST /close and exists so orchestrator dashboard commands can
-// use the common /tabs/{id}/... proxy path.
-func (h *Handlers) HandleTabClose(w http.ResponseWriter, r *http.Request) {
- tabID := strings.TrimSpace(r.PathValue("id"))
- if tabID == "" {
- httpx.Error(w, 400, fmt.Errorf("tab id required"))
- return
- }
-
- if r.Body != nil && r.Body != http.NoBody && r.ContentLength != 0 {
- var req struct {
- TabID string `json:"tabId"`
- }
- if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, maxBodySize)).Decode(&req); err != nil && !errors.Is(err, io.EOF) {
- httpx.Error(w, 400, fmt.Errorf("decode: %w", err))
- return
- }
- if req.TabID != "" && req.TabID != tabID {
- httpx.Error(w, 400, fmt.Errorf("tabId in body does not match path id"))
- return
- }
- }
-
- h.closeTab(w, r, tabID)
-}
-
-// HandleClose closes the tab identified by the JSON body, or the current/default
-// tab when tabId is omitted. It is the shorthand form of POST /tabs/{id}/close.
-func (h *Handlers) HandleClose(w http.ResponseWriter, r *http.Request) {
- var req struct {
- TabID string `json:"tabId"`
- }
- if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, maxBodySize)).Decode(&req); err != nil && !errors.Is(err, io.EOF) {
- httpx.Error(w, 400, fmt.Errorf("decode: %w", err))
- return
- }
-
- h.closeTab(w, r, strings.TrimSpace(req.TabID))
-}
-
-func (h *Handlers) closeTab(w http.ResponseWriter, r *http.Request, tabID string) {
- if !h.ensureChromeOrRespond(w) {
- return
- }
- if tabID == "" {
- _, resolvedTabID, err := h.tabContext(r, "")
- if err != nil {
- httpx.Error(w, 404, err)
- return
- }
- tabID = resolvedTabID
- }
-
- if err := h.Bridge.CloseTab(tabID); err != nil {
- httpx.Error(w, 500, err)
- return
- }
-
- h.recordActivity(r, activity.Update{Action: "tab.close", TabID: tabID})
- w.Header().Set(activity.HeaderPTTabID, tabID)
-
- httpx.JSON(w, 200, map[string]any{"closed": true, "tabId": tabID})
-}
-
-// HandleTabBack navigates a specific tab back in history.
-func (h *Handlers) HandleTabBack(w http.ResponseWriter, r *http.Request) {
- q := r.URL.Query()
- q.Set("tabId", r.PathValue("id"))
- r.URL.RawQuery = q.Encode()
- h.HandleBack(w, r)
-}
-
-// HandleTabForward navigates a specific tab forward in history.
-func (h *Handlers) HandleTabForward(w http.ResponseWriter, r *http.Request) {
- q := r.URL.Query()
- q.Set("tabId", r.PathValue("id"))
- r.URL.RawQuery = q.Encode()
- h.HandleForward(w, r)
-}
-
-// HandleTabReload reloads a specific tab.
-func (h *Handlers) HandleTabReload(w http.ResponseWriter, r *http.Request) {
- q := r.URL.Query()
- q.Set("tabId", r.PathValue("id"))
- r.URL.RawQuery = q.Encode()
- h.HandleReload(w, r)
-}
-
-// HandleBack navigates the current (or specified) tab back in history.
-func (h *Handlers) HandleBack(w http.ResponseWriter, r *http.Request) {
- if !h.ensureChromeOrRespond(w) {
- return
- }
- tabID := r.URL.Query().Get("tabId")
- ctx, resolvedID, err := h.Bridge.TabContext(tabID)
- if err != nil {
- httpx.Error(w, 404, err)
- return
- }
-
- // Use CDP directly instead of chromedp.NavigateBack() which wraps in
- // responseAction() and waits for Page.loadEventFired — hangs indefinitely.
- var noHistory bool
- if err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {
- cur, entries, err := page.GetNavigationHistory().Do(ctx)
- if err != nil {
- return fmt.Errorf("get history: %w", err)
- }
- if cur <= 0 || cur > int64(len(entries)-1) {
- noHistory = true
- return nil
- }
- return page.NavigateToHistoryEntry(entries[cur-1].ID).Do(ctx)
- })); err != nil {
- httpx.Error(w, 500, fmt.Errorf("back: %w", err))
- return
- }
- if !noHistory {
- time.Sleep(200 * time.Millisecond)
- }
-
- var curURL string
- _ = chromedp.Run(ctx, chromedp.Location(&curURL))
- httpx.JSON(w, 200, map[string]any{"tabId": resolvedID, "url": curURL})
-}
-
-// HandleForward navigates the current (or specified) tab forward in history.
-func (h *Handlers) HandleForward(w http.ResponseWriter, r *http.Request) {
- if !h.ensureChromeOrRespond(w) {
- return
- }
- tabID := r.URL.Query().Get("tabId")
- ctx, resolvedID, err := h.Bridge.TabContext(tabID)
- if err != nil {
- httpx.Error(w, 404, err)
- return
- }
-
- // Use CDP directly instead of chromedp.NavigateForward() which wraps in
- // responseAction() and waits for Page.loadEventFired — hangs indefinitely.
- var noHistory bool
- if err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {
- cur, entries, err := page.GetNavigationHistory().Do(ctx)
- if err != nil {
- return fmt.Errorf("get history: %w", err)
- }
- if cur < 0 || cur >= int64(len(entries)-1) {
- noHistory = true
- return nil
- }
- return page.NavigateToHistoryEntry(entries[cur+1].ID).Do(ctx)
- })); err != nil {
- httpx.Error(w, 500, fmt.Errorf("forward: %w", err))
- return
- }
- if !noHistory {
- time.Sleep(200 * time.Millisecond)
- }
-
- var curURL string
- _ = chromedp.Run(ctx, chromedp.Location(&curURL))
- httpx.JSON(w, 200, map[string]any{"tabId": resolvedID, "url": curURL})
-}
-
// binaryFileExtensions are extensions that Chrome cannot render and will abort on
var binaryFileExtensions = []string{".gz", ".zip", ".tar", ".rar", ".7z", ".bz2", ".xz", ".pdf", ".exe", ".bin", ".dmg", ".iso"}
@@ -665,28 +390,3 @@ func navigateErrorWithHint(w http.ResponseWriter, code int, err error, url strin
}
httpx.Error(w, code, fmt.Errorf("navigate: %w", err))
}
-
-// HandleReload reloads the current (or specified) tab.
-func (h *Handlers) HandleReload(w http.ResponseWriter, r *http.Request) {
- if !h.ensureChromeOrRespond(w) {
- return
- }
- tabID := r.URL.Query().Get("tabId")
- ctx, resolvedID, err := h.Bridge.TabContext(tabID)
- if err != nil {
- httpx.Error(w, 404, err)
- return
- }
-
- if err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {
- return page.Reload().Do(ctx)
- })); err != nil {
- httpx.Error(w, 500, fmt.Errorf("reload: %w", err))
- return
- }
-
- // Wait briefly for page to start loading
- var curURL string
- _ = chromedp.Run(ctx, chromedp.Location(&curURL))
- httpx.JSON(w, 200, map[string]any{"tabId": resolvedID, "url": curURL})
-}
diff --git a/internal/handlers/network_export.go b/internal/handlers/network_export.go
index 52bac586..d702af13 100644
--- a/internal/handlers/network_export.go
+++ b/internal/handlers/network_export.go
@@ -2,7 +2,6 @@ package handlers
import (
"context"
- "encoding/json"
"fmt"
"net/http"
"os"
@@ -11,7 +10,6 @@ import (
"strings"
"sync"
"time"
- "unicode/utf8"
"github.com/pinchtab/pinchtab/internal/bridge"
"github.com/pinchtab/pinchtab/internal/bridge/observe"
@@ -36,7 +34,7 @@ func CleanupStaleTmpExports(stateDir string) {
exportDir := filepath.Join(stateDir, "exports")
entries, err := os.ReadDir(exportDir)
if err != nil {
- return // directory doesn't exist yet — nothing to clean
+ return
}
cutoff := time.Now().Add(-staleTmpAge)
for _, entry := range entries {
@@ -92,17 +90,14 @@ func (h *Handlers) HandleNetworkExport(w http.ResponseWriter, r *http.Request) {
return
}
- // Tab lease enforcement (#12)
owner := resolveOwner(r, "")
if err := h.enforceTabLease(resolvedTabID, owner); err != nil {
httpx.ErrorCode(w, http.StatusLocked, "tab_locked", err.Error(), false, nil)
return
}
- // Activity recording (#13)
h.recordReadRequest(r, "network-export", resolvedTabID)
- // Resolve format from registry
formatName := r.URL.Query().Get("format")
if formatName == "" {
formatName = "har"
@@ -117,7 +112,6 @@ func (h *Handlers) HandleNetworkExport(w http.ResponseWriter, r *http.Request) {
return
}
- // Get network entries
nm := h.Bridge.NetworkMonitor()
if nm == nil {
enc := factory("PinchTab", h.version())
@@ -284,271 +278,6 @@ func (h *Handlers) writeExportFile(
return nil
}
-// HandleNetworkExportStream streams network entries to a file as they arrive.
-//
-// @Endpoint GET /network/export/stream
-// @Description Live capture: write entries to file as they are captured
-//
-// @Param tabId string query Tab ID
-// @Param format string query Export format (default: har)
-// @Param path string query Output filename (required)
-// @Param body string query "true" to include response bodies
-// @Param redact string query "false" to include sensitive headers (default: redacted)
-// @Param filter... (same as HandleNetworkExport)
-//
-// @Response 200 text/event-stream SSE progress events
-// @Response 423 application/json Tab is locked
-func (h *Handlers) HandleNetworkExportStream(w http.ResponseWriter, r *http.Request) {
- if err := h.ensureChrome(); err != nil {
- if h.writeBridgeUnavailable(w, err) {
- return
- }
- httpx.Error(w, 500, fmt.Errorf("chrome initialization: %w", err))
- return
- }
-
- userPath := r.URL.Query().Get("path")
- if userPath == "" {
- httpx.Error(w, 400, fmt.Errorf("path required for streaming export"))
- return
- }
-
- tabID := r.URL.Query().Get("tabId")
- tabCtx, resolvedTabID, err := h.tabContext(r, tabID)
- if err != nil {
- httpx.Error(w, 404, err)
- return
- }
- if _, ok := h.enforceCurrentTabDomainPolicy(w, r, tabCtx, resolvedTabID); !ok {
- return
- }
-
- owner := resolveOwner(r, "")
- if err := h.enforceTabLease(resolvedTabID, owner); err != nil {
- httpx.ErrorCode(w, http.StatusLocked, "tab_locked", err.Error(), false, nil)
- return
- }
-
- h.recordReadRequest(r, "network-export-stream", resolvedTabID)
-
- formatName := r.URL.Query().Get("format")
- if formatName == "" {
- formatName = "har"
- }
- factory := observe.GetFormat(formatName)
- if factory == nil {
- httpx.JSON(w, 400, map[string]any{
- "code": "unknown_format",
- "error": fmt.Sprintf("unknown format %q", formatName),
- "available": observe.ListFormats(),
- })
- return
- }
-
- nm := h.Bridge.NetworkMonitor()
- if nm == nil {
- httpx.Error(w, 500, fmt.Errorf("network monitor not available"))
- return
- }
-
- bufferSize := parseBufferSize(r)
- buf := nm.GetBuffer(resolvedTabID)
- if buf == nil {
- if err := nm.StartCaptureWithSize(tabCtx, resolvedTabID, bufferSize); err != nil {
- httpx.Error(w, 500, fmt.Errorf("start capture: %w", err))
- return
- }
- buf = nm.GetBuffer(resolvedTabID)
- }
-
- includeBody := r.URL.Query().Get("body") == "true"
- redactHeaders := r.URL.Query().Get("redact") != "false"
- filter := parseNetworkFilter(r)
-
- // Path safety (#1)
- exportDir := filepath.Join(h.Config.StateDir, "exports")
- if err := os.MkdirAll(exportDir, 0750); err != nil {
- httpx.Error(w, 500, fmt.Errorf("create dir: %w", err))
- return
- }
-
- safeName := filepath.Base(userPath)
- safePath, err := httpx.SafeCreatePath(exportDir, safeName)
- if err != nil {
- httpx.Error(w, 400, fmt.Errorf("invalid path: %w", err))
- return
- }
- absBase, _ := filepath.Abs(exportDir)
- absPath, err := filepath.Abs(safePath)
- if err != nil || !strings.HasPrefix(absPath, absBase+string(filepath.Separator)) {
- httpx.Error(w, 400, fmt.Errorf("path escapes export directory"))
- return
- }
-
- // Write to temp file, rename on finish (#8)
- tmpPath := absPath + ".tmp"
- f, err := os.OpenFile(tmpPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
- if err != nil {
- httpx.Error(w, 500, fmt.Errorf("create file: %w", err))
- return
- }
-
- enc := factory("PinchTab", h.version())
- if err := enc.Start(f); err != nil {
- _ = f.Close()
- _ = os.Remove(tmpPath)
- httpx.Error(w, 500, fmt.Errorf("start encoder: %w", err))
- return
- }
-
- // Semaphore to throttle concurrent body fetches in streaming mode.
- streamBodySem := make(chan struct{}, bodyFetchConcurrency)
-
- // Subscribe for live entries
- subID, ch := buf.Subscribe()
- defer buf.Unsubscribe(subID)
-
- // SSE setup
- flusher, ok := w.(http.Flusher)
- if !ok {
- _ = enc.Finish()
- _ = f.Close()
- _ = os.Remove(tmpPath)
- httpx.Error(w, 500, fmt.Errorf("streaming not supported"))
- return
- }
-
- w.Header().Set("Content-Type", "text/event-stream")
- w.Header().Set("Cache-Control", "no-cache")
- w.Header().Set("Connection", "keep-alive")
- w.Header().Set("X-Accel-Buffering", "no")
-
- // Set a hard deadline so forgotten streams don't leak resources.
- streamDeadline := time.NewTimer(maxExportStreamDuration)
- defer streamDeadline.Stop()
-
- // Clear per-write deadline for long-lived SSE (#7), the stream timer above caps total duration.
- _ = http.NewResponseController(w).SetWriteDeadline(time.Time{})
-
- count := 0
- keepalive := time.NewTicker(15 * time.Second)
- defer keepalive.Stop()
-
- var finalizeOnce sync.Once
- finalize := func() {
- finalizeOnce.Do(func() {
- _ = enc.Finish()
- if err := f.Close(); err == nil {
- // Atomic rename on success (#8)
- if count > 0 {
- _ = os.Rename(tmpPath, absPath)
- } else {
- _ = os.Remove(tmpPath)
- }
- } else {
- _ = os.Remove(tmpPath)
- }
- })
- }
-
- // sendDone emits the final SSE event with the correct path (empty when no file was written).
- sendDone := func() {
- result := map[string]any{"entries": count}
- if count > 0 {
- result["path"] = absPath
- }
- data, _ := json.Marshal(result)
- _, _ = fmt.Fprintf(w, "event: done\ndata: %s\n\n", data)
- flusher.Flush()
- }
-
- for {
- select {
- case <-r.Context().Done():
- finalize()
- // Don't write to ResponseWriter after client disconnect (#6)
- return
-
- case <-streamDeadline.C:
- finalize()
- data, _ := json.Marshal(map[string]any{"entries": count, "reason": "max_duration_reached"})
- _, _ = fmt.Fprintf(w, "event: timeout\ndata: %s\n\n", data)
- sendDone()
- return
-
- case entry, ok := <-ch:
- if !ok {
- finalize()
- sendDone()
- return
- }
-
- // The subscriber fires on requestWillBeSent (entry created) before
- // responseReceived/loadingFinished populate status, headers, timing.
- // Wait for the entry to be Finished before exporting it.
- if !entry.Finished && !entry.Failed {
- reqID := entry.RequestID
- waitDeadline := time.After(10 * time.Second)
- poll := time.NewTicker(200 * time.Millisecond)
- waitDone:
- for {
- select {
- case <-r.Context().Done():
- poll.Stop()
- finalize()
- return
- case <-waitDeadline:
- poll.Stop()
- if updated, found := buf.Get(reqID); found {
- entry = updated
- }
- break waitDone
- case <-poll.C:
- if updated, found := buf.Get(reqID); found && (updated.Finished || updated.Failed) {
- entry = updated
- poll.Stop()
- break waitDone
- }
- }
- }
- }
-
- if !filter.Match(entry) {
- continue
- }
- var body string
- var b64 bool
- if includeBody && entry.Finished && !entry.Failed {
- // Throttle body fetches to avoid saturating the CDP connection.
- streamBodySem <- struct{}{}
- body, b64, _ = nm.GetResponseBody(tabCtx, entry.RequestID)
- <-streamBodySem
- if len(body) > maxExportBodyBytes {
- body = ""
- b64 = false
- }
- }
- export := observe.NetworkEntryToExport(entry, body, b64)
- if redactHeaders {
- export.Request.Headers = observe.RedactSensitiveHeaders(export.Request.Headers)
- export.Response.Headers = observe.RedactSensitiveHeaders(export.Response.Headers)
- }
- if err := enc.Encode(export); err != nil {
- finalize()
- return
- }
- count++
- data, _ := json.Marshal(map[string]any{"entries": count, "url": safetruncateURL(entry.URL)})
- _, _ = fmt.Fprintf(w, "event: export\ndata: %s\n\n", data)
- flusher.Flush()
-
- case <-keepalive.C:
- _, _ = fmt.Fprintf(w, ": keepalive\n\n")
- flusher.Flush()
- }
- }
-}
-
// HandleTabNetworkExport handles GET /tabs/{id}/network/export.
func (h *Handlers) HandleTabNetworkExport(w http.ResponseWriter, r *http.Request) {
tabID := r.PathValue("id")
@@ -565,26 +294,6 @@ func (h *Handlers) HandleTabNetworkExport(w http.ResponseWriter, r *http.Request
h.HandleNetworkExport(w, req)
}
-// HandleTabNetworkExportStream handles GET /tabs/{id}/network/export/stream.
-func (h *Handlers) HandleTabNetworkExportStream(w http.ResponseWriter, r *http.Request) {
- tabID := r.PathValue("id")
- if tabID == "" {
- httpx.Error(w, 400, fmt.Errorf("tab id required"))
- return
- }
- q := r.URL.Query()
- q.Set("tabId", tabID)
- req := r.Clone(r.Context())
- u := *r.URL
- u.RawQuery = q.Encode()
- req.URL = &u
- h.HandleNetworkExportStream(w, req)
-}
-
-// ---------------------------------------------------------------------------
-// Helpers
-// ---------------------------------------------------------------------------
-
func parseNetworkFilter(r *http.Request) bridge.NetworkFilter {
f := bridge.NetworkFilter{
URLPattern: r.URL.Query().Get("filter"),
@@ -606,17 +315,3 @@ func (h *Handlers) version() string {
}
return "dev"
}
-
-// safetruncateURL truncates at a valid UTF-8 boundary (#21).
-func safetruncateURL(u string) string {
- const maxLen = 120
- if len(u) <= maxLen {
- return u
- }
- for i := maxLen; i > 0; i-- {
- if utf8.RuneStart(u[i]) {
- return u[:i]
- }
- }
- return u[:maxLen]
-}
diff --git a/internal/handlers/network_export_stream.go b/internal/handlers/network_export_stream.go
new file mode 100644
index 00000000..ee2507b1
--- /dev/null
+++ b/internal/handlers/network_export_stream.go
@@ -0,0 +1,309 @@
+package handlers
+
+import (
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "os"
+ "path/filepath"
+ "strings"
+ "sync"
+ "time"
+ "unicode/utf8"
+
+ "github.com/pinchtab/pinchtab/internal/bridge/observe"
+ "github.com/pinchtab/pinchtab/internal/httpx"
+)
+
+// HandleNetworkExportStream streams network entries to a file as they arrive.
+//
+// @Endpoint GET /network/export/stream
+// @Description Live capture: write entries to file as they are captured
+//
+// @Param tabId string query Tab ID
+// @Param format string query Export format (default: har)
+// @Param path string query Output filename (required)
+// @Param body string query "true" to include response bodies
+// @Param redact string query "false" to include sensitive headers (default: redacted)
+// @Param filter... (same as HandleNetworkExport)
+//
+// @Response 200 text/event-stream SSE progress events
+// @Response 423 application/json Tab is locked
+func (h *Handlers) HandleNetworkExportStream(w http.ResponseWriter, r *http.Request) {
+ if err := h.ensureChrome(); err != nil {
+ if h.writeBridgeUnavailable(w, err) {
+ return
+ }
+ httpx.Error(w, 500, fmt.Errorf("chrome initialization: %w", err))
+ return
+ }
+
+ userPath := r.URL.Query().Get("path")
+ if userPath == "" {
+ httpx.Error(w, 400, fmt.Errorf("path required for streaming export"))
+ return
+ }
+
+ tabID := r.URL.Query().Get("tabId")
+ tabCtx, resolvedTabID, err := h.tabContext(r, tabID)
+ if err != nil {
+ httpx.Error(w, 404, err)
+ return
+ }
+ if _, ok := h.enforceCurrentTabDomainPolicy(w, r, tabCtx, resolvedTabID); !ok {
+ return
+ }
+
+ owner := resolveOwner(r, "")
+ if err := h.enforceTabLease(resolvedTabID, owner); err != nil {
+ httpx.ErrorCode(w, http.StatusLocked, "tab_locked", err.Error(), false, nil)
+ return
+ }
+
+ h.recordReadRequest(r, "network-export-stream", resolvedTabID)
+
+ formatName := r.URL.Query().Get("format")
+ if formatName == "" {
+ formatName = "har"
+ }
+ factory := observe.GetFormat(formatName)
+ if factory == nil {
+ httpx.JSON(w, 400, map[string]any{
+ "code": "unknown_format",
+ "error": fmt.Sprintf("unknown format %q", formatName),
+ "available": observe.ListFormats(),
+ })
+ return
+ }
+
+ nm := h.Bridge.NetworkMonitor()
+ if nm == nil {
+ httpx.Error(w, 500, fmt.Errorf("network monitor not available"))
+ return
+ }
+
+ bufferSize := parseBufferSize(r)
+ buf := nm.GetBuffer(resolvedTabID)
+ if buf == nil {
+ if err := nm.StartCaptureWithSize(tabCtx, resolvedTabID, bufferSize); err != nil {
+ httpx.Error(w, 500, fmt.Errorf("start capture: %w", err))
+ return
+ }
+ buf = nm.GetBuffer(resolvedTabID)
+ }
+
+ includeBody := r.URL.Query().Get("body") == "true"
+ redactHeaders := r.URL.Query().Get("redact") != "false"
+ filter := parseNetworkFilter(r)
+
+ // Path safety (#1)
+ exportDir := filepath.Join(h.Config.StateDir, "exports")
+ if err := os.MkdirAll(exportDir, 0750); err != nil {
+ httpx.Error(w, 500, fmt.Errorf("create dir: %w", err))
+ return
+ }
+
+ safeName := filepath.Base(userPath)
+ safePath, err := httpx.SafeCreatePath(exportDir, safeName)
+ if err != nil {
+ httpx.Error(w, 400, fmt.Errorf("invalid path: %w", err))
+ return
+ }
+ absBase, _ := filepath.Abs(exportDir)
+ absPath, err := filepath.Abs(safePath)
+ if err != nil || !strings.HasPrefix(absPath, absBase+string(filepath.Separator)) {
+ httpx.Error(w, 400, fmt.Errorf("path escapes export directory"))
+ return
+ }
+
+ // Write to temp file, rename on finish (#8)
+ tmpPath := absPath + ".tmp"
+ f, err := os.OpenFile(tmpPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
+ if err != nil {
+ httpx.Error(w, 500, fmt.Errorf("create file: %w", err))
+ return
+ }
+
+ enc := factory("PinchTab", h.version())
+ if err := enc.Start(f); err != nil {
+ _ = f.Close()
+ _ = os.Remove(tmpPath)
+ httpx.Error(w, 500, fmt.Errorf("start encoder: %w", err))
+ return
+ }
+
+ // Semaphore to throttle concurrent body fetches in streaming mode.
+ streamBodySem := make(chan struct{}, bodyFetchConcurrency)
+
+ subID, ch := buf.Subscribe()
+ defer buf.Unsubscribe(subID)
+
+ flusher, ok := w.(http.Flusher)
+ if !ok {
+ _ = enc.Finish()
+ _ = f.Close()
+ _ = os.Remove(tmpPath)
+ httpx.Error(w, 500, fmt.Errorf("streaming not supported"))
+ return
+ }
+
+ w.Header().Set("Content-Type", "text/event-stream")
+ w.Header().Set("Cache-Control", "no-cache")
+ w.Header().Set("Connection", "keep-alive")
+ w.Header().Set("X-Accel-Buffering", "no")
+
+ // Set a hard deadline so forgotten streams don't leak resources.
+ streamDeadline := time.NewTimer(maxExportStreamDuration)
+ defer streamDeadline.Stop()
+
+ // Clear per-write deadline for long-lived SSE (#7), the stream timer above caps total duration.
+ _ = http.NewResponseController(w).SetWriteDeadline(time.Time{})
+
+ count := 0
+ keepalive := time.NewTicker(15 * time.Second)
+ defer keepalive.Stop()
+
+ var finalizeOnce sync.Once
+ finalize := func() {
+ finalizeOnce.Do(func() {
+ _ = enc.Finish()
+ if err := f.Close(); err == nil {
+ // Atomic rename on success (#8)
+ if count > 0 {
+ _ = os.Rename(tmpPath, absPath)
+ } else {
+ _ = os.Remove(tmpPath)
+ }
+ } else {
+ _ = os.Remove(tmpPath)
+ }
+ })
+ }
+
+ // sendDone emits the final SSE event with the correct path (empty when no file was written).
+ sendDone := func() {
+ result := map[string]any{"entries": count}
+ if count > 0 {
+ result["path"] = absPath
+ }
+ data, _ := json.Marshal(result)
+ _, _ = fmt.Fprintf(w, "event: done\ndata: %s\n\n", data)
+ flusher.Flush()
+ }
+
+ for {
+ select {
+ case <-r.Context().Done():
+ finalize()
+ // Don't write to ResponseWriter after client disconnect (#6)
+ return
+
+ case <-streamDeadline.C:
+ finalize()
+ data, _ := json.Marshal(map[string]any{"entries": count, "reason": "max_duration_reached"})
+ _, _ = fmt.Fprintf(w, "event: timeout\ndata: %s\n\n", data)
+ sendDone()
+ return
+
+ case entry, ok := <-ch:
+ if !ok {
+ finalize()
+ sendDone()
+ return
+ }
+
+ // The subscriber fires on requestWillBeSent (entry created) before
+ // responseReceived/loadingFinished populate status, headers, timing.
+ // Wait for the entry to be Finished before exporting it.
+ if !entry.Finished && !entry.Failed {
+ reqID := entry.RequestID
+ waitDeadline := time.After(10 * time.Second)
+ poll := time.NewTicker(200 * time.Millisecond)
+ waitDone:
+ for {
+ select {
+ case <-r.Context().Done():
+ poll.Stop()
+ finalize()
+ return
+ case <-waitDeadline:
+ poll.Stop()
+ if updated, found := buf.Get(reqID); found {
+ entry = updated
+ }
+ break waitDone
+ case <-poll.C:
+ if updated, found := buf.Get(reqID); found && (updated.Finished || updated.Failed) {
+ entry = updated
+ poll.Stop()
+ break waitDone
+ }
+ }
+ }
+ }
+
+ if !filter.Match(entry) {
+ continue
+ }
+ var body string
+ var b64 bool
+ if includeBody && entry.Finished && !entry.Failed {
+ // Throttle body fetches to avoid saturating the CDP connection.
+ streamBodySem <- struct{}{}
+ body, b64, _ = nm.GetResponseBody(tabCtx, entry.RequestID)
+ <-streamBodySem
+ if len(body) > maxExportBodyBytes {
+ body = ""
+ b64 = false
+ }
+ }
+ export := observe.NetworkEntryToExport(entry, body, b64)
+ if redactHeaders {
+ export.Request.Headers = observe.RedactSensitiveHeaders(export.Request.Headers)
+ export.Response.Headers = observe.RedactSensitiveHeaders(export.Response.Headers)
+ }
+ if err := enc.Encode(export); err != nil {
+ finalize()
+ return
+ }
+ count++
+ data, _ := json.Marshal(map[string]any{"entries": count, "url": safetruncateURL(entry.URL)})
+ _, _ = fmt.Fprintf(w, "event: export\ndata: %s\n\n", data)
+ flusher.Flush()
+
+ case <-keepalive.C:
+ _, _ = fmt.Fprintf(w, ": keepalive\n\n")
+ flusher.Flush()
+ }
+ }
+}
+
+// HandleTabNetworkExportStream handles GET /tabs/{id}/network/export/stream.
+func (h *Handlers) HandleTabNetworkExportStream(w http.ResponseWriter, r *http.Request) {
+ tabID := r.PathValue("id")
+ if tabID == "" {
+ httpx.Error(w, 400, fmt.Errorf("tab id required"))
+ return
+ }
+ q := r.URL.Query()
+ q.Set("tabId", tabID)
+ req := r.Clone(r.Context())
+ u := *r.URL
+ u.RawQuery = q.Encode()
+ req.URL = &u
+ h.HandleNetworkExportStream(w, req)
+}
+
+// safetruncateURL truncates at a valid UTF-8 boundary (#21).
+func safetruncateURL(u string) string {
+ const maxLen = 120
+ if len(u) <= maxLen {
+ return u
+ }
+ for i := maxLen; i > 0; i-- {
+ if utf8.RuneStart(u[i]) {
+ return u[:i]
+ }
+ }
+ return u[:maxLen]
+}
diff --git a/internal/handlers/tab_lifecycle.go b/internal/handlers/tab_lifecycle.go
new file mode 100644
index 00000000..9e8156ab
--- /dev/null
+++ b/internal/handlers/tab_lifecycle.go
@@ -0,0 +1,187 @@
+package handlers
+
+import (
+ "context"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "io"
+ "net/http"
+ "strings"
+
+ "github.com/chromedp/chromedp"
+ "github.com/pinchtab/pinchtab/internal/activity"
+ "github.com/pinchtab/pinchtab/internal/bridge"
+ "github.com/pinchtab/pinchtab/internal/httpx"
+)
+
+const tabActionNew = "new"
+
+func (h *Handlers) HandleTab(w http.ResponseWriter, r *http.Request) {
+ var req struct {
+ Action string `json:"action"`
+ TabID string `json:"tabId"`
+ URL string `json:"url"`
+ }
+ if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, maxBodySize)).Decode(&req); err != nil {
+ httpx.Error(w, 400, fmt.Errorf("decode: %w", err))
+ return
+ }
+
+ switch req.Action {
+ case tabActionNew:
+ var target *validatedNavigateTarget
+ trustedResolveCIDRs := parseCIDRs(h.Config.TrustedResolveCIDRs)
+ trustedCIDRs := parseCIDRs(h.Config.TrustedProxyCIDRs)
+ if req.URL != "" && req.URL != "about:blank" {
+ if err := validateNavigateURL(req.URL); err != nil {
+ httpx.Error(w, 400, err)
+ return
+ }
+ domainResult := h.IDPIGuard.CheckDomain(req.URL)
+ if domainResult.Blocked {
+ httpx.Error(w, http.StatusForbidden, fmt.Errorf("navigation blocked by IDPI: %s", domainResult.Reason))
+ return
+ }
+ if domainResult.Threat {
+ w.Header().Set("X-IDPI-Warning", domainResult.Reason)
+ }
+ var err error
+ target, err = validateNavigateTarget(req.URL, h.IDPIGuard.DomainAllowed(req.URL), trustedResolveCIDRs)
+ if err != nil {
+ httpx.Error(w, http.StatusForbidden, err)
+ return
+ }
+ }
+
+ if !h.ensureChromeOrRespond(w) {
+ return
+ }
+
+ // Create a blank tab first so the requested URL becomes the first
+ // real history entry.
+ newTabID, ctx, _, err := h.Bridge.CreateTab("")
+ if err != nil {
+ httpx.Error(w, 500, err)
+ return
+ }
+
+ if req.URL != "" && req.URL != "about:blank" {
+ tCtx, tCancel := context.WithTimeout(ctx, h.Config.NavigateTimeout)
+ defer tCancel()
+ go httpx.CancelOnClientDone(r.Context(), tCancel)
+ navGuard, err := installNavigateRuntimeGuard(tCtx, tCancel, target, trustedCIDRs)
+ if err != nil {
+ _ = h.Bridge.CloseTab(newTabID)
+ httpx.Error(w, 500, fmt.Errorf("navigation guard: %w", err))
+ return
+ }
+ if err := bridge.NavigatePageWithRedirectLimit(tCtx, req.URL, h.Config.MaxRedirects); err != nil {
+ if navGuard != nil {
+ if blockedErr := navGuard.blocked(); blockedErr != nil {
+ _ = h.Bridge.CloseTab(newTabID)
+ httpx.Error(w, http.StatusForbidden, blockedErr)
+ return
+ }
+ }
+ _ = h.Bridge.CloseTab(newTabID)
+ code := 500
+ if errors.Is(err, bridge.ErrTooManyRedirects) {
+ code = 422
+ }
+ navigateErrorWithHint(w, code, err, req.URL)
+ return
+ }
+ }
+
+ var curURL, title string
+ _ = chromedp.Run(ctx, chromedp.Location(&curURL), chromedp.Title(&title))
+
+ httpx.JSON(w, 200, map[string]any{"tabId": newTabID, "url": curURL, "title": title})
+
+ case "focus":
+ if req.TabID == "" {
+ httpx.Error(w, 400, fmt.Errorf("tabId required"))
+ return
+ }
+ if !h.ensureChromeOrRespond(w) {
+ return
+ }
+ if err := h.Bridge.FocusTab(req.TabID); err != nil {
+ httpx.Error(w, 404, err)
+ return
+ }
+
+ h.recordActivity(r, activity.Update{Action: "tab.focus", TabID: req.TabID})
+
+ httpx.JSON(w, 200, map[string]any{"focused": true, "tabId": req.TabID})
+
+ default:
+ httpx.Error(w, 400, fmt.Errorf("action must be 'new' or 'focus'; use /close to close a tab"))
+ }
+}
+
+// HandleTabClose closes the tab identified by the path. It is the tab-scoped
+// equivalent of POST /close and exists so orchestrator dashboard commands can
+// use the common /tabs/{id}/... proxy path.
+func (h *Handlers) HandleTabClose(w http.ResponseWriter, r *http.Request) {
+ tabID := strings.TrimSpace(r.PathValue("id"))
+ if tabID == "" {
+ httpx.Error(w, 400, fmt.Errorf("tab id required"))
+ return
+ }
+
+ if r.Body != nil && r.Body != http.NoBody && r.ContentLength != 0 {
+ var req struct {
+ TabID string `json:"tabId"`
+ }
+ if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, maxBodySize)).Decode(&req); err != nil && !errors.Is(err, io.EOF) {
+ httpx.Error(w, 400, fmt.Errorf("decode: %w", err))
+ return
+ }
+ if req.TabID != "" && req.TabID != tabID {
+ httpx.Error(w, 400, fmt.Errorf("tabId in body does not match path id"))
+ return
+ }
+ }
+
+ h.closeTab(w, r, tabID)
+}
+
+// HandleClose closes the tab identified by the JSON body, or the current/default
+// tab when tabId is omitted. It is the shorthand form of POST /tabs/{id}/close.
+func (h *Handlers) HandleClose(w http.ResponseWriter, r *http.Request) {
+ var req struct {
+ TabID string `json:"tabId"`
+ }
+ if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, maxBodySize)).Decode(&req); err != nil && !errors.Is(err, io.EOF) {
+ httpx.Error(w, 400, fmt.Errorf("decode: %w", err))
+ return
+ }
+
+ h.closeTab(w, r, strings.TrimSpace(req.TabID))
+}
+
+func (h *Handlers) closeTab(w http.ResponseWriter, r *http.Request, tabID string) {
+ if !h.ensureChromeOrRespond(w) {
+ return
+ }
+ if tabID == "" {
+ _, resolvedTabID, err := h.tabContext(r, "")
+ if err != nil {
+ httpx.Error(w, 404, err)
+ return
+ }
+ tabID = resolvedTabID
+ }
+
+ if err := h.Bridge.CloseTab(tabID); err != nil {
+ httpx.Error(w, 500, err)
+ return
+ }
+
+ h.recordActivity(r, activity.Update{Action: "tab.close", TabID: tabID})
+ w.Header().Set(activity.HeaderPTTabID, tabID)
+
+ httpx.JSON(w, 200, map[string]any{"closed": true, "tabId": tabID})
+}
diff --git a/internal/profiles/profiles.go b/internal/profiles/profiles.go
index 112d1bf1..066eb351 100644
--- a/internal/profiles/profiles.go
+++ b/internal/profiles/profiles.go
@@ -3,94 +3,17 @@ package profiles
import (
"fmt"
"io/fs"
- "log/slog"
- "net/url"
"os"
"path/filepath"
"sort"
"strings"
"sync"
"time"
- "unicode"
"github.com/pinchtab/pinchtab/internal/activity"
"github.com/pinchtab/pinchtab/internal/bridge"
- "github.com/pinchtab/pinchtab/internal/ids"
)
-var idMgr = ids.NewManager()
-
-var reservedWindowsProfileNames = map[string]struct{}{
- "CON": {},
- "PRN": {},
- "AUX": {},
- "NUL": {},
- "COM1": {},
- "COM2": {},
- "COM3": {},
- "COM4": {},
- "COM5": {},
- "COM6": {},
- "COM7": {},
- "COM8": {},
- "COM9": {},
- "LPT1": {},
- "LPT2": {},
- "LPT3": {},
- "LPT4": {},
- "LPT5": {},
- "LPT6": {},
- "LPT7": {},
- "LPT8": {},
- "LPT9": {},
-}
-
-func profileID(name string) string {
- return idMgr.ProfileID(name)
-}
-
-// ValidateProfileName enforces a cross-platform-safe profile name policy for
-// filesystem usage and shell-adjacent process cleanup on Windows.
-func ValidateProfileName(name string) error {
- if name == "" {
- return fmt.Errorf("profile name cannot be empty")
- }
- trimmed := strings.TrimSpace(name)
- if trimmed == "" {
- return fmt.Errorf("profile name cannot be blank")
- }
- if trimmed != name {
- return fmt.Errorf("profile name cannot start or end with whitespace")
- }
- if strings.Contains(name, "..") {
- return fmt.Errorf("profile name cannot contain '..'")
- }
- if strings.ContainsAny(name, "/\\") {
- return fmt.Errorf("profile name cannot contain '/' or '\\'")
- }
- if strings.HasSuffix(name, ".") {
- return fmt.Errorf("profile name cannot end with '.'")
- }
- for _, r := range name {
- if unicode.IsLetter(r) || unicode.IsDigit(r) || r == ' ' || r == '-' || r == '_' || r == '.' {
- continue
- }
- return fmt.Errorf("profile name contains invalid character %q", r)
- }
- base := name
- if dot := strings.IndexRune(base, '.'); dot >= 0 {
- base = base[:dot]
- }
- if _, reserved := reservedWindowsProfileNames[strings.ToUpper(base)]; reserved {
- return fmt.Errorf("profile name cannot use reserved device name %q", base)
- }
- return nil
-}
-
-func isProfileNameValidationError(err error) bool {
- return err != nil && strings.HasPrefix(err.Error(), "profile name ")
-}
-
type ProfileManager struct {
baseDir string
activity activity.Recorder
@@ -280,386 +203,6 @@ func (pm *ProfileManager) profileInfo(dirName string) (ProfileDetailedInfo, erro
}, nil
}
-func (pm *ProfileManager) Import(name, sourcePath string) error {
- if err := ValidateProfileName(name); err != nil {
- return err
- }
- pm.mu.Lock()
- defer pm.mu.Unlock()
-
- if _, err := pm.findProfileDirByName(name); err == nil {
- return fmt.Errorf("profile %q already exists", name)
- }
- dest := filepath.Join(pm.baseDir, profileID(name))
- if _, err := os.Stat(dest); err == nil {
- return fmt.Errorf("profile %q already exists", name)
- }
-
- resolvedSourcePath, err := resolveImportSourcePath(sourcePath)
- if err != nil {
- return err
- }
-
- if _, err := os.Stat(filepath.Join(resolvedSourcePath, "Default")); err != nil {
- if _, err2 := os.Stat(filepath.Join(resolvedSourcePath, "Preferences")); err2 != nil {
- return fmt.Errorf("source doesn't look like a Chrome user data dir (no Default/ or Preferences found)")
- }
- }
-
- srcInfo, err := os.Lstat(resolvedSourcePath)
- if err != nil {
- return fmt.Errorf("source path invalid: %w", err)
- }
- if srcInfo.Mode()&os.ModeSymlink != 0 {
- return fmt.Errorf("source path must not be a symlink")
- }
- if !srcInfo.IsDir() {
- return fmt.Errorf("source path must be a directory")
- }
-
- slog.Info("importing profile", "name", name, "source", resolvedSourcePath)
- if err := copyDir(resolvedSourcePath, dest); err != nil {
- return fmt.Errorf("copy failed: %w", err)
- }
-
- if err := os.WriteFile(filepath.Join(dest, ".pinchtab-imported"), []byte(resolvedSourcePath), 0600); err != nil {
- slog.Warn("failed to write import marker", "err", err)
- }
- return writeProfileMeta(dest, ProfileMeta{
- ID: profileID(name),
- Name: name,
- })
-}
-
-func (pm *ProfileManager) ImportWithMeta(name, sourcePath string, meta ProfileMeta) error {
- if err := pm.Import(name, sourcePath); err != nil {
- return err
- }
- if meta.ID == "" {
- meta.ID = profileID(name)
- }
- if meta.Name == "" {
- meta.Name = name
- }
- dest := filepath.Join(pm.baseDir, profileID(name))
- return writeProfileMeta(dest, meta)
-}
-
-func (pm *ProfileManager) Create(name string) error {
- if err := ValidateProfileName(name); err != nil {
- return err
- }
- pm.mu.Lock()
- defer pm.mu.Unlock()
-
- if _, err := pm.findProfileDirByName(name); err == nil {
- return fmt.Errorf("profile %q already exists", name)
- }
- dest := filepath.Join(pm.baseDir, profileID(name))
- if _, err := os.Stat(dest); err == nil {
- return fmt.Errorf("profile %q already exists", name)
- }
- if err := os.MkdirAll(filepath.Join(dest, "Default"), 0755); err != nil {
- return err
- }
- return writeProfileMeta(dest, ProfileMeta{
- ID: profileID(name),
- Name: name,
- })
-}
-
-func resolveImportSourcePath(sourcePath string) (string, error) {
- if sourcePath == "" {
- return "", fmt.Errorf("source path required")
- }
-
- cleaned := filepath.Clean(sourcePath)
- if !filepath.IsAbs(cleaned) {
- abs, err := filepath.Abs(cleaned)
- if err != nil {
- return "", fmt.Errorf("source path invalid: %w", err)
- }
- cleaned = abs
- }
-
- roots, err := allowedImportRoots()
- if err != nil {
- return "", err
- }
- for _, root := range roots {
- if pathWithinRoot(cleaned, root) {
- return cleaned, nil
- }
- }
- return "", fmt.Errorf("source path must be within %s", strings.Join(roots, " or "))
-}
-
-func allowedImportRoots() ([]string, error) {
- roots := []string{filepath.Clean(os.TempDir())}
-
- homeDir, err := os.UserHomeDir()
- if err != nil {
- return nil, fmt.Errorf("resolve home dir: %w", err)
- }
- roots = append(roots, filepath.Clean(homeDir))
- return roots, nil
-}
-
-func pathWithinRoot(path, root string) bool {
- rel, err := filepath.Rel(root, path)
- if err != nil {
- return false
- }
- return rel == "." || (!strings.HasPrefix(rel, ".."+string(os.PathSeparator)) && rel != "..")
-}
-
-func (pm *ProfileManager) CreateWithMeta(name string, meta ProfileMeta) error {
- if err := pm.Create(name); err != nil {
- return err
- }
- if meta.ID == "" {
- meta.ID = profileID(name)
- }
- if meta.Name == "" {
- meta.Name = name
- }
- dest := filepath.Join(pm.baseDir, profileID(name))
- return writeProfileMeta(dest, meta)
-}
-
-func (pm *ProfileManager) Reset(name string) error {
- if err := ValidateProfileName(name); err != nil {
- return err
- }
- pm.mu.Lock()
- defer pm.mu.Unlock()
-
- dir, err := pm.findProfileDirByName(name)
- if err != nil {
- return err
- }
-
- resetProfileDir(dir)
- slog.Info("profile reset", "name", name)
- return nil
-}
-
-func resetProfileDir(dir string) {
- nukeDirs := []string{
- "Default/Sessions",
- "Default/Session Storage",
- "Default/Cache",
- "Default/Code Cache",
- "Default/GPUCache",
- "Default/Service Worker",
- "Default/blob_storage",
- "ShaderCache",
- "GrShaderCache",
- }
-
- nukeFiles := []string{
- "Default/Cookies",
- "Default/Cookies-journal",
- "Default/History",
- "Default/History-journal",
- "Default/Visited Links",
- }
-
- for _, d := range nukeDirs {
- path := filepath.Join(dir, d)
- if err := os.RemoveAll(path); err != nil {
- slog.Warn("reset: failed to remove dir", "path", path, "err", err)
- }
- }
- for _, f := range nukeFiles {
- _ = os.Remove(filepath.Join(dir, f))
- }
-}
-
-func (pm *ProfileManager) Delete(name string) error {
- if err := ValidateProfileName(name); err != nil {
- return err
- }
- pm.mu.Lock()
- defer pm.mu.Unlock()
-
- dir, err := pm.findProfileDirByName(name)
- if err != nil {
- return err
- }
- return os.RemoveAll(dir)
-}
-
-func (pm *ProfileManager) RecordAction(profile string, record bridge.ActionRecord) {
- _ = profile
- _ = record
-}
-
-func (pm *ProfileManager) Logs(name string, limit int) []bridge.ActionRecord {
- return pm.logsFromActivity(name, limit)
-}
-
-func (pm *ProfileManager) Analytics(name string) bridge.AnalyticsReport {
- return analyticsFromActionRecords(pm.logsFromActivity(name, 1000))
-}
-
-func dirSizeMB(path string) float64 {
- var total int64
- _ = filepath.WalkDir(path, func(_ string, entry fs.DirEntry, err error) error {
- if err != nil || entry.IsDir() {
- return nil
- }
- info, err := entry.Info()
- if err == nil {
- total += info.Size()
- }
- return nil
- })
- return float64(total) / (1024 * 1024)
-}
-
-func (pm *ProfileManager) logsFromActivity(name string, limit int) []bridge.ActionRecord {
- pm.mu.RLock()
- rec := pm.activity
- pm.mu.RUnlock()
- if rec == nil || !rec.Enabled() {
- return []bridge.ActionRecord{}
- }
-
- events, err := rec.Query(activity.Filter{
- ProfileName: name,
- Limit: limit,
- })
- if err != nil {
- return []bridge.ActionRecord{}
- }
-
- logs := make([]bridge.ActionRecord, 0, len(events))
- for _, evt := range events {
- logs = append(logs, bridge.ActionRecord{
- Timestamp: evt.Timestamp,
- Method: evt.Method,
- Endpoint: evt.Path,
- URL: evt.URL,
- TabID: evt.TabID,
- DurationMs: evt.DurationMs,
- Status: evt.Status,
- })
- }
- sort.Slice(logs, func(i, j int) bool {
- return logs[i].Timestamp.Before(logs[j].Timestamp)
- })
- if limit > 0 && len(logs) > limit {
- return logs[len(logs)-limit:]
- }
- return logs
-}
-
-func analyticsFromActionRecords(logs []bridge.ActionRecord) bridge.AnalyticsReport {
- report := bridge.AnalyticsReport{
- TotalActions: len(logs),
- CommonHosts: make(map[string]int),
- TopEndpoints: make(map[string]int),
- }
-
- last24h := time.Now().Add(-24 * time.Hour)
- for _, l := range logs {
- if l.Timestamp.After(last24h) {
- report.Last24h++
- }
- if l.URL != "" {
- u, err := url.Parse(l.URL)
- if err == nil && u.Host != "" {
- report.CommonHosts[u.Host]++
- }
- }
- if l.Endpoint != "" {
- report.TopEndpoints[l.Endpoint]++
- }
- }
- if len(report.TopEndpoints) == 0 {
- report.TopEndpoints = nil
- }
- if len(report.CommonHosts) == 0 {
- report.CommonHosts = map[string]int{}
- }
- return report
-}
-
-func (pm *ProfileManager) UpdateMeta(name string, meta map[string]string) error {
- pm.mu.Lock()
- defer pm.mu.Unlock()
-
- if err := ValidateProfileName(name); err != nil {
- return err
- }
-
- dir, err := pm.findProfileDirByName(name)
- if err != nil {
- return err
- }
-
- existing := readProfileMeta(dir)
- if existing.Name == "" {
- existing.Name = name
- }
-
- if useWhen, ok := meta["useWhen"]; ok {
- existing.UseWhen = useWhen
- }
- if description, ok := meta["description"]; ok {
- existing.Description = description
- }
-
- return writeProfileMeta(dir, existing)
-}
-
-func (pm *ProfileManager) Rename(oldName, newName string) error {
- if err := ValidateProfileName(oldName); err != nil {
- return err
- }
- if err := ValidateProfileName(newName); err != nil {
- return err
- }
- if oldName == newName {
- return nil
- }
-
- pm.mu.Lock()
- defer pm.mu.Unlock()
-
- oldDir, err := pm.findProfileDirByName(oldName)
- if err != nil {
- return err
- }
-
- if _, err := pm.findProfileDirByName(newName); err == nil {
- return fmt.Errorf("profile %q already exists", newName)
- }
-
- newDir := filepath.Join(pm.baseDir, profileID(newName))
- if _, err := os.Stat(newDir); err == nil {
- return fmt.Errorf("profile directory for %q already exists", newName)
- }
-
- meta := readProfileMeta(oldDir)
- meta.ID = profileID(newName)
- meta.Name = newName
- if err := writeProfileMeta(oldDir, meta); err != nil {
- return fmt.Errorf("failed to update profile metadata: %w", err)
- }
-
- if err := os.Rename(oldDir, newDir); err != nil {
- meta.ID = profileID(oldName)
- meta.Name = oldName
- _ = writeProfileMeta(oldDir, meta)
- return fmt.Errorf("failed to rename profile directory: %w", err)
- }
-
- slog.Info("profile renamed", "from", oldName, "to", newName)
- return nil
-}
-
func (pm *ProfileManager) FindByID(id string) (string, error) {
pm.mu.RLock()
defer pm.mu.RUnlock()
@@ -689,3 +232,18 @@ func (pm *ProfileManager) FindByID(id string) (string, error) {
}
return "", fmt.Errorf("profile with id %q not found", id)
}
+
+func dirSizeMB(path string) float64 {
+ var total int64
+ _ = filepath.WalkDir(path, func(_ string, entry fs.DirEntry, err error) error {
+ if err != nil || entry.IsDir() {
+ return nil
+ }
+ info, err := entry.Info()
+ if err == nil {
+ total += info.Size()
+ }
+ return nil
+ })
+ return float64(total) / (1024 * 1024)
+}
diff --git a/internal/profiles/profiles_activity.go b/internal/profiles/profiles_activity.go
new file mode 100644
index 00000000..8a9bf11b
--- /dev/null
+++ b/internal/profiles/profiles_activity.go
@@ -0,0 +1,91 @@
+package profiles
+
+import (
+ "net/url"
+ "sort"
+ "time"
+
+ "github.com/pinchtab/pinchtab/internal/activity"
+ "github.com/pinchtab/pinchtab/internal/bridge"
+)
+
+func (pm *ProfileManager) RecordAction(profile string, record bridge.ActionRecord) {
+ _ = profile
+ _ = record
+}
+
+func (pm *ProfileManager) Logs(name string, limit int) []bridge.ActionRecord {
+ return pm.logsFromActivity(name, limit)
+}
+
+func (pm *ProfileManager) Analytics(name string) bridge.AnalyticsReport {
+ return analyticsFromActionRecords(pm.logsFromActivity(name, 1000))
+}
+
+func (pm *ProfileManager) logsFromActivity(name string, limit int) []bridge.ActionRecord {
+ pm.mu.RLock()
+ rec := pm.activity
+ pm.mu.RUnlock()
+ if rec == nil || !rec.Enabled() {
+ return []bridge.ActionRecord{}
+ }
+
+ events, err := rec.Query(activity.Filter{
+ ProfileName: name,
+ Limit: limit,
+ })
+ if err != nil {
+ return []bridge.ActionRecord{}
+ }
+
+ logs := make([]bridge.ActionRecord, 0, len(events))
+ for _, evt := range events {
+ logs = append(logs, bridge.ActionRecord{
+ Timestamp: evt.Timestamp,
+ Method: evt.Method,
+ Endpoint: evt.Path,
+ URL: evt.URL,
+ TabID: evt.TabID,
+ DurationMs: evt.DurationMs,
+ Status: evt.Status,
+ })
+ }
+ sort.Slice(logs, func(i, j int) bool {
+ return logs[i].Timestamp.Before(logs[j].Timestamp)
+ })
+ if limit > 0 && len(logs) > limit {
+ return logs[len(logs)-limit:]
+ }
+ return logs
+}
+
+func analyticsFromActionRecords(logs []bridge.ActionRecord) bridge.AnalyticsReport {
+ report := bridge.AnalyticsReport{
+ TotalActions: len(logs),
+ CommonHosts: make(map[string]int),
+ TopEndpoints: make(map[string]int),
+ }
+
+ last24h := time.Now().Add(-24 * time.Hour)
+ for _, l := range logs {
+ if l.Timestamp.After(last24h) {
+ report.Last24h++
+ }
+ if l.URL != "" {
+ u, err := url.Parse(l.URL)
+ if err == nil && u.Host != "" {
+ report.CommonHosts[u.Host]++
+ }
+ }
+ if l.Endpoint != "" {
+ report.TopEndpoints[l.Endpoint]++
+ }
+ }
+ if len(report.TopEndpoints) == 0 {
+ report.TopEndpoints = nil
+ }
+ if len(report.CommonHosts) == 0 {
+ report.CommonHosts = map[string]int{}
+ }
+ return report
+}
diff --git a/internal/profiles/profiles_crud.go b/internal/profiles/profiles_crud.go
new file mode 100644
index 00000000..6ae9091d
--- /dev/null
+++ b/internal/profiles/profiles_crud.go
@@ -0,0 +1,247 @@
+package profiles
+
+import (
+ "fmt"
+ "log/slog"
+ "os"
+ "path/filepath"
+)
+
+func (pm *ProfileManager) Import(name, sourcePath string) error {
+ if err := ValidateProfileName(name); err != nil {
+ return err
+ }
+ pm.mu.Lock()
+ defer pm.mu.Unlock()
+
+ if _, err := pm.findProfileDirByName(name); err == nil {
+ return fmt.Errorf("profile %q already exists", name)
+ }
+ dest := filepath.Join(pm.baseDir, profileID(name))
+ if _, err := os.Stat(dest); err == nil {
+ return fmt.Errorf("profile %q already exists", name)
+ }
+
+ resolvedSourcePath, err := resolveImportSourcePath(sourcePath)
+ if err != nil {
+ return err
+ }
+
+ if _, err := os.Stat(filepath.Join(resolvedSourcePath, "Default")); err != nil {
+ if _, err2 := os.Stat(filepath.Join(resolvedSourcePath, "Preferences")); err2 != nil {
+ return fmt.Errorf("source doesn't look like a Chrome user data dir (no Default/ or Preferences found)")
+ }
+ }
+
+ srcInfo, err := os.Lstat(resolvedSourcePath)
+ if err != nil {
+ return fmt.Errorf("source path invalid: %w", err)
+ }
+ if srcInfo.Mode()&os.ModeSymlink != 0 {
+ return fmt.Errorf("source path must not be a symlink")
+ }
+ if !srcInfo.IsDir() {
+ return fmt.Errorf("source path must be a directory")
+ }
+
+ slog.Info("importing profile", "name", name, "source", resolvedSourcePath)
+ if err := copyDir(resolvedSourcePath, dest); err != nil {
+ return fmt.Errorf("copy failed: %w", err)
+ }
+
+ if err := os.WriteFile(filepath.Join(dest, ".pinchtab-imported"), []byte(resolvedSourcePath), 0600); err != nil {
+ slog.Warn("failed to write import marker", "err", err)
+ }
+ return writeProfileMeta(dest, ProfileMeta{
+ ID: profileID(name),
+ Name: name,
+ })
+}
+
+func (pm *ProfileManager) ImportWithMeta(name, sourcePath string, meta ProfileMeta) error {
+ if err := pm.Import(name, sourcePath); err != nil {
+ return err
+ }
+ if meta.ID == "" {
+ meta.ID = profileID(name)
+ }
+ if meta.Name == "" {
+ meta.Name = name
+ }
+ dest := filepath.Join(pm.baseDir, profileID(name))
+ return writeProfileMeta(dest, meta)
+}
+
+func (pm *ProfileManager) Create(name string) error {
+ if err := ValidateProfileName(name); err != nil {
+ return err
+ }
+ pm.mu.Lock()
+ defer pm.mu.Unlock()
+
+ if _, err := pm.findProfileDirByName(name); err == nil {
+ return fmt.Errorf("profile %q already exists", name)
+ }
+ dest := filepath.Join(pm.baseDir, profileID(name))
+ if _, err := os.Stat(dest); err == nil {
+ return fmt.Errorf("profile %q already exists", name)
+ }
+ if err := os.MkdirAll(filepath.Join(dest, "Default"), 0755); err != nil {
+ return err
+ }
+ return writeProfileMeta(dest, ProfileMeta{
+ ID: profileID(name),
+ Name: name,
+ })
+}
+
+func (pm *ProfileManager) CreateWithMeta(name string, meta ProfileMeta) error {
+ if err := pm.Create(name); err != nil {
+ return err
+ }
+ if meta.ID == "" {
+ meta.ID = profileID(name)
+ }
+ if meta.Name == "" {
+ meta.Name = name
+ }
+ dest := filepath.Join(pm.baseDir, profileID(name))
+ return writeProfileMeta(dest, meta)
+}
+
+func (pm *ProfileManager) Reset(name string) error {
+ if err := ValidateProfileName(name); err != nil {
+ return err
+ }
+ pm.mu.Lock()
+ defer pm.mu.Unlock()
+
+ dir, err := pm.findProfileDirByName(name)
+ if err != nil {
+ return err
+ }
+
+ resetProfileDir(dir)
+ slog.Info("profile reset", "name", name)
+ return nil
+}
+
+func resetProfileDir(dir string) {
+ nukeDirs := []string{
+ "Default/Sessions",
+ "Default/Session Storage",
+ "Default/Cache",
+ "Default/Code Cache",
+ "Default/GPUCache",
+ "Default/Service Worker",
+ "Default/blob_storage",
+ "ShaderCache",
+ "GrShaderCache",
+ }
+
+ nukeFiles := []string{
+ "Default/Cookies",
+ "Default/Cookies-journal",
+ "Default/History",
+ "Default/History-journal",
+ "Default/Visited Links",
+ }
+
+ for _, d := range nukeDirs {
+ path := filepath.Join(dir, d)
+ if err := os.RemoveAll(path); err != nil {
+ slog.Warn("reset: failed to remove dir", "path", path, "err", err)
+ }
+ }
+ for _, f := range nukeFiles {
+ _ = os.Remove(filepath.Join(dir, f))
+ }
+}
+
+func (pm *ProfileManager) Delete(name string) error {
+ if err := ValidateProfileName(name); err != nil {
+ return err
+ }
+ pm.mu.Lock()
+ defer pm.mu.Unlock()
+
+ dir, err := pm.findProfileDirByName(name)
+ if err != nil {
+ return err
+ }
+ return os.RemoveAll(dir)
+}
+
+func (pm *ProfileManager) UpdateMeta(name string, meta map[string]string) error {
+ pm.mu.Lock()
+ defer pm.mu.Unlock()
+
+ if err := ValidateProfileName(name); err != nil {
+ return err
+ }
+
+ dir, err := pm.findProfileDirByName(name)
+ if err != nil {
+ return err
+ }
+
+ existing := readProfileMeta(dir)
+ if existing.Name == "" {
+ existing.Name = name
+ }
+
+ if useWhen, ok := meta["useWhen"]; ok {
+ existing.UseWhen = useWhen
+ }
+ if description, ok := meta["description"]; ok {
+ existing.Description = description
+ }
+
+ return writeProfileMeta(dir, existing)
+}
+
+func (pm *ProfileManager) Rename(oldName, newName string) error {
+ if err := ValidateProfileName(oldName); err != nil {
+ return err
+ }
+ if err := ValidateProfileName(newName); err != nil {
+ return err
+ }
+ if oldName == newName {
+ return nil
+ }
+
+ pm.mu.Lock()
+ defer pm.mu.Unlock()
+
+ oldDir, err := pm.findProfileDirByName(oldName)
+ if err != nil {
+ return err
+ }
+
+ if _, err := pm.findProfileDirByName(newName); err == nil {
+ return fmt.Errorf("profile %q already exists", newName)
+ }
+
+ newDir := filepath.Join(pm.baseDir, profileID(newName))
+ if _, err := os.Stat(newDir); err == nil {
+ return fmt.Errorf("profile directory for %q already exists", newName)
+ }
+
+ meta := readProfileMeta(oldDir)
+ meta.ID = profileID(newName)
+ meta.Name = newName
+ if err := writeProfileMeta(oldDir, meta); err != nil {
+ return fmt.Errorf("failed to update profile metadata: %w", err)
+ }
+
+ if err := os.Rename(oldDir, newDir); err != nil {
+ meta.ID = profileID(oldName)
+ meta.Name = oldName
+ _ = writeProfileMeta(oldDir, meta)
+ return fmt.Errorf("failed to rename profile directory: %w", err)
+ }
+
+ slog.Info("profile renamed", "from", oldName, "to", newName)
+ return nil
+}
diff --git a/internal/profiles/profiles_import.go b/internal/profiles/profiles_import.go
new file mode 100644
index 00000000..f4810b6c
--- /dev/null
+++ b/internal/profiles/profiles_import.go
@@ -0,0 +1,53 @@
+package profiles
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+ "strings"
+)
+
+func resolveImportSourcePath(sourcePath string) (string, error) {
+ if sourcePath == "" {
+ return "", fmt.Errorf("source path required")
+ }
+
+ cleaned := filepath.Clean(sourcePath)
+ if !filepath.IsAbs(cleaned) {
+ abs, err := filepath.Abs(cleaned)
+ if err != nil {
+ return "", fmt.Errorf("source path invalid: %w", err)
+ }
+ cleaned = abs
+ }
+
+ roots, err := allowedImportRoots()
+ if err != nil {
+ return "", err
+ }
+ for _, root := range roots {
+ if pathWithinRoot(cleaned, root) {
+ return cleaned, nil
+ }
+ }
+ return "", fmt.Errorf("source path must be within %s", strings.Join(roots, " or "))
+}
+
+func allowedImportRoots() ([]string, error) {
+ roots := []string{filepath.Clean(os.TempDir())}
+
+ homeDir, err := os.UserHomeDir()
+ if err != nil {
+ return nil, fmt.Errorf("resolve home dir: %w", err)
+ }
+ roots = append(roots, filepath.Clean(homeDir))
+ return roots, nil
+}
+
+func pathWithinRoot(path, root string) bool {
+ rel, err := filepath.Rel(root, path)
+ if err != nil {
+ return false
+ }
+ return rel == "." || (!strings.HasPrefix(rel, ".."+string(os.PathSeparator)) && rel != "..")
+}
diff --git a/internal/profiles/profiles_validation.go b/internal/profiles/profiles_validation.go
new file mode 100644
index 00000000..f11f930d
--- /dev/null
+++ b/internal/profiles/profiles_validation.go
@@ -0,0 +1,82 @@
+package profiles
+
+import (
+ "fmt"
+ "strings"
+ "unicode"
+
+ "github.com/pinchtab/pinchtab/internal/ids"
+)
+
+var idMgr = ids.NewManager()
+
+var reservedWindowsProfileNames = map[string]struct{}{
+ "CON": {},
+ "PRN": {},
+ "AUX": {},
+ "NUL": {},
+ "COM1": {},
+ "COM2": {},
+ "COM3": {},
+ "COM4": {},
+ "COM5": {},
+ "COM6": {},
+ "COM7": {},
+ "COM8": {},
+ "COM9": {},
+ "LPT1": {},
+ "LPT2": {},
+ "LPT3": {},
+ "LPT4": {},
+ "LPT5": {},
+ "LPT6": {},
+ "LPT7": {},
+ "LPT8": {},
+ "LPT9": {},
+}
+
+func profileID(name string) string {
+ return idMgr.ProfileID(name)
+}
+
+// ValidateProfileName enforces a cross-platform-safe profile name policy for
+// filesystem usage and shell-adjacent process cleanup on Windows.
+func ValidateProfileName(name string) error {
+ if name == "" {
+ return fmt.Errorf("profile name cannot be empty")
+ }
+ trimmed := strings.TrimSpace(name)
+ if trimmed == "" {
+ return fmt.Errorf("profile name cannot be blank")
+ }
+ if trimmed != name {
+ return fmt.Errorf("profile name cannot start or end with whitespace")
+ }
+ if strings.Contains(name, "..") {
+ return fmt.Errorf("profile name cannot contain '..'")
+ }
+ if strings.ContainsAny(name, "/\\") {
+ return fmt.Errorf("profile name cannot contain '/' or '\\'")
+ }
+ if strings.HasSuffix(name, ".") {
+ return fmt.Errorf("profile name cannot end with '.'")
+ }
+ for _, r := range name {
+ if unicode.IsLetter(r) || unicode.IsDigit(r) || r == ' ' || r == '-' || r == '_' || r == '.' {
+ continue
+ }
+ return fmt.Errorf("profile name contains invalid character %q", r)
+ }
+ base := name
+ if dot := strings.IndexRune(base, '.'); dot >= 0 {
+ base = base[:dot]
+ }
+ if _, reserved := reservedWindowsProfileNames[strings.ToUpper(base)]; reserved {
+ return fmt.Errorf("profile name cannot use reserved device name %q", base)
+ }
+ return nil
+}
+
+func isProfileNameValidationError(err error) bool {
+ return err != nil && strings.HasPrefix(err.Error(), "profile name ")
+}