High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
feat: integrate PortAllocator into orchestrator Launch() (Phase 3)
Mario01d601e3a6e3
diff --git a/cmd/pinchtab/cmd_dashboard.go b/cmd/pinchtab/cmd_dashboard.goindex f3ec8b77..99ce626d 100644--- a/cmd/pinchtab/cmd_dashboard.go+++ b/cmd/pinchtab/cmd_dashboard.go@@ -116,9 +116,7 @@ func runDashboard(cfg *config.RuntimeConfig) {if autoLaunch {defaultProfile := os.Getenv("PINCHTAB_DEFAULT_PROFILE")defaultPort := os.Getenv("PINCHTAB_DEFAULT_PORT")- if defaultPort == "" {- defaultPort = "9867"- }+ // defaultPort is optional - will auto-allocate if not specifiedif defaultProfile == "" {defaultProfile = "default"}@@ -135,7 +133,7 @@ func runDashboard(cfg *config.RuntimeConfig) {slog.Warn("auto-launch failed", "err", err)return}- slog.Info("auto-launched default instance", "id", inst.ID, "port", defaultPort, "headless", headlessDefault)+ slog.Info("auto-launched default instance", "id", inst.ID, "port", inst.Port, "headless", headlessDefault)}()} else {slog.Info("dashboard auto-launch disabled", "hint", "set PINCHTAB_AUTO_LAUNCH=1 to enable")diff --git a/internal/orchestrator/handlers.go b/internal/orchestrator/handlers.goindex fb356396..b9ab2b45 100644--- a/internal/orchestrator/handlers.go+++ b/internal/orchestrator/handlers.go@@ -59,13 +59,11 @@ func (o *Orchestrator) handleStartByID(w http.ResponseWriter, r *http.Request) {}var req struct {- Port string `json:"port"`+ Port string `json:"port,omitempty"`Headless bool `json:"headless"`}_ = json.NewDecoder(r.Body).Decode(&req)- if req.Port == "" {- req.Port = "0"- }+ // Port is optional - if not provided, Launch() will auto-allocateinst, err := o.Launch(name, req.Port, req.Headless)if err != nil {@@ -92,17 +90,18 @@ func (o *Orchestrator) handleStopByID(w http.ResponseWriter, r *http.Request) {func (o *Orchestrator) handleLaunchByName(w http.ResponseWriter, r *http.Request) {var req struct {Name string `json:"name"`- Port string `json:"port"`+ Port string `json:"port,omitempty"`Headless bool `json:"headless"`}if err := json.NewDecoder(r.Body).Decode(&req); err != nil {web.Error(w, 400, fmt.Errorf("invalid JSON"))return}- if req.Name == "" || req.Port == "" {- web.Error(w, 400, fmt.Errorf("name and port required"))+ if req.Name == "" {+ web.Error(w, 400, fmt.Errorf("name is required"))return}+ // Port is optional - if not provided, Launch() will auto-allocateinst, err := o.Launch(req.Name, req.Port, req.Headless)if err != nil {diff --git a/internal/orchestrator/orchestrator.go b/internal/orchestrator/orchestrator.goindex 29713e6b..7bafd421 100644--- a/internal/orchestrator/orchestrator.go+++ b/internal/orchestrator/orchestrator.go@@ -8,6 +8,7 @@ import ("net/http""os""path/filepath"+ "strconv""strings""sync""time"@@ -108,25 +109,41 @@ func installStableBinary(src, dst string) error {func (o *Orchestrator) Launch(name, port string, headless bool) (*bridge.Instance, error) {o.mu.Lock()- defer o.mu.Unlock()++ // Auto-allocate port if not specified+ if port == "" || port == "0" {+ o.mu.Unlock()+ allocatedPort, err := o.portAllocator.AllocatePort()+ if err != nil {+ return nil, fmt.Errorf("failed to allocate port: %w", err)+ }+ port = fmt.Sprintf("%d", allocatedPort)+ o.mu.Lock()+ }for _, inst := range o.instances {if inst.Port == port && instanceIsActive(inst) {+ o.mu.Unlock()return nil, fmt.Errorf("port %s already in use by instance %q", port, inst.Name)}if inst.Name == name && instanceIsActive(inst) {+ o.mu.Unlock()return nil, fmt.Errorf("profile %q already has an active instance (%s)", name, inst.Status)}}if !o.runner.IsPortAvailable(port) {+ o.mu.Unlock()return nil, fmt.Errorf("port %s is already in use on this machine", port)}id := fmt.Sprintf("%s-%s", name, port)if inst, ok := o.instances[id]; ok && inst.Status == "running" {+ o.mu.Unlock()return nil, fmt.Errorf("instance %q already running", id)}+ o.mu.Unlock()+profilePath := filepath.Join(o.baseDir, name)if err := os.MkdirAll(filepath.Join(profilePath, "Default"), 0755); err != nil {return nil, fmt.Errorf("create profile dir: %w", err)@@ -172,7 +189,9 @@ func (o *Orchestrator) Launch(name, port string, headless bool) (*bridge.InstanclogBuf: logBuf,}+ o.mu.Lock()o.instances[id] = inst+ o.mu.Unlock()go o.monitor(inst)@@ -273,6 +292,12 @@ func (o *Orchestrator) markStopped(id string) {defer o.mu.Unlock()if inst, ok := o.instances[id]; ok {inst.Status = "stopped"+ // Release the port back to the allocator+ portStr := inst.Port+ if portInt, err := strconv.Atoi(portStr); err == nil {+ o.portAllocator.ReleasePort(portInt)+ slog.Debug("released port", "id", id, "port", portStr)+ }}}