Skip to content

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

feat: add /download endpoint — fetch files using browser session

Mario853777b9c4a8
diff --git a/cmd/pinchtab/cmd_dashboard.go b/cmd/pinchtab/cmd_dashboard.go
index b398ce9c..ebad25eb 100644
--- a/cmd/pinchtab/cmd_dashboard.go
+++ b/cmd/pinchtab/cmd_dashboard.go
@@ -58,7 +58,7 @@ func runDashboard(cfg *config.RuntimeConfig) {
"/tabs", "/snapshot", "/screenshot", "/text",
"/navigate", "/action", "/actions", "/evaluate",
"/tab", "/tab/lock", "/tab/unlock",
- "/cookies", "/stealth/status", "/fingerprint/rotate",
+ "/cookies", "/download", "/stealth/status", "/fingerprint/rotate",
"/screencast", "/screencast/tabs",
}
for _, ep := range proxyEndpoints {
diff --git a/internal/handlers/download.go b/internal/handlers/download.go
new file mode 100644
index 00000000..34fa6b92
--- /dev/null
+++ b/internal/handlers/download.go
@@ -0,0 +1,155 @@
+package handlers
+
+import (
+ "context"
+ "encoding/base64"
+ "fmt"
+ "net/http"
+ "os"
+ "path/filepath"
+ "time"
+
+ "github.com/chromedp/cdproto/network"
+ "github.com/chromedp/chromedp"
+ "github.com/pinchtab/pinchtab/internal/web"
+)
+
+// HandleDownload fetches a URL using the browser's session (cookies, stealth)
+// and returns the content. This preserves authentication and fingerprint.
+//
+// GET /download?url=<url>[&tabId=<id>][&output=file&path=/tmp/file][&raw=true]
+func (h *Handlers) HandleDownload(w http.ResponseWriter, r *http.Request) {
+ dlURL := r.URL.Query().Get("url")
+ if dlURL == "" {
+ web.Error(w, 400, fmt.Errorf("url parameter required"))
+ return
+ }
+
+ output := r.URL.Query().Get("output")
+ filePath := r.URL.Query().Get("path")
+ raw := r.URL.Query().Get("raw") == "true"
+
+ // Create a temporary tab for the download — avoids navigating the user's tab away.
+ browserCtx := h.Bridge.BrowserContext()
+ tabCtx, tabCancel := chromedp.NewContext(browserCtx)
+ defer tabCancel()
+
+ tCtx, tCancel := context.WithTimeout(tabCtx, 30*time.Second)
+ defer tCancel()
+ go web.CancelOnClientDone(r.Context(), tCancel)
+
+ // Enable network tracking to capture response metadata.
+ var requestID network.RequestID
+ var responseMIME string
+ var responseStatus int
+ done := make(chan struct{}, 1)
+
+ chromedp.ListenTarget(tCtx, func(ev interface{}) {
+ switch e := ev.(type) {
+ case *network.EventResponseReceived:
+ if e.Response.URL == dlURL && requestID == "" {
+ requestID = e.RequestID
+ responseMIME = e.Response.MimeType
+ responseStatus = int(e.Response.Status)
+ }
+ case *network.EventLoadingFinished:
+ if e.RequestID == requestID && requestID != "" {
+ select {
+ case done <- struct{}{}:
+ default:
+ }
+ }
+ case *network.EventLoadingFailed:
+ if e.RequestID == requestID && requestID != "" {
+ select {
+ case done <- struct{}{}:
+ default:
+ }
+ }
+ }
+ })
+
+ if err := chromedp.Run(tCtx, network.Enable()); err != nil {
+ web.Error(w, 500, fmt.Errorf("network enable: %w", err))
+ return
+ }
+
+ // Navigate the temp tab to the URL — uses browser's cookie jar and stealth.
+ if err := chromedp.Run(tCtx, chromedp.Navigate(dlURL)); err != nil {
+ web.Error(w, 502, fmt.Errorf("navigate to download URL: %w", err))
+ return
+ }
+
+ // Wait for response.
+ select {
+ case <-done:
+ case <-tCtx.Done():
+ web.Error(w, 504, fmt.Errorf("download timed out"))
+ return
+ }
+
+ if responseStatus >= 400 {
+ web.Error(w, 502, fmt.Errorf("remote server returned HTTP %d", responseStatus))
+ return
+ }
+
+ // Get response body via CDP.
+ var body []byte
+ if err := chromedp.Run(tCtx,
+ chromedp.ActionFunc(func(ctx context.Context) error {
+ b, err := network.GetResponseBody(requestID).Do(ctx)
+ if err != nil {
+ return err
+ }
+ body = b
+ return nil
+ }),
+ ); err != nil {
+ web.Error(w, 500, fmt.Errorf("get response body: %w", err))
+ return
+ }
+
+ if responseMIME == "" {
+ responseMIME = "application/octet-stream"
+ }
+
+ // Write to file.
+ if output == "file" {
+ if filePath == "" {
+ web.Error(w, 400, fmt.Errorf("path required when output=file"))
+ return
+ }
+ if err := os.MkdirAll(filepath.Dir(filePath), 0755); err != nil {
+ web.Error(w, 500, fmt.Errorf("failed to create directory: %w", err))
+ return
+ }
+ if err := os.WriteFile(filePath, body, 0644); err != nil {
+ web.Error(w, 500, fmt.Errorf("failed to write file: %w", err))
+ return
+ }
+ web.JSON(w, 200, map[string]any{
+ "status": "saved",
+ "path": filePath,
+ "size": len(body),
+ "contentType": responseMIME,
+ })
+ return
+ }
+
+ // Raw bytes.
+ if raw {
+ w.Header().Set("Content-Type", responseMIME)
+ w.Header().Set("Content-Length", fmt.Sprintf("%d", len(body)))
+ w.WriteHeader(200)
+ _, _ = w.Write(body)
+ return
+ }
+
+ // Default: base64 JSON response.
+ web.JSON(w, 200, map[string]any{
+ "data": base64.StdEncoding.EncodeToString(body),
+ "contentType": responseMIME,
+ "size": len(body),
+ "url": dlURL,
+ })
+}
diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go
index 0fca4299..4b8ca784 100644
--- a/internal/handlers/handlers.go
+++ b/internal/handlers/handlers.go
@@ -45,6 +45,7 @@ func (h *Handlers) RegisterRoutes(mux *http.ServeMux, doShutdown func()) {
mux.HandleFunc("POST /cookies", h.HandleSetCookies)
mux.HandleFunc("GET /stealth/status", h.HandleStealthStatus)
mux.HandleFunc("POST /fingerprint/rotate", h.HandleFingerprintRotate)
+ mux.HandleFunc("GET /download", h.HandleDownload)
mux.HandleFunc("GET /screencast", h.HandleScreencast)
mux.HandleFunc("GET /screencast/tabs", h.HandleScreencastAll)
mux.HandleFunc("GET /welcome", func(w http.ResponseWriter, r *http.Request) {
diff --git a/skill/pinchtab/SKILL.md b/skill/pinchtab/SKILL.md
index f2f869cd..b3e51e5d 100644
--- a/skill/pinchtab/SKILL.md
+++ b/skill/pinchtab/SKILL.md
@@ -371,6 +371,22 @@ curl "http://localhost:9867/text?mode=raw"
Returns `{url, title, text}`. Cheapest option (~1K tokens for most pages).
+### Download files
+
+```bash
+# Download using browser session (preserves cookies, auth, stealth)
+# Returns base64 JSON by default
+curl "http://localhost:9867/download?url=https://site.com/report.pdf"
+
+# Raw bytes (pipe to file)
+curl "http://localhost:9867/download?url=https://site.com/image.jpg&raw=true" -o image.jpg
+
+# Save directly to disk
+curl "http://localhost:9867/download?url=https://site.com/export.csv&output=file&path=/tmp/export.csv"
+```
+
+Key use case: downloading files from authenticated sites — the browser's cookies and stealth settings are used automatically. No need to extract cookies and use curl separately.
+
### Screenshot
```bash