Skip to content

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

feat: implement Chrome initialization (Phase 2 - Part 1)

Mario56491d2a2ad7
diff --git a/internal/bridge/bridge.go b/internal/bridge/bridge.go
index 0c0aa436..95ae497e 100644
--- a/internal/bridge/bridge.go
+++ b/internal/bridge/bridge.go
@@ -106,9 +106,24 @@ func (b *Bridge) EnsureChrome(cfg *config.RuntimeConfig) error {
return nil // Already has browser context
}
- // This will be implemented via a callback from cmd/pinchtab/browser.go
- // For now, return an error if not initialized
- return fmt.Errorf("browser not initialized and EnsureChrome not implemented")
+ // Initialize Chrome if not already done
+ allocCtx, allocCancel, browserCtx, browserCancel, err := InitChrome(cfg)
+ 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
+
+ // Initialize TabManager now that browser is ready
+ if b.Config != nil && b.TabManager == nil {
+ b.TabManager = NewTabManager(browserCtx, b.Config, b.tabSetup)
+ }
+
+ return nil
}
func (b *Bridge) SetBrowserContexts(allocCtx context.Context, allocCancel context.CancelFunc, browserCtx context.Context, browserCancel context.CancelFunc) {
diff --git a/internal/bridge/init.go b/internal/bridge/init.go
new file mode 100644
index 00000000..5ecd0050
--- /dev/null
+++ b/internal/bridge/init.go
@@ -0,0 +1,133 @@
+package bridge
+
+import (
+ "context"
+ "fmt"
+ "log/slog"
+ "math/rand"
+ "time"
+
+ "github.com/chromedp/chromedp"
+ "github.com/pinchtab/pinchtab/internal/assets"
+ "github.com/pinchtab/pinchtab/internal/config"
+ "github.com/pinchtab/pinchtab/internal/human"
+)
+
+// InitChrome initializes a Chrome browser for a Bridge instance
+// It allocates the browser, starts it with appropriate flags (headless/headed),
+// and returns the contexts ready for use.
+func InitChrome(cfg *config.RuntimeConfig) (context.Context, context.CancelFunc, context.Context, context.CancelFunc, error) {
+ // Setup allocator
+ allocCtx, allocCancel, opts := setupAllocator(cfg)
+
+ // Start Chrome browser
+ browserCtx, browserCancel, err := startChrome(allocCtx, cfg, opts)
+ if err != nil {
+ allocCancel()
+ return nil, nil, nil, nil, fmt.Errorf("failed to start chrome: %w", err)
+ }
+
+ slog.Info("chrome initialized", "headless", cfg.Headless, "profile", cfg.ProfileDir)
+ return allocCtx, allocCancel, browserCtx, browserCancel, nil
+}
+
+// setupAllocator creates a Chrome allocator with appropriate options
+func setupAllocator(cfg *config.RuntimeConfig) (context.Context, context.CancelFunc, []chromedp.ExecAllocatorOption) {
+ opts := chromedp.DefaultExecAllocatorOptions[:]
+
+ // Headless mode
+ if cfg.Headless {
+ opts = append(opts, chromedp.Headless)
+ } else {
+ opts = append(opts, chromedp.Flag("headless", false))
+ }
+
+ // Chrome binary
+ if cfg.ChromeBinary != "" {
+ opts = append(opts, chromedp.ExecPath(cfg.ChromeBinary))
+ }
+
+ // Profile
+ if cfg.ProfileDir != "" {
+ opts = append(opts, chromedp.UserDataDir(cfg.ProfileDir))
+ }
+
+ // Window size
+ w, h := randomWindowSize()
+ opts = append(opts, chromedp.WindowSize(w, h))
+
+ // Common stealth flags
+ opts = append(opts,
+ chromedp.Flag("disable-automation", ""),
+ chromedp.Flag("disable-blink-features", "AutomationControlled"),
+ chromedp.Flag("disable-dev-shm-usage", ""),
+ chromedp.Flag("no-first-run", ""),
+ chromedp.Flag("no-default-browser-check", ""),
+ )
+
+ // Extra flags
+ if cfg.ChromeExtraFlags != "" {
+ opts = append(opts, chromedp.Flag("", cfg.ChromeExtraFlags))
+ }
+
+ // Timezone
+ if cfg.Timezone != "" {
+ opts = append(opts, chromedp.Flag("TZ", cfg.Timezone))
+ }
+
+ ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
+
+ return ctx, cancel, opts
+}
+
+// startChrome starts the Chrome browser with the given options
+func startChrome(allocCtx context.Context, cfg *config.RuntimeConfig, opts []chromedp.ExecAllocatorOption) (context.Context, context.CancelFunc, error) {
+ // Create allocator context
+ allocCtx, allocCancel := chromedp.NewExecAllocator(allocCtx, opts...)
+
+ // Create browser context
+ browserCtx, cancel := chromedp.NewContext(allocCtx)
+
+ // Initialize stealth script
+ stealthSeed := rand.Intn(1000000000)
+ human.SetHumanRandSeed(int64(stealthSeed))
+ seededScript := fmt.Sprintf("var __pinchtab_seed = %d;\nvar __pinchtab_stealth_level = %q;\n", stealthSeed, cfg.StealthLevel) + assets.StealthScript
+
+ // Start browser (connect to Chrome)
+ if err := chromedp.Run(browserCtx); err != nil {
+ cancel()
+ allocCancel()
+ return nil, nil, fmt.Errorf("failed to connect to chrome: %w", err)
+ }
+
+ // Inject stealth script
+ if err := chromedp.Run(browserCtx, chromedp.ActionFunc(func(ctx context.Context) error {
+ return injectedScript(ctx, seededScript)
+ })); err != nil {
+ cancel()
+ allocCancel()
+ return nil, nil, fmt.Errorf("failed to inject stealth script: %w", err)
+ }
+
+ return browserCtx, func() {
+ cancel()
+ allocCancel()
+ }, nil
+}
+
+// injectedScript injects stealth code into the browser
+func injectedScript(ctx context.Context, script string) error {
+ // This is a placeholder - actual implementation would use chromedp
+ // to evaluate the script in the browser context
+ return nil
+}
+
+// randomWindowSize returns a random common window size
+func randomWindowSize() (int, int) {
+ sizes := [][2]int{
+ {1920, 1080}, {1366, 768}, {1536, 864}, {1440, 900},
+ {1280, 720}, {1600, 900}, {2560, 1440}, {1280, 800},
+ }
+ s := sizes[rand.Intn(len(sizes))]
+ return s[0], s[1]
+}