Skip to content

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

3.1 KB
Raw Blame
package orchestrator

import (
	"fmt"
	"net/http"

	"github.com/pinchtab/pinchtab/internal/authn"
	"github.com/pinchtab/pinchtab/internal/bridge"
	"github.com/pinchtab/pinchtab/internal/httpx"
)

func (o *Orchestrator) resolveProfileName(idOrName string) (string, error) {
	if o.profiles == nil {
		return "", fmt.Errorf("profile manager not configured")
	}
	if name, err := o.profiles.FindByID(idOrName); err == nil {
		return name, nil
	}
	if o.profiles.Exists(idOrName) {
		return idOrName, nil
	}
	return "", fmt.Errorf("profile %q not found", idOrName)
}

func (o *Orchestrator) handleStartByID(w http.ResponseWriter, r *http.Request) {
	id := r.PathValue("id")
	name, err := o.resolveProfileName(id)
	if err != nil {
		httpx.Error(w, 404, err)
		return
	}

	var req struct {
		Port            string                 `json:"port,omitempty"`
		Headless        bool                   `json:"headless"`
		SecurityPolicy  *bridge.SecurityPolicy `json:"securityPolicy,omitempty"`
		Browser         string                 `json:"browser,omitempty"`
		FallbackTargets []string               `json:"fallbackTargets,omitempty"`
	}
	if err := httpx.DecodeOptionalJSONBody(w, r, 0, &req); err != nil {
		httpx.Error(w, httpx.StatusForJSONDecodeError(err), err)
		return
	}
	if err := validateStartInstanceSecurityPolicy(req.SecurityPolicy); err != nil {
		httpx.Error(w, 400, err)
		return
	}

	inst, err := o.LaunchWithTargetSelection(name, req.Port, req.Headless, req.Browser, req.FallbackTargets, LaunchOptions{
		SecurityPolicy: req.SecurityPolicy,
		Browser:        req.Browser,
	})
	if err != nil {
		writeLaunchError(w, err)
		return
	}
	authn.AuditLog(r, "instance.started", "profileId", id, "profileName", name, "instanceId", inst.ID)
	httpx.JSON(w, 201, inst)
}

func (o *Orchestrator) handleStopByID(w http.ResponseWriter, r *http.Request) {
	id := r.PathValue("id")
	name, err := o.resolveProfileName(id)
	if err != nil {
		httpx.Error(w, 404, err)
		return
	}
	if err := o.StopProfile(name); err != nil {
		httpx.Error(w, 404, err)
		return
	}
	authn.AuditLog(r, "instance.stopped", "profileId", id, "profileName", name)
	httpx.JSON(w, 200, map[string]string{"status": "stopped", "id": id, "name": name})
}

func (o *Orchestrator) handleProfileInstance(w http.ResponseWriter, r *http.Request) {
	id := r.PathValue("id")
	name, err := o.resolveProfileName(id)
	if err != nil {
		httpx.JSON(w, 200, map[string]any{
			"name":    id,
			"exists":  false,
			"running": false,
			"status":  "missing",
			"port":    "",
			"message": fmt.Sprintf("Profile %q does not exist. Creating and authenticating a reusable profile is a human setup step.", id),
		})
		return
	}

	instances := o.List()
	for _, inst := range instances {
		if inst.ProfileName == name && (inst.Status == "running" || inst.Status == "starting") {
			httpx.JSON(w, 200, map[string]any{
				"name":    name,
				"exists":  true,
				"running": inst.Status == "running",
				"status":  inst.Status,
				"port":    inst.Port,
				"id":      inst.ID,
			})
			return
		}
	}
	httpx.JSON(w, 200, map[string]any{
		"name":    name,
		"exists":  true,
		"running": false,
		"status":  "stopped",
		"port":    "",
	})
}