High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
feat: add orchestrator proxy routes for instance operations (Phase 4)
Mario4eee00983672
diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.goindex 0a98ec62..c393fa30 100644--- a/internal/handlers/handlers.go+++ b/internal/handlers/handlers.go@@ -34,6 +34,7 @@ func (h *Handlers) ensureChrome() error {func (h *Handlers) RegisterRoutes(mux *http.ServeMux, doShutdown func()) {mux.HandleFunc("GET /health", h.HandleHealth)+ mux.HandleFunc("POST /ensure-chrome", h.HandleEnsureChrome)mux.HandleFunc("GET /tabs", h.HandleTabs)mux.HandleFunc("GET /snapshot", h.HandleSnapshot)mux.HandleFunc("GET /screenshot", h.HandleScreenshot)diff --git a/internal/handlers/health_tabs.go b/internal/handlers/health_tabs.goindex 0a6d5e19..90683742 100644--- a/internal/handlers/health_tabs.go+++ b/internal/handlers/health_tabs.go@@ -1,6 +1,7 @@package handlersimport (+ "fmt""net/http""time"@@ -16,6 +17,15 @@ func (h *Handlers) HandleHealth(w http.ResponseWriter, r *http.Request) {web.JSON(w, 200, map[string]any{"status": "ok", "tabs": len(targets), "cdp": h.Config.CdpURL})}+func (h *Handlers) HandleEnsureChrome(w http.ResponseWriter, r *http.Request) {+ // Ensure Chrome is initialized for this instance+ if err := h.ensureChrome(); err != nil {+ web.Error(w, 500, fmt.Errorf("chrome initialization failed: %w", err))+ return+ }+ web.JSON(w, 200, map[string]string{"status": "chrome_ready"})+}+func (h *Handlers) HandleTabs(w http.ResponseWriter, r *http.Request) {targets, err := h.Bridge.ListTargets()if err != nil {diff --git a/internal/orchestrator/handlers.go b/internal/orchestrator/handlers.goindex b9ab2b45..e6c14d10 100644--- a/internal/orchestrator/handlers.go+++ b/internal/orchestrator/handlers.go@@ -27,6 +27,33 @@ func (o *Orchestrator) RegisterHandlers(mux *http.ServeMux) {mux.HandleFunc("POST /instances/{id}/stop", o.handleStopByInstanceID)mux.HandleFunc("GET /instances/{id}/logs", o.handleLogsByID)mux.HandleFunc("GET /instances/{id}/proxy/screencast", o.handleProxyScreencast)++ // Instance proxy routes - forward to specific instance port+ // Browser operations+ mux.HandleFunc("POST /instances/{id}/navigate", o.proxyToInstance)+ mux.HandleFunc("GET /instances/{id}/snapshot", o.proxyToInstance)+ mux.HandleFunc("POST /instances/{id}/action", o.proxyToInstance)+ mux.HandleFunc("POST /instances/{id}/actions", o.proxyToInstance)+ mux.HandleFunc("GET /instances/{id}/screenshot", o.proxyToInstance)+ mux.HandleFunc("GET /instances/{id}/pdf", o.proxyToInstance)+ mux.HandleFunc("GET /instances/{id}/text", o.proxyToInstance)+ mux.HandleFunc("POST /instances/{id}/evaluate", o.proxyToInstance)+ mux.HandleFunc("GET /instances/{id}/tabs", o.proxyToInstance)++ // Chrome management+ mux.HandleFunc("POST /instances/{id}/ensure-chrome", o.proxyToInstance)++ // Tab management+ mux.HandleFunc("POST /instances/{id}/tab", o.proxyToInstance)+ mux.HandleFunc("POST /instances/{id}/tab/lock", o.proxyToInstance)+ mux.HandleFunc("POST /instances/{id}/tab/unlock", o.proxyToInstance)++ // Other operations+ mux.HandleFunc("GET /instances/{id}/cookies", o.proxyToInstance)+ mux.HandleFunc("POST /instances/{id}/cookies", o.proxyToInstance)+ mux.HandleFunc("GET /instances/{id}/download", o.proxyToInstance)+ mux.HandleFunc("POST /instances/{id}/upload", o.proxyToInstance)+ mux.HandleFunc("GET /instances/{id}/screencast", o.proxyToInstance)}func (o *Orchestrator) handleList(w http.ResponseWriter, r *http.Request) {@@ -184,3 +211,97 @@ func (o *Orchestrator) handleProxyScreencast(w http.ResponseWriter, r *http.RequtargetURL := fmt.Sprintf("ws://localhost:%s/screencast?tabId=%s", inst.Port, tabID)web.JSON(w, 200, map[string]string{"wsUrl": targetURL})}++// proxyToInstance forwards requests to a specific instance port+// This allows clients to call /instances/{id}/navigate instead of knowing the instance port+func (o *Orchestrator) proxyToInstance(w http.ResponseWriter, r *http.Request) {+ id := r.PathValue("id")++ o.mu.RLock()+ inst, ok := o.instances[id]+ o.mu.RUnlock()++ if !ok {+ web.Error(w, 404, fmt.Errorf("instance %q not found", id))+ return+ }++ if inst.Status != "running" {+ web.Error(w, 503, fmt.Errorf("instance %q is not running (status: %s)", id, inst.Status))+ return+ }++ // Build target URL by replacing /instances/{id} with the instance port path+ // Request: POST /instances/work-9868/navigate?url=...+ // Target: POST http://localhost:9868/navigate?url=...+ targetPath := r.URL.Path+ // Remove /instances/{id} prefix (19 + len(id) characters)+ if len(targetPath) > len("/instances/"+id) {+ targetPath = targetPath[len("/instances/"+id):]+ } else {+ targetPath = ""+ }++ targetURL := fmt.Sprintf("http://localhost:%s%s", inst.Port, targetPath)+ if r.URL.RawQuery != "" {+ targetURL += "?" + r.URL.RawQuery+ }++ // Create proxy request+ proxyReq, err := http.NewRequest(r.Method, targetURL, r.Body)+ if err != nil {+ web.Error(w, 500, fmt.Errorf("failed to create proxy request: %w", err))+ return+ }++ // Copy headers from original request (except Host and hop-by-hop headers)+ for key, values := range r.Header {+ switch key {+ case "Host", "Connection", "Keep-Alive", "Proxy-Authenticate",+ "Proxy-Authorization", "Te", "Trailers", "Transfer-Encoding", "Upgrade":+ // Skip hop-by-hop headers+ default:+ for _, value := range values {+ proxyReq.Header.Add(key, value)+ }+ }+ }++ // Use orchestrator's HTTP client+ resp, err := o.client.Do(proxyReq)+ if err != nil {+ web.Error(w, 502, fmt.Errorf("failed to proxy to instance: %w", err))+ return+ }+ defer resp.Body.Close()++ // Copy response headers+ for key, values := range resp.Header {+ for _, value := range values {+ w.Header().Add(key, value)+ }+ }++ // Copy response status and body+ w.WriteHeader(resp.StatusCode)+ _, _ = w.Write(readResponseBody(resp))+}++// readResponseBody reads the full response body+func readResponseBody(resp *http.Response) []byte {+ if resp.Body == nil {+ return []byte{}+ }+ body := make([]byte, 0)+ buf := make([]byte, 4096)+ for {+ n, err := resp.Body.Read(buf)+ if n > 0 {+ body = append(body, buf[:n]...)+ }+ if err != nil {+ break+ }+ }+ return body+}