High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
feat: 'pinchtab dashboard' subcommand — standalone dashboard mode
Marioc2d129b184eb
diff --git a/cmd_dashboard.go b/cmd_dashboard.gonew file mode 100644index 00000000..4091779f--- /dev/null+++ b/cmd_dashboard.go@@ -0,0 +1,224 @@+package main++import (+ "context"+ "fmt"+ "log/slog"+ "net/http"+ "os"+ "os/signal"+ "path/filepath"+ "strings"+ "sync"+ "syscall"+ "time"+)++// runDashboard starts a lightweight dashboard server — no Chrome, no bridge.+// It manages Pinchtab instances via the orchestrator and serves the dashboard UI.+func runDashboard() {+ dashPort := port // reuse --port / BRIDGE_PORT+ if dashPort == "" {+ dashPort = "9870"+ }++ slog.Info("🦀 Pinchtab Dashboard", "port", dashPort)++ profilesDir := filepath.Join(stateDir, "profiles")+ os.MkdirAll(profilesDir, 0755)++ profMgr := NewProfileManager(profilesDir)+ dashboard := NewDashboard(nil)+ orchestrator := NewOrchestrator(profilesDir)++ mux := http.NewServeMux()++ // Dashboard UI + SSE+ dashboard.RegisterHandlers(mux)+ orchestrator.RegisterHandlers(mux)+ profMgr.RegisterHandlers(mux)++ // Health endpoint for the dashboard itself+ mux.HandleFunc("GET /health", func(w http.ResponseWriter, r *http.Request) {+ jsonResp(w, 200, map[string]string{"status": "ok", "mode": "dashboard"})+ })++ // Proxy common bridge endpoints to the default instance+ // This lets the dashboard UI call /tabs, /screencast/tabs etc on the dashboard port+ proxyEndpoints := []string{+ "/tabs", "/snapshot", "/screenshot", "/text",+ "/navigate", "/action", "/actions", "/evaluate",+ "/tab", "/tab/lock", "/tab/unlock",+ "/cookies", "/stealth/status", "/fingerprint/rotate",+ "/screencast", "/screencast/tabs",+ }+ for _, ep := range proxyEndpoints {+ endpoint := ep // capture+ mux.HandleFunc(endpoint, func(w http.ResponseWriter, r *http.Request) {+ // Find first running instance and proxy to it+ instances := orchestrator.List()+ var target string+ for _, inst := range instances {+ if inst.Status == "running" {+ target = inst.URL+ break+ }+ }+ if target == "" {+ jsonErr(w, 503, fmt.Errorf("no running instances — launch one from the Profiles tab"))+ return+ }+ proxyRequest(w, r, target+endpoint)+ })+ }++ // Profile observer+ profileObserver := func(evt AgentEvent) {+ if evt.Profile != "" {+ profMgr.tracker.Record(evt.Profile, ActionRecord{+ Timestamp: evt.Timestamp,+ Method: strings.SplitN(evt.Action, " ", 2)[0],+ Endpoint: strings.SplitN(evt.Action, " ", 2)[1],+ URL: evt.URL,+ TabID: evt.TabID,+ DurationMs: evt.DurationMs,+ Status: evt.Status,+ })+ }+ }++ handler := dashboard.TrackingMiddleware(+ []EventObserver{profileObserver},+ loggingMiddleware(corsMiddleware(authMiddleware(mux))),+ )++ srv := &http.Server{Addr: ":" + dashPort, Handler: handler}++ // Auto-launch default instance if configured+ defaultProfile := os.Getenv("PINCHTAB_DEFAULT_PROFILE")+ defaultPort := os.Getenv("PINCHTAB_DEFAULT_PORT")+ if defaultPort == "" {+ defaultPort = "9867"+ }+ if defaultProfile == "" {+ defaultProfile = "default"+ }++ // Ensure default profile exists+ os.MkdirAll(filepath.Join(profilesDir, defaultProfile, "Default"), 0755)++ autoLaunch := os.Getenv("PINCHTAB_NO_AUTO_LAUNCH") == ""+ if autoLaunch {+ go func() {+ // Wait for server to be ready+ time.Sleep(500 * time.Millisecond)+ headlessDefault := os.Getenv("PINCHTAB_HEADED") == ""+ inst, err := orchestrator.Launch(defaultProfile, defaultPort, headlessDefault)+ if err != nil {+ slog.Warn("auto-launch failed", "err", err)+ return+ }+ slog.Info("auto-launched default instance", "id", inst.ID, "port", defaultPort, "headless", headlessDefault)+ }()+ }++ // Shutdown+ shutdownOnce := &sync.Once{}+ doShutdown := func() {+ shutdownOnce.Do(func() {+ slog.Info("shutting down dashboard...")+ dashboard.Shutdown()+ orchestrator.Shutdown()+ ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)+ defer cancel()+ srv.Shutdown(ctx)+ })+ }++ mux.HandleFunc("POST /shutdown", func(w http.ResponseWriter, r *http.Request) {+ jsonResp(w, 200, map[string]string{"status": "shutting down"})+ go doShutdown()+ })++ go func() {+ sig := make(chan os.Signal, 1)+ signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)+ <-sig+ doShutdown()+ }()++ slog.Info("dashboard ready", "url", fmt.Sprintf("http://localhost:%s/dashboard", dashPort))++ if err := srv.ListenAndServe(); err != http.ErrServerClosed {+ slog.Error("server", "err", err)+ os.Exit(1)+ }+}++// proxyRequest forwards an HTTP request to a target URL.+// For WebSocket upgrades (screencast), it does a WebSocket proxy.+func proxyRequest(w http.ResponseWriter, r *http.Request, targetURL string) {+ // Preserve query params+ if r.URL.RawQuery != "" {+ targetURL += "?" + r.URL.RawQuery+ }++ // Check if this is a WebSocket upgrade (screencast)+ if isWebSocketUpgrade(r) {+ proxyWebSocket(w, r, targetURL)+ return+ }++ // Regular HTTP proxy+ client := &http.Client{Timeout: 30 * time.Second}+ proxyReq, err := http.NewRequestWithContext(r.Context(), r.Method, targetURL, r.Body)+ if err != nil {+ jsonErr(w, 502, fmt.Errorf("proxy error: %w", err))+ return+ }++ // Forward headers+ for k, vv := range r.Header {+ for _, v := range vv {+ proxyReq.Header.Add(k, v)+ }+ }++ resp, err := client.Do(proxyReq)+ if err != nil {+ jsonErr(w, 502, fmt.Errorf("instance unreachable: %w", err))+ return+ }+ defer resp.Body.Close()++ // Copy response+ for k, vv := range resp.Header {+ for _, v := range vv {+ w.Header().Add(k, v)+ }+ }+ w.WriteHeader(resp.StatusCode)++ buf := make([]byte, 32*1024)+ for {+ n, err := resp.Body.Read(buf)+ if n > 0 {+ w.Write(buf[:n])+ if f, ok := w.(http.Flusher); ok {+ f.Flush()+ }+ }+ if err != nil {+ break+ }+ }+}++func isWebSocketUpgrade(r *http.Request) bool {+ for _, v := range r.Header["Upgrade"] {+ if strings.EqualFold(v, "websocket") {+ return true+ }+ }+ return false+}diff --git a/dashboard/profiles.js b/dashboard/profiles.jsindex eec9bdfd..029a978f 100644--- a/dashboard/profiles.js+++ b/dashboard/profiles.js@@ -37,7 +37,10 @@ async function loadProfiles() {const grid = document.getElementById('profiles-grid');const cards = [];- cards.push(renderMainCard(tabsData.tabs ? tabsData.tabs.length : 0));+ // Only show Main card if we're running in embedded mode (bridge serves /health with no "mode")+ if (!health.mode) {+ cards.push(renderMainCard(tabsData.tabs ? tabsData.tabs.length : 0));+ }profiles.forEach(p => {cards.push(renderProfileCard(p.name, p.sizeMB, p.source, running[p.name] || null));diff --git a/main.go b/main.goindex 30c9b5a0..5cc156ad 100644--- a/main.go+++ b/main.go@@ -43,6 +43,12 @@ func main() {os.Exit(0)}+ // Dashboard-only mode — no Chrome, just UI + orchestrator+ if len(os.Args) > 1 && os.Args[1] == "dashboard" {+ runDashboard()+ return+ }+if err := os.MkdirAll(stateDir, 0755); err != nil {slog.Error("cannot create state dir", "err", err)os.Exit(1)diff --git a/proxy_ws.go b/proxy_ws.gonew file mode 100644index 00000000..f3379eac--- /dev/null+++ b/proxy_ws.go@@ -0,0 +1,64 @@+package main++import (+ "io"+ "log/slog"+ "net"+ "net/http"+ "strings"+)++// proxyWebSocket does a raw TCP tunnel for WebSocket connections.+// This is the simplest approach — no frame parsing, just bidirectional copy.+func proxyWebSocket(w http.ResponseWriter, r *http.Request, targetURL string) {+ // Convert http(s) to ws+ wsTarget := strings.Replace(targetURL, "http://", "", 1)+ wsTarget = strings.Replace(wsTarget, "https://", "", 1)++ // Extract host:port and path+ host := wsTarget+ path := "/"+ if idx := strings.Index(wsTarget, "/"); idx >= 0 {+ host = wsTarget[:idx]+ path = wsTarget[idx:]+ }++ // Connect to target+ backend, err := net.Dial("tcp", host)+ if err != nil {+ http.Error(w, "backend unavailable", http.StatusBadGateway)+ slog.Error("ws proxy: backend dial failed", "target", host, "err", err)+ return+ }+ defer backend.Close()++ // Hijack the client connection+ hj, ok := w.(http.Hijacker)+ if !ok {+ http.Error(w, "server doesn't support hijacking", http.StatusInternalServerError)+ return+ }+ client, _, err := hj.Hijack()+ if err != nil {+ slog.Error("ws proxy: hijack failed", "err", err)+ return+ }+ defer client.Close()++ // Forward the original HTTP upgrade request to backend+ reqLine := r.Method + " " + path + " HTTP/1.1\r\n"+ backend.Write([]byte(reqLine))+ backend.Write([]byte("Host: " + host + "\r\n"))+ for k, vv := range r.Header {+ for _, v := range vv {+ backend.Write([]byte(k + ": " + v + "\r\n"))+ }+ }+ backend.Write([]byte("\r\n"))++ // Bidirectional copy+ done := make(chan struct{}, 2)+ go func() { io.Copy(client, backend); done <- struct{}{} }()+ go func() { io.Copy(backend, client); done <- struct{}{} }()+ <-done+}