Skip to content

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

fix: add bridge-only mode for spawned instances + fix navigate to always create tabs

Mario0814e3b1f605
diff --git a/cmd/pinchtab/cmd_bridge.go b/cmd/pinchtab/cmd_bridge.go
new file mode 100644
index 00000000..b34e4bb1
--- /dev/null
+++ b/cmd/pinchtab/cmd_bridge.go
@@ -0,0 +1,93 @@
+package main
+
+import (
+ "context"
+ "log/slog"
+ "net/http"
+ "os"
+ "os/signal"
+ "sync"
+ "syscall"
+ "time"
+
+ "github.com/pinchtab/pinchtab/internal/bridge"
+ "github.com/pinchtab/pinchtab/internal/config"
+ "github.com/pinchtab/pinchtab/internal/handlers"
+)
+
+// runBridgeServer starts a bridge server without orchestrator or dashboard
+// This is used for spawned instances by the orchestrator
+func runBridgeServer(cfg *config.RuntimeConfig) {
+ listenAddr := cfg.ListenAddr()
+ slog.Info("🦀 Pinchtab Bridge Server", "listen", listenAddr, "profile", cfg.ProfileDir)
+
+ // Create a bridge instance with lazy initialization
+ // Chrome will be initialized on first request via ensureChrome()
+ bridgeInstance := bridge.New(context.Background(), nil, cfg)
+
+ mux := http.NewServeMux()
+
+ // Register all bridge handlers
+ h := handlers.New(bridgeInstance, cfg, nil, nil, nil)
+ shutdownOnce := &sync.Once{}
+ doShutdown := func() {
+ shutdownOnce.Do(func() {
+ slog.Info("shutting down bridge server...")
+ })
+ }
+ h.RegisterRoutes(mux, doShutdown)
+
+ // HTTP server
+ server := &http.Server{
+ Addr: listenAddr,
+ Handler: loggingMiddleware(mux),
+ }
+
+ go func() {
+ slog.Info("bridge server listening", "addr", listenAddr)
+ if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
+ slog.Error("server error", "err", err)
+ os.Exit(1)
+ }
+ }()
+
+ // Graceful shutdown on signal
+ sigChan := make(chan os.Signal, 1)
+ signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
+ <-sigChan
+
+ doShutdown()
+
+ ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
+ defer cancel()
+ if err := server.Shutdown(ctx); err != nil {
+ slog.Error("shutdown error", "err", err)
+ }
+}
+
+// Simple middleware to log HTTP requests
+func loggingMiddleware(next http.Handler) http.Handler {
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ start := time.Now()
+ recorder := &statusRecorder{ResponseWriter: w, statusCode: 200}
+
+ next.ServeHTTP(recorder, r)
+
+ slog.Info("request",
+ "method", r.Method,
+ "path", r.URL.Path,
+ "status", recorder.statusCode,
+ "ms", time.Since(start).Milliseconds(),
+ )
+ })
+}
+
+type statusRecorder struct {
+ http.ResponseWriter
+ statusCode int
+}
+
+func (r *statusRecorder) WriteHeader(code int) {
+ r.statusCode = code
+ r.ResponseWriter.WriteHeader(code)
+}
diff --git a/cmd/pinchtab/main.go b/cmd/pinchtab/main.go
index 039fca56..148fb702 100644
--- a/cmd/pinchtab/main.go
+++ b/cmd/pinchtab/main.go
@@ -53,6 +53,12 @@ func main() {
return
}
+ // Check if running as bridge-only instance (spawned by orchestrator)
+ if os.Getenv("BRIDGE_ONLY") == "1" {
+ runBridgeServer(cfg)
+ return
+ }
+
// Default: run dashboard mode
// (includes 'pinchtab' with no args and unrecognized args like 'dashboard')
runDashboard(cfg)
diff --git a/internal/handlers/navigation.go b/internal/handlers/navigation.go
index 4683f007..0593e1df 100644
--- a/internal/handlers/navigation.go
+++ b/internal/handlers/navigation.go
@@ -41,6 +41,12 @@ func (h *Handlers) HandleNavigate(w http.ResponseWriter, r *http.Request) {
return
}
+ // Default to creating new tab (API design: /navigate always creates new tab)
+ // Unless explicitly reusing an existing tab by specifying TabID
+ if req.TabID == "" {
+ req.NewTab = true
+ }
+
titleWait := time.Duration(0)
if req.WaitTitle > 0 {
if req.WaitTitle > 30 {
diff --git a/internal/orchestrator/orchestrator.go b/internal/orchestrator/orchestrator.go
index 90f2df7f..6ee81ec9 100644
--- a/internal/orchestrator/orchestrator.go
+++ b/internal/orchestrator/orchestrator.go
@@ -170,6 +170,7 @@ func (o *Orchestrator) Launch(name, port string, headless bool) (*bridge.Instanc
"BRIDGE_STATE_DIR": instanceStateDir,
"BRIDGE_HEADLESS": headlessStr,
"BRIDGE_NO_RESTORE": "true",
+ "BRIDGE_ONLY": "1",
})
logBuf := newRingBuffer(64 * 1024)