Skip to content

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

feat: add BRIDGE_USER_AGENT with CDP Sec-Ch-Ua override (#17)

shural7b3140026d32
diff --git a/cmd/pinchtab/browser.go b/cmd/pinchtab/browser.go
index e2096f50..70658557 100644
--- a/cmd/pinchtab/browser.go
+++ b/cmd/pinchtab/browser.go
@@ -19,6 +19,7 @@ import (
"github.com/chromedp/chromedp"
"github.com/pinchtab/pinchtab/internal/bridge"
"github.com/pinchtab/pinchtab/internal/config"
+ "github.com/pinchtab/pinchtab/internal/uameta"
)
func setupAllocator(cfg *config.RuntimeConfig) (context.Context, context.CancelFunc, []chromedp.ExecAllocatorOption) {
@@ -238,3 +239,19 @@ func applyTimezone(browserCtx context.Context, cfg *config.RuntimeConfig) {
slog.Info("timezone override", "tz", cfg.Timezone)
}
}
+
+func applyUserAgentOverride(browserCtx context.Context, cfg *config.RuntimeConfig) {
+ override := uameta.Build(cfg.UserAgent, cfg.ChromeVersion)
+ if override == nil {
+ return
+ }
+ if err := chromedp.Run(browserCtx,
+ chromedp.ActionFunc(func(ctx context.Context) error {
+ return override.Do(ctx)
+ }),
+ ); err != nil {
+ slog.Warn("user-agent override failed", "err", err)
+ } else {
+ slog.Info("user-agent override applied")
+ }
+}
diff --git a/cmd/pinchtab/main.go b/cmd/pinchtab/main.go
index 375aa143..cd48a99f 100644
--- a/cmd/pinchtab/main.go
+++ b/cmd/pinchtab/main.go
@@ -100,6 +100,7 @@ func main() {
defer browserCancel()
applyTimezone(browserCtx, cfg)
+ applyUserAgentOverride(browserCtx, cfg)
b := bridge.New(allocCtx, browserCtx, cfg)
b.StealthScript = seededScript
diff --git a/internal/bridge/bridge.go b/internal/bridge/bridge.go
index d9af28af..9b871c53 100644
--- a/internal/bridge/bridge.go
+++ b/internal/bridge/bridge.go
@@ -9,6 +9,7 @@ import (
"github.com/chromedp/cdproto/page"
"github.com/chromedp/chromedp"
"github.com/pinchtab/pinchtab/internal/config"
+ "github.com/pinchtab/pinchtab/internal/uameta"
)
type TabEntry struct {
@@ -60,6 +61,13 @@ func (b *Bridge) injectStealth(ctx context.Context) {
}
func (b *Bridge) tabSetup(ctx context.Context) {
+ if override := uameta.Build(b.Config.UserAgent, b.Config.ChromeVersion); override != nil {
+ if err := chromedp.Run(ctx, chromedp.ActionFunc(func(c context.Context) error {
+ return override.Do(c)
+ })); err != nil {
+ slog.Warn("ua override failed on tab setup", "err", err)
+ }
+ }
b.injectStealth(ctx)
if b.Config.NoAnimations {
b.InjectNoAnimations(ctx)
diff --git a/internal/config/config.go b/internal/config/config.go
index 39db7d29..d2629a1d 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -26,6 +26,7 @@ type RuntimeConfig struct {
MaxTabs int
ChromeBinary string
ChromeExtraFlags string
+ UserAgent string
NoAnimations bool
StealthLevel string
ActionTimeout time.Duration
@@ -107,6 +108,7 @@ func Load() *RuntimeConfig {
MaxTabs: envIntOr("BRIDGE_MAX_TABS", 20),
ChromeBinary: os.Getenv("CHROME_BINARY"),
ChromeExtraFlags: os.Getenv("CHROME_FLAGS"),
+ UserAgent: os.Getenv("BRIDGE_USER_AGENT"),
NoAnimations: os.Getenv("BRIDGE_NO_ANIMATIONS") == "true",
StealthLevel: envOr("BRIDGE_STEALTH", "light"),
ActionTimeout: 15 * time.Second,
diff --git a/internal/uameta/uameta.go b/internal/uameta/uameta.go
new file mode 100644
index 00000000..7bfa0489
--- /dev/null
+++ b/internal/uameta/uameta.go
@@ -0,0 +1,86 @@
+// Package uameta builds CDP UserAgentMetadata from config.
+package uameta
+
+import (
+ "runtime"
+ "strings"
+
+ "github.com/chromedp/cdproto/emulation"
+)
+
+// Build creates a SetUserAgentOverride action with full UserAgentMetadata.
+// chromeVersion should be the full version (e.g. "144.0.7559.133").
+// If empty, returns nil.
+func Build(userAgent, chromeVersion string) *emulation.SetUserAgentOverrideParams {
+ if userAgent == "" {
+ return nil
+ }
+
+ major := chromeVersion
+ if i := strings.Index(chromeVersion, "."); i > 0 {
+ major = chromeVersion[:i]
+ }
+
+ platform, arch := detectPlatform()
+
+ return emulation.SetUserAgentOverride(userAgent).
+ WithAcceptLanguage("en-US,en").
+ WithPlatform(platform).
+ WithUserAgentMetadata(&emulation.UserAgentMetadata{
+ Platform: platformName(),
+ PlatformVersion: platformVersion(),
+ Architecture: arch,
+ Bitness: "64",
+ Mobile: false,
+ Brands: []*emulation.UserAgentBrandVersion{
+ {Brand: "Not(A:Brand", Version: "99"},
+ {Brand: "Google Chrome", Version: major},
+ {Brand: "Chromium", Version: major},
+ },
+ FullVersionList: []*emulation.UserAgentBrandVersion{
+ {Brand: "Not(A:Brand", Version: "99.0.0.0"},
+ {Brand: "Google Chrome", Version: chromeVersion},
+ {Brand: "Chromium", Version: chromeVersion},
+ },
+ })
+}
+
+func detectPlatform() (jsNavigatorPlatform, architecture string) {
+ switch runtime.GOARCH {
+ case "arm64":
+ architecture = "arm"
+ default:
+ architecture = "x86"
+ }
+
+ switch runtime.GOOS {
+ case "darwin":
+ return "MacIntel", architecture
+ case "windows":
+ return "Win32", architecture
+ default:
+ return "Linux x86_64", architecture
+ }
+}
+
+func platformName() string {
+ switch runtime.GOOS {
+ case "darwin":
+ return "macOS"
+ case "windows":
+ return "Windows"
+ default:
+ return "Linux"
+ }
+}
+
+func platformVersion() string {
+ switch runtime.GOOS {
+ case "darwin":
+ return "14.0.0"
+ case "windows":
+ return "15.0.0"
+ default:
+ return "6.5.0"
+ }
+}
diff --git a/internal/uameta/uameta_test.go b/internal/uameta/uameta_test.go
new file mode 100644
index 00000000..e580849f
--- /dev/null
+++ b/internal/uameta/uameta_test.go
@@ -0,0 +1,40 @@
+package uameta
+
+import (
+ "testing"
+)
+
+func TestBuild_Empty(t *testing.T) {
+ if Build("", "144.0.0.0") != nil {
+ t.Fatal("expected nil for empty user agent")
+ }
+}
+
+func TestBuild_Versions(t *testing.T) {
+ p := Build("Mozilla/5.0 Test", "144.0.7559.133")
+ if p == nil {
+ t.Fatal("expected non-nil")
+ }
+ meta := p.UserAgentMetadata
+ if meta == nil {
+ t.Fatal("expected metadata")
+ }
+ // Check major version extraction
+ for _, b := range meta.Brands {
+ if b.Brand == "Google Chrome" && b.Version != "144" {
+ t.Errorf("expected major version 144, got %s", b.Version)
+ }
+ }
+ for _, b := range meta.FullVersionList {
+ if b.Brand == "Google Chrome" && b.Version != "144.0.7559.133" {
+ t.Errorf("expected full version 144.0.7559.133, got %s", b.Version)
+ }
+ }
+}
+
+func TestDetectPlatform(t *testing.T) {
+ platform, arch := detectPlatform()
+ if platform == "" || arch == "" {
+ t.Fatal("expected non-empty platform and arch")
+ }
+}