Skip to content

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

refactor(config): build FileConfigFromRuntime from per-section builders that share nothing with the runtime (PIN-006)

Luigi Agosti1485dffd2eb0
diff --git a/internal/config/config_file_marshal.go b/internal/config/config_file_marshal.go
index 5dfe9b90..fdd315b5 100644
--- a/internal/config/config_file_marshal.go
+++ b/internal/config/config_file_marshal.go
@@ -9,15 +9,20 @@ import (
)
func copyStringSlice(items []string) []string {
- if items == nil {
- return []string{}
- }
if len(items) == 0 {
return []string{}
}
return append([]string(nil), items...)
}
+func ptr[T any](v T) *T {
+ return &v
+}
+
+func secondsPtr(d time.Duration) *int {
+ return ptr(int(d / time.Second))
+}
+
func intPtrIfPositive(v int) *int {
if v <= 0 {
return nil
@@ -377,270 +382,226 @@ func FileConfigFromRuntime(cfg *RuntimeConfig) FileConfig {
if cfg == nil {
return DefaultFileConfig()
}
+ browsersDefault := cfg.DefaultBrowser
+ if browsersDefault == "" {
+ browsersDefault = BrowserChrome
+ }
+ fc := FileConfig{
+ Schema: CurrentConfigSchemaURL(),
+ Server: serverConfigFromRuntime(cfg),
+ Browser: browserConfigFromRuntime(cfg),
+ InstanceDefaults: instanceDefaultsFromRuntime(cfg),
+ Security: securityConfigFromRuntime(cfg),
+ Profiles: profilesConfigFromRuntime(cfg),
+ MultiInstance: multiInstanceConfigFromRuntime(cfg),
+ Timeouts: timeoutsConfigFromRuntime(cfg),
+ Observability: observabilityConfigFromRuntime(cfg),
+ Sessions: sessionsConfigFromRuntime(cfg),
+ AutoSolver: autoSolverConfigFromRuntime(cfg),
+ Browsers: BrowsersConfig{Default: browsersDefault, Available: cloneStringSlice(cfg.BrowsersAvailable)},
+ }
+ reconcileDefaultTargetProvider(&fc.Browser, browsersDefault, cfg)
+ return fc
+}
- noRestore := cfg.NoRestore
- blockImages := cfg.BlockImages
- blockMedia := cfg.BlockMedia
- blockAds := cfg.BlockAds
- maxTabs := cfg.MaxTabs
- maxParallelTabs := cfg.MaxParallelTabs
- noAnimations := cfg.NoAnimations
- captureAllowActivation := cfg.CaptureAllowActivation
- humanize := cfg.Humanize
- allowEvaluate := cfg.AllowEvaluate
- allowMacro := cfg.AllowMacro
- allowScreencast := cfg.AllowScreencast
- allowDownload := cfg.AllowDownload
- allowCookies := cfg.AllowCookies
- allowNetworkIntercept := cfg.AllowNetworkIntercept
- allowFileScheme := cfg.AllowFileScheme
- downloadAllowedDomains := copyStringSlice(cfg.DownloadAllowedDomains)
- downloadMaxBytes := cfg.EffectiveDownloadMaxBytes()
- allowUpload := cfg.AllowUpload
- allowClipboard := cfg.AllowClipboard
- allowStateExport := cfg.AllowStateExport
- uploadMaxRequestBytes := cfg.EffectiveUploadMaxRequestBytes()
- uploadMaxFiles := cfg.EffectiveUploadMaxFiles()
- uploadMaxFileBytes := cfg.EffectiveUploadMaxFileBytes()
- uploadMaxTotalBytes := cfg.EffectiveUploadMaxTotalBytes()
- maxRedirects := cfg.MaxRedirects
- trustLoopbackProxy := cfg.TrustLoopbackProxy
- attachEnabled := cfg.AttachEnabled
- attachForwardProxyAuth := cfg.AttachForwardProxyAuth
- idpi := cfg.IDPI
- 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
- agentSessionEnabled := cfg.Sessions.Agent.Enabled
- agentSessionIdleSec := int(cfg.Sessions.Agent.IdleTimeout / time.Second)
- agentSessionMaxLifetimeSec := int(cfg.Sessions.Agent.MaxLifetime / time.Second)
- 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
+func serverConfigFromRuntime(cfg *RuntimeConfig) ServerConfig {
+ return ServerConfig{
+ Port: cfg.Port,
+ Bind: cfg.Bind,
+ Token: cfg.Token,
+ StateDir: cfg.StateDir,
+ LogLevel: cfg.LogLevel,
+ NetworkBufferSize: intPtrIfPositive(cfg.NetworkBufferSize),
+ RetainNetworkBodies: ptr(cfg.RetainNetworkBodies),
+ RetainNetworkBodyMaxBytes: ptr(cfg.RetainNetworkBodyMaxBytes),
+ TrustProxyHeaders: ptr(cfg.TrustProxyHeaders),
+ CookieSecure: cfg.CookieSecure,
+ }
+}
- quarantineKeep := cfg.ProfileQuarantineKeep
- dialogAutoAccept := cfg.DialogAutoAccept
+func browserConfigFromRuntime(cfg *RuntimeConfig) BrowserConfig {
+ return BrowserConfig{
+ BrowserVersion: cfg.BrowserVersion,
+ BrowserBinary: cfg.BrowserBinary,
+ BrowserDebugPort: intPtrIfPositive(cfg.BrowserDebugPort),
+ BrowserExtraFlags: cfg.BrowserExtraFlags,
+ Cloak: cloakBrowserConfigFromRuntime(cfg),
+ ExtensionPaths: cloneStringSlice(cfg.ExtensionPaths),
+ Proxy: cloneBrowserProxyConfig(cfg.Proxy),
+ DefaultTarget: cfg.DefaultTarget,
+ FallbackOrder: cloneStringSlice(cfg.FallbackOrder),
+ Targets: cloneBrowserTargetsConfig(cfg.Targets),
+ }
+}
+func instanceDefaultsFromRuntime(cfg *RuntimeConfig) InstanceDefaultsConfig {
mode := "headless"
if !cfg.Headless {
mode = "headed"
}
-
- var netBufSize *int
- if cfg.NetworkBufferSize > 0 {
- v := cfg.NetworkBufferSize
- netBufSize = &v
+ return InstanceDefaultsConfig{
+ Mode: mode,
+ NoRestore: ptr(cfg.NoRestore),
+ Timezone: cfg.Timezone,
+ BlockImages: ptr(cfg.BlockImages),
+ BlockMedia: ptr(cfg.BlockMedia),
+ BlockAds: ptr(cfg.BlockAds),
+ MaxTabs: ptr(cfg.MaxTabs),
+ MaxParallelTabs: ptr(cfg.MaxParallelTabs),
+ UserAgent: cfg.UserAgent,
+ NoAnimations: ptr(cfg.NoAnimations),
+ CaptureAllowActivation: ptr(cfg.CaptureAllowActivation),
+ Humanize: ptr(cfg.Humanize),
+ StealthLevel: cfg.StealthLevel,
+ TabEvictionPolicy: cfg.TabEvictionPolicy,
+ TabPolicy: tabPolicyDefaultsFromRuntime(cfg),
+ DialogAutoAccept: ptr(cfg.DialogAutoAccept),
}
- retainBodies := cfg.RetainNetworkBodies
- retainBodyMaxBytes := cfg.RetainNetworkBodyMaxBytes
+}
- // Always emit browsers.default; stop writing the deprecated browser.provider
- // field. Write the value verbatim (unknown values are kept raw by config load
- // so the load-time warning + chrome fallback still fire — don't normalize and
- // silently rewrite them); only an empty value falls back to chrome.
- browsersDefault := cfg.DefaultBrowser
- if browsersDefault == "" {
- browsersDefault = BrowserChrome
+func securityConfigFromRuntime(cfg *RuntimeConfig) SecurityConfig {
+ idpi := cfg.IDPI
+ idpi.CustomPatterns = cloneStringSlice(cfg.IDPI.CustomPatterns)
+ return SecurityConfig{
+ AllowEvaluate: ptr(cfg.AllowEvaluate),
+ AllowMacro: ptr(cfg.AllowMacro),
+ AllowScreencast: ptr(cfg.AllowScreencast),
+ AllowDownload: ptr(cfg.AllowDownload),
+ AllowCookies: ptr(cfg.AllowCookies),
+ AllowNetworkIntercept: ptr(cfg.AllowNetworkIntercept),
+ AllowFileScheme: ptr(cfg.AllowFileScheme),
+ AllowedDomains: cloneStringSlice(cfg.AllowedDomains),
+ DownloadAllowedDomains: cloneStringSlice(cfg.DownloadAllowedDomains),
+ DownloadMaxBytes: ptr(cfg.EffectiveDownloadMaxBytes()),
+ AllowUpload: ptr(cfg.AllowUpload),
+ AllowClipboard: ptr(cfg.AllowClipboard),
+ AllowStateExport: ptr(cfg.AllowStateExport),
+ UploadMaxRequestBytes: ptr(cfg.EffectiveUploadMaxRequestBytes()),
+ UploadMaxFiles: ptr(cfg.EffectiveUploadMaxFiles()),
+ UploadMaxFileBytes: ptr(cfg.EffectiveUploadMaxFileBytes()),
+ UploadMaxTotalBytes: ptr(cfg.EffectiveUploadMaxTotalBytes()),
+ MaxRedirects: ptr(cfg.MaxRedirects),
+ TrustedProxyCIDRs: cloneStringSlice(cfg.TrustedProxyCIDRs),
+ TrustedResolveCIDRs: cloneStringSlice(cfg.TrustedResolveCIDRs),
+ TrustLoopbackProxy: ptr(cfg.TrustLoopbackProxy),
+ Attach: AttachConfig{
+ Enabled: ptr(cfg.AttachEnabled),
+ AllowHosts: cloneStringSlice(cfg.AttachAllowHosts),
+ AllowSchemes: cloneStringSlice(cfg.AttachAllowSchemes),
+ ForwardProxyAuth: ptr(cfg.AttachForwardProxyAuth),
+ },
+ IDPI: &idpi,
}
- browsersBlock := BrowsersConfig{
- Default: browsersDefault,
- Available: append([]string(nil), cfg.BrowsersAvailable...),
+}
+
+func profilesConfigFromRuntime(cfg *RuntimeConfig) ProfilesConfig {
+ return ProfilesConfig{
+ BaseDir: cfg.ProfilesBaseDir,
+ DefaultProfile: cfg.DefaultProfile,
+ QuarantineKeep: ptr(cfg.ProfileQuarantineKeep),
}
+}
- fc := FileConfig{
- Schema: CurrentConfigSchemaURL(),
- Server: ServerConfig{
- Port: cfg.Port,
- Bind: cfg.Bind,
- Token: cfg.Token,
- StateDir: cfg.StateDir,
- LogLevel: cfg.LogLevel,
- NetworkBufferSize: netBufSize,
- RetainNetworkBodies: &retainBodies,
- RetainNetworkBodyMaxBytes: &retainBodyMaxBytes,
- TrustProxyHeaders: &cfg.TrustProxyHeaders,
- CookieSecure: cfg.CookieSecure,
- },
- Browser: BrowserConfig{
- // Provider intentionally omitted — deprecated in favor of browsers.default.
- BrowserVersion: cfg.BrowserVersion,
- BrowserBinary: cfg.BrowserBinary,
- BrowserDebugPort: intPtrIfPositive(cfg.BrowserDebugPort),
- BrowserExtraFlags: cfg.BrowserExtraFlags,
- Cloak: cloakBrowserConfigFromRuntime(cfg),
- ExtensionPaths: append([]string(nil), cfg.ExtensionPaths...),
- Proxy: cloneBrowserProxyConfig(cfg.Proxy),
- DefaultTarget: cfg.DefaultTarget,
- FallbackOrder: append([]string(nil), cfg.FallbackOrder...),
- Targets: cloneBrowserTargetsConfig(cfg.Targets),
- },
- InstanceDefaults: InstanceDefaultsConfig{
- Mode: mode,
- NoRestore: &noRestore,
- Timezone: cfg.Timezone,
- BlockImages: &blockImages,
- BlockMedia: &blockMedia,
- BlockAds: &blockAds,
- MaxTabs: &maxTabs,
- MaxParallelTabs: &maxParallelTabs,
- UserAgent: cfg.UserAgent,
- NoAnimations: &noAnimations,
- CaptureAllowActivation: &captureAllowActivation,
- Humanize: &humanize,
- StealthLevel: cfg.StealthLevel,
- TabEvictionPolicy: cfg.TabEvictionPolicy,
- TabPolicy: tabPolicyDefaultsFromRuntime(cfg),
- DialogAutoAccept: &dialogAutoAccept,
+func multiInstanceConfigFromRuntime(cfg *RuntimeConfig) MultiInstanceConfig {
+ return MultiInstanceConfig{
+ Strategy: cfg.Strategy,
+ AllocationPolicy: cfg.AllocationPolicy,
+ InstancePortStart: ptr(cfg.InstancePortStart),
+ InstancePortEnd: ptr(cfg.InstancePortEnd),
+ Restart: MultiInstanceRestartConfig{
+ MaxRestarts: ptr(cfg.RestartMaxRestarts),
+ InitBackoffSec: secondsPtr(cfg.RestartInitBackoff),
+ MaxBackoffSec: secondsPtr(cfg.RestartMaxBackoff),
+ StableAfterSec: secondsPtr(cfg.RestartStableAfter),
},
- Security: SecurityConfig{
- AllowEvaluate: &allowEvaluate,
- AllowMacro: &allowMacro,
- AllowScreencast: &allowScreencast,
- AllowDownload: &allowDownload,
- AllowCookies: &allowCookies,
- AllowNetworkIntercept: &allowNetworkIntercept,
- AllowFileScheme: &allowFileScheme,
- AllowedDomains: append([]string(nil), cfg.AllowedDomains...),
- DownloadAllowedDomains: downloadAllowedDomains,
- DownloadMaxBytes: &downloadMaxBytes,
- AllowUpload: &allowUpload,
- AllowClipboard: &allowClipboard,
- AllowStateExport: &allowStateExport,
- UploadMaxRequestBytes: &uploadMaxRequestBytes,
- UploadMaxFiles: &uploadMaxFiles,
- UploadMaxFileBytes: &uploadMaxFileBytes,
- UploadMaxTotalBytes: &uploadMaxTotalBytes,
- MaxRedirects: &maxRedirects,
- TrustedProxyCIDRs: append([]string(nil), cfg.TrustedProxyCIDRs...),
- TrustedResolveCIDRs: append([]string(nil), cfg.TrustedResolveCIDRs...),
- TrustLoopbackProxy: &trustLoopbackProxy,
- Attach: AttachConfig{
- Enabled: &attachEnabled,
- AllowHosts: append([]string(nil), cfg.AttachAllowHosts...),
- AllowSchemes: append([]string(nil), cfg.AttachAllowSchemes...),
- ForwardProxyAuth: &attachForwardProxyAuth,
+ }
+}
+
+func timeoutsConfigFromRuntime(cfg *RuntimeConfig) TimeoutsConfig {
+ return 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),
+ }
+}
+
+func observabilityConfigFromRuntime(cfg *RuntimeConfig) ObservabilityFileConfig {
+ activity := cfg.Observability.Activity
+ return ObservabilityFileConfig{
+ Activity: ActivityFileConfig{
+ Enabled: ptr(activity.Enabled),
+ SessionIdleSec: ptr(activity.SessionIdleSec),
+ RetentionDays: ptr(activity.RetentionDays),
+ Events: ActivityEventsFileConfig{
+ Dashboard: ptr(activity.Events.Dashboard),
+ Server: ptr(activity.Events.Server),
+ Bridge: ptr(activity.Events.Bridge),
+ Orchestrator: ptr(activity.Events.Orchestrator),
+ Scheduler: ptr(activity.Events.Scheduler),
+ MCP: ptr(activity.Events.MCP),
+ Other: ptr(activity.Events.Other),
},
- IDPI: &idpi,
},
- Profiles: ProfilesConfig{
- BaseDir: cfg.ProfilesBaseDir,
- DefaultProfile: cfg.DefaultProfile,
- QuarantineKeep: &quarantineKeep,
- },
- MultiInstance: MultiInstanceConfig{
- Strategy: cfg.Strategy,
- AllocationPolicy: cfg.AllocationPolicy,
- InstancePortStart: &start,
- InstancePortEnd: &end,
- Restart: MultiInstanceRestartConfig{
- MaxRestarts: &restartMaxRestarts,
- InitBackoffSec: &restartInitBackoffSec,
- MaxBackoffSec: &restartMaxBackoffSec,
- StableAfterSec: &restartStableAfterSec,
- },
+ }
+}
+
+func sessionsConfigFromRuntime(cfg *RuntimeConfig) SessionsFileConfig {
+ dashboard := cfg.Sessions.Dashboard
+ agent := cfg.Sessions.Agent
+ return SessionsFileConfig{
+ Dashboard: DashboardSessionFileConfig{
+ Persist: ptr(dashboard.Persist),
+ IdleTimeoutSec: secondsPtr(dashboard.IdleTimeout),
+ MaxLifetimeSec: secondsPtr(dashboard.MaxLifetime),
+ ElevationWindowSec: secondsPtr(dashboard.ElevationWindow),
+ PersistElevationAcrossRestart: ptr(dashboard.PersistElevationAcrossRestart),
+ RequireElevation: ptr(dashboard.RequireElevation),
},
- 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),
+ Agent: AgentSessionFileConfig{
+ Enabled: ptr(agent.Enabled),
+ Mode: agent.Mode,
+ IdleTimeoutSec: secondsPtr(agent.IdleTimeout),
+ MaxLifetimeSec: secondsPtr(agent.MaxLifetime),
},
- 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,
- },
- },
+ }
+}
+
+func autoSolverConfigFromRuntime(cfg *RuntimeConfig) AutoSolverFileConfig {
+ a := cfg.AutoSolver
+ return AutoSolverFileConfig{
+ Enabled: ptr(a.Enabled),
+ AutoTrigger: ptr(a.AutoTrigger),
+ TriggerOnNavigate: ptr(a.TriggerOnNavigate),
+ TriggerOnAction: ptr(a.TriggerOnAction),
+ MaxAttempts: ptr(a.MaxAttempts),
+ SolverTimeoutSec: ptr(a.SolverTimeoutSec),
+ RetryBaseDelayMs: ptr(a.RetryBaseDelayMs),
+ RetryMaxDelayMs: ptr(a.RetryMaxDelayMs),
+ Solvers: cloneStringSlice(a.Solvers),
+ LLMProvider: a.LLMProvider,
+ LLMFallback: ptr(a.LLMFallback),
+ External: AutoSolverExtConf{
+ CapsolverKey: a.CapsolverKey,
+ TwoCaptchaKey: a.TwoCaptchaKey,
},
- Sessions: SessionsFileConfig{
- Dashboard: DashboardSessionFileConfig{
- Persist: &dashboardSessionPersist,
- IdleTimeoutSec: &dashboardSessionIdleSec,
- MaxLifetimeSec: &dashboardSessionMaxLifetimeSec,
- ElevationWindowSec: &dashboardSessionElevationWindowSec,
- PersistElevationAcrossRestart: &dashboardSessionPersistElevationAcrossRestart,
- RequireElevation: &dashboardSessionRequireElevation,
- },
- Agent: AgentSessionFileConfig{
- Enabled: &agentSessionEnabled,
- Mode: cfg.Sessions.Agent.Mode,
- IdleTimeoutSec: &agentSessionIdleSec,
- MaxLifetimeSec: &agentSessionMaxLifetimeSec,
+ Credentials: AutoSolverCredentialsConf{
+ Login: AutoSolverLoginConf{
+ User: a.Credentials.Login.User,
+ Password: a.Credentials.Login.Password,
},
- },
- 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,
+ Signup: AutoSolverSignupConf{
+ Name: a.Credentials.Signup.Name,
+ Email: a.Credentials.Signup.Email,
+ Password: a.Credentials.Signup.Password,
},
- 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,
- },
+ Form: AutoSolverFormConf{
+ Field1: a.Credentials.Form.Field1,
+ Field2: a.Credentials.Form.Field2,
+ Email: a.Credentials.Form.Email,
},
},
- Browsers: browsersBlock,
}
-
- reconcileDefaultTargetProvider(&fc.Browser, browsersDefault, cfg)
-
- return fc
}
// reconcileDefaultTargetProvider keeps the serialized default browser target
diff --git a/internal/config/file_config_from_runtime_alias_test.go b/internal/config/file_config_from_runtime_alias_test.go
new file mode 100644
index 00000000..75de10ab
--- /dev/null
+++ b/internal/config/file_config_from_runtime_alias_test.go
@@ -0,0 +1,119 @@
+package config
+
+import (
+ "go/ast"
+ "go/parser"
+ "go/token"
+ "testing"
+)
+
+func TestFileConfigFromRuntimeSharesNothingWithTheRuntime(t *testing.T) {
+ cfg := populatedRuntimeConfig(t)
+ before := *cfg
+ beforePatterns := append([]string(nil), cfg.IDPI.CustomPatterns...)
+ beforeAllowHosts := append([]string(nil), cfg.AttachAllowHosts...)
+
+ fc := FileConfigFromRuntime(cfg)
+ *fc.Server.TrustProxyHeaders = !*fc.Server.TrustProxyHeaders
+ fc.Security.IDPI.CustomPatterns[0] = "mutated"
+ fc.Security.IDPI.Enabled = !fc.Security.IDPI.Enabled
+ fc.Security.Attach.AllowHosts[0] = "mutated"
+ *fc.Security.AllowEvaluate = !*fc.Security.AllowEvaluate
+ *fc.MultiInstance.Restart.MaxRestarts++
+ fc.Browser.ExtensionPaths[0] = "mutated"
+
+ if cfg.TrustProxyHeaders != before.TrustProxyHeaders {
+ t.Fatalf("Server.TrustProxyHeaders aliases the runtime field")
+ }
+ if cfg.IDPI.CustomPatterns[0] != beforePatterns[0] || cfg.IDPI.Enabled != before.IDPI.Enabled {
+ t.Fatalf("Security.IDPI shares memory with the runtime: %+v", cfg.IDPI)
+ }
+ if cfg.AttachAllowHosts[0] != beforeAllowHosts[0] || cfg.AllowEvaluate != before.AllowEvaluate || cfg.RestartMaxRestarts != before.RestartMaxRestarts || cfg.ExtensionPaths[0] == "mutated" {
+ t.Fatalf("a FileConfig write reached the runtime: %+v", cfg)
+ }
+}
+
+func TestFileConfigFromRuntimeDelegatesToPerSectionBuilders(t *testing.T) {
+ fset := token.NewFileSet()
+ f, err := parser.ParseFile(fset, "config_file_marshal.go", nil, 0)
+ if err != nil {
+ t.Fatal(err)
+ }
+ for _, decl := range f.Decls {
+ fn, ok := decl.(*ast.FuncDecl)
+ if !ok || fn.Name.Name != "FileConfigFromRuntime" {
+ continue
+ }
+ lines := fset.Position(fn.End()).Line - fset.Position(fn.Pos()).Line
+ if lines > 60 {
+ t.Fatalf("FileConfigFromRuntime spans %d lines, want under 60 with the sections built by their own functions", lines)
+ }
+ var addressOfRuntime []string
+ ast.Inspect(fn.Body, func(n ast.Node) bool {
+ if u, ok := n.(*ast.UnaryExpr); ok && u.Op == token.AND {
+ addressOfRuntime = append(addressOfRuntime, fset.Position(u.Pos()).String())
+ }
+ return true
+ })
+ for _, b := range []string{"serverConfigFromRuntime", "securityConfigFromRuntime", "instanceDefaultsFromRuntime"} {
+ if !callsFunction(fn.Body, b) {
+ t.Errorf("FileConfigFromRuntime does not delegate to %s", b)
+ }
+ }
+ return
+ }
+ t.Fatal("FileConfigFromRuntime not found")
+}
+
+func TestSectionBuildersNeverTakeTheAddressOfARuntimeField(t *testing.T) {
+ fset := token.NewFileSet()
+ f, err := parser.ParseFile(fset, "config_file_marshal.go", nil, 0)
+ if err != nil {
+ t.Fatal(err)
+ }
+ checked := 0
+ for _, decl := range f.Decls {
+ fn, ok := decl.(*ast.FuncDecl)
+ if !ok || fn.Recv != nil || fn.Type.Params.NumFields() != 1 {
+ continue
+ }
+ param := fn.Type.Params.List[0]
+ star, ok := param.Type.(*ast.StarExpr)
+ if !ok {
+ continue
+ }
+ if ident, ok := star.X.(*ast.Ident); !ok || ident.Name != "RuntimeConfig" {
+ continue
+ }
+ checked++
+ cfgName := param.Names[0].Name
+ ast.Inspect(fn.Body, func(n ast.Node) bool {
+ u, ok := n.(*ast.UnaryExpr)
+ if !ok || u.Op != token.AND {
+ return true
+ }
+ if sel, ok := u.X.(*ast.SelectorExpr); ok {
+ if root, ok := sel.X.(*ast.Ident); ok && root.Name == cfgName {
+ t.Errorf("%s: %s takes the address of a runtime field; copy the value through ptr instead", fset.Position(u.Pos()), fn.Name.Name)
+ }
+ }
+ return true
+ })
+ }
+ if checked < 10 {
+ t.Fatalf("checked %d runtime-taking builders, want the section builders plus FileConfigFromRuntime", checked)
+ }
+}
+
+func callsFunction(body *ast.BlockStmt, name string) bool {
+ found := false
+ ast.Inspect(body, func(n ast.Node) bool {
+ if call, ok := n.(*ast.CallExpr); ok {
+ if id, ok := call.Fun.(*ast.Ident); ok && id.Name == name {
+ found = true
+ }
+ }
+ return true
+ })
+ return found
+}
diff --git a/internal/config/file_config_from_runtime_golden_test.go b/internal/config/file_config_from_runtime_golden_test.go
new file mode 100644
index 00000000..07a95eb1
--- /dev/null
+++ b/internal/config/file_config_from_runtime_golden_test.go
@@ -0,0 +1,92 @@
+package config
+
+import (
+ "bytes"
+ "encoding/json"
+ "flag"
+ "os"
+ "path/filepath"
+ "testing"
+)
+
+var updateGolden = flag.Bool("update", false, "rewrite the FileConfigFromRuntime golden")
+
+const populatedFileConfigJSON = `{
+ "server": {"port": "9911", "bind": "0.0.0.0", "token": "tok", "stateDir": "/tmp/state", "logLevel": "debug",
+ "networkBufferSize": 250, "retainNetworkBodies": true, "retainNetworkBodyMaxBytes": 4096,
+ "trustProxyHeaders": true, "cookieSecure": true},
+ "browsers": {"default": "cloak", "available": ["chrome", "cloak"]},
+ "browser": {"browserVersion": "131", "browserBinary": "/usr/bin/chromium", "browserDebugPort": 9333,
+ "browserExtraFlags": "--flag-a", "extensionPaths": ["/ext/a", "/ext/b"],
+ "proxy": {"server": "http://proxy:3128", "username": "u", "password": "p", "bypass": ["localhost"]},
+ "cloak": {"level": "high"},
+ "defaultTarget": "primary", "fallbackOrder": ["primary", "backup"],
+ "targets": {"primary": {"provider": "chrome"}, "backup": {"provider": "cloak"}}},
+ "instanceDefaults": {"mode": "headed", "noRestore": true, "timezone": "Europe/Rome", "blockImages": true,
+ "blockMedia": true, "blockAds": true, "maxTabs": 7, "maxParallelTabs": 3,
+ "userAgent": "ua/1", "noAnimations": true, "captureAllowActivation": true, "humanize": true,
+ "stealthLevel": "max", "tabEvictionPolicy": "reject", "dialogAutoAccept": true,
+ "tabPolicy": {"allowedDomains": ["a.example"], "blockedDomains": ["b.example"]}},
+ "security": {"allowEvaluate": true, "allowMacro": true, "allowScreencast": true, "allowDownload": true,
+ "allowCookies": true, "allowNetworkIntercept": true, "allowFileScheme": true,
+ "allowedDomains": [], "downloadAllowedDomains": ["dl.example"], "downloadMaxBytes": 123456,
+ "allowUpload": true, "allowClipboard": true, "allowStateExport": true,
+ "uploadMaxRequestBytes": 1000, "uploadMaxFiles": 4, "uploadMaxFileBytes": 500, "uploadMaxTotalBytes": 900,
+ "maxRedirects": 9, "trustedProxyCIDRs": ["10.0.0.0/8"], "trustedResolveCIDRs": ["192.168.0.0/16"],
+ "trustLoopbackProxy": true,
+ "attach": {"enabled": true, "allowHosts": ["attach.example"], "allowSchemes": ["ws"], "forwardProxyAuth": true},
+ "idpi": {"enabled": true, "strictMode": true, "scanContent": true, "wrapContent": true,
+ "customPatterns": ["ignore previous"], "scanTimeoutSec": 3, "shieldThreshold": 55}},
+ "profiles": {"baseDir": "/tmp/profiles", "defaultProfile": "main", "quarantineKeep": 2},
+ "multiInstance": {"strategy": "always-on", "allocationPolicy": "round-robin", "instancePortStart": 9900, "instancePortEnd": 9950,
+ "restart": {"maxRestarts": 5, "initBackoffSec": 2, "maxBackoffSec": 60, "stableAfterSec": 120}},
+ "timeouts": {"actionSec": 11, "navigateSec": 22, "shutdownSec": 33, "waitNavMs": 444},
+ "observability": {"activity": {"enabled": true, "sessionIdleSec": 300, "retentionDays": 9,
+ "events": {"dashboard": true, "server": true, "bridge": true, "orchestrator": true,
+ "scheduler": true, "mcp": true, "other": true}}},
+ "sessions": {"dashboard": {"persist": true, "idleTimeoutSec": 600, "maxLifetimeSec": 7200, "elevationWindowSec": 90,
+ "persistElevationAcrossRestart": true, "requireElevation": true},
+ "agent": {"enabled": true, "mode": "strict", "idleTimeoutSec": 120, "maxLifetimeSec": 3600}},
+ "autoSolver": {"enabled": true, "autoTrigger": true, "triggerOnNavigate": true, "triggerOnAction": true,
+ "maxAttempts": 4, "solverTimeoutSec": 30, "retryBaseDelayMs": 100, "retryMaxDelayMs": 900,
+ "solvers": ["capsolver", "2captcha"], "llmProvider": "anthropic", "llmFallback": true,
+ "external": {"capsolverKey": "ck", "twoCaptchaKey": "tk"},
+ "credentials": {"login": {"user": "lu", "password": "lp"},
+ "signup": {"name": "sn", "email": "se", "password": "sp"},
+ "form": {"field1": "f1", "field2": "f2", "email": "fe"}}}
+}`
+
+func populatedRuntimeConfig(t *testing.T) *RuntimeConfig {
+ t.Helper()
+ var fc FileConfig
+ if err := json.Unmarshal([]byte(populatedFileConfigJSON), &fc); err != nil {
+ t.Fatalf("fixture: %v", err)
+ }
+ cfg := &RuntimeConfig{}
+ applyFileConfig(cfg, &fc)
+ if len(cfg.IDPI.CustomPatterns) == 0 || !cfg.TrustProxyHeaders || len(cfg.AttachAllowHosts) == 0 {
+ t.Fatalf("fixture did not populate the fields the golden exists to pin: %+v", cfg)
+ }
+ return cfg
+}
+
+func TestFileConfigFromRuntimeEmitsTheRecordedJSON(t *testing.T) {
+ got, err := json.MarshalIndent(FileConfigFromRuntime(populatedRuntimeConfig(t)), "", " ")
+ if err != nil {
+ t.Fatal(err)
+ }
+ got = append(got, '\n')
+ path := filepath.Join("testdata", "file_config_from_runtime.golden.json")
+ if *updateGolden {
+ if err := os.WriteFile(path, got, 0o600); err != nil {
+ t.Fatal(err)
+ }
+ }
+ want, err := os.ReadFile(path)
+ if err != nil {
+ t.Fatalf("read golden: %v (run with -update to record it)", err)
+ }
+ if !bytes.Equal(got, want) {
+ t.Fatalf("FileConfigFromRuntime JSON drifted from %s; diff it against the marshalled output before deciding whether to re-record:\n%s", path, got)
+ }
+}
diff --git a/internal/config/testdata/file_config_from_runtime.golden.json b/internal/config/testdata/file_config_from_runtime.golden.json
new file mode 100644
index 00000000..0985275b
--- /dev/null
+++ b/internal/config/testdata/file_config_from_runtime.golden.json
@@ -0,0 +1,228 @@
+{
+ "$schema": "https://raw.githubusercontent.com/pinchtab/pinchtab/main/schema/config.json",
+ "server": {
+ "port": "9911",
+ "bind": "0.0.0.0",
+ "token": "tok",
+ "stateDir": "/tmp/state",
+ "logLevel": "debug",
+ "networkBufferSize": 250,
+ "retainNetworkBodies": true,
+ "retainNetworkBodyMaxBytes": 4096,
+ "trustProxyHeaders": true,
+ "cookieSecure": true
+ },
+ "browser": {
+ "version": "",
+ "binary": "",
+ "extraFlags": "",
+ "cloak": {
+ "storageQuotaMB": 0,
+ "disableDefaultStealthArgs": true
+ },
+ "extensionPaths": [
+ "/ext/a",
+ "/ext/b"
+ ],
+ "proxy": {
+ "server": "http://proxy:3128",
+ "username": "u",
+ "password": "p"
+ },
+ "defaultTarget": "primary",
+ "fallbackOrder": [
+ "primary",
+ "backup"
+ ],
+ "targets": {
+ "backup": {
+ "provider": "cloak",
+ "cloak": {},
+ "proxy": {}
+ },
+ "primary": {
+ "provider": "chrome",
+ "cloak": {},
+ "proxy": {}
+ }
+ }
+ },
+ "browsers": {
+ "default": "cloak",
+ "available": [
+ "chrome",
+ "cloak"
+ ]
+ },
+ "instanceDefaults": {
+ "mode": "headed",
+ "noRestore": true,
+ "timezone": "Europe/Rome",
+ "blockImages": true,
+ "blockMedia": true,
+ "blockAds": true,
+ "maxTabs": 7,
+ "maxParallelTabs": 3,
+ "userAgent": "ua/1",
+ "noAnimations": true,
+ "captureAllowActivation": true,
+ "humanize": true,
+ "stealthLevel": "max",
+ "tabEvictionPolicy": "reject",
+ "dialogAutoAccept": true
+ },
+ "security": {
+ "allowEvaluate": true,
+ "allowMacro": true,
+ "allowScreencast": true,
+ "allowDownload": true,
+ "allowCookies": true,
+ "allowNetworkIntercept": true,
+ "allowFileScheme": true,
+ "allowedDomains": null,
+ "downloadAllowedDomains": [
+ "dl.example"
+ ],
+ "downloadMaxBytes": 123456,
+ "allowUpload": true,
+ "allowClipboard": true,
+ "allowStateExport": true,
+ "stateEncryptionKey": null,
+ "uploadMaxRequestBytes": 1000,
+ "uploadMaxFiles": 4,
+ "uploadMaxFileBytes": 500,
+ "uploadMaxTotalBytes": 900,
+ "maxRedirects": 9,
+ "trustedProxyCIDRs": [
+ "10.0.0.0/8"
+ ],
+ "trustedResolveCIDRs": [
+ "192.168.0.0/16"
+ ],
+ "trustLoopbackProxy": true,
+ "attach": {
+ "enabled": true,
+ "allowHosts": [
+ "attach.example"
+ ],
+ "allowSchemes": [
+ "ws"
+ ],
+ "forwardProxyAuth": true
+ },
+ "idpi": {
+ "enabled": true,
+ "strictMode": true,
+ "scanContent": true,
+ "wrapContent": true,
+ "customPatterns": [
+ "ignore previous"
+ ],
+ "scanTimeoutSec": 3,
+ "shieldThreshold": 55
+ }
+ },
+ "profiles": {
+ "baseDir": "/tmp/profiles",
+ "defaultProfile": "main",
+ "quarantineKeep": 2
+ },
+ "multiInstance": {
+ "strategy": "always-on",
+ "allocationPolicy": "round-robin",
+ "instancePortStart": 9900,
+ "instancePortEnd": 9950,
+ "restart": {
+ "maxRestarts": 5,
+ "initBackoffSec": 2,
+ "maxBackoffSec": 60,
+ "stableAfterSec": 120
+ }
+ },
+ "timeouts": {
+ "actionSec": 11,
+ "navigateSec": 22,
+ "shutdownSec": 33,
+ "waitNavMs": 444
+ },
+ "scheduler": {
+ "enabled": null,
+ "strategy": "",
+ "maxQueueSize": null,
+ "maxPerAgent": null,
+ "maxInflight": null,
+ "maxPerAgentInflight": null,
+ "resultTTLSec": null,
+ "workerCount": null,
+ "maxBatchSize": null
+ },
+ "observability": {
+ "activity": {
+ "enabled": true,
+ "sessionIdleSec": 300,
+ "retentionDays": 9,
+ "stateDir": "",
+ "events": {
+ "dashboard": true,
+ "server": true,
+ "bridge": true,
+ "orchestrator": true,
+ "scheduler": true,
+ "mcp": true,
+ "other": true
+ }
+ }
+ },
+ "sessions": {
+ "dashboard": {
+ "persist": true,
+ "idleTimeoutSec": 600,
+ "maxLifetimeSec": 7200,
+ "elevationWindowSec": 90,
+ "persistElevationAcrossRestart": true,
+ "requireElevation": true
+ },
+ "agent": {
+ "enabled": true,
+ "mode": "strict",
+ "idleTimeoutSec": 120,
+ "maxLifetimeSec": 3600
+ }
+ },
+ "autoSolver": {
+ "enabled": true,
+ "autoTrigger": true,
+ "triggerOnNavigate": true,
+ "triggerOnAction": true,
+ "maxAttempts": 4,
+ "solverTimeoutSec": 30,
+ "retryBaseDelayMs": 100,
+ "retryMaxDelayMs": 900,
+ "solvers": [
+ "capsolver",
+ "2captcha"
+ ],
+ "llmProvider": "anthropic",
+ "llmFallback": true,
+ "external": {
+ "capsolverKey": "ck",
+ "twoCaptchaKey": "tk"
+ },
+ "credentials": {
+ "login": {
+ "user": "lu",
+ "password": "lp"
+ },
+ "signup": {
+ "name": "sn",
+ "email": "se",
+ "password": "sp"
+ },
+ "form": {
+ "field1": "f1",
+ "field2": "f2",
+ "email": "fe"
+ }
+ }
+ }
+}